1 30 package com.genimen.djeneric.tools.modeler.domaineditor; 31 32 import java.awt.event.MouseAdapter ; 33 import java.awt.event.MouseEvent ; 34 35 import javax.swing.event.TableModelEvent ; 36 import javax.swing.table.TableColumn ; 37 import javax.swing.table.TableColumnModel ; 38 39 import com.genimen.djeneric.language.Messages; 40 import com.genimen.djeneric.repository.DjDomain; 41 import com.genimen.djeneric.repository.DjDomainValue; 42 import com.genimen.djeneric.repository.DjPersistenceManager; 43 import com.genimen.djeneric.tools.modeler.ModelEditor; 44 import com.genimen.djeneric.ui.DjModelColumn; 45 import com.genimen.djeneric.ui.DjTable; 46 import com.genimen.djeneric.ui.DjTableModel; 47 48 public class DomainValueTableModel extends DjTableModel 49 { 50 private static final long serialVersionUID = 1L; 51 private final int _colCount = 4; 52 private DjPersistenceManager _mgr; 53 private ModelEditor _editor; 54 55 private DjTable _parentTable = null; 56 57 private final int IDX_SEQ = 0; 58 private final int IDX_VALUE = 1; 59 private final int IDX_HIGH_VALUE = 2; 60 private final int IDX_DESCRIPTION = 3; 61 62 public DomainValueTableModel(ModelEditor editor, DjTable parentTable) 63 { 64 _editor = editor; 65 _mgr = editor.getPersistenceManager(); 66 _parentTable = parentTable; 67 68 _columns = new DjModelColumn[_colCount]; 69 70 _columns[IDX_SEQ] = new DjModelColumn(Messages.getString("global.Seq"), 40, Messages 71 .getString("DomainValueTableModel.OrderInList")); 72 _columns[IDX_VALUE] = new DjModelColumn(Messages.getString("DomainValueTableModel.ValueLowValue"), 200, Messages 73 .getString("DomainValueTableModel.ValueLowValueDescr")); 74 _columns[IDX_HIGH_VALUE] = new DjModelColumn(Messages.getString("DomainValueTableModel.HighValue"), 200, Messages 75 .getString("DomainValueTableModel.HighValueDescr")); 76 _columns[IDX_DESCRIPTION] = new DjModelColumn(Messages.getString("global.Description"), 300, Messages 77 .getString("DomainValueTableModel.DescriptiveTitle")); 78 } 79 80 protected DjDomain getDomain() 81 { 82 if (_parentTable == null) return null; 83 84 int idx = _parentTable.getSelectedRow(); 85 if (idx == -1) return null; 86 DomainTableModel mdl = (DomainTableModel) _parentTable.getModel(); 87 88 return mdl.getDomain(idx); 89 } 90 91 public int getRowCount() 92 { 93 if (getDomain() == null || getDomain().isDynamic()) return 0; 94 95 return getDomain().getPersonalDomainValueCount(); 96 } 97 98 public int getColumnCount() 99 { 100 return _colCount; 101 } 102 103 public Object getValueAt(int nRow, int nCol) 104 { 105 if (getDomain() == null) return ""; 106 if (nRow < 0 || nRow >= getRowCount()) return ""; 107 108 DjDomainValue c = getDomain().getPersonalDomainValue(nRow); 109 if (nCol == IDX_SEQ) return new Integer (c.getSeq()); 110 else if (nCol == IDX_VALUE) 111 { 112 Object o = c.getValue(); 113 if (o == null) return ""; 114 return o.toString(); 115 } 116 else if (nCol == IDX_HIGH_VALUE) 117 { 118 Object o = c.getHigh(); 119 if (o == null) return ""; 120 return o.toString(); 121 } 122 else if (nCol == IDX_DESCRIPTION) return c.getDescription(); 123 else return Messages.getString("global.InvalidColumn", String.valueOf(nCol)); 124 } 125 126 public void setValueAt(Object aValue, int rowIndex, int columnIndex) 127 { 128 if (rowIndex < 0 || rowIndex >= getRowCount()) return; 129 DjDomain dom = getDomain(); 130 if (dom == null) return; 131 132 if (aValue == null) return; 133 134 try 135 { 136 DjDomainValue c = getDomain().getPersonalDomainValue(rowIndex); 137 if (columnIndex == IDX_SEQ) 138 { 139 c.setSeq(convert2Int(aValue.toString())); 140 } 141 else if (columnIndex == IDX_VALUE) c.setValue(aValue.toString()); 142 else if (columnIndex == IDX_HIGH_VALUE) c.setHigh(aValue.toString()); 143 else if (columnIndex == IDX_DESCRIPTION) c.setDescription(aValue.toString()); 144 setStatusMessage(ModelEditor.OK_MSG); 145 } 146 catch (Exception x) 147 { 148 setStatusMessage(x); 149 } 150 151 } 152 153 public boolean isCellEditable(int row, int column) 154 { 155 if (!isEditable()) return false; 156 return !getDomain().isBuiltIn(); 157 } 158 159 int counter = 1; 160 161 public int insertRow(int atIdx) 162 { 163 if (!isEditable()) return -1; 164 try 165 { 166 DjDomain dom = getDomain(); 167 if (dom == null) return -1; 168 if (dom.isBuiltIn()) return -1; 169 170 DjDomainValue c = new DjDomainValue(dom, "", "", ""); 171 c.setSeq((dom.getPersonalDomainValueCount() + 1) * 10); 172 173 dom.addDomainValue(atIdx, c); 174 setStatusMessage(ModelEditor.OK_MSG); 175 return atIdx; 176 } 177 catch (Exception x) 178 { 179 setStatusMessage(x.getMessage(), false); 180 } 181 return -1; 182 } 183 184 public boolean deleteRow(int atIdx) 185 { 186 if (!isEditable()) return false; 187 try 188 { 189 DjDomain dom = getDomain(); 190 if (dom == null) return false; 191 if (dom.isBuiltIn()) return false; 192 193 dom.removeDomainValue(getDomain().getPersonalDomainValue(atIdx)); 194 setStatusMessage(ModelEditor.OK_MSG); 195 return true; 196 } 197 catch (Exception x) 198 { 199 setStatusMessage(x.getMessage(), false); 200 } 201 return false; 202 } 203 204 public MouseAdapter getHeaderClickListener(DjTable table) 205 { 206 return new ColumnListener(table); 207 } 208 209 class ColumnListener extends MouseAdapter 210 { 211 protected DjTable _table; 212 boolean _sortAsc = true; 213 int _sortCol = -1; 214 215 public ColumnListener(DjTable table) 216 { 217 _table = table; 218 } 219 220 public void mouseClicked(MouseEvent e) 221 { 222 TableColumnModel colModel = _table.getColumnModel(); 223 int columnModelIndex = colModel.getColumnIndexAtX(e.getX()); 224 int modelIndex = colModel.getColumn(columnModelIndex).getModelIndex(); 225 226 if (modelIndex < 0) return; 227 if (_sortCol == modelIndex) _sortAsc = !_sortAsc; 228 else _sortCol = modelIndex; 229 230 for (int i = 0; i < getColumnCount(); i++) 231 { 232 TableColumn column = colModel.getColumn(i); 233 column.setHeaderValue(getColumnName(column.getModelIndex())); 234 } 235 _table.getTableHeader().repaint(); 236 237 _table.tableChanged(new TableModelEvent (DomainValueTableModel.this)); 239 _table.repaint(); 240 } 241 } 242 } | Popular Tags |