KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * <p> Project: com.nightlabs.editor2d </p>
3  * <p> Copyright: Copyright (c) 2004 </p>
4  * <p> Company: NightLabs GmbH (Germany) </p>
5  * <p> Creation Date: 26.10.2004 </p>
6  * <p> Author: Daniel Mazurek </p>
7 **/

8 package com.nightlabs.editor2d.command;
9
10 import org.apache.log4j.Logger;
11 import org.eclipse.draw2d.geometry.Rectangle;
12 import org.eclipse.gef.commands.Command;
13
14 import com.nightlabs.editor2d.DrawComponent;
15 import com.nightlabs.editor2d.DrawComponentContainer;
16 import com.nightlabs.editor2d.EditorPlugin;
17 import com.nightlabs.editor2d.util.J2DUtil;
18
19 public class CreateDrawComponentCommand
20 extends Command
21 {
22   public static final Logger LOGGER = Logger.getLogger(CreateDrawComponentCommand.class);
23   
24     /** The DrawComponent to add */
25     protected DrawComponent drawComponent;
26     /** DrawComponentContainer to add to. */
27     protected DrawComponentContainer parent;
28     /** True, if newDrawComponent was added to parent. */
29     protected boolean shapeAdded;
30     /** the DrawOrderIndex of the DrawComponentContainer */
31     protected int drawOrderIndex;
32     
33     protected Rectangle rect;
34     
35     /**
36      * Create a command that will add a new DrawComponent to a MultiLayerDrawComponent.
37      * @param parent the MultiLayerDrawComponent that will hold the new element
38      * @param req a request to create a new DrawComponent
39      * @throws IllegalArgumentException if any parameter is null, or the request
40      * does not provide a new DrawComponent instance
41      */

42     public CreateDrawComponentCommand()
43     {
44       super(EditorPlugin.getResourceString("command_create_drawcomponent"));
45     }
46     
47     public CreateDrawComponentCommand(String JavaDoc name)
48     {
49       super(name);
50     }
51     
52     /* (non-Javadoc)
53      * @see org.eclipse.gef.commands.Command#canUndo()
54      */

55     public boolean canUndo() {
56         return shapeAdded;
57     }
58     
59     /* (non-Javadoc)
60      * @see org.eclipse.gef.commands.Command#execute()
61      */

62     public void execute()
63     {
64     drawComponent.setBounds(J2DUtil.toAWTRectangle(rect));
65       
66     parent.addDrawComponent(drawComponent);
67     shapeAdded = true;
68         drawOrderIndex = parent.getDrawComponents().indexOf(drawComponent);
69     
70     if (drawComponent instanceof DrawComponentContainer) {
71       ((DrawComponentContainer)drawComponent).setParent(parent);
72     }
73     }
74         
75     /* (non-Javadoc)
76      * @see org.eclipse.gef.commands.Command#redo()
77      */

78     public void redo()
79     {
80     parent.addDrawComponent(drawComponent, drawOrderIndex);
81     }
82         
83     /* (non-Javadoc)
84      * @see org.eclipse.gef.commands.Command#undo()
85      */

86     public void undo()
87     {
88     parent.removeDrawComponent(drawComponent);
89     }
90     
91     public void setParent(DrawComponentContainer newParent) {
92         parent = newParent;
93     }
94     
95     public void setLocation(Rectangle r) {
96         rect = r;
97     }
98     
99     public void setChild(DrawComponent dc) {
100         drawComponent = dc;
101     }
102     
103     public DrawComponent getChild() {
104       return drawComponent;
105     }
106     
107     public void setIndex(int index) {
108       this.drawOrderIndex = index;
109     }
110   
111 // protected boolean ignoreSize = false;
112
// public void setIgnoreSize(boolean ignoreSize) {
113
// this.ignoreSize = ignoreSize;
114
// }
115
}
116
Popular Tags