1 56 package org.objectstyle.cayenne.modeler.action; 57 58 import java.awt.event.ActionEvent ; 59 60 import org.objectstyle.cayenne.map.DataMap; 61 import org.objectstyle.cayenne.map.DbEntity; 62 import org.objectstyle.cayenne.map.ObjEntity; 63 import org.objectstyle.cayenne.map.event.EntityEvent; 64 import org.objectstyle.cayenne.modeler.Application; 65 import org.objectstyle.cayenne.modeler.ProjectController; 66 import org.objectstyle.cayenne.modeler.event.EntityDisplayEvent; 67 import org.objectstyle.cayenne.modeler.util.CayenneAction; 68 import org.objectstyle.cayenne.project.NamedObjectFactory; 69 import org.objectstyle.cayenne.project.ProjectPath; 70 import org.objectstyle.cayenne.util.EntityMergeSupport; 71 import org.objectstyle.cayenne.util.NameConverter; 72 73 76 public class CreateObjEntityAction extends CayenneAction { 77 78 public static String getActionName() { 79 return "Create ObjEntity"; 80 } 81 82 85 public CreateObjEntityAction(Application application) { 86 super(getActionName(), application); 87 } 88 89 public String getIconName() { 90 return "icon-new_objentity.gif"; 91 } 92 93 96 public void performAction(ActionEvent e) { 97 createObjEntity(); 98 } 99 100 protected void createObjEntity() { 101 ProjectController mediator = getProjectController(); 102 103 DataMap dataMap = mediator.getCurrentDataMap(); 104 ObjEntity entity = (ObjEntity) NamedObjectFactory.createObject( 105 ObjEntity.class, 106 mediator.getCurrentDataMap()); 107 108 entity.setSuperClassName(dataMap.getDefaultSuperclass()); 110 entity.setDeclaredLockType(dataMap.getDefaultLockType()); 111 112 DbEntity dbEntity = mediator.getCurrentDbEntity(); 113 if (dbEntity != null){ 114 entity.setDbEntity(dbEntity); 115 String eName = NameConverter.undescoredToJava(dbEntity.getName(), true); 116 String entityName = eName; 117 int i = 1; 118 while(dataMap.getObjEntity(entityName) != null){ 119 entityName = eName + String.valueOf(i); 120 } 121 entity.setName(entityName); 122 } 123 124 String pkg = dataMap.getDefaultPackage(); 125 if (pkg != null) { 126 if (!pkg.endsWith(".")) { 127 pkg = pkg + "."; 128 } 129 130 entity.setClassName(pkg + entity.getName()); 131 } 132 133 dataMap.addObjEntity(entity); 134 135 EntityMergeSupport merger = new EntityMergeSupport(dataMap); 137 merger.synchronizeWithDbEntity(entity); 138 139 mediator.fireObjEntityEvent(new EntityEvent(this, entity, EntityEvent.ADD)); 140 EntityDisplayEvent displayEvent = new EntityDisplayEvent( 141 this, 142 entity, 143 dataMap, 144 mediator.getCurrentDataNode(), 145 mediator.getCurrentDataDomain()); 146 mediator.fireObjEntityDisplayEvent(displayEvent); 147 } 148 149 152 public boolean enableForPath(ProjectPath path) { 153 if (path == null) { 154 return false; 155 } 156 157 return path.firstInstanceOf(DataMap.class) != null; 158 } 159 } | Popular Tags |