1 7 8 package com.sun.security.auth.callback; 9 10 11 import javax.security.auth.callback.Callback ; 12 import javax.security.auth.callback.CallbackHandler ; 13 import javax.security.auth.callback.ConfirmationCallback ; 14 import javax.security.auth.callback.NameCallback ; 15 import javax.security.auth.callback.PasswordCallback ; 16 import javax.security.auth.callback.TextOutputCallback ; 17 import javax.security.auth.callback.UnsupportedCallbackException ; 18 19 20 import java.awt.Component ; 21 import java.util.ArrayList ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 import javax.swing.Box ; 25 import javax.swing.JLabel ; 26 import javax.swing.JOptionPane ; 27 import javax.swing.JPasswordField ; 28 import javax.swing.JTextField ; 29 30 38 public class DialogCallbackHandler implements CallbackHandler { 39 40 41 42 43 private Component parentComponent; 44 private static final int JPasswordFieldLen = 8 ; 45 46 47 48 51 public DialogCallbackHandler() { } 52 53 59 public DialogCallbackHandler(Component parentComponent) { 60 this.parentComponent = parentComponent; 61 } 62 63 67 private static interface Action { 68 void perform(); 69 } 70 71 78 79 public void handle(Callback [] callbacks) 80 throws UnsupportedCallbackException 81 { 82 83 final List messages = new ArrayList (3); 84 85 86 final List okActions = new ArrayList (2); 87 88 ConfirmationInfo confirmation = new ConfirmationInfo(); 89 90 for (int i = 0; i < callbacks.length; i++) { 91 if (callbacks[i] instanceof TextOutputCallback ) { 92 TextOutputCallback tc = (TextOutputCallback ) callbacks[i]; 93 94 switch (tc.getMessageType()) { 95 case TextOutputCallback.INFORMATION: 96 confirmation.messageType = JOptionPane.INFORMATION_MESSAGE; 97 break; 98 case TextOutputCallback.WARNING: 99 confirmation.messageType = JOptionPane.WARNING_MESSAGE; 100 break; 101 case TextOutputCallback.ERROR: 102 confirmation.messageType = JOptionPane.ERROR_MESSAGE; 103 break; 104 default: 105 throw new UnsupportedCallbackException ( 106 callbacks[i], "Unrecognized message type"); 107 } 108 109 messages.add(tc.getMessage()); 110 111 } else if (callbacks[i] instanceof NameCallback ) { 112 final NameCallback nc = (NameCallback ) callbacks[i]; 113 114 JLabel prompt = new JLabel (nc.getPrompt()); 115 116 final JTextField name = new JTextField (); 117 String defaultName = nc.getDefaultName(); 118 if (defaultName != null) { 119 name.setText(defaultName); 120 } 121 122 126 Box namePanel = Box.createHorizontalBox(); 127 namePanel.add(prompt); 128 namePanel.add(name); 129 messages.add(namePanel); 130 131 132 okActions.add(new Action() { 133 public void perform() { 134 nc.setName(name.getText()); 135 } 136 }); 137 138 } else if (callbacks[i] instanceof PasswordCallback ) { 139 final PasswordCallback pc = (PasswordCallback ) callbacks[i]; 140 141 JLabel prompt = new JLabel (pc.getPrompt()); 142 143 final JPasswordField password = 144 new JPasswordField (JPasswordFieldLen); 145 if (!pc.isEchoOn()) { 146 password.setEchoChar('*'); 147 } 148 149 Box passwordPanel = Box.createHorizontalBox(); 150 passwordPanel.add(prompt); 151 passwordPanel.add(password); 152 messages.add(passwordPanel); 153 154 okActions.add(new Action() { 155 public void perform() { 156 pc.setPassword(password.getPassword()); 157 } 158 }); 159 160 } else if (callbacks[i] instanceof ConfirmationCallback ) { 161 ConfirmationCallback cc = (ConfirmationCallback )callbacks[i]; 162 163 confirmation.setCallback(cc); 164 if (cc.getPrompt() != null) { 165 messages.add(cc.getPrompt()); 166 } 167 168 } else { 169 throw new UnsupportedCallbackException ( 170 callbacks[i], "Unrecognized Callback"); 171 } 172 } 173 174 175 int result = JOptionPane.showOptionDialog( 176 parentComponent, 177 messages.toArray(), 178 "Confirmation", 179 confirmation.optionType, 180 confirmation.messageType, 181 null, 182 confirmation.options, 183 confirmation.initialValue); 184 185 186 if (result == JOptionPane.OK_OPTION 187 || result == JOptionPane.YES_OPTION) 188 { 189 Iterator iterator = okActions.iterator(); 190 while (iterator.hasNext()) { 191 ((Action) iterator.next()).perform(); 192 } 193 } 194 confirmation.handleResult(result); 195 } 196 197 201 private static class ConfirmationInfo { 202 203 private int[] translations; 204 205 int optionType = JOptionPane.OK_CANCEL_OPTION; 206 Object [] options = null; 207 Object initialValue = null; 208 209 int messageType = JOptionPane.QUESTION_MESSAGE; 210 211 private ConfirmationCallback callback; 212 213 214 void setCallback(ConfirmationCallback callback) 215 throws UnsupportedCallbackException 216 { 217 this.callback = callback; 218 219 int confirmationOptionType = callback.getOptionType(); 220 switch (confirmationOptionType) { 221 case ConfirmationCallback.YES_NO_OPTION: 222 optionType = JOptionPane.YES_NO_OPTION; 223 translations = new int[] { 224 JOptionPane.YES_OPTION, ConfirmationCallback.YES, 225 JOptionPane.NO_OPTION, ConfirmationCallback.NO, 226 JOptionPane.CLOSED_OPTION, ConfirmationCallback.NO 227 }; 228 break; 229 case ConfirmationCallback.YES_NO_CANCEL_OPTION: 230 optionType = JOptionPane.YES_NO_CANCEL_OPTION; 231 translations = new int[] { 232 JOptionPane.YES_OPTION, ConfirmationCallback.YES, 233 JOptionPane.NO_OPTION, ConfirmationCallback.NO, 234 JOptionPane.CANCEL_OPTION, ConfirmationCallback.CANCEL, 235 JOptionPane.CLOSED_OPTION, ConfirmationCallback.CANCEL 236 }; 237 break; 238 case ConfirmationCallback.OK_CANCEL_OPTION: 239 optionType = JOptionPane.OK_CANCEL_OPTION; 240 translations = new int[] { 241 JOptionPane.OK_OPTION, ConfirmationCallback.OK, 242 JOptionPane.CANCEL_OPTION, ConfirmationCallback.CANCEL, 243 JOptionPane.CLOSED_OPTION, ConfirmationCallback.CANCEL 244 }; 245 break; 246 case ConfirmationCallback.UNSPECIFIED_OPTION: 247 options = callback.getOptions(); 248 253 translations = new int[] { 254 JOptionPane.CLOSED_OPTION, callback.getDefaultOption() 255 }; 256 break; 257 default: 258 throw new UnsupportedCallbackException ( 259 callback, 260 "Unrecognized option type: " + confirmationOptionType); 261 } 262 263 int confirmationMessageType = callback.getMessageType(); 264 switch (confirmationMessageType) { 265 case ConfirmationCallback.WARNING: 266 messageType = JOptionPane.WARNING_MESSAGE; 267 break; 268 case ConfirmationCallback.ERROR: 269 messageType = JOptionPane.ERROR_MESSAGE; 270 break; 271 case ConfirmationCallback.INFORMATION: 272 messageType = JOptionPane.INFORMATION_MESSAGE; 273 break; 274 default: 275 throw new UnsupportedCallbackException ( 276 callback, 277 "Unrecognized message type: " + confirmationMessageType); 278 } 279 } 280 281 282 283 void handleResult(int result) { 284 if (callback == null) { 285 return; 286 } 287 288 for (int i = 0; i < translations.length; i += 2) { 289 if (translations[i] == result) { 290 result = translations[i + 1]; 291 break; 292 } 293 } 294 callback.setSelectedIndex(result); 295 } 296 } 297 } 298 | Popular Tags |