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
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
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
;
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
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";
86 // -------------------------------------------------------------------------
88 public AWTCallbackHandler()
91 actionCommand
= ACTION_NONE
;
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
));
127 dialog
.add(buttons
, BorderLayout
.SOUTH
);
131 catch (InterruptedException ie
) { }
132 if (actionCommand
.equals(ACTION_OK
))
134 if (c
.allowMultipleSelections())
136 c
.setSelectedIndexes(list
.getSelectedIndexes());
140 c
.setSelectedIndex(list
.getSelectedIndex());
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"));
156 case ConfirmationCallback
.INFORMATION
:
157 dialog
.setTitle(messages
.getString("callback.information"));
159 case ConfirmationCallback
.WARNING
:
160 dialog
.setTitle(messages
.getString("callback.warning"));
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
));
170 String
[] choices
= 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")
180 ConfirmationCallback
.CANCEL
, ConfirmationCallback
.OK
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")
190 ConfirmationCallback
.CANCEL
, ConfirmationCallback
.NO
,
191 ConfirmationCallback
.YES
194 case ConfirmationCallback
.YES_NO_OPTION
:
195 choices
= new String
[] {
196 messages
.getString("callback.no"),
197 messages
.getString("callback.yes")
200 ConfirmationCallback
.NO
, ConfirmationCallback
.YES
203 case ConfirmationCallback
.UNSPECIFIED_OPTION
:
204 choices
= c
.getOptions();
205 values
= new int[choices
.length
];
206 for (int i
= 0; i
< values
.length
; i
++)
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);
222 catch (InterruptedException ie
) { }
223 for (int i
= 0; i
< choices
.length
; i
++)
225 if (actionCommand
.equals(choices
[i
]))
227 c
.setSelectedIndex(values
[i
]);
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();
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)
250 lang
.append(country
);
252 lang
.append(variant
);
255 else if (country
.length() > 0)
258 lang
.append(country
);
261 else if (variant
.length() > 0)
264 lang
.append(variant
);
267 languages
[i
] = lang
.toString();
268 if (locales
[i
].equals(def
))
272 new ChoiceCallback(messages
.getString("callback.language"), languages
,
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);
305 buttons
.setLayout(new FlowLayout(FlowLayout
.RIGHT
));
312 catch (InterruptedException ie
) { }
313 if (actionCommand
.equals(ACTION_OK
))
315 c
.setName(input
.getText());
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();
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);
342 buttons
.setLayout(new FlowLayout(FlowLayout
.RIGHT
));
349 catch (InterruptedException ie
) { }
350 if (actionCommand
.equals(ACTION_OK
))
352 c
.setPassword(input
.getText().toCharArray());
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
));
382 dialog
.add(buttons
, BorderLayout
.SOUTH
);
386 catch (InterruptedException ie
) { }
387 if (actionCommand
.equals(ACTION_OK
))
389 c
.setText(text
.getText());
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"));
405 case ConfirmationCallback
.INFORMATION
:
406 dialog
.setTitle(messages
.getString("callback.information"));
408 case ConfirmationCallback
.WARNING
:
409 dialog
.setTitle(messages
.getString("callback.warning"));
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
));
419 ok
.addActionListener(this);
425 catch (InterruptedException ie
) { }
427 ownerFrame
.dispose();
430 // ActionListener interface implementation.
431 // -------------------------------------------------------------------------
433 public synchronized void actionPerformed(ActionEvent ae
)
435 actionCommand
= ae
.getActionCommand();
439 // WindowListener interface implementation.
440 // -------------------------------------------------------------------------
442 public synchronized void windowClosing(WindowEvent we
)
444 actionCommand
= ACTION_NONE
;
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
) { }