1 19 24 25 package org.netbeans.modules.j2ee.sun.share.configbean.customizers; 26 27 import java.awt.BorderLayout ; 28 import java.awt.Dimension ; 29 import java.awt.GridBagConstraints ; 30 import java.awt.Insets ; 31 import java.beans.PropertyChangeEvent ; 32 import java.beans.PropertyChangeListener ; 33 import java.text.MessageFormat ; 34 import java.util.ArrayList ; 35 import java.util.Collection ; 36 import java.util.ResourceBundle ; 37 import javax.swing.JPanel ; 38 import javax.swing.JScrollPane ; 39 import javax.swing.JTable ; 40 import javax.swing.ListSelectionModel ; 41 import javax.swing.event.ListSelectionEvent ; 42 import javax.swing.event.ListSelectionListener ; 43 44 import org.netbeans.modules.j2ee.sun.share.Constants; 45 import org.netbeans.modules.j2ee.sun.share.SecurityMasterListModel; 46 import org.netbeans.modules.j2ee.sun.share.configbean.Utils; 47 import org.netbeans.modules.j2ee.sun.share.configbean.customizers.common.FixedHeightJTable; 48 import org.netbeans.modules.j2ee.sun.share.configbean.customizers.common.HelpContext; 49 import org.netbeans.modules.j2ee.sun.share.configbean.customizers.common.InputDialog; 50 51 55 public class SecurityAddGroupPanel extends JPanel implements ListSelectionListener { 56 57 private static final ResourceBundle customizerBundle = ResourceBundle.getBundle( 58 "org.netbeans.modules.j2ee.sun.share.configbean.customizers.Bundle"); 60 private final GroupTableModel groupModel; 61 62 private Dimension initialPreferredSize; 63 64 private String groupName; 65 66 68 private JTable existingGroupsTable; 69 private SecurityMasterListModel existingGroupsModel; 70 71 74 public SecurityAddGroupPanel(GroupTableModel gml) { 75 groupModel = gml; 76 groupName = null; 77 78 initComponents(); 79 initUserComponents(); 80 initFields(); 81 } 82 83 protected String getGrouplName() { 84 return groupName; 85 } 86 87 92 private void initComponents() { 94 java.awt.GridBagConstraints gridBagConstraints; 95 96 buttonGroup = new javax.swing.ButtonGroup (); 97 jLabel1 = new javax.swing.JLabel (); 98 jLblRequiredMark = new javax.swing.JLabel (); 99 jLblGroupName = new javax.swing.JLabel (); 100 jTxtGroupName = new javax.swing.JTextField (); 101 jLabel2 = new javax.swing.JLabel (); 102 103 setLayout(new java.awt.GridBagLayout ()); 104 105 jLabel1.setText(customizerBundle.getString("LBL_GroupEntryDesc")); 106 gridBagConstraints = new java.awt.GridBagConstraints (); 107 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 108 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 109 gridBagConstraints.insets = new java.awt.Insets (6, 6, 0, 5); 110 add(jLabel1, gridBagConstraints); 111 112 jLblRequiredMark.setLabelFor(jTxtGroupName); 113 jLblRequiredMark.setText("*"); 114 gridBagConstraints = new java.awt.GridBagConstraints (); 115 gridBagConstraints.insets = new java.awt.Insets (6, 30, 0, 0); 116 add(jLblRequiredMark, gridBagConstraints); 117 118 jLblGroupName.setLabelFor(jTxtGroupName); 119 jLblGroupName.setText(customizerBundle.getString("LBL_GroupName_1")); 120 gridBagConstraints = new java.awt.GridBagConstraints (); 121 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 122 gridBagConstraints.insets = new java.awt.Insets (6, 6, 0, 0); 123 add(jLblGroupName, gridBagConstraints); 124 125 jTxtGroupName.addKeyListener(new java.awt.event.KeyAdapter () { 126 public void keyReleased(java.awt.event.KeyEvent evt) { 127 jTxtGroupNameKeyReleased(evt); 128 } 129 }); 130 131 gridBagConstraints = new java.awt.GridBagConstraints (); 132 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 133 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 134 gridBagConstraints.weightx = 1.0; 135 gridBagConstraints.insets = new java.awt.Insets (6, 6, 0, 11); 136 add(jTxtGroupName, gridBagConstraints); 137 jTxtGroupName.getAccessibleContext().setAccessibleName(customizerBundle.getString("ACSN_GroupName")); 138 jTxtGroupName.getAccessibleContext().setAccessibleDescription(customizerBundle.getString("ACSD_GroupName")); 139 140 jLabel2.setText(customizerBundle.getString("LBL_GroupTableDescription")); 141 gridBagConstraints = new java.awt.GridBagConstraints (); 142 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 143 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 144 gridBagConstraints.insets = new java.awt.Insets (6, 6, 0, 5); 145 add(jLabel2, gridBagConstraints); 146 147 } 149 private void jTxtGroupNameKeyReleased(java.awt.event.KeyEvent evt) { groupName = jTxtGroupName.getText(); 151 firePropertyChange(Constants.USER_DATA_CHANGED, null, null); 152 } 154 private javax.swing.ButtonGroup buttonGroup; 156 private javax.swing.JLabel jLabel1; 157 private javax.swing.JLabel jLabel2; 158 private javax.swing.JLabel jLblGroupName; 159 private javax.swing.JLabel jLblRequiredMark; 160 private javax.swing.JTextField jTxtGroupName; 161 163 private void initUserComponents() { 164 167 initialPreferredSize = getPreferredSize(); 168 169 171 existingGroupsTable = new FixedHeightJTable(); 172 existingGroupsTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 173 existingGroupsTable.getSelectionModel().addListSelectionListener(this); 174 175 JScrollPane scrollPane = new JScrollPane (); 176 scrollPane.setViewportView(existingGroupsTable); 177 GridBagConstraints gridBagConstraints = new GridBagConstraints (); 178 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 179 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 180 gridBagConstraints.weightx = 1.0; 181 gridBagConstraints.weighty = 1.0; 182 gridBagConstraints.insets = new Insets (2, 30, 11, 11); 183 add(scrollPane, gridBagConstraints); 184 185 getAccessibleContext().setAccessibleName(customizerBundle.getString("ACSN_AddGroupName")); getAccessibleContext().setAccessibleDescription(customizerBundle.getString("ACSD_AddGroupName")); } 188 189 private void initFields() { 190 existingGroupsModel = SecurityMasterListModel.getGroupMasterModel(); 192 existingGroupsTable.setModel(existingGroupsModel); 193 194 updateTextFields(); 196 197 setPreferredSize(new Dimension (initialPreferredSize.width, initialPreferredSize.height + 148)); 200 } 201 202 private void updateTextFields() { 203 jTxtGroupName.setText(groupName); 204 } 205 206 public void valueChanged(ListSelectionEvent e) { 207 ListSelectionModel selModel = existingGroupsTable.getSelectionModel(); 208 if(!selModel.getValueIsAdjusting()) { 209 int selectedRow = existingGroupsTable.getSelectedRow(); 210 if(selectedRow != -1) { 211 Object entry = existingGroupsModel.getRow(selectedRow); 212 if(entry instanceof String ) { 213 groupName = (String ) entry; 214 updateTextFields(); 215 firePropertyChange(Constants.USER_DATA_CHANGED, null, null); 216 } 217 } 218 } 219 } 220 221 Collection getErrors() { 222 ArrayList errors = new ArrayList (); 224 String newGroupName = getGrouplName(); 225 226 228 if(!Utils.notEmpty(newGroupName)) { 229 errors.add(customizerBundle.getString("ERR_BlankGroupName")); } 231 232 236 if(newGroupName != null && groupModel.contains(newGroupName)) { 237 errors.add(MessageFormat.format(customizerBundle.getString("ERR_GroupExists"), new Object [] { newGroupName })); } 239 240 return errors; 241 } 242 243 private void commit() { 244 String newGroupName = getGrouplName(); 245 246 groupModel.addElement(newGroupName); 248 249 if(!existingGroupsModel.contains(newGroupName)) { 251 existingGroupsModel.addElement(newGroupName); 252 } 253 } 254 255 262 static void addGroupName(JPanel parent, GroupTableModel model) { 263 SecurityAddGroupPanel addGroupPanel = new SecurityAddGroupPanel(model); 264 addGroupPanel.displayDialog(parent, customizerBundle.getString("TITLE_AddGroup"), HelpContext.HELP_SECURITY_NEW_GROUP); } 267 268 private void displayDialog(JPanel parent, String title, String helpId) { 269 BetterInputDialog dialog = new BetterInputDialog(parent, title, helpId, this); 270 271 do { 272 int dialogChoice = dialog.display(); 273 274 if(dialogChoice == dialog.CANCEL_OPTION) { 275 break; 276 } 277 278 if(dialogChoice == dialog.OK_OPTION) { 279 Collection errors = getErrors(); 280 281 if(dialog.hasErrors()) { 282 dialog.showErrors(); 284 } else { 285 commit(); 286 } 287 } 288 } while(dialog.hasErrors()); 289 } 290 291 private static class BetterInputDialog extends InputDialog { 292 private final SecurityAddGroupPanel dialogPanel; 293 private final String panelHelpId; 294 295 public BetterInputDialog(JPanel parent, String title, String helpId, SecurityAddGroupPanel childPanel) { 296 super(parent, title); 297 298 dialogPanel = childPanel; 299 panelHelpId = helpId; 300 301 dialogPanel.setPreferredSize(new Dimension (parent.getWidth()*3/4, 302 dialogPanel.getPreferredSize().height)); 303 304 this.getAccessibleContext().setAccessibleName(dialogPanel.getAccessibleContext().getAccessibleName()); 305 this.getAccessibleContext().setAccessibleDescription(dialogPanel.getAccessibleContext().getAccessibleDescription()); 306 307 getContentPane().add(childPanel, BorderLayout.CENTER); 308 addListeners(); 309 pack(); 310 setLocationInside(parent); 311 handleErrorDisplay(); 312 } 313 314 private void addListeners() { 315 dialogPanel.addPropertyChangeListener(Constants.USER_DATA_CHANGED, new PropertyChangeListener () { 316 public void propertyChange(PropertyChangeEvent evt) { 317 handleErrorDisplay(); 318 } 319 }); 320 } 321 322 private void handleErrorDisplay() { 323 ArrayList errors = new ArrayList (); 324 errors.addAll(dialogPanel.getErrors()); 325 setErrors(errors); 326 } 327 328 protected String getHelpId() { 329 return panelHelpId; 330 } 331 } 332 } 333 | Popular Tags |