1 56 package org.objectstyle.cayenne.modeler.editor; 57 58 import java.awt.BorderLayout ; 59 import java.awt.Component ; 60 import java.awt.event.ActionEvent ; 61 import java.awt.event.ActionListener ; 62 import java.util.EventObject ; 63 64 import javax.swing.ComboBoxModel ; 65 import javax.swing.DefaultCellEditor ; 66 import javax.swing.DefaultComboBoxModel ; 67 import javax.swing.JButton ; 68 import javax.swing.JComboBox ; 69 import javax.swing.JPanel ; 70 import javax.swing.JTable ; 71 import javax.swing.JToolBar ; 72 import javax.swing.event.ChangeEvent ; 73 import javax.swing.event.ListSelectionEvent ; 74 import javax.swing.event.ListSelectionListener ; 75 import javax.swing.event.TableModelEvent ; 76 import javax.swing.event.TableModelListener ; 77 import javax.swing.table.DefaultTableCellRenderer ; 78 import javax.swing.table.TableColumn ; 79 80 import org.objectstyle.cayenne.map.DataMap; 81 import org.objectstyle.cayenne.map.DbEntity; 82 import org.objectstyle.cayenne.map.DbRelationship; 83 import org.objectstyle.cayenne.map.Entity; 84 import org.objectstyle.cayenne.map.MapObject; 85 import org.objectstyle.cayenne.map.event.DbEntityListener; 86 import org.objectstyle.cayenne.map.event.DbRelationshipListener; 87 import org.objectstyle.cayenne.map.event.EntityEvent; 88 import org.objectstyle.cayenne.map.event.RelationshipEvent; 89 import org.objectstyle.cayenne.modeler.Application; 90 import org.objectstyle.cayenne.modeler.ProjectController; 91 import org.objectstyle.cayenne.modeler.action.CreateRelationshipAction; 92 import org.objectstyle.cayenne.modeler.action.DbEntitySyncAction; 93 import org.objectstyle.cayenne.modeler.action.RemoveRelationshipAction; 94 import org.objectstyle.cayenne.modeler.dialog.ResolveDbRelationshipDialog; 95 import org.objectstyle.cayenne.modeler.event.DbEntityDisplayListener; 96 import org.objectstyle.cayenne.modeler.event.EntityDisplayEvent; 97 import org.objectstyle.cayenne.modeler.event.RelationshipDisplayEvent; 98 import org.objectstyle.cayenne.modeler.util.CayenneTable; 99 import org.objectstyle.cayenne.modeler.util.CayenneWidgetFactory; 100 import org.objectstyle.cayenne.modeler.util.CellRenderers; 101 import org.objectstyle.cayenne.modeler.util.ModelerUtil; 102 import org.objectstyle.cayenne.modeler.util.PanelFactory; 103 import org.objectstyle.cayenne.modeler.util.UIUtil; 104 105 111 public class DbEntityRelationshipTab extends JPanel implements ActionListener , 112 DbEntityDisplayListener, DbEntityListener, DbRelationshipListener, 113 ExistingSelectionProcessor, ListSelectionListener , TableModelListener { 114 115 protected ProjectController mediator; 116 protected CayenneTable table; 117 protected JButton resolve; 118 119 public DbEntityRelationshipTab(ProjectController mediator) { 120 super(); 121 122 this.mediator = mediator; 123 this.mediator.addDbEntityDisplayListener(this); 124 this.mediator.addDbEntityListener(this); 125 this.mediator.addDbRelationshipListener(this); 126 127 init(); 128 resolve.addActionListener(this); 129 } 130 131 protected void init() { 132 this.setLayout(new BorderLayout ()); 133 134 JToolBar toolBar = new JToolBar (); 135 Application app = Application.getInstance(); 136 toolBar.add(app.getAction(CreateRelationshipAction.getActionName()).buildButton()); 137 toolBar.add(app.getAction(DbEntitySyncAction.getActionName()).buildButton()); 138 139 toolBar.addSeparator(); 140 141 resolve = new JButton (); 142 resolve.setIcon(ModelerUtil.buildIcon("icon-info.gif")); 143 resolve.setToolTipText("Database Mapping"); 144 toolBar.add(resolve); 145 146 toolBar.addSeparator(); 147 148 toolBar.add(app.getAction(RemoveRelationshipAction.getActionName()).buildButton()); 149 add(toolBar, BorderLayout.NORTH); 150 151 table = new CayenneTable(); 152 table.setDefaultRenderer(DbEntity.class, new EntityRenderer()); 153 154 add(PanelFactory.createTablePanel(table, null), BorderLayout.CENTER); 155 } 156 157 public void actionPerformed(ActionEvent e) { 158 Object src = e.getSource(); 159 if (src == resolve) { 160 resolveRelationship(); 161 } 162 } 163 164 public void valueChanged(ListSelectionEvent e) { 165 processExistingSelection(e); 166 } 167 168 public void tableChanged(TableModelEvent e) { 169 DbRelationship rel = null; 170 if (table.getSelectedRow() >= 0) { 171 DbRelationshipTableModel model = (DbRelationshipTableModel) table.getModel(); 172 rel = model.getRelationship(table.getSelectedRow()); 173 resolve.setEnabled(rel.getTargetEntity() != null); 174 } 175 } 176 177 180 public void selectRelationship(DbRelationship rel) { 181 if (rel == null) { 182 Application.getInstance().getAction(RemoveRelationshipAction.getActionName()).setEnabled(false); 183 return; 184 } 185 Application.getInstance().getAction(RemoveRelationshipAction.getActionName()).setEnabled(true); 187 188 DbRelationshipTableModel model = (DbRelationshipTableModel) table.getModel(); 189 java.util.List rels = model.getObjectList(); 190 int relPos = rels.indexOf(rel); 191 if (relPos >= 0) { 192 table.select(relPos); 193 } 194 } 195 196 public void processExistingSelection(EventObject e) { 197 if (e instanceof ChangeEvent ) { 198 table.clearSelection(); 199 } 200 DbRelationship rel = null; 201 if (table.getSelectedRow() >= 0) { 202 DbRelationshipTableModel model; 203 model = (DbRelationshipTableModel) table.getModel(); 204 rel = model.getRelationship(table.getSelectedRow()); 205 resolve.setEnabled(rel.getTargetEntity() != null); 206 207 UIUtil.scrollToSelectedRow(table); 209 } 210 else 211 resolve.setEnabled(false); 212 213 RelationshipDisplayEvent ev = new RelationshipDisplayEvent(this, rel, mediator 214 .getCurrentDbEntity(), mediator.getCurrentDataMap(), mediator 215 .getCurrentDataDomain()); 216 217 mediator.fireDbRelationshipDisplayEvent(ev); 218 } 219 220 private void resolveRelationship() { 221 int row = table.getSelectedRow(); 222 if (row < 0) { 223 return; 224 } 225 226 DbRelationshipTableModel model = (DbRelationshipTableModel) table.getModel(); 228 DbRelationship rel = model.getRelationship(row); 229 ResolveDbRelationshipDialog dialog = new ResolveDbRelationshipDialog(rel); 230 dialog.setVisible(true); 231 dialog.dispose(); 232 } 233 234 235 public void currentDbEntityChanged(EntityDisplayEvent e) { 236 DbEntity entity = (DbEntity) e.getEntity(); 237 if (entity != null && e.isEntityChanged()) { 238 rebuildTable(entity); 239 } 240 241 if (e.isUnselectAttributes()) { 244 table.clearSelection(); 245 } 246 } 247 248 protected void rebuildTable(DbEntity entity) { 249 DbRelationshipTableModel model = new DbRelationshipTableModel( 250 entity, 251 mediator, 252 this); 253 model.addTableModelListener(this); 254 table.setModel(model); 255 table.setRowHeight(25); 256 table.setRowMargin(3); 257 TableColumn col = table.getColumnModel().getColumn(DbRelationshipTableModel.NAME); 258 col.setMinWidth(150); 259 col = table.getColumnModel().getColumn(DbRelationshipTableModel.TARGET); 260 col.setMinWidth(150); 261 262 JComboBox combo = CayenneWidgetFactory.createComboBox(); 263 combo.setRenderer(CellRenderers.entityListRendererWithIcons(entity.getDataMap())); 264 combo.setModel(createComboModel()); 265 combo.setEditable(false); 266 col.setCellEditor(new DefaultCellEditor (combo)); 267 table.getSelectionModel().addListSelectionListener(this); 268 } 269 270 public void dbEntityChanged(EntityEvent e) { 271 } 272 273 public void dbEntityAdded(EntityEvent e) { 274 reloadEntityList(e); 275 } 276 277 public void dbEntityRemoved(EntityEvent e) { 278 reloadEntityList(e); 279 } 280 281 public void dbRelationshipChanged(RelationshipEvent e) { 282 if (e.getSource() != this) { 283 if (!(table.getModel() instanceof DbRelationshipTableModel)) { 284 rebuildTable((DbEntity) e.getEntity()); 285 } 286 287 table.select(e.getRelationship()); 288 DbRelationshipTableModel model = (DbRelationshipTableModel) table.getModel(); 289 model.fireTableDataChanged(); 290 } 291 } 292 293 public void dbRelationshipAdded(RelationshipEvent e) { 294 rebuildTable((DbEntity) e.getEntity()); 295 table.select(e.getRelationship()); 296 } 297 298 public void dbRelationshipRemoved(RelationshipEvent e) { 299 DbRelationshipTableModel model = (DbRelationshipTableModel) table.getModel(); 300 int ind = model.getObjectList().indexOf(e.getRelationship()); 301 model.removeRelationship(e.getRelationship()); 302 table.select(ind); 303 } 304 305 309 private void reloadEntityList(EntityEvent e) { 310 if (e.getSource() == this) 311 return; 312 if (mediator.getCurrentDbEntity() == e.getEntity()) 314 return; 315 if (mediator.getCurrentDbEntity() == null) 317 return; 318 TableColumn col = table.getColumnModel().getColumn( 319 DbRelationshipTableModel.TARGET); 320 DefaultCellEditor editor = (DefaultCellEditor ) col.getCellEditor(); 321 JComboBox combo = (JComboBox ) editor.getComponent(); 322 combo.setModel(createComboModel()); 323 324 DbRelationshipTableModel model = (DbRelationshipTableModel) table.getModel(); 325 model.fireTableDataChanged(); 326 table.getSelectionModel().addListSelectionListener(this); 327 } 328 329 332 private ComboBoxModel createComboModel() { 333 DataMap map = mediator.getCurrentDataMap(); 334 Object [] objects = map.getNamespace().getDbEntities().toArray(); 335 return new DefaultComboBoxModel (objects); 336 } 337 338 class EntityRenderer extends DefaultTableCellRenderer { 339 340 public Component getTableCellRendererComponent( 341 JTable table, 342 Object value, 343 boolean isSelected, 344 boolean hasFocus, 345 int row, 346 int column) { 347 348 if (value instanceof MapObject) { 349 MapObject mapObject = (MapObject) value; 350 String label = mapObject.getName(); 351 352 if (mapObject instanceof Entity) { 353 Entity entity = (Entity) mapObject; 354 355 DataMap dataMap = entity.getDataMap(); 357 if (dataMap != null && dataMap != mediator.getCurrentDataMap()) { 358 label += " (" + dataMap.getName() + ")"; 359 } 360 } 361 362 value = label; 363 } 364 365 super.getTableCellRendererComponent( 366 table, 367 value, 368 isSelected, 369 hasFocus, 370 row, 371 column); 372 373 return this; 374 } 375 } 376 377 } | Popular Tags |