libjava/ChangeLog:
[official-gcc.git] / libjava / classpath / gnu / javax / security / auth / callback / AWTCallbackHandler.java
blobd75ce4104fa31655445651edd8472a237bafe739
1 /* AWTCallbackHandler.java --
2 Copyright (C) 2004, 2006 Free Software Foundation, Inc.
4 This file is a part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or (at
9 your option) any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
19 USA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu.javax.security.auth.callback;
41 import gnu.java.lang.CPStringBuilder;
43 import java.awt.BorderLayout;
44 import java.awt.Button;
45 import java.awt.Dialog;
46 import java.awt.FlowLayout;
47 import java.awt.Frame;
48 import java.awt.GridLayout;
49 import java.awt.Label;
50 import java.awt.List;
51 import java.awt.Panel;
52 import java.awt.TextArea;
53 import java.awt.TextField;
55 import java.awt.event.ActionEvent;
56 import java.awt.event.ActionListener;
57 import java.awt.event.WindowEvent;
58 import java.awt.event.WindowListener;
60 import java.util.Locale;
62 import javax.security.auth.callback.ChoiceCallback;
63 import javax.security.auth.callback.ConfirmationCallback;
64 import javax.security.auth.callback.LanguageCallback;
65 import javax.security.auth.callback.NameCallback;
66 import javax.security.auth.callback.PasswordCallback;
67 import javax.security.auth.callback.TextInputCallback;
68 import javax.security.auth.callback.TextOutputCallback;
70 public class AWTCallbackHandler extends AbstractCallbackHandler
71 implements ActionListener, WindowListener
74 // Fields.
75 // -------------------------------------------------------------------------
77 protected String actionCommand;
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";
85 // Constructor.
86 // -------------------------------------------------------------------------
88 public AWTCallbackHandler()
90 super ("AWT");
91 actionCommand = ACTION_NONE;
94 // Instance methods.
95 // -------------------------------------------------------------------------
97 protected synchronized void handleChoice(ChoiceCallback c)
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++)
115 list.add(choices[i]);
117 if (c.getDefaultChoice() >= 0 && c.getDefaultChoice() < choices.length)
119 list.select(c.getDefaultChoice());
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))
134 if (c.allowMultipleSelections())
136 c.setSelectedIndexes(list.getSelectedIndexes());
138 else
140 c.setSelectedIndex(list.getSelectedIndex());
143 dialog.dispose();
144 ownerFrame.dispose();
147 protected synchronized void handleConfirmation(ConfirmationCallback c)
149 Frame ownerFrame = new Frame();
150 Dialog dialog = new Dialog(ownerFrame);
151 switch (c.getMessageType())
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("");
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())
174 case ConfirmationCallback.OK_CANCEL_OPTION:
175 choices = new String[] {
176 messages.getString("callback.cancel"),
177 messages.getString("callback.ok")
179 values = new int[] {
180 ConfirmationCallback.CANCEL, ConfirmationCallback.OK
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")
189 values = new int[] {
190 ConfirmationCallback.CANCEL, ConfirmationCallback.NO,
191 ConfirmationCallback.YES
193 break;
194 case ConfirmationCallback.YES_NO_OPTION:
195 choices = new String[] {
196 messages.getString("callback.no"),
197 messages.getString("callback.yes")
199 values = new int[] {
200 ConfirmationCallback.NO, ConfirmationCallback.YES
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();
212 for (int i = 0; i < choices.length; i++)
214 Button b = new Button(choices[i]);
215 b.setActionCommand(choices[i]);
216 b.addActionListener(this);
217 buttons.add(b);
219 dialog.pack();
220 dialog.show();
221 try { wait(); }
222 catch (InterruptedException ie) { }
223 for (int i = 0; i < choices.length; i++)
225 if (actionCommand.equals(choices[i]))
227 c.setSelectedIndex(values[i]);
228 break;
231 dialog.dispose();
232 ownerFrame.dispose();
235 protected synchronized void handleLanguage(LanguageCallback c)
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++)
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)
249 lang.append(" (");
250 lang.append(country);
251 lang.append(", ");
252 lang.append(variant);
253 lang.append(")");
255 else if (country.length() > 0)
257 lang.append(" (");
258 lang.append(country);
259 lang.append(")");
261 else if (variant.length() > 0)
263 lang.append(" (");
264 lang.append(variant);
265 lang.append(")");
267 languages[i] = lang.toString();
268 if (locales[i].equals(def))
269 defind = i;
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)
278 int index = c2.getSelectedIndexes()[0];
279 if (index >= 0 && index < locales.length)
280 c.setLocale(locales[index]);
284 protected synchronized void handleName(NameCallback c)
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)
294 input.setText(c.getDefaultName());
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))
315 c.setName(input.getText());
317 dialog.dispose();
318 ownerFrame.dispose();
321 protected synchronized void handlePassword(PasswordCallback c)
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())
331 input.setEchoChar('*');
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))
352 c.setPassword(input.getText().toCharArray());
354 dialog.dispose();
355 ownerFrame.dispose();
358 protected synchronized void handleTextInput(TextInputCallback c)
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)
368 text.setText(c.getDefaultText());
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))
389 c.setText(text.getText());
391 dialog.dispose();
392 ownerFrame.dispose();
395 protected synchronized void handleTextOutput(TextOutputCallback c)
397 Frame ownerFrame = new Frame();
398 Dialog dialog = new Dialog(ownerFrame);
399 dialog.setLayout(new GridLayout(2, 1));
400 switch (c.getMessageType() /*c.getStyle()*/)
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("");
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();
430 // ActionListener interface implementation.
431 // -------------------------------------------------------------------------
433 public synchronized void actionPerformed(ActionEvent ae)
435 actionCommand = ae.getActionCommand();
436 notifyAll();
439 // WindowListener interface implementation.
440 // -------------------------------------------------------------------------
442 public synchronized void windowClosing(WindowEvent we)
444 actionCommand = ACTION_NONE;
445 notifyAll();
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) { }