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