KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > editor2d > command > DeleteLayerCommand


1 /**
2  * <p> Project: com.nightlabs.editor2d </p>
3  * <p> Copyright: Copyright (c) 2004 </p>
4  * <p> Company: NightLabs GmbH (Germany) </p>
5  * <p> Creation Date: 16.11.2004 </p>
6  * <p> Author: Daniel Mazurek </p>
7 **/

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     /** The DrawComponent to delete */
21     private Layer child;
22     /** MultiLayerDrawComponent to removed from. */
23     private final MultiLayerDrawComponent parent;
24     /** True, if child was removed from its parent. */
25     private boolean wasRemoved;
26     /** the LayerIndex of the MultiLayerDrawComponent */
27     private int layerIndex;
28     /** the Deletion String */
29     public static final String JavaDoc DELETE_LAYER = EditorPlugin.getResourceString("command_delete_layer");
30     /**
31      * Create a command that will remove the shape from its parent.
32      * @param parent the ShapesDiagram containing the child
33      * @param child the Shape to remove
34      * @throws IllegalArgumentException if any parameter is null
35      */

36     public DeleteLayerCommand(MultiLayerDrawComponent parent, Layer child)
37     {
38         if (parent == null || child == null) {
39             throw new IllegalArgumentException JavaDoc();
40         }
41         setLabel(DELETE_LAYER);
42         this.parent = parent;
43         this.child = child;
44     }
45     
46     /* (non-Javadoc)
47      * @see org.eclipse.gef.commands.Command#canUndo()
48      */

49     public boolean canUndo() {
50         return wasRemoved;
51     }
52     
53     /* (non-Javadoc)
54      * @see org.eclipse.gef.commands.Command#execute()
55      */

56     public void execute()
57     {
58 // layerIndex = parent.getDrawComponents().indexOf(child);
59
// wasRemoved = parent.getDrawComponents().remove(child);
60
layerIndex = parent.getDrawComponents().indexOf(child);
61         wasRemoved = parent.getDrawComponents().remove(child);
62         setCurrentLayer();
63     }
64     
65     /* (non-Javadoc)
66      * @see org.eclipse.gef.commands.Command#redo()
67      */

68     public void redo()
69     {
70       parent.getDrawComponents().remove(child);
71       setCurrentLayer();
72     }
73     
74     /* (non-Javadoc)
75      * @see org.eclipse.gef.commands.Command#undo()
76      */

77     public void undo()
78     {
79       parent.getDrawComponents().add(layerIndex, child);
80       parent.setCurrentLayer(child);
81       
82 // Layer l = (Layer) parent.getDrawComponents().get(layerIndex);
83
// l.getDrawComponents().add(layerIndex, child);
84
// setCurrentLayer();
85
}
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