KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > modeler > action > RemoveAction


1 /* ====================================================================
2  *
3  * The ObjectStyle Group Software License, version 1.1
4  * ObjectStyle Group - http://objectstyle.org/
5  *
6  * Copyright (c) 2002-2005, Andrei (Andrus) Adamchik and individual authors
7  * of the software. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if any,
22  * must include the following acknowlegement:
23  * "This product includes software developed by independent contributors
24  * and hosted on ObjectStyle Group web site (http://objectstyle.org/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The names "ObjectStyle Group" and "Cayenne" must not be used to endorse
29  * or promote products derived from this software without prior written
30  * permission. For written permission, email
31  * "andrus at objectstyle dot org".
32  *
33  * 5. Products derived from this software may not be called "ObjectStyle"
34  * or "Cayenne", nor may "ObjectStyle" or "Cayenne" appear in their
35  * names without prior written permission.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE OBJECTSTYLE GROUP OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals and hosted on ObjectStyle Group web site. For more
53  * information on the ObjectStyle Group, please see
54  * <http://objectstyle.org/>.
55  */

56
57 package org.objectstyle.cayenne.modeler.action;
58
59 import java.awt.event.ActionEvent JavaDoc;
60 import java.awt.event.KeyEvent JavaDoc;
61
62 import javax.swing.KeyStroke JavaDoc;
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 /**
97  * Removes currently selected object from the project. This can be Domain, DataNode,
98  * Entity, Attribute or Relationship.
99  */

100 public class RemoveAction extends CayenneAction {
101
102     public static String JavaDoc getActionName() {
103         return "Remove";
104     }
105
106     public RemoveAction(Application application) {
107         super(getActionName(), application);
108     }
109
110     protected RemoveAction(String JavaDoc actionName, Application application){
111         super(actionName, application);
112     }
113
114     public String JavaDoc getIconName() {
115         return "icon-trash.gif";
116     }
117
118     public KeyStroke JavaDoc getAcceleratorKey() {
119         return KeyStroke.getKeyStroke(KeyEvent.VK_D, ActionEvent.CTRL_MASK);
120     }
121
122     public void performAction(ActionEvent JavaDoc 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             // In context of Data node just remove from Data Node
158
if (mediator.getCurrentDataNode() != null) {
159                 removeDataMapFromDataNode();
160             }
161             else {
162                 // Not under Data Node, remove completely
163
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     /**
208      * Removes current DbEntity from its DataMap and fires "remove" EntityEvent.
209      */

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     /**
222      * Removes current Query from its DataMap and fires "remove" QueryEvent.
223      */

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     /**
236      * Removes current Procedure from its DataMap and fires "remove" ProcedureEvent.
237      */

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     /**
250      * Removes current object entity from its DataMap.
251      */

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         // Force reloading of the data node in the browse view
341
mediator.fireDataNodeEvent(new DataNodeEvent(Application.getFrame(), node));
342     }
343
344     /**
345      * Returns <code>true</code> if last object in the path contains a removable object.
346      */

347     public boolean enableForPath(ProjectPath path) {
348         if (path == null) {
349             return false;
350         }
351
352         Object JavaDoc 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