KickJava   Java API By Example, From Geeks To Geeks.

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


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  * SecurityEditPrincipalPanel.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.PrincipalNameMapping;
39 import org.netbeans.modules.j2ee.sun.share.configbean.ASDDVersion;
40 import org.netbeans.modules.j2ee.sun.share.configbean.Utils;
41 import org.netbeans.modules.j2ee.sun.share.configbean.customizers.common.HelpContext;
42 import org.netbeans.modules.j2ee.sun.share.configbean.customizers.common.InputDialog;
43
44 /**
45  *
46  * @author Peter Williams
47  */

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

53     private final PrincipalTableModel principalModel;
54
55     private final String JavaDoc originalPrincipalName;
56     private String JavaDoc principalName;
57
58     private final String JavaDoc originalClassName;
59     private String JavaDoc className;
60
61     // true if AS 9.0+ fields are visible.
62
private final boolean as90FeaturesVisible;
63
64     /**
65      * Creates new form SecurityEditPrincipalPanel
66      */

67     public SecurityEditPrincipalPanel(PrincipalNameMapping entry, PrincipalTableModel pml, ASDDVersion asVersion) {
68         principalModel = pml;
69         originalPrincipalName = principalName = entry.getPrincipalName();
70         originalClassName = className = entry.getClassName();
71         
72         as90FeaturesVisible = ASDDVersion.SUN_APPSERVER_9_0.compareTo(asVersion) <= 0;
73         
74         initComponents();
75         initUserComponents();
76         initFields();
77     }
78
79     protected String JavaDoc getPrincipalName() {
80         return principalName;
81     }
82     
83     protected String JavaDoc getOriginalPrincipalName() {
84         return originalPrincipalName;
85     }
86
87     protected String JavaDoc getClassName() {
88         return className;
89     }
90     
91     protected String JavaDoc getOriginalClassName() {
92         return originalClassName;
93     }
94
95     /** This method is called from within the constructor to
96      * initialize the form.
97      * WARNING: Do NOT modify this code. The content of this method is
98      * always regenerated by the Form Editor.
99      */

100     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
101
private void initComponents() {
102         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
103
104         jLblPrincipalEntryDesc = new javax.swing.JLabel JavaDoc();
105         jLblRequiredMark = new javax.swing.JLabel JavaDoc();
106         jLblPrincipalName = new javax.swing.JLabel JavaDoc();
107         jTxtPrincipalName = new javax.swing.JTextField JavaDoc();
108         jLblClassName = new javax.swing.JLabel JavaDoc();
109         jTxtClassName = new javax.swing.JTextField JavaDoc();
110
111         setLayout(new java.awt.GridBagLayout JavaDoc());
112
113         jLblPrincipalEntryDesc.setText(customizerBundle.getString("LBL_PrincipalEntryDesc90"));
114         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
115         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
116         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
117         gridBagConstraints.insets = new java.awt.Insets JavaDoc(6, 6, 0, 5);
118         add(jLblPrincipalEntryDesc, gridBagConstraints);
119
120         jLblRequiredMark.setLabelFor(jTxtPrincipalName);
121         jLblRequiredMark.setText("*");
122         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
123         gridBagConstraints.insets = new java.awt.Insets JavaDoc(6, 6, 0, 0);
124         add(jLblRequiredMark, gridBagConstraints);
125
126         jLblPrincipalName.setLabelFor(jTxtPrincipalName);
127         jLblPrincipalName.setText(customizerBundle.getString("LBL_PrincipalName_1"));
128         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
129         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
130         gridBagConstraints.insets = new java.awt.Insets JavaDoc(6, 6, 0, 0);
131         add(jLblPrincipalName, gridBagConstraints);
132
133         jTxtPrincipalName.addKeyListener(new java.awt.event.KeyAdapter JavaDoc() {
134             public void keyReleased(java.awt.event.KeyEvent JavaDoc evt) {
135                 jTxtPrincipalNameKeyReleased(evt);
136             }
137         });
138
139         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
140         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
141         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
142         gridBagConstraints.weightx = 1.0;
143         gridBagConstraints.insets = new java.awt.Insets JavaDoc(6, 6, 0, 11);
144         add(jTxtPrincipalName, gridBagConstraints);
145         jTxtPrincipalName.getAccessibleContext().setAccessibleName(customizerBundle.getString("ACSN_PrincipalName"));
146         jTxtPrincipalName.getAccessibleContext().setAccessibleDescription(customizerBundle.getString("ACSD_PrincipalName"));
147
148         jLblClassName.setLabelFor(jTxtClassName);
149         jLblClassName.setText(customizerBundle.getString("LBL_ClassName_1"));
150         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
151         gridBagConstraints.gridx = 1;
152         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
153         gridBagConstraints.insets = new java.awt.Insets JavaDoc(6, 6, 0, 0);
154         add(jLblClassName, gridBagConstraints);
155
156         jTxtClassName.addKeyListener(new java.awt.event.KeyAdapter JavaDoc() {
157             public void keyReleased(java.awt.event.KeyEvent JavaDoc evt) {
158                 jTxtClassNameKeyReleased(evt);
159             }
160         });
161
162         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
163         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
164         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
165         gridBagConstraints.weightx = 1.0;
166         gridBagConstraints.insets = new java.awt.Insets JavaDoc(6, 6, 0, 11);
167         add(jTxtClassName, gridBagConstraints);
168         jTxtClassName.getAccessibleContext().setAccessibleName(customizerBundle.getString("ACSN_ClassName"));
169         jTxtClassName.getAccessibleContext().setAccessibleDescription(customizerBundle.getString("ACSD_ClassName"));
170
171     }// </editor-fold>//GEN-END:initComponents
172

