KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > form > layoutsupport > delegates > JLayeredPaneSupport


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.layoutsupport.delegates;
21
22 import java.awt.*;
23 import javax.swing.*;
24 import java.beans.*;
25 import java.util.Iterator JavaDoc;
26 import java.lang.reflect.Method JavaDoc;
27
28 import org.openide.nodes.*;
29
30 import org.netbeans.modules.form.layoutsupport.*;
31 import org.netbeans.modules.form.codestructure.*;
32 import org.netbeans.modules.form.FormProperty;
33
34 /**
35  * Dedicated layout support class for JLayeredPane. It is based on
36  * AbsoluteLayoutSupport - similarly as NullLayoutSupport, but with one
37  * additional constraints parameter - the layer.
38  *
39  * @author Tomas Pavek
40  */

41
42 public class JLayeredPaneSupport extends AbsoluteLayoutSupport {
43
44     private static Method JavaDoc setBoundsMethod;
45
46     /** Gets the supported layout manager class - JLayeredPane.
47      * @return the class supported by this delegate
48      */

49     public Class JavaDoc getSupportedClass() {
50         return JLayeredPane.class;
51     }
52
53     /** This method is called when switching layout - giving an opportunity to
54      * convert the previous constrainst of components to constraints of the new
55      * layout (this layout). It should do nothing for JLayeredPane - but with
56      * must override it from from AbsoluteLayoutSupport.
57      * @param previousConstraints [input] layout constraints of components in
58      * the previous layout
59      * @param currentConstraints [output] array of converted constraints for
60      * the new layout - to be filled
61      * @param components [input] real components in a real container having the
62      * previous layout
63      */

64     public void convertConstraints(LayoutConstraints[] previousConstraints,
65                                    LayoutConstraints[] currentConstraints,
66                                    Component[] components)
67     {
68         return; // not needed here (contrary to AbsoluteLayoutSupport)
69
}
70
71     /** Adds real components to given container (according to layout
72      * constraints stored for the components).
73      * @param container instance of a real container to be added to
74      * @param containerDelegate effective container delegate of the container
75      * @param components components to be added
76      * @param index position at which to add the components to container
77      */

78     public void addComponentsToContainer(Container container,
79                                          Container containerDelegate,
80                                          Component[] components,
81                                          int index)
82     {
83         if (!(container instanceof JLayeredPane))
84             return;
85
86         for (int i=0; i < components.length; i++) {
87             LayoutConstraints constraints = getConstraints(i + index);
88             if (constraints instanceof LayeredConstraints) {
89                 Component comp = components[i];
90                 container.add(comp, constraints.getConstraintsObject(), i + index);
91
92                 Rectangle bounds = ((LayeredConstraints)constraints).getBounds();
93                 if (bounds.width == -1 || bounds.height == -1) {
94                     Dimension pref = comp.getPreferredSize();
95                     if (bounds.width == -1)
96                         bounds.width = pref.width;
97                     if (bounds.height == -1)
98                         bounds.height = pref.height;
99                 }
100                 comp.setBounds(bounds);
101             }
102         }
103     }
104
105     // ------
106

107     /** This method is called from readComponentCode method to read layout
108      * constraints of a component from code.
109      * @param constrExp CodeExpression object of the constraints (taken from
110      * add method in the code)
111      * @param constrCode CodeGroup to be filled with the relevant constraints
112      * initialization code
113      * @param compExp CodeExpression of the component for which the constraints
114      * are read
115      * @return LayoutConstraints based on information read form code
116      */

117     protected LayoutConstraints readConstraintsCode(CodeExpression constrExp,
118                                                     CodeGroup constrCode,
119                                                     CodeExpression compExp)
120     {
121         LayeredConstraints constr = new LayeredConstraints(0, 0, 0, -1, -1);
122 // constr.refComponent = getLayoutContext().getPrimaryComponent(index);
123

124         Iterator JavaDoc it = CodeStructure.getDefinedStatementsIterator(compExp);
125         CodeStatement[] statements = CodeStructure.filterStatements(
126                                             it, getSetBoundsMethod());
127         if (statements.length > 0) {
128             CodeStatement boundsStatement = statements[statements.length-1];
129             constr.readPropertyExpressions(
130                        boundsStatement.getStatementParameters(), 1);
131             constrCode.addStatement(boundsStatement);
132         }
133
134         FormCodeSupport.readPropertyExpression(constrExp,
135                                                constr.getProperties()[0],
136                                                false);
137
138         return constr;
139     }
140
141     /** Creates code for a component added to the layout (opposite to
142      * readComponentCode method).
143      * @param componentCode CodeGroup to be filled with complete component code
144      * (code for initializing the layout constraints and adding the
145      * component to the layout)
146      * @param compExp CodeExpression object representing component
147      * @param index position of the component in the layout
148      */

149     protected CodeExpression createConstraintsCode(CodeGroup constrCode,
150                                                    LayoutConstraints constr,
151                                                    CodeExpression compExp,
152                                                    int index)
153     {
154         if (!(constr instanceof LayeredConstraints))
155             return null;
156
157         LayeredConstraints layerConstr = (LayeredConstraints) constr;
158         layerConstr.refComponent = getLayoutContext().getPrimaryComponent(index);
159
160         CodeStructure codeStructure = getCodeStructure();
161
162         CodeStatement boundsStatement = CodeStructure.createStatement(
163                           compExp,
164                           getSetBoundsMethod(),
165                           layerConstr.createPropertyExpressions(codeStructure, 1));
166         constrCode.addStatement(boundsStatement);
167
168         return codeStructure.createExpression(
169                  FormCodeSupport.createOrigin(layerConstr.getProperties()[0]));
170     }
171
172     /** This method is called to get a default component layout constraints
173      * metaobject in case it is not provided (e.g. in addComponents method).
174      * @return the default LayoutConstraints object for the supported layout;
175      * null if no component constraints are used
176      */

177     protected LayoutConstraints createDefaultConstraints() {
178         return new LayeredConstraints(0, 0, 0, -1, -1);
179     }
180
181     // ----------
182

183     // overriding AbsoluteLayoutSupport
184
protected LayoutConstraints createNewConstraints(
185                                     LayoutConstraints currentConstr,
186                                     int x, int y, int w, int h)
187     {
188         int layer = currentConstr instanceof LayeredConstraints ?
189                     ((LayeredConstraints)currentConstr).getLayer() : 0;
190
191         return new LayeredConstraints(layer, x, y, w, h);
192     }
193
194     private static Method JavaDoc getSetBoundsMethod() {
195         if (setBoundsMethod == null) {
196             try {
197                 setBoundsMethod = Component.class.getMethod(
198                                     "setBounds", // NOI18N
199
new Class JavaDoc[] { Integer.TYPE, Integer.TYPE,
200                                                   Integer.TYPE, Integer.TYPE });
201             }
202             catch (NoSuchMethodException JavaDoc ex) { // should not happen
203
ex.printStackTrace();
204             }
205         }
206         return setBoundsMethod;
207     }
208
209     // ----------
210

211     /** Extended AbsoluteLayoutConstraints class - with additional layer
212      * property.
213      */

214     public static class LayeredConstraints extends AbsoluteLayoutConstraints {
215         private int layer;
216
217         public LayeredConstraints(int layer, int x, int y, int w, int h) {
218             super(x, y, w, h);
219             this.layer = layer;
220             nullMode = true;
221         }
222
223         public int getLayer() {
224             return layer;
225         }
226
227         // ------
228

229         public Object JavaDoc getConstraintsObject() {
230             return new Integer JavaDoc(layer);
231         }
232
233         public LayoutConstraints cloneConstraints() {
234             return new LayeredConstraints(layer, x, y, w, h);
235         }
236
237         // -------
238

239         protected Node.Property[] createProperties() {
240             Node.Property[] props = super.createProperties();
241             Node.Property[] layeredProps = new Node.Property[props.length + 1];
242
243             layeredProps[0] =
244                 new FormProperty("LayeredConstraints layer", // NOI18N
245
Integer.TYPE,
246                              getBundle().getString("PROP_layer"), // NOI18N
247
getBundle().getString("HINT_layer")) { // NOI18N
248

249                     public Object JavaDoc getTargetValue() {
250                         return new Integer JavaDoc(layer);
251                     }
252                     public void setTargetValue(Object JavaDoc value) {
253                         layer = ((Integer JavaDoc)value).intValue();
254                     }
255                     public boolean supportsDefaultValue () {
256                         return true;
257                     }
258                     public Object JavaDoc getDefaultValue() {
259                         return new Integer JavaDoc(0);
260                     }
261                     public PropertyEditor getExpliciteEditor() {
262                         return new LayerEditor();
263                     }
264                     public Object JavaDoc getValue(String JavaDoc key) {
265                         if ("canEditAsText".equals(key)) // NOI18N
266
return Boolean.TRUE;
267                         return super.getValue(key);
268                     }
269                     public void setPropertyContext(
270                         org.netbeans.modules.form.FormPropertyContext ctx)
271                     { // disabling this method due to limited persistence
272
} // capabilities (compatibility with previous versions)
273
};
274
275             for (int i=0; i < props.length; i++)
276                 layeredProps[i+1] = props[i];
277
278             return layeredProps;
279         }
280     }
281
282     // ---------
283

284     public static final class LayerEditor extends PropertyEditorSupport {
285
286         final String JavaDoc[] tags = {
287             "DEFAULT_LAYER", // NOI18N
288
"PALETTE_LAYER", // NOI18N
289
"MODAL_LAYER", // NOI18N
290
"POPUP_LAYER", // NOI18N
291
"DRAG_LAYER" // NOI18N
292
};
293
294         final Integer JavaDoc[] values = {
295             JLayeredPane.DEFAULT_LAYER,
296             JLayeredPane.PALETTE_LAYER,
297             JLayeredPane.MODAL_LAYER,
298             JLayeredPane.POPUP_LAYER,
299             JLayeredPane.DRAG_LAYER
300         };
301
302         final String JavaDoc[] javaInitStrings = {
303             "javax.swing.JLayeredPane.DEFAULT_LAYER", // NOI18N
304
"javax.swing.JLayeredPane.PALETTE_LAYER", // NOI18N
305
"javax.swing.JLayeredPane.MODAL_LAYER", // NOI18N
306
"javax.swing.JLayeredPane.POPUP_LAYER", // NOI18N
307
"javax.swing.JLayeredPane.DRAG_LAYER" // NOI18N
308
};
309
310         public String JavaDoc[] getTags() {
311             return tags;
312         }
313
314         public String JavaDoc getAsText() {
315             Object JavaDoc value = getValue();
316             for (int i=0; i < values.length; i++)
317                 if (values[i].equals(value))
318                     return tags[i];
319
320             return value.toString();
321         }
322
323         public void setAsText(String JavaDoc str) {
324             for (int i=0; i < tags.length; i++)
325                 if (tags[i].equals(str)) {
326                     setValue(values[i]);
327                     return;
328                 }
329
330             try {
331                 setValue(new Integer JavaDoc(Integer.parseInt(str)));
332             }
333             catch (NumberFormatException JavaDoc e) {} // ignore
334
}
335
336         public String JavaDoc getJavaInitializationString() {
337             Object JavaDoc value = getValue();
338             for (int i=0; i < values.length; i++)
339                 if (values[i].equals(value))
340                     return javaInitStrings[i];
341
342             return value != null ?
343                        "new Integer(" + value.toString() + ")" // NOI18N
344
: null;
345         }
346     }
347 }
348
Popular Tags