Source for gnu.javax.security.auth.callback.AWTCallbackHandler

   1: /* AWTCallbackHandler.java --
   2:    Copyright (C) 2004, 2006  Free Software Foundation, Inc.
   3: 
   4: This file is a part of GNU Classpath.
   5: 
   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.
  10: 
  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.
  15: 
  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
  20: 
  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.
  25: 
  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.  */
  37: 
  38: 
  39: package gnu.javax.security.auth.callback;
  40: 
  41: import gnu.java.lang.CPStringBuilder;
  42: 
  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;
  54: 
  55: import java.awt.event.ActionEvent;
  56: import java.awt.event.ActionListener;
  57: import java.awt.event.WindowEvent;
  58: import java.awt.event.WindowListener;
  59: 
  60: import java.util.Locale;
  61: 
  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;
  69: 
  70: public class AWTCallbackHandler extends AbstractCallbackHandler
  71:   implements ActionListener, WindowListener
  72: {
  73: 
  74:   // Fields.
  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:   // Constructor.
  86:   // -------------------------------------------------------------------------
  87: 
  88:   public AWTCallbackHandler()
  89:   {
  90:     super ("AWT");
  91:     actionCommand = ACTION_NONE;
  92:   }
  93: 
  94:   // Instance methods.
  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() /*c.getStyle()*/)
 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:   // ActionListener interface implementation.
 431:   // -------------------------------------------------------------------------
 432: 
 433:   public synchronized void actionPerformed(ActionEvent ae)
 434:   {
 435:     actionCommand = ae.getActionCommand();
 436:     notifyAll();
 437:   }
 438: 
 439:   // WindowListener interface implementation.
 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: }