KickJava   Java API By Example, From Geeks To Geeks.

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


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

26 package org.nightlabs.editor2d.command;
27
28 import java.awt.Rectangle JavaDoc;
29
30 import org.eclipse.gef.commands.Command;
31 import org.nightlabs.editor2d.DrawComponent;
32 import org.nightlabs.editor2d.DrawComponentContainer;
33 import org.nightlabs.editor2d.EditorPlugin;
34
35 /**
36  * <p> Author: Daniel.Mazurek[AT]NightLabs[DOT]de </p>
37  */

38 public class CloneDrawComponentCommand
39 extends Command
40 {
41
42     /**
43      *
44      * @param source the DrawComponent to clone
45      * @param parent the DrawComponentContainer to which the clone should be added
46      */

47     public CloneDrawComponentCommand(DrawComponent source, DrawComponentContainer parent)
48     {
49         super();
50         setLabel(EditorPlugin.getResourceString("command.clone.text"));
51         this.drawComponent = source;
52         this.parent = parent;
53     }
54
55     protected DrawComponent drawComponent = null;
56     public void setDrawComponent(DrawComponent dc) {
57         this.drawComponent = dc;
58     }
59     public DrawComponent getDrawComponent() {
60         return drawComponent;
61     }
62     
63     protected DrawComponentContainer parent = null;
64     public DrawComponentContainer getParent() {
65         return parent;
66     }
67     public void setParent(DrawComponentContainer parent) {
68         this.parent = parent;
69     }
70     
71     protected String JavaDoc cloneName = null;
72     /**
73      *
74      * @return the name of the clone
75      */

76     public String JavaDoc getCloneName()
77     {
78         if (cloneName == null) {
79             if (drawComponent != null) {
80                 cloneName = drawComponent.getName() + " " + getCopyString();
81             } else {
82                 cloneName = getCopyString();
83             }
84         }
85         return cloneName;
86     }
87     /**
88      *
89      * @param cloneName sets the name of the cloned DrawComponent
90      */

91     public void setCloneName(String JavaDoc cloneName) {
92         this.cloneName = cloneName;
93     }
94     
95     protected static final Rectangle JavaDoc DEFAULT_CLONE_BOUNDS = new Rectangle JavaDoc(0, 0, 10, 10);
96     
97     protected Rectangle JavaDoc cloneBounds = null;
98     public Rectangle JavaDoc getCloneBounds()
99     {
100         if (cloneBounds == null)
101         {
102             if (drawComponent != null) {
103                 cloneBounds = new Rectangle JavaDoc(drawComponent.getBounds());
104             }
105             else {
106                 cloneBounds = DEFAULT_CLONE_BOUNDS;
107             }
108         }
109         return cloneBounds;
110     }
111     public void setCloneBounds(Rectangle JavaDoc cloneBounds) {
112         this.cloneBounds = cloneBounds;
113     }
114     
115     protected DrawComponent clone = null;
116     
117 // public void execute()
118
// {
119
// clone = (DrawComponent) drawComponent.clone();
120
// clone.setName(getCloneName());
121
// clone.setBounds(getCloneBounds());
122
// if (!parent.equals(clone.getParent())) {
123
// clone.getParent().removeDrawComponent(clone);
124
// parent.addDrawComponent(clone);
125
// }
126
// }
127

128     public void execute()
129     {
130         clone = (DrawComponent) drawComponent.clone(getParent());
131         clone.setName(getCloneName());
132         clone.setBounds(getCloneBounds());
133     }
134         
135     public void redo()
136     {
137         parent.addDrawComponent(clone);
138     }
139     
140     public void undo()
141     {
142         parent.removeDrawComponent(clone);
143     }
144             
145     protected String JavaDoc getCopyString()
146     {
147         return "("+EditorPlugin.getResourceString("action.clone.text")+")";
148     }
149
150 }
151
Popular Tags