1 56 package org.objectstyle.cayenne.modeler.dialog; 57 58 import java.awt.BorderLayout ; 59 import java.awt.Dimension ; 60 import java.awt.event.ActionEvent ; 61 import java.awt.event.ActionListener ; 62 import java.util.ArrayList ; 63 import java.util.Collection ; 64 import java.util.Collections ; 65 import java.util.Iterator ; 66 import java.util.List ; 67 68 import javax.swing.DefaultCellEditor ; 69 import javax.swing.JButton ; 70 import javax.swing.JComboBox ; 71 import javax.swing.JOptionPane ; 72 import javax.swing.JScrollPane ; 73 import javax.swing.JTextField ; 74 import javax.swing.ListSelectionModel ; 75 import javax.swing.table.TableColumn ; 76 77 import org.objectstyle.cayenne.CayenneRuntimeException; 78 import org.objectstyle.cayenne.map.DbEntity; 79 import org.objectstyle.cayenne.map.DbJoin; 80 import org.objectstyle.cayenne.map.DbRelationship; 81 import org.objectstyle.cayenne.map.Entity; 82 import org.objectstyle.cayenne.map.Relationship; 83 import org.objectstyle.cayenne.map.event.RelationshipEvent; 84 import org.objectstyle.cayenne.modeler.Application; 85 import org.objectstyle.cayenne.modeler.util.CayenneDialog; 86 import org.objectstyle.cayenne.modeler.util.CayenneTable; 87 import org.objectstyle.cayenne.modeler.util.CayenneWidgetFactory; 88 import org.objectstyle.cayenne.modeler.util.ModelerUtil; 89 import org.objectstyle.cayenne.modeler.util.PanelFactory; 90 import org.objectstyle.cayenne.modeler.util.ProjectUtil; 91 import org.objectstyle.cayenne.project.NamedObjectFactory; 92 import org.objectstyle.cayenne.util.Util; 93 94 import com.jgoodies.forms.builder.PanelBuilder; 95 import com.jgoodies.forms.layout.CellConstraints; 96 import com.jgoodies.forms.layout.FormLayout; 97 98 101 public class ResolveDbRelationshipDialog extends CayenneDialog { 102 103 protected DbRelationship relationship; 104 protected DbRelationship reverseRelationship; 105 106 protected JTextField name; 107 protected JTextField reverseName; 108 protected CayenneTable table; 109 protected JButton addButton; 110 protected JButton removeButton; 111 protected JButton saveButton; 112 protected JButton cancelButton; 113 114 private boolean cancelPressed; 115 116 public ResolveDbRelationshipDialog(DbRelationship relationship) { 117 118 super(Application.getFrame(), "", true); 119 120 initView(); 121 initController(); 122 initWithModel(relationship); 123 124 this.pack(); 125 this.centerWindow(); 126 } 127 128 131 private void initView() { 132 133 name = new JTextField (25); 135 reverseName = new JTextField (25); 136 addButton = new JButton ("Add"); 137 removeButton = new JButton ("Remove"); 138 saveButton = new JButton ("Done"); 139 cancelButton = new JButton ("Cancel"); 140 141 table = new AttributeTable(); 142 table.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 143 144 getContentPane().setLayout(new BorderLayout ()); 146 147 CellConstraints cc = new CellConstraints(); 148 PanelBuilder builder = new PanelBuilder( 149 new FormLayout( 150 "right:max(50dlu;pref), 3dlu, fill:min(150dlu;pref), 3dlu, fill:min(150dlu;pref)", 151 "p, 3dlu, p, 3dlu, p, 9dlu, p, 3dlu, top:14dlu, 3dlu, top:p:grow")); 152 builder.setDefaultDialogBorder(); 153 154 builder.addSeparator("DbRelationship Information", cc.xywh(1, 1, 5, 1)); 155 builder.addLabel("Relationship:", cc.xy(1, 3)); 156 builder.add(name, cc.xywh(3, 3, 1, 1)); 157 builder.addLabel("Reverse Relationship", cc.xy(1, 5)); 158 builder.add(reverseName, cc.xywh(3, 5, 1, 1)); 159 160 builder.addSeparator("Joins", cc.xywh(1, 7, 5, 1)); 161 builder.add(new JScrollPane (table), cc.xywh(1, 9, 3, 3, "fill, fill")); 162 builder.add(addButton, cc.xywh(5, 9, 1, 1)); 163 builder.add(removeButton, cc.xywh(5, 11, 1, 1)); 164 165 getContentPane().add(builder.getPanel(), BorderLayout.CENTER); 166 getContentPane().add(PanelFactory.createButtonPanel(new JButton [] { 167 saveButton, cancelButton 168 }), BorderLayout.SOUTH); 169 } 170 171 private void initWithModel(DbRelationship relationship) { 172 173 if (relationship.getSourceEntity() == null) { 175 throw new CayenneRuntimeException("Null source entity: " + relationship); 176 } 177 178 if (relationship.getTargetEntity() == null) { 179 throw new CayenneRuntimeException("Null target entity: " + relationship); 180 } 181 182 if (relationship.getSourceEntity().getDataMap() == null) { 183 throw new CayenneRuntimeException("Null DataMap: " 184 + relationship.getSourceEntity()); 185 } 186 187 this.relationship = relationship; 188 this.reverseRelationship = relationship.getReverseRelationship(); 189 190 setTitle("DbRelationship Info: " 192 + relationship.getSourceEntity().getName() 193 + " to " 194 + relationship.getTargetEntityName()); 195 196 table.setModel(new DbJoinTableModel(relationship, getMediator(), this, true)); 197 TableColumn sourceColumn = table.getColumnModel().getColumn( 198 DbJoinTableModel.SOURCE); 199 sourceColumn.setMinWidth(150); 200 JComboBox comboBox = CayenneWidgetFactory.createComboBox(ModelerUtil 201 .getDbAttributeNames(getMediator(), (DbEntity) relationship 202 .getSourceEntity()), true); 203 comboBox.setEditable(false); 204 sourceColumn.setCellEditor(new DefaultCellEditor (comboBox)); 205 206 TableColumn targetColumn = table.getColumnModel().getColumn( 207 DbJoinTableModel.TARGET); 208 targetColumn.setMinWidth(150); 209 comboBox = CayenneWidgetFactory.createComboBox(ModelerUtil.getDbAttributeNames( 210 getMediator(), 211 (DbEntity) relationship.getTargetEntity()), true); 212 comboBox.setEditable(false); 213 targetColumn.setCellEditor(new DefaultCellEditor (comboBox)); 214 215 if (reverseRelationship != null) { 216 reverseName.setText(reverseRelationship.getName()); 217 } 218 219 name.setText(relationship.getName()); 220 } 221 222 private void initController() { 223 addButton.addActionListener(new ActionListener () { 224 225 public void actionPerformed(ActionEvent e) { 226 DbJoinTableModel model = (DbJoinTableModel) table.getModel(); 227 model.addRow(new DbJoin(relationship)); 228 table.select(model.getRowCount() - 1); 229 } 230 }); 231 232 removeButton.addActionListener(new ActionListener () { 233 234 public void actionPerformed(ActionEvent e) { 235 DbJoinTableModel model = (DbJoinTableModel) table.getModel(); 236 stopEditing(); 237 int row = table.getSelectedRow(); 238 model.removeRow(model.getJoin(row)); 239 } 240 }); 241 242 saveButton.addActionListener(new ActionListener () { 243 244 public void actionPerformed(ActionEvent e) { 245 cancelPressed = false; 246 save(); 247 } 248 }); 249 250 cancelButton.addActionListener(new ActionListener () { 251 252 public void actionPerformed(ActionEvent e) { 253 cancelPressed = true; 254 setVisible(false); 255 } 256 }); 257 } 258 259 public boolean isCancelPressed() { 260 return cancelPressed; 261 } 262 263 private void stopEditing() { 264 int col_index = table.getEditingColumn(); 266 if (col_index >= 0) { 267 TableColumn col = table.getColumnModel().getColumn(col_index); 268 col.getCellEditor().stopCellEditing(); 269 } 270 } 271 272 private void save() { 273 274 String name = this.name.getText(); 276 if (name.length() == 0) { 277 name = null; 278 } 279 280 if (name == null) { 281 name = NamedObjectFactory.createName(DbRelationship.class, relationship 282 .getSourceEntity()); 283 } 284 285 if (!validateName(relationship.getSourceEntity(), relationship, name)) { 286 return; 287 } 288 289 String reverseName = this.reverseName.getText().trim(); 290 if (reverseName.length() == 0) { 291 reverseName = null; 292 } 293 294 if (reverseName == null) { 295 reverseName = NamedObjectFactory.createName( 296 DbRelationship.class, 297 relationship.getTargetEntity()); 298 } 299 300 DbJoinTableModel model = (DbJoinTableModel) table.getModel(); 302 boolean updatingReverse = model.getObjectList().size() > 0; 303 304 if (updatingReverse 305 && !validateName( 306 relationship.getTargetEntity(), 307 reverseRelationship, 308 reverseName)) { 309 return; 310 } 311 312 if (!Util.nullSafeEquals(name, relationship.getName())) { 314 String oldName = relationship.getName(); 315 ProjectUtil.setRelationshipName( 316 relationship.getSourceEntity(), 317 relationship, 318 name); 319 320 getMediator().fireDbRelationshipEvent( 321 new RelationshipEvent(this, relationship, relationship 322 .getSourceEntity(), oldName)); 323 } 324 325 model.commit(); 326 327 if (relationship.isToDependentPK() && !relationship.isValidForDepPk()) { 330 relationship.setToDependentPK(false); 331 } 332 333 if (updatingReverse) { 336 337 if (reverseRelationship == null) { 339 reverseRelationship = new DbRelationship(reverseName); 340 reverseRelationship.setSourceEntity(relationship.getTargetEntity()); 341 reverseRelationship.setTargetEntity(relationship.getSourceEntity()); 342 reverseRelationship.setToMany(!relationship.isToMany()); 343 relationship.getTargetEntity().addRelationship(reverseRelationship); 344 345 if (relationship.getSourceEntity() == relationship.getTargetEntity()) { 348 getMediator().fireDbRelationshipEvent( 349 new RelationshipEvent( 350 this, 351 reverseRelationship, 352 reverseRelationship.getSourceEntity(), 353 RelationshipEvent.ADD)); 354 } 355 } 356 else if (!Util.nullSafeEquals(reverseName, reverseRelationship.getName())) { 357 String oldName = reverseRelationship.getName(); 358 ProjectUtil.setRelationshipName( 359 reverseRelationship.getSourceEntity(), 360 reverseRelationship, 361 reverseName); 362 363 getMediator().fireDbRelationshipEvent( 364 new RelationshipEvent( 365 this, 366 reverseRelationship, 367 reverseRelationship.getSourceEntity(), 368 oldName)); 369 } 370 371 Collection reverseJoins = getReverseJoins(); 372 reverseRelationship.setJoins(reverseJoins); 373 374 if (!relationship.isToDependentPK()) { 376 Iterator it = reverseJoins.iterator(); 377 if (it.hasNext()) { 378 boolean toDepPK = true; 379 while (it.hasNext()) { 380 DbJoin join = (DbJoin) it.next(); 381 if (!join.getTarget().isPrimaryKey()) { 382 toDepPK = false; 383 break; 384 } 385 } 386 387 reverseRelationship.setToDependentPK(toDepPK); 388 } 389 } 390 391 } 392 393 getMediator() 394 .fireDbRelationshipEvent( 395 new RelationshipEvent(this, relationship, relationship 396 .getSourceEntity())); 397 dispose(); 398 } 399 400 private boolean validateName(Entity entity, Relationship relationship, String newName) { 401 402 Relationship existing = entity.getRelationship(newName); 403 if (existing != null && (relationship == null || relationship != existing)) { 404 JOptionPane.showMessageDialog( 405 this, 406 "There is an existing relationship named \"" 407 + newName 408 + "\". Select a different name."); 409 return false; 410 } 411 412 return true; 413 } 414 415 private Collection getReverseJoins() { 416 Collection joins = relationship.getJoins(); 417 418 if ((joins == null) || (joins.size() == 0)) { 419 return Collections.EMPTY_LIST; 420 } 421 422 List reverseJoins = new ArrayList (joins.size()); 423 424 Iterator it = joins.iterator(); 427 while (it.hasNext()) { 428 DbJoin pair = (DbJoin) it.next(); 429 DbJoin reverseJoin = pair.createReverseJoin(); 430 431 reverseJoin.setRelationship(reverseRelationship); 434 reverseJoins.add(reverseJoin); 435 } 436 437 return reverseJoins; 438 } 439 440 final class AttributeTable extends CayenneTable { 441 442 final Dimension preferredSize = new Dimension (203, 100); 443 444 public Dimension getPreferredScrollableViewportSize() { 445 return preferredSize; 446 } 447 } 448 } | Popular Tags |