Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/cybertivity/powerjournal/framework/Dialogs.java


1   /*
2    *  Title:     Similated Intelligence
3    *  Copyright: Copyright (c) 2001 Cybertivity
4    *  Company:   <A HREF="http://www.cybertivity.com">Cybertivity</A>
5    *  @author    <A HREF="mailto:chris.arrowood@cybertivity.com">Chris Arrowood</A>
6    *  @version   $Id: Dialogs.java,v 1.2 2001/11/25 02:09:31 arrowood Exp $
7    */
8   package com.cybertivity.powerjournal.framework;
9   import java.lang.reflect.InvocationTargetException;
10  import javax.swing.JOptionPane;
11  import javax.swing.SwingUtilities;
12  import java.awt.Component;
13  
14  /**
15   *  Description of the Class
16   *
17   * @author arrowood
18   * @created July 17, 2001
19   */
20  public class Dialogs {
21  
22      /**
23       *  Show a yes/no/cancel dialog and return the result.
24       *
25       * @param title Title of Dialog
26       * @param text Text of the dialog
27       * @return The Confirm value
28       */
29  
30      public static int getConfirm(Component parent, String title, String text) {
31          ConfirmHelper confirmHelper = new ConfirmHelper(parent, title, text,
32                  JOptionPane.YES_NO_CANCEL_OPTION);
33  
34          if (SwingUtilities.isEventDispatchThread()) {
35              confirmHelper.run();
36          } else {
37              try {
38                  SwingUtilities.invokeAndWait(confirmHelper);
39              } catch (InterruptedException ex) {
40                  // Do nothing
41              } catch (InvocationTargetException ex) {
42                  // Do nothing
43              }
44          }
45  
46          return confirmHelper.get();
47      }
48  
49      public static int getConfirm(String title, String text) {
50          return getConfirm(null, title, text);
51      }
52  
53  
54      /**
55       *  Show an option dialog and return the result.
56       *
57       * @param title Title of Dialog
58       * @param text Text of the dialog
59       * @param options
60       * @param defaultOption Description of Parameter
61       * @return The Option value
62       */
63  
64      public static int getOption(Component parent, String title, String text, Object[] options,
65              Object defaultOption) {
66          OptionHelper optionHelper = new OptionHelper(parent, title, text, options,
67                  defaultOption);
68  
69          if (SwingUtilities.isEventDispatchThread()) {
70              optionHelper.run();
71          } else {
72              try {
73                  SwingUtilities.invokeAndWait(optionHelper);
74              } catch (InterruptedException ex) {
75                  // Do nothing
76              } catch (InvocationTargetException ex) {
77                  // Do nothing
78              }
79          }
80  
81          return optionHelper.get();
82      }
83  
84      public static int getOption(String title, String text, Object[] options,
85              Object defaultOption) {
86          return getOption(null, title, text, options, defaultOption);
87      }
88  
89  
90      /**
91       *  Show an error dialog.
92       *
93       * @param title Title of Dialog
94       * @param text Text of the dialog
95       */
96  
97      public static void showError(Component parent, String title, String text) {
98          MessageHelper messageHelper = new MessageHelper(parent, title, text,
99                  JOptionPane.ERROR_MESSAGE);
100 
101         if (SwingUtilities.isEventDispatchThread()) {
102             messageHelper.run();
103         } else {
104             try {
105                 SwingUtilities.invokeAndWait(messageHelper);
106             } catch (InterruptedException ex) {
107                 // Do nothing
108             } catch (InvocationTargetException ex) {
109                 // Do nothing
110             }
111         }
112     }
113 
114     public static void showError(String title, String text) {
115         showError(null,title, text);
116 
117     }
118 
119     /**
120      *  Show a warning dialog.
121      *
122      * @param title Title of Dialog
123      * @param text Text of the dialog
124      */
125 
126     public static void showWarning(Component parent, String title, String text) {
127         MessageHelper messageHelper = new MessageHelper(parent , title, text,
128                 JOptionPane.WARNING_MESSAGE);
129 
130         if (SwingUtilities.isEventDispatchThread()) {
131             messageHelper.run();
132         } else {
133             try {
134                 SwingUtilities.invokeAndWait(messageHelper);
135             } catch (InterruptedException ex) {
136                 // Do nothing
137             } catch (InvocationTargetException ex) {
138                 // Do nothing
139             }
140         }
141     }
142     public static void showWarning(String title, String text) {
143         showWarning(null, title, text);
144     }
145 
146     /**
147      *  Show an information dialog.
148      *
149      * @param title Title of Dialog
150      * @param text Text of the dialog
151      */
152 
153     public static void showInformation(Component parent, String title, String text) {
154         MessageHelper messageHelper = new MessageHelper(parent, title, text,
155                 JOptionPane.INFORMATION_MESSAGE);
156 
157         if (SwingUtilities.isEventDispatchThread()) {
158             messageHelper.run();
159         } else {
160             try {
161                 SwingUtilities.invokeAndWait(messageHelper);
162             } catch (InterruptedException ex) {
163                 // Do nothing
164             } catch (InvocationTargetException ex) {
165                 // Do nothing
166             }
167         }
168     }
169     public static void showInformation(String title, String text) {
170         showInformation(null, title, text);
171     }
172 
173     /**
174      *  Superclass of message and confirm helpers.
175      *
176      * @author arrowood
177      * @created July 17, 2001
178      */
179 
180     private static class Helper {
181         /**
182          */
183         protected String title = null;
184         /**
185          */
186         protected String text = null;
187         /**
188          */
189         protected int type = 0;
190 
191 
192         /**
193          *  Constructor for the Helper object
194          *
195          * @param title Title of Dialog
196          * @param text Text of the dialog
197          * @param type
198          */
199         protected Helper(Component parent, String title, String text, int type) {
200             this.title = title;
201             this.text = text;
202             this.type = type;
203         }
204     }
205 
206 
207     /**
208      *  Message display helper class. Each message dialog uses an instance of this
209      *  which it passes to invokeAndWait to make sure JOptionPane is called from
210      *  the event thread.
211      *
212      * @author arrowood
213      * @created July 17, 2001
214      */
215 
216     private static class MessageHelper extends Helper implements Runnable {
217         /**
218          *  Constructor for the MessageHelper object
219          *
220          * @param title Title of Dialog
221          * @param text Text of the dialog
222          * @param type
223          * @param parent Description of the Parameter
224          */
225         Component parent = null;
226         public MessageHelper(Component parentArg, String title, String text, int type) {
227             super(parentArg, title, text, type);
228             parent=parentArg;
229         }
230 
231 
232         /**
233          *  Main processing method for the MessageHelper object
234          */
235         public void run() {
236             JOptionPane.showMessageDialog(parent, text, title, type);
237         }
238     }
239 
240 
241     private static class ConfirmHelper extends Helper implements Runnable {
242         private int result = 0;
243 
244 
245         /**
246          *  Constructor for the ConfirmHelper object
247          *
248          * @param title Title of Dialog
249          * @param text Text of the dialog
250          * @param type
251          */
252         Component parent = null;
253         public ConfirmHelper(Component parentArg, String title, String text, int type) {
254             super(parentArg, title, text, type);
255             parent = parentArg;
256         }
257 
258 
259         public int get() {
260             return result;
261         }
262 
263 
264         /**
265          *  Main processing method for the ConfirmHelper object
266          */
267         public void run() {
268             result = JOptionPane.showConfirmDialog(parent, text, title, type);
269         }
270     }
271 
272 
273     private static class OptionHelper extends Helper implements Runnable {
274         Object[] options = null;
275         Object defaultOption = null;
276         private int result = 0;
277         Component parent = null;
278 
279 
280         /**
281          *  Constructor for the OptionHelper object
282          *
283          * @param title Title of Dialog
284          * @param text Text of the dialog
285          * @param options
286          * @param defaultOption
287          * @param parent Description of the Parameter
288          */
289         public OptionHelper(Component parentArg, String title, String text, Object[] options,
290                 Object defaultOption) {
291             super(parentArg, title, text, 0);
292             parentArg = parent;
293             this.options = options;
294             this.defaultOption = defaultOption;
295         }
296 
297 
298         public int get() {
299             return result;
300         }
301 
302 
303         /**
304          *  Main processing method for the OptionHelper object
305          */
306         public void run() {
307             result = JOptionPane.showOptionDialog(parent,
308                     text, title, JOptionPane.DEFAULT_OPTION,
309                     JOptionPane.WARNING_MESSAGE, null, options, defaultOption);
310         }
311     }
312 }
313