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