KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ddloaders > multiview > EjbJarSecurityRolesTableModel


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 package org.netbeans.modules.j2ee.ddloaders.multiview;
21
22 import org.netbeans.modules.j2ee.dd.api.common.SecurityRole;
23 import org.netbeans.modules.j2ee.dd.api.ejb.AssemblyDescriptor;
24 import org.netbeans.modules.j2ee.dd.api.ejb.EjbJar;
25 import org.netbeans.modules.j2ee.ddloaders.web.multiview.EjbRefsTablePanel;
26 import org.netbeans.modules.j2ee.ddloaders.web.multiview.SecurityRolePanel;
27 import org.netbeans.modules.j2ee.ddloaders.web.multiview.SecurityRoleTablePanel;
28 import org.netbeans.modules.xml.multiview.XmlMultiViewDataSynchronizer;
29 import org.netbeans.modules.xml.multiview.ui.EditDialog;
30 import org.openide.util.NbBundle;
31
32 /**
33  * Table model for the security roles table.
34  *
35  * @author ptliu
36  */

37 public class EjbJarSecurityRolesTableModel extends InnerTableModel {
38     private static final String JavaDoc[] COLUMN_NAMES = {Utils.getBundleMessage("LBL_RoleName"),
39     Utils.getBundleMessage("LBL_Description")};
40     
41     private static final int[] COLUMN_WIDTHS = new int[]{170, 250};
42     
43     private AssemblyDescriptor assemblyDesc;
44     
45     private EjbJar ejbJar;
46     
47     public EjbJarSecurityRolesTableModel(XmlMultiViewDataSynchronizer synchronizer,
48             EjbJar ejbJar) {
49         super(synchronizer, COLUMN_NAMES, COLUMN_WIDTHS);
50         
51         this.ejbJar = ejbJar;
52         this.assemblyDesc = ejbJar.getSingleAssemblyDescriptor();
53     }
54     
55     public void setValueAt(Object JavaDoc value, int rowIndex, int columnIndex) {
56         SecurityRole role = assemblyDesc.getSecurityRole(rowIndex);
57         
58         switch (columnIndex) {
59             case 0:
60                 role.setRoleName((String JavaDoc) value);
61                 break;
62             case 1:
63                 role.setDescription((String JavaDoc) value);
64                 break;
65         }
66         
67         modelUpdatedFromUI();
68         fireTableCellUpdated(rowIndex, columnIndex);
69     }
70     
71     public int getRowCount() {
72         if (assemblyDesc == null) return 0;
73         
74         return assemblyDesc.getSecurityRole().length;
75     }
76     
77     public Object JavaDoc getValueAt(int rowIndex, int columnIndex) {
78         SecurityRole role = assemblyDesc.getSecurityRole(rowIndex);
79         
80         switch (columnIndex) {
81             case 0:
82                 return role.getRoleName();
83             case 1:
84                 return role.getDefaultDescription();
85         }
86         
87         return null;
88     }
89     
90     public int addRow() {
91         
92         if (assemblyDesc == null) {
93             assemblyDesc = getAssemblyDesc();
94         }
95         
96         final SecurityRolePanel dialogPanel = new SecurityRolePanel();
97         final String JavaDoc currentRoleName = null;
98         
99         EditDialog dialog = new EditDialog(dialogPanel,NbBundle.getMessage(EjbRefsTablePanel.class,"TTL_SecurityRole"), true) {
100             protected String JavaDoc validate() {
101                 String JavaDoc name = dialogPanel.getRoleName().trim();
102                 
103                 if (name.length()==0) {
104                     return NbBundle.getMessage(SecurityRoleTablePanel.class,"TXT_EmptySecurityRoleName");
105                 } else {
106                     SecurityRole[] roles = assemblyDesc.getSecurityRole();
107                     boolean exists=false;
108                     
109                     for (int i = 0; i < roles.length; i++) {
110                         if (name.equals(roles[i].getRoleName())){
111                             return NbBundle.getMessage(SecurityRoleTablePanel.class,"TXT_SecurityRoleNameExists",name);
112                         }
113                     }
114                 }
115                 
116                 return null;
117             }
118         };
119         dialog.setValid(false);
120         javax.swing.event.DocumentListener JavaDoc docListener = new EditDialog.DocListener(dialog);
121         dialogPanel.getRoleNameTF().getDocument().addDocumentListener(docListener);
122         dialogPanel.getDescriptionTA().getDocument().addDocumentListener(docListener);
123         
124         java.awt.Dialog JavaDoc d = org.openide.DialogDisplayer.getDefault().createDialog(dialog);
125         d.setVisible(true);
126         
127         dialogPanel.getRoleNameTF().getDocument().removeDocumentListener(docListener);
128         dialogPanel.getDescriptionTA().getDocument().removeDocumentListener(docListener);
129         
130         if (dialog.getValue().equals(EditDialog.OK_OPTION)) {
131             SecurityRole role = assemblyDesc.newSecurityRole();
132             role.setRoleName(dialogPanel.getRoleName());
133             role.setDescription(dialogPanel.getDescription());
134             assemblyDesc.addSecurityRole(role);
135             modelUpdatedFromUI();
136         }
137         
138         return getRowCount() - 1;
139     }
140     
141     
142     
143     
144     public void removeRow(final int row) {
145         SecurityRole role = assemblyDesc.getSecurityRole(row);
146         assemblyDesc.removeSecurityRole(role);
147         
148         modelUpdatedFromUI();
149     }
150     
151     private AssemblyDescriptor getAssemblyDesc() {
152         AssemblyDescriptor assemblyDesc = ejbJar.getSingleAssemblyDescriptor();
153         
154         if (assemblyDesc == null) {
155             assemblyDesc = ejbJar.newAssemblyDescriptor();
156             ejbJar.setAssemblyDescriptor(assemblyDesc);
157         }
158         
159         return assemblyDesc;
160     }
161 }
162
Popular Tags