KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > editor > view > SimpleViewLayoutState


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.lib.editor.view;
21
22 import javax.swing.text.AbstractDocument JavaDoc;
23 import javax.swing.text.View JavaDoc;
24 import org.netbeans.editor.view.spi.FlyView;
25 import org.netbeans.editor.view.spi.LockView;
26 import org.netbeans.editor.view.spi.ViewLayoutState;
27 import org.netbeans.editor.view.spi.ViewUtilities;
28
29 /**
30  * Implementation of {@link org.netbeans.editor.view.spi.ViewLayoutState}
31  * for non-flyweight views that only caches preferred spans
32  * along both axes.
33  * <br>
34  * Preferred span is returned for maximum and minimum spans
35  * along both axes.
36  *
37  * @author Miloslav Metelka
38  * @version 1.00
39  */

40
41 public class SimpleViewLayoutState implements ViewLayoutState {
42     
43     /**
44      * Bit value in <code>statusBits</code> determining
45      * whether the x is the major axis of this layout state.
46      */

47     private static final int X_MAJOR_AXIS_BIT = 1;
48     
49     /**
50      * Bit value in <code>statusBits</code> determining
51      * that preference along the major axis of this layout state has changed.
52      */

53     private static final int MAJOR_AXIS_PREFERENCE_CHANGED_BIT = 2;
54
55     /**
56      * Bit value in <code>statusBits</code> determining
57      * that preference along the minor axis of this layout state has changed.
58      */

59     private static final int MINOR_AXIS_PREFERENCE_CHANGED_BIT = 4;
60     
61     /**
62      * Bit value in <code>statusBits</code> determining
63      * that size of the wrapped view needs to be set again
64      * by <code>View.setSize()</code>
65      */

66     private static final int VIEW_SIZE_INVALID_BIT = 8;
67     
68     protected static final int LAST_USED_BIT = VIEW_SIZE_INVALID_BIT;
69
70     /**
71      * Bit composition being used to test whether
72      * the layout is up-to-date or not.
73      */

74     private static final int ANY_INVALID
75         = MAJOR_AXIS_PREFERENCE_CHANGED_BIT
76         | MINOR_AXIS_PREFERENCE_CHANGED_BIT
77         | VIEW_SIZE_INVALID_BIT;
78
79     
80     private int statusBits; // 4
81

82     private View JavaDoc view; // 8
83

84     private double layoutMajorAxisRawOffset; // double => 16
85

86     private int viewRawIndex; // 20
87

88     // major axis
89
private float layoutMajorAxisPreferredSpan; // 24
90

91     // minor axis
92
private float layoutMinorAxisPreferredSpan; // 28
93

94     private float layoutMinorAxisAlignment; // 32
95

96     
97     public SimpleViewLayoutState(View JavaDoc v) {
98 // assert !(v instanceof FlyView); // ensure not flyweight view
99

100         view = v;
101     }
102
103     public final View JavaDoc getView() {
104         return view;
105     }
106     
107     public boolean isFlyweight() {
108         return false;
109     }
110     
111     public ViewLayoutState selectLayoutMajorAxis(int axis) {
112 // assert ViewUtilities.isAxisValid(axis);
113

114         if (axis == View.X_AXIS) {
115             setStatusBits(X_MAJOR_AXIS_BIT);
116         } else { // y axis
117
clearStatusBits(X_MAJOR_AXIS_BIT);
118         }
119         
120         return this;
121     }
122     
123     protected final boolean isXMajorAxis() {
124         return isStatusBitsNonZero(X_MAJOR_AXIS_BIT);
125     }
126     
127     protected final int getMajorAxis() {
128         return isXMajorAxis() ? View.X_AXIS : View.Y_AXIS;
129     }
130     
131     protected final int getMinorAxis() {
132         return isXMajorAxis() ? View.Y_AXIS : View.X_AXIS;
133     }
134     
135     protected final ViewLayoutState.Parent getLayoutStateParent() {
136         View JavaDoc parent = getView().getParent();
137         return (parent instanceof ViewLayoutState.Parent)
138             ? ((ViewLayoutState.Parent)parent)
139             : null;
140     }
141
142     public void updateLayout() {
143         View JavaDoc parent = view.getParent();
144         if (parent == null) { // disconnected from hierarchy => no layout update
145
return;
146         }
147
148         while (!isLayoutValid()) { // Nothing to do
149
doUpdateLayout(parent);
150         }
151     }
152         
153     protected void doUpdateLayout(View JavaDoc parent) {
154         ViewLayoutState.Parent lsParent = (parent instanceof ViewLayoutState.Parent)
155             ? (ViewLayoutState.Parent)parent
156             : null;
157
158         // Check whether minor axis has changed
159
if (isStatusBitsNonZero(MINOR_AXIS_PREFERENCE_CHANGED_BIT)) { // minor not valid
160
clearStatusBits(MINOR_AXIS_PREFERENCE_CHANGED_BIT);
161
162             int minorAxis = getMinorAxis();
163             if (minorAxisUpdateLayout(minorAxis)) {
164                 if (lsParent != null) {
165                     lsParent.minorAxisPreferenceChanged(this);
166                 }
167             }
168         }
169
170         // Check whether major axis has changed
171
if (isStatusBitsNonZero(MAJOR_AXIS_PREFERENCE_CHANGED_BIT)) { // major not valid
172
clearStatusBits(MAJOR_AXIS_PREFERENCE_CHANGED_BIT);
173
174             float oldSpan = getLayoutMajorAxisPreferredSpanFloat();
175             float newSpan = view.getPreferredSpan(getMajorAxis());
176             setLayoutMajorAxisPreferredSpan(newSpan);
177             double majorAxisSpanDelta = newSpan - oldSpan;
178             if (majorAxisSpanDelta != 0 && lsParent != null) {
179                 lsParent.majorAxisPreferenceChanged(this, majorAxisSpanDelta);
180             }
181         }
182
183         // Check whether size must be set on the view
184
if (isStatusBitsNonZero(VIEW_SIZE_INVALID_BIT)) {
185             clearStatusBits(VIEW_SIZE_INVALID_BIT);
186
187             if (lsParent != null) {
188                 float width;
189                 float height;
190                 float majorAxisSpan = (float)getLayoutMajorAxisPreferredSpan();
191                 float minorAxisSpan = lsParent.getMinorAxisSpan(this);
192                 if (isXMajorAxis()) { // x is major axis
193
width = majorAxisSpan;
194                     height = minorAxisSpan;
195                 } else {
196                     width = minorAxisSpan;
197                     height = majorAxisSpan;
198                 }
199
200                 view.setSize(width, height);
201             }
202         }
203         
204         // Call recursively to make sure that there is no more work.
205
updateLayout();
206     }
207     
208     protected boolean minorAxisUpdateLayout(int minorAxis) {
209         boolean minorAxisPreferenceChanged = false;
210         float val;
211         
212         val = view.getPreferredSpan(minorAxis);
213         if (val != getLayoutMinorAxisPreferredSpan()) {
214             setLayoutMinorAxisPreferredSpan(val);
215             minorAxisPreferenceChanged = true;
216         }
217         
218         val = view.getAlignment(getMinorAxis());
219         if (val != getLayoutMinorAxisAlignment()) {
220             setLayoutMinorAxisAlignment(val);
221             minorAxisPreferenceChanged = true;
222         }
223
224         return minorAxisPreferenceChanged;
225     }
226
227     public int getViewRawIndex() {
228         return viewRawIndex;
229     }
230     
231     public void setViewRawIndex(int viewRawIndex) {
232         this.viewRawIndex = viewRawIndex;
233     }
234
235     public double getLayoutMajorAxisRawOffset() {
236         return layoutMajorAxisRawOffset;
237     }
238     
239     public void setLayoutMajorAxisRawOffset(double layoutMajorAxisRawOffset) {
240         this.layoutMajorAxisRawOffset = layoutMajorAxisRawOffset;
241     }
242     
243     public double getLayoutMajorAxisPreferredSpan() {
244         return layoutMajorAxisPreferredSpan;
245     }
246     
247     public float getLayoutMajorAxisPreferredSpanFloat() {
248         return layoutMajorAxisPreferredSpan;
249     }
250
251     protected void setLayoutMajorAxisPreferredSpan(float layoutMajorAxisPreferredSpan) {
252         this.layoutMajorAxisPreferredSpan = layoutMajorAxisPreferredSpan;
253     }
254     
255     public float getLayoutMinorAxisPreferredSpan() {
256         return layoutMinorAxisPreferredSpan;
257     }
258     
259     protected void setLayoutMinorAxisPreferredSpan(float layoutMinorAxisPreferredSpan) {
260         this.layoutMinorAxisPreferredSpan = layoutMinorAxisPreferredSpan;
261     }
262
263     public float getLayoutMinorAxisMinimumSpan() {
264         return getLayoutMinorAxisPreferredSpan();
265     }
266
267     public float getLayoutMinorAxisMaximumSpan() {
268         return getLayoutMinorAxisPreferredSpan();
269     }
270     
271     public float getLayoutMinorAxisAlignment() {
272         return layoutMinorAxisAlignment;
273     }
274
275     public void setLayoutMinorAxisAlignment(float layoutMinorAxisAlignment) {
276         this.layoutMinorAxisAlignment = layoutMinorAxisAlignment;
277     }
278     
279     public void viewPreferenceChanged(boolean width, boolean height) {
280         if (isXMajorAxis()) { // x is major axis
281
if (width) {
282                 setStatusBits(MAJOR_AXIS_PREFERENCE_CHANGED_BIT); // major no longer valid
283
}
284             if (height) {
285                 setStatusBits(MINOR_AXIS_PREFERENCE_CHANGED_BIT); // minor no longer valid
286
}
287         } else {
288             if (width) {
289                 setStatusBits(MINOR_AXIS_PREFERENCE_CHANGED_BIT); // minor no longer valid
290
}
291             if (height) {
292                 setStatusBits(MAJOR_AXIS_PREFERENCE_CHANGED_BIT); // major no longer valid
293
}
294         }
295         setStatusBits(VIEW_SIZE_INVALID_BIT); // child size no longer valid
296
}
297     
298     public void markViewSizeInvalid() {
299         setStatusBits(VIEW_SIZE_INVALID_BIT);
300     }
301
302     public boolean isLayoutValid() {
303         return !isStatusBitsNonZero(ANY_INVALID);
304     }
305
306     protected final int getStatusBits(int bits) {
307         return (statusBits & bits);
308     }
309     
310     protected final boolean isStatusBitsNonZero(int bits) {
311         return (getStatusBits(bits) != 0);
312     }
313     
314     protected final void setStatusBits(int bits) {
315         statusBits |= bits;
316     }
317     
318     protected final void clearStatusBits(int bits) {
319         statusBits &= ~bits;
320     }
321
322 }
323
Popular Tags