1 56 package org.objectstyle.cayenne.modeler.action; 57 58 import java.awt.event.ActionEvent ; 59 60 import org.objectstyle.cayenne.map.DbEntity; 61 import org.objectstyle.cayenne.map.DbRelationship; 62 import org.objectstyle.cayenne.map.Entity; 63 import org.objectstyle.cayenne.map.ObjEntity; 64 import org.objectstyle.cayenne.map.ObjRelationship; 65 import org.objectstyle.cayenne.map.event.RelationshipEvent; 66 import org.objectstyle.cayenne.modeler.Application; 67 import org.objectstyle.cayenne.modeler.ProjectController; 68 import org.objectstyle.cayenne.modeler.event.RelationshipDisplayEvent; 69 import org.objectstyle.cayenne.modeler.util.CayenneAction; 70 import org.objectstyle.cayenne.project.NamedObjectFactory; 71 import org.objectstyle.cayenne.project.ProjectPath; 72 73 76 public class CreateRelationshipAction extends CayenneAction { 77 78 public static String getActionName() { 79 return "Create Relationship"; 80 } 81 82 85 public CreateRelationshipAction(Application application) { 86 super(getActionName(), application); 87 } 88 89 public String getIconName() { 90 return "icon-relationship.gif"; 91 } 92 93 96 public void performAction(ActionEvent e) { 97 ObjEntity objEnt = getProjectController().getCurrentObjEntity(); 98 if (objEnt != null) { 99 createObjRelationship(objEnt); 100 } else { 101 DbEntity dbEnt = getProjectController().getCurrentDbEntity(); 102 if (dbEnt != null) { 103 createDbRelationship(dbEnt); 104 } 105 } 106 } 107 108 public void createObjRelationship(ObjEntity objEnt) { 109 ProjectController mediator = getProjectController(); 110 111 ObjRelationship rel = 112 (ObjRelationship) NamedObjectFactory.createObject( 113 ObjRelationship.class, 114 objEnt); 115 rel.setSourceEntity(objEnt); 116 objEnt.addRelationship(rel); 117 118 mediator.fireObjRelationshipEvent( 119 new RelationshipEvent(this, rel, objEnt, RelationshipEvent.ADD)); 120 121 RelationshipDisplayEvent rde = new RelationshipDisplayEvent( 122 this, 123 rel, 124 objEnt, 125 mediator.getCurrentDataMap(), 126 mediator.getCurrentDataDomain()); 127 128 mediator.fireObjRelationshipDisplayEvent(rde); 129 } 130 131 public void createDbRelationship(DbEntity dbEnt) { 132 ProjectController mediator = getProjectController(); 133 134 DbRelationship rel = 135 (DbRelationship) NamedObjectFactory.createObject(DbRelationship.class, dbEnt); 136 137 rel.setSourceEntity(dbEnt); 138 dbEnt.addRelationship(rel); 139 140 mediator.fireDbRelationshipEvent( 141 new RelationshipEvent(this, rel, dbEnt, RelationshipEvent.ADD)); 142 143 RelationshipDisplayEvent rde = new RelationshipDisplayEvent( 144 this, 145 rel, 146 dbEnt, 147 mediator.getCurrentDataMap(), 148 mediator.getCurrentDataDomain()); 149 150 mediator.fireDbRelationshipDisplayEvent(rde); 151 } 152 153 156 public boolean enableForPath(ProjectPath path) { 157 if (path == null) { 158 return false; 159 } 160 161 return path.firstInstanceOf(Entity.class) != null; 162 } 163 } 164 | Popular Tags |