1 /* 2 * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 3 * Use is subject to license terms. 4 * 5 * ident "%Z%%M% %I% %E% SMI" 6 */ 7 8 import java.awt.*; 9 import java.awt.event.*; 10 import java.text.*; 11 import java.util.*; 12 13 /* 14 * This class creates a dialog box that helps the user select encryption types 15 * with some mouse clicks. The dialog box need only be created 16 * once. The Ok and Cancel buttons merely call setVisible with an 17 * argument of false. 18 */ 19 20 // The layout will consist of 2 panels: 21 // topPanel contains the dynamic list of encryption type check boxes. 22 // bottomPanel contains the buttons ok, clear, cancel, and help. 23 // The two panels are separated by a LineSeparator. 24 25 public class EncListDialog extends Dialog { 26 27 private boolean save; 28 29 private int i; 30 31 private Frame parent; 32 33 private Button ok; 34 private Button clear; 35 private Button cancel; 36 private Button help; 37 38 private HelpDialog hd = null; 39 40 private Panel topPanel; 41 private Panel bottomPanel; 42 43 private static Toolkit toolkit = Toolkit.getDefaultToolkit(); 44 45 private Kadmin kadmin; 46 private Checkbox cb[]; 47 private Integer grp_num[]; 48 private String encList = ""; 49 50 // For I18N 51 private static ResourceBundle rb = 52 ResourceBundle.getBundle("GuiResource" /* NOI18N */); 53 private static ResourceBundle hrb = 54 ResourceBundle.getBundle("HelpData" /* NOI18N */); 55 56 /* 57 * Constructor that lays out the components and sets the different 58 * event handlers. 59 */ 60 public EncListDialog(Frame parent, Color background, Color foreground, 61 Kadmin session) { 62 super(parent, getString("SEAM Encryption Type List Helper"), 63 true); 64 65 this.parent = parent; 66 67 this.kadmin = session; 68 69 setLayout(new GridBagLayout()); 70 addCheckboxes(); 71 72 addButtons(); 73 setSize(250, 300); 74 setResizable(true); 75 76 addWindowListener(new DCWindowListener()); 77 } 78 79 /* 80 * Adds the check boxes only 81 */ 82 private void addCheckboxes() { 83 84 GridBagConstraints gbc = new GridBagConstraints(); 85 86 gbc.weighty = 1; 87 88 topPanel = new Panel(); 89 topPanel.setLayout(new GridBagLayout()); 90 gbc.gridwidth = GridBagConstraints.REMAINDER; 91 gbc.fill = GridBagConstraints.BOTH; 92 gbc.anchor = GridBagConstraints.CENTER; 93 gbc.gridx = 0; 94 gbc.gridy = 0; 95 add(topPanel, gbc); 96 97 gbc.fill = GridBagConstraints.NONE; 98 gbc.anchor = GridBagConstraints.WEST; 99 gbc.gridx = 0; 100 gbc.gridwidth = 1; 101 102 String et[] = kadmin.getEncList(); 103 104 cb = new Checkbox[et.length]; 105 grp_num = new Integer[et.length]; 106 107 for (int i = 0; i < et.length; i++) { 108 String[] grp_enc = et[i].split(" "); 109 cb[i] = new Checkbox(grp_enc[1]); 110 CBListener cbl = new CBListener(); 111 cb[i].addItemListener(cbl); 112 grp_num[i] = new Integer(grp_enc[0]); 113 gbc.gridy = i; 114 topPanel.add(cb[i], gbc); 115 } 116 } 117 118 // Adds all the buttons 119 private void addButtons() { 120 121 GridBagConstraints gbc = new GridBagConstraints(); 122 gbc.weighty = 1; 123 124 gbc.gridwidth = GridBagConstraints.REMAINDER; 125 gbc.gridheight = 1; 126 gbc.fill = GridBagConstraints.BOTH; 127 gbc.gridx = 0; 128 gbc.gridy = 2; 129 add(new LineSeparator(), gbc); 130 131 bottomPanel = new Panel(); 132 ok = new Button(getString("OK")); 133 clear = new Button(getString("Clear")); 134 cancel = new Button(getString("Cancel")); 135 help = new Button(getString("Help")); 136 bottomPanel.add(ok); 137 bottomPanel.add(clear); 138 bottomPanel.add(cancel); 139 bottomPanel.add(help); 140 gbc.fill = GridBagConstraints.HORIZONTAL; 141 gbc.gridwidth = GridBagConstraints.REMAINDER; 142 gbc.gridx = 0; 143 gbc.gridy = 3; 144 add(bottomPanel, gbc); 145 146 DCButtonListener bl = new DCButtonListener(); 147 ok.addActionListener(bl); 148 clear.addActionListener(bl); 149 cancel.addActionListener(bl); 150 help.addActionListener(bl); 151 } 152 153 /* 154 * Closes (hides) the dialog box when the user is done 155 * @param save true if the box is being dismissed by clicking on 156 * "ok" and the user wants to retain the modified value, false 157 * otherwise. 158 */ 159 private void encListDialogClose(boolean save) { 160 this.save = save; 161 setVisible(false); 162 } 163 164 /* 165 * Checks if the user requested that the value in this 166 * EncListDialog be used e.g., by clicking on "Ok" instead of 167 * "Cancel." 168 * @return true if the user wants to save the value in the 169 * EncListDialog, false otherwise. 170 */ 171 172 public boolean isSaved() { 173 return save; 174 } 175 /* 176 * Sets the current enc list for the principal during modification. 177 * @param enc types of current principal. 178 */ 179 public void setEncTypes(String e_str) { 180 181 if (e_str.compareTo("") == 0) 182 return; 183 184 String[] e_list = e_str.split(" "); 185 186 for (int i = 0; i < e_list.length; i++) { 187 for (int j = 0; j < cb.length; j++) { 188 if (cb[j].getLabel().compareTo(e_list[i]) 189 == 0) { 190 cb[j].setState(true); 191 break; 192 } 193 } 194 } 195 } 196 197 // *********************************************** 198 // I N N E R C L A S S E S F O L L O W 199 // *********************************************** 200 201 /* 202 * Listener for an annoying work around in deselection of a check box 203 * in case the user doesn't want any items in a grouped list. 204 */ 205 private class CBListener implements ItemListener { 206 207 public void itemStateChanged(ItemEvent e) { 208 Checkbox c = (Checkbox) e.getItemSelectable(); 209 210 if (e.getStateChange() == e.DESELECTED) { 211 c.setState(false); 212 } else if (e.getStateChange() == e.SELECTED) { 213 for (int i = 0; i < cb.length; i++) { 214 if (c == cb[i]) { 215 for (int j = 0; j < cb.length; j++) { 216 if (grp_num[j].equals(grp_num[i]) 217 == true) { 218 cb[j].setState(false); 219 } 220 } 221 break; 222 } 223 } 224 c.setState(true); 225 // else what else is there 226 } 227 } 228 } 229 230 /* 231 * Listener for closing the dialog box through the window close 232 * menu. 233 */ 234 private class DCWindowListener extends WindowAdapter { 235 236 public void windowClosing(WindowEvent e) { 237 encListDialogClose(false); 238 } 239 } 240 241 /* 242 * Listener for all the buttons. The listener is shared for the sake 243 * of reducing the number of overall listeners. 244 * TBD: I18N the help 245 */ 246 private class DCButtonListener implements ActionListener { 247 248 public void actionPerformed(ActionEvent e) { 249 if (e.getSource() == ok) { 250 EncListDialog.this.encListDialogClose(true); 251 } else if (e.getSource() == cancel) { 252 EncListDialog.this.encListDialogClose(false); 253 } else if (e.getSource() == clear) { 254 for (int i = 0; i < cb.length; i++) { 255 cb[i].setState(false); 256 } 257 } else if (e.getSource() == help) { 258 if (hd != null) 259 hd.show(); 260 else { 261 hd = new HelpDialog( 262 EncListDialog.this.parent, 263 getString( 264 "Help for Encryption Type Dialog"), 265 false); 266 hd.setVisible(true); 267 hd.setText(getString(hrb, 268 "EncryptionTypeDialogHelp")); 269 } 270 } 271 } // actionPerformed 272 } 273 274 /* 275 * The string representation of the dialog box. 276 * @return a String which contians the encryption type list 277 */ 278 public String toString() { 279 280 for (int i = 0; i < cb.length; i++) { 281 if (cb[i].getState() == true) 282 encList = encList.concat(cb[i].getLabel() + 283 " "); 284 } 285 return encList; 286 } 287 288 /* 289 * Call rb.getString(), but catch exception and return English 290 * key so that small spelling errors don't cripple the GUI 291 */ 292 private static final String getString(String key) { 293 return (getString(rb, key)); 294 } 295 296 private static final String getString(ResourceBundle rb, String key) { 297 try { 298 String res = rb.getString(key); 299 return res; 300 } catch (MissingResourceException e) { 301 System.out.println("Missing resource "+key+ 302 ", using English."); 303 return key; 304 } 305 } 306 }