1 30 package com.genimen.djeneric.tools.modeler.extenteditor; 31 32 import java.awt.Component ; 33 import java.awt.event.MouseAdapter ; 34 import java.awt.event.MouseEvent ; 35 36 import javax.swing.DefaultComboBoxModel ; 37 import javax.swing.JComboBox ; 38 import javax.swing.JTable ; 39 import javax.swing.JTextField ; 40 import javax.swing.event.TableModelEvent ; 41 import javax.swing.table.TableCellEditor ; 42 import javax.swing.table.TableColumn ; 43 import javax.swing.table.TableColumnModel ; 44 45 import com.genimen.djeneric.language.Messages; 46 import com.genimen.djeneric.repository.DjExtent; 47 import com.genimen.djeneric.repository.DjPersistenceManager; 48 import com.genimen.djeneric.repository.DjProperty; 49 import com.genimen.djeneric.repository.DjRelation; 50 import com.genimen.djeneric.tools.modeler.ModelEditor; 51 import com.genimen.djeneric.ui.DjCellEditor; 52 import com.genimen.djeneric.ui.DjModelColumn; 53 import com.genimen.djeneric.ui.DjTable; 54 import com.genimen.djeneric.ui.DjTableModel; 55 import com.genimen.djeneric.util.DjLogger; 56 57 public class RelationTableModel extends DjTableModel 58 { 59 private static final long serialVersionUID = 1L; 60 private final int _colCount = 7; 61 private DjExtent _extent; 62 private DjPersistenceManager _mgr; 63 64 private final static String ONE = "1:1"; 65 private final static String MANY = "1:n"; 66 67 private final int IDX_PROPERTY = 0; 68 private final int IDX_MASTER = 1; 69 private final int IDX_NAME = 2; 70 private final int IDX_CONTAINS = 3; 71 private final int IDX_UNICITY = 4; 72 private final int IDX_RESTRICTION = 5; 73 private final int IDX_DESCRIPTION = 6; 74 75 public RelationTableModel(DjPersistenceManager mgr, DjExtent extent) 76 { 77 _extent = extent; 78 _mgr = mgr; 79 80 _columns = new DjModelColumn[_colCount]; 81 82 _columns[IDX_NAME] = new DjModelColumn(Messages.getString("global.Name"), 120, Messages 83 .getString("RelationTableModel.NameOfRel"), new InheritanceIndicationRenderer(extent, 84 InheritanceIndicationRenderer.RELATION)); 85 _columns[IDX_MASTER] = new DjModelColumn(Messages.getString("global.Master"), 120, Messages 86 .getString("RelationTableModel.Reference2Master")); 87 _columns[IDX_PROPERTY] = new DjModelColumn(Messages.getString("global.Property"), 100, Messages 88 .getString("RelationTableModel.Property2Master")); 89 _columns[IDX_CONTAINS] = new DjModelColumn(Messages.getString("global.Type"), 80, Messages 90 .getString("RelationTableModel.TypeOfRel")); 91 _columns[IDX_UNICITY] = new DjModelColumn(Messages.getString("RelationTableModel.Unicity"), 80, Messages 92 .getString("RelationTableModel.UnicityDescr")); 93 _columns[IDX_RESTRICTION] = new DjModelColumn(Messages.getString("RelationTableModel.Restriction"), 200, Messages 94 .getString("RelationTableModel.RestrictsSelection")); 95 _columns[IDX_DESCRIPTION] = new DjModelColumn(Messages.getString("global.Description"), 300, Messages 96 .getString("RelationTableModel.DescriptionOfRel")); 97 } 98 99 public TableCellEditor getColumnEditor(int column) 100 { 101 try 102 { 103 if (column == IDX_MASTER) 104 { 105 return new DjCellEditor(new JComboBox (_mgr.getExtentsSorted())); 106 } 107 else if (column == IDX_PROPERTY) 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, 114 int column) 115 { 116 JComboBox cbb = (JComboBox ) editorComponent; 117 cbb.setModel(new DefaultComboBoxModel (_extent.getPropertyNames())); 118 delegate.setValue(value); 119 return editorComponent; 120 } 121 }; 122 } 123 else if (column == IDX_CONTAINS) 124 { 125 return new DjCellEditor(new JComboBox (new String []{DjPersistenceManager.RELATION_TYPE_CONTAINS, 126 DjPersistenceManager.RELATION_TYPE_REFERENCES})); 127 } 128 else if (column == IDX_UNICITY) 129 { 130 return new DjCellEditor(new JComboBox (new String []{MANY, ONE})); 131 } 132 else if (column == IDX_RESTRICTION) 133 { 134 JTextField fld = new JTextField (); 135 fld.setFont(new java.awt.Font ("Monospaced", 0, 12)); 136 return new DjCellEditor(fld); 137 } 138 139 return super.getColumnEditor(column); 140 } 141 catch (Exception x) 142 { 143 DjLogger.log(x); 144 return null; 145 } 146 } 147 148 public boolean isCellEditable(int row, int column) 149 { 150 if (!isEditable()) return false; 151 152 DjRelation r = _extent.getMasterRelation(row); 153 if (_extent.isInherited(r)) return false; 154 155 return super.isCellEditable(row, column); 156 } 157 158 public int getRowCount() 159 { 160 if (_extent == null) return 0; 161 162 return _extent.getMasterRelationCount(); 163 } 164 165 public DjExtent getExtent() 166 { 167 return _extent; 168 } 169 170 public int getColumnCount() 171 { 172 return _colCount; 173 } 174 175 public Object getValueAt(int nRow, int nCol) 176 { 177 if (nRow < 0 || nRow >= getRowCount()) return ""; 178 179 DjRelation r = _extent.getMasterRelation(nRow); 180 if (nCol == IDX_NAME) return r.getName(); 181 else if (nCol == IDX_MASTER) return r.getMasterExtent(); 182 else if (nCol == IDX_PROPERTY) 183 { 184 DjProperty c = r.getDetailProperty(); 185 if (c != null) return c.getName(); 186 return null; 187 } 188 else if (nCol == IDX_CONTAINS) return (r.isDetailsContained() 189 ? DjPersistenceManager.RELATION_TYPE_CONTAINS 190 : DjPersistenceManager.RELATION_TYPE_REFERENCES); 191 else if (nCol == IDX_UNICITY) 192 { 193 if (r.getUnicity() == DjRelation.ONE_TO_ONE) return ONE; 194 return MANY; 195 } 196 else if (nCol == IDX_RESTRICTION) return r.getRestrictionExpression(); 197 else if (nCol == IDX_DESCRIPTION) return r.getDescription(); 198 else return Messages.getString("global.InvalidColumn", String.valueOf(nCol)); 199 } 200 201 public void setValueAt(Object aValue, int rowIndex, int columnIndex) 202 { 203 if (rowIndex < 0 || rowIndex >= getRowCount()) return; 204 if (aValue == null) return; 205 206 notifyModified(); 207 try 208 { 209 DjRelation r = _extent.getMasterRelation(rowIndex); 210 if (columnIndex == IDX_NAME) r.setName(aValue.toString()); 211 else if (columnIndex == IDX_MASTER) r.setMasterExtent((DjExtent) aValue); 212 else if (columnIndex == IDX_PROPERTY) r.setDetailProperty((String ) aValue); 213 else if (columnIndex == IDX_CONTAINS) r.setDetailsContained(aValue.toString() 214 .equals(DjPersistenceManager.RELATION_TYPE_CONTAINS)); 215 else if (columnIndex == IDX_UNICITY) 216 { 217 if (ONE.equalsIgnoreCase((String ) aValue)) r.setUnicity(DjRelation.ONE_TO_ONE); 218 else r.setUnicity(DjRelation.ONE_TO_MANY); 219 } 220 else if (columnIndex == IDX_RESTRICTION) 221 { 222 r.setRestrictionExpression((String ) aValue); 223 r.getRestriction(); 225 } 226 else if (columnIndex == IDX_DESCRIPTION) r.setDescription(aValue.toString()); 227 setStatusMessage(ModelEditor.OK_MSG); 228 } 229 catch (Exception x) 230 { 231 setStatusMessage(x); 232 } 233 234 } 235 236 int counter = 1; 237 238 public int insertRow(int atIdx) 239 { 240 if (!isEditable()) return -1; 241 try 242 { 243 244 DjRelation r = _mgr.createRelation(getExtent().getDefaultMasterRelationName(), null, _extent, null, true, ""); 245 _extent.addMasterRelation(r); 246 notifyModified(); 247 setStatusMessage(ModelEditor.OK_MSG); 248 return _extent.getMasterRelationCount() - 1; 249 } 250 catch (Exception x) 251 { 252 setStatusMessage(x.getMessage(), false); 253 } 254 return -1; 255 } 256 257 public boolean deleteRow(int atIdx) 258 { 259 if (!isEditable()) return false; 260 try 261 { 262 DjRelation r = _extent.getMasterRelation(atIdx); 263 if (_extent.isInherited(r)) 264 { 265 setStatusMessage(Messages.getString("RelationTableModel.CanNotDelRelInherited", _extent.getSuper().getName()), 266 false); 267 return false; 268 } 269 270 DjProperty detailProp = _extent.getMasterRelation(atIdx).getDetailProperty(); 271 _extent.removeMasterRelation(_extent.getMasterRelation(atIdx)); 272 _extent.removeProperty(detailProp); 273 notifyModified(); 274 setStatusMessage(ModelEditor.OK_MSG); 275 return true; 276 } 277 catch (Exception x) 278 { 279 setStatusMessage(x); 280 } 281 return false; 282 } 283 284 public MouseAdapter getHeaderClickListener(DjTable table) 285 { 286 return new ColumnListener(table); 287 } 288 289 class ColumnListener extends MouseAdapter 290 { 291 protected DjTable _table; 292 boolean _sortAsc = true; 293 int _sortCol = -1; 294 295 public ColumnListener(DjTable table) 296 { 297 _table = table; 298 } 299 300 public void mouseClicked(MouseEvent e) 301 { 302 TableColumnModel colModel = _table.getColumnModel(); 303 int columnModelIndex = colModel.getColumnIndexAtX(e.getX()); 304 int modelIndex = colModel.getColumn(columnModelIndex).getModelIndex(); 305 306 if (modelIndex < 0) return; 307 if (_sortCol == modelIndex) _sortAsc = !_sortAsc; 308 else _sortCol = modelIndex; 309 310 for (int i = 0; i < getColumnCount(); i++) 311 { 312 TableColumn column = colModel.getColumn(i); 313 column.setHeaderValue(getColumnName(column.getModelIndex())); 314 } 315 _table.getTableHeader().repaint(); 316 317 _table.tableChanged(new TableModelEvent (RelationTableModel.this)); 320 _table.repaint(); 321 } 322 } 323 } | Popular Tags |