1 56 package org.objectstyle.cayenne.modeler.action; 57 58 import java.awt.event.ActionEvent ; 59 60 import org.objectstyle.cayenne.map.DbAttribute; 61 import org.objectstyle.cayenne.map.DbEntity; 62 import org.objectstyle.cayenne.map.DerivedDbAttribute; 63 import org.objectstyle.cayenne.map.DerivedDbEntity; 64 import org.objectstyle.cayenne.map.Entity; 65 import org.objectstyle.cayenne.map.ObjAttribute; 66 import org.objectstyle.cayenne.map.ObjEntity; 67 import org.objectstyle.cayenne.map.event.AttributeEvent; 68 import org.objectstyle.cayenne.modeler.Application; 69 import org.objectstyle.cayenne.modeler.ProjectController; 70 import org.objectstyle.cayenne.modeler.event.AttributeDisplayEvent; 71 import org.objectstyle.cayenne.modeler.util.CayenneAction; 72 import org.objectstyle.cayenne.project.NamedObjectFactory; 73 import org.objectstyle.cayenne.project.ProjectPath; 74 75 78 public class CreateAttributeAction extends CayenneAction { 79 80 public static String getActionName() { 81 return "Create Attribute"; 82 } 83 84 88 public CreateAttributeAction(Application application) { 89 super(getActionName(), application); 90 } 91 92 public String getIconName() { 93 return "icon-attribute.gif"; 94 } 95 96 99 public void performAction(ActionEvent e) { 100 if (getProjectController().getCurrentObjEntity() != null) { 101 createObjAttribute(); 102 } 103 else if (getProjectController().getCurrentDbEntity() != null) { 104 createDbAttribute(); 105 } 106 } 107 108 public void createObjAttribute() { 109 ProjectController mediator = getProjectController(); 110 ObjEntity objEntity = mediator.getCurrentObjEntity(); 111 112 ObjAttribute attr = (ObjAttribute) NamedObjectFactory.createObject( 113 ObjAttribute.class, 114 objEntity); 115 objEntity.addAttribute(attr); 116 117 mediator.fireObjAttributeEvent(new AttributeEvent( 118 this, 119 attr, 120 objEntity, 121 AttributeEvent.ADD)); 122 123 AttributeDisplayEvent ade = new AttributeDisplayEvent( 124 this, 125 attr, 126 objEntity, 127 mediator.getCurrentDataMap(), 128 mediator.getCurrentDataDomain()); 129 130 mediator.fireObjAttributeDisplayEvent(ade); 131 } 132 133 public void createDbAttribute() { 134 Class attrClass = null; 135 136 DbEntity dbEntity = getProjectController().getCurrentDbEntity(); 137 if (dbEntity instanceof DerivedDbEntity) { 138 if (((DerivedDbEntity) dbEntity).getParentEntity() == null) { 139 return; 140 } 141 attrClass = DerivedDbAttribute.class; 142 } 143 else { 144 attrClass = DbAttribute.class; 145 } 146 147 DbAttribute attr = 148 (DbAttribute) NamedObjectFactory.createObject(attrClass, dbEntity); 149 dbEntity.addAttribute(attr); 150 151 ProjectController mediator = getProjectController(); 152 mediator.fireDbAttributeEvent( 153 new AttributeEvent(this, attr, dbEntity, AttributeEvent.ADD)); 154 155 AttributeDisplayEvent ade = new AttributeDisplayEvent( 156 this, 157 attr, 158 dbEntity, 159 mediator.getCurrentDataMap(), 160 mediator.getCurrentDataDomain()); 161 162 mediator.fireDbAttributeDisplayEvent(ade); 163 } 164 165 168 public boolean enableForPath(ProjectPath path) { 169 if (path == null) { 170 return false; 171 } 172 173 return path.firstInstanceOf(Entity.class) != null; 174 } 175 } 176 | Popular Tags |