KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.beans.*;
24 import java.util.*;
25 import java.lang.reflect.Method JavaDoc;
26
27 import org.openide.util.Utilities;
28
29 import org.netbeans.modules.form.layoutsupport.*;
30 import org.netbeans.modules.form.codestructure.*;
31
32 /**
33  * Support for null layout manager.
34  *
35  * @author Tomas Pavek
36  */

37
38 public class NullLayoutSupport extends AbsoluteLayoutSupport {
39
40     private static Method JavaDoc setBoundsMethod;
41
42     /** The icon for NullLayout. */
43     private static String JavaDoc iconURL = "org/netbeans/modules/form/layoutsupport/resources/NullLayout.gif"; // NOI18N
44
/** The icon for NullLayout. */
45     private static String JavaDoc icon32URL = "org/netbeans/modules/form/layoutsupport/resources/NullLayout32.gif"; // NOI18N
46

47     /** Gets the supported layout manager class - this one is rather special,
48      * it's null.
49      * @return the class supported by this delegate
50      */

51     public Class JavaDoc getSupportedClass() {
52         return null;
53     }
54
55     /** Provides an icon to be used for the layout node in Component
56      * Inspector. Only 16x16 color icon is required.
57      * @param type is one of BeanInfo constants: ICON_COLOR_16x16,
58      * ICON_COLOR_32x32, ICON_MONO_16x16, ICON_MONO_32x32
59      * @return icon to be displayed for node in Component Inspector
60      */

61     public Image getIcon(int type) {
62         switch (type) {
63             case BeanInfo.ICON_COLOR_16x16:
64             case BeanInfo.ICON_MONO_16x16:
65                 return Utilities.loadImage(iconURL);
66             default:
67                 return Utilities.loadImage(icon32URL);
68         }
69     }
70
71     /** Gets code for setting up one component's constraints and adding the
72      * component to the layout (container).
73      * @return one component's layout code
74      */

75     public CodeGroup getComponentCode(int index) {
76         // hack: be sure that the constraints object is associated with the
77
// primary component (to be able to get its preferred size)
78
LayoutConstraints constr = getConstraints(index);
79         if (constr instanceof AbsoluteLayoutConstraints) {
80             AbsoluteLayoutConstraints absConstr =
81                 (AbsoluteLayoutConstraints) constr;
82             if (absConstr.refComponent == null)
83                 absConstr.refComponent =
84                     getLayoutContext().getPrimaryComponent(index);
85         }
86
87         return super.getComponentCode(index);
88     }
89
90     /** Sets up the layout (without adding components) on a real container,
91      * according to the internal metadata representation.
92      * @param container instance of a real container to be set
93      * @param containerDelegate effective container delegate of the container
94      * (e.g. like content pane of JFrame)
95      */

96     public void setLayoutToContainer(Container container,
97                                      Container containerDelegate)
98     {
99         containerDelegate.setLayout(null);
100     }
101
102     /** Adds real components to given container (according to layout
103      * constraints stored for the components).
104      * @param container instance of a real container to be added to
105      * @param containerDelegate effective container delegate of the container
106      * (e.g. like content pane of JFrame)
107      * @param components components to be added
108      * @param index position at which to add the components to container
109      */

110     public void addComponentsToContainer(Container container,
111                                          Container containerDelegate,
112                                          Component[] components,
113                                          int index)
114     {
115         for (int i=0; i < components.length; i++) {
116             LayoutConstraints constr = getConstraints(i + index);
117             if (constr instanceof AbsoluteLayoutConstraints) {
118                 AbsoluteLayoutConstraints alc = (AbsoluteLayoutConstraints)constr;
119                 Component comp = components[i];
120                 Rectangle bounds = alc.getBounds();
121                 if (bounds.width == -1 || bounds.height == -1) {
122                     Dimension pref = !(comp instanceof javax.swing.JComponent JavaDoc)
123                                             && alc.refComponent != null ?
124                         alc.refComponent.getPreferredSize() :
125                         comp.getPreferredSize();
126
127                     if (bounds.width == -1)
128                         bounds.width = pref.width;
129                     if (bounds.height == -1)
130                         bounds.height = pref.height;
131                 }
132                 containerDelegate.add(comp, i + index);
133                 comp.setBounds(bounds);
134             }
135         }
136     }
137
138     // ---------
139

140     /** Creates code structures for a new layout manager (opposite to
141      * readInitLayoutCode).
142      * @param initLayoutCode CodeGroup to be filled with relevant
143      * initialization code
144      * @return created CodeExpression representing the layout manager
145      * (so representing null value in this case)
146      */

147     protected CodeExpression createInitLayoutCode(CodeGroup layoutCode) {
148         return getCodeStructure().createNullExpression(LayoutManager.class);
149     }
150
151     /** This method is used for scanning code structures and recognizing
152      * components added to containers and their constraints. It's called from
153      * initialize method. When a relevant code statement is found, then the
154      * CodeExpression of component is get and added to component, and also the
155      * layout constraints information is read. The special thing for null
156      * layout is that components are initailized with setBounds call instead of
157      * using constraints object, so we must override the reading process from
158      * AbstractLayoutSupport.
159      * @param statement CodeStatement to be tested if it contains relevant code
160      * @param componentCode CodeGroup to be filled with all component code
161      * @return CodeExpression representing found component; null if the
162      * statement is not relevant
163      */

164     protected CodeExpression readComponentCode(CodeStatement statement,
165                                                CodeGroup componentCode)
166     {
167         if (getSimpleAddMethod().equals(statement.getMetaObject())) {
168             CodeExpression compExp = statement.getStatementParameters()[0];
169             componentCode.addStatement(statement);
170
171             AbsoluteLayoutConstraints constr =
172                 new AbsoluteLayoutConstraints(0, 0, -1, -1);
173             constr.nullMode = true;
174 // constr.refComponent = getLayoutContext().getPrimaryComponent(index);
175

176             // search for setBounds statement on component
177
Iterator it = CodeStructure.getDefinedStatementsIterator(compExp);
178             CodeStatement[] statements = CodeStructure.filterStatements(
179                                                 it, getSetBoundsMethod());
180             if (statements.length > 0) {
181                 CodeStatement boundsStatement =
182                     statements[statements.length-1];
183                 constr.readPropertyExpressions(
184                     boundsStatement.getStatementParameters(), 0);
185                 componentCode.addStatement(boundsStatement);
186             }
187             getConstraintsList().add(constr);
188
189             return compExp;
190         }
191         return null;
192     }
193
194     /** Creates code for a component added to the layout (opposite to
195      * readComponentCode method). As well as for readComponentCode - null
196      * layout requires the components to be initailized with setBounds call
197      * instead of using constraints object, so this method must be overridden
198      * (from AbstractLayoutSupport).
199      * @param componentCode CodeGroup to be filled with complete component code
200      * (code for initializing the layout constraints and adding the
201      * component to the layout)
202      * @param compExp CodeExpression object representing component
203      * @param index position of the component in the layout
204      */

205     protected void createComponentCode(CodeGroup componentCode,
206                                        CodeExpression compExp,
207                                        int index)
208     {
209         // create code for "add" method
210
componentCode.addStatement(
211                 CodeStructure.createStatement(
212                         getActiveContainerCodeExpression(),
213                         getSimpleAddMethod(),
214                         new CodeExpression[] { compExp }));
215
216         // create code for "setBounds" method
217
LayoutConstraints constr = getConstraints(index);
218         if (constr instanceof AbsoluteLayoutConstraints) {
219             AbsoluteLayoutConstraints absConstr =
220                 (AbsoluteLayoutConstraints) constr;
221             absConstr.nullMode = true;
222             absConstr.refComponent = getLayoutContext().getPrimaryComponent(index);
223
224             componentCode.addStatement(
225                 CodeStructure.createStatement(
226                     compExp,
227                     getSetBoundsMethod(),
228                     absConstr.createPropertyExpressions(getCodeStructure(), 0)));
229         }
230     }
231
232     private static Method JavaDoc getSetBoundsMethod() {
233         if (setBoundsMethod == null) {
234             try {
235                 setBoundsMethod = Component.class.getMethod(
236                                     "setBounds", // NOI18N
237
new Class JavaDoc[] { Integer.TYPE, Integer.TYPE,
238                                                   Integer.TYPE, Integer.TYPE });
239             }
240             catch (NoSuchMethodException JavaDoc ex) { // should not happen
241
ex.printStackTrace();
242             }
243         }
244         return setBoundsMethod;
245     }
246 }
247
Popular Tags