1 56 package org.objectstyle.cayenne.modeler.editor; 57 58 import java.awt.BorderLayout ; 59 import java.awt.Color ; 60 import java.awt.Component ; 61 import java.awt.event.ActionEvent ; 62 import java.awt.event.ActionListener ; 63 import java.util.Collection ; 64 import java.util.EventObject ; 65 66 import javax.swing.DefaultCellEditor ; 67 import javax.swing.DefaultComboBoxModel ; 68 import javax.swing.JButton ; 69 import javax.swing.JComboBox ; 70 import javax.swing.JPanel ; 71 import javax.swing.JTable ; 72 import javax.swing.JToolBar ; 73 import javax.swing.event.ChangeEvent ; 74 import javax.swing.event.ListSelectionEvent ; 75 import javax.swing.event.ListSelectionListener ; 76 import javax.swing.event.TableModelEvent ; 77 import javax.swing.event.TableModelListener ; 78 import javax.swing.table.DefaultTableCellRenderer ; 79 import javax.swing.table.TableColumn ; 80 81 import org.apache.log4j.Logger; 82 import org.objectstyle.cayenne.map.DataMap; 83 import org.objectstyle.cayenne.map.DeleteRule; 84 import org.objectstyle.cayenne.map.Entity; 85 import org.objectstyle.cayenne.map.MapObject; 86 import org.objectstyle.cayenne.map.ObjEntity; 87 import org.objectstyle.cayenne.map.ObjRelationship; 88 import org.objectstyle.cayenne.map.event.EntityEvent; 89 import org.objectstyle.cayenne.map.event.ObjEntityListener; 90 import org.objectstyle.cayenne.map.event.ObjRelationshipListener; 91 import org.objectstyle.cayenne.map.event.RelationshipEvent; 92 import org.objectstyle.cayenne.modeler.Application; 93 import org.objectstyle.cayenne.modeler.ProjectController; 94 import org.objectstyle.cayenne.modeler.action.CreateRelationshipAction; 95 import org.objectstyle.cayenne.modeler.action.ObjEntitySyncAction; 96 import org.objectstyle.cayenne.modeler.action.RemoveRelationshipAction; 97 import org.objectstyle.cayenne.modeler.dialog.objentity.ObjRelationshipInfoController; 98 import org.objectstyle.cayenne.modeler.event.EntityDisplayEvent; 99 import org.objectstyle.cayenne.modeler.event.ObjEntityDisplayListener; 100 import org.objectstyle.cayenne.modeler.event.RelationshipDisplayEvent; 101 import org.objectstyle.cayenne.modeler.util.CayenneTable; 102 import org.objectstyle.cayenne.modeler.util.CayenneWidgetFactory; 103 import org.objectstyle.cayenne.modeler.util.CellRenderers; 104 import org.objectstyle.cayenne.modeler.util.ModelerUtil; 105 import org.objectstyle.cayenne.modeler.util.PanelFactory; 106 import org.objectstyle.cayenne.modeler.util.UIUtil; 107 108 114 public class ObjEntityRelationshipTab extends JPanel implements ObjEntityDisplayListener, 115 ObjEntityListener, ObjRelationshipListener, ExistingSelectionProcessor { 116 117 private static final Logger logObj = Logger.getLogger(ObjEntityRelationshipTab.class); 118 119 private static final Object [] deleteRules = new Object [] { 120 DeleteRule.deleteRuleName(DeleteRule.NO_ACTION), 121 DeleteRule.deleteRuleName(DeleteRule.NULLIFY), 122 DeleteRule.deleteRuleName(DeleteRule.CASCADE), 123 DeleteRule.deleteRuleName(DeleteRule.DENY), 124 }; 125 126 ProjectController mediator; 127 128 CayenneTable table; 129 JButton resolve; 130 131 public ObjEntityRelationshipTab(ProjectController mediator) { 132 this.mediator = mediator; 133 134 init(); 135 initController(); 136 } 137 138 private void init() { 139 this.setLayout(new BorderLayout ()); 140 141 JToolBar toolBar = new JToolBar (); 142 Application app = Application.getInstance(); 143 toolBar.add(app.getAction(CreateRelationshipAction.getActionName()).buildButton()); 144 toolBar.add(app.getAction(ObjEntitySyncAction.getActionName()).buildButton()); 145 146 toolBar.addSeparator(); 147 148 resolve = new JButton (); 149 resolve.setIcon(ModelerUtil.buildIcon("icon-info.gif")); 150 resolve.setToolTipText("Edit Relationship"); 151 toolBar.add(resolve); 152 153 toolBar.addSeparator(); 154 155 toolBar.add(app.getAction(RemoveRelationshipAction.getActionName()).buildButton()); 156 add(toolBar, BorderLayout.NORTH); 157 158 table = new CayenneTable(); 159 table.setDefaultRenderer(String .class, new StringRenderer()); 160 table.setDefaultRenderer(ObjEntity.class, new EntityRenderer()); 161 162 add(PanelFactory.createTablePanel(table, null), BorderLayout.CENTER); 163 } 164 165 private void initController() { 166 mediator.addObjEntityDisplayListener(this); 167 mediator.addObjEntityListener(this); 168 mediator.addObjRelationshipListener(this); 169 170 resolve.addActionListener(new ActionListener () { 171 172 public void actionPerformed(ActionEvent e) { 173 int row = table.getSelectedRow(); 174 if (row < 0) { 175 return; 176 } 177 178 ObjRelationshipTableModel model = (ObjRelationshipTableModel) table 179 .getModel(); 180 new ObjRelationshipInfoController(mediator, model.getRelationship(row)) 181 .startup(); 182 183 table.getSelectionModel().clearSelection(); 186 table.select(row); 187 } 188 }); 189 190 table.getSelectionModel().addListSelectionListener(new ListSelectionListener () { 191 192 public void valueChanged(ListSelectionEvent e) { 193 processExistingSelection(e); 194 } 195 }); 196 } 197 198 201 public void selectRelationship(ObjRelationship rel) { 202 if (rel == null) { 203 Application.getInstance().getAction(RemoveRelationshipAction.getActionName()).setEnabled(false); 204 return; 205 } 206 Application.getInstance().getAction(RemoveRelationshipAction.getActionName()).setEnabled(true); 208 209 ObjRelationshipTableModel model = (ObjRelationshipTableModel) table.getModel(); 210 java.util.List rels = model.getObjectList(); 211 int relPos = rels.indexOf(rel); 212 if (relPos >= 0) { 213 table.select(relPos); 214 } 215 } 216 217 public void processExistingSelection(EventObject e) { 218 if (e instanceof ChangeEvent ) { 219 table.clearSelection(); 220 } 221 ObjRelationship rel = null; 222 if (table.getSelectedRow() >= 0) { 223 ObjRelationshipTableModel model = (ObjRelationshipTableModel) table 224 .getModel(); 225 rel = model.getRelationship(table.getSelectedRow()); 226 if (rel.getTargetEntity() != null 227 && ((ObjEntity) rel.getSourceEntity()).getDbEntity() != null 228 && ((ObjEntity) rel.getTargetEntity()).getDbEntity() != null) { 229 resolve.setEnabled(true); 230 } 231 else 232 resolve.setEnabled(false); 233 234 UIUtil.scrollToSelectedRow(table); 236 } 237 else 238 resolve.setEnabled(false); 239 240 RelationshipDisplayEvent ev = new RelationshipDisplayEvent(this, rel, mediator 241 .getCurrentObjEntity(), mediator.getCurrentDataMap(), mediator 242 .getCurrentDataDomain()); 243 244 mediator.fireObjRelationshipDisplayEvent(ev); 245 } 246 247 248 public void currentObjEntityChanged(EntityDisplayEvent e) { 249 if (e.getSource() == this) { 250 return; 251 } 252 253 ObjEntity entity = (ObjEntity) e.getEntity(); 254 if (entity != null) { 257 rebuildTable(entity); 258 } 259 260 if (e.isUnselectAttributes()) { 263 table.clearSelection(); 264 } 265 } 266 267 270 private Object [] createObjEntityComboModel() { 271 DataMap map = mediator.getCurrentDataMap(); 272 273 if(map == null) { 275 logObj.warn("createObjEntityComboModel:: Null DataMap."); 276 return new Object [0]; 277 } 278 279 if(map.getNamespace() == null) { 280 logObj.warn("createObjEntityComboModel:: Null DataMap namespace - " + map); 281 return new Object [0]; 282 } 283 284 Collection objEntities = map.getNamespace().getObjEntities(); 285 return objEntities.toArray(); 286 } 287 288 public void objEntityChanged(EntityEvent e) { 289 } 290 291 public void objEntityAdded(EntityEvent e) { 292 reloadEntityList(e); 293 } 294 295 public void objEntityRemoved(EntityEvent e) { 296 reloadEntityList(e); 297 } 298 299 public void objRelationshipChanged(RelationshipEvent e) { 300 table.select(e.getRelationship()); 301 } 302 303 public void objRelationshipAdded(RelationshipEvent e) { 304 rebuildTable((ObjEntity) e.getEntity()); 305 table.select(e.getRelationship()); 306 } 307 308 public void objRelationshipRemoved(RelationshipEvent e) { 309 ObjRelationshipTableModel model = (ObjRelationshipTableModel) table.getModel(); 310 int ind = model.getObjectList().indexOf(e.getRelationship()); 311 model.removeRow(e.getRelationship()); 312 table.select(ind); 313 } 314 315 319 private void reloadEntityList(EntityEvent e) { 320 if (e.getSource() != this){ 321 return; 322 } 323 324 ObjEntity entity = mediator.getCurrentObjEntity(); 326 if (entity == e.getEntity() || entity == null) { 327 return; 328 } 329 330 TableColumn col = table.getColumnModel().getColumn( 331 ObjRelationshipTableModel.REL_TARGET); 332 DefaultCellEditor editor = (DefaultCellEditor ) col.getCellEditor(); 333 334 JComboBox combo = (JComboBox ) editor.getComponent(); 335 combo.setRenderer(CellRenderers.entityListRendererWithIcons(entity.getDataMap())); 336 combo.setModel(new DefaultComboBoxModel (createObjEntityComboModel())); 337 338 ObjRelationshipTableModel model = (ObjRelationshipTableModel) table.getModel(); 339 model.fireTableDataChanged(); 340 } 341 342 protected void rebuildTable(ObjEntity entity) { 343 final ObjRelationshipTableModel model = new ObjRelationshipTableModel( 344 entity, 345 mediator, 346 this); 347 348 model.addTableModelListener(new TableModelListener () { 349 350 public void tableChanged(TableModelEvent e) { 351 if (table.getSelectedRow() >= 0) { 352 ObjRelationship rel = model.getRelationship(table.getSelectedRow()); 353 if (rel.getTargetEntity() != null 354 && ((ObjEntity) rel.getSourceEntity()).getDbEntity() != null 355 && ((ObjEntity) rel.getTargetEntity()).getDbEntity() != null) { 356 resolve.setEnabled(true); 357 } 358 else 359 resolve.setEnabled(false); 360 } 361 } 362 }); 363 364 table.setModel(model); 365 table.setRowHeight(25); 366 table.setRowMargin(3); 367 368 TableColumn lockColumn = table.getColumnModel().getColumn( 369 ObjRelationshipTableModel.REL_LOCKING); 370 lockColumn.setMinWidth(100); 371 372 TableColumn col = table.getColumnModel().getColumn( 373 ObjRelationshipTableModel.REL_NAME); 374 col.setMinWidth(150); 375 376 col = table.getColumnModel().getColumn(ObjRelationshipTableModel.REL_TARGET); 377 col.setMinWidth(150); 378 JComboBox targetCombo = CayenneWidgetFactory.createComboBox( 379 createObjEntityComboModel(), 380 false); 381 targetCombo.setRenderer(CellRenderers.entityListRendererWithIcons(entity 382 .getDataMap())); 383 targetCombo.setEditable(false); 384 targetCombo.setSelectedIndex(-1); 385 DefaultCellEditor editor = new DefaultCellEditor (targetCombo); 386 editor.setClickCountToStart(1); 387 col.setCellEditor(editor); 388 389 col = table.getColumnModel().getColumn(ObjRelationshipTableModel.REL_CARDINALITY); 390 col.setMinWidth(150); 391 392 col = table.getColumnModel().getColumn(ObjRelationshipTableModel.REL_DELETERULE); 393 col.setMinWidth(60); 394 JComboBox deleteRulesCombo = CayenneWidgetFactory.createComboBox( 395 deleteRules, 396 false); 397 deleteRulesCombo.setEditable(false); 398 deleteRulesCombo.setSelectedIndex(0); editor = new DefaultCellEditor (deleteRulesCombo); 400 editor.setClickCountToStart(1); 401 col.setCellEditor(editor); 402 } 403 404 class EntityRenderer extends StringRenderer { 405 406 public Component getTableCellRendererComponent( 407 JTable table, 408 Object value, 409 boolean isSelected, 410 boolean hasFocus, 411 int row, 412 int column) { 413 414 if (value instanceof MapObject) { 415 MapObject mapObject = (MapObject) value; 416 String label = mapObject.getName(); 417 418 if (mapObject instanceof Entity) { 419 Entity entity = (Entity) mapObject; 420 421 DataMap dataMap = entity.getDataMap(); 423 if (dataMap != null && dataMap != mediator.getCurrentDataMap()) { 424 label += " (" + dataMap.getName() + ")"; 425 } 426 } 427 428 value = label; 429 } 430 431 return super.getTableCellRendererComponent( 432 table, 433 value, 434 isSelected, 435 hasFocus, 436 row, 437 column); 438 } 439 } 440 441 class StringRenderer extends DefaultTableCellRenderer { 442 443 public Component getTableCellRendererComponent( 444 JTable table, 445 Object value, 446 boolean isSelected, 447 boolean hasFocus, 448 int row, 449 int column) { 450 451 super.getTableCellRendererComponent( 452 table, 453 value, 454 isSelected, 455 hasFocus, 456 row, 457 column); 458 459 ObjRelationshipTableModel model = (ObjRelationshipTableModel) table 460 .getModel(); 461 ObjRelationship relationship = model.getRelationship(row); 462 463 if (relationship != null 464 && relationship.getSourceEntity() != model.getEntity()) { 465 setForeground(Color.GRAY); 466 } 467 else { 468 setForeground(isSelected && !hasFocus 469 ? table.getSelectionForeground() 470 : table.getForeground()); 471 } 472 473 return this; 474 } 475 } 476 } | Popular Tags |