1 56 package org.objectstyle.cayenne.modeler; 57 58 import java.awt.Component ; 59 60 import org.objectstyle.cayenne.access.DataDomain; 61 import org.objectstyle.cayenne.map.DataMap; 62 import org.objectstyle.cayenne.map.DbAttribute; 63 import org.objectstyle.cayenne.map.DbEntity; 64 import org.objectstyle.cayenne.map.DbRelationship; 65 import org.objectstyle.cayenne.map.ObjAttribute; 66 import org.objectstyle.cayenne.map.ObjEntity; 67 import org.objectstyle.cayenne.map.ObjRelationship; 68 import org.objectstyle.cayenne.modeler.action.CreateDataMapAction; 69 import org.objectstyle.cayenne.modeler.action.CreateDomainAction; 70 import org.objectstyle.cayenne.modeler.action.CreateNodeAction; 71 import org.objectstyle.cayenne.modeler.action.ImportDataMapAction; 72 import org.objectstyle.cayenne.modeler.action.ImportDBAction; 73 import org.objectstyle.cayenne.modeler.action.ImportEOModelAction; 74 import org.objectstyle.cayenne.modeler.action.ProjectAction; 75 import org.objectstyle.cayenne.modeler.action.RemoveAction; 76 import org.objectstyle.cayenne.modeler.action.RevertAction; 77 import org.objectstyle.cayenne.modeler.action.SaveAction; 78 import org.objectstyle.cayenne.modeler.action.SaveAsAction; 79 import org.objectstyle.cayenne.modeler.action.ValidateAction; 80 import org.objectstyle.cayenne.modeler.util.CayenneAction; 81 import org.objectstyle.cayenne.modeler.util.CayenneController; 82 import org.objectstyle.cayenne.project.ProjectPath; 83 84 87 public class ActionController extends CayenneController { 88 89 public ActionController(Application application) { 90 super(application); 91 } 92 93 public Component getView() { 94 throw new UnsupportedOperationException ("ActionController is 'headless'"); 95 } 96 97 public void domainSelected(DataDomain domain) { 98 enableDataDomainActions(domain); 99 updateRemoveAction(domain); 100 } 101 102 public void projectOpened() { 103 enableProjectActions(); 104 updateRemoveAction(null); 105 } 106 107 public void projectClosed() { 108 disableAllActions(); 109 getAction(ValidateAction.getActionName()).setEnabled(false); 110 getAction(ProjectAction.getActionName()).setEnabled(false); 111 getAction(SaveAction.getActionName()).setEnabled(false); 112 getAction(SaveAsAction.getActionName()).setEnabled(false); 113 getAction(RevertAction.getActionName()).setEnabled(false); 114 getAction(CreateDomainAction.getActionName()).setEnabled(false); 115 116 updateRemoveAction(null); 117 } 118 119 122 protected void updateRemoveAction(Object selected) { 123 String name = null; 124 125 if (selected == null) { 126 name = "Remove"; 127 } 128 else if (selected instanceof DataDomain) { 129 name = "Remove DataDomain"; 130 } 131 else if (selected instanceof DataMap) { 132 name = "Remove DataMap"; 133 } 134 else if (selected instanceof DbEntity) { 135 name = "Remove DbEntity"; 136 } 137 else if (selected instanceof ObjEntity) { 138 name = "Remove ObjEntity"; 139 } 140 else if (selected instanceof DbAttribute) { 141 name = "Remove DbAttribute"; 142 } 143 else if (selected instanceof ObjAttribute) { 144 name = "Remove ObjAttribute"; 145 } 146 else if (selected instanceof DbRelationship) { 147 name = "Remove DbRelationship"; 148 } 149 else if (selected instanceof ObjRelationship) { 150 name = "Remove ObjRelationship"; 151 } 152 else { 153 name = "Remove"; 154 } 155 156 getAction(RemoveAction.getActionName()).setName(name); 157 } 158 159 162 private CayenneAction getAction(String key) { 163 return application.getAction(key); 164 } 165 166 169 protected void disableAllActions() { 170 Object [] keys = application.getActionMap().allKeys(); 172 int len = keys.length; 173 for (int i = 0; i < len; i++) { 174 175 if (keys[i].equals(SaveAction.getActionName()) 177 || keys[i].equals(RevertAction.getActionName())) { 178 continue; 179 } 180 181 application.getActionMap().get(keys[i]).setEnabled(false); 182 } 183 } 184 185 188 protected void enableProjectActions() { 189 disableAllActions(); 190 getAction(CreateDomainAction.getActionName()).setEnabled(true); 191 getAction(ProjectAction.getActionName()).setEnabled(true); 192 getAction(ValidateAction.getActionName()).setEnabled(true); 193 getAction(SaveAsAction.getActionName()).setEnabled(true); 194 } 195 196 199 protected void updateActions(ProjectPath objectPath) { 200 Object [] keys = application.getActionMap().allKeys(); 201 int len = keys.length; 202 for (int i = 0; i < len; i++) { 203 CayenneAction action = getAction((String ) keys[i]); 204 action.setEnabled(action.enableForPath(objectPath)); 205 } 206 } 207 208 211 protected void enableDataDomainActions(DataDomain domain) { 212 enableProjectActions(); 213 214 if (domain != null) { 215 getAction(ImportDataMapAction.getActionName()).setEnabled(true); 216 getAction(CreateDataMapAction.getActionName()).setEnabled(true); 217 getAction(RemoveAction.getActionName()).setEnabled(true); 218 getAction(CreateNodeAction.getActionName()).setEnabled(true); 219 getAction(ImportDBAction.getActionName()).setEnabled(true); 220 getAction(ImportEOModelAction.getActionName()).setEnabled(true); 221 } 222 } 223 } | Popular Tags |