1 19 24 25 package org.netbeans.modules.j2ee.sun.share; 26 27 import java.util.Arrays ; 28 import java.util.Vector ; 29 import java.util.ResourceBundle ; 30 import java.text.MessageFormat ; 31 import javax.swing.table.AbstractTableModel ; 32 33 37 public class SecurityMasterListModel extends AbstractTableModel { 38 39 private static final ResourceBundle bundle = ResourceBundle.getBundle( 40 "org.netbeans.modules.j2ee.sun.share.Bundle"); 42 public static final String DUPLICATE_PRINCIPAL = bundle.getString("ERR_PrincipalExists"); public static final String DUPLICATE_GROUP = bundle.getString("ERR_GroupExists"); 45 public static final String [] PRINCIPAL_COLUMN_NAMES = { 46 bundle.getString("LBL_PrincipalColumnName"), 47 bundle.getString("LBL_PrincipalClassColumnName") 48 }; 49 50 public static final String [] GROUP_COLUMN_NAMES = { 51 bundle.getString("LBL_GroupColumnName") 52 }; 53 54 private Vector masterList; 57 58 private final String [] columnNames; 59 private final int columnCount; 60 61 private String duplicateErrorPattern; 62 63 64 private SecurityMasterListModel(String dupErrorPattern) { 65 this(dupErrorPattern, GROUP_COLUMN_NAMES, 1); 66 } 67 68 private SecurityMasterListModel(String dupErrorPattern, String [] colNames, int columns) { 69 assert colNames.length == columns; 71 duplicateErrorPattern = dupErrorPattern; 72 columnNames = colNames; 73 columnCount = columns; 74 masterList = new Vector (); 75 } 76 77 private SecurityMasterListModel(String dupErrorPattern, String [] colNames, Object [] objects, int columns) { 78 assert colNames.length == columns; 80 duplicateErrorPattern = dupErrorPattern; 81 columnNames = colNames; 82 columnCount = columns; 83 masterList = new Vector (Arrays.asList(objects)); 84 } 85 86 88 90 public void addElement(Object obj) { 91 int index = masterList.size(); 92 masterList.add(obj); 93 fireTableRowsInserted(index, index); 94 } 95 96 98 public boolean removeElement(Object 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 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 129 public boolean replaceElement(Object oldObj, Object 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 142 public Object getValueAt(int rowIndex, int columnIndex) { 143 Object result = null; 144 if(rowIndex >= 0 && rowIndex < masterList.size() && columnIndex >= 0 && columnIndex < columnCount) { 145 Object entry = masterList.get(rowIndex); 146 147 if(entry instanceof String ) { 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 getColumnName(int column) { 173 if(column >= 0 && column < columnCount) { 174 return columnNames[column]; 175 } 176 return null; 177 } 178 179 181 public boolean contains(Object obj) { 182 return masterList.contains(obj); 183 } 184 185 public String getDuplicateErrorMessage(String roleName) { 186 Object [] args = { roleName }; 187 return MessageFormat.format(duplicateErrorPattern, args); 188 } 189 190 public Object getRow(int rowIndex) { 191 Object result = null; 192 if(rowIndex >= 0 && rowIndex < masterList.size()) { 193 result = masterList.get(rowIndex); 194 } 195 return result; 196 } 197 198 200 private static SecurityMasterListModel principalMaster = new SecurityMasterListModel(DUPLICATE_PRINCIPAL, PRINCIPAL_COLUMN_NAMES, 2); 201 202 205 public static SecurityMasterListModel getPrincipalMasterModel() { 206 return principalMaster; 207 } 208 209 211 private static SecurityMasterListModel groupMaster = new SecurityMasterListModel(DUPLICATE_GROUP); 212 213 216 public static SecurityMasterListModel getGroupMasterModel() { 217 return groupMaster; 218 } 219 } 220 | Popular Tags |