KickJava   Java API By Example, From Geeks To Geeks.

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


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.apache.log4j.Logger;
31 import org.eclipse.draw2d.geometry.Rectangle;
32 import org.eclipse.gef.commands.Command;
33
34 import org.nightlabs.editor2d.DrawComponent;
35 import org.nightlabs.editor2d.DrawComponentContainer;
36 import org.nightlabs.editor2d.EditorPlugin;
37 import org.nightlabs.editor2d.util.J2DUtil;
38
39 public class CreateDrawComponentCommand
40 extends Command
41 {
42   public static final Logger LOGGER = Logger.getLogger(CreateDrawComponentCommand.class);
43   
44     /** The DrawComponent to add */
45     protected DrawComponent drawComponent;
46     /** DrawComponentContainer to add to. */
47     protected DrawComponentContainer parent;
48     /** True, if newDrawComponent was added to parent. */
49     protected boolean shapeAdded;
50     /** the DrawOrderIndex of the DrawComponentContainer */
51     protected int drawOrderIndex;
52     
53     protected Rectangle rect;
54     
55     /**
56      * Create a command that will add a new DrawComponent to a MultiLayerDrawComponent.
57      * @param parent the MultiLayerDrawComponent that will hold the new element
58      * @param req a request to create a new DrawComponent
59      * @throws IllegalArgumentException if any parameter is null, or the request
60      * does not provide a new DrawComponent instance
61      */

62     public CreateDrawComponentCommand()
63     {
64       super(EditorPlugin.getResourceString("command.create.drawcomponent"));
65     }
66     
67     public CreateDrawComponentCommand(String JavaDoc name)
68     {
69       super(name);
70     }
71     
72     /*
73      * @see org.eclipse.gef.commands.Command#canUndo()
74      */

75     public boolean canUndo() {
76         return shapeAdded;
77     }
78     
79     /*
80      * @see org.eclipse.gef.commands.Command#execute()
81      */

82     public void execute()
83     {
84     drawComponent.setBounds(J2DUtil.toAWTRectangle(rect));
85       
86     parent.addDrawComponent(drawComponent);
87     shapeAdded = true;
88         drawOrderIndex = parent.getDrawComponents().indexOf(drawComponent);
89     
90     if (drawComponent instanceof DrawComponentContainer) {
91       ((DrawComponentContainer)drawComponent).setParent(parent);
92     }
93     }
94         
95     /*
96      * @see org.eclipse.gef.commands.Command#redo()
97      */

98     public void redo() {
99     parent.addDrawComponent(drawComponent, drawOrderIndex);
100     }
101         
102     /*
103      * @see org.eclipse.gef.commands.Command#undo()
104      */

105     public void undo() {
106     parent.removeDrawComponent(drawComponent);
107     }
108     
109     public void setParent(DrawComponentContainer newParent) {
110         parent = newParent;
111     }
112     
113     public void setLocation(Rectangle r) {
114         rect = r;
115     }
116     
117     public void setChild(DrawComponent dc) {
118         drawComponent = dc;
119     }
120     
121     public DrawComponent getChild() {
122       return drawComponent;
123     }
124     
125     public void setIndex(int index) {
126       this.drawOrderIndex = index;
127     }
128 }
129
Popular Tags