KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nightlabs > editor2d > command > DeleteDrawComponentCommand


1 /* *****************************************************************************
2  * NightLabs Editor2D - Graphical editor framework *
3  * Copyright (C) 2004-2005 NightLabs - http://NightLabs.org *
4  * Project author: Daniel Mazurek <Daniel.Mazurek [at] nightlabs [dot] org> *
5  * *
6  * This library is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU Lesser General Public *
8  * License as published by the Free Software Foundation; either *
9  * version 2.1 of the License, or (at your option) any later version. *
10  * *
11  * This library is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14  * Lesser General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Lesser General Public *
17  * License along with this library; if not, write to the *
18  * Free Software Foundation, Inc., *
19  * 51 Franklin St, Fifth Floor, *
20  * Boston, MA 02110-1301 USA *
21  * *
22  * Or get it online : *
23  * http://www.gnu.org/copyleft/lesser.html *
24  * *
25  * *
26  ******************************************************************************/

27
28 package org.nightlabs.editor2d.command;
29
30 import org.eclipse.gef.commands.Command;
31
32 import org.nightlabs.editor2d.DrawComponent;
33 import org.nightlabs.editor2d.DrawComponentContainer;
34 import org.nightlabs.editor2d.EditorPlugin;
35
36 public class DeleteDrawComponentCommand
37 extends Command
38 {
39     /** The DrawComponent to delete */
40     protected DrawComponent child;
41     
42     /** MultiLayerDrawComponent to removed from. */
43     protected final DrawComponentContainer parent;
44   
45     /** the DrawComponentsIndex of the DrawComponentContainer */
46     protected int index;
47     
48     /** True, if child was removed from its parent. */
49     protected boolean wasRemoved;
50
51     /** the Deletion String */
52     public static final String JavaDoc DELETE_DRAWCOMPONENT = EditorPlugin.getResourceString("command.delete.drawcomponent");
53   
54     /**
55      * Create a command that will remove the shape from its parent.
56      * @param parent the ShapesDiagram containing the child
57      * @param child the Shape to remove
58      * @throws IllegalArgumentException if any parameter is null
59      */

60     public DeleteDrawComponentCommand(DrawComponentContainer parent, DrawComponent child)
61     {
62         if (parent == null || child == null) {
63             throw new IllegalArgumentException JavaDoc("Neither param parent not param child may be null!");
64         }
65         setLabel(DELETE_DRAWCOMPONENT);
66         this.parent = parent;
67         this.child = child;
68     }
69     
70     /* (non-Javadoc)
71      * @see org.eclipse.gef.commands.Command#canUndo()
72      */

73     public boolean canUndo() {
74         return wasRemoved;
75     }
76     
77     /* (non-Javadoc)
78      * @see org.eclipse.gef.commands.Command#execute()
79      */

80     public void execute()
81     {
82       index = parent.getDrawComponents().indexOf(child);
83       if (index == -1)
84         throw new IllegalStateException JavaDoc("DrawComponent "+child.getId()+" is not contained in DrawComponentContainer "+parent.getId());
85     parent.removeDrawComponent(child);
86     wasRemoved = true;
87     }
88     
89     /* (non-Javadoc)
90      * @see org.eclipse.gef.commands.Command#redo()
91      */

92     public void redo()
93     {
94     parent.removeDrawComponent(index);
95     }
96     
97     /* (non-Javadoc)
98      * @see org.eclipse.gef.commands.Command#undo()
99      */

100     public void undo()
101     {
102     parent.addDrawComponent(child, index);
103     }
104     
105 }
106
Popular Tags