KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > swingwtx > swing > JOptionPane


1 /*
2    SwingWT
3    Copyright(c)2003-2004, R. Rawson-Tetley
4
5    For more information on distributing and using this program, please
6    see the accompanying "COPYING" file.
7
8    Contact me by electronic mail: bobintetley@users.sourceforge.net
9
10    $Log: JOptionPane.java,v $
11    Revision 1.26 2004/04/30 23:18:26 dannaab
12    List selection support, misc bug fixes
13
14    Revision 1.25 2004/04/29 12:49:28 bobintetley
15    Additional JOptionePane constants, missing JTree methods and improved awt.Image support
16
17    Revision 1.24 2004/04/23 09:36:28 bobintetley
18    *** empty log message ***
19
20    Revision 1.23 2004/04/21 11:41:11 bobintetley
21    *** empty log message ***
22
23    Revision 1.22 2004/04/19 15:43:25 bobintetley
24    Missing (and incorrect) Dialog/JDialog constructors implemented
25
26    Revision 1.21 2004/04/19 12:49:37 bobintetley
27    JTaskTray implementation (and demo), along with Frame repaint fix
28
29    Revision 1.20 2004/03/23 09:58:58 bobintetley
30    SystemColor and FileDialog implementation, plus JOptionPane.showOptionDialog support
31
32    Revision 1.19 2004/01/26 10:57:45 bobintetley
33    HTML handling (throws it away - SWT can't do anything with it)
34
35    Revision 1.18 2004/01/23 08:23:51 bobintetley
36    Fix to Ok button in MessageBox for MacOSX users
37
38    Revision 1.17 2004/01/20 15:52:56 bobintetley
39    Code from an anonymous developer
40
41    Revision 1.16 2003/12/17 13:38:02 bobintetley
42    setSelectedValue(Obj, bool) support and JOptionPane.showInputDialog now uses JList
43
44    Revision 1.15 2003/12/17 13:20:59 bobintetley
45    Full showInputDialog support and JDialogs can now block correctly
46
47    Revision 1.14 2003/12/16 18:57:56 bobintetley
48    Thread safety
49
50    Revision 1.13 2003/12/16 18:42:38 bobintetley
51    More thread safety
52
53    Revision 1.12 2003/12/15 18:14:52 bobintetley
54    Additional showConfirmDialog signature
55
56    Revision 1.11 2003/12/15 18:12:22 bobintetley
57    Additional constants for YES_NO
58
59    Revision 1.10 2003/12/15 16:40:04 bobintetley
60    Core methods + skeleton JTableHeader/JScrollBar support
61
62    Revision 1.9 2003/12/14 09:13:38 bobintetley
63    Added CVS log to source headers
64
65 */

