KickJava   Java API By Example, From Geeks To Geeks.

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


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  * SecurityAddPrincipalPanel.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 import javax.swing.event.TableModelListener JavaDoc;
44 import javax.swing.table.TableModel JavaDoc;
45
46 import org.netbeans.modules.j2ee.sun.share.Constants;
47 import org.netbeans.modules.j2ee.sun.share.PrincipalNameMapping;
48 import org.netbeans.modules.j2ee.sun.share.SecurityMasterListModel;
49 import org.netbeans.modules.j2ee.sun.share.configbean.ASDDVersion;
50 import org.netbeans.modules.j2ee.sun.share.configbean.Utils;
51 import org.netbeans.modules.j2ee.sun.share.configbean.customizers.common.FixedHeightJTable;
52 import org.netbeans.modules.j2ee.sun.share.configbean.customizers.common.HelpContext;
53 import org.netbeans.modules.j2ee.sun.share.configbean.customizers.common.InputDialog;
54
55 /**
56  *
57  * @author Peter Williams
58  */

59 public class SecurityAddPrincipalPanel extends JPanel JavaDoc implements ListSelectionListener JavaDoc {
60     
61     private static final ResourceBundle JavaDoc customizerBundle = ResourceBundle.getBundle(
62         "org.netbeans.modules.j2ee.sun.share.configbean.customizers.Bundle"); // NOI18N
63

64     private final PrincipalTableModel principalModel;
65
66     private Dimension JavaDoc initialPreferredSize;
67     
68     private String JavaDoc principalName;
69     private String JavaDoc className;
70     
71     /** principal names table & model
72      */

73     private JTable JavaDoc existingPrincipalsTable;
74     private SecurityMasterListModel existingPrincipalsModel;
75     private TableModel JavaDoc versionedModel;
76     
77     // true if AS 9.0+ fields are visible.
78
private final boolean as90FeaturesVisible;
79     
80     /**
81      * Creates new form SecurityAddPrincipalPanel
82      */

83     private SecurityAddPrincipalPanel(PrincipalTableModel pml, ASDDVersion asVersion) {
84         principalModel = pml;
85         principalName = className = null;
86         
87         as90FeaturesVisible = ASDDVersion.SUN_APPSERVER_9_0.compareTo(asVersion) <= 0;
88         
89         initComponents();
90         initUserComponents();
91         initFields();
92     }
93     
94     protected String JavaDoc getPrincipalName() {
95         return principalName;
96     }
97
98     protected String JavaDoc getClassName() {
99         return className;
100     }
101
102     /** This method is called from within the constructor to
103      * initialize the form.
104      * WARNING: Do NOT modify this code. The content of this method is
105      * always regenerated by the Form Editor.
106      */

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

189     private void jTxtClassNameKeyReleased(java.awt.event.KeyEvent JavaDoc evt) {//GEN-FIRST:event_jTxtClassNameKeyReleased
190
className = jTxtClassName.getText();
191         firePropertyChange(Constants.USER_DATA_CHANGED, null, null);
192     }//GEN-LAST:event_jTxtClassNameKeyReleased
193

194     private void jTxtPrincipalNameKeyReleased(java.awt.event.KeyEvent JavaDoc evt) {//GEN-FIRST:event_jTxtPrincipalNameKeyReleased
195
principalName = jTxtPrincipalName.getText();
196         firePropertyChange(Constants.USER_DATA_CHANGED, null, null);
197     }//GEN-LAST:event_jTxtPrincipalNameKeyReleased
198

199     
200     // Variables declaration - do not modify//GEN-BEGIN:variables
201
private javax.swing.ButtonGroup JavaDoc buttonGroup;
202     private javax.swing.JLabel JavaDoc jLblClassName;
203     private javax.swing.JLabel JavaDoc jLblPrincipalEntryDesc;
204     private javax.swing.JLabel JavaDoc jLblPrincipalName;
205     private javax.swing.JLabel JavaDoc jLblPrincipalTableDesc;
206     private javax.swing.JLabel JavaDoc jLblRequiredMark;
207     private javax.swing.JTextField JavaDoc jTxtClassName;
208     private javax.swing.JTextField JavaDoc jTxtPrincipalName;
209     // End of variables declaration//GEN-END:variables
210

211     private void initUserComponents() {
212         /** Adjust panel for 8.1 vs. 9.0 features:
213          */

214         if(!as90FeaturesVisible) {
215             jLblPrincipalEntryDesc.setText(customizerBundle.getString("LBL_PrincipalEntryDesc81")); // NOI18N
216
jLblClassName.setVisible(false);
217             jTxtClassName.setVisible(false);
218         }
219         
220         /* Save preferred size before adding table. We have our own width and
221          * will add a constant of our own choosing for the height in init(), below.
222          */

223         initialPreferredSize = getPreferredSize();
224         
225         /** Add table after preferred size is saved.
226          */

227         existingPrincipalsTable = new FixedHeightJTable();
228         existingPrincipalsTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
229         existingPrincipalsTable.getSelectionModel().addListSelectionListener(this);
230         
231         JScrollPane JavaDoc scrollPane = new JScrollPane JavaDoc();
232         scrollPane.setViewportView(existingPrincipalsTable);
233         GridBagConstraints JavaDoc gridBagConstraints = new GridBagConstraints JavaDoc();
234         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
235         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
236         gridBagConstraints.weightx = 1.0;
237         gridBagConstraints.weighty = 1.0;
238         gridBagConstraints.insets = new Insets JavaDoc(2, 30, 11, 11);
239         add(scrollPane, gridBagConstraints);
240         
241         getAccessibleContext().setAccessibleName(customizerBundle.getString("ACSN_AddPrincipalName")); // NOI18N
242
getAccessibleContext().setAccessibleDescription(customizerBundle.getString("ACSD_AddPrincipalName")); // NOI18N
243
}
244     
245     private void initFields() {
246         // Initialize table data model
247
existingPrincipalsModel = SecurityMasterListModel.getPrincipalMasterModel();
248         versionedModel = new VersionedPrincipalTableModel(existingPrincipalsModel, as90FeaturesVisible);
249         existingPrincipalsTable.setModel(versionedModel);
250
251         // Initialize text fields.
252
updateTextFields();
253         
254         // Set preferred size (just height really) to be something reasonable (because
255
// the default is unnecessarily tall).
256
setPreferredSize(new Dimension JavaDoc(initialPreferredSize.width, initialPreferredSize.height + 148));
257     }
258     
259     private void updateTextFields() {
260         jTxtPrincipalName.setText(principalName);
261         jTxtClassName.setText(className);
262     }
263
264     public void valueChanged(ListSelectionEvent JavaDoc e) {
265         ListSelectionModel JavaDoc selModel = existingPrincipalsTable.getSelectionModel();
266         if(!selModel.getValueIsAdjusting()) {
267             int selectedRow = existingPrincipalsTable.getSelectedRow();
268             if(selectedRow != -1) {
269                 Object JavaDoc entry = existingPrincipalsModel.getRow(selectedRow);
270                 if(entry instanceof PrincipalNameMapping) {
271                     PrincipalNameMapping principalEntry = (PrincipalNameMapping) entry;
272
273                     principalName = principalEntry.getPrincipalName();
274                     className = principalEntry.getClassName();
275                     updateTextFields();
276                     
277                     firePropertyChange(Constants.USER_DATA_CHANGED, null, null);
278                 }
279             }
280         }
281     }
282
283     Collection JavaDoc getErrors() {
284         // Validate what the user typed in as a valid principal name
285
ArrayList JavaDoc errors = new ArrayList JavaDoc();
286         String JavaDoc newPrincipalName = getPrincipalName();
287
288         /** New name must not be blank (for add or edit version)
289          */

290         if(!Utils.notEmpty(newPrincipalName)) {
291             errors.add(customizerBundle.getString("ERR_BlankPrincipalName")); // NOI18N
292
}
293
294         /** Duplicate checking:
295          * Add operations always need to check for duplicates against
296          * the entire list.
297          */

298         if(newPrincipalName != null && principalModel.contains(new PrincipalNameMapping(newPrincipalName))) {
299             errors.add(MessageFormat.format(customizerBundle.getString("ERR_PrincipalExists"), new Object JavaDoc [] { newPrincipalName })); // NOI18N
300
}
301
302         /** Class name:
303          * If specified, the classname field must contain a semantically valid
304          * java classname. We do not verify if the class exists or is accessible.
305          */

306         if(as90FeaturesVisible) {
307             String JavaDoc newClassName = getClassName();
308             if(Utils.notEmpty(newClassName) && !Utils.isJavaClass(newClassName)) {
309                 errors.add(customizerBundle.getString("ERR_InvalidJavaClassName")); // NOI18N
310
}
311         }
312
313         return errors;
314     }
315
316     private void commit() {
317         String JavaDoc newPrincipalName = getPrincipalName();
318         String JavaDoc newClassName = getClassName();
319
320         // Add to security model of this descriptor
321
principalModel.addElement(new PrincipalNameMapping(newPrincipalName, newClassName));
322
323         // Also add to global mapping list if not already present.
324
PrincipalNameMapping tmpMapping = new PrincipalNameMapping(newPrincipalName, newClassName);
325         if(!existingPrincipalsModel.contains(tmpMapping)) {
326             existingPrincipalsModel.addElement(tmpMapping);
327         }
328     }
329     
330     /** Puts up an 'Add...' dialog, doing validation against the supplied model,
331      * and ultimately updating the data model if the user hits <OK> and clears
332      * any errors.
333      *
334      * @param parent JPanel that is the parent of this popup - used for centering and sizing.
335      * @param theModel The particular Security model instance we're updating.
336      */

337     static void addPrincipalName(JPanel JavaDoc parent, PrincipalTableModel model, ASDDVersion asVersion) {
338         SecurityAddPrincipalPanel addPrincipalPanel = new SecurityAddPrincipalPanel(model, asVersion);
339         addPrincipalPanel.displayDialog(parent, customizerBundle.getString("TITLE_AddPrincipal"), // NOI18N
340
HelpContext.HELP_SECURITY_NEW_PRINCIPAL);
341     }
342
343     private void displayDialog(JPanel JavaDoc parent, String JavaDoc title, String JavaDoc helpId) {
344         BetterInputDialog dialog = new BetterInputDialog(parent, title, helpId, this);
345
346         do {
347             int dialogChoice = dialog.display();
348
349             if(dialogChoice == dialog.CANCEL_OPTION) {
350                 break;
351             }
352
353             if(dialogChoice == dialog.OK_OPTION) {
354                 Collection JavaDoc errors = getErrors();
355
356                 if(dialog.hasErrors()) {
357                     // !PW is this even necessary w/ new validation model?
358
dialog.showErrors();
359                 } else {
360                     commit();
361                 }
362             }
363         } while(dialog.hasErrors());
364     }
365
366     private static class BetterInputDialog extends InputDialog {
367         private final SecurityAddPrincipalPanel dialogPanel;
368         private final String JavaDoc panelHelpId;
369
370         public BetterInputDialog(JPanel JavaDoc parent, String JavaDoc title, String JavaDoc helpId, SecurityAddPrincipalPanel childPanel) {
371             super(parent, title);
372
373             dialogPanel = childPanel;
374             panelHelpId = helpId;
375
376             dialogPanel.setPreferredSize(new Dimension JavaDoc(parent.getWidth()*3/4,
377                 dialogPanel.getPreferredSize().height));
378
379             this.getAccessibleContext().setAccessibleName(dialogPanel.getAccessibleContext().getAccessibleName());
380             this.getAccessibleContext().setAccessibleDescription(dialogPanel.getAccessibleContext().getAccessibleDescription());
381
382             getContentPane().add(childPanel, BorderLayout.CENTER);
383             addListeners();
384             pack();
385             setLocationInside(parent);
386             handleErrorDisplay();
387         }
388
389         private void addListeners() {
390             dialogPanel.addPropertyChangeListener(Constants.USER_DATA_CHANGED, new PropertyChangeListener JavaDoc() {
391                 public void propertyChange(PropertyChangeEvent JavaDoc evt) {
392                     handleErrorDisplay();
393                 }
394             });
395         }
396
397         private void handleErrorDisplay() {
398             ArrayList JavaDoc errors = new ArrayList JavaDoc();
399             errors.addAll(dialogPanel.getErrors());
400             setErrors(errors);
401         }
402
403         protected String JavaDoc getHelpId() {
404             return panelHelpId;
405         }
406     }
407     
408     /** Put a wrapper around the master principal table model that is disable
409      * display of the classname column for appserver version < 9.0
410      */

411     private static class VersionedPrincipalTableModel implements TableModel JavaDoc {
412         
413         private final SecurityMasterListModel realModel;
414         private final boolean showClassColumn;
415        
416         public VersionedPrincipalTableModel(SecurityMasterListModel model, boolean showClasses) {
417             realModel = model;
418             showClassColumn = showClasses;
419         }
420
421         public void removeTableModelListener(TableModelListener JavaDoc l) {
422             realModel.removeTableModelListener(l);
423         }
424
425         public void addTableModelListener(TableModelListener JavaDoc l) {
426             realModel.addTableModelListener(l);
427         }
428
429         public void setValueAt(Object JavaDoc aValue, int rowIndex, int columnIndex) {
430             realModel.setValueAt(aValue, rowIndex, columnIndex);
431         }
432
433         public String JavaDoc getColumnName(int columnIndex) {
434             return realModel.getColumnName(columnIndex);
435         }
436
437         public Class JavaDoc getColumnClass(int columnIndex) {
438             return realModel.getColumnClass(columnIndex);
439         }
440
441         public boolean isCellEditable(int rowIndex, int columnIndex) {
442             return realModel.isCellEditable(rowIndex, columnIndex);
443         }
444
445         public Object JavaDoc getValueAt(int rowIndex, int columnIndex) {
446             return realModel.getValueAt(rowIndex, columnIndex);
447         }
448
449         public int getRowCount() {
450             return realModel.getRowCount();
451         }
452
453         public int getColumnCount() {
454             return showClassColumn ? realModel.getColumnCount() : 1;
455         }
456     }
457 }
458
Popular Tags