KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* *****************************************************************************
2  * NightLabs Editor2D - Graphical editor framework *
3  * Copyright (C) 2004-2005 NightLabs - http://NightLabs.org *
4  * Project author: Daniel Mazurek <Daniel.Mazurek [at] nightlabs [dot] org> *
5  * *
6  * This library is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU Lesser General Public *
8  * License as published by the Free Software Foundation; either *
9  * version 2.1 of the License, or (at your option) any later version. *
10  * *
11  * This library is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14  * Lesser General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Lesser General Public *
17  * License along with this library; if not, write to the *
18  * Free Software Foundation, Inc., *
19  * 51 Franklin St, Fifth Floor, *
20  * Boston, MA 02110-1301 USA *
21  * *
22  * Or get it online : *
23  * http://www.gnu.org/copyleft/lesser.html *
24  * *
25  * *
26  ******************************************************************************/

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.EditorGuide;
34
35 public class ChangeGuideCommand
36 extends Command
37 {
38   private DrawComponent part;
39   private EditorGuide oldGuide, newGuide;
40   private int oldAlign, newAlign;
41   private boolean horizontal;
42
43   public ChangeGuideCommand(DrawComponent part, boolean horizontalGuide) {
44     super();
45     this.part = part;
46     horizontal = horizontalGuide;
47   }
48
49   protected void changeGuide(EditorGuide oldGuide, EditorGuide newGuide, int newAlignment) {
50     if (oldGuide != null && oldGuide != newGuide) {
51         oldGuide.detachPart(part);
52     }
53     // You need to re-attach the part even if the oldGuide and the newGuide are the same
54
// because the alignment could have changed
55
if (newGuide != null) {
56         newGuide.attachPart(part, newAlignment);
57     }
58   }
59
60   public void execute() {
61     // Cache the old values
62
oldGuide = horizontal ? part.getHorizontalGuide() : part.getVerticalGuide();
63     if (oldGuide != null)
64         oldAlign = oldGuide.getAlignment(part);
65     
66     redo();
67   }
68
69   public void redo() {
70     changeGuide(oldGuide, newGuide, newAlign);
71   }
72
73   public void setNewGuide(EditorGuide guide, int alignment) {
74     newGuide = guide;
75     newAlign = alignment;
76   }
77
78   public void undo() {
79     changeGuide(newGuide, oldGuide, oldAlign);
80   }
81 }
82
Popular Tags