OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libjava/] [classpath/] [gnu/] [javax/] [security/] [auth/] [callback/] [SwingCallbackHandler.java] - Blame information for rev 769

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 769 jeremybenn
 /* SwingCallbackHandler.java --
2
    Copyright (C) 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 java.awt.Container;
42
import java.awt.Dimension;
43
import java.awt.FlowLayout;
44
import java.awt.Font;
45
import java.awt.GridBagConstraints;
46
import java.awt.GridBagLayout;
47
import java.awt.Insets;
48
import java.awt.event.ActionEvent;
49
import java.awt.event.ActionListener;
50
 
51
import java.io.IOException;
52
 
53
import java.util.Locale;
54
 
55
import javax.security.auth.callback.Callback;
56
import javax.security.auth.callback.ChoiceCallback;
57
import javax.security.auth.callback.ConfirmationCallback;
58
import javax.security.auth.callback.LanguageCallback;
59
import javax.security.auth.callback.NameCallback;
60
import javax.security.auth.callback.PasswordCallback;
61
import javax.security.auth.callback.TextInputCallback;
62
import javax.security.auth.callback.TextOutputCallback;
63
 
64
import javax.swing.JButton;
65
import javax.swing.JDialog;
66
import javax.swing.JLabel;
67
import javax.swing.JList;
68
import javax.swing.JPanel;
69
import javax.swing.JPasswordField;
70
import javax.swing.JScrollPane;
71
import javax.swing.JTextArea;
72
import javax.swing.JTextField;
73
import javax.swing.ListSelectionModel;
74
 
75
public class SwingCallbackHandler extends AbstractCallbackHandler
76
{
77
  public SwingCallbackHandler ()
78
  {
79
    super ("SWING");
80
  }
81
 
82
  protected void handleChoice (final ChoiceCallback callback)
83
    throws IOException
84
  {
85
    final JDialog dialog = new JDialog ();
86
    dialog.setResizable (false);
87
    Container content = dialog.getContentPane ();
88
    GridBagLayout layout = new GridBagLayout ();
89
    content.setLayout (layout);
90
    JLabel prompt = new JLabel (callback.getPrompt (), JLabel.LEFT);
91
    content.add (prompt, new GridBagConstraints (0, 0, 1, 1, 0, 0,
92
                                                 GridBagConstraints.WEST,
93
                                                 GridBagConstraints.NONE,
94
                                                 new Insets (5, 5, 5, 5), 5, 5));
95
 
96
    String[] choices = callback.getChoices ();
97
    final JList choicesList = new JList (choices);
98
    JScrollPane choicesPane = new JScrollPane (choicesList,
99
                                               JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
100
                                               JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
101
    final int defaultChoice = callback.getDefaultChoice ();
102
    choicesList.setSelectedIndex (defaultChoice);
103
    choicesList.setSelectionMode (callback.allowMultipleSelections ()
104
                                  ? ListSelectionModel.MULTIPLE_INTERVAL_SELECTION
105
                                  : ListSelectionModel.SINGLE_SELECTION);
106
    content.add (choicesPane, new GridBagConstraints (0, 1, 1, 1, 1.0, 1.0,
107
                                                      GridBagConstraints.CENTER,
108
                                                      GridBagConstraints.BOTH,
109
                                                      new Insets (0, 10, 0, 10), 5, 5));
110
 
111
    JPanel confirmButtons = new JPanel ();
112
    confirmButtons.setLayout (new FlowLayout (FlowLayout.RIGHT));
113
    JButton cancel = new JButton (messages.getString ("callback.cancel"));
114
    JButton ok = new JButton (messages.getString ("callback.ok"));
115
    confirmButtons.add (cancel);
116
    confirmButtons.add (ok);
117
    content.add (confirmButtons, new GridBagConstraints (0, 2, 1, 1, 0, 0,
118
                                                         GridBagConstraints.EAST,
119
                                                         GridBagConstraints.NONE,
120
                                                         new Insets (5, 5, 5, 5),
121
                                                         0, 0));
122
    dialog.getRootPane ().setDefaultButton (ok);
123
 
124
    cancel.addActionListener (new ActionListener ()
125
      {
126
        public void actionPerformed (final ActionEvent ae)
127
        {
128
          callback.setSelectedIndex (defaultChoice);
129
          dialog.setVisible (false);
130
          synchronized (callback)
131
            {
132
              callback.notify ();
133
            }
134
        }
135
      });
136
    ok.addActionListener (new ActionListener ()
137
      {
138
        public void actionPerformed (final ActionEvent ae)
139
        {
140
          if (callback.allowMultipleSelections ())
141
            {
142
              int[] indices = choicesList.getSelectedIndices ();
143
              if (indices != null && indices.length > 0)
144
                callback.setSelectedIndexes (indices);
145
              else
146
                callback.setSelectedIndex (defaultChoice);
147
            }
148
          else
149
            {
150
              int selected = choicesList.getSelectedIndex ();
151
              if (selected != -1)
152
                callback.setSelectedIndex (selected);
153
              else
154
                callback.setSelectedIndex (defaultChoice);
155
            }
156
          dialog.setVisible (false);
157
          synchronized (callback)
158
            {
159
              callback.notify ();
160
            }
161
        }
162
      });
163
 
164
    dialog.pack ();
165
    dialog.setSize (new Dimension (400, 400));
166
    dialog.setVisible (true);
167
    waitForInput (dialog, callback);
168
  }
169
 
170
  protected void handleConfirmation (final ConfirmationCallback callback)
171
    throws IOException
172
  {
173
    final JDialog dialog = new JDialog ();
174
    switch (callback.getMessageType ())
175
      {
176
      case ConfirmationCallback.ERROR:
177
        dialog.setTitle (messages.getString ("callback.error"));
178
        break;
179
      case ConfirmationCallback.WARNING:
180
        dialog.setTitle (messages.getString ("callback.warning"));
181
        break;
182
      case ConfirmationCallback.INFORMATION:
183
        dialog.setTitle (messages.getString ("callback.information"));
184
        break;
185
      }
186
    Container content = dialog.getContentPane ();
187
    content.setLayout (new GridBagLayout ());
188
 
189
    String prompt = callback.getPrompt ();
190
    if (prompt != null)
191
      {
192
        content.add (new JLabel (prompt),
193
                     new GridBagConstraints (0, 0, 1, 1, 0, 0,
194
                                             GridBagConstraints.WEST,
195
                                             GridBagConstraints.NONE,
196
                                             new Insets (5, 5, 5, 25), 0, 0));
197
      }
198
 
199
    final String[] options = callback.getOptions ();
200
    ActionListener listener = new ActionListener ()
201
      {
202
        public void actionPerformed (ActionEvent ae)
203
        {
204
          String cmd = ae.getActionCommand ();
205
          if (options != null)
206
            {
207
              for (int i = 0; i < options.length; i++)
208
                {
209
                  if (cmd.equals (options[i]))
210
                    {
211
                      callback.setSelectedIndex (i);
212
                      break;
213
                    }
214
                }
215
            }
216
          else
217
            {
218
              if (cmd.equals ("cancel"))
219
                callback.setSelectedIndex (ConfirmationCallback.CANCEL);
220
              else if (cmd.equals ("okay"))
221
                callback.setSelectedIndex (ConfirmationCallback.OK);
222
              else if (cmd.equals ("yes"))
223
                callback.setSelectedIndex (ConfirmationCallback.YES);
224
              else if (cmd.equals ("no"))
225
                callback.setSelectedIndex (ConfirmationCallback.NO);
226
            }
227
          dialog.setVisible (false);
228
          synchronized (callback)
229
            {
230
              callback.notify ();
231
            }
232
        }
233
      };
234
 
235
    JPanel buttons = new JPanel ();
236
    buttons.setLayout (new FlowLayout (FlowLayout.RIGHT));
237
    switch (callback.getOptionType ())
238
      {
239
      case ConfirmationCallback.YES_NO_CANCEL_OPTION:
240
        {
241
          JButton cancel = new JButton (messages.getString ("callback.cancel"));
242
          buttons.add (cancel);
243
          cancel.setActionCommand ("cancel");
244
          cancel.addActionListener (listener);
245
        }
246
        /* Fall-through. */
247
      case ConfirmationCallback.YES_NO_OPTION:
248
        {
249
          JButton yes = new JButton (messages.getString ("callback.yes"));
250
          JButton no = new JButton (messages.getString ("callback.no"));
251
          buttons.add (no);
252
          buttons.add (yes);
253
          yes.setActionCommand ("yes");
254
          yes.addActionListener (listener);
255
          no.setActionCommand ("no");
256
          no.addActionListener (listener);
257
          dialog.getRootPane ().setDefaultButton (yes);
258
        }
259
        break;
260
      case ConfirmationCallback.OK_CANCEL_OPTION:
261
        {
262
          JButton okay = new JButton (messages.getString ("callback.ok"));
263
          JButton cancel = new JButton (messages.getString ("callback.cancel"));
264
          buttons.add (cancel);
265
          buttons.add (okay);
266
          okay.setActionCommand ("okay");
267
          okay.addActionListener (listener);
268
          cancel.setActionCommand ("cancel");
269
          cancel.addActionListener (listener);
270
          dialog.getRootPane ().setDefaultButton (okay);
271
        }
272
        break;
273
      case ConfirmationCallback.UNSPECIFIED_OPTION:
274
        for (int i = 0; i < options.length; i++)
275
          {
276
            JButton button = new JButton (options[i]);
277
            buttons.add (button);
278
            button.setActionCommand (options[i]);
279
            button.addActionListener (listener);
280
            if (i == options.length - 1)
281
              dialog.getRootPane ().setDefaultButton (button);
282
          }
283
      }
284
    content.add (buttons,
285
                 new GridBagConstraints (0, GridBagConstraints.RELATIVE,
286
                                         1, 1, 1, 1,
287
                                         GridBagConstraints.SOUTHEAST,
288
                                         GridBagConstraints.BOTH,
289
                                         new Insets (5, 5, 5, 5), 0, 0));
290
    dialog.setResizable (false);
291
    dialog.pack ();
292
    dialog.setVisible (true);
293
    waitForInput (dialog, callback);
294
  }
295
 
296
  protected void handleLanguage (final LanguageCallback callback)
297
    throws IOException
298
  {
299
    Locale locale = Locale.getDefault ();
300
    Locale[] locales = Locale.getAvailableLocales ();
301
    String[] localeNames = new String[locales.length+1];
302
    int defaultIndex = 0;
303
    for (int i = 0; i < locales.length; i++)
304
      {
305
        localeNames[i+1] = locales[i].getDisplayLanguage (locales[i]);
306
        String country = locales[i].getDisplayCountry (locales[i]);
307
        if (country.length () > 0)
308
          localeNames[i+1] += " (" + country + ")";
309
        if (locales[i].equals (locale))
310
          defaultIndex = i;
311
      }
312
    locales[0] = locale;
313
    localeNames[0] = locale.getDisplayLanguage (locale);
314
    String country = locale.getDisplayCountry (locale);
315
    if (country.length () > 0)
316
      localeNames[0] += " (" + country + ")";
317
    ChoiceCallback cb = new ChoiceCallback (messages.getString ("callback.language"),
318
                                                                localeNames, 0,
319
                                                                false);
320
    handleChoice (cb);
321
    int selected = cb.getSelectedIndexes ()[0];
322
    if (selected > 0)
323
      callback.setLocale (locales[selected - 1]);
324
    else
325
      callback.setLocale (locale);
326
  }
327
 
328
  protected void handleName (final NameCallback callback)
329
    throws IOException
330
  {
331
    final JDialog dialog = new JDialog ();
332
    Container content = dialog.getContentPane ();
333
    content.setLayout (new GridBagLayout ());
334
 
335
    content.add (new JLabel (callback.getPrompt ()),
336
                             new GridBagConstraints (0, 0, 1, 1, 0, 0,
337
                                                     GridBagConstraints.NORTHEAST,
338
                                                     GridBagConstraints.VERTICAL,
339
                                                     new Insets (10, 10, 15, 5), 0, 0));
340
 
341
    final JTextField name = new JTextField ();
342
    name.setColumns (20);
343
    String _name;
344
    if ((_name = callback.getDefaultName ()) != null)
345
      name.setText (_name);
346
    content.add (name, new GridBagConstraints (1, 0, 1, 1, 1, 1,
347
                                               GridBagConstraints.NORTHWEST,
348
                                               GridBagConstraints.BOTH,
349
                                               new Insets (10, 5, 15, 10), 0, 0));
350
 
351
    ActionListener listener = new ActionListener ()
352
      {
353
        public void actionPerformed (ActionEvent ae)
354
        {
355
          String cmd = ae.getActionCommand ();
356
          if (cmd.equals ("okay"))
357
            callback.setName (name.getText ());
358
          dialog.setVisible (false);
359
          synchronized (callback)
360
            {
361
              callback.notify ();
362
            }
363
        }
364
      };
365
 
366
    JPanel buttons = new JPanel ();
367
    buttons.setLayout (new FlowLayout (FlowLayout.RIGHT));
368
    JButton cancel = new JButton (messages.getString ("callback.cancel"));
369
    JButton okay = new JButton (messages.getString ("callback.ok"));
370
    cancel.setActionCommand ("cancel");
371
    cancel.addActionListener (listener);
372
    buttons.add (cancel);
373
    okay.setActionCommand ("okay");
374
    okay.addActionListener (listener);
375
    buttons.add (okay);
376
    content.add (buttons, new GridBagConstraints (0, 1, 2, 1, 0, 0,
377
                                                  GridBagConstraints.SOUTHEAST,
378
                                                  GridBagConstraints.NONE,
379
                                                  new Insets (0, 10, 10, 10), 0, 0));
380
 
381
    dialog.setResizable (false);
382
    dialog.pack ();
383
    dialog.setVisible (true);
384
    dialog.getRootPane ().setDefaultButton (okay);
385
    waitForInput (dialog, callback);
386
  }
387
 
388
  protected void handlePassword (final PasswordCallback callback)
389
    throws IOException
390
  {
391
    final JDialog dialog = new JDialog ();
392
    Container content = dialog.getContentPane ();
393
    content.setLayout (new GridBagLayout ());
394
 
395
    content.add (new JLabel (callback.getPrompt ()),
396
                             new GridBagConstraints (0, 0, 1, 1, 0, 0,
397
                                                     GridBagConstraints.NORTHEAST,
398
                                                     GridBagConstraints.VERTICAL,
399
                                                     new Insets (10, 10, 15, 5), 0, 0));
400
 
401
    final JPasswordField password = new JPasswordField ();
402
    password.setColumns (20);
403
    password.setEchoChar (callback.isEchoOn () ? '\u0000' : '\u2022');
404
    content.add (password, new GridBagConstraints (1, 0, 1, 1, 1, 1,
405
                                                   GridBagConstraints.NORTHWEST,
406
                                                   GridBagConstraints.BOTH,
407
                                                   new Insets (10, 5, 15, 10), 0, 0));
408
 
409
    ActionListener listener = new ActionListener ()
410
      {
411
        public void actionPerformed (ActionEvent ae)
412
        {
413
          String cmd = ae.getActionCommand ();
414
          if (cmd.equals ("okay"))
415
            callback.setPassword (password.getPassword ());
416
          dialog.setVisible (false);
417
          synchronized (callback)
418
            {
419
              callback.notify ();
420
            }
421
        }
422
      };
423
 
424
    JPanel buttons = new JPanel ();
425
    buttons.setLayout (new FlowLayout (FlowLayout.RIGHT));
426
    JButton cancel = new JButton (messages.getString ("callback.cancel"));
427
    JButton okay = new JButton (messages.getString ("callback.ok"));
428
    cancel.setActionCommand ("cancel");
429
    cancel.addActionListener (listener);
430
    buttons.add (cancel);
431
    okay.setActionCommand ("okay");
432
    okay.addActionListener (listener);
433
    buttons.add (okay);
434
    content.add (buttons, new GridBagConstraints (0, 1, 2, 1, 0, 0,
435
                                                  GridBagConstraints.SOUTHEAST,
436
                                                  GridBagConstraints.NONE,
437
                                                  new Insets (0, 10, 10, 10), 0, 0));
438
 
439
    dialog.setResizable (false);
440
    dialog.pack ();
441
    dialog.setVisible (true);
442
    dialog.getRootPane ().setDefaultButton (okay);
443
    waitForInput (dialog, callback);
444
  }
445
 
446
  protected void handleTextInput (final TextInputCallback callback)
447
    throws IOException
448
  {
449
    final JDialog dialog = new JDialog ();
450
    Container content = dialog.getContentPane ();
451
    content.setLayout (new GridBagLayout ());
452
 
453
    content.add (new JLabel (callback.getPrompt ()),
454
                             new GridBagConstraints (0, 0, 1, 1, 0, 0,
455
                                                     GridBagConstraints.NORTHWEST,
456
                                                     GridBagConstraints.NONE,
457
                                                     new Insets (10, 10, 15, 5), 0, 0));
458
 
459
    final JTextArea text = new JTextArea (24, 80);
460
    text.setEditable (true);
461
    String _text;
462
    if ((_text = callback.getDefaultText ()) != null)
463
      text.setText (_text);
464
    text.setFont (new Font ("Monospaced", Font.PLAIN, 12));
465
    JScrollPane textPane = new JScrollPane (text,
466
                                            JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
467
                                            JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
468
    content.add (textPane,
469
                 new GridBagConstraints (0, 1, 1, 1, 1, 1,
470
                                         GridBagConstraints.CENTER,
471
                                         GridBagConstraints.BOTH,
472
                                         new Insets (5, 10, 5, 10), 0, 0));
473
 
474
    ActionListener listener = new ActionListener ()
475
      {
476
        public void actionPerformed (ActionEvent ae)
477
        {
478
          String cmd = ae.getActionCommand ();
479
          if (cmd.equals ("okay"))
480
            callback.setText (text.getText ());
481
          dialog.setVisible (false);
482
          synchronized (callback)
483
            {
484
              callback.notify ();
485
            }
486
        }
487
      };
488
 
489
    JPanel buttons = new JPanel ();
490
    buttons.setLayout (new FlowLayout (FlowLayout.RIGHT));
491
    JButton cancel = new JButton (messages.getString ("callback.cancel"));
492
    JButton okay = new JButton (messages.getString ("callback.ok"));
493
    cancel.setActionCommand ("cancel");
494
    cancel.addActionListener (listener);
495
    buttons.add (cancel);
496
    okay.setActionCommand ("okay");
497
    okay.addActionListener (listener);
498
    buttons.add (okay);
499
    content.add (buttons, new GridBagConstraints (0, 2, 1, 1, 0, 0,
500
                                                  GridBagConstraints.SOUTHEAST,
501
                                                  GridBagConstraints.NONE,
502
                                                  new Insets (0, 10, 10, 10), 0, 0));
503
 
504
    dialog.setResizable (true);
505
    dialog.pack ();
506
    dialog.setVisible (true);
507
    dialog.getRootPane ().setDefaultButton (okay);
508
    waitForInput (dialog, callback);
509
  }
510
 
511
  protected void handleTextOutput (final TextOutputCallback callback)
512
    throws IOException
513
  {
514
    final JDialog dialog = new JDialog ();
515
    switch (callback.getMessageType ())
516
      {
517
      case TextOutputCallback.ERROR:
518
        dialog.setTitle (messages.getString ("callback.error"));
519
        break;
520
      case TextOutputCallback.WARNING:
521
        dialog.setTitle (messages.getString ("callback.warning"));
522
        break;
523
      case TextOutputCallback.INFORMATION:
524
        dialog.setTitle (messages.getString ("callback.information"));
525
        break;
526
      }
527
    Container content = dialog.getContentPane ();
528
    content.setLayout (new GridBagLayout ());
529
 
530
    final JTextArea text = new JTextArea (24, 80);
531
    text.setEditable (false);
532
    text.setText (callback.getMessage ());
533
    text.setFont (new Font ("Monospaced", Font.PLAIN, 12));
534
    JScrollPane textPane = new JScrollPane (text,
535
                                            JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
536
                                            JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
537
    content.add (textPane,
538
                 new GridBagConstraints (0, 0, 1, 1, 1, 1,
539
                                         GridBagConstraints.CENTER,
540
                                         GridBagConstraints.BOTH,
541
                                         new Insets (10, 10, 5, 10), 0, 0));
542
 
543
    ActionListener listener = new ActionListener ()
544
      {
545
        public void actionPerformed (ActionEvent ae)
546
        {
547
          dialog.setVisible (false);
548
          synchronized (callback)
549
            {
550
              callback.notify ();
551
            }
552
        }
553
      };
554
 
555
    JButton okay = new JButton (messages.getString ("callback.ok"));
556
    okay.setActionCommand ("okay");
557
    okay.addActionListener (listener);
558
    content.add (okay, new GridBagConstraints (0, 1, 1, 1, 0, 0,
559
                                               GridBagConstraints.SOUTHEAST,
560
                                               GridBagConstraints.NONE,
561
                                               new Insets (0, 10, 10, 10), 0, 0));
562
 
563
    dialog.setResizable (true);
564
    dialog.pack ();
565
    dialog.setVisible (true);
566
    dialog.getRootPane ().setDefaultButton (okay);
567
    waitForInput (dialog, callback);
568
  }
569
 
570
  private void waitForInput (JDialog dialog, Callback callback)
571
  {
572
    synchronized (callback)
573
      {
574
        while (dialog.isVisible ())
575
          {
576
            try
577
              {
578
                callback.wait (1000);
579
              }
580
            catch (InterruptedException ignored)
581
              {
582
              }
583
          }
584
      }
585
    dialog.dispose ();
586
  }
587
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.