KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ddloaders > web > multiview > SecurityRoleRefTablePanel


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
21 package org.netbeans.modules.j2ee.ddloaders.web.multiview;
22
23 import org.netbeans.modules.j2ee.dd.api.common.SecurityRole;
24 import org.netbeans.modules.j2ee.dd.api.common.SecurityRoleRef;
25 import org.netbeans.modules.j2ee.dd.api.web.Servlet;
26 import org.netbeans.modules.j2ee.dd.api.web.WebApp;
27 import org.netbeans.modules.j2ee.ddloaders.web.DDDataObject;
28 import org.netbeans.modules.xml.multiview.ui.DefaultTablePanel;
29 import org.netbeans.modules.xml.multiview.ui.EditDialog;
30 import org.openide.util.NbBundle;
31
32 /**
33  * SecurityRoleRefTablePanel.java
34  *
35  * Panel for displaying the securirty role reference table.
36  *
37  * @author ptliu
38  */

39 public class SecurityRoleRefTablePanel extends DefaultTablePanel {
40     
41     private SecurityRoleRefTableModel model;
42     private WebApp webApp;
43     private DDDataObject dObj;
44     private Servlet servlet;
45     
46     /** Creates new form SecurityRoleRefTablePanel */
47     public SecurityRoleRefTablePanel(final DDDataObject dObj,
48             final SecurityRoleRefTableModel model) {
49         super(model);
50         this.model=model;
51         this.dObj=dObj;
52         
53         removeButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
54             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
55                 dObj.modelUpdatedFromUI();
56                 dObj.setChangedFromUI(true);
57                 int row = getTable().getSelectedRow();
58                 model.removeRow(row);
59                 dObj.setChangedFromUI(false);
60             }
61         });
62         editButton.addActionListener(new TableActionListener(false));
63         addButton.addActionListener(new TableActionListener(true));
64     }
65     
66     void setModel(WebApp webApp, Servlet servlet, SecurityRoleRef[] roleRefs) {
67         model.setData(servlet, roleRefs);
68         model.setWebApp(webApp);
69         this.webApp=webApp;
70         this.servlet = servlet;
71     }
72     
73     private class TableActionListener implements java.awt.event.ActionListener JavaDoc {
74         private boolean add;
75         
76         TableActionListener(boolean add) {
77             this.add=add;
78         }
79         
80         public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
81             final int row = (add?-1:getTable().getSelectedRow());
82             final SecurityRoleRefPanel dialogPanel = new SecurityRoleRefPanel(
83                     webApp.getSecurityRole());
84             
85             if (!add) {
86                 SecurityRoleRef roleRef = model.getSecurityRoleRef(row);
87                 dialogPanel.setRoleRefName(roleRef.getRoleName());
88                 dialogPanel.setRoleRefLink(roleRef.getRoleLink());
89                 dialogPanel.setDescription(roleRef.getDefaultDescription());
90             }
91             
92             EditDialog dialog = new EditDialog(dialogPanel,NbBundle.getMessage(EjbRefsTablePanel.class,"TTL_SecurityRoleRef"),add) {
93                 protected String JavaDoc validate() {
94                     String JavaDoc name = dialogPanel.getRoleRefName().trim();
95                     SecurityRoleRef roleRef = null;
96                     
97                     if (row != -1)
98                         roleRef = model.getSecurityRoleRef(row);
99                     
100                     if (name.length()==0) {
101                         return NbBundle.getMessage(SecurityRoleRefTablePanel.class,"TXT_EmptySecurityRoleRefName");
102                     } else {
103                         SecurityRoleRef[] roleRefs = servlet.getSecurityRoleRef();
104                         
105                         for (int i = 0; i < roleRefs.length; i++) {
106                             if (roleRefs[i] != roleRef && name.equals(roleRefs[i].getRoleName())) {
107                                 return NbBundle.getMessage(SecurityRoleRefTablePanel.class,"TXT_SecurityRoleRefNameExists",name);
108                             }
109                         }
110                     }
111                     
112                     if (isEmpty(dialogPanel.getRoleRefLink())) {
113                         return NbBundle.getMessage(SecurityRoleRefTablePanel.class,"TXT_EmptySecurityRoleRefLink");
114                     }
115                     
116                     return null;
117                 }
118             };
119             
120             if (add)
121                 dialog.setValid(false); // disable OK button
122

123             javax.swing.event.DocumentListener JavaDoc docListener = new EditDialog.DocListener(dialog);
124             dialogPanel.getRoleRefNameTF().getDocument().addDocumentListener(docListener);
125             dialogPanel.getDescriptionTA().getDocument().addDocumentListener(docListener);
126             
127             java.awt.Dialog JavaDoc d = org.openide.DialogDisplayer.getDefault().createDialog(dialog);
128             d.setVisible(true);
129             
130             dialogPanel.getRoleRefNameTF().getDocument().removeDocumentListener(docListener);
131             dialogPanel.getDescriptionTA().getDocument().removeDocumentListener(docListener);
132             
133             if (dialog.getValue().equals(EditDialog.OK_OPTION)) {
134                 dObj.modelUpdatedFromUI();
135                 dObj.setChangedFromUI(true);
136                 
137                 String JavaDoc roleRefName = dialogPanel.getRoleRefName();
138                 String JavaDoc roleRefLink = dialogPanel.getRoleRefLink();
139                 String JavaDoc description = dialogPanel.getDescription();
140                 
141                 
142                 if (add)
143                     model.addRow(new String JavaDoc[]{roleRefName, roleRefLink, description});
144                 else
145                     model.editRow(row, new String JavaDoc[]{roleRefName, roleRefLink, description});
146                 
147                 dObj.setChangedFromUI(false);
148             }
149         }
150     }
151     
152     /**
153      * @return true if the given <code>str</code> is null, empty or contains
154      * only spaces.
155      */

156     private boolean isEmpty(String JavaDoc str){
157         return null == str || "".equals(str.trim());
158     }
159     
160     
161     /** This method is called from within the constructor to
162      * initialize the form.
163      * WARNING: Do NOT modify this code. The content of this method is
164      * always regenerated by the Form Editor.
165      */

166     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
167
private void initComponents() {
168
169         setLayout(new java.awt.GridBagLayout JavaDoc());
170
171     }// </editor-fold>//GEN-END:initComponents
172

173     
174     // Variables declaration - do not modify//GEN-BEGIN:variables
175
// End of variables declaration//GEN-END:variables
176

177 }
178
Popular Tags