1 56 57 package org.objectstyle.cayenne.modeler.editor; 58 59 import java.awt.BorderLayout ; 60 import java.awt.Color ; 61 import java.awt.event.ActionEvent ; 62 import java.awt.event.ActionListener ; 63 import java.util.Arrays ; 64 import java.util.EventObject ; 65 66 import javax.swing.DefaultComboBoxModel ; 67 import javax.swing.JButton ; 68 import javax.swing.JCheckBox ; 69 import javax.swing.JComboBox ; 70 import javax.swing.JOptionPane ; 71 import javax.swing.JPanel ; 72 import javax.swing.JTextField ; 73 import javax.swing.JToolBar ; 74 75 import org.apache.commons.collections.CollectionUtils; 76 import org.apache.commons.collections.Predicate; 77 import org.objectstyle.cayenne.access.DataDomain; 78 import org.objectstyle.cayenne.exp.Expression; 79 import org.objectstyle.cayenne.map.DataMap; 80 import org.objectstyle.cayenne.map.DbEntity; 81 import org.objectstyle.cayenne.map.MapObject; 82 import org.objectstyle.cayenne.map.ObjEntity; 83 import org.objectstyle.cayenne.map.event.EntityEvent; 84 import org.objectstyle.cayenne.modeler.Application; 85 import org.objectstyle.cayenne.modeler.ProjectController; 86 import org.objectstyle.cayenne.modeler.action.CreateAttributeAction; 87 import org.objectstyle.cayenne.modeler.action.CreateRelationshipAction; 88 import org.objectstyle.cayenne.modeler.action.ObjEntitySyncAction; 89 import org.objectstyle.cayenne.modeler.event.EntityDisplayEvent; 90 import org.objectstyle.cayenne.modeler.event.ObjEntityDisplayListener; 91 import org.objectstyle.cayenne.modeler.util.CayenneWidgetFactory; 92 import org.objectstyle.cayenne.modeler.util.CellRenderers; 93 import org.objectstyle.cayenne.modeler.util.Comparators; 94 import org.objectstyle.cayenne.modeler.util.ModelerUtil; 95 import org.objectstyle.cayenne.modeler.util.TextAdapter; 96 import org.objectstyle.cayenne.util.Util; 97 import org.objectstyle.cayenne.util.XMLEncoder; 98 import org.objectstyle.cayenne.validation.ValidationException; 99 import org.scopemvc.util.convertor.StringConvertor; 100 import org.scopemvc.util.convertor.StringConvertors; 101 102 import com.jgoodies.forms.builder.DefaultFormBuilder; 103 import com.jgoodies.forms.layout.FormLayout; 104 105 111 public class ObjEntityTab extends JPanel implements ObjEntityDisplayListener, 112 ExistingSelectionProcessor { 113 114 private static final Object noInheritance = new MapObject( 115 "Direct Mapping to Table/View") { 116 117 public void encodeAsXML(XMLEncoder encoder) { 118 } 119 }; 120 121 protected ProjectController mediator; 122 protected TextAdapter name; 123 protected TextAdapter className; 124 protected TextAdapter superClassName; 125 protected TextAdapter qualifier; 126 protected JComboBox dbEntityCombo; 127 protected JButton syncWithDbEntityButton; 128 protected JComboBox superEntityCombo; 129 protected JButton tableLabel; 130 protected JCheckBox readOnly; 131 protected JCheckBox optimisticLocking; 132 133 protected TextAdapter clientClassName; 134 protected TextAdapter clientSuperClassName; 135 136 public ObjEntityTab(ProjectController mediator) { 137 this.mediator = mediator; 138 initView(); 139 initController(); 140 } 141 142 private void initView() { 143 this.setLayout(new BorderLayout ()); 144 145 JToolBar toolBar = new JToolBar (); 146 Application app = Application.getInstance(); 147 toolBar.add(app.getAction(ObjEntitySyncAction.getActionName()).buildButton()); 148 toolBar.add(app.getAction(CreateAttributeAction.getActionName()).buildButton()); 149 toolBar 150 .add(app 151 .getAction(CreateRelationshipAction.getActionName()) 152 .buildButton()); 153 add(toolBar, BorderLayout.NORTH); 154 155 name = new TextAdapter(new JTextField ()) { 157 158 protected void updateModel(String text) { 159 setEntityName(text); 160 } 161 }; 162 superClassName = new TextAdapter(new JTextField ()) { 163 164 protected void updateModel(String text) { 165 setSuperClassName(text); 166 } 167 }; 168 className = new TextAdapter(new JTextField ()) { 169 170 protected void updateModel(String text) { 171 setClassName(text); 172 } 173 }; 174 qualifier = new TextAdapter(new JTextField ()) { 175 176 protected void updateModel(String text) { 177 setQualifier(text); 178 } 179 }; 180 181 clientClassName = new TextAdapter(new JTextField ()) { 182 183 protected void updateModel(String text) { 184 setClientClassName(text); 185 } 186 }; 187 clientSuperClassName = new TextAdapter(new JTextField ()) { 188 189 protected void updateModel(String text) { 190 setClientSuperClassName(text); 191 } 192 }; 193 194 dbEntityCombo = CayenneWidgetFactory.createComboBox(); 195 superEntityCombo = CayenneWidgetFactory.createComboBox(); 196 197 readOnly = new JCheckBox (); 198 optimisticLocking = new JCheckBox (); 199 200 tableLabel = CayenneWidgetFactory.createLabelButton("Table/View:"); 201 syncWithDbEntityButton = CayenneWidgetFactory.createButton("Sync w/DbEntity"); 202 syncWithDbEntityButton.setIcon(ModelerUtil.buildIcon("icon-sync.gif")); 203 204 FormLayout layout = new FormLayout( 206 "right:max(50dlu;pref), 3dlu, fill:max(200dlu;pref), 3dlu, fill:140", 207 ""); 208 DefaultFormBuilder builder = new DefaultFormBuilder(layout); 209 builder.setDefaultDialogBorder(); 210 211 builder.appendSeparator("ObjEntity Configuration"); 212 builder.append("ObjEntity Name:", name.getComponent(), 3); 213 builder.append("Inheritance:", superEntityCombo, 3); 214 builder.append(tableLabel, dbEntityCombo, syncWithDbEntityButton); 215 216 builder.appendSeparator(); 217 218 builder.append("Java Class:", className.getComponent(), 3); 219 builder.append("Superclass:", superClassName.getComponent(), 3); 220 builder.append("Qualifier", qualifier.getComponent(), 3); 221 builder.append("Read-Only:", readOnly, 3); 222 builder.append("Optimistic Locking:", optimisticLocking, 3); 223 224 builder.appendSeparator("Java Client"); 225 builder.append("Client Java Class:", clientClassName.getComponent(), 3); 226 builder.append("Client Superclass:", clientSuperClassName.getComponent(), 3); 227 228 add(builder.getPanel(), BorderLayout.CENTER); 229 } 230 231 private void initController() { 232 234 mediator.addObjEntityDisplayListener(this); 235 236 dbEntityCombo.addActionListener(new ActionListener () { 237 238 public void actionPerformed(ActionEvent e) { 239 ObjEntity entity = mediator.getCurrentObjEntity(); 241 DbEntity dbEntity = (DbEntity) dbEntityCombo.getSelectedItem(); 242 243 if (dbEntity != entity.getDbEntity()) { 244 entity.setDbEntity(dbEntity); 245 mediator.fireObjEntityEvent(new EntityEvent(this, entity)); 246 } 247 } 248 }); 249 250 superEntityCombo.addActionListener(new ActionListener () { 251 252 public void actionPerformed(ActionEvent e) { 253 MapObject superEntity = (MapObject) superEntityCombo.getSelectedItem(); 255 String name = (superEntity == noInheritance) ? null : superEntity 256 .getName(); 257 258 ObjEntity entity = mediator.getCurrentObjEntity(); 259 260 if (!Util.nullSafeEquals(name, entity.getSuperEntityName())) { 261 entity.setSuperEntityName(name); 262 263 activateFields(name == null); 266 dbEntityCombo.getModel().setSelectedItem(entity.getDbEntity()); 267 superClassName.setText(entity.getSuperClassName()); 268 269 272 DataDomain domain = mediator.getCurrentDataDomain(); 273 DataMap map = mediator.getCurrentDataMap(); 274 275 mediator.fireObjEntityEvent(new EntityEvent(this, entity)); 276 mediator.fireObjEntityDisplayEvent(new EntityDisplayEvent( 277 this, 278 entity, 279 map, 280 domain)); 281 } 282 } 283 }); 284 285 tableLabel.addActionListener(new ActionListener () { 286 287 public void actionPerformed(ActionEvent e) { 288 DbEntity entity = mediator.getCurrentObjEntity().getDbEntity(); 290 if (entity != null) { 291 DataDomain dom = mediator.getCurrentDataDomain(); 292 mediator.fireDbEntityDisplayEvent(new EntityDisplayEvent( 293 this, 294 entity, 295 entity.getDataMap(), 296 dom)); 297 } 298 } 299 }); 300 301 syncWithDbEntityButton.addActionListener(new ObjEntitySyncAction(mediator 302 .getApplication())); 303 304 readOnly.addActionListener(new ActionListener () { 305 306 public void actionPerformed(ActionEvent e) { 307 ObjEntity entity = mediator.getCurrentObjEntity(); 308 if (entity != null) { 309 entity.setReadOnly(readOnly.isSelected()); 310 mediator.fireObjEntityEvent(new EntityEvent(this, entity)); 311 } 312 } 313 }); 314 315 optimisticLocking.addActionListener(new ActionListener () { 316 317 public void actionPerformed(ActionEvent e) { 318 ObjEntity entity = mediator.getCurrentObjEntity(); 319 if (entity != null) { 320 entity.setDeclaredLockType(optimisticLocking.isSelected() 321 ? ObjEntity.LOCK_TYPE_OPTIMISTIC 322 : ObjEntity.LOCK_TYPE_NONE); 323 mediator.fireObjEntityEvent(new EntityEvent(this, entity)); 324 } 325 } 326 }); 327 } 328 329 333 private void initFromModel(final ObjEntity entity) { 334 qualifier.getComponent().setBackground(Color.WHITE); 336 337 name.setText(entity.getName()); 338 superClassName.setText(entity.getSuperClassName()); 339 className.setText(entity.getClassName()); 340 readOnly.setSelected(entity.isReadOnly()); 341 342 clientClassName.setText(entity.getClientClassName()); 343 clientSuperClassName.setText(entity.getClientSuperClassName()); 344 345 346 StringConvertor convertor = StringConvertors.forClass(Expression.class); 347 qualifier.setText(convertor.valueAsString(entity.getDeclaredQualifier())); 348 349 optimisticLocking 353 .setSelected(entity.getDeclaredLockType() == ObjEntity.LOCK_TYPE_OPTIMISTIC); 354 355 DataMap map = mediator.getCurrentDataMap(); 357 Object [] dbEntities = map.getNamespace().getDbEntities().toArray(); 358 Arrays.sort(dbEntities, Comparators.getDataMapChildrenComparator()); 359 360 DefaultComboBoxModel dbModel = new DefaultComboBoxModel (dbEntities); 361 dbModel.setSelectedItem(entity.getDbEntity()); 362 dbEntityCombo.setRenderer(CellRenderers.entityListRendererWithIcons(map)); 363 dbEntityCombo.setModel(dbModel); 364 365 activateFields(entity.getSuperEntityName() == null); 367 368 Predicate inheritanceFilter = new Predicate() { 370 371 public boolean evaluate(Object object) { 372 if (entity == object) { 374 return false; 375 } 376 377 if (object instanceof ObjEntity) { 378 return !((ObjEntity) object).isSubentityOf(entity); 379 } 380 381 return false; 382 } 383 }; 384 385 Object [] objEntities = CollectionUtils 386 .select(map.getNamespace().getObjEntities(), inheritanceFilter) 387 .toArray(); 388 Arrays.sort(objEntities, Comparators.getDataMapChildrenComparator()); 389 Object [] finalObjEntities = new Object [objEntities.length + 1]; 390 finalObjEntities[0] = noInheritance; 391 System.arraycopy(objEntities, 0, finalObjEntities, 1, objEntities.length); 392 393 DefaultComboBoxModel superEntityModel = new DefaultComboBoxModel (finalObjEntities); 394 superEntityModel.setSelectedItem(entity.getSuperEntity()); 395 superEntityCombo.setRenderer(CellRenderers.entityListRendererWithIcons(map)); 396 superEntityCombo.setModel(superEntityModel); 397 } 398 399 void setClassName(String className) { 400 if (className != null && className.trim().length() == 0) { 401 className = null; 402 } 403 404 ObjEntity entity = mediator.getCurrentObjEntity(); 405 406 if (entity != null && !Util.nullSafeEquals(entity.getClassName(), className)) { 408 entity.setClassName(className); 409 mediator.fireObjEntityEvent(new EntityEvent(this, entity)); 410 } 411 } 412 413 void setSuperClassName(String text) { 414 415 if (text != null && text.trim().length() == 0) { 416 text = null; 417 } 418 419 ObjEntity ent = mediator.getCurrentObjEntity(); 420 421 if (ent != null && !Util.nullSafeEquals(ent.getSuperClassName(), text)) { 422 ent.setSuperClassName(text); 423 mediator.fireObjEntityEvent(new EntityEvent(this, ent)); 424 } 425 } 426 427 void setClientClassName(String className) { 428 if (className != null && className.trim().length() == 0) { 429 className = null; 430 } 431 432 ObjEntity entity = mediator.getCurrentObjEntity(); 433 434 if (entity != null 436 && !Util.nullSafeEquals(entity.getClientClassName(), className)) { 437 entity.setClientClassName(className); 438 mediator.fireObjEntityEvent(new EntityEvent(this, entity)); 439 } 440 } 441 442 void setClientSuperClassName(String text) { 443 444 if (text != null && text.trim().length() == 0) { 445 text = null; 446 } 447 448 ObjEntity ent = mediator.getCurrentObjEntity(); 449 450 if (ent != null && !Util.nullSafeEquals(ent.getClientSuperClassName(), text)) { 451 ent.setClientSuperClassName(text); 452 mediator.fireObjEntityEvent(new EntityEvent(this, ent)); 453 } 454 } 455 456 void setQualifier(String text) { 457 if (text != null && text.trim().length() == 0) { 458 text = null; 459 } 460 461 ObjEntity entity = mediator.getCurrentObjEntity(); 462 if (entity != null) { 463 464 StringConvertor convertor = StringConvertors.forClass(Expression.class); 465 try { 466 String oldQualifier = convertor.valueAsString(entity 467 .getDeclaredQualifier()); 468 if (!Util.nullSafeEquals(oldQualifier, text)) { 469 Expression exp = (Expression) convertor.stringAsValue(text); 470 entity.setDeclaredQualifier(exp); 471 mediator.fireObjEntityEvent(new EntityEvent(this, entity)); 472 } 473 } 474 catch (IllegalArgumentException ex) { 475 throw new ValidationException(ex.getMessage()); 477 } 478 } 479 } 480 481 void setEntityName(String newName) { 482 if (newName != null && newName.trim().length() == 0) { 483 newName = null; 484 } 485 486 ObjEntity entity = mediator.getCurrentObjEntity(); 487 if (entity == null) { 488 return; 489 } 490 491 if (Util.nullSafeEquals(newName, entity.getName())) { 492 return; 493 } 494 495 if (newName == null) { 496 throw new ValidationException("Entity name is required."); 497 } 498 else if (entity.getDataMap().getObjEntity(newName) == null) { 499 EntityEvent e = new EntityEvent(this, entity, entity.getName()); 501 entity.setName(newName); 502 503 mediator.fireObjEntityEvent(e); 504 505 String suggestedClassName = suggestedClassName(entity); 507 508 String entityClassName = entity.getClassName(); 510 if (entityClassName == null 511 || entityClassName.length() == 0 512 || entityClassName.endsWith("UntitledObjEntity")) { 513 className.setText(suggestedClassName); 514 entity.setClassName(suggestedClassName); 515 } 516 517 if (suggestedClassName != null 518 && !suggestedClassName.equals(entity.getClassName())) { 519 JOptionPane pane = new JOptionPane ( 520 new Object [] { 521 "Change class name to match new entity name?", 522 "Suggested class name: " + suggestedClassName 523 }, 524 JOptionPane.QUESTION_MESSAGE, 525 JOptionPane.OK_CANCEL_OPTION, 526 null, 527 new Object [] { 528 "Change", "Cancel" 529 }); 530 531 pane 532 .createDialog(Application.getFrame(), "Update Class Name") 533 .setVisible(true); 534 if ("Change".equals(pane.getValue())) { 535 className.setText(suggestedClassName); 536 setClassName(suggestedClassName); 537 } 538 } 539 } 540 else { 541 throw new ValidationException("There is another entity with name '" 543 + newName 544 + "'."); 545 } 546 } 547 548 551 private String suggestedClassName(ObjEntity entity) { 552 String name = entity.getName(); 553 if (name == null || name.trim().length() == 0) { 554 return null; 555 } 556 557 String oldFQN = entity.getClassName(); 559 String pkg = (entity.getDataMap() != null) ? entity 560 .getDataMap() 561 .getDefaultPackage() : null; 562 563 if (oldFQN != null && oldFQN.lastIndexOf('.') > 0) { 564 pkg = oldFQN.substring(0, oldFQN.lastIndexOf('.')); 565 } 566 567 if (pkg == null) { 568 pkg = ""; 569 } 570 else { 571 pkg = pkg + '.'; 572 } 573 574 int dot = name.lastIndexOf('.'); 576 if (dot >= 0 && dot < name.length() - 1) { 577 name = name.substring(dot + 1); 578 } 579 580 return pkg + name; 581 } 582 583 void activateFields(boolean active) { 584 superClassName.getComponent().setEnabled(active); 585 superClassName.getComponent().setEditable(active); 586 clientSuperClassName.getComponent().setEnabled(active); 587 clientSuperClassName.getComponent().setEditable(active); 588 dbEntityCombo.setEnabled(active); 589 } 590 591 public void processExistingSelection(EventObject e) { 592 EntityDisplayEvent ede = new EntityDisplayEvent(this, mediator 593 .getCurrentObjEntity(), mediator.getCurrentDataMap(), mediator 594 .getCurrentDataDomain()); 595 mediator.fireObjEntityDisplayEvent(ede); 596 } 597 598 public void currentObjEntityChanged(EntityDisplayEvent e) { 599 ObjEntity entity = (ObjEntity) e.getEntity(); 600 if (entity == null || !e.isEntityChanged()) { 601 return; 602 } 603 604 initFromModel(entity); 605 } 606 607 } | Popular Tags |