comparison scripts/java/org/octave/JDialogBox.java @ 15625:acf0addfc610

include Octave Forge java package in core Octave * scripts/java: New directory tree. * scripts/Makefile.am: Include java/module.mk. (JAR_FILES): New variable. (nobase_fcnfile_DATA): Include $(JAR_FILES) in the list. (all-local): Depend on $(JAR_FILES). (java/PKG_ADD, java_GEN_FCN_FILES, java/$(octave_dirstamp)): New rules. * libinterp/link-deps (LIBOCTINTERP_LINK_DEP): Include $(JAVA_LIBS) in the list. * dldfcn/__java__.h, dldfcn/__java__.cc: New files. * dldfcn/module-files (__java__.cc): New file description. * doc/interpreter/java.txi: New file. * doc/interpreter/octave.texi: Include java.texi. * doc/interpreter/java-images: New directory. * doc/interpreter/Makefile.am (JAVA_IMAGES): New variable. (IMAGES): Include $(JAVA_IMAGSES) in the list. (MUNGED_TEXI_SRC): Include java.texi in the list. * configure.ac: Check for Java libraries and tools. Include Java info in the summary message. * build-aux/common.mk (JAVA_CPPFLAGS, JAVA_LIBS): New variables. * NEWS: Update. * contributors.in: Include Martin Hepperle in the list.
author John W. Eaton <jwe@octave.org>
date Fri, 23 Nov 2012 15:29:13 -0500
parents
children 6e39fe7992d9
comparison
equal deleted inserted replaced
15624:550147454137 15625:acf0addfc610
1 /*
2 ** Copyright (C) 2010 Martin Hepperle
3 **
4 ** This program is free software; you can redistribute it and/or modify
5 ** it under the terms of the GNU General Public License as published by
6 ** the Free Software Foundation; either version 2 of the License, or
7 ** (at your option) any later version.
8 **
9 ** This program is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ** GNU General Public License for more details.
13 **
14 ** You should have received a copy of the GNU General Public License
15 ** along with this program; If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 package org.octave;
19
20 import java.net.*;
21 import java.util.*;
22
23 import java.awt.*;
24 import javax.swing.*;
25
26 /**
27 * <p>Implementation of various dialog box functions</p>
28 *
29 * <p>The following functions are provided for being called via
30 * Octave script files:</p>
31 * <ul>
32 * <li>errordlg</li>
33 * <li>helpdlg</li>
34 * <li>inputdlg</li>
35 * </ul>
36 *
37 * <p>Copyright (c) 2010 Martin Hepperle</p>
38 *
39 * @author Martin Hepperle
40 * @version 1.0
41 */
42 public class JDialogBox
43 {
44 public static final int CLOSE_OK = 1;
45 public static final int CLOSE_NO = 2;
46 public static final int CLOSE_CANCEL = 3;
47
48 // dialog type
49 public static int FLAG_LABEL = 1;
50 public static int FLAG_TEXT = 2;
51 public static int FLAG_INPUT = 4;
52 public static int FLAG_LIST = 8;
53 // icon selection
54 public static int FLAG_QUESTION = 32;
55 public static int FLAG_ERROR = 64;
56 public static int FLAG_WARNING = 128;
57 public static int FLAG_INFORMATION = 256;
58
59 public static int DLG_QUEST = FLAG_QUESTION | FLAG_TEXT;
60 public static int DLG_INPUT = FLAG_QUESTION | FLAG_INPUT;
61 public static int DLG_LIST = FLAG_QUESTION | FLAG_LIST;
62
63 private final static String m_OK = "OK";
64 private final static String m_Yes = "Yes";
65 private final static String m_No = "No";
66 private final static String m_Cancel = "Cancel";
67
68 private JButton m_ButtonNO;
69 private JButton m_ButtonOK;
70 private JButton m_ButtonCANCEL;
71 private JButton m_Focus;
72 private JTextArea m_TextField[];
73 private JList m_List;
74 private JFrame m_ParentFrame;
75 private int m_CloseMethod;
76
77 // ------------------------------------
78 // implementation of listdlg function
79 // ------------------------------------
80
81 /**
82 *
83 * @param listcell String[] - a array of strings, one for each list entry
84 * @param selmode String
85 * @param sizecell Object[]
86 * @param initialcell Object[]
87 * @param name String
88 * @param promptcell String[]
89 * @param okstring String
90 * @param cancelstring String
91 * @return String[]
92 */
93 public static int[] listdlg ( String[] listcell,
94 String selmode,
95 Object[] sizecell,
96 Object[] initialcell,
97 String name,
98 String[] promptcell,
99 String okstring,
100 String cancelstring )
101 {
102 JDialogBox d = new JDialogBox ();
103 d.genericdlg ( promptcell, listcell, name, selmode,
104 DLG_LIST,
105 sizecell, initialcell, okstring, null, cancelstring );
106 return ( d.getSelectedIndices () );
107 }
108
109
110 // ------------------------------------
111 // implementation of inputdlg function
112 // -------------------------------------
113
114 /**
115 * Implements a variation of the inputdlg function.
116 *
117 * @param prompt String[] - an array of text strings to be used as labels.
118 * @param title String - a text string to be used to label the dialog caption.
119 * @param RowsCols int - defines the width of the text fields in columns.
120 * @param defaults Object[] - an array of text strings or numbers to be
121 * placed into the text fields as default values.
122 * @return String[] - an array of text strings containing the content of the
123 * text fields.
124 */
125 public static String[] inputdlg ( String[] prompt,
126 String title,
127 Object[] RowsCols,
128 Object[] defaults )
129 {
130 JDialogBox d = new JDialogBox ();
131 d.genericdlg ( prompt, null, title, "on",
132 FLAG_INPUT | FLAG_QUESTION,
133 RowsCols, defaults, m_OK, null, m_Cancel );
134 return ( d.getInput () );
135 }
136
137
138 /**
139 * Extract the current content from the text fields of an inputdlg.
140 *
141 * @return String[] - the text contained in the fields of an inputdlg.
142 * null if the dialog was cancelled.
143 */
144 private String[] getInput ()
145 {
146 String s[] = null;
147
148 if ( m_CloseMethod == CLOSE_OK )
149 {
150 s = new String[m_TextField.length];
151
152 for ( int i = 0; i < s.length; i++ )
153 {
154 s[i] = new String ( m_TextField[i].getText () );
155 }
156 }
157
158 return ( s );
159 }
160
161
162 private String getResult ()
163 {
164 String s = null;
165
166 if ( m_CloseMethod == CLOSE_OK )
167 {
168 s = m_ButtonOK.getText ();
169 }
170 else if ( m_CloseMethod == CLOSE_CANCEL )
171 {
172 s = m_ButtonCANCEL.getText ();
173 }
174 else
175 {
176 s = m_ButtonNO.getText ();
177 }
178
179 return ( s );
180 }
181
182
183 /**
184 * Extract the current content from the text fields of an inputdlg.
185 *
186 * @return String[] - the text contained in the fields of an inputdlg.
187 * null if the dialog was cancelled.
188 */
189 private String[] getSelection ()
190 {
191 String s[] = null;
192
193 if ( m_CloseMethod == CLOSE_OK )
194 {
195 int selection[] = m_List.getSelectedIndices ();
196
197 s = new String[selection.length];
198
199 for ( int i = 0; i < s.length; i++ )
200 {
201
202 // s[i] = new String ( Integer.toString(selection[i]) );
203 s[i] = ( m_List.getSelectedValues ()[i] ).toString ();
204 }
205 }
206
207 return ( s );
208 }
209
210
211 private int[] getSelectedIndices ()
212 {
213 int s[] = null;
214
215 if ( m_CloseMethod == CLOSE_OK )
216 {
217 s = m_List.getSelectedIndices ();
218 for ( int i = 0; i < s.length; i++ )
219 {
220
221 // translate to 1 based indices
222 s[i] = s[i] + 1;
223 }
224 }
225
226 return ( s );
227 }
228
229
230 public void SelectAll ()
231 {
232 if ( null != m_List )
233 {
234 m_List.setSelectionInterval ( 0, m_List.getModel ().getSize () - 1 );
235 }
236 }
237
238
239 // -------------------------------------
240 // implementation of helpdlg function
241 // -------------------------------------
242
243 /**
244 * Implements a simple helpdlg with default text and caption. Not very useful.
245 *
246 * Octave > helpdlg('helpstring','title')
247 *
248 * Called via helpdlg.m.
249 *
250 * @param helpstring String - a message string to be presented to the user.
251 * The string can have embedded newline (\n) characters to break the message
252 * into multiple lines.
253 * @param title String - a text string to be used to label the dialog caption.
254 * @return int - always 1
255 */
256 public static int helpdlg ( String helpstring, String title )
257 {
258 JDialogBox d = new JDialogBox ();
259 String s[] = new String[1];
260 s[0] = helpstring;
261 return ( d.genericdlg ( s, null, title, "on",
262 FLAG_TEXT | FLAG_INFORMATION, null, null,
263 m_OK, null, m_Cancel ) );
264 }
265
266
267 // -------------------------------------
268 // implementation of emptydlg function
269 // -------------------------------------
270
271 /**
272 * Implements a simple helpdlg with default text and caption. Not very useful.
273 *
274 * Octave > emptydlg('messagestring','title')
275 *
276 * Called via dlgbox.m.
277 *
278 * @param messagestring String - a message string to be presented to the user.
279 * The string can have embedded newline (\n) characters to break the message
280 * into multiple lines.
281 * @param title String - a text string to be used to label the dialog caption.
282 * @return int - always 1
283 */
284 public static int emptydlg ( String helpstring, String title )
285 {
286 JDialogBox d = new JDialogBox ();
287 String s[] = new String[1];
288 s[0] = helpstring;
289 return ( d.genericdlg ( s, null, title, "on",
290 FLAG_TEXT, null, null,
291 m_OK, null, m_Cancel ) );
292 }
293
294
295 // -------------------------------------------
296 // implementation of questdlg related functions
297 // -------------------------------------------
298
299 /**
300 * Implements a simple questdlg with default text and caption. Not very useful.
301 *
302 * @param question String - the question to be presented
303 * @param title String - the caption
304 * @param options String[] - 'str1', 'str2', 'str3', 'default'
305 * @return String - the caption of the button pressed by the user
306 */
307 public static String questdlg ( String question,
308 String title,
309 String[] options )
310 {
311 JDialogBox d = new JDialogBox ();
312 String s[] = new String[1];
313 s[0] = question;
314 d.genericdlg ( s, options, title, "on",
315 DLG_QUEST, null, null,
316 options[0], options[1], options[2] );
317 return ( d.getResult () );
318 }
319
320
321 // -------------------------------------
322 // implementation of errordlg function
323 // -------------------------------------
324
325 /**
326 * Implements a simple errordlg with default text and caption. Not very useful.
327 *
328 * @param errorstring String - the error message to display.
329 * @param dlgname String - the caption of the dialog box.
330 * @return int - always 1
331 */
332 public static int errordlg ( String errorstring, String dlgname )
333 {
334 JDialogBox d = new JDialogBox ();
335 String s[] = new String[1];
336 s[0] = errorstring;
337 return ( d.genericdlg ( s, null, dlgname, "on", FLAG_TEXT | FLAG_ERROR,
338 null, null,
339 m_OK, null, m_Cancel ) );
340 }
341
342
343 // -------------------------------------------
344 // implementation of warndlg related functions
345 // -------------------------------------------
346
347 /**
348 * Implements a simple warndlg with default text and caption. Not very useful.
349 *
350 * Called via warndlg.m.
351 *
352 * @param errorstring String - the message to be presented to the user.
353 * @param dlgname String - the caption of the dialog box.
354 * @return int - always 1
355 */
356 public static int warndlg ( String errorstring, String dlgname )
357 {
358 JDialogBox d = new JDialogBox ();
359 String s[] = new String[1];
360 s[0] = errorstring;
361 return ( d.genericdlg ( s, null, dlgname, "on", FLAG_TEXT | FLAG_WARNING,
362 null, null,
363 m_OK, null, m_Cancel ) );
364 }
365
366
367 // -------------------------------------
368 // generic dlg function
369 // -------------------------------------
370 /**
371 * A generic dialog creation and display function.
372 *
373 * @param message String[]
374 * @param list String[]
375 * @param caption String
376 * @param on String
377 * @param flag int
378 * @param RowsCols Object[]
379 * @param defaults Object[]
380 * @param okstring String
381 * @param nostring String
382 * @param cancelstring String
383 * @return int
384 */
385 public int genericdlg ( String message[],
386 String list[],
387 String caption,
388 String on,
389 int flag,
390 Object[] RowsCols,
391 Object[] defaults,
392 String okstring,
393 String nostring,
394 String cancelstring )
395 {
396 TeXtranslator theTranslator = new TeXtranslator ();
397 setSystemLnF ( true );
398
399 caption = theTranslator.replace ( caption );
400
401 m_ButtonNO = null;
402 m_ButtonOK = null;
403 m_ButtonCANCEL = null;
404
405 // create a modal dialog with an empty frame as its parent
406 m_ParentFrame = new JFrame ();
407
408 // --- trick to bring dialog to the front
409 // In Windows, the dialog is not brought to the foreground, but hidden
410 // behind the Octave window as long as the parent frame is not visible.
411 // To avoid that the frame is visible, we move it outside of the screen.
412 m_ParentFrame.setBounds ( Toolkit.getDefaultToolkit ().getScreenSize ().
413 width + 100,
414 Toolkit.getDefaultToolkit ().getScreenSize ().
415 height + 100, 1, 1 );
416 m_ParentFrame.setVisible ( true );
417 //-- end of trick
418
419 JDialog dlg;
420 dlg = new JDialog ( m_ParentFrame );
421 dlg.setTitle ( caption );
422
423 dlg.setModal ( true );
424 dlg.setDefaultCloseOperation ( JDialog.DISPOSE_ON_CLOSE );
425
426 DlgListener theListener = new DlgListener ( this );
427
428 Container d = dlg.getContentPane ();
429 d.setLayout ( new BorderLayout ( 8, 8 ) );
430
431 // spacer
432 d.add ( new JLabel ( " " ), BorderLayout.NORTH );
433 d.add ( new JLabel ( " " ), BorderLayout.EAST );
434
435 JPanel p = new JPanel ();
436
437 if ( FLAG_LABEL == ( FLAG_LABEL & flag ) )
438 {
439 // a single line label
440 JLabel l = new JLabel ( theTranslator.replace ( message[0] ) );
441 p.add ( l );
442 }
443 else if ( FLAG_TEXT == ( FLAG_TEXT & flag ) )
444 {
445 String msg = theTranslator.replace ( message[0] );
446 // a multi-line text display for helpdlg
447 StringTokenizer st = new StringTokenizer ( msg, "\n" );
448
449 int nRows = ( null == RowsCols ) ? 1 :
450 Integer.parseInt ( RowsCols[0].toString () );
451 nRows = Math.max ( nRows, st.countTokens () );
452 int nCols = Math.max ( 1, msg.length () / nRows );
453
454 p.setLayout ( new GridLayout ( message.length, 1 ) );
455 JTextArea ta = new JTextArea ( msg, nRows, nCols );
456 ta.setEditable ( false );
457 ta.setFocusable ( false );
458 ta.setOpaque ( false );
459 // replace ugly monospaced font
460 ta.setFont ( p.getFont () );
461 p.add ( ta );
462 }
463 else if ( FLAG_INPUT == ( FLAG_INPUT & flag ) )
464 {
465 // a multi label/textfield entry dialog for inputdlg
466 GridBagConstraints gbc = new GridBagConstraints ();
467 gbc.insets.top = 4;
468 gbc.insets.left = 8;
469 gbc.gridx = 0;
470 gbc.anchor = GridBagConstraints.NORTHWEST;
471
472 p.setLayout ( new GridBagLayout () );
473 m_TextField = new JTextArea[message.length];
474
475 // default values
476 int nRows = 1;
477 int nCols = 10;
478
479 for ( int i = 0; i < message.length; i++ )
480 {
481 String msg = theTranslator.replace ( message[i] );
482 JLabel l = new JLabel ( msg );
483 l.setHorizontalAlignment ( Label.LEFT );
484 gbc.gridy = 2 * i;
485 p.add ( l, gbc );
486 /**
487 * @todo CHECK handling of RowsCols for inputdlg
488 */
489 if ( RowsCols != null )
490 {
491 if ( RowsCols.length == 2 * message.length )
492 {
493 nRows = Integer.parseInt ( RowsCols[i].toString () );
494 nCols = Integer.parseInt ( RowsCols[RowsCols.length / 2 +
495 i].toString () );
496 }
497 }
498
499 m_TextField[i] = new JTextArea ( "", Math.max ( nRows, 1 ), nCols );
500 // avoid resizing
501 m_TextField[i].setPreferredSize ( new Dimension ( Math.max ( nRows,
502 1 ), nCols ) );
503 m_TextField[i].setAutoscrolls ( false );
504 m_TextField[i].setFont ( p.getFont () );
505 m_TextField[i].setBorder ( new javax.swing.border.EtchedBorder () );
506 m_TextField[i].addKeyListener ( theListener );
507
508 gbc.gridy = 2 * i + 1;
509 p.add ( m_TextField[i], gbc );
510 }
511
512 if ( defaults != null )
513 {
514 if ( defaults.length == message.length )
515 {
516 for ( int i = 0; i < message.length; i++ )
517 {
518 String def = theTranslator.replace ( defaults[i].toString () );
519 m_TextField[i].setText ( def );
520 }
521 }
522 }
523 }
524 else if ( DLG_LIST == ( DLG_LIST & flag ) )
525 {
526 GridBagConstraints gbc = new GridBagConstraints ();
527 gbc.insets.top = 4;
528 gbc.insets.left = 8;
529 gbc.gridx = 0;
530 gbc.anchor = GridBagConstraints.NORTHWEST;
531
532 p.setLayout ( new GridBagLayout () );
533
534 for ( int i = 0; i < message.length; i++ )
535 {
536 // a single line label
537 String msg = theTranslator.replace ( message[i] );
538 JLabel l = new JLabel ( msg );
539 gbc.gridy = i;
540 p.add ( l, gbc );
541 }
542
543 String lst[] = new String[list.length];
544
545 for ( int i = 0; i < list.length; i++ )
546 {
547 lst[i] = theTranslator.replace ( list[i] );
548 }
549 m_List = new JList ( lst );
550
551 // replace ugly monospaced font
552 m_List.setFont ( p.getFont () );
553
554 m_List.setMinimumSize ( new Dimension ( Math.max ( 1,
555 Integer.parseInt ( RowsCols[0].toString () ) ),
556 Math.max ( 1,
557 Integer.parseInt ( RowsCols[1].toString () ) ) ) );
558 m_List.setPreferredSize ( new Dimension ( Math.max ( 1,
559 Integer.parseInt ( RowsCols[1].toString () ) ),
560 Math.max ( 1,
561 Integer.parseInt ( RowsCols[0].toString () ) ) ) );
562 m_List.setBorder ( new javax.swing.border.EtchedBorder () );
563
564 gbc.gridy = message.length;
565 gbc.fill = GridBagConstraints.HORIZONTAL;
566 p.add ( m_List, gbc );
567
568 if ( on.toLowerCase ().equals ( "single" ) )
569 {
570 // single selection list
571 m_List.setSelectionMode ( ListSelectionModel.SINGLE_SELECTION );
572
573 m_List.setSelectedIndex ( Integer.parseInt (
574 defaults[0].toString () ) - 1 );
575 }
576 else
577 {
578 // multiple selection possible
579 m_List.setSelectionMode ( ListSelectionModel.
580 MULTIPLE_INTERVAL_SELECTION );
581
582 int selection[] = new int[defaults.length];
583 for ( int i = 0; i < defaults.length; i++ )
584 {
585 selection[i] = Integer.parseInt ( defaults[i].toString () ) - 1;
586 }
587 m_List.setSelectedIndices ( selection );
588
589 JButton b = new JButton ( "Select All" );
590 b.setActionCommand ( "SELALL" );
591 b.addActionListener ( theListener );
592 gbc.gridy = message.length + 1;
593 gbc.fill = GridBagConstraints.HORIZONTAL;
594 p.add ( b, gbc );
595 }
596
597 }
598
599 // prepare icon, if any
600 String sIconFile = null;
601 String sIconResource = null;
602 Icon theIcon = null;
603
604 if ( FLAG_ERROR == ( FLAG_ERROR & flag ) )
605 {
606 sIconFile = "images/error.png";
607 // Java for Windows
608 sIconResource = "OptionPane.errorIcon";
609 // Java for Linux does not offer these standard icons...
610 }
611 else if ( FLAG_WARNING == ( FLAG_WARNING & flag ) )
612 {
613 sIconFile = "images/warning.png";
614 // Java for Windows
615 sIconResource = "OptionPane.warningIcon";
616 // Java for Linux does not offer these standard icons...
617 }
618 else if ( FLAG_QUESTION == ( FLAG_QUESTION & flag ) )
619 {
620 sIconFile = "images/question.png";
621 // Java for Windows
622 sIconResource = "OptionPane.questionIcon";
623 // Java for Linux does not offer these standard icons...
624 }
625 else if ( FLAG_INFORMATION == ( FLAG_INFORMATION & flag ) )
626 {
627 sIconFile = "images/information.png";
628 // Java for Windows
629 sIconResource = "OptionPane.informationIcon";
630 // Java for Linux does not offer these standard icons...
631 }
632
633 // first try to find the UIManager specific icon to fit look and feel
634 // Note: the Windows XP look and feel offers 50 icons.
635 if ( sIconResource != null )
636 {
637 UIDefaults df = UIManager.getLookAndFeelDefaults ();
638 theIcon = df.getIcon ( sIconResource );
639 }
640
641 // fallback on bitmap image resource if icon was not found
642 if ( theIcon == null &&
643 sIconFile != null )
644 {
645 URL theResource = JDialogBox.class.getResource ( sIconFile );
646 if ( theResource != null )
647 {
648 theIcon = new ImageIcon ( theResource );
649 }
650 }
651
652 if ( theIcon != null )
653 {
654 // dummy panel to provide space around icon
655 JPanel pi = new JPanel ( new GridLayout ( 1, 3 ) );
656 pi.add ( new JLabel () );
657 pi.add ( new JLabel ( theIcon ) );
658 pi.add ( new JLabel () );
659 d.add ( pi, BorderLayout.WEST );
660
661 // use Octave icon if available. otherwise use dialog icon
662 Icon theOctaveIcon = getOctaveIcon ();
663 prepareFrameIcon ( m_ParentFrame,
664 theOctaveIcon == null ? theIcon : theOctaveIcon );
665 }
666
667 d.add ( p, BorderLayout.CENTER );
668
669 // button bar (2 rows of 3 columns each
670
671 p = new JPanel ();
672 p.setLayout ( new java.awt.GridLayout ( 2, 3 ) );
673
674 // spacer row
675 p.add ( new JLabel () );
676 p.add ( new JLabel () );
677 p.add ( new JLabel () );
678
679 if ( DLG_QUEST == ( DLG_QUEST & flag ) )
680 {
681 // questdlg with empty option[2]: only two buttons
682 if ( nostring.length () < 1 )
683 {
684 // spacer: left
685 p.add ( new JLabel () );
686 }
687 }
688 else
689 {
690 // spacer: left
691 p.add ( new JLabel () );
692 }
693
694 m_ButtonOK = new JButton ( theTranslator.replace ( okstring ) );
695 m_ButtonOK.setActionCommand ( "OK" );
696 m_ButtonOK.addActionListener ( theListener );
697 m_Focus = m_ButtonOK;
698 p.add ( m_ButtonOK );
699
700 if ( DLG_QUEST == ( DLG_QUEST & flag ) )
701 {
702 // questdlg with empty option[2]: only two buttons
703 if ( nostring.length () > 01 )
704 {
705 // questdlg has three buttons
706 m_ButtonNO = new JButton ( theTranslator.replace ( nostring ) );
707 m_ButtonNO.setActionCommand ( "NO" );
708 m_ButtonNO.addActionListener ( theListener );
709 p.add ( m_ButtonNO );
710 if ( DLG_QUEST == ( DLG_QUEST & flag ) )
711 {
712 // select default button
713 if ( list[3].equals ( nostring ) )
714 {
715 m_Focus = m_ButtonNO;
716 }
717 }
718 }
719 }
720
721 if ( DLG_INPUT == ( DLG_INPUT & flag ) ||
722 DLG_LIST == ( DLG_LIST & flag ) ||
723 DLG_QUEST == ( DLG_QUEST & flag ) )
724 {
725 m_ButtonCANCEL = new JButton ( theTranslator.replace ( cancelstring ) );
726 m_ButtonCANCEL.setActionCommand ( "CANCEL" );
727 m_ButtonCANCEL.addActionListener ( theListener );
728 p.add ( m_ButtonCANCEL );
729 if ( DLG_QUEST == ( DLG_QUEST & flag ) )
730 {
731 // select default button
732 if ( list[3].equals ( cancelstring ) )
733 {
734 m_Focus = m_ButtonCANCEL;
735 }
736 }
737 }
738 else
739 {
740 // spacer: right
741 p.add ( new JLabel () );
742 }
743
744 d.add ( p, BorderLayout.SOUTH );
745 dlg.pack ();
746
747 dlg.addWindowListener ( theListener );
748
749 if ( on.equals ( "on" ) )
750 {
751 m_ParentFrame.setAlwaysOnTop ( true );
752 }
753
754 // center dialog on screen
755 Dimension dlgSize = dlg.getSize ();
756 Dimension screenSize = Toolkit.getDefaultToolkit ().getScreenSize ();
757
758 dlg.setLocation ( ( screenSize.width - dlgSize.width ) / 2,
759 ( screenSize.height - dlgSize.height ) / 2 );
760
761 dlg.setVisible ( true );
762 dlg.requestFocus ();
763
764 m_ParentFrame.setVisible ( false );
765
766 return ( 1 );
767 }
768
769
770 /**
771 *
772 * @return Icon - null if icon was not found
773 */
774 private Icon getOctaveIcon ()
775 {
776 Icon theIcon = null;
777 URL theResource = JDialogBox.class.getResource ( "images/octave.png" );
778 if ( theResource != null )
779 {
780 theIcon = new ImageIcon ( theResource );
781 }
782 return theIcon;
783 }
784
785
786 /**
787 * Replace the standard Java frame icon with an Octave Icon.
788 *
789 * @param theFrame Frame - the Frame to decorate
790 * @param theIcon Icon - the icon to use if octave icon is not found.
791 */
792 private void prepareFrameIcon ( Frame theFrame, Icon theIcon )
793 {
794 // prepare icon for upper left corner of Frame window
795 // maybe there is a simpler way to achieve this
796 int w = theIcon.getIconWidth ();
797 int h = theIcon.getIconHeight ();
798 // Frame must be made displayable by packing it for createImage() to succeed
799 theFrame.pack ();
800 Image theImage = theFrame.createImage ( w, h );
801 theIcon.paintIcon ( theFrame, theImage.getGraphics (), 0, 0 );
802 theFrame.setIconImage ( theImage );
803 }
804
805
806 /**
807 * Select Look and Feel
808 *
809 * @param bSystemLnF boolean - if true, the current systesm Look&Feel is used,
810 * otherwise the Swing/Metal cross platform Look&Feel is used.
811 */
812 private void setSystemLnF ( boolean bSystemLnF )
813 {
814 try
815 {
816 if ( bSystemLnF )
817 {
818 // switch from Swing LnF to local system LnF
819 UIManager.setLookAndFeel ( UIManager.
820 getSystemLookAndFeelClassName () );
821 }
822 else
823 {
824 // use Swing LnF
825 UIManager.setLookAndFeel ( UIManager.
826 getCrossPlatformLookAndFeelClassName () );
827 }
828 }
829 catch ( Exception exception )
830 {
831 exception.printStackTrace ();
832 }
833 }
834
835
836 /**
837 * Called when the dialog is closed. Allows for specific cleanup actions.
838 *
839 * @param closeMethod int - OctaveDialog.CLOSE_OK, OctaveDialog.CLOSE_CANCEL
840 */
841 public void closeDialog ( int closeMethod )
842 {
843 m_CloseMethod = closeMethod;
844 m_ParentFrame.dispose ();
845 }
846
847
848 public void setFocus ()
849 {
850 if ( null != m_Focus )
851 {
852 m_Focus.requestFocus ();
853 m_ParentFrame.getRootPane ().setDefaultButton ( m_Focus );
854 m_ParentFrame.setAlwaysOnTop ( true );
855 }
856 }
857
858
859 /**
860 * Tests the dialogs
861 *
862 * @param args String[] - not used.
863 */
864 public static void main ( String[] args )
865 {
866 TeXtranslator t = new TeXtranslator();
867
868 if(false)
869 {
870 // find out key names of icon UI resources
871 UIDefaults df = UIManager.getLookAndFeelDefaults ();
872
873 for ( Enumeration e = df.keys (); e.hasMoreElements (); )
874 {
875 String s = e.nextElement ().toString ();
876
877 if ( s.toLowerCase ().contains ( "icon" ) )
878 {
879 System.out.println ( s );
880 }
881 }
882 }
883
884 try
885 {
886 Class[] argTypes = new Class[1];
887 argTypes[0] = String.class;
888
889 java.lang.reflect.Constructor c = ClassHelper.findConstructor ( java.lang.StringBuffer.class,
890 argTypes );
891 Object argValues[] = new Object[1];
892 argValues[0] = new String("initial value");
893 Object sb = c.newInstance(argValues);
894 System.out.println(sb.toString());
895
896 ClassHelper.invokeMethod(sb,"append",argValues,argTypes);
897 System.out.println(sb.toString());
898
899 argValues = new Object[2];
900 argTypes = new Class[2];
901 argTypes[0] = Integer.class;
902 argTypes[1] = String.class;
903 argValues[0] = new Integer(0);
904 argValues[1] = new String("inserted");
905
906 ClassHelper.invokeMethod(sb,"insert",argValues,argTypes);
907 System.out.println(sb.toString());
908 }
909 catch ( Throwable e )
910 {}
911
912 if ( true )
913 {
914 return;
915 }
916
917 helpdlg ( "If you need help\nyou should ask for help\nif someone is around\notherwise you are on your own.",
918 "Information" );
919
920 String[] options = new String[4];
921 options[0] = "Yeah \\vartheta is too low";
922 options[1] = "Maybe";
923 options[2] = "Nay \\vartheta is too high";
924 options[3] = "Maybe";
925
926 System.out.println ( questdlg ( "Is it too cold?", "Temperature", options ) );
927
928 // test variants of errordlg
929 // does not affect layering of dialog
930 errordlg ( "Background error!", "Error" );
931
932 // test variants of helpdlg
933
934 // test variants of inputdlg
935 String prompt[] = new String[2];
936 prompt[0] = "Question 1";
937 prompt[1] = "Question 2";
938 String defaults[] = new String[2];
939 defaults[0] = "1.1";
940 defaults[1] = "2.2";
941 String title = "Enter values";
942
943 Integer rc[] = new Integer[2 * 2];
944 rc[0] = new Integer ( 1 );
945 rc[1] = new Integer ( 2 );
946 rc[2] = new Integer ( 10 );
947 rc[3] = new Integer ( 20 );
948
949 inputdlg ( prompt, title, rc, defaults );
950
951 String listcell[] = new String[4];
952 listcell[0] = "a \\alpha";
953 listcell[1] = "b \\beta";
954 listcell[2] = "c \\gamma";
955 listcell[3] = "d \\delta";
956
957 Integer size[] = new Integer[2];
958 size[0] = new Integer ( 80 );
959 size[1] = new Integer ( 100 );
960
961 Integer initial[] = new Integer[2];
962 initial[0] = new Integer ( 4 );
963 initial[1] = new Integer ( 2 );
964
965 String promptcell[] = new String[2];
966 promptcell[0] = "Select something";
967 promptcell[1] = "(or even more than one thing)";
968
969 int idx[] = listdlg ( listcell,
970 "Multiple",
971 size,
972 initial,
973 "name",
974 promptcell,
975 "okstring",
976 "cancelstring" );
977
978 if ( idx != null )
979 {
980 for ( int i = 0; i < idx.length; i++ )
981 {
982 System.out.println ( idx[i] );
983 }
984 }
985 }
986 }