66
67
68 package swingwtx.swing;
69
70 import org.eclipse.swt.SWT;
71 import org.eclipse.swt.widgets.MessageBox;
72 import org.eclipse.swt.widgets.Shell;
73
74 import swingwt.awt.BorderLayout;
75 import swingwt.awt.Container;
76 import swingwt.awt.Dimension;
77 import swingwt.awt.Frame;
78 import swingwt.awt.Toolkit;
79 import swingwt.awt.event.ActionEvent;
80 import swingwt.awt.event.ActionListener;
81
82 public class JOptionPane extends JPanel {
83     
84     public final static int YES_OPTION = SWT.YES;
85     public final static int NO_OPTION = SWT.NO;
86     public final static int OK_OPTION = SWT.OK;
87     public final static int OK_CANCEL_OPTION = SWT.OK | SWT.CANCEL;
88     public final static int CLOSED_OPTION = SWT.CLOSE;
89     public final static int DEFAULT_OPTION = SWT.DEFAULT;
90     public final static int YES_NO_OPTION = SWT.YES | SWT.NO;
91     public final static int WARNING_MESSAGE = SWT.ICON_WARNING;
92     public final static int QUESTION_MESSAGE = SWT.ICON_QUESTION;
93     public final static int INFORMATION_MESSAGE = SWT.ICON_INFORMATION;
94     public final static int ERROR_MESSAGE = SWT.ICON_ERROR;
95     
96     protected static Object JavaDoc lastInputDialogReturnValue = null;
97     protected static int lastInputDialogReturnIndex = -1;
98     
99     private static int confRetVal = 0;
100     private static boolean jobFinished = false;
101     private static JFrame rootFrame = null;
102     
103     public static void setRootFrame(JFrame frame) {
104         rootFrame = frame;
105     }
106     
107     public static JFrame getRootFrame() {
108         return rootFrame;
109     }
110     
111     public static int showConfirmDialog(swingwt.awt.Container parent, Object JavaDoc message) {
112         return showConfirmDialog(parent, message, QUESTION_MESSAGE);
113     }
114     
115     public static int showConfirmDialog(final swingwt.awt.Container parent, final Object JavaDoc message,
116                                         final String JavaDoc title, final int buttons, final int messageType) {
117         confRetVal = 0;
118         SwingUtilities.invokeSync(new Runnable JavaDoc() {
119             public void run() {
120                 Shell container = getModalParent(parent);
121                 MessageBox m = new MessageBox(container, messageType | SWT.YES | SWT.NO);
122                 m.setMessage(getOptionPaneMessage(message));
123                 m.setText(title);
124                 confRetVal = m.open();
125                 switch (confRetVal) {
126                     case SWT.YES: confRetVal = YES_OPTION; break;
127                     case SWT.NO: confRetVal = NO_OPTION; break;
128                 }
129             }
130         });
131         return confRetVal;
132     }
133     
134     public static int showConfirmDialog(swingwt.awt.Container parent, Object JavaDoc message, String JavaDoc title, int messageType) {
135         return showConfirmDialog(parent, message, title, 0, messageType);
136     }
137     
138     public static int showConfirmDialog(final swingwt.awt.Container parent, final Object JavaDoc message, final int messageType) {
139         return showConfirmDialog(parent, message, "", messageType);
140     }
141     
142     public static void showMessageDialog(swingwt.awt.Container parent, Object JavaDoc message) {
143         showMessageDialog(parent, message, "Information", SWT.ICON_INFORMATION);
144     }
145     
146     /** FIXME: Where the fuck is the title? */
147     public static void showMessageDialog(final swingwt.awt.Container parent, final Object JavaDoc message, final String JavaDoc title, final int messageType) {
148         SwingUtilities.invokeAsync(new Runnable JavaDoc() {
149             public void run() {
150                 MessageBox m = new MessageBox(getModalParent(parent), messageType | SWT.OK);
151                 m.setMessage(getOptionPaneMessage(message));
152                 m.setText(title);
153                 m.open();
154             }
155         });
156     }
157     
158     public static String JavaDoc showInputDialog(swingwt.awt.Container parent, Object JavaDoc message) {
159         SwingStyleInputDialog dlg = new SwingStyleInputDialog(parent, getOptionPaneMessage(message), "Input", 0, null, null, null);
160         dlg.show();
161         if (lastInputDialogReturnValue == null)
162             return null;
163         else
164             return lastInputDialogReturnValue.toString();
165     }
166     public static String JavaDoc showInputDialog(swingwt.awt.Container parent, Object JavaDoc message, Object JavaDoc initialSelectionValue) {
167         SwingStyleInputDialog dlg = new SwingStyleInputDialog(parent, getOptionPaneMessage(message), "Input", 0, null, null, initialSelectionValue);
168         dlg.show();
169         if (lastInputDialogReturnValue == null)
170             return null;
171         else
172             return lastInputDialogReturnValue.toString();
173     }
174     public static String JavaDoc showInputDialog(swingwt.awt.Container parent, Object JavaDoc message, String JavaDoc title, int messageType) {
175         SwingStyleInputDialog dlg = new SwingStyleInputDialog(parent, getOptionPaneMessage(message), title, messageType, null, null, null);
176         dlg.show();
177         if (lastInputDialogReturnValue == null)
178             return null;
179         else
180             return lastInputDialogReturnValue.toString();
181     }
182     public static Object JavaDoc showInputDialog(final swingwt.awt.Container parent, final Object JavaDoc message, final String JavaDoc title, final int messageType, final Icon icon, final Object JavaDoc[] selectionValues, final Object JavaDoc initialSelectionValue) {
183         SwingUtilities.invokeSync(new Runnable JavaDoc() {
184             public void run() {
185                 SwingStyleInputDialog dlg = new SwingStyleInputDialog(parent, getOptionPaneMessage(message), title, messageType, icon, selectionValues, initialSelectionValue);
186                 dlg.show();
187             }
188         });
189         return lastInputDialogReturnValue;
190     }
191     
192     public static int showOptionDialog(final swingwt.awt.Container parent, final Object JavaDoc message, final String JavaDoc title, final int optionType, final int messageType,
193                                        final Icon icon, final Object JavaDoc[] options, final Object JavaDoc initialValue) {
194         SwingUtilities.invokeSync(new Runnable JavaDoc() {
195             public void run() {
196                 SwingStyleInputDialog dlg = new SwingStyleInputDialog(parent, getOptionPaneMessage(message), title, messageType, icon, options, initialValue);
197                 dlg.show();
198             }
199         });
200         return lastInputDialogReturnIndex;
201     }
202     
203     /**
204      * Handles Swing-style messages, strips HTML and converts
205      * String arrays to a single string
206      */

207     protected static String JavaDoc getOptionPaneMessage(Object JavaDoc message) {
208         String JavaDoc out = "";
209         // If we have an array, concatenate
210
if (message instanceof String JavaDoc[]) {
211             String JavaDoc[] msgs = (String JavaDoc[]) message;
212             for (int i = 0; i < msgs.length; i++) {
213                 out += msgs[i] + " ";
214             }
215         }
216         else
217             // Otherwise, just take the string content
218
out = message.toString();
219         
220         // Strip HTML
221
out = SwingWTUtils.removeHTML(out);
222         
223         return out;
224     }
225     
226     /**
227      * Ties up the current thread until a particular
228      * task is done.
229      */

230     private static void waitToFinish() {
231         while (!isJobFinished()) {
232             try {
233                 Thread.sleep(50);
234             }
235             catch(Exception JavaDoc e) {}
236         }
237     }
238         
239     protected static synchronized boolean isJobFinished() { return jobFinished; }
240     protected static synchronized void setJobFinished(boolean b) { jobFinished = b; }
241  
242     /** Determines from the object passed whether it should be
243      * the parent for the dialog.
244      * If the container is null, the root pane set by the developer is used -
245      * if that's null too, a shared frame is used (created if it doesn't exist)
246      * If the container is not a frame of some type, the containing window for it is used.
247      * THIS ROUTINE IS NOT THREAD SAFE - ONLY CALL FROM THE EVENT DISPATCH THREAD
248      */

249     private static Shell getModalParent(Container c) {
250         if (c == null) {
251             if (getRootFrame() == null)
252                 return getSharedFrame();
253             else
254                 return ((Shell) getRootFrame().getPeer());
255         }
256         if (!(c.composite instanceof Shell))
257             return c.composite.getShell();
258         else
259             return (Shell) c.composite;
260     }
261     private static Shell sharedFrame = null;
262     /**
263      * Invisible shell used as the parent for dialogs
264      * with a null container.
265      */

266     public static Shell getSharedFrame() {
267         if ( sharedFrame == null ) {
268             sharedFrame = new Shell(SwingWTUtils.getDisplay(), SWT.NO_TRIM);
269         }
270         return sharedFrame;
271     }
272     
273 }
274
275 /**
276  * Behaves like the input dialog used by Swing
277  */

278 class SwingStyleInputDialog extends JDialog {
279     
280     private boolean isTextEntry = false;
281     private JTextArea text = null;
282     private JList sel = null;
283     
284     public SwingStyleInputDialog(swingwt.awt.Component parent, Object JavaDoc message, String JavaDoc title, int messageType, Icon icon, Object JavaDoc[] selectionValues, Object JavaDoc initialSelectionValue) {
285         
286         super((Frame) null, title, true);
287         
288         // Is it a text entry? Must be if no choices
289
isTextEntry = (selectionValues == null);
290         
291         // Common items
292
// --------------
293
setTitle(title);
294         
295         JLabel mess = new JLabel(message.toString());
296         getContentPane().add(mess, BorderLayout.NORTH);
297         
298         // Buttons
299
JPanel pnlButtons = new JPanel();
300         getContentPane().add(pnlButtons, BorderLayout.SOUTH);
301         
302         JButton btnOk = new JButton("Ok");
303         btnOk.setMnemonic('O');
304         btnOk.addActionListener( new ActionListener() {
305             public void actionPerformed(ActionEvent e) {
306                 btnOk_clicked();
307             }
308         });
309         pnlButtons.add(btnOk);
310         JButton btnCancel = new JButton("Cancel");
311         btnCancel.setMnemonic('C');
312         btnCancel.addActionListener( new ActionListener() {
313             public void actionPerformed(ActionEvent e) {
314                 btnCancel_clicked();
315             }
316         });
317         pnlButtons.add(btnCancel);
318         
319         
320         if (isTextEntry) {
321             // Present a new text entry area
322
JScrollPane scr = new JScrollPane();
323             text = new JTextArea();
324             text.setWrapStyleWord(true);
325             text.setLineWrap(true);
326             scr.setViewportView(text);
327             scr.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
328             scr.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
329             getContentPane().add(scr, BorderLayout.CENTER);
330             // Set size and display pos of dialog
331
setSize(400, 150);
332         }
333         else
334         {
335             // Make a JList with stuff in there
336
sel = new JList(selectionValues);
337             sel.setSelectedValue(initialSelectionValue, true);
338             getContentPane().add(sel, BorderLayout.CENTER);
339             // Set size and display pos of dialog
340
setSize(400, 300);
341         }
342         
343         // Center it
344
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
345         Dimension frameSize = getSize();
346         if (frameSize.height > screenSize.height) {
347           frameSize.height = screenSize.height;
348         }
349         if (frameSize.width > screenSize.width) {
350           frameSize.width = screenSize.width;
351         }
352         setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
353     }
354     
355     protected void btnOk_clicked() {
356         if (isTextEntry)
357             JOptionPane.lastInputDialogReturnValue = text.getText();
358         else {
359             JOptionPane.lastInputDialogReturnValue = sel.getSelectedValue();
360             JOptionPane.lastInputDialogReturnIndex = sel.getSelectedIndex();
361         }
362         JOptionPane.setJobFinished(true);
363         dispose();
364     }
365     
366     protected void btnCancel_clicked() {
367         JOptionPane.lastInputDialogReturnValue = null;
368         JOptionPane.lastInputDialogReturnIndex = -1;
369         JOptionPane.setJobFinished(true);
370         dispose();
371     }
372     
373 }
374
Popular Tags