KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Collection JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.Map JavaDoc;
32
33 import org.eclipse.gef.commands.Command;
34 import org.eclipse.gef.ui.actions.Clipboard;
35 import org.nightlabs.editor2d.DrawComponent;
36 import org.nightlabs.editor2d.EditorPlugin;
37
38 /**
39  * <p> Author: Daniel.Mazurek[AT]NightLabs[DOT]de </p>
40  */

41 public class CutDrawComponentCommand
42 extends Command
43 {
44
45     public CutDrawComponentCommand(Collection JavaDoc<DrawComponent> dcs)
46     {
47         super();
48         setLabel(EditorPlugin.getResourceString("command.cut.text"));
49         this.dcs = dcs;
50     }
51
52     protected Collection JavaDoc<DrawComponent> dcs = null;
53     public Collection JavaDoc<DrawComponent> getDrawComponents() {
54         return dcs;
55     }
56     
57     protected Map JavaDoc<DrawComponent, Integer JavaDoc> dc2Index = new HashMap JavaDoc<DrawComponent, Integer JavaDoc>();
58     
59     public void execute()
60     {
61         Clipboard clipboard = Clipboard.getDefault();
62         clipboard.setContents(dcs);
63         for (Iterator JavaDoc<DrawComponent> it = dcs.iterator(); it.hasNext(); )
64         {
65             DrawComponent dc = it.next();
66             int index = dc.getParent().getDrawComponents().indexOf(dc);
67             dc2Index.put(dc, new Integer JavaDoc(index));
68             dc.getParent().removeDrawComponent(index);
69         }
70     }
71
72     public void redo()
73     {
74         execute();
75     }
76
77     public void undo()
78     {
79         for (Iterator JavaDoc<DrawComponent> it = dcs.iterator(); it.hasNext(); )
80         {
81             DrawComponent dc = it.next();
82             Integer JavaDoc dcIndex = dc2Index.get(dc);
83             if (dcIndex != null) {
84                 dc.getParent().addDrawComponent(dc, dcIndex.intValue());
85             }
86         }
87     }
88     
89 }
90
Popular Tags