KickJava   Java API By Example, From Geeks To Geeks.

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


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.CommonDDBean;
24 import org.netbeans.modules.j2ee.dd.api.common.EjbLocalRef;
25 import org.netbeans.modules.j2ee.dd.api.common.EjbRef;
26 import org.netbeans.modules.j2ee.dd.api.common.SecurityRole;
27 import org.netbeans.modules.j2ee.dd.api.web.WebApp;
28 import org.netbeans.modules.j2ee.ddloaders.web.DDDataObject;
29 import org.netbeans.modules.xml.multiview.ui.DefaultTablePanel;
30 import org.netbeans.modules.xml.multiview.ui.EditDialog;
31 import org.openide.util.NbBundle;
32
33 /**
34  * SecurityRoleTablePanel.java
35  *
36  * Panel for displaying the security role table.
37  *
38  * @author ptliu
39  */

40 public class SecurityRoleTablePanel extends DefaultTablePanel {
41     private SecurityRoleTableModel model;
42     private WebApp webApp;
43     private DDDataObject dObj;
44     
45     /** Creates new form ContextParamPanel */
46     public SecurityRoleTablePanel(final DDDataObject dObj, final SecurityRoleTableModel model) {
47         super(model);
48         this.model=model;
49         this.dObj=dObj;
50         
51         removeButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
52             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
53                 dObj.modelUpdatedFromUI();
54                 dObj.setChangedFromUI(true);
55                 int row = getTable().getSelectedRow();
56                 model.removeRow(row);
57                 dObj.setChangedFromUI(false);
58             }
59         });
60         editButton.addActionListener(new TableActionListener(false));
61         addButton.addActionListener(new TableActionListener(true));
62     }
63
64     void setModel(WebApp webApp, SecurityRole[] roles) {
65         model.setData(webApp, roles);
66         this.webApp=webApp;
67     }
68     
69     private class TableActionListener implements java.awt.event.ActionListener JavaDoc {
70         private boolean add;
71         
72         TableActionListener(boolean add) {
73             this.add=add;
74         }
75         
76         public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
77             final int row = (add?-1:getTable().getSelectedRow());
78             final SecurityRolePanel dialogPanel = new SecurityRolePanel();
79             final String JavaDoc currentRoleName = null;
80             SecurityRole role = null;
81             
82             if (!add) {
83                 role = model.getSecurityRole(row);
84                 dialogPanel.setRoleName(role.getRoleName());
85                 dialogPanel.setDescription(role.getDefaultDescription());
86             }
87             
88             final SecurityRole currentRole = role;
89             
90             EditDialog dialog = new EditDialog(dialogPanel,NbBundle.getMessage(EjbRefsTablePanel.class,"TTL_SecurityRole"),add) {
91                 protected String JavaDoc validate() {
92                     String JavaDoc name = dialogPanel.getRoleName().trim();
93             
94                     if (name.length()==0) {
95                         return NbBundle.getMessage(SecurityRoleTablePanel.class,"TXT_EmptySecurityRoleName");
96                     } else {
97                         SecurityRole[] roles = webApp.getSecurityRole();
98                         boolean exists=false;
99                         
100                         for (int i = 0; i < roles.length; i++) {
101                             if (name.equals(roles[i].getRoleName()) &&
102                                     roles[i] != currentRole) {
103                                 return NbBundle.getMessage(SecurityRoleTablePanel.class,"TXT_SecurityRoleNameExists",name);
104                             }
105                         }
106                     }
107                     
108                     return null;
109                 }
110             };
111        
112             if (add)
113                 dialog.setValid(false); // disable OK button
114

115             javax.swing.event.DocumentListener JavaDoc docListener = new EditDialog.DocListener(dialog);
116             dialogPanel.getRoleNameTF().getDocument().addDocumentListener(docListener);
117             dialogPanel.getDescriptionTA().getDocument().addDocumentListener(docListener);
118             
119             java.awt.Dialog JavaDoc d = org.openide.DialogDisplayer.getDefault().createDialog(dialog);
120             d.setVisible(true);
121             
122             dialogPanel.getRoleNameTF().getDocument().removeDocumentListener(docListener);
123             dialogPanel.getDescriptionTA().getDocument().removeDocumentListener(docListener);
124         
125             if (dialog.getValue().equals(EditDialog.OK_OPTION)) {
126                 dObj.modelUpdatedFromUI();
127                 dObj.setChangedFromUI(true);
128                 
129                 String JavaDoc roleName = dialogPanel.getRoleName();
130                 String JavaDoc description = dialogPanel.getDescription();
131          
132                 if (add)
133                     model.addRow(new String JavaDoc[]{roleName, description});
134                 else
135                     model.editRow(row, new String JavaDoc[]{roleName, description});
136                 
137                 dObj.setChangedFromUI(false);
138             }
139         }
140     }
141  
142 }
143
Popular Tags