KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sshtools > ui > awt > options > OptionDialog


1 package com.sshtools.ui.awt.options;
2
3 import java.awt.BorderLayout JavaDoc;
4 import java.awt.Button JavaDoc;
5 import java.awt.Color JavaDoc;
6 import java.awt.Component JavaDoc;
7 import java.awt.Dialog JavaDoc;
8 import java.awt.Dimension JavaDoc;
9 import java.awt.FlowLayout JavaDoc;
10 import java.awt.Frame JavaDoc;
11 import java.awt.GridBagConstraints JavaDoc;
12 import java.awt.GridBagLayout JavaDoc;
13 import java.awt.Image JavaDoc;
14 import java.awt.Insets JavaDoc;
15 import java.awt.Label JavaDoc;
16 import java.awt.Panel JavaDoc;
17 import java.awt.ScrollPane JavaDoc;
18 import java.awt.TextField JavaDoc;
19 import java.awt.event.ActionEvent JavaDoc;
20 import java.awt.event.ActionListener JavaDoc;
21 import java.awt.event.WindowAdapter JavaDoc;
22 import java.awt.event.WindowEvent JavaDoc;
23 import java.io.PrintWriter JavaDoc;
24 import java.io.StringWriter JavaDoc;
25 import java.util.StringTokenizer JavaDoc;
26 import java.util.Vector JavaDoc;
27
28 import com.sshtools.ui.awt.ImageCanvas;
29 import com.sshtools.ui.awt.MultilineLabel;
30 import com.sshtools.ui.awt.UIUtil;
31
32 public class OptionDialog extends Panel JavaDoc {
33
34     public static String JavaDoc INFORMATION_ICON = "/images/information-48x48.png"; //$NON-NLS-1$
35
public static String JavaDoc WARNING_ICON = "/images/warning-48x48.png"; //$NON-NLS-1$
36
public static String JavaDoc QUESTION_ICON = "/images/question-48x48.png"; //$NON-NLS-1$
37
public static String JavaDoc ERROR_ICON = "/images/error-48x48.png"; //$NON-NLS-1$
38

39
40     public static final int INFORMATION = 0;
41
42     public static final int QUESTION = 1;
43
44     public static final int WARNING = 2;
45
46     public static final int ERROR = 3;
47
48     public static final int UNCATEGORISED = 99;
49
50     public static boolean useDialogForPrompt = true;
51
52     public static final Option CHOICE_YES = new Option(Messages.getString("OptionDialog.yes")); //$NON-NLS-1$
53

54     public static final Option CHOICE_SHOW = new Option(Messages.getString("OptionDialog.show")); //$NON-NLS-1$
55

56     public static final Option CHOICE_HIDE = new Option(Messages.getString("OptionDialog.hide")); //$NON-NLS-1$
57

58     public static final Option CHOICE_NO = new Option(Messages.getString("OptionDialog.no")); //$NON-NLS-1$
59

60     public static final Option CHOICE_OK = new Option(Messages.getString("OptionDialog.ok")); //$NON-NLS-1$
61

62     public static final Option CHOICE_YES_TO_ALL = new Option(Messages.getString("OptionDialog.yesToAll")); //$NON-NLS-1$
63

64     public static final Option CHOICE_CANCEL = new Option(Messages.getString("OptionDialog.cancel")); //$NON-NLS-1$
65

66     public static final Option CHOICES_YES_NO[] = { CHOICE_YES, CHOICE_NO };
67
68     public static final Option CHOICES_OK_CANCEL[] = { CHOICE_OK, CHOICE_CANCEL };
69
70     public static final Option CHOICES_OK[] = { CHOICE_OK };
71     
72     private Object JavaDoc lock_;
73     private boolean dismissed;
74     private Option selected;
75     private OptionCallback callback;
76     private Dialog JavaDoc dialog;
77
78     public OptionDialog(int type, Object JavaDoc text, Option choices[], OptionCallback callback) {
79         this(type, text, choices, callback, null);
80     }
81     
82     public OptionDialog(int type, Object JavaDoc text, Option choices[], OptionCallback callback, Component JavaDoc buttonBarAccessory) {
83         super(new BorderLayout JavaDoc());
84         lock_ = new Object JavaDoc();
85         dismissed = false;
86         this.callback = callback;
87         Panel JavaDoc titlePanel = new Panel JavaDoc(new FlowLayout JavaDoc(0));
88         Image JavaDoc icon = null;
89         switch (type) {
90         case 0:
91             // '\0'
92
icon = UIUtil.loadImage(getClass(), INFORMATION_ICON);
93             break;
94         case 2:
95             // '\002'
96
icon = UIUtil.loadImage(getClass(), WARNING_ICON);
97             break;
98         case 1:
99             // '\001'
100
icon = UIUtil.loadImage(getClass(), QUESTION_ICON);
101             break;
102         case 99:
103             break;
104         default:
105             icon = UIUtil.loadImage(getClass(), ERROR_ICON);
106             break;
107         }
108         if (icon != null) {
109             UIUtil.waitFor(icon, this);
110         }
111         Panel JavaDoc textPanel = new Panel JavaDoc(new GridBagLayout JavaDoc());
112         GridBagConstraints JavaDoc gbc = new GridBagConstraints JavaDoc();
113         gbc.anchor = GridBagConstraints.WEST;
114         gbc.fill = GridBagConstraints.NONE;
115         if (text instanceof Component JavaDoc) {
116             UIUtil.gridBagAdd(textPanel, (Component JavaDoc) text, gbc, 0);
117         } else {
118             StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(String.valueOf(text), "\n"); //$NON-NLS-1$
119
while (st.hasMoreTokens()) {
120                 String JavaDoc n = st.nextToken().trim();
121                 if (n.length() > 0) {
122                     UIUtil.gridBagAdd(textPanel, new Label JavaDoc(n), gbc, 0);
123                 }
124             }
125         }
126         add(textPanel, "Center"); //$NON-NLS-1$
127
Panel JavaDoc choicePanel = new Panel JavaDoc(new FlowLayout JavaDoc(buttonBarAccessory == null ? FlowLayout.CENTER : FlowLayout.RIGHT));
128         OptionWrapper choice = new OptionWrapper();
129         choice.idx = -1;
130         for (int i = 0; choices != null && i < choices.length; i++) {
131             // ImageButton b = new ImageButton(choices[i].getIcon(), choices[i]
132
// .getText(), choices[i].getText());
133
Button JavaDoc b = new Button JavaDoc(choices[i].getText()) {
134                 public Dimension JavaDoc getMinimumSize() {
135                     return new Dimension JavaDoc(60, super.getMinimumSize().height);
136                 }
137
138                 public Dimension JavaDoc getPreferredSize() {
139                     return getMinimumSize();
140                 }
141             };
142             choicePanel.add(b);
143             b.addActionListener(new BlockingActionListener(choices[i]));
144         }
145         if (icon != null) {
146             add(new ImageCanvas(icon), "West"); //$NON-NLS-1$
147
}
148         if(buttonBarAccessory != null) {
149             Panel JavaDoc p= new Panel JavaDoc(new GridBagLayout JavaDoc());
150             GridBagConstraints JavaDoc gbc2 = new GridBagConstraints JavaDoc();
151             gbc2.anchor = GridBagConstraints.WEST;
152             gbc2.fill = GridBagConstraints.HORIZONTAL;
153             gbc2.insets = new Insets JavaDoc(2, 4, 2, 2);
154             UIUtil.gridBagAdd(p, buttonBarAccessory, gbc2, GridBagConstraints.RELATIVE);
155             gbc2.anchor = GridBagConstraints.EAST;
156             gbc2.weightx = 1.0;
157             UIUtil.gridBagAdd(p, choicePanel, gbc2, GridBagConstraints.REMAINDER);
158             add(p, BorderLayout.SOUTH);
159         }
160         else {
161             add(choicePanel, "South"); //$NON-NLS-1$
162
}
163     }
164
165     public static void main(String JavaDoc[] args) {
166         Frame JavaDoc f = new Frame JavaDoc();
167         OptionDialog.error(f, "Test", "This is a test error", new Exception JavaDoc("Test")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
168
}
169
170     public void choice(Option choice) {
171         selected = choice;
172         if (dialog != null)
173             dialog.dispose();
174         else
175             synchronized (lock_) {
176                 dismissed = true;
177                 lock_.notify();
178             }
179     }
180
181     public Option dialogPrompt(Component JavaDoc parent, String JavaDoc title) {
182         java.awt.Frame JavaDoc f = parent == null ? null : (parent instanceof Frame JavaDoc ? (Frame JavaDoc) parent : UIUtil.getFrameAncestor(parent));
183         if (f == null) {
184             f = UIUtil.getSharedFrame();
185         }
186         dialog = new Dialog JavaDoc(f, title, true);
187         dialog.addWindowListener(new WindowAdapter JavaDoc() {
188             public void windowClosing(WindowEvent JavaDoc evt) {
189                 dialog.dispose();
190             }
191         });
192         dialog.setLayout(new BorderLayout JavaDoc());
193         dialog.add(this);
194         dialog.pack();
195         Dimension JavaDoc s = dialog.getSize();
196         dialog.setSize(s.width + 16, s.height + 16);
197         UIUtil.positionComponent(UIUtil.CENTER, dialog);
198         dialog.setResizable(false);
199         dialog.setVisible(true);
200         return selected;
201     }
202
203     public static char[] promptForAuthentication(Component JavaDoc parent, String JavaDoc title) {
204         return promptForAuthentication(parent, title, Messages.getString("OptionDialog.password")); //$NON-NLS-1$
205
}
206
207     public static char[] promptForAuthentication(Component JavaDoc parent, String JavaDoc title, String JavaDoc label) {
208         String JavaDoc t = promptForText(parent, title, "", null, '*', label); //$NON-NLS-1$
209
return t != null ? t.toCharArray() : null;
210     }
211
212     public static String JavaDoc promptForText(Component JavaDoc parent, String JavaDoc title, String JavaDoc defaultText, Component JavaDoc accessory, char echoCharacter,
213                     String JavaDoc label) {
214         return promptForText(parent, title, defaultText, accessory, echoCharacter, label, -1, "South"); //$NON-NLS-1$
215
}
216
217     public static String JavaDoc promptForText(Component JavaDoc parent, String JavaDoc title, String JavaDoc defaultText, Component JavaDoc accessory, char echoCharacter,
218                     String JavaDoc label, int textWidth, String JavaDoc accesoryPosition) {
219         Panel JavaDoc p = new Panel JavaDoc(new BorderLayout JavaDoc());
220         Panel JavaDoc middle = new Panel JavaDoc(new FlowLayout JavaDoc());
221         middle.add(new Label JavaDoc(label));
222         TextField JavaDoc text = new TextField JavaDoc(defaultText, textWidth == -1 ? 15 : textWidth);
223         middle.add(text);
224         p.add(middle, "Center"); //$NON-NLS-1$
225
if (echoCharacter != ' ')
226             text.setEchoChar(echoCharacter);
227         text.requestFocus();
228         final OptionDialog dialog = new OptionDialog(QUESTION, p, CHOICES_OK_CANCEL, null);
229         text.addActionListener(new ActionListener JavaDoc() {
230             public void actionPerformed(ActionEvent JavaDoc evt) {
231                 dialog.choice(CHOICE_OK);
232             }
233         });
234         if (accessory != null)
235             p.add(accessory, accesoryPosition);
236         if (dialog.dialogPrompt(parent, title) == CHOICE_OK)
237             return text.getText();
238         else
239             return null;
240     }
241
242     public static Option prompt(Component JavaDoc parent, int type, String JavaDoc title, Object JavaDoc text, Option choices[]) {
243         return prompt(parent, type, title, text, choices, null);
244     }
245
246     public static Option prompt(Component JavaDoc parent, int type, String JavaDoc title, Object JavaDoc text, Option choices[], OptionCallback callback) {
247         return prompt(parent, type, title, text, choices, callback, null);
248     }
249     
250     public static Option prompt(Component JavaDoc parent, int type, String JavaDoc title, Object JavaDoc text, Option choices[], OptionCallback callback, Component JavaDoc buttonBarAccesory) {
251         return new OptionDialog(type, text, choices, callback, buttonBarAccesory).dialogPrompt(parent, title);
252     }
253
254     /**
255      * Show an error message with detail
256      *
257      * @param parent
258      * @param title
259      * @param exception
260      */

261     public static void error(Component JavaDoc parent, String JavaDoc title, Throwable JavaDoc exception) {
262         error(parent, title, null, exception);
263     }
264
265     /**
266      * Show an error message with detail
267      *
268      * @param parent
269      * @param title
270      * @param exception
271      */

272     public static void error(Component JavaDoc parent, String JavaDoc title, String JavaDoc message) {
273         error(parent, title, message, null);
274     }
275
276     /**
277      * Show an error message with toggable detail
278      *
279      * @param parent
280      * @param mesg
281      * @param title
282      * @param exception
283      */

284     public static void error(Component JavaDoc parent, String JavaDoc title, String JavaDoc mesg, Throwable JavaDoc exception) {
285         error(parent, title, mesg, exception, null);
286     }
287
288     /**
289      * Show an error message with toggable detail
290      *
291      * @param parent
292      * @param mesg
293      * @param title
294      * @param exception
295      */

296     public static Option error(Component JavaDoc parent, String JavaDoc title, String JavaDoc mesg, Throwable JavaDoc exception, Option[] options) {
297         boolean details = false;
298         if (exception != null) {
299             exception.printStackTrace();
300         }
301         while (true) {
302             Vector JavaDoc optlist = new Vector JavaDoc();
303             int detailsIdx = -1;
304             if (options != null) {
305                 for (int i = 0; i < options.length; i++) {
306                     optlist.addElement(options[i]);
307                 }
308             }
309             if (exception != null) {
310                 detailsIdx = optlist.size();
311                 if (details) {
312                     optlist.addElement(CHOICE_HIDE);
313                 } else {
314                     optlist.addElement(CHOICE_SHOW);
315                 }
316             }
317             if (options == null) {
318                 optlist.addElement(CHOICE_OK);
319             }
320             Option[] opts = new Option[optlist.size()];
321             for (int i = 0; i < optlist.size(); i++) {
322                 opts[i] = (Option) optlist.elementAt(i);
323             }
324
325             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
326             if (mesg != null) {
327                 buf.append(mesg + "\n"); //$NON-NLS-1$
328
}
329             appendException(exception, 0, buf, details);
330             Component JavaDoc message;
331             if (details) {
332                 MultilineLabel text = new MultilineLabel(buf.toString());
333                 message = new ScrollPane JavaDoc(ScrollPane.SCROLLBARS_AS_NEEDED);
334                 message.setSize(new Dimension JavaDoc(520, 400));
335                 ((ScrollPane JavaDoc) message).add(text);
336
337             } else {
338                 message = new MultilineLabel(buf.toString());
339             }
340             Option opt = prompt(parent, ERROR, title, message, opts);
341             if (opt == CHOICE_HIDE || opt == CHOICE_SHOW) {
342                 details = !details;
343             } else {
344                 if (options != null) {
345                     return opt;
346                 } else {
347                     return null;
348                 }
349             }
350         }
351     }
352
353     protected static void appendException(Throwable JavaDoc exception, int level, StringBuffer JavaDoc buf, boolean details) {
354         try {
355             if (((exception != null) && (exception.getMessage() != null)) && (exception.getMessage().length() > 0)) {
356                 if (details && (level > 0)) {
357                     buf.append("\n \nCaused by ...\n"); //$NON-NLS-1$
358
}
359                 buf.append(exception.getMessage());
360             }
361             if (details) {
362                 if (exception != null) {
363                     if ((exception.getMessage() != null) && (exception.getMessage().length() == 0)) {
364                         buf.append("\n \nCaused by ..."); //$NON-NLS-1$
365
} else {
366                         buf.append("\n \n"); //$NON-NLS-1$
367
}
368                 }
369                 StringWriter JavaDoc sw = new StringWriter JavaDoc();
370                 if (exception != null) {
371                     exception.printStackTrace(new PrintWriter JavaDoc(sw));
372                 }
373                 buf.append(sw.toString());
374             }
375             try {
376                 java.lang.reflect.Method JavaDoc method = exception.getClass().getMethod("getCause", new Class JavaDoc[] {}); //$NON-NLS-1$
377
Throwable JavaDoc cause = (Throwable JavaDoc) method.invoke(exception, (Object JavaDoc[]) null);
378                 if (cause != null) {
379                     appendException(cause, level + 1, buf, details);
380                 }
381             } catch (Exception JavaDoc e) {
382             }
383         } catch (Throwable JavaDoc ex) {
384         }
385     }
386
387     public static void info(Component JavaDoc parent, String JavaDoc title, String JavaDoc message) {
388         prompt(parent, 0, title, message, CHOICES_OK);
389     }
390
391     static class OptionWrapper {
392         int idx;
393
394         OptionWrapper() {
395         }
396     }
397
398     class BlockingActionListener implements ActionListener JavaDoc {
399         public void actionPerformed(ActionEvent JavaDoc e) {
400             if (callback != null && !callback.allowClose(choice))
401                 return;
402             selected = choice;
403             if (dialog != null)
404                 dialog.dispose();
405             else
406                 synchronized (lock_) {
407                     dismissed = true;
408                     lock_.notify();
409                 }
410         }
411
412         Option choice;
413
414         BlockingActionListener(Option choice) {
415             this.choice = choice;
416         }
417     }
418 }
Popular Tags