KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > share > SecurityMasterListModel


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  * SecurityMasterListModel.java
21  *
22  * Created on January 22, 2004, 12:39 PM
23  */

24
25 package org.netbeans.modules.j2ee.sun.share;
26
27 import java.util.Arrays JavaDoc;
28 import java.util.Vector JavaDoc;
29 import java.util.ResourceBundle JavaDoc;
30 import java.text.MessageFormat JavaDoc;
31 import javax.swing.table.AbstractTableModel JavaDoc;
32
33 /**
34  *
35  * @author Peter Williams
36  */

37 public class SecurityMasterListModel extends AbstractTableModel JavaDoc {
38
39     private static final ResourceBundle JavaDoc bundle = ResourceBundle.getBundle(
40         "org.netbeans.modules.j2ee.sun.share.Bundle"); // NOI18N
41

42     public static final String JavaDoc DUPLICATE_PRINCIPAL = bundle.getString("ERR_PrincipalExists"); // NOI18N
43
public static final String JavaDoc DUPLICATE_GROUP = bundle.getString("ERR_GroupExists"); // NOI18N
44

45     public static final String JavaDoc [] PRINCIPAL_COLUMN_NAMES = {
46         bundle.getString("LBL_PrincipalColumnName"),
47         bundle.getString("LBL_PrincipalClassColumnName")
48     };
49
50     public static final String JavaDoc [] GROUP_COLUMN_NAMES = {
51         bundle.getString("LBL_GroupColumnName")
52     };
53
54     // !PW FIXME will likely have to replace this with LinkedHashMap to have
55
// decent performance adding and editing entries in large lists (> 25)
56
private Vector JavaDoc masterList;
57
58     private final String JavaDoc [] columnNames;
59     private final int columnCount;
60
61     private String JavaDoc duplicateErrorPattern;
62
63     /** Creates a new instance of SecurityMasterListModel */
64     private SecurityMasterListModel(String JavaDoc dupErrorPattern) {
65         this(dupErrorPattern, GROUP_COLUMN_NAMES, 1);
66     }
67
68     private SecurityMasterListModel(String JavaDoc dupErrorPattern, String JavaDoc [] colNames, int columns) {
69         assert colNames.length == columns; // # column names == # columns
70

71         duplicateErrorPattern = dupErrorPattern;
72         columnNames = colNames;
73         columnCount = columns;
74         masterList = new Vector JavaDoc();
75     }
76
77     private SecurityMasterListModel(String JavaDoc dupErrorPattern, String JavaDoc [] colNames, Object JavaDoc [] objects, int columns) {
78         assert colNames.length == columns; // # column names == # columns
79

80         duplicateErrorPattern = dupErrorPattern;
81         columnNames = colNames;
82         columnCount = columns;
83         masterList = new Vector JavaDoc(Arrays.asList(objects));
84     }
85
86     /** Manipulation methods
87      */

88     /** add element
89      */

90     public void addElement(Object JavaDoc obj) {
91         int index = masterList.size();
92         masterList.add(obj);
93         fireTableRowsInserted(index, index);
94     }
95
96     /** remove element
97      */

98     public boolean removeElement(Object JavaDoc obj) {
99         int index = masterList.indexOf(obj);
100         boolean result = masterList.removeElement(obj);
101         if(index >= 0) {
102             fireTableRowsDeleted(index, index);
103         }
104         return result;
105     }
106
107     public void removeElementAt(int index) {
108         if(index >= 0 || index < masterList.size()) {
109             masterList.removeElementAt(index);
110             fireTableRowsDeleted(index, index);
111         }
112     }
113
114     public void removeElements(int[] indices) {
115         // !PW FIXME this method has an unwritten requirement that the
116
// list of indices passed in is ordered in ascending numerical order.
117
if(indices.length > 0) {
118             for(int i = indices.length-1; i >= 0; i--) {
119                 if(indices[i] >= 0 || indices[i] < masterList.size()) {
120                     masterList.removeElementAt(indices[i]);
121                 }
122             }
123             fireTableRowsUpdated(indices[0], indices[indices.length-1]);
124         }
125     }
126
127     /** replace element
128      */

129     public boolean replaceElement(Object JavaDoc oldObj, Object JavaDoc newObj) {
130         boolean result = false;
131         int index = masterList.indexOf(oldObj);
132         if(index != -1) {
133             masterList.setElementAt(newObj, index);
134             fireTableRowsUpdated(index, index);
135         }
136         return result;
137     }
138
139     /**
140      * Implementation of missing pieces of TableModel interface
141      */

142     public Object JavaDoc getValueAt(int rowIndex, int columnIndex) {
143         Object JavaDoc result = null;
144         if(rowIndex >= 0 && rowIndex < masterList.size() && columnIndex >= 0 && columnIndex < columnCount) {
145             Object JavaDoc entry = masterList.get(rowIndex);
146
147             if(entry instanceof String JavaDoc) {
148                 assert columnCount == 1 : "Invalid object for getValueAt() in SecurityMasterListModel.";
149                 result = entry;
150             } else if(entry instanceof PrincipalNameMapping) {
151                 PrincipalNameMapping principalEntry = (PrincipalNameMapping) masterList.get(rowIndex);
152                 if(columnIndex == 0) {
153                     result = principalEntry.getPrincipalName();
154                 } else {
155                     result = principalEntry.getClassName();
156                 }
157             } else {
158                 assert false : "Invalid object for getValueAt() in SecurityMasterListModel.";
159             }
160         }
161         return result;
162     }
163
164     public int getRowCount() {
165         return masterList.size();
166     }
167
168     public int getColumnCount() {
169         return columnCount;
170     }
171
172     public String JavaDoc getColumnName(int column) {
173         if(column >= 0 && column < columnCount) {
174             return columnNames[column];
175         }
176         return null;
177     }
178
179     /** Other public access methods
180      */

181     public boolean contains(Object JavaDoc obj) {
182         return masterList.contains(obj);
183     }
184
185     public String JavaDoc getDuplicateErrorMessage(String JavaDoc roleName) {
186         Object JavaDoc [] args = { roleName };
187         return MessageFormat.format(duplicateErrorPattern, args);
188     }
189
190     public Object JavaDoc getRow(int rowIndex) {
191         Object JavaDoc result = null;
192         if(rowIndex >= 0 && rowIndex < masterList.size()) {
193             result = masterList.get(rowIndex);
194         }
195         return result;
196     }
197
198     /** Principal Name List
199      */

200     private static SecurityMasterListModel principalMaster = new SecurityMasterListModel(DUPLICATE_PRINCIPAL, PRINCIPAL_COLUMN_NAMES, 2);
201
202     /** Retrieves the principal role name ListModel
203      * @return The ListModel representing the global principal role list.
204      */

205     public static SecurityMasterListModel getPrincipalMasterModel() {
206         return principalMaster;
207     }
208
209     /** Group Name List
210      */

211     private static SecurityMasterListModel groupMaster = new SecurityMasterListModel(DUPLICATE_GROUP);
212
213     /** Retrieves the group role name ListModel
214      * @return The ListModel representing the global group role list.
215      */

216     public static SecurityMasterListModel getGroupMasterModel() {
217         return groupMaster;
218     }
219 }
220
Popular Tags