173     private void jTxtClassNameKeyReleased(java.awt.event.KeyEvent JavaDoc evt) {//GEN-FIRST:event_jTxtClassNameKeyReleased
174
className = jTxtClassName.getText();
175         firePropertyChange(Constants.USER_DATA_CHANGED, null, null);
176     }//GEN-LAST:event_jTxtClassNameKeyReleased
177

178     private void jTxtPrincipalNameKeyReleased(java.awt.event.KeyEvent JavaDoc evt) {//GEN-FIRST:event_jTxtPrincipalNameKeyReleased
179
principalName = jTxtPrincipalName.getText();
180         firePropertyChange(Constants.USER_DATA_CHANGED, null, null);
181     }//GEN-LAST:event_jTxtPrincipalNameKeyReleased
182

183     // Variables declaration - do not modify//GEN-BEGIN:variables
184
private javax.swing.JLabel JavaDoc jLblClassName;
185     private javax.swing.JLabel JavaDoc jLblPrincipalEntryDesc;
186     private javax.swing.JLabel JavaDoc jLblPrincipalName;
187     private javax.swing.JLabel JavaDoc jLblRequiredMark;
188     private javax.swing.JTextField JavaDoc jTxtClassName;
189     private javax.swing.JTextField JavaDoc jTxtPrincipalName;
190     // End of variables declaration//GEN-END:variables
191

192     private void initUserComponents() {
193         /** Adjust panel for 8.1 vs. 9.0 features:
194          */

195         if(!as90FeaturesVisible) {
196             jLblPrincipalEntryDesc.setText(customizerBundle.getString("LBL_PrincipalEntryDesc81")); // NOI18N
197
jLblClassName.setVisible(false);
198             jTxtClassName.setVisible(false);
199         }
200         
201         getAccessibleContext().setAccessibleName(customizerBundle.getString("ACSN_EditPrincipalName")); // NOI18N
202
getAccessibleContext().setAccessibleDescription(customizerBundle.getString("ACSD_EditPrincipalName")); // NOI18N
203
}
204     
205     private void initFields() {
206         jTxtPrincipalName.setText(principalName);
207         jTxtClassName.setText(className);
208     }
209     
210     Collection JavaDoc getErrors() {
211         // Validate what the user typed in as a valid principal name
212
ArrayList JavaDoc errors = new ArrayList JavaDoc();
213         String JavaDoc newPrincipalName = getPrincipalName();
214
215         /** New name must not be blank (for add or edit version)
216          */

217         if(!Utils.notEmpty(newPrincipalName)) {
218             errors.add(customizerBundle.getString("ERR_BlankPrincipalName")); // NOI18N
219
}
220
221         /** Duplicate checking:
222          * Add operations always need to check for duplicates against
223          * the entire list.
224          */

225         if(newPrincipalName != null && !newPrincipalName.equals(getOriginalPrincipalName()) &&
226                 principalModel.contains(new PrincipalNameMapping(newPrincipalName))) {
227             errors.add(MessageFormat.format(customizerBundle.getString("ERR_PrincipalExists"), new Object JavaDoc [] { newPrincipalName })); // NOI18N
228
}
229
230         /** Class name:
231          * If specified, the classname field must contain a semantically valid
232          * java classname. We do not verify if the class exists or is accessible.
233          */

234         String JavaDoc newClassName = getClassName();
235         if(Utils.notEmpty(newClassName) && !Utils.isJavaClass(newClassName)) {
236             errors.add(customizerBundle.getString("ERR_InvalidJavaClassName")); // NOI18N
237
}
238
239         return errors;
240     }
241     
242     /** Puts up an 'Edit...' dialog, doing validation against the supplied model,
243      * and ultimately updating the data model if the user hits <OK> and clears
244      * any errors.
245      *
246      * @param parent JPanel that is the parent of this popup - used for centering and sizing.
247      * @param entry The existing entry. This will be prefilled into the edit field.
248      * @param theModel The particular Security model instance we're updating.
249      */

250     static void editPrincipalName(JPanel JavaDoc parent, PrincipalNameMapping entry, PrincipalTableModel model, ASDDVersion asVersion) {
251         SecurityEditPrincipalPanel editPrincipalPanel = new SecurityEditPrincipalPanel(entry, model, asVersion);
252         editPrincipalPanel.displayDialog(parent, customizerBundle.getString("TITLE_EditPrincipal"), // NOI18N
253
HelpContext.HELP_SECURITY_EDIT_PRINCIPAL);
254     }
255     
256     private void displayDialog(JPanel JavaDoc parent, String JavaDoc title, String JavaDoc helpId) {
257         BetterInputDialog dialog = new BetterInputDialog(parent, title, helpId, this);
258
259         do {
260             int dialogChoice = dialog.display();
261
262             if(dialogChoice == dialog.CANCEL_OPTION) {
263                 break;
264             }
265
266             if(dialogChoice == dialog.OK_OPTION) {
267                 Collection JavaDoc errors = getErrors();
268
269                 String JavaDoc newPrincipalName = getPrincipalName();
270                 String JavaDoc oldPrincipalName = getOriginalPrincipalName();
271                 String JavaDoc newClassName = getClassName();
272                 String JavaDoc oldClassName = getOriginalClassName();
273
274                 if(dialog.hasErrors()) {
275                     // !PW is this even necessary w/ new validation model?
276
dialog.showErrors();
277                 } else {
278                     // Add to security model of this descriptor
279
if(!Utils.strEquals(newPrincipalName, oldPrincipalName) || !Utils.strEquals(newClassName, oldClassName)) {
280                         PrincipalNameMapping oldEntry = new PrincipalNameMapping(oldPrincipalName, oldClassName);
281                         PrincipalNameMapping newEntry = new PrincipalNameMapping(newPrincipalName, newClassName);
282                         principalModel.replaceElement(oldEntry, newEntry);
283                     }
284                     
285                     // Also add to global mapping list if not already present.
286
// PrincipalNameMapping tmpMapping = new PrincipalNameMapping(newPrincipalName, newClassName);
287
// if(!existingPrincipalsModel.contains(tmpMapping)) {
288
// existingPrincipalsModel.addElement(tmpMapping);
289
// }
290
}
291             }
292         } while(dialog.hasErrors());
293     }
294
295     private static class BetterInputDialog extends InputDialog {
296         private final SecurityEditPrincipalPanel dialogPanel;
297         private final String JavaDoc panelHelpId;
298
299         public BetterInputDialog(JPanel JavaDoc parent, String JavaDoc title, String JavaDoc helpId, SecurityEditPrincipalPanel childPanel) {
300             super(parent, title);
301
302             dialogPanel = childPanel;
303             panelHelpId = helpId;
304
305             dialogPanel.setPreferredSize(new Dimension JavaDoc(parent.getWidth()*3/4,
306                 dialogPanel.getPreferredSize().height));
307
308             this.getAccessibleContext().setAccessibleName(dialogPanel.getAccessibleContext().getAccessibleName());
309             this.getAccessibleContext().setAccessibleDescription(dialogPanel.getAccessibleContext().getAccessibleDescription());
310
311             getContentPane().add(childPanel, BorderLayout.CENTER);
312             addListeners();
313             pack();
314             setLocationInside(parent);
315             handleErrorDisplay();
316         }
317
318         private void addListeners() {
319             dialogPanel.addPropertyChangeListener(Constants.USER_DATA_CHANGED, new PropertyChangeListener JavaDoc() {
320                 public void propertyChange(PropertyChangeEvent JavaDoc evt) {
321                     handleErrorDisplay();
322                 }
323             });
324         }
325
326         private void handleErrorDisplay() {
327             ArrayList JavaDoc errors = new ArrayList JavaDoc();
328             errors.addAll(dialogPanel.getErrors());
329             setErrors(errors);
330         }
331
332         protected String JavaDoc getHelpId() {
333             return panelHelpId;
334         }
335     }
336 }
337
Popular Tags