1 30 package com.genimen.djeneric.tools.modeler.userperspective; 31 32 import java.awt.Component ; 33 import java.util.ArrayList ; 34 import java.util.HashMap ; 35 import java.util.Iterator ; 36 37 import javax.swing.DefaultComboBoxModel ; 38 import javax.swing.JCheckBox ; 39 import javax.swing.JComboBox ; 40 import javax.swing.JTable ; 41 import javax.swing.event.PopupMenuEvent ; 42 import javax.swing.event.PopupMenuListener ; 43 import javax.swing.table.TableCellEditor ; 44 import javax.swing.text.JTextComponent ; 45 46 import com.genimen.djeneric.language.Messages; 47 import com.genimen.djeneric.repository.DjDomain; 48 import com.genimen.djeneric.repository.DjDomainValue; 49 import com.genimen.djeneric.repository.DjExtent; 50 import com.genimen.djeneric.repository.DjProperty; 51 import com.genimen.djeneric.repository.exceptions.DjenericException; 52 import com.genimen.djeneric.repository.exceptions.ObjectNotDefinedException; 53 import com.genimen.djeneric.structure.ExtentUsage; 54 import com.genimen.djeneric.structure.PropertyUsage; 55 import com.genimen.djeneric.structure.RelationDescriptor; 56 import com.genimen.djeneric.tools.modeler.ModelEditor; 57 import com.genimen.djeneric.ui.DjCellEditor; 58 import com.genimen.djeneric.ui.DjCheckCellRenderer; 59 import com.genimen.djeneric.ui.DjModelColumn; 60 import com.genimen.djeneric.ui.DjPropertyPathCompleter; 61 import com.genimen.djeneric.ui.DjTableModel; 62 63 public class PropertyUsageTableModel extends DjTableModel 64 { 65 private static final long serialVersionUID = 1L; 66 private ExtentUsage _extentUsage; 67 68 private final int IDX_SEQ = 0; 69 private final int IDX_COLUMN = 1; 70 private final int IDX_PROMPT = 2; 71 private final int IDX_DESCRIPTION = 3; 72 private final int IDX_REQUIRED = 4; 73 private final int IDX_UPDATEABLE = 5; 74 private final int IDX_QUERYABLE = 6; 75 private final int IDX_DISPLAYED = 7; 76 private final int IDX_DEFAULT_VALUE = 8; 77 private final int IDX_COMPONENT = 9; 78 private final int IDX_WIDTH = 10; 79 private final int IDX_HEIGHT = 11; 80 private final int IDX_SORT_ORDER = 12; 81 private final int IDX_RELATIVE_PATH = 13; 82 private final int IDX_CUSTOM_EDITOR = 14; 83 84 private final int _colCount = 15; 85 86 public PropertyUsageTableModel(ExtentUsage extentUsage) 87 { 88 _extentUsage = extentUsage; 89 90 _columns = new DjModelColumn[_colCount]; 91 92 _columns[IDX_SEQ] = new DjModelColumn(Messages.getString("global.Seq"), 30, Messages 93 .getString("PropertyUsageTableModel.SequentialOrder")); 94 _columns[IDX_COLUMN] = new DjModelColumn(Messages.getString("global.Property"), 150, Messages 95 .getString("PropertyUsageTableModel.UnderlyingProp")); 96 _columns[IDX_PROMPT] = new DjModelColumn(Messages.getString("global.Prompt"), 150, Messages 97 .getString("PropertyUsageTableModel.PromptForProp")); 98 _columns[IDX_DESCRIPTION] = new DjModelColumn(Messages.getString("PropertyUsageTableModel.Tooltip"), 150, Messages 99 .getString("PropertyUsageTableModel.Tooltip")); 100 _columns[IDX_REQUIRED] = new DjModelColumn(Messages.getString("global.Req"), 30, 101 Messages.getString("global.IsReq"), new DjCheckCellRenderer()); 102 _columns[IDX_UPDATEABLE] = new DjModelColumn(Messages.getString("PropertyUsageTableModel.Upd"), 30, Messages 103 .getString("PropertyUsageTableModel.IsUpd"), new DjCheckCellRenderer()); 104 _columns[IDX_QUERYABLE] = new DjModelColumn(Messages.getString("global.Qry"), 30, Messages 105 .getString("global.IsQuery"), new DjCheckCellRenderer()); 106 _columns[IDX_DISPLAYED] = new DjModelColumn(Messages.getString("PropertyUsageTableModel.Dsp"), 30, Messages 107 .getString("PropertyUsageTableModel.IsDsp"), new DjCheckCellRenderer()); 108 _columns[IDX_DEFAULT_VALUE] = new DjModelColumn(Messages.getString("global.Default"), 80, Messages 109 .getString("PropertyUsageTableModel.DefaultValue")); 110 _columns[IDX_COMPONENT] = new DjModelColumn(Messages.getString("global.Component"), 80, Messages 111 .getString("PropertyUsageTableModel.HowDsp")); 112 _columns[IDX_WIDTH] = new DjModelColumn(Messages.getString("global.Width"), 50, Messages 113 .getString("PropertyUsageTableModel.WidthInPxs")); 114 _columns[IDX_HEIGHT] = new DjModelColumn(Messages.getString("global.Height"), 50, Messages 115 .getString("PropertyUsageTableModel.HeightInPxs")); 116 _columns[IDX_SORT_ORDER] = new DjModelColumn(Messages.getString("PropertyUsageTableModel.SortOrder"), 35, Messages 117 .getString("PropertyUsageTableModel.HowToSort")); 118 _columns[IDX_RELATIVE_PATH] = new DjModelColumn(Messages.getString("PropertyUsageTableModel.RelativePath"), 150, 119 Messages.getString("PropertyUsageTableModel.RelativePathDescr")); 120 _columns[IDX_CUSTOM_EDITOR] = new DjModelColumn(Messages.getString("global.CustomEditor"), 150, Messages 121 .getString("PropertyUsageTableModel.CustomEditor")); 122 } 123 124 public DjExtent getExtent() 125 { 126 return _extentUsage.getExtent(); 127 } 128 129 public DjProperty[] getAvailableProperties(DjProperty selectedProperty) 130 { 131 ArrayList result = new ArrayList (); 132 result.add(selectedProperty); 133 DjExtent extent = _extentUsage.getExtent(); 134 for (int i = 0; i < extent.getPropertyCount(); i++) 135 { 136 boolean found = false; 137 for (int j = 0; j < _extentUsage.getPropertyUsageCount(); j++) 138 { 139 if (_extentUsage.getPropertyUsage(j).getBaseProperty().getInternalId() == extent.getProperty(i).getInternalId()) 140 { 141 found = true; 142 break; 143 } 144 } 145 if (!found) result.add(extent.getProperty(i)); 146 } 147 return (DjProperty[]) result.toArray(new DjProperty[0]); 148 } 149 150 public TableCellEditor getColumnEditor(int column) 151 { 152 if (column == IDX_COLUMN) 153 { 154 return new DjCellEditor(new JComboBox ()) 155 { 156 private static final long serialVersionUID = 1L; 157 158 public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) 159 { 160 JComboBox cbb = (JComboBox ) editorComponent; 161 cbb.setModel(new DefaultComboBoxModel (getAvailableProperties(_extentUsage.getPropertyUsage(row) 162 .getBaseProperty()))); 163 delegate.setValue(value); 164 return editorComponent; 165 } 166 }; 167 } 168 else if (column == IDX_RELATIVE_PATH) 169 { 170 return new CompletionCellEditor(new JComboBox ()); 171 } 172 else if (column == IDX_DEFAULT_VALUE) 173 { 174 return new DefaultValueCellEditor(new JComboBox ()); 175 } 176 else if (column == IDX_COMPONENT) 177 { 178 return new DjCellEditor(new JComboBox (DjDomain.getComponentTypeNames())); 179 } 180 else if (column == IDX_REQUIRED || column == IDX_UPDATEABLE || column == IDX_QUERYABLE || column == IDX_DISPLAYED) 181 { 182 return new DjCellEditor(new JCheckBox ()); 183 } 184 185 return _columns[column].getCellEditor(); 186 } 187 188 public int getRowCount() 189 { 190 if (_extentUsage == null) return 0; 191 192 return _extentUsage.getPropertyUsageCount(); 193 } 194 195 public int getPropertyCount() 196 { 197 return _colCount; 198 } 199 200 public Object getValueAt(int nRow, int nCol) 201 { 202 if (nRow < 0 || nRow >= getRowCount()) return ""; 203 204 PropertyUsage c = _extentUsage.getPropertyUsage(nRow); 205 if (nCol == IDX_SEQ) return new Integer (c.getSeq()); 206 else if (nCol == IDX_COLUMN) return c.getBaseProperty(); 207 else if (nCol == IDX_PROMPT) return c.getPrompt(); 208 else if (nCol == IDX_DESCRIPTION) return c.getDescription(); 209 else if (nCol == IDX_DEFAULT_VALUE) return c.getDefaultValue(); 210 else if (nCol == IDX_COMPONENT) return c.getComponentTypeName(); 211 else if (nCol == IDX_WIDTH) 212 { 213 if (c.getDisplayWidth() <= 0) return ""; 214 return new Integer (c.getDisplayWidth()); 215 } 216 else if (nCol == IDX_HEIGHT) 217 { 218 if (c.getDisplayHeight() <= 0) return ""; 219 return new Integer (c.getDisplayHeight()); 220 } 221 else if (nCol == IDX_SORT_ORDER) 222 { 223 if (c.getSortOrder() == 0) return ""; 224 return new Integer (c.getSortOrder()); 225 } 226 else if (nCol == IDX_REQUIRED) return new Boolean (c.isRequired()); 227 else if (nCol == IDX_QUERYABLE) return new Boolean (c.isQueryable()); 228 else if (nCol == IDX_DISPLAYED) return new Boolean (c.isDisplayed()); 229 else if (nCol == IDX_UPDATEABLE) return new Boolean (c.isUpdateable()); 230 else if (nCol == IDX_RELATIVE_PATH) return c.getRelativePath(); 231 else if (nCol == IDX_CUSTOM_EDITOR) 232 { 233 if (c.isCustomComponent()) 234 { 235 return c.getCustomEditorClass(); 236 } 237 else return ""; 238 } 239 else return Messages.getString("PropertyUsageTableModel.InvalidCell", String.valueOf(nCol)); 240 } 241 242 public void setValueAt(Object aValue, int rowIndex, int columnIndex) 243 { 244 if (rowIndex < 0 || rowIndex >= getRowCount()) return; 245 if (aValue == null) return; 246 notifyModified(); 247 248 boolean setMsgToOk = true; 249 250 try 251 { 252 PropertyUsage c = _extentUsage.getPropertyUsage(rowIndex); 253 if (columnIndex == IDX_SEQ) 254 { 255 c.setSeq(convert2Int(aValue.toString())); 256 } 257 else if (columnIndex == IDX_WIDTH) 258 { 259 c.setDisplayWidth(convert2Int(aValue.toString())); 260 } 261 else if (columnIndex == IDX_HEIGHT) 262 { 263 c.setDisplayHeight(convert2Int(aValue.toString())); 264 } 265 else if (columnIndex == IDX_SORT_ORDER) 266 { 267 c.setSortOrder(convert2Int(aValue.toString())); 268 } 269 else if (columnIndex == IDX_COLUMN) 270 { 271 if (c.getBaseProperty().getInternalId() == ((DjProperty) aValue).getInternalId()) 272 { 273 return; 275 } 276 c.setProperty((DjProperty) aValue); 277 c.setPrompt(((DjProperty) aValue).getPrompt()); 278 c.setRequired(((DjProperty) aValue).isRequired()); 279 c.setComponentType(((DjProperty) aValue).getType().getComponentType()); 280 fireTableRowsUpdated(rowIndex, rowIndex); 281 } 282 else if (columnIndex == IDX_PROMPT) c.setPrompt(aValue.toString()); 283 else if (columnIndex == IDX_DESCRIPTION) c.setDescription(aValue.toString()); 284 else if (columnIndex == IDX_DEFAULT_VALUE) c.setDefaultValue(aValue.toString()); 285 else if (columnIndex == IDX_COMPONENT) c.setComponentType(aValue.toString()); 286 else if (columnIndex == IDX_REQUIRED) c.setRequired(((Boolean ) aValue).booleanValue()); 287 else if (columnIndex == IDX_QUERYABLE) c.setQueryable(((Boolean ) aValue).booleanValue()); 288 else if (columnIndex == IDX_DISPLAYED) c.setDisplayed(((Boolean ) aValue).booleanValue()); 289 else if (columnIndex == IDX_UPDATEABLE) c.setUpdateable(((Boolean ) aValue).booleanValue()); 290 else if (columnIndex == IDX_RELATIVE_PATH) 291 { 292 c.setRelativePath(aValue.toString()); 293 if (!c.hasValidRelativePath()) 294 { 295 setStatusMessage(Messages.getString("PropertyUsageTableModel.InvalidPath", aValue.toString()), false); 296 setMsgToOk = false; 297 } 298 } 299 else if (columnIndex == IDX_CUSTOM_EDITOR) c.setCustomEditorClass(aValue.toString()); 300 if (setMsgToOk) setStatusMessage(ModelEditor.OK_MSG); 301 } 302 catch (Exception x) 303 { 304 setStatusMessage(x); 305 } 306 307 } 308 309 DjProperty getNextProperty() throws DjenericException 310 { 311 DjExtent extent = _extentUsage.getExtent(); 312 313 RelationDescriptor via = _extentUsage.getVia(); 314 String detailProp = ""; 315 if (via != null) detailProp = via.getDetailPropertyName(); 316 317 for (int i = 0; i < extent.getPropertyCount(); i++) 318 { 319 boolean found = false; 320 DjProperty mightAdd = extent.getProperty(i); 321 322 if (mightAdd.getName().equals(extent.getIdProperty().getName()) || mightAdd.getName().equals(detailProp)) continue; 325 326 for (int j = 0; j < _extentUsage.getPropertyUsageCount(); j++) 327 { 328 DjProperty prop = _extentUsage.getPropertyUsage(j).getBaseProperty(); 329 if (prop.getInternalId() == mightAdd.getInternalId() 330 && _extentUsage.getPropertyUsage(j).getRelativePath() == null) 331 { 332 found = true; 333 break; 334 } 335 } 336 if (!found) return mightAdd; 337 } 338 339 return null; 340 } 341 342 int counter = 1; 343 344 public int insertRow(int atIdx) 345 { 346 if (_extentUsage == null) return -1; 347 try 348 { 349 DjProperty nextCol = getNextProperty(); 350 if (nextCol == null) 351 { 352 setStatusMessage(Messages.getString("PropertyUsageTableModel.AllPropsAdded")); 353 return -1; 354 } 355 356 PropertyUsage c = new PropertyUsage(nextCol); 357 c.setComponentType(nextCol.getType().getComponentType()); 358 c.setSortOrder(nextCol.getSortOrder()); 359 c.setQueryable(nextCol.isQueryable()); 360 _extentUsage.addPropertyUsage(atIdx, c); 361 notifyModified(); 362 setStatusMessage(ModelEditor.OK_MSG); 363 return atIdx; 364 } 365 catch (Exception x) 366 { 367 setStatusMessage(x.getMessage(), false); 368 } 369 return -1; 370 } 371 372 public boolean deleteRow(int atIdx) 373 { 374 try 375 { 376 _extentUsage.removePropertyUsage(atIdx); 377 notifyModified(); 378 setStatusMessage(ModelEditor.OK_MSG); 379 return true; 380 } 381 catch (Exception x) 382 { 383 setStatusMessage(x.getMessage(), false); 384 } 385 return false; 386 } 387 388 public boolean isCellEditable(int row, int column) 389 { 390 if (column == IDX_CUSTOM_EDITOR) 391 { 392 PropertyUsage c = _extentUsage.getPropertyUsage(row); 393 return c.isCustomComponent(); 394 } 395 else if (column == IDX_RELATIVE_PATH) 396 { 397 PropertyUsage c = _extentUsage.getPropertyUsage(row); 398 return (c.getBaseProperty().getType() instanceof DjExtent); 399 } 400 401 return super.isCellEditable(row, column); 402 } 403 404 private final class DefaultValueCellEditor extends DjCellEditor 405 { 406 private static final long serialVersionUID = 1L; 407 408 private DefaultValueCellEditor(JComboBox comboBox) 409 { 410 super(comboBox); 411 } 412 413 public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) 414 { 415 ArrayList possibles = getValidDefaults(row); 416 417 JComboBox cbb = (JComboBox ) editorComponent; 418 cbb.setEditable(true); 419 cbb.setModel(new DefaultComboBoxModel (possibles.toArray())); 420 delegate.setValue(value); 421 return editorComponent; 422 } 423 424 private ArrayList getValidDefaults(int row) 425 { 426 HashMap params; 427 if (_extentUsage.getParent() != null) 428 { 429 params = _extentUsage.getParent().getParameters(); 430 } 431 else 432 { 433 params = new HashMap (); 434 } 435 436 ArrayList possibles = new ArrayList (); 437 438 DjProperty prop = _extentUsage.getPropertyUsage(row).getBaseProperty(); 439 if (prop != null && prop.getType() instanceof DjDomain) 440 { 441 DjDomain domain = (DjDomain) prop.getType(); 442 DjDomainValue[] values = domain.getValidValues(); 443 for (int i = 0; i < values.length; i++) 444 possibles.add(values[i].getValue()); 445 } 446 447 Iterator it = params.keySet().iterator(); 448 while (it.hasNext()) 449 possibles.add("{" + it.next().toString() + "}"); 450 451 possibles.add("{$timestamp}"); 452 possibles.add("{$user}"); 453 possibles.add("{$username}"); 454 possibles.add("{$count}"); 455 possibles.add("{$seq}"); 456 possibles.add("{$unique}"); 457 possibles.add("{:<parameter>}"); 458 possibles.add("{:<parameter>.<property>.<...>}"); 459 return possibles; 460 } 461 } 462 463 class CompletionCellEditor extends DjCellEditor implements PopupMenuListener 464 { 465 private static final long serialVersionUID = 1L; 466 JComboBox _cbb; 467 DjPropertyPathCompleter _ppc; 468 DjExtent _rootExtent; 469 470 public CompletionCellEditor(JComboBox cbb) 471 { 472 super(cbb); 473 cbb.setEditable(true); 474 _cbb = cbb; 475 _ppc = new DjPropertyPathCompleter(); 476 477 Object o = cbb.getEditor().getEditorComponent(); 478 if (o instanceof JTextComponent ) _ppc.setEditor((JTextComponent ) o); 479 cbb.addPopupMenuListener(this); 480 481 } 482 483 public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) 484 { 485 _rootExtent = (DjExtent) _extentUsage.getPropertyUsage(row).getBaseProperty().getType(); 486 delegate.setValue(value); 487 488 return editorComponent; 489 } 490 491 public void popupMenuCanceled(PopupMenuEvent e) 492 { 493 494 } 495 496 public void popupMenuWillBecomeInvisible(PopupMenuEvent e) 497 { 498 ArrayList lst = new ArrayList (); 499 for (int i = 0; i < 10; i++) 500 { 501 lst.add(_ppc.getEditor().getText()); 502 } 503 _cbb.setModel(new DefaultComboBoxModel (lst.toArray())); 504 } 505 506 public void popupMenuWillBecomeVisible(PopupMenuEvent e) 507 { 508 DjExtent rootExtent = _rootExtent; 509 _ppc.setRootExtent(rootExtent); 510 511 ArrayList withPrefix = new ArrayList (); 512 String currentValue = _ppc.getEditor().getText(); 513 514 try 515 { 516 String path = _ppc.getEditor().getText(); 517 int idx = path.lastIndexOf("."); 518 if (idx != -1) 519 { 520 path = path.substring(0, idx).trim(); 521 DjProperty prop = _rootExtent.getProperty(path); 522 if (prop.getType() instanceof DjExtent) 523 { 524 rootExtent = (DjExtent) prop.getType(); 525 } 526 } 527 528 ArrayList lst = _ppc.getCompletions(); 529 String prefix = path; 530 if (rootExtent == _rootExtent) prefix = ""; 531 532 if (prefix.trim().length() > 0) 533 { 534 withPrefix.add(prefix); 535 prefix += "."; 536 } 537 538 Iterator it = lst.iterator(); 539 while (it.hasNext()) 540 { 541 withPrefix.add(prefix + it.next().toString()); 542 } 543 } 544 catch (ObjectNotDefinedException x) 545 { 546 setStatusMessage(x.getMessage()); 547 } 548 withPrefix.remove(currentValue); 549 withPrefix.add(0, currentValue); 550 551 JComboBox cbb = (JComboBox ) editorComponent; 552 cbb.setModel(new DefaultComboBoxModel (withPrefix.toArray())); 553 } 554 } 555 } | Popular Tags |