1 27 28 package org.nightlabs.editor2d.command; 29 30 import org.eclipse.gef.commands.Command; 31 32 import org.nightlabs.editor2d.DrawComponent; 33 import org.nightlabs.editor2d.DrawComponentContainer; 34 import org.nightlabs.editor2d.EditorPlugin; 35 36 public class DeleteDrawComponentCommand 37 extends Command 38 { 39 40 protected DrawComponent child; 41 42 43 protected final DrawComponentContainer parent; 44 45 46 protected int index; 47 48 49 protected boolean wasRemoved; 50 51 52 public static final String DELETE_DRAWCOMPONENT = EditorPlugin.getResourceString("command.delete.drawcomponent"); 53 54 60 public DeleteDrawComponentCommand(DrawComponentContainer parent, DrawComponent child) 61 { 62 if (parent == null || child == null) { 63 throw new IllegalArgumentException ("Neither param parent not param child may be null!"); 64 } 65 setLabel(DELETE_DRAWCOMPONENT); 66 this.parent = parent; 67 this.child = child; 68 } 69 70 73 public boolean canUndo() { 74 return wasRemoved; 75 } 76 77 80 public void execute() 81 { 82 index = parent.getDrawComponents().indexOf(child); 83 if (index == -1) 84 throw new IllegalStateException ("DrawComponent "+child.getId()+" is not contained in DrawComponentContainer "+parent.getId()); 85 parent.removeDrawComponent(child); 86 wasRemoved = true; 87 } 88 89 92 public void redo() 93 { 94 parent.removeDrawComponent(index); 95 } 96 97 100 public void undo() 101 { 102 parent.addDrawComponent(child, index); 103 } 104 105 } 106 | Popular Tags |