KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > form > layoutdesign > LayoutEvent


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.form.layoutdesign;
21
22 import java.util.EventObject JavaDoc;
23
24 /**
25  * Holds information about a change in the layout model. Is able to undo/redo
26  * the change.
27  */

28
29 final class LayoutEvent extends EventObject JavaDoc {
30
31     static final int COMPONENT_ADDED = 1;
32     static final int COMPONENT_REMOVED = 2;
33     static final int INTERVAL_ADDED = 3;
34     static final int INTERVAL_REMOVED = 4;
35     static final int INTERVAL_ALIGNMENT_CHANGED = 5;
36     static final int GROUP_ALIGNMENT_CHANGED = 6;
37     static final int INTERVAL_SIZE_CHANGED = 7;
38     static final int INTERVAL_ATTRIBUTES_CHANGED = 8;
39     static final int INTERVAL_LINKSIZE_CHANGED = 9;
40     static final int CONTAINER_ATTR_CHANGED = 10;
41     static final int COMPONENT_REGISTERED = 11;
42     static final int COMPONENT_UNREGISTERED = 12;
43
44     private int changeType;
45
46     private LayoutComponent component;
47     private LayoutComponent parentComp;
48     private LayoutInterval interval;
49     private LayoutInterval parentInt;
50     private LayoutInterval[] layoutRoots;
51     private int index;
52     private int oldAlignment;
53     private int newAlignment;
54     private int oldAttributes;
55     private int newAttributes;
56     private int[] oldSizes;
57     private int[] newSizes;
58
59     private int oldLinkSizeId;
60     private int newLinkSizeId;
61
62     private int dimension;
63
64     // -----
65
// setup
66

67     LayoutEvent(LayoutModel source, int changeType) {
68         super(source);
69         this.changeType = changeType;
70     }
71     
72     void setComponent(LayoutComponent comp) {
73         this.component = comp;
74     }
75
76     void setComponent(LayoutComponent comp, LayoutComponent parent, int index) {
77         this.component = comp;
78         this.parentComp = parent;
79         this.index = index;
80     }
81
82     void setContainer(LayoutComponent comp, LayoutInterval[] roots) {
83         this.component = comp;
84         this.layoutRoots = roots;
85     }
86
87     void setInterval(LayoutInterval interval, LayoutInterval parent, int index) {
88         this.interval = interval;
89         this.parentInt = parent;
90         this.index = index;
91     }
92
93     void setAlignment(LayoutInterval interval, int oldAlign, int newAlign) {
94         this.interval = interval;
95         this.oldAlignment = oldAlign;
96         this.newAlignment = newAlign;
97     }
98
99     void setLinkSizeGroup(LayoutComponent component, int oldLinkSizeId, int newLinkSizeId, int dimension) {
100         if (component == null) {
101             Thread.dumpStack();
102         }
103         this.component = component;
104         this.oldLinkSizeId = oldLinkSizeId;
105         this.newLinkSizeId = newLinkSizeId;
106         this.dimension = dimension;
107     }
108
109     void setAttributes(LayoutInterval interval, int oldAttributes, int newAttributes) {
110         this.interval = interval;
111         this.oldAttributes = oldAttributes;
112         this.newAttributes = newAttributes;
113     }
114
115     void setSize(LayoutInterval interval,
116                  int oldMin, int oldPref, int oldMax,
117                  int newMin, int newPref, int newMax)
118     {
119         this.interval = interval;
120         this.oldSizes = new int[] { oldMin, oldPref, oldMax };
121         this.newSizes = new int[] { newMin, newPref, newMax };
122     }
123
124     // -----
125
// getters
126

127     LayoutModel getModel() {
128         return (LayoutModel) source;
129     }
130
131     int getType() {
132         return changeType;
133     }
134
135     LayoutComponent getComponent() {
136         return component;
137     }
138
139     LayoutComponent getParentComponent() {
140         return parentComp;
141     }
142
143     LayoutInterval getInterval() {
144         return interval;
145     }
146
147     LayoutInterval getParentInterval() {
148         return parentInt;
149     }
150
151     int getIndex() {
152         return index;
153     }
154
155     // -----
156
// undo & redo
157

158     void undo() {
159         switch (changeType) {
160             case COMPONENT_ADDED:
161                 undoComponentAddition();
162                 break;
163             case COMPONENT_REMOVED:
164                 undoComponentRemoval();
165                 break;
166             case INTERVAL_ADDED:
167                 undoIntervalAddition();
168                 break;
169             case INTERVAL_REMOVED:
170                 undoIntervalRemoval();
171                 break;
172             case INTERVAL_ALIGNMENT_CHANGED:
173                 // getModel().setIntervalAlignment(interval, oldAlignment);
174
interval.setAlignment(oldAlignment);
175                 break;
176             case GROUP_ALIGNMENT_CHANGED:
177                 // getModel().setGroupAlignment(interval, oldAlignment);
178
interval.setGroupAlignment(oldAlignment);
179                 break;
180             case INTERVAL_SIZE_CHANGED:
181                 // getModel().setIntervalSize(interval, oldSizes[0], oldSizes[1], oldSizes[2]);
182
interval.setSizes(oldSizes[0], oldSizes[1], oldSizes[2]);
183                 break;
184             case INTERVAL_ATTRIBUTES_CHANGED:
185                 interval.setAttributes(oldAttributes);
186                 break;
187             case INTERVAL_LINKSIZE_CHANGED:
188                 undoLinkSize(oldLinkSizeId);
189                 break;
190             case CONTAINER_ATTR_CHANGED:
191                 changeContainerAttr();
192                 break;
193             case COMPONENT_REGISTERED:
194                 undoComponentRegistration();
195                 break;
196             case COMPONENT_UNREGISTERED:
197                 undoComponentUnregistration();
198                 break;
199         }
200     }
201
202     void redo() {
203         switch (changeType) {
204             case COMPONENT_ADDED:
205                 undoComponentRemoval();
206                 break;
207             case COMPONENT_REMOVED:
208                 undoComponentAddition();
209                 break;
210             case INTERVAL_ADDED:
211                 undoIntervalRemoval();
212                 break;
213             case INTERVAL_REMOVED:
214                 undoIntervalAddition();
215                 break;
216             case INTERVAL_ALIGNMENT_CHANGED:
217                 // getModel().setIntervalAlignment(interval, newAlignment);
218
interval.setAlignment(newAlignment);
219                 break;
220             case GROUP_ALIGNMENT_CHANGED:
221                 // getModel().setGroupAlignment(interval, newAlignment);
222
interval.setGroupAlignment(newAlignment);
223                 break;
224             case INTERVAL_SIZE_CHANGED:
225                 // getModel().setIntervalSize(interval, newSizes[0], newSizes[1], newSizes[2]);
226
interval.setSizes(newSizes[0], newSizes[1], newSizes[2]);
227                 break;
228             case INTERVAL_ATTRIBUTES_CHANGED:
229                 interval.setAttributes(newAttributes);
230                 break;
231             case INTERVAL_LINKSIZE_CHANGED:
232                 undoLinkSize(newLinkSizeId);
233                 break;
234             case CONTAINER_ATTR_CHANGED:
235                 changeContainerAttr();
236                 break;
237             case COMPONENT_REGISTERED:
238                 undoComponentUnregistration();
239                 break;
240             case COMPONENT_UNREGISTERED:
241                 undoComponentRegistration();
242                 break;
243         }
244     }
245     
246     private void undoLinkSize(int id) {
247         getModel().removeComponentFromLinkSizedGroup(component, dimension);
248         if (!(id == LayoutConstants.NOT_EXPLICITLY_DEFINED)) {
249             getModel().addComponentToLinkSizedGroupImpl(id, component.getId(), dimension);
250         }
251     }
252
253     private void undoComponentAddition() {
254         getModel().removeComponentImpl(component);
255     }
256
257     private void undoComponentRemoval() {
258         getModel().addComponentImpl(component, parentComp, index);
259     }
260
261     private void undoIntervalAddition() {
262         getModel().removeInterval(parentInt, index);
263     }
264
265     private void undoIntervalRemoval() {
266         getModel().addInterval(interval, parentInt, index);
267     }
268     
269     private void changeContainerAttr() {
270         component.setLayoutContainer(!component.isLayoutContainer(), layoutRoots);
271     }
272     
273     private void undoComponentRegistration() {
274         getModel().unregisterComponentImpl(component);
275     }
276     
277     private void undoComponentUnregistration() {
278         getModel().registerComponentImpl(component);
279     }
280     
281 }
282
Popular Tags