KickJava   Java API By Example, From Geeks To Geeks.

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


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  * SecurityEditGroupPanel.java
21  *
22  * Created on April 13, 2006, 12:15 AM
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.beans.PropertyChangeEvent JavaDoc;
30 import java.beans.PropertyChangeListener JavaDoc;
31 import java.text.MessageFormat JavaDoc;
32 import java.util.ArrayList JavaDoc;
33 import java.util.Collection JavaDoc;
34 import java.util.ResourceBundle JavaDoc;
35 import javax.swing.JPanel JavaDoc;
36
37 import org.netbeans.modules.j2ee.sun.share.Constants;
38 import org.netbeans.modules.j2ee.sun.share.configbean.Utils;
39 import org.netbeans.modules.j2ee.sun.share.configbean.customizers.common.HelpContext;
40 import org.netbeans.modules.j2ee.sun.share.configbean.customizers.common.InputDialog;
41
42 /**
43  *
44  * @author Peter Williams
45  */

46 public class SecurityEditGroupPanel extends JPanel JavaDoc {
47     
48     private static final ResourceBundle JavaDoc customizerBundle = ResourceBundle.getBundle(
49         "org.netbeans.modules.j2ee.sun.share.configbean.customizers.Bundle"); // NOI18N
50

51     private final GroupTableModel groupModel;
52
53     private final String JavaDoc originalGroupName;
54     private String JavaDoc groupName;
55     
56     /** Creates new form SecurityEditGroupPanel */
57     public SecurityEditGroupPanel(String JavaDoc entry, GroupTableModel gml) {
58         groupModel = gml;
59         originalGroupName = groupName = entry;
60         
61         initComponents();
62         initUserComponents();
63         initFields();
64     }
65     
66     protected String JavaDoc getGroupName() {
67         return groupName;
68     }
69     
70     protected String JavaDoc getOriginalGroupName() {
71         return originalGroupName;
72     }
73
74     /** This method is called from within the constructor to
75      * initialize the form.
76      * WARNING: Do NOT modify this code. The content of this method is
77      * always regenerated by the Form Editor.
78      */

79     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
80
private void initComponents() {
81         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
82
83         jLblRequiredMark = new javax.swing.JLabel JavaDoc();
84         jLblGroupName = new javax.swing.JLabel JavaDoc();
85         jTxtGroupName = new javax.swing.JTextField JavaDoc();
86
87         setLayout(new java.awt.GridBagLayout JavaDoc());
88
89         jLblRequiredMark.setLabelFor(jTxtGroupName);
90         jLblRequiredMark.setText("*");
91         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
92         gridBagConstraints.insets = new java.awt.Insets JavaDoc(6, 6, 0, 0);
93         add(jLblRequiredMark, gridBagConstraints);
94
95         jLblGroupName.setLabelFor(jTxtGroupName);
96         jLblGroupName.setText(customizerBundle.getString("LBL_GroupName_1"));
97         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
98         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
99         gridBagConstraints.insets = new java.awt.Insets JavaDoc(6, 6, 0, 0);
100         add(jLblGroupName, gridBagConstraints);
101
102         jTxtGroupName.addKeyListener(new java.awt.event.KeyAdapter JavaDoc() {
103             public void keyReleased(java.awt.event.KeyEvent JavaDoc evt) {
104                 jTxtGroupNameKeyReleased(evt);
105             }
106         });
107
108         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
109         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
110         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
111         gridBagConstraints.weightx = 1.0;
112         gridBagConstraints.insets = new java.awt.Insets JavaDoc(6, 6, 0, 11);
113         add(jTxtGroupName, gridBagConstraints);
114         jTxtGroupName.getAccessibleContext().setAccessibleName(customizerBundle.getString("ACSN_GroupName"));
115         jTxtGroupName.getAccessibleContext().setAccessibleDescription(customizerBundle.getString("ACSD_GroupName"));
116
117     }// </editor-fold>//GEN-END:initComponents
118

119     private void jTxtGroupNameKeyReleased(java.awt.event.KeyEvent JavaDoc evt) {//GEN-FIRST:event_jTxtGroupNameKeyReleased
120
groupName = jTxtGroupName.getText();
121         firePropertyChange(Constants.USER_DATA_CHANGED, null, null);
122     }//GEN-LAST:event_jTxtGroupNameKeyReleased
123

124     // Variables declaration - do not modify//GEN-BEGIN:variables
125
private javax.swing.JLabel JavaDoc jLblGroupName;
126     private javax.swing.JLabel JavaDoc jLblRequiredMark;
127     private javax.swing.JTextField JavaDoc jTxtGroupName;
128     // End of variables declaration//GEN-END:variables
129

130     private void initUserComponents() {
131         getAccessibleContext().setAccessibleName(customizerBundle.getString("ACSN_EditGroupName")); // NOI18N
132
getAccessibleContext().setAccessibleDescription(customizerBundle.getString("ACSD_EditGroupName")); // NOI18N
133
}
134     
135     private void initFields() {
136         jTxtGroupName.setText(groupName);
137     }
138     
139     Collection JavaDoc getErrors() {
140         // Validate what the user typed in as a valid group name
141
ArrayList JavaDoc errors = new ArrayList JavaDoc();
142         String JavaDoc newGroupName = getGroupName();
143
144         /** New name must not be blank (for add or edit version)
145          */

146         if(!Utils.notEmpty(newGroupName)) {
147             errors.add(customizerBundle.getString("ERR_BlankGroupName")); // NOI18N
148
}
149
150         /** Duplicate checking:
151          * Add operations always need to check for duplicates against
152          * the entire list.
153          */

154         if(newGroupName != null && !newGroupName.equals(getOriginalGroupName()) &&
155                 groupModel.contains(newGroupName)) {
156             errors.add(MessageFormat.format(customizerBundle.getString("ERR_GroupExists"), new Object JavaDoc [] { newGroupName })); // NOI18N
157
}
158
159         return errors;
160     }
161     
162     /** Puts up an 'Edit...' dialog, doing validation against the supplied model,
163      * and ultimately updating the data model if the user hits <OK> and clears
164      * any errors.
165      *
166      * @param parent JPanel that is the parent of this popup - used for centering and sizing.
167      * @param entry The existing entry. This will be prefilled into the edit field.
168      * @param theModel The particular Security model instance we're updating.
169      */

170     static void editGroupName(JPanel JavaDoc parent, String JavaDoc entry, GroupTableModel model) {
171         SecurityEditGroupPanel editGroupPanel = new SecurityEditGroupPanel(entry, model);
172         editGroupPanel.displayDialog(parent, customizerBundle.getString("TITLE_EditGroup"), // NOI18N
173
HelpContext.HELP_SECURITY_EDIT_GROUP);
174     }
175     
176     private void displayDialog(JPanel JavaDoc parent, String JavaDoc title, String JavaDoc helpId) {
177         BetterInputDialog dialog = new BetterInputDialog(parent, title, helpId, this);
178
179         do {
180             int dialogChoice = dialog.display();
181
182             if(dialogChoice == dialog.CANCEL_OPTION) {
183                 break;
184             }
185
186             if(dialogChoice == dialog.OK_OPTION) {
187                 Collection JavaDoc errors = getErrors();
188
189                 String JavaDoc newGrouplName = getGroupName();
190                 String JavaDoc oldGroupName = getOriginalGroupName();
191
192                 if(dialog.hasErrors()) {
193                     // !PW is this even necessary w/ new validation model?
194
dialog.showErrors();
195                 } else {
196                     // Add to security model of this descriptor
197
if(!Utils.strEquals(newGrouplName, oldGroupName)) {
198                         groupModel.replaceElement(oldGroupName, newGrouplName);
199                     }
200                     
201                     // Also add to global mapping list if not already present.
202
// if(!existingGroupsModel.contains(newGrouplName)) {
203
// existingGroupsModel.addElement(newGrouplName);
204
// }
205
}
206             }
207         } while(dialog.hasErrors());
208     }
209
210     private static class BetterInputDialog extends InputDialog {
211         private final SecurityEditGroupPanel dialogPanel;
212         private final String JavaDoc panelHelpId;
213
214         public BetterInputDialog(JPanel JavaDoc parent, String JavaDoc title, String JavaDoc helpId, SecurityEditGroupPanel childPanel) {
215             super(parent, title);
216
217             dialogPanel = childPanel;
218             panelHelpId = helpId;
219
220             dialogPanel.setPreferredSize(new Dimension JavaDoc(parent.getWidth()*3/4,
221                 dialogPanel.getPreferredSize().height));
222
223             this.getAccessibleContext().setAccessibleName(dialogPanel.getAccessibleContext().getAccessibleName());
224             this.getAccessibleContext().setAccessibleDescription(dialogPanel.getAccessibleContext().getAccessibleDescription());
225
226             getContentPane().add(childPanel, BorderLayout.CENTER);
227             addListeners();
228             pack();
229             setLocationInside(parent);
230             handleErrorDisplay();
231         }
232
233         private void addListeners() {
234             dialogPanel.addPropertyChangeListener(Constants.USER_DATA_CHANGED, new PropertyChangeListener JavaDoc() {
235                 public void propertyChange(PropertyChangeEvent JavaDoc evt) {
236                     handleErrorDisplay();
237                 }
238             });
239         }
240
241         private void handleErrorDisplay() {
242             ArrayList JavaDoc errors = new ArrayList JavaDoc();
243             errors.addAll(dialogPanel.getErrors());
244             setErrors(errors);
245         }
246
247         protected String JavaDoc getHelpId() {
248             return panelHelpId;
249         }
250     }
251 }
252
Popular Tags