KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nightlabs > editor2d > actions > CloneAction


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.actions;
27
28 import java.awt.Rectangle JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31
32 import org.eclipse.gef.commands.Command;
33 import org.eclipse.gef.commands.CompoundCommand;
34 import org.nightlabs.config.Config;
35 import org.nightlabs.config.ConfigException;
36 import org.nightlabs.editor2d.AbstractEditor;
37 import org.nightlabs.editor2d.DrawComponent;
38 import org.nightlabs.editor2d.EditorPlugin;
39 import org.nightlabs.editor2d.command.CloneDrawComponentCommand;
40 import org.nightlabs.editor2d.command.CreateDrawComponentCommand;
41 import org.nightlabs.editor2d.config.QuickOptionsConfigModule;
42
43 /**
44  * <p> Author: Daniel.Mazurek[AT]NightLabs[DOT]de </p>
45  */

46 public class CloneAction
47 extends AbstractEditorSelectionAction
48 {
49     public static final String JavaDoc ID = CloneAction.class.getName();
50     
51     /**
52      * @param part
53      * @param style
54      */

55     public CloneAction(AbstractEditor part, int style) {
56         super(part, style);
57     }
58
59     /**
60      * @param part
61      */

62     public CloneAction(AbstractEditor part) {
63         super(part);
64     }
65
66   protected void init()
67   {
68     super.init();
69     setText(EditorPlugin.getResourceString("action.clone.text"));
70     setToolTipText(EditorPlugin.getResourceString("action.clone.tooltip"));
71     setId(ID);
72 // setImageDescriptor(ImageDescriptor.createFromFile(EditorPlugin.class,"icons/editShape16.gif"));
73
initConfigModule();
74   }
75     
76     protected QuickOptionsConfigModule confMod = null;
77     protected QuickOptionsConfigModule getConfigModule()
78     {
79         if (confMod == null)
80             initConfigModule();
81         
82         return confMod;
83     }
84     
85     protected void initConfigModule()
86     {
87         try {
88             confMod = (QuickOptionsConfigModule) Config.sharedInstance().createConfigModule(QuickOptionsConfigModule.class);
89         } catch (ConfigException e) {
90             throw new RuntimeException JavaDoc(e);
91         }
92     }
93   
94     /**
95     *@return true, if objects are selected, except the RootEditPart or LayerEditParts
96     */

97     protected boolean calculateEnabled() {
98         return !getDefaultSelection(false).isEmpty();
99     }
100
101 // /**
102
// * clones all selected DrawComponents and combines them
103
// *
104
// */
105
// public void run()
106
// {
107
// List dcs = getSelection(DrawComponent.class, true);
108
// CompoundCommand cmd = new CompoundCommand();
109
// for (Iterator it = dcs.iterator(); it.hasNext(); )
110
// {
111
// DrawComponent dc = (DrawComponent) it.next();
112
// CreateDrawComponentCommand createCmd = new CreateDrawComponentCommand();
113
// DrawComponent clone = (DrawComponent) dc.clone();
114
// clone.setName(clone.getName() + getCopyString());
115
// createCmd.setChild(clone);
116
// createCmd.setParent(dc.getParent());
117
//
118
// // TODO: translate clone depending on values in ConfigModule
119
// cmd.add(createCmd);
120
// }
121
// execute(cmd);
122
// }
123

124     /**
125      * clones all selected DrawComponents and combines them to one Command
126      *
127      */

128     public void run()
129     {
130         List JavaDoc dcs = getSelection(DrawComponent.class, true);
131         CompoundCommand cmd = new CompoundCommand();
132         for (Iterator JavaDoc it = dcs.iterator(); it.hasNext(); )
133         {
134             DrawComponent dc = (DrawComponent) it.next();
135             CloneDrawComponentCommand cloneCmd = new CloneDrawComponentCommand(dc, dc.getParent());
136             int distX = getConfigModule().getCloneDistanceX();
137             int distY = getConfigModule().getCloneDistanceY();
138             if (distX != 0 || distY != 0) {
139                 Rectangle JavaDoc dcBounds = new Rectangle JavaDoc(dc.getBounds());
140                 dcBounds.setLocation(dcBounds.x + distX, dcBounds.y + distY);
141                 cloneCmd.setCloneBounds(dcBounds);
142             }
143             cmd.add(cloneCmd);
144         }
145         execute(cmd);
146     }
147     
148 }
149
Popular Tags