1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52:
53: import ;
54: import ;
55: import ;
56: import ;
57:
58: import ;
59:
60: import ;
61: import ;
62: import ;
63: import ;
64: import ;
65: import ;
66: import ;
67:
68: public class AWTCallbackHandler extends AbstractCallbackHandler
69: implements ActionListener, WindowListener
70: {
71:
72:
73:
74:
75: protected String actionCommand;
76:
77: private static final String ACTION_CANCEL = "CANCEL";
78: private static final String ACTION_NO = "NO";
79: private static final String ACTION_NONE = "NONE";
80: private static final String ACTION_OK = "OK";
81: private static final String ACTION_YES = "YES";
82:
83:
84:
85:
86: public AWTCallbackHandler()
87: {
88: super ("AWT");
89: actionCommand = ACTION_NONE;
90: }
91:
92:
93:
94:
95: protected synchronized void handleChoice(ChoiceCallback c)
96: {
97: Frame ownerFrame = new Frame();
98: Dialog dialog = new Dialog(ownerFrame);
99: String[] choices = c.getChoices();
100: dialog.setTitle(c.getPrompt());
101: Label label = new Label(c.getPrompt());
102: List list = new List(Math.min(5, choices.length),
103: c.allowMultipleSelections());
104: Panel buttons = new Panel();
105: Button ok = new Button(messages.getString("callback.ok"));
106: ok.setActionCommand(ACTION_OK);
107: ok.addActionListener(this);
108: Button cancel = new Button(messages.getString("callback.cancel"));
109: cancel.setActionCommand(ACTION_CANCEL);
110: cancel.addActionListener(this);
111: for (int i = 0; i < choices.length; i++)
112: {
113: list.add(choices[i]);
114: }
115: if (c.getDefaultChoice() >= 0 && c.getDefaultChoice() < choices.length)
116: {
117: list.select(c.getDefaultChoice());
118: }
119: dialog.setLayout(new BorderLayout());
120: dialog.add(label, BorderLayout.NORTH);
121: dialog.add(list, BorderLayout.CENTER);
122: buttons.setLayout(new FlowLayout(FlowLayout.RIGHT));
123: buttons.add(cancel);
124: buttons.add(ok);
125: dialog.add(buttons, BorderLayout.SOUTH);
126: dialog.pack();
127: dialog.show();
128: try { wait(); }
129: catch (InterruptedException ie) { }
130: if (actionCommand.equals(ACTION_OK))
131: {
132: if (c.allowMultipleSelections())
133: {
134: c.setSelectedIndexes(list.getSelectedIndexes());
135: }
136: else
137: {
138: c.setSelectedIndex(list.getSelectedIndex());
139: }
140: }
141: dialog.dispose();
142: ownerFrame.dispose();
143: }
144:
145: protected synchronized void handleConfirmation(ConfirmationCallback c)
146: {
147: Frame ownerFrame = new Frame();
148: Dialog dialog = new Dialog(ownerFrame);
149: switch (c.getMessageType())
150: {
151: case ConfirmationCallback.ERROR:
152: dialog.setTitle(messages.getString("callback.error"));
153: break;
154: case ConfirmationCallback.INFORMATION:
155: dialog.setTitle(messages.getString("callback.information"));
156: break;
157: case ConfirmationCallback.WARNING:
158: dialog.setTitle(messages.getString("callback.warning"));
159: break;
160: default:
161: dialog.setTitle("");
162: }
163: dialog.setLayout(new GridLayout(2, 1));
164: dialog.add(new Label(c.getPrompt()));
165: Panel buttons = new Panel();
166: buttons.setLayout(new FlowLayout(FlowLayout.RIGHT));
167: dialog.add(buttons);
168: String[] choices = null;
169: int[] values = null;
170: switch (c.getOptionType())
171: {
172: case ConfirmationCallback.OK_CANCEL_OPTION:
173: choices = new String[] {
174: messages.getString("callback.cancel"),
175: messages.getString("callback.ok")
176: };
177: values = new int[] {
178: ConfirmationCallback.CANCEL, ConfirmationCallback.OK
179: };
180: break;
181: case ConfirmationCallback.YES_NO_CANCEL_OPTION:
182: choices = new String[] {
183: messages.getString("callback.cancel"),
184: messages.getString("callback.no"),
185: messages.getString("callback.yes")
186: };
187: values = new int[] {
188: ConfirmationCallback.CANCEL, ConfirmationCallback.NO,
189: ConfirmationCallback.YES
190: };
191: break;
192: case ConfirmationCallback.YES_NO_OPTION:
193: choices = new String[] {
194: messages.getString("callback.no"),
195: messages.getString("callback.yes")
196: };
197: values = new int[] {
198: ConfirmationCallback.NO, ConfirmationCallback.YES
199: };
200: break;
201: case ConfirmationCallback.UNSPECIFIED_OPTION:
202: choices = c.getOptions();
203: values = new int[choices.length];
204: for (int i = 0; i < values.length; i++)
205: values[i] = i;
206: break;
207: default:
208: throw new IllegalArgumentException();
209: }
210: for (int i = 0; i < choices.length; i++)
211: {
212: Button b = new Button(choices[i]);
213: b.setActionCommand(choices[i]);
214: b.addActionListener(this);
215: buttons.add(b);
216: }
217: dialog.pack();
218: dialog.show();
219: try { wait(); }
220: catch (InterruptedException ie) { }
221: for (int i = 0; i < choices.length; i++)
222: {
223: if (actionCommand.equals(choices[i]))
224: {
225: c.setSelectedIndex(values[i]);
226: break;
227: }
228: }
229: dialog.dispose();
230: ownerFrame.dispose();
231: }
232:
233: protected synchronized void handleLanguage(LanguageCallback c)
234: {
235: Locale[] locales = Locale.getAvailableLocales();
236: String[] languages = new String[locales.length];
237: Locale def = Locale.getDefault();
238: int defind = 0;
239: for (int i = 0; i < locales.length; i++)
240: {
241: StringBuffer lang =
242: new StringBuffer(locales[i].getDisplayLanguage(locales[i]));
243: String country = locales[i].getDisplayCountry(locales[i]);
244: String variant = locales[i].getDisplayVariant(locales[i]);
245: if (country.length() > 0 && variant.length() > 0)
246: {
247: lang.append(" (");
248: lang.append(country);
249: lang.append(", ");
250: lang.append(variant);
251: lang.append(")");
252: }
253: else if (country.length() > 0)
254: {
255: lang.append(" (");
256: lang.append(country);
257: lang.append(")");
258: }
259: else if (variant.length() > 0)
260: {
261: lang.append(" (");
262: lang.append(variant);
263: lang.append(")");
264: }
265: languages[i] = lang.toString();
266: if (locales[i].equals(def))
267: defind = i;
268: }
269: ChoiceCallback c2 =
270: new ChoiceCallback(messages.getString("callback.language"), languages,
271: defind, false);
272: handleChoice(c2);
273: c.setLocale(def);
274: if (c2.getSelectedIndexes() != null && c2.getSelectedIndexes().length > 0)
275: {
276: int index = c2.getSelectedIndexes()[0];
277: if (index >= 0 && index < locales.length)
278: c.setLocale(locales[index]);
279: }
280: }
281:
282: protected synchronized void handleName(NameCallback c)
283: {
284: Frame ownerFrame = new Frame();
285: Dialog dialog = new Dialog(ownerFrame);
286: dialog.setTitle(c.getPrompt());
287: dialog.setLayout(new GridLayout(3, 1));
288: Label label = new Label(c.getPrompt());
289: TextField input = new TextField();
290: if (c.getDefaultName() != null)
291: {
292: input.setText(c.getDefaultName());
293: }
294: Panel buttons = new Panel();
295: Button ok = new Button(messages.getString("callback.ok"));
296: ok.setActionCommand(ACTION_OK);
297: ok.addActionListener(this);
298: Button cancel = new Button(messages.getString("callback.cancel"));
299: cancel.setActionCommand(ACTION_CANCEL);
300: cancel.addActionListener(this);
301: dialog.add(label);
302: dialog.add(input);
303: buttons.setLayout(new FlowLayout(FlowLayout.RIGHT));
304: buttons.add(ok);
305: buttons.add(cancel);
306: dialog.add(buttons);
307: dialog.pack();
308: dialog.show();
309: try { wait(); }
310: catch (InterruptedException ie) { }
311: if (actionCommand.equals(ACTION_OK))
312: {
313: c.setName(input.getText());
314: }
315: dialog.dispose();
316: ownerFrame.dispose();
317: }
318:
319: protected synchronized void handlePassword(PasswordCallback c)
320: {
321: Frame ownerFrame = new Frame();
322: Dialog dialog = new Dialog(ownerFrame);
323: dialog.setTitle(c.getPrompt());
324: dialog.setLayout(new GridLayout(3, 1));
325: Label label = new Label(c.getPrompt());
326: TextField input = new TextField();
327: if (!c.isEchoOn())
328: {
329: input.setEchoChar('*');
330: }
331: Panel buttons = new Panel();
332: Button ok = new Button(messages.getString("callback.ok"));
333: ok.setActionCommand(ACTION_OK);
334: ok.addActionListener(this);
335: Button cancel = new Button(messages.getString("callback.cancel"));
336: cancel.setActionCommand(ACTION_CANCEL);
337: cancel.addActionListener(this);
338: dialog.add(label);
339: dialog.add(input);
340: buttons.setLayout(new FlowLayout(FlowLayout.RIGHT));
341: buttons.add(ok);
342: buttons.add(cancel);
343: dialog.add(buttons);
344: dialog.pack();
345: dialog.show();
346: try { wait(); }
347: catch (InterruptedException ie) { }
348: if (actionCommand.equals(ACTION_OK))
349: {
350: c.setPassword(input.getText().toCharArray());
351: }
352: dialog.dispose();
353: ownerFrame.dispose();
354: }
355:
356: protected synchronized void handleTextInput(TextInputCallback c)
357: {
358: Frame ownerFrame = new Frame();
359: Dialog dialog = new Dialog(ownerFrame);
360: dialog.setTitle(c.getPrompt());
361: dialog.setLayout(new BorderLayout());
362: Label label = new Label(c.getPrompt());
363: TextArea text = new TextArea(10, 40);
364: if (c.getDefaultText() != null)
365: {
366: text.setText(c.getDefaultText());
367: }
368: Panel buttons = new Panel();
369: Button ok = new Button(messages.getString("callback.ok"));
370: ok.setActionCommand(ACTION_OK);
371: ok.addActionListener(this);
372: Button cancel = new Button(messages.getString("callback.cancel"));
373: cancel.setActionCommand(ACTION_CANCEL);
374: cancel.addActionListener(this);
375: dialog.add(label, BorderLayout.NORTH);
376: dialog.add(text, BorderLayout.CENTER);
377: buttons.setLayout(new FlowLayout(FlowLayout.RIGHT));
378: buttons.add(ok);
379: buttons.add(cancel);
380: dialog.add(buttons, BorderLayout.SOUTH);
381: dialog.pack();
382: dialog.show();
383: try { wait(); }
384: catch (InterruptedException ie) { }
385: if (actionCommand.equals(ACTION_OK))
386: {
387: c.setText(text.getText());
388: }
389: dialog.dispose();
390: ownerFrame.dispose();
391: }
392:
393: protected synchronized void handleTextOutput(TextOutputCallback c)
394: {
395: Frame ownerFrame = new Frame();
396: Dialog dialog = new Dialog(ownerFrame);
397: dialog.setLayout(new GridLayout(2, 1));
398: switch (c.getMessageType() )
399: {
400: case ConfirmationCallback.ERROR:
401: dialog.setTitle(messages.getString("callback.error"));
402: break;
403: case ConfirmationCallback.INFORMATION:
404: dialog.setTitle(messages.getString("callback.information"));
405: break;
406: case ConfirmationCallback.WARNING:
407: dialog.setTitle(messages.getString("callback.warning"));
408: break;
409: default:
410: dialog.setTitle("");
411: }
412: Label label = new Label(c.getMessage());
413: Panel buttons = new Panel();
414: Button ok = new Button(messages.getString("callback.ok"));
415: buttons.setLayout(new FlowLayout(FlowLayout.RIGHT));
416: buttons.add(ok);
417: ok.addActionListener(this);
418: dialog.add(label);
419: dialog.add(buttons);
420: dialog.pack();
421: dialog.show();
422: try { wait(); }
423: catch (InterruptedException ie) { }
424: dialog.dispose();
425: ownerFrame.dispose();
426: }
427:
428:
429:
430:
431: public synchronized void actionPerformed(ActionEvent ae)
432: {
433: actionCommand = ae.getActionCommand();
434: notifyAll();
435: }
436:
437:
438:
439:
440: public synchronized void windowClosing(WindowEvent we)
441: {
442: actionCommand = ACTION_NONE;
443: notifyAll();
444: }
445:
446: public void windowOpened(WindowEvent we) { }
447: public void windowClosed(WindowEvent we) { }
448: public void windowIconified(WindowEvent we) { }
449: public void windowDeiconified(WindowEvent we) { }
450: public void windowActivated(WindowEvent we) { }
451: public void windowDeactivated(WindowEvent we) { }
452: }