1 26 package org.nightlabs.editor2d.command; 27 28 import java.util.Collection ; 29 import java.util.HashMap ; 30 import java.util.Iterator ; 31 import java.util.Map ; 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 41 public class CutDrawComponentCommand 42 extends Command 43 { 44 45 public CutDrawComponentCommand(Collection <DrawComponent> dcs) 46 { 47 super(); 48 setLabel(EditorPlugin.getResourceString("command.cut.text")); 49 this.dcs = dcs; 50 } 51 52 protected Collection <DrawComponent> dcs = null; 53 public Collection <DrawComponent> getDrawComponents() { 54 return dcs; 55 } 56 57 protected Map <DrawComponent, Integer > dc2Index = new HashMap <DrawComponent, Integer >(); 58 59 public void execute() 60 { 61 Clipboard clipboard = Clipboard.getDefault(); 62 clipboard.setContents(dcs); 63 for (Iterator <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 (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 <DrawComponent> it = dcs.iterator(); it.hasNext(); ) 80 { 81 DrawComponent dc = it.next(); 82 Integer dcIndex = dc2Index.get(dc); 83 if (dcIndex != null) { 84 dc.getParent().addDrawComponent(dc, dcIndex.intValue()); 85 } 86 } 87 } 88 89 } 90 | Popular Tags |