KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > security > util > UserSponsor


1 /*
2  * JacORB - a free Java ORB
3  *
4  * Copyright (C) 1999-2004 Gerald Brose
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the Free
18  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  *
20  */

21 package org.jacorb.security.util;
22
23 import java.awt.*;
24 import javax.swing.*;
25 import java.awt.event.ActionListener JavaDoc;
26 import java.awt.event.ActionEvent JavaDoc;
27
28 /**
29  * A user sponsor, i.e. a frame that asks for user input
30  *
31  * @author Gerald Brose
32  * @version $Id: UserSponsor.java,v 1.7 2004/05/06 12:40:01 nicolas Exp $
33  */

34
35 public class UserSponsor
36     extends JDialog
37     implements ActionListener JavaDoc
38 {
39     private JButton okButton;
40     private JButton cancelButton;
41     private boolean done = false;
42     private boolean cancelled = false;
43     private JPasswordField [] opaqueFields;
44     private JTextField[] clearFields;
45     private JComboBox[] listOptions;
46
47     /**
48      */

49     UserSponsor(String JavaDoc title,
50         String JavaDoc message,
51         String JavaDoc[] clear_input_labels,
52         String JavaDoc[] opaque_input_labels)
53     {
54     this(title, message, clear_input_labels, null, null, opaque_input_labels);
55     }
56
57
58
59     /**
60      */

61     UserSponsor(String JavaDoc title,
62         String JavaDoc message,
63         String JavaDoc[] clear_input_labels,
64         String JavaDoc[] list_option_labels,
65         String JavaDoc[][] input_options,
66         String JavaDoc[] opaque_input_labels)
67     {
68     if( list_option_labels != null && input_options != null &&
69         list_option_labels.length != input_options.length )
70     {
71         throw new IllegalArgumentException JavaDoc(
72          "Number of list option labels must match number of input option lists!") ;
73     }
74
75     setTitle(title);
76     setModal(true);
77
78     done = false;
79     cancelled = false;
80
81     int clear_len = ( clear_input_labels != null ? clear_input_labels.length : 0 );
82     int list_len = ( list_option_labels != null ? list_option_labels.length : 0 );
83     int opaque_len = ( opaque_input_labels != null ? opaque_input_labels.length : 0 );
84
85     JPanel[] panels = new JPanel[clear_len + list_len + opaque_len + 2];
86
87     clearFields = new JTextField[clear_len];
88     opaqueFields = new JPasswordField[opaque_len];
89     listOptions = new JComboBox[list_len];
90     
91     panels[0] = new JPanel();
92     panels[0].setLayout( new BorderLayout());
93     panels[0].add( new JLabel( message ));
94
95     int idx = 1;
96
97     if( clear_len > 0 )
98     {
99         for( int i = 0; i < clear_len; i++ )
100         {
101         panels[idx+ i] = new JPanel();
102         panels[idx+ i].setLayout( new BorderLayout());
103         panels[idx+ i].add( new JLabel( clear_input_labels[i]), BorderLayout.WEST);
104         JTextField jtext = new JTextField(20);
105         jtext.addActionListener( this );
106         clearFields[i] = jtext;
107         panels[idx+ i].add( clearFields[i],BorderLayout.EAST);
108         panels[idx+i-1].add(panels[idx + i], BorderLayout.SOUTH);
109         }
110     }
111
112     idx += clear_len;
113
114     for( int i = 0; i < opaque_len; i++ )
115     {
116         panels[idx + i] = new JPanel();
117         panels[idx + i].setLayout( new BorderLayout());
118         panels[idx + i].add( new JLabel( opaque_input_labels[i]), BorderLayout.WEST);
119         JPasswordField jpwd = new JPasswordField(20);
120         jpwd.addActionListener( this );
121         opaqueFields[i] = jpwd;
122         panels[idx + i].add( opaqueFields[i],BorderLayout.EAST);
123         panels[idx + i-1].add(panels[idx + i], BorderLayout.SOUTH);
124     }
125
126     idx += opaque_len;
127
128     for( int i = 0; i < list_len; i++ )
129     {
130         panels[idx + i] = new JPanel();
131         panels[idx + i].setLayout( new BorderLayout());
132         panels[idx + i].add( new JLabel( list_option_labels[i]), BorderLayout.WEST);
133         listOptions[i] = new JComboBox( input_options[i] );
134         listOptions[i].setEditable(false);
135         panels[idx + i].add( listOptions[i],BorderLayout.EAST);
136         panels[idx + i-1].add(panels[idx + i], BorderLayout.SOUTH);
137     }
138
139     panels[panels.length-1] = new JPanel();
140
141     okButton = new JButton ("OK");
142     okButton.addActionListener(this);
143
144     panels[panels.length-1].add(okButton);
145
146     cancelButton = new JButton ("Cancel");
147     cancelButton.addActionListener(this);
148
149     panels[panels.length-1].add(cancelButton);
150
151     panels[panels.length-2].add( panels[panels.length-1], BorderLayout.SOUTH);
152
153     getContentPane().add(panels[0]);
154     pack();
155     setVisible(true);
156
157     }
158
159
160
161     // implementation of java.awt.event.ActionListener interface
162

163     /**
164      *
165      * @param param1 <description>
166      */

167
168     public void actionPerformed(ActionEvent JavaDoc event)
169     {
170        if( event.getSource() instanceof JTextField )
171        {
172        cancelled = false;
173        }
174        else
175        {
176        JButton _source = (JButton) event.getSource();
177     
178        if (_source == cancelButton)
179        {
180            cancelled = true;
181        }
182        else if (_source == okButton)
183        {
184            cancelled = false;
185        }
186        }
187
188        done = true;
189        synchronized( this )
190        {
191        notifyAll();
192        }
193        setVisible(false);
194        dispose();
195     }
196
197     public boolean getInput(String JavaDoc[] clear_input,
198                 char[][] opaque_input)
199     {
200
201     return getInput(clear_input, null, opaque_input);
202     }
203
204     public boolean getInput(String JavaDoc[] clear_input, String JavaDoc[] list_input,
205                 char[][] opaque_input)
206     {
207     if( list_input != null &&
208         ( listOptions == null ||
209         ( listOptions.length != list_input.length )
210           )
211         )
212     {
213         throw new IllegalArgumentException JavaDoc("Length of input list must match number of option lists!") ;
214     }
215
216     while(!done)
217     {
218         try
219         {
220         synchronized( this )
221         {
222             wait();
223         }
224         }
225         catch( InterruptedException JavaDoc ie )
226         {
227         cancelled = true;
228         done = true;
229         }
230     };
231     
232     if( !cancelled )
233     {
234         if( clear_input != null )
235         {
236         for( int i = 0; i < clear_input.length; i++ )
237             clear_input[i] = clearFields[i].getText();
238         }
239         
240         if( opaque_input != null )
241         {
242         for( int i = 0; i < opaque_input.length; i++ )
243             opaque_input[i] =opaqueFields[i].getPassword();
244         }
245
246
247         if( list_input != null )
248         {
249         for( int i = 0; i < list_input.length; i++ )
250         {
251             int idx = listOptions[i].getSelectedIndex();
252             if( idx == -1 )
253             list_input[i] = "<unselected";
254             else
255             list_input[i] = (String JavaDoc)listOptions[i].getModel().getElementAt(idx);
256         }
257         }
258     }
259     
260     return !cancelled;
261     }
262
263     public static char[] getPasswd(String JavaDoc message)
264     {
265         char[][] passwordHolder = new char[1][];
266         UserSponsor us = new UserSponsor( "Password",
267                           message,
268                           null, new String JavaDoc[] { "Password" }
269                           );
270         us.getInput ( null, passwordHolder );
271         return passwordHolder[ 0 ];
272     }
273
274
275 }
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
Popular Tags