1 21 package org.jacorb.security.util; 22 23 import java.awt.*; 24 import javax.swing.*; 25 import java.awt.event.ActionListener ; 26 import java.awt.event.ActionEvent ; 27 28 34 35 public class UserSponsor 36 extends JDialog 37 implements ActionListener 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 49 UserSponsor(String title, 50 String message, 51 String [] clear_input_labels, 52 String [] opaque_input_labels) 53 { 54 this(title, message, clear_input_labels, null, null, opaque_input_labels); 55 } 56 57 58 59 61 UserSponsor(String title, 62 String message, 63 String [] clear_input_labels, 64 String [] list_option_labels, 65 String [][] input_options, 66 String [] 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 ( 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 163 167 168 public void actionPerformed(ActionEvent 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 [] clear_input, 198 char[][] opaque_input) 199 { 200 201 return getInput(clear_input, null, opaque_input); 202 } 203 204 public boolean getInput(String [] clear_input, String [] 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 ("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 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 )listOptions[i].getModel().getElementAt(idx); 256 } 257 } 258 } 259 260 return !cancelled; 261 } 262 263 public static char[] getPasswd(String message) 264 { 265 char[][] passwordHolder = new char[1][]; 266 UserSponsor us = new UserSponsor( "Password", 267 message, 268 null, new String [] { "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 |