KickJava   Java API By Example, From Geeks To Geeks.

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


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.awt.Rectangle JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.beans.PropertyChangeSupport JavaDoc;
25 import java.util.*;
26
27 /**
28  * This class manages layout information about a component in the layout.
29  * It refers to corresponding layout intervals (horizontal and vertical) in the
30  * layout structure.
31  *
32  * A layout component can be found according to its Id in the layout model.
33  *
34  * The component may serve the role of layout container - then it also defines
35  * top (root) intervals (horizontal and vertical) for its internal layouts.
36  *
37  * @see LayoutInterval
38  *
39  * @author Tomas Pavek
40  */

41
42 public final class LayoutComponent implements LayoutConstants {
43
44     // Identification of the component in the model
45
private String JavaDoc componentId;
46
47     // The parent component of this component
48
private LayoutComponent parentComponent;
49
50     // Layout intervals representing the component in the layout hierarchy.
51
// There is one interval for each dimension.
52
private LayoutInterval[] layoutIntervals;
53
54     // Potential resizability of the component in the design area.
55
private boolean[] resizability;
56
57     // Root layout intervals of a container layout. There is one interval for
58
// each dimension. Defined by components that are layout containers, i.e.
59
// managing layout of their subcomponents. Otherwise the array is null.
60
private LayoutInterval[] layoutRoots;
61
62     // Subcomponents of this component.
63
private java.util.List JavaDoc subComponents;
64     
65     private PropertyChangeSupport JavaDoc propertyChangeSupport = new PropertyChangeSupport JavaDoc(this);
66
67     // horizontal size-link
68
private int horizontalLinkId = NOT_EXPLICITLY_DEFINED;
69     
70     // vertical size-link
71
private int verticalLinkId = NOT_EXPLICITLY_DEFINED;
72     
73     // -----
74
// setup
75

76     public LayoutComponent(String JavaDoc id, boolean isContainer) {
77         if (id == null)
78             throw new NullPointerException JavaDoc();
79         componentId = id;
80         layoutIntervals = new LayoutInterval[DIM_COUNT];
81         for (int i=0; i < DIM_COUNT; i++) {
82             layoutIntervals[i] = new LayoutInterval(SINGLE);
83             layoutIntervals[i].setComponent(this);
84             layoutIntervals[i].setSizes(USE_PREFERRED_SIZE,
85                                         NOT_EXPLICITLY_DEFINED,
86                                         USE_PREFERRED_SIZE);
87         }
88         if (isContainer) {
89             layoutRoots = new LayoutInterval[DIM_COUNT];
90             for (int i=0; i < DIM_COUNT; i++) {
91                 layoutRoots[i] = new LayoutInterval(PARALLEL);
92 // layoutRoots[i].setSizes(NOT_EXPLICITLY_DEFINED,
93
// NOT_EXPLICITLY_DEFINED,
94
// Short.MAX_VALUE);
95
}
96         }
97     }
98
99     public LayoutComponent(String JavaDoc id, boolean isContainer, int initialWidth, int initialHeight) {
100         this(id, isContainer);
101         if (isContainer) {
102             for (int i=0; i < DIM_COUNT; i++) {
103                 LayoutInterval gap = new LayoutInterval(SINGLE);
104                 gap.setSizes(0, i==HORIZONTAL ? initialWidth : initialHeight, Short.MAX_VALUE);
105                 layoutRoots[i].add(gap, 0);
106             }
107         }
108         else {
109             layoutIntervals[HORIZONTAL].setPreferredSize(initialWidth);
110             layoutIntervals[VERTICAL].setPreferredSize(initialHeight);
111         }
112     }
113
114     void setId(String JavaDoc id) {
115         componentId = id;
116     }
117
118     void setLayoutInterval(LayoutInterval interval, int dimension) {
119         layoutIntervals[dimension] = interval;
120     }
121
122     void setResizability(boolean[] resizability) {
123         this.resizability = resizability;
124     }
125
126     boolean[] getResizability() {
127         return resizability;
128     }
129
130     // -------
131
// public methods
132

133     public String JavaDoc getId() {
134         return componentId;
135     }
136
137     public LayoutComponent getParent() {
138         return parentComponent;
139     }
140
141     public boolean isParentOf(LayoutComponent comp) {
142         do {
143             comp = comp.getParent();
144             if (comp == this)
145                 return true;
146         }
147         while (comp != null);
148         return false;
149     }
150
151     public LayoutInterval getLayoutInterval(int dimension) {
152         return layoutIntervals[dimension];
153     }
154
155     public boolean isLayoutContainer() {
156         return layoutRoots != null;
157     }
158
159     public LayoutInterval getLayoutRoot(int dimension) {
160         return layoutRoots[dimension];
161     }
162     
163     LayoutInterval[] getLayoutRoots() {
164         return layoutRoots;
165     }
166
167     // --------
168

169     public Iterator getSubcomponents() {
170         return subComponents != null && subComponents.size() > 0 ?
171                subComponents.iterator() : Collections.EMPTY_LIST.iterator();
172     }
173     
174     int getSubComponentCount() {
175         return (subComponents == null) ? 0 : subComponents.size();
176     }
177     
178     LayoutComponent getSubComponent(int index) {
179         return (LayoutComponent)subComponents.get(index);
180     }
181
182     int indexOf(LayoutComponent comp) {
183         return subComponents != null ? subComponents.indexOf(comp) : -1;
184     }
185
186 // int add(LayoutComponent comp) {
187
// return add(comp, -1);
188
// }
189

190     int add(LayoutComponent comp, int index) {
191         assert isLayoutContainer();
192
193         if (subComponents == null) {
194             subComponents = new LinkedList();
195         }
196         if (index < 0) {
197             index = subComponents.size();
198         }
199         subComponents.add(index, comp);
200         comp.parentComponent = this;
201
202         return index;
203     }
204
205     int remove(LayoutComponent comp) {
206         int index;
207         if (subComponents != null) {
208             index = subComponents.indexOf(comp);
209             if (index >= 0) {
210                 subComponents.remove(index);
211                 comp.parentComponent = null;
212             }
213         }
214         else index = -1;
215         return index;
216     }
217
218     void setLayoutContainer(boolean isContainer, LayoutInterval[] roots) {
219         if (isContainer != isLayoutContainer()) {
220             if (isContainer) {
221                 if (roots == null) {
222                     layoutRoots = new LayoutInterval[DIM_COUNT];
223                     for (int i=0; i < DIM_COUNT; i++) {
224                         layoutRoots[i] = new LayoutInterval(PARALLEL);
225                     }
226                 } else {
227                     layoutRoots = roots;
228                 }
229             }
230             else {
231                 layoutRoots = null;
232                 subComponents = null;
233             }
234         }
235     }
236
237     // -----
238

239     static LayoutComponent getCommonParent(LayoutComponent comp1, LayoutComponent comp2) {
240         // Find all parents of given components
241
Iterator parents1 = parentsOfComponent(comp1).iterator();
242         Iterator parents2 = parentsOfComponent(comp2).iterator();
243         LayoutComponent parent1 = (LayoutComponent)parents1.next();
244         LayoutComponent parent2 = (LayoutComponent)parents2.next();
245
246         // Candidate for the common parent
247
LayoutComponent parent = null;
248         while (parent1 == parent2) {
249             parent = parent1;
250             if (parents1.hasNext()) {
251                 parent1 = (LayoutComponent)parents1.next();
252             } else {
253                 break;
254             }
255             if (parents2.hasNext()) {
256                 parent2 = (LayoutComponent)parents2.next();
257             } else {
258                 break;
259             }
260         }
261         return parent;
262     }
263
264     private static List parentsOfComponent(LayoutComponent comp) {
265         List parents = new LinkedList();
266         while (comp != null) {
267             parents.add(0, comp);
268             comp = comp.getParent();
269         }
270         return parents;
271     }
272
273     // -----
274
// current state of the layout - current position and size of component
275
// kept to be available quickly for the layout designer
276

277     void setCurrentBounds(Rectangle JavaDoc bounds, int baseline) {
278         LayoutRegion space = layoutIntervals[0].getCurrentSpace();;
279         space.set(bounds, baseline > 0 ? bounds.y + baseline : LayoutRegion.UNKNOWN);
280         for (int i=1; i < layoutIntervals.length; i++) {
281             layoutIntervals[i].setCurrentSpace(space);
282         }
283     }
284
285     void setCurrentInterior(Rectangle JavaDoc bounds) {
286         LayoutRegion space = null;
287         for (int i=0; i < layoutRoots.length; i++) {
288             if (space == null) {
289                 space = layoutRoots[i].getCurrentSpace();
290                 space.set(bounds, LayoutRegion.UNKNOWN);
291             }
292             else {
293                 layoutRoots[i].setCurrentSpace(space);
294             }
295         }
296     }
297     
298     /**
299      * @return whether this intervals size is linked with some other component in a direction horizontal or vertical
300      */

301     public boolean isLinkSized(int dimension) {
302         if (dimension == HORIZONTAL) {
303             return NOT_EXPLICITLY_DEFINED != horizontalLinkId;
304         }
305         return NOT_EXPLICITLY_DEFINED != verticalLinkId;
306     }
307     
308     /**
309      * @return whether this intervals size is linked with some other component in a direction horizontal or vertical
310      */

311     public int getLinkSizeId(int dimension) {
312         if (dimension == HORIZONTAL) {
313             return horizontalLinkId;
314         }
315         return verticalLinkId;
316     }
317
318     /**
319      * @return whether this intervals size is linked with some other component in a direction horizontal or vertical
320      */

321     public void setLinkSizeId(int id, int dimension) {
322         if (dimension == HORIZONTAL) {
323             horizontalLinkId = id;
324         } else {
325             verticalLinkId = id;
326         }
327         
328     }
329     
330     // Listener support
331
public void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {
332         propertyChangeSupport.addPropertyChangeListener(listener);
333     }
334     
335     public void removePropertyChangeListener(PropertyChangeListener JavaDoc listener) {
336         propertyChangeSupport.removePropertyChangeListener(listener);
337     }
338     
339     void firePropertyChange(String JavaDoc propertyName, Object JavaDoc oldValue, Object JavaDoc newValue) {
340         propertyChangeSupport.firePropertyChange(propertyName, oldValue, newValue);
341     }
342     
343 }
344
Popular Tags