1 30 package com.genimen.djeneric.tools.modeler.domaineditor; 31 32 import java.awt.Component ; 33 import java.awt.event.MouseAdapter ; 34 import java.awt.event.MouseEvent ; 35 import java.util.ArrayList ; 36 import java.util.Arrays ; 37 import java.util.Collections ; 38 import java.util.Comparator ; 39 40 import javax.swing.DefaultComboBoxModel ; 41 import javax.swing.JCheckBox ; 42 import javax.swing.JComboBox ; 43 import javax.swing.JTable ; 44 import javax.swing.event.TableModelEvent ; 45 import javax.swing.table.TableCellEditor ; 46 import javax.swing.table.TableColumn ; 47 import javax.swing.table.TableColumnModel ; 48 49 import com.genimen.djeneric.language.Messages; 50 import com.genimen.djeneric.repository.DjDomain; 51 import com.genimen.djeneric.repository.DjPackage; 52 import com.genimen.djeneric.repository.DjPersistenceManager; 53 import com.genimen.djeneric.tools.modeler.ModelEditor; 54 import com.genimen.djeneric.ui.DjCellEditor; 55 import com.genimen.djeneric.ui.DjCheckCellRenderer; 56 import com.genimen.djeneric.ui.DjModelColumn; 57 import com.genimen.djeneric.ui.DjTable; 58 import com.genimen.djeneric.ui.DjTableModel; 59 import com.genimen.djeneric.util.DjString; 60 61 public class DomainTableModel extends DjTableModel 62 { 63 private static final long serialVersionUID = 1L; 64 private DjPersistenceManager _mgr; 65 66 private final int IDX_NAME = 0; 67 private final int IDX_TYPE = 1; 68 private final int IDX_LENGTH = 2; 69 private final int IDX_CASE_CONVERSION = 3; 70 private final int IDX_DECIMALS = 4; 71 private final int IDX_SUPER = 5; 72 private final int IDX_ENFORCE = 6; 73 private final int IDX_FORMAT_MASK = 7; 74 private final int IDX_RE_MASK = 8; 75 private final int IDX_RE_FAIL_MSG = 9; 76 private final int IDX_COMPONENT = 10; 77 private final int IDX_PACKAGE = 11; 78 private final int IDX_WIDTH = 12; 79 private final int IDX_HEIGHT = 13; 80 private final int IDX_DESCRIPTION = 14; 81 private final int _colCount = 15; 82 83 DjDomain _noneDomain = new DjDomain(); 84 85 DjPackage _defaultPackage = new DjPackage(Messages 86 .getString("ExtentSelectorDialog.DefaultPackage")); 87 DjPackage _allPackages = new DjPackage(Messages 88 .getString("ExtentSelectorDialog.AllPackages")); 89 90 ArrayList _data = new ArrayList (); 91 DjPackage _filterPackage = _allPackages; 92 93 public DomainTableModel(DjPersistenceManager mgr) 94 { 95 _mgr = mgr; 96 97 _columns = new DjModelColumn[_colCount]; 98 99 _columns[IDX_NAME] = new DjModelColumn(Messages.getString("global.Name"), 120, Messages 100 .getString("DomainTableModel.NameOfTheDomain")); 101 _columns[IDX_TYPE] = new DjModelColumn(Messages.getString("global.Type"), 80, Messages 102 .getString("DomainTableModel.UnderlyingType")); 103 _columns[IDX_LENGTH] = new DjModelColumn(Messages.getString("DomainTableModel.Len"), 50, Messages 104 .getString("DomainTableModel.MaximumLength")); 105 _columns[IDX_CASE_CONVERSION] = new DjModelColumn(Messages.getString("DomainTableModel.CaseConv"), 90, Messages 106 .getString("DomainTableModel.CaseConvDescr")); 107 _columns[IDX_DECIMALS] = new DjModelColumn(Messages.getString("DomainTableModel.Dec"), 30, Messages 108 .getString("DomainTableModel.Decimals")); 109 _columns[IDX_SUPER] = new DjModelColumn(Messages.getString("global.Super"), 120, Messages 110 .getString("DomainTableModel.SuperToInheritFrom")); 111 _columns[IDX_ENFORCE] = new DjModelColumn(Messages.getString("DomainTableModel.Enforce"), 50, Messages 112 .getString("DomainTableModel.EnforceValueInList"), new DjCheckCellRenderer()); 113 _columns[IDX_FORMAT_MASK] = new DjModelColumn(Messages.getString("DomainTableModel.FormatMask"), 100, Messages 114 .getString("DomainTableModel.JavaFormatMask")); 115 _columns[IDX_RE_MASK] = new DjModelColumn(Messages.getString("DomainTableModel.ValidationMask"), 100, Messages 116 .getString("DomainTableModel.RegExp")); 117 _columns[IDX_RE_FAIL_MSG] = new DjModelColumn(Messages.getString("DomainTableModel.MaskFailureMsg"), 100, Messages 118 .getString("DomainTableModel.ValidationMaskMessage")); 119 _columns[IDX_COMPONENT] = new DjModelColumn(Messages.getString("global.Component"), 90, Messages 120 .getString("DomainTableModel.HowToDsp")); 121 _columns[IDX_PACKAGE] = new DjModelColumn(Messages.getString("global.Package"), 90, Messages 122 .getString("DomainTableModel.HowToPackage")); 123 _columns[IDX_WIDTH] = new DjModelColumn(Messages.getString("global.Width"), 40, Messages 124 .getString("DomainTableModel.DisplayWidthPixels")); 125 _columns[IDX_HEIGHT] = new DjModelColumn(Messages.getString("global.Height"), 40, Messages 126 .getString("DomainTableModel.DisplayHeightPixels")); 127 _columns[IDX_DESCRIPTION] = new DjModelColumn(Messages.getString("global.Description"), 200, Messages 128 .getString("DomainTableModel.GeneralDescr")); 129 } 130 131 public TableCellEditor getColumnEditor(int column) 132 { 133 if (column == IDX_TYPE) 134 { 135 JComboBox cbb = new JComboBox (new String []{"String", "int", "long", "BigDecimal", "Date", "byte[]"}); 136 return new DjCellEditor(cbb); 137 } 138 if (column == IDX_CASE_CONVERSION) 139 { 140 JComboBox cbb = new JComboBox (new String []{Messages.getString("DomainTableModel.CaseNone"), 141 Messages.getString("DomainTableModel.CaseUpper"), Messages.getString("DomainTableModel.CaseLower")}); 142 return new DjCellEditor(cbb); 143 } 144 else if (column == IDX_COMPONENT) 145 { 146 return new DjCellEditor(new JComboBox (DjDomain.getComponentTypeNames())); 147 } 148 else if (column == IDX_PACKAGE) 149 { 150 return new DjCellEditor(new JComboBox ()) 151 { 152 private static final long serialVersionUID = 1L; 153 154 public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) 155 { 156 ArrayList all = new ArrayList (); 157 all.add(_defaultPackage); 158 all.addAll(Arrays.asList(_mgr.getPackages())); 159 160 JComboBox cbb = (JComboBox ) editorComponent; 161 cbb.setEditable(false); 162 cbb.setModel(new DefaultComboBoxModel (all.toArray())); 163 delegate.setValue(value); 164 return editorComponent; 165 } 166 }; 167 } 168 else if (column == IDX_SUPER) 169 { 170 return new DjCellEditor(new JComboBox ()) 171 { 172 private static final long serialVersionUID = 1L; 173 174 public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) 175 { 176 JComboBox cbb = (JComboBox ) editorComponent; 177 DjDomain[] curr = _mgr.getDomains(); 178 DjDomain[] all = new DjDomain[curr.length]; 179 all[0] = _noneDomain; 180 int idx = 1; 181 for (int i = 0; i < curr.length; i++) 182 { 183 if (i != row) all[idx++] = curr[i]; 184 } 185 186 cbb.setModel(new DefaultComboBoxModel (all)); 187 delegate.setValue(value); 188 return editorComponent; 189 } 190 }; 191 } 192 else if (column == IDX_ENFORCE) 193 { 194 return new DjCellEditor(new JCheckBox ()); 195 } 196 197 return super.getColumnEditor(column); 198 } 199 200 public int getRowCount() 201 { 202 if (_mgr == null) return 0; 203 204 return _data.size(); 205 } 206 207 public int getColumnCount() 208 { 209 return _colCount; 210 } 211 212 public Object getValueAt(int nRow, int nCol) 213 { 214 if (nRow < 0 || nRow >= getRowCount()) return ""; 215 216 DjDomain c = (DjDomain) _data.get(nRow); 217 218 if (nCol == IDX_NAME) return c.getName(); 219 else if (nCol == IDX_CASE_CONVERSION) 220 { 221 String cnv = c.getCaseConversion(); 222 if (cnv.equals(DjDomain.CASE_UPPER)) return Messages.getString("DomainTableModel.CaseUpper"); 223 if (cnv.equals(DjDomain.CASE_LOWER)) return Messages.getString("DomainTableModel.CaseLower"); 224 return Messages.getString("DomainTableModel.CaseNone"); 225 } 226 else if (nCol == IDX_SUPER) 227 { 228 DjDomain dom = c.getSuperDomain(); 229 if (dom == null) return _noneDomain; 230 return dom; 231 } 232 else if (nCol == IDX_TYPE) return c.getNativeType(); 233 else if (nCol == IDX_LENGTH) 234 { 235 if (c.getLength() <= 0) return ""; 236 return "" + c.getLength(); 237 } 238 else if (nCol == IDX_WIDTH) 239 { 240 if (c.getDisplayWidth() <= 0) return ""; 241 return "" + c.getDisplayWidth(); 242 } 243 else if (nCol == IDX_HEIGHT) 244 { 245 if (c.getDisplayHeight() <= 0) return ""; 246 return "" + c.getDisplayHeight(); 247 } 248 else if (nCol == IDX_DECIMALS) 249 { 250 if (c.getDecimals() <= 0) return ""; 251 return "" + c.getDecimals(); 252 } 253 else if (nCol == IDX_ENFORCE) return new Boolean (c.isEnforced()); 254 else if (nCol == IDX_FORMAT_MASK) return c.getFormatMask(); 255 else if (nCol == IDX_RE_MASK) return c.getReValidationMask(); 256 else if (nCol == IDX_RE_FAIL_MSG) return c.getReFailureMessage(); 257 else if (nCol == IDX_COMPONENT) return c.getComponentTypeName(); 258 else if (nCol == IDX_PACKAGE) 259 { 260 DjPackage p = c.getPackage(); 261 if (p == null) return _defaultPackage; 262 else return p; 263 } 264 else if (nCol == IDX_DESCRIPTION) return c.getDescription(); 265 else return Messages.getString("global.InvalidColumn", String.valueOf(nCol)); 266 } 267 268 public void setValueAt(Object aValue, int rowIndex, int columnIndex) 269 { 270 if (rowIndex < 0 || rowIndex >= getRowCount()) return; 271 if (aValue == null) return; 272 notifyModified(); 273 274 try 275 { 276 DjDomain c = (DjDomain) _data.get(rowIndex); 277 if (columnIndex == IDX_NAME) 278 { 279 c.setName(aValue.toString()); 280 _mgr.updateDomainHashes(); 281 } 282 else if (columnIndex == IDX_CASE_CONVERSION) 283 { 284 String cnv = aValue.toString(); 285 if (cnv.equals(Messages.getString("DomainTableModel.CaseUpper"))) c.setCaseConversion(DjDomain.CASE_UPPER); 286 else if (cnv.equals(Messages.getString("DomainTableModel.CaseLower"))) c.setCaseConversion(DjDomain.CASE_LOWER); 287 else c.setCaseConversion(DjDomain.CASE_NONE); 288 } 289 else if (columnIndex == IDX_SUPER) 290 { 291 if (aValue == _noneDomain) c.setSuperDomain(null); 292 else c.setSuperDomain((DjDomain) aValue); 293 } 294 else if (columnIndex == IDX_TYPE) c.setNativeType(aValue.toString()); 295 else if (columnIndex == IDX_LENGTH) c.setLength(convert2Int(aValue.toString())); 296 else if (columnIndex == IDX_WIDTH) c.setDisplayWidth(convert2Int(aValue.toString())); 297 else if (columnIndex == IDX_HEIGHT) c.setDisplayHeight(convert2Int(aValue.toString())); 298 else if (columnIndex == IDX_DECIMALS) c.setDecimals(convert2Int(aValue.toString())); 299 else if (columnIndex == IDX_ENFORCE) c.setEnforced(((Boolean ) aValue).booleanValue()); 300 else if (columnIndex == IDX_FORMAT_MASK) c.setFormatMask(aValue.toString()); 301 else if (columnIndex == IDX_RE_MASK) c.setReValidationMask(aValue.toString()); 302 else if (columnIndex == IDX_RE_FAIL_MSG) c.setReFailureMessage(aValue.toString()); 303 else if (columnIndex == IDX_COMPONENT) c.setComponentType(aValue.toString()); 304 else if (columnIndex == IDX_PACKAGE) 305 { 306 DjPackage p = (DjPackage) aValue; 307 if (p == _defaultPackage) p = null; 308 c.setPackage(p); 309 } 310 else if (columnIndex == IDX_DESCRIPTION) c.setDescription(aValue.toString()); 311 setStatusMessage(ModelEditor.OK_MSG); 312 } 313 catch (Exception x) 314 { 315 setStatusMessage(x); 316 } 317 } 318 319 public boolean isCellEditable(int row, int column) 320 { 321 if (!isEditable()) return false; 322 323 if ((column == IDX_FORMAT_MASK) || (column == IDX_PACKAGE) || (column == IDX_COMPONENT) || (column == IDX_WIDTH) 324 || (column == IDX_HEIGHT)) return true; 325 326 DjDomain c = (DjDomain) _data.get(row); 327 if (c.getSuperDomain() != null && column == IDX_TYPE) return false; 328 return !c.isBuiltIn(); 329 } 330 331 int counter = 1; 332 333 public int insertRow(int atIdx) 334 { 335 if (!isEditable()) return -1; 336 try 337 { 338 DjDomain c = new DjDomain(Messages.getString("global.newSomething") + counter++, "String", 0, 0, "", Messages 339 .getString("DomainTableModel.ANewDomain"), true); 340 341 if (_filterPackage != _defaultPackage && _filterPackage != _allPackages) c.setPackage(_filterPackage); 342 _mgr.addDomain(c); 343 _data.add(atIdx, c); 344 notifyModified(); 345 setStatusMessage(ModelEditor.OK_MSG); 346 return atIdx; 347 } 348 catch (Exception x) 349 { 350 setStatusMessage(x.getMessage(), false); 351 } 352 return -1; 353 } 354 355 public boolean deleteRow(int atIdx) 356 { 357 if (!isEditable()) return false; 358 try 359 { 360 DjDomain c = (DjDomain) _data.get(atIdx); 361 if (c.isBuiltIn()) 362 { 363 setStatusMessage(Messages.getString("DomainTableModel.CanNotDeleteBuiltIn"), false); 364 return false; 365 } 366 if (_mgr.isDomainUsed(c)) 367 { 368 setStatusMessage(Messages.getString("DomainTableModel.CanNotDeleteUsed"), false); 369 return false; 370 } 371 _mgr.removeDomain(c); 372 _data.remove(c); 373 notifyModified(); 374 setStatusMessage(ModelEditor.OK_MSG); 375 return true; 376 } 377 catch (Exception x) 378 { 379 setStatusMessage(x.getMessage(), false); 380 } 381 return false; 382 } 383 384 public MouseAdapter getHeaderClickListener(DjTable table) 385 { 386 return new ColumnListener(table); 387 } 388 389 public void setFilter(DjPackage filterPackage) 390 { 391 _filterPackage = filterPackage; 392 _data = new ArrayList (); 393 _data.addAll(Arrays.asList(_mgr.getDomains())); 394 if (_filterPackage != _allPackages) 395 { 396 if (_filterPackage == _defaultPackage) filterPackages(_data, null); 397 else filterPackages(_data, _filterPackage); 398 } 399 } 400 401 private void filterPackages(ArrayList filtered, DjPackage pack) 402 { 403 int i = 0; 404 while (i < filtered.size()) 405 { 406 DjDomain dom = (DjDomain) filtered.get(i); 407 if (dom.getPackage() != pack) filtered.remove(i); 408 else i++; 409 } 410 } 411 412 public DjPackage getAllPackagesPackage() 413 { 414 return _allPackages; 415 } 416 417 public DjPackage getDefaultPackage() 418 { 419 return _defaultPackage; 420 } 421 422 class ColumnListener extends MouseAdapter 423 { 424 protected DjTable _table; 425 boolean _sortAsc = true; 426 int _sortCol = -1; 427 428 public ColumnListener(DjTable table) 429 { 430 _table = table; 431 } 432 433 public void mouseClicked(MouseEvent e) 434 { 435 TableColumnModel colModel = _table.getColumnModel(); 436 int columnModelIndex = colModel.getColumnIndexAtX(e.getX()); 437 int modelIndex = colModel.getColumn(columnModelIndex).getModelIndex(); 438 if (modelIndex < 0) return; 439 if (_sortCol == modelIndex) _sortAsc = !_sortAsc; 440 else _sortCol = modelIndex; 441 for (int i = 0; i < getColumnCount(); i++) 442 { 443 TableColumn column = colModel.getColumn(i); 444 column.setHeaderValue(getColumnName(column.getModelIndex())); 445 } 446 _table.getTableHeader().repaint(); 447 Collections.sort(_data, new DomainComparator(modelIndex, _sortAsc)); 448 _table.tableChanged(new TableModelEvent (DomainTableModel.this)); 449 _table.repaint(); 450 } 451 } 452 453 public DjDomain getDomain(int idx) 454 { 455 return (DjDomain) _data.get(idx); 456 } 457 458 public class DomainComparator implements Comparator 459 { 460 int _cellIdx; 461 boolean _sortAsc; 462 463 public DomainComparator(int cellIdx, boolean sortAsc) 464 { 465 _cellIdx = cellIdx; 466 _sortAsc = sortAsc; 467 } 468 469 public int compare(Object o1, Object o2) 470 { 471 DjDomain f1 = (DjDomain) o1; 472 DjDomain f2 = (DjDomain) o2; 473 474 int result = 0; 475 476 if (_cellIdx == IDX_CASE_CONVERSION) result = DjString 477 .safeCompare(f1.getCaseConversion(), f2.getCaseConversion()); 478 if (_cellIdx == IDX_COMPONENT) result = DjString 479 .safeCompare(f1.getComponentTypeName(), f2.getComponentTypeName()); 480 if (_cellIdx == IDX_DECIMALS) result = f1.getDecimals() - f2.getDecimals(); 481 if (_cellIdx == IDX_DESCRIPTION) result = DjString.safeCompare(f1.getDescription(), f2.getDescription()); 482 if (_cellIdx == IDX_ENFORCE) result = (f1.isEnforced() ? 1 : 0) - (f2.isEnforced() ? 1 : 0); 483 if (_cellIdx == IDX_FORMAT_MASK) result = DjString.safeCompare(f1.getFormatMask(), f2.getFormatMask()); 484 if (_cellIdx == IDX_HEIGHT) result = f1.getDisplayHeight() - f2.getDisplayHeight(); 485 if (_cellIdx == IDX_LENGTH) result = f1.getLength() - f2.getLength(); 486 if (_cellIdx == IDX_NAME) result = DjString.safeCompare(f1.getName(), f2.getName()); 487 if (_cellIdx == IDX_PACKAGE) result = DjString.safeCompare(f1.getPackage(), f2.getPackage()); 488 if (_cellIdx == IDX_RE_FAIL_MSG) result = DjString 489 .safeCompare(f1.getReFailureMessage(), f2.getReFailureMessage()); 490 if (_cellIdx == IDX_SUPER) result = DjString.safeCompare(f1.getSuperDomain(), f2.getSuperDomain()); 491 if (_cellIdx == IDX_TYPE) result = DjString.safeCompare(f1.getNativeType(), f2.getNativeType()); 492 if (_cellIdx == IDX_WIDTH) result = f1.getDisplayWidth() - f2.getDisplayWidth(); 493 494 if (!_sortAsc) result = -result; 495 return result; 496 } 497 } 498 499 } | Popular Tags |