KickJava   Java API By Example, From Geeks To Geeks.

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


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: 12.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.DrawComponent;
13 import com.nightlabs.editor2d.EditorGuide;
14
15 public class ChangeGuideCommand
16 extends Command
17 {
18   private DrawComponent part;
19   private EditorGuide oldGuide, newGuide;
20   private int oldAlign, newAlign;
21   private boolean horizontal;
22
23   public ChangeGuideCommand(DrawComponent part, boolean horizontalGuide) {
24     super();
25     this.part = part;
26     horizontal = horizontalGuide;
27   }
28
29   protected void changeGuide(EditorGuide oldGuide, EditorGuide newGuide, int newAlignment) {
30     if (oldGuide != null && oldGuide != newGuide) {
31         oldGuide.detachPart(part);
32     }
33     // You need to re-attach the part even if the oldGuide and the newGuide are the same
34
// because the alignment could have changed
35
if (newGuide != null) {
36         newGuide.attachPart(part, newAlignment);
37     }
38   }
39
40   public void execute() {
41     // Cache the old values
42
oldGuide = horizontal ? part.getHorizontalGuide() : part.getVerticalGuide();
43     if (oldGuide != null)
44         oldAlign = oldGuide.getAlignment(part);
45     
46     redo();
47   }
48
49   public void redo() {
50     changeGuide(oldGuide, newGuide, newAlign);
51   }
52
53   public void setNewGuide(EditorGuide guide, int alignment) {
54     newGuide = guide;
55     newAlign = alignment;
56   }
57
58   public void undo() {
59     changeGuide(newGuide, oldGuide, oldAlign);
60   }
61 }
62
Popular Tags