| 1 8 package com.nightlabs.editor2d.command; 9 10 import org.apache.log4j.Logger; 11 import org.eclipse.draw2d.geometry.Rectangle; 12 import org.eclipse.gef.commands.Command; 13 14 import com.nightlabs.editor2d.DrawComponent; 15 import com.nightlabs.editor2d.DrawComponentContainer; 16 import com.nightlabs.editor2d.EditorPlugin; 17 import com.nightlabs.editor2d.util.J2DUtil; 18 19 public class CreateDrawComponentCommand 20 extends Command 21 { 22 public static final Logger LOGGER = Logger.getLogger(CreateDrawComponentCommand.class); 23 24 25 protected DrawComponent drawComponent; 26 27 protected DrawComponentContainer parent; 28 29 protected boolean shapeAdded; 30 31 protected int drawOrderIndex; 32 33 protected Rectangle rect; 34 35 42 public CreateDrawComponentCommand() 43 { 44 super(EditorPlugin.getResourceString("command_create_drawcomponent")); 45 } 46 47 public CreateDrawComponentCommand(String name) 48 { 49 super(name); 50 } 51 52 55 public boolean canUndo() { 56 return shapeAdded; 57 } 58 59 62 public void execute() 63 { 64 drawComponent.setBounds(J2DUtil.toAWTRectangle(rect)); 65 66 parent.addDrawComponent(drawComponent); 67 shapeAdded = true; 68 drawOrderIndex = parent.getDrawComponents().indexOf(drawComponent); 69 70 if (drawComponent instanceof DrawComponentContainer) { 71 ((DrawComponentContainer)drawComponent).setParent(parent); 72 } 73 } 74 75 78 public void redo() 79 { 80 parent.addDrawComponent(drawComponent, drawOrderIndex); 81 } 82 83 86 public void undo() 87 { 88 parent.removeDrawComponent(drawComponent); 89 } 90 91 public void setParent(DrawComponentContainer newParent) { 92 parent = newParent; 93 } 94 95 public void setLocation(Rectangle r) { 96 rect = r; 97 } 98 99 public void setChild(DrawComponent dc) { 100 drawComponent = dc; 101 } 102 103 public DrawComponent getChild() { 104 return drawComponent; 105 } 106 107 public void setIndex(int index) { 108 this.drawOrderIndex = index; 109 } 110 111 } 116 | Popular Tags |