1 8 package com.nightlabs.editor2d.command; 9 10 import org.eclipse.gef.commands.Command; 11 12 import com.nightlabs.editor2d.EditorPlugin; 13 import com.nightlabs.editor2d.Layer; 14 import com.nightlabs.editor2d.MultiLayerDrawComponent; 15 16 17 public class DeleteLayerCommand 18 extends Command 19 { 20 21 private Layer child; 22 23 private final MultiLayerDrawComponent parent; 24 25 private boolean wasRemoved; 26 27 private int layerIndex; 28 29 public static final String DELETE_LAYER = EditorPlugin.getResourceString("command_delete_layer"); 30 36 public DeleteLayerCommand(MultiLayerDrawComponent parent, Layer child) 37 { 38 if (parent == null || child == null) { 39 throw new IllegalArgumentException (); 40 } 41 setLabel(DELETE_LAYER); 42 this.parent = parent; 43 this.child = child; 44 } 45 46 49 public boolean canUndo() { 50 return wasRemoved; 51 } 52 53 56 public void execute() 57 { 58 layerIndex = parent.getDrawComponents().indexOf(child); 61 wasRemoved = parent.getDrawComponents().remove(child); 62 setCurrentLayer(); 63 } 64 65 68 public void redo() 69 { 70 parent.getDrawComponents().remove(child); 71 setCurrentLayer(); 72 } 73 74 77 public void undo() 78 { 79 parent.getDrawComponents().add(layerIndex, child); 80 parent.setCurrentLayer(child); 81 82 } 86 87 protected void setCurrentLayer() 88 { 89 if (layerIndex != 0) { 90 parent.setCurrentLayer((Layer) parent.getDrawComponents().get(layerIndex-1)); 91 } else if ( layerIndex==0 && parent.getDrawComponents().size() > 2) { 92 parent.setCurrentLayer((Layer) parent.getDrawComponents().get(layerIndex+1)); 93 } 94 } 95 96 public MultiLayerDrawComponent getParent() { 97 return parent; 98 } 99 100 } 101 | Popular Tags |