1 30 package com.genimen.djeneric.tools.administrator.editors; 31 32 import java.awt.Component ; 33 import java.util.ArrayList ; 34 import java.util.Vector ; 35 36 import javax.swing.DefaultComboBoxModel ; 37 import javax.swing.JCheckBox ; 38 import javax.swing.JComboBox ; 39 import javax.swing.JTable ; 40 import javax.swing.table.TableCellEditor ; 41 42 import com.genimen.djeneric.language.Messages; 43 import com.genimen.djeneric.repository.DjContext; 44 import com.genimen.djeneric.repository.DjContextManager; 45 import com.genimen.djeneric.repository.DjUser; 46 import com.genimen.djeneric.repository.DjUserContextAssociation; 47 import com.genimen.djeneric.tools.modeler.ModelEditor; 48 import com.genimen.djeneric.ui.DjCellEditor; 49 import com.genimen.djeneric.ui.DjCheckCellRenderer; 50 import com.genimen.djeneric.ui.DjModelColumn; 51 import com.genimen.djeneric.ui.DjTableModel; 52 import com.genimen.djeneric.util.DjLogger; 53 54 public class AccessTableModel extends DjTableModel 55 { 56 private static final long serialVersionUID = 1L; 57 private final static int _colCount = 5; 58 59 private ArrayList _allAssocs; 60 private DjUser _user = null; 61 private DjContext _context = null; 62 private DjContextManager _cmgr; 63 private final static int IDX_OTHEREND = 0; 64 private final static int IDX_QUERY = 1; 65 private final static int IDX_CREATE = 2; 66 private final static int IDX_MODIFY = 3; 67 private final static int IDX_DELETE = 4; 68 69 public AccessTableModel(DjContextManager cmgr, ArrayList allAssocs, Object parentToAssocs) 70 { 71 _allAssocs = allAssocs; 72 _cmgr = cmgr; 73 if (parentToAssocs instanceof DjUser) 74 { 75 _user = (DjUser) parentToAssocs; 76 } 77 else 78 { 79 _context = (DjContext) parentToAssocs; 80 } 81 82 _columns = new DjModelColumn[_colCount]; 83 84 if (_user != null) 85 { 86 _columns[IDX_OTHEREND] = new DjModelColumn(Messages.getString("AccessTableModel.Context"), 200, Messages 87 .getString("AccessTableModel.ContextGranted")); 88 } 89 else 90 { 91 _columns[IDX_OTHEREND] = new DjModelColumn(Messages.getString("AccessTableModel.User"), 200, Messages 92 .getString("AccessTableModel.UserGranted")); 93 } 94 95 _columns[IDX_QUERY] = new DjModelColumn(Messages.getString("global.Query"), 60, Messages 96 .getString("AccessTableModel.CanQueryObjects"), new DjCheckCellRenderer()); 97 _columns[IDX_CREATE] = new DjModelColumn(Messages.getString("global.Create"), 60, Messages 98 .getString("AccessTableModel.CanCreateObjects"), new DjCheckCellRenderer()); 99 _columns[IDX_MODIFY] = new DjModelColumn(Messages.getString("AccessTableModel.Modify"), 60, Messages 100 .getString("AccessTableModel.CanModifyObjects"), new DjCheckCellRenderer()); 101 _columns[IDX_DELETE] = new DjModelColumn(Messages.getString("global.Delete"), 60, Messages 102 .getString("AccessTableModel.CanDeleteObjects"), new DjCheckCellRenderer()); 103 } 104 105 public TableCellEditor getColumnEditor(int column) 106 { 107 if (column == IDX_OTHEREND) 108 { 109 return new DjCellEditor(new JComboBox ()) 110 { 111 private static final long serialVersionUID = 1L; 112 113 public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) 114 { 115 JComboBox cbb = (JComboBox ) editorComponent; 116 try 117 { 118 if (_user != null) 119 { 120 DjContext[] ctxt = _cmgr.getContexts(); 121 Vector v = new Vector (); 122 DjUserContextAssociation c = (DjUserContextAssociation) _allAssocs.get(row); 123 if (c.getContext() != null) v.add(c.getContext()); 124 125 for (int i = 0; i < ctxt.length; i++) 126 { 127 boolean canAdd = true; 128 for (int j = 0; j < _allAssocs.size(); j++) 129 { 130 c = (DjUserContextAssociation) _allAssocs.get(j); 131 if (c.getContext() != null && c.getContext().equals(ctxt[i])) canAdd = false; 132 } 133 if (canAdd) v.add(ctxt[i]); 134 } 135 cbb.setModel(new DefaultComboBoxModel (v)); 136 } 137 else 138 { 139 DjUser[] devs = _cmgr.getUsers(); 140 Vector v = new Vector (); 141 DjUserContextAssociation c = (DjUserContextAssociation) _allAssocs.get(row); 142 if (c.getUser() != null) v.add(c.getUser()); 143 for (int i = 0; i < devs.length; i++) 144 { 145 boolean canAdd = true; 146 for (int j = 0; j < _allAssocs.size(); j++) 147 { 148 c = (DjUserContextAssociation) _allAssocs.get(j); 149 if (c.getUser() != null && c.getUser().equals(devs[i])) canAdd = false; 150 } 151 if (canAdd) v.add(devs[i]); 152 } 153 cbb.setModel(new DefaultComboBoxModel (v)); 154 } 155 } 156 catch (Exception x) 157 { 158 DjLogger.log(x); 159 } 160 delegate.setValue(value); 161 return editorComponent; 162 } 163 }; 164 } 165 if (column == IDX_QUERY) 166 { 167 return new DjCellEditor(new JCheckBox ()); 168 } 169 if (column == IDX_CREATE) 170 { 171 return new DjCellEditor(new JCheckBox ()); 172 } 173 if (column == IDX_MODIFY) 174 { 175 return new DjCellEditor(new JCheckBox ()); 176 } 177 if (column == IDX_DELETE) 178 { 179 return new DjCellEditor(new JCheckBox ()); 180 } 181 182 return super.getColumnEditor(column); 183 } 184 185 public int getRowCount() 186 { 187 if (_allAssocs == null) return 0; 188 189 return _allAssocs.size(); 190 } 191 192 public int getColumnCount() 193 { 194 return _colCount; 195 } 196 197 public Object getValueAt(int nRow, int nCol) 198 { 199 if (nRow < 0 || nRow >= getRowCount()) return ""; 200 201 DjUserContextAssociation c = (DjUserContextAssociation) _allAssocs.get(nRow); 202 if (nCol == IDX_OTHEREND) 203 { 204 if (_user != null) return c.getContext(); 205 return c.getUser(); 206 } 207 else if (nCol == IDX_QUERY) return new Boolean (c.isQuery()); 208 else if (nCol == IDX_CREATE) return new Boolean (c.isCreate()); 209 else if (nCol == IDX_MODIFY) return new Boolean (c.isModify()); 210 else if (nCol == IDX_DELETE) return new Boolean (c.isDelete()); 211 else return Messages.getString("global.InvalidColumn", String.valueOf(nCol)); 212 } 213 214 public void setValueAt(Object aValue, int rowIndex, int columnIndex) 215 { 216 if (rowIndex < 0 || rowIndex >= getRowCount()) return; 217 if (aValue == null) return; 218 notifyModified(); 219 220 try 221 { 222 DjUserContextAssociation c = (DjUserContextAssociation) _allAssocs.get(rowIndex); 223 if (columnIndex == IDX_OTHEREND) 224 { 225 if (aValue instanceof DjUser) c.setUser((DjUser) aValue); 226 else c.setContext((DjContext) aValue); 227 } 228 else if (columnIndex == IDX_QUERY) c.setQuery(((Boolean ) aValue).booleanValue()); 229 else if (columnIndex == IDX_CREATE) c.setCreate(((Boolean ) aValue).booleanValue()); 230 else if (columnIndex == IDX_MODIFY) c.setModify(((Boolean ) aValue).booleanValue()); 231 else if (columnIndex == IDX_DELETE) c.setDelete(((Boolean ) aValue).booleanValue()); 232 setStatusMessage(ModelEditor.OK_MSG); 233 } 234 catch (Exception x) 235 { 236 setStatusMessage(x); 237 } 238 239 } 240 241 int counter = 1; 242 243 public int insertRow(int atIdx) 244 { 245 try 246 { 247 DjUserContextAssociation c = _cmgr.createNewUserContextAssociation(); 248 c.setUser(_user); 249 c.setContext(_context); 250 _allAssocs.add(atIdx, c); 251 notifyModified(); 252 setStatusMessage(ModelEditor.OK_MSG); 253 return atIdx; 254 } 255 catch (Exception x) 256 { 257 setStatusMessage(x.getMessage(), false); 258 } 259 return -1; 260 } 261 262 public boolean deleteRow(int atIdx) 263 { 264 try 265 { 266 _allAssocs.remove(atIdx); 267 notifyModified(); 268 setStatusMessage(ModelEditor.OK_MSG); 269 return true; 270 } 271 catch (Exception x) 272 { 273 setStatusMessage(x.getMessage(), false); 274 } 275 return false; 276 } 277 278 } | Popular Tags |