1 56 57 package org.objectstyle.cayenne.modeler.action; 58 59 import java.awt.event.ActionEvent ; 60 import java.awt.event.KeyEvent ; 61 62 import javax.swing.KeyStroke ; 63 64 import org.objectstyle.cayenne.access.DataDomain; 65 import org.objectstyle.cayenne.access.DataNode; 66 import org.objectstyle.cayenne.map.Attribute; 67 import org.objectstyle.cayenne.map.DataMap; 68 import org.objectstyle.cayenne.map.DbAttribute; 69 import org.objectstyle.cayenne.map.DbEntity; 70 import org.objectstyle.cayenne.map.DbRelationship; 71 import org.objectstyle.cayenne.map.Entity; 72 import org.objectstyle.cayenne.map.ObjAttribute; 73 import org.objectstyle.cayenne.map.ObjEntity; 74 import org.objectstyle.cayenne.map.ObjRelationship; 75 import org.objectstyle.cayenne.map.Procedure; 76 import org.objectstyle.cayenne.map.ProcedureParameter; 77 import org.objectstyle.cayenne.map.Relationship; 78 import org.objectstyle.cayenne.map.event.AttributeEvent; 79 import org.objectstyle.cayenne.map.event.DataMapEvent; 80 import org.objectstyle.cayenne.map.event.DataNodeEvent; 81 import org.objectstyle.cayenne.map.event.DomainEvent; 82 import org.objectstyle.cayenne.map.event.EntityEvent; 83 import org.objectstyle.cayenne.map.event.MapEvent; 84 import org.objectstyle.cayenne.map.event.ProcedureEvent; 85 import org.objectstyle.cayenne.map.event.ProcedureParameterEvent; 86 import org.objectstyle.cayenne.map.event.QueryEvent; 87 import org.objectstyle.cayenne.map.event.RelationshipEvent; 88 import org.objectstyle.cayenne.modeler.Application; 89 import org.objectstyle.cayenne.modeler.ProjectController; 90 import org.objectstyle.cayenne.modeler.util.CayenneAction; 91 import org.objectstyle.cayenne.modeler.util.ProjectUtil; 92 import org.objectstyle.cayenne.project.ApplicationProject; 93 import org.objectstyle.cayenne.project.ProjectPath; 94 import org.objectstyle.cayenne.query.Query; 95 96 100 public class RemoveAction extends CayenneAction { 101 102 public static String getActionName() { 103 return "Remove"; 104 } 105 106 public RemoveAction(Application application) { 107 super(getActionName(), application); 108 } 109 110 protected RemoveAction(String actionName, Application application){ 111 super(actionName, application); 112 } 113 114 public String getIconName() { 115 return "icon-trash.gif"; 116 } 117 118 public KeyStroke getAcceleratorKey() { 119 return KeyStroke.getKeyStroke(KeyEvent.VK_D, ActionEvent.CTRL_MASK); 120 } 121 122 public void performAction(ActionEvent e) { 123 remove(); 124 } 125 126 private void remove() { 127 ProjectController mediator = getProjectController(); 128 129 if (mediator.getCurrentObjAttribute() != null) { 130 removeObjAttribute(); 131 } 132 else if (mediator.getCurrentDbAttribute() != null) { 133 removeDbAttribute(); 134 } 135 else if (mediator.getCurrentObjRelationship() != null) { 136 removeObjRelationship(); 137 } 138 else if (mediator.getCurrentDbRelationship() != null) { 139 removeDbRelationship(); 140 } 141 else if (mediator.getCurrentProcedureParameter() != null) { 142 removeProcedureParameter(); 143 } 144 else if (mediator.getCurrentObjEntity() != null) { 145 removeObjEntity(); 146 } 147 else if (mediator.getCurrentDbEntity() != null) { 148 removeDbEntity(); 149 } 150 else if (mediator.getCurrentQuery() != null) { 151 removeQuery(); 152 } 153 else if (mediator.getCurrentProcedure() != null) { 154 removeProcedure(); 155 } 156 else if (mediator.getCurrentDataMap() != null) { 157 if (mediator.getCurrentDataNode() != null) { 159 removeDataMapFromDataNode(); 160 } 161 else { 162 removeDataMap(); 164 } 165 } 166 else if (mediator.getCurrentDataNode() != null) { 167 removeDataNode(); 168 } 169 else if (mediator.getCurrentDataDomain() != null) { 170 removeDomain(); 171 } 172 } 173 174 protected void removeDomain() { 175 ApplicationProject project = (ApplicationProject) getCurrentProject(); 176 ProjectController mediator = getProjectController(); 177 DataDomain domain = mediator.getCurrentDataDomain(); 178 project.getConfiguration().removeDomain(domain.getName()); 179 mediator.fireDomainEvent(new DomainEvent( 180 Application.getFrame(), 181 domain, 182 MapEvent.REMOVE)); 183 } 184 185 protected void removeDataMap() { 186 ProjectController mediator = getProjectController(); 187 DataMap map = mediator.getCurrentDataMap(); 188 DataDomain domain = mediator.getCurrentDataDomain(); 189 domain.removeMap(map.getName()); 190 mediator.fireDataMapEvent(new DataMapEvent( 191 Application.getFrame(), 192 map, 193 MapEvent.REMOVE)); 194 } 195 196 protected void removeDataNode() { 197 ProjectController mediator = getProjectController(); 198 DataNode node = mediator.getCurrentDataNode(); 199 DataDomain domain = mediator.getCurrentDataDomain(); 200 domain.removeDataNode(node.getName()); 201 mediator.fireDataNodeEvent(new DataNodeEvent( 202 Application.getFrame(), 203 node, 204 MapEvent.REMOVE)); 205 } 206 207 210 protected void removeDbEntity() { 211 ProjectController mediator = getProjectController(); 212 DbEntity ent = mediator.getCurrentDbEntity(); 213 DataMap map = mediator.getCurrentDataMap(); 214 map.removeDbEntity(ent.getName(), true); 215 mediator.fireDbEntityEvent(new EntityEvent( 216 Application.getFrame(), 217 ent, 218 MapEvent.REMOVE)); 219 } 220 221 224 protected void removeQuery() { 225 ProjectController mediator = getProjectController(); 226 Query query = mediator.getCurrentQuery(); 227 DataMap map = mediator.getCurrentDataMap(); 228 map.removeQuery(query.getName()); 229 mediator.fireQueryEvent(new QueryEvent( 230 Application.getFrame(), 231 query, 232 MapEvent.REMOVE)); 233 } 234 235 238 protected void removeProcedure() { 239 ProjectController mediator = getProjectController(); 240 Procedure procedure = mediator.getCurrentProcedure(); 241 DataMap map = mediator.getCurrentDataMap(); 242 map.removeProcedure(procedure.getName()); 243 mediator.fireProcedureEvent(new ProcedureEvent( 244 Application.getFrame(), 245 procedure, 246 MapEvent.REMOVE)); 247 } 248 249 252 protected void removeObjEntity() { 253 ProjectController mediator = getProjectController(); 254 ObjEntity ent = mediator.getCurrentObjEntity(); 255 256 DataMap map = mediator.getCurrentDataMap(); 257 map.removeObjEntity(ent.getName(), true); 258 mediator.fireObjEntityEvent(new EntityEvent( 259 Application.getFrame(), 260 ent, 261 MapEvent.REMOVE)); 262 } 263 264 protected void removeProcedureParameter() { 265 ProjectController mediator = getProjectController(); 266 ProcedureParameter parameter = mediator.getCurrentProcedureParameter(); 267 mediator.getCurrentProcedure().removeCallParameter(parameter.getName()); 268 269 ProcedureParameterEvent e = new ProcedureParameterEvent( 270 Application.getFrame(), 271 parameter, 272 MapEvent.REMOVE); 273 274 mediator.fireProcedureParameterEvent(e); 275 } 276 277 protected void removeObjAttribute() { 278 ProjectController mediator = getProjectController(); 279 ObjEntity entity = mediator.getCurrentObjEntity(); 280 ObjAttribute attrib = mediator.getCurrentObjAttribute(); 281 282 entity.removeAttribute(attrib.getName()); 283 AttributeEvent e = new AttributeEvent( 284 Application.getFrame(), 285 attrib, 286 entity, 287 MapEvent.REMOVE); 288 mediator.fireObjAttributeEvent(e); 289 } 290 291 protected void removeDbAttribute() { 292 ProjectController mediator = getProjectController(); 293 DbEntity entity = mediator.getCurrentDbEntity(); 294 DbAttribute attrib = mediator.getCurrentDbAttribute(); 295 entity.removeAttribute(attrib.getName()); 296 ProjectUtil.cleanObjMappings(mediator.getCurrentDataMap()); 297 298 AttributeEvent e = new AttributeEvent( 299 Application.getFrame(), 300 attrib, 301 entity, 302 MapEvent.REMOVE); 303 mediator.fireDbAttributeEvent(e); 304 } 305 306 protected void removeObjRelationship() { 307 ProjectController mediator = getProjectController(); 308 ObjEntity entity = mediator.getCurrentObjEntity(); 309 ObjRelationship rel = mediator.getCurrentObjRelationship(); 310 entity.removeRelationship(rel.getName()); 311 RelationshipEvent e = new RelationshipEvent( 312 Application.getFrame(), 313 rel, 314 entity, 315 RelationshipEvent.REMOVE); 316 mediator.fireObjRelationshipEvent(e); 317 } 318 319 protected void removeDbRelationship() { 320 ProjectController mediator = getProjectController(); 321 DbEntity entity = mediator.getCurrentDbEntity(); 322 DbRelationship rel = mediator.getCurrentDbRelationship(); 323 entity.removeRelationship(rel.getName()); 324 ProjectUtil.cleanObjMappings(mediator.getCurrentDataMap()); 325 326 RelationshipEvent e = new RelationshipEvent( 327 Application.getFrame(), 328 rel, 329 entity, 330 RelationshipEvent.REMOVE); 331 mediator.fireDbRelationshipEvent(e); 332 } 333 334 protected void removeDataMapFromDataNode() { 335 ProjectController mediator = getProjectController(); 336 DataNode node = mediator.getCurrentDataNode(); 337 DataMap map = mediator.getCurrentDataMap(); 338 node.removeDataMap(map.getName()); 339 340 mediator.fireDataNodeEvent(new DataNodeEvent(Application.getFrame(), node)); 342 } 343 344 347 public boolean enableForPath(ProjectPath path) { 348 if (path == null) { 349 return false; 350 } 351 352 Object lastObject = path.getObject(); 353 354 if (lastObject instanceof DataDomain) { 355 return true; 356 } 357 else if (lastObject instanceof DataMap) { 358 return true; 359 } 360 else if (lastObject instanceof DataNode) { 361 return true; 362 } 363 else if (lastObject instanceof Entity) { 364 return true; 365 } 366 else if (lastObject instanceof Attribute) { 367 return true; 368 } 369 else if (lastObject instanceof Relationship) { 370 return true; 371 } 372 else if (lastObject instanceof Procedure) { 373 return true; 374 } 375 else if (lastObject instanceof ProcedureParameter) { 376 return true; 377 } 378 else { 379 return false; 380 } 381 } 382 } | Popular Tags |