KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > share > configbean > customizers > SecurityAddGroupPanel


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 /*
20  * SecurityAddGroupPanel.java
21  *
22  * Created on April 12, 2006, 11:57 PM
23  */

24
25 package org.netbeans.modules.j2ee.sun.share.configbean.customizers;
26
27 import java.awt.BorderLayout JavaDoc;
28 import java.awt.Dimension JavaDoc;
29 import java.awt.GridBagConstraints JavaDoc;
30 import java.awt.Insets JavaDoc;
31 import java.beans.PropertyChangeEvent JavaDoc;
32 import java.beans.PropertyChangeListener JavaDoc;
33 import java.text.MessageFormat JavaDoc;
34 import java.util.ArrayList JavaDoc;
35 import java.util.Collection JavaDoc;
36 import java.util.ResourceBundle JavaDoc;
37 import javax.swing.JPanel JavaDoc;
38 import javax.swing.JScrollPane JavaDoc;
39 import javax.swing.JTable JavaDoc;
40 import javax.swing.ListSelectionModel JavaDoc;
41 import javax.swing.event.ListSelectionEvent JavaDoc;
42 import javax.swing.event.ListSelectionListener JavaDoc;
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 /**
52  *
53  * @author Peter Williams
54  */

55 public class SecurityAddGroupPanel extends JPanel JavaDoc implements ListSelectionListener JavaDoc {
56     
57     private static final ResourceBundle JavaDoc customizerBundle = ResourceBundle.getBundle(
58         "org.netbeans.modules.j2ee.sun.share.configbean.customizers.Bundle"); // NOI18N
59

60     private final GroupTableModel groupModel;
61
62     private Dimension JavaDoc initialPreferredSize;
63     
64     private String JavaDoc groupName;
65     
66     /** group names table & model
67      */

68     private JTable JavaDoc existingGroupsTable;
69     private SecurityMasterListModel existingGroupsModel;
70     
71     /**
72      * Creates new form SecurityAddGroupPanel
73      */

74     public SecurityAddGroupPanel(GroupTableModel gml) {
75         groupModel = gml;
76         groupName = null;
77         
78         initComponents();
79         initUserComponents();
80         initFields();
81     }
82     
83     protected String JavaDoc getGrouplName() {
84         return groupName;
85     }
86
87     /** This method is called from within the constructor to
88      * initialize the form.
89      * WARNING: Do NOT modify this code. The content of this method is
90      * always regenerated by the Form Editor.
91      */

92     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
93
private void initComponents() {
94         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
95
96         buttonGroup = new javax.swing.ButtonGroup JavaDoc();
97         jLabel1 = new javax.swing.JLabel JavaDoc();
98         jLblRequiredMark = new javax.swing.JLabel JavaDoc();
99         jLblGroupName = new javax.swing.JLabel JavaDoc();
100         jTxtGroupName = new javax.swing.JTextField JavaDoc();
101         jLabel2 = new javax.swing.JLabel JavaDoc();
102
103         setLayout(new java.awt.GridBagLayout JavaDoc());
104
105         jLabel1.setText(customizerBundle.getString("LBL_GroupEntryDesc"));
106         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
107         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
108         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
109         gridBagConstraints.insets = new java.awt.Insets JavaDoc(6, 6, 0, 5);
110         add(jLabel1, gridBagConstraints);
111
112         jLblRequiredMark.setLabelFor(jTxtGroupName);
113         jLblRequiredMark.setText("*");
114         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
115         gridBagConstraints.insets = new java.awt.Insets JavaDoc(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 JavaDoc();
121         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
122         gridBagConstraints.insets = new java.awt.Insets JavaDoc(6, 6, 0, 0);
123         add(jLblGroupName, gridBagConstraints);
124
125         jTxtGroupName.addKeyListener(new java.awt.event.KeyAdapter JavaDoc() {
126             public void keyReleased(java.awt.event.KeyEvent JavaDoc evt) {
127                 jTxtGroupNameKeyReleased(evt);
128             }
129         });
130
131         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
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 JavaDoc(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 JavaDoc();
142         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
143         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
144         gridBagConstraints.insets = new java.awt.Insets JavaDoc(6, 6, 0, 5);
145         add(jLabel2, gridBagConstraints);
146
147     }// </editor-fold>//GEN-END:initComponents
148

149     private void jTxtGroupNameKeyReleased(java.awt.event.KeyEvent JavaDoc evt) {//GEN-FIRST:event_jTxtGroupNameKeyReleased
150
groupName = jTxtGroupName.getText();
151         firePropertyChange(Constants.USER_DATA_CHANGED, null, null);
152     }//GEN-LAST:event_jTxtGroupNameKeyReleased
153

154     // Variables declaration - do not modify//GEN-BEGIN:variables
155
private javax.swing.ButtonGroup JavaDoc buttonGroup;
156     private javax.swing.JLabel JavaDoc jLabel1;
157     private javax.swing.JLabel JavaDoc jLabel2;
158     private javax.swing.JLabel JavaDoc jLblGroupName;
159     private javax.swing.JLabel JavaDoc jLblRequiredMark;
160     private javax.swing.JTextField JavaDoc jTxtGroupName;
161     // End of variables declaration//GEN-END:variables
162

163     private void initUserComponents() {
164         /* Save preferred size before adding table. We have our own width and
165          * will add a constant of our own choosing for the height in init(), below.
166          */

167         initialPreferredSize = getPreferredSize();
168         
169         /** Add table after preferred size is saved.
170          */

171         existingGroupsTable = new FixedHeightJTable();
172         existingGroupsTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
173         existingGroupsTable.getSelectionModel().addListSelectionListener(this);
174         
175         JScrollPane JavaDoc scrollPane = new JScrollPane JavaDoc();
176         scrollPane.setViewportView(existingGroupsTable);
177         GridBagConstraints JavaDoc gridBagConstraints = new GridBagConstraints JavaDoc();
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 JavaDoc(2, 30, 11, 11);
183         add(scrollPane, gridBagConstraints);
184         
185         getAccessibleContext().setAccessibleName(customizerBundle.getString("ACSN_AddGroupName")); // NOI18N
186
getAccessibleContext().setAccessibleDescription(customizerBundle.getString("ACSD_AddGroupName")); // NOI18N
187
}
188     
189     private void initFields() {
190         // Initialize table data model
191
existingGroupsModel = SecurityMasterListModel.getGroupMasterModel();
192         existingGroupsTable.setModel(existingGroupsModel);
193         
194         // Initialize text fields.
195
updateTextFields();
196         
197         // Set preferred size (just height really) to be something reasonable (because
198
// the default is unnecessarily tall).
199
setPreferredSize(new Dimension JavaDoc(initialPreferredSize.width, initialPreferredSize.height + 148));
200     }
201     
202     private void updateTextFields() {
203         jTxtGroupName.setText(groupName);
204     }
205
206     public void valueChanged(ListSelectionEvent JavaDoc e) {
207         ListSelectionModel JavaDoc selModel = existingGroupsTable.getSelectionModel();
208         if(!selModel.getValueIsAdjusting()) {
209             int selectedRow = existingGroupsTable.getSelectedRow();
210             if(selectedRow != -1) {
211                 Object JavaDoc entry = existingGroupsModel.getRow(selectedRow);
212                 if(entry instanceof String JavaDoc) {
213                     groupName = (String JavaDoc) entry;
214                     updateTextFields();
215                     firePropertyChange(Constants.USER_DATA_CHANGED, null, null);
216                 }
217             }
218         }
219     }
220
221     Collection JavaDoc getErrors() {
222         // Validate what the user typed in as a valid group name
223
ArrayList JavaDoc errors = new ArrayList JavaDoc();
224         String JavaDoc newGroupName = getGrouplName();
225
226         /** New name must not be blank (for add or edit version)
227          */

228         if(!Utils.notEmpty(newGroupName)) {
229             errors.add(customizerBundle.getString("ERR_BlankGroupName")); // NOI18N
230
}
231
232         /** Duplicate checking:
233          * Add operations always need to check for duplicates against
234          * the entire list.
235          */

236         if(newGroupName != null && groupModel.contains(newGroupName)) {
237             errors.add(MessageFormat.format(customizerBundle.getString("ERR_GroupExists"), new Object JavaDoc [] { newGroupName })); // NOI18N
238
}
239
240         return errors;
241     }
242     
243     private void commit() {
244         String JavaDoc newGroupName = getGrouplName();
245
246         // Add to security model of this descriptor
247
groupModel.addElement(newGroupName);
248
249         // Also add to global mapping list if not already present.
250
if(!existingGroupsModel.contains(newGroupName)) {
251             existingGroupsModel.addElement(newGroupName);
252         }
253     }
254     
255     /** Puts up an 'Add...' dialog, doing validation against the supplied model,
256      * and ultimately updating the data model if the user hits <OK> and clears
257      * any errors.
258      *
259      * @param parent JPanel that is the parent of this popup - used for centering and sizing.
260      * @param theModel The particular Security model instance we're updating.
261      */

262     static void addGroupName(JPanel JavaDoc parent, GroupTableModel model) {
263         SecurityAddGroupPanel addGroupPanel = new SecurityAddGroupPanel(model);
264         addGroupPanel.displayDialog(parent, customizerBundle.getString("TITLE_AddGroup"), // NOI18N
265
HelpContext.HELP_SECURITY_NEW_GROUP); // NOI18N
266
}
267     
268     private void displayDialog(JPanel JavaDoc parent, String JavaDoc title, String JavaDoc 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 JavaDoc errors = getErrors();
280
281                 if(dialog.hasErrors()) {
282                     // !PW is this even necessary w/ new validation model?
283
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 JavaDoc panelHelpId;
294
295         public BetterInputDialog(JPanel JavaDoc parent, String JavaDoc title, String JavaDoc helpId, SecurityAddGroupPanel childPanel) {
296             super(parent, title);
297
298             dialogPanel = childPanel;
299             panelHelpId = helpId;
300
301             dialogPanel.setPreferredSize(new Dimension JavaDoc(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 JavaDoc() {
316                 public void propertyChange(PropertyChangeEvent JavaDoc evt) {
317                     handleErrorDisplay();
318                 }
319             });
320         }
321
322         private void handleErrorDisplay() {
323             ArrayList JavaDoc errors = new ArrayList JavaDoc();
324             errors.addAll(dialogPanel.getErrors());
325             setErrors(errors);
326         }
327
328         protected String JavaDoc getHelpId() {
329             return panelHelpId;
330         }
331     }
332 }
333
Popular Tags