KickJava   Java API By Example, From Geeks To Geeks.

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


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
26 import org.openide.nodes.Node;
27
28 import org.netbeans.modules.form.layoutsupport.*;
29 import org.netbeans.modules.form.codestructure.*;
30 import org.netbeans.modules.form.FormProperty;
31
32 /**
33  * Support class for BorderLayout. This is an example of support for layout
34  * manager using simple component constraints (String).
35  *
36  * @author Tran Duc Trung, Tomas Pavek, Jan Stola
37  */

38 // Expects ltr orientation of designer
39
public class BorderLayoutSupport extends AbstractLayoutSupport
40 {
41     /** Gets the supported layout manager class - BorderLayout.
42      * @return the class supported by this delegate
43      */

44     public Class JavaDoc getSupportedClass() {
45         return BorderLayout.class;
46     }
47
48     /** This method calculates layout constraints for a component dragged
49      * over a container (or just for mouse cursor being moved over container,
50      * without any component).
51      * @param container instance of a real container over/in which the
52      * component is dragged
53      * @param containerDelegate effective container delegate of the container
54      * (for layout managers we always use container delegate instead of
55      * the container)
56      * @param component the real component being dragged, can be null
57      * @param index position (index) of the component in its container;
58      * not needed for BorderLayout
59      * @param posInCont position of mouse in the container delegate
60      * @param posInComp position of mouse in the dragged component; not needed
61      * for BorderLayout
62      * @return new LayoutConstraints object corresponding to the position of
63      * the component in the container
64      */

65     public LayoutConstraints getNewConstraints(Container container,
66                                                Container containerDelegate,
67                                                Component component,
68                                                int index,
69                                                Point posInCont,
70                                                Point posInComp)
71     {
72         if (component != null && component.getParent() != containerDelegate)
73             component = null;
74
75         String JavaDoc primary = BorderLayout.CENTER;
76         String JavaDoc alternateX = null;
77         String JavaDoc alternateY = null;
78
79         int w = containerDelegate.getSize().width;
80         int h = containerDelegate.getSize().height;
81
82         Insets contInsets = containerDelegate.getInsets();
83         int marginW = getMargin(w - contInsets.left - contInsets.right);
84         int marginH = getMargin(h - contInsets.top - contInsets.bottom);
85
86         int xC = 1; // center by default (0 - left, 1 - center, 2 - right)
87
int yC = 1; // center by default (0 - top, 1 - center, 2 - bottom)
88

89         if (w > 25) {
90             if (posInCont.x < contInsets.left+marginW) xC = 0; // left
91
else if (posInCont.x >= w-marginW-contInsets.right) xC = 2; // right
92
}
93         if (h > 25) {
94             if (posInCont.y < contInsets.top+marginH) yC = 0; // top
95
else if (posInCont.y >= h-marginH-contInsets.bottom) yC = 2; // bottom
96
}
97
98         if (xC == 0) primary = BorderLayout.LINE_START;
99         else if (xC == 2) primary = BorderLayout.LINE_END;
100         else alternateX = posInCont.x - contInsets.left <
101                             (w - contInsets.left - contInsets.right)/2 ?
102                 BorderLayout.LINE_START : BorderLayout.LINE_END;
103
104         if (yC == 0) { // top
105
alternateX = primary;
106             primary = BorderLayout.PAGE_START;
107         }
108         else if (yC == 2) { // bottom
109
alternateX = primary;
110             primary = BorderLayout.PAGE_END;
111         }
112         else alternateY = posInCont.y - contInsets.top <
113                             (h - contInsets.top - contInsets.bottom)/2 ?
114                 BorderLayout.PAGE_START : BorderLayout.PAGE_END;
115
116         String JavaDoc[] suggested = new String JavaDoc[] { primary, alternateY, alternateX };
117         String JavaDoc[] free = findFreePositions();
118
119         for (int i=0; i < suggested.length; i++) {
120             String JavaDoc str = suggested[i];
121             if (str == null) continue;
122
123             for (int j=0; j < free.length; j++)
124                 if (free[j].equals(str)) {
125                     assistantParams = str;
126                     return new BorderConstraints(str);
127                 }
128
129             if (component != null) {
130                 int idx = getComponentOnPosition(str);
131                 if (containerDelegate.getComponent(idx) == component) {
132                     assistantParams = str;
133                     return new BorderConstraints(str);
134                 }
135             }
136         }
137         assistantParams = free[0];
138         return new BorderConstraints(free[0]);
139     }
140
141     private String JavaDoc assistantParams;
142     public String JavaDoc getAssistantContext() {
143         return "borderLayout"; // NOI18N
144
}
145
146     public Object JavaDoc[] getAssistantParams() {
147         return new Object JavaDoc[] {assistantParams};
148     }
149
150     /** This method paints a dragging feedback for a component dragged over
151      * a container (or just for mouse cursor being moved over container,
152      * without any component).
153      * @param container instance of a real container over/in which the
154      * component is dragged
155      * @param containerDelegate effective container delegate of the container
156      * (for layout managers we always use container delegate instead of
157      * the container)
158      * @param component the real component being dragged, can be null
159      * @param newConstraints component layout constraints to be presented
160      * @param newIndex component's index position to be presented; not used
161      * for BorderLayout
162      * @param g Graphics object for painting (with color and line style set)
163      * @return whether any feedback was painted (true in this case)
164      */

165     public boolean paintDragFeedback(Container container,
166                                      Container containerDelegate,
167                                      Component component,
168                                      LayoutConstraints newConstraints,
169                                      int newIndex,
170                                      Graphics g)
171     {
172         String JavaDoc position = (String JavaDoc) newConstraints.getConstraintsObject();
173         Component[] comps = containerDelegate.getComponents();
174         int index;
175
176         Dimension contSize = containerDelegate.getSize();
177         Insets contInsets = containerDelegate.getInsets();
178         Dimension compPrefSize =
179             component != null ? component.getPreferredSize() : new Dimension(0,0);
180
181         int x1, y1, x2, y2;
182         int marginW = getMargin(contSize.width - contInsets.left - contInsets.right);
183         int marginH = getMargin(contSize.height - contInsets.top - contInsets.bottom);
184
185         if (BorderLayout.PAGE_START.equals(position)) {
186             x1 = contInsets.left;
187             x2 = contSize.width - contInsets.right;
188             y1 = contInsets.top;
189             y2 = contInsets.top + (compPrefSize.height > 0 ?
190                                    compPrefSize.height : marginH);
191         }
192         else if (BorderLayout.PAGE_END.equals(position)) {
193             x1 = contInsets.left;
194             x2 = contSize.width - contInsets.right;
195             y1 = contSize.height - contInsets.bottom
196                    - (compPrefSize.height > 0 ? compPrefSize.height : marginH);
197             y2 = contSize.height - contInsets.bottom;
198         }
199         else { // LINE_START, LINE_END or CENTER
200
if (BorderLayout.LINE_START.equals(position)) {
201                 x1 = contInsets.left;
202                 x2 = contInsets.left + (compPrefSize.width > 0 ?
203                                         compPrefSize.width : marginW);
204             }
205             else if (BorderLayout.LINE_END.equals(position)) {
206                 x1 = contSize.width - contInsets.right
207                        - (compPrefSize.width > 0 ? compPrefSize.width : marginW);
208                 x2 = contSize.width - contInsets.right;
209             }
210             else { // CENTER
211
index = getComponentOnPosition(BorderLayout.LINE_START);
212                 x1 = contInsets.left;
213                 if (index >= 0)
214                     x1 += comps[index].getSize().width;
215
216                 index = getComponentOnPosition(BorderLayout.LINE_END);
217                 x2 = contSize.width - contInsets.right;
218                 if (index >= 0)
219                     x2 -= comps[index].getSize().width;
220             }
221
222             // y1 and y2 are the same for LINE_START, LINE_END and CENTER
223
index = getComponentOnPosition(BorderLayout.PAGE_START);
224             y1 = contInsets.top;
225             if (index >= 0)
226                 y1 += comps[index].getSize().height;
227
228             index = getComponentOnPosition(BorderLayout.PAGE_END);
229             y2 = contSize.height - contInsets.bottom;
230             if (index >= 0)
231                 y2 -= comps[index].getSize().height;
232         }
233
234         if (x1 >= x2) {
235             x1 = contInsets.left;
236             x2 = contSize.width - contInsets.right;
237             if (x1 >= x2) return true; // container is too small
238
}
239         if (y1 >= y2) {
240             y1 = contInsets.top;
241             x2 = contSize.height - contInsets.bottom;
242             if (y1 >= y2) return true; // container is too small
243
}
244
245         g.drawRect(x1, y1, x2-x1-1, y2-y1-1);
246
247         return true;
248     }
249
250     // ----------
251

252     /** This method is called from readComponentCode method to read layout
253      * constraints of a component from code. It is just a simple String for
254      * BorderLayout.
255      * @param constrExp CodeExpression object of the constraints (taken from
256      * add method in the code)
257      * @param constrCode CodeGroup to be filled with the relevant constraints
258      * initialization code; not needed here because String is just
259      * a single code expression
260      * @param compExp CodeExpression of the component for which the constraints
261      * are read (not needed here)
262      * @return LayoutConstraints based on information read form code
263      */

264     protected LayoutConstraints readConstraintsCode(CodeExpression constrExp,
265                                                     CodeGroup constrCode,
266                                                     CodeExpression compExp)
267     {
268         BorderConstraints constr = new BorderConstraints(BorderLayout.CENTER);
269         FormCodeSupport.readPropertyExpression(constrExp,
270                                                constr.getProperties()[0],
271                                                false);
272         return constr;
273     }
274
275     /** Called from createComponentCode method, creates code for a component
276      * layout constraints (opposite to readConstraintsCode).
277      * @param constrCode CodeGroup to be filled with constraints code; not
278      * needed here String (used as the constraints object) is just
279      * a single code expression
280      * @param constr layout constraints metaobject representing the constraints
281      * @param compExp CodeExpression object representing the component; not
282      * needed here
283      * @return created CodeExpression representing the layout constraints
284      */

285     protected CodeExpression createConstraintsCode(CodeGroup constrCode,
286                                                    LayoutConstraints constr,
287                                                    CodeExpression compExp,
288                                                    int index)
289     {
290         if (!(constr instanceof BorderConstraints))
291             return null; // should not happen
292

293         return getCodeStructure().createExpression(
294                    FormCodeSupport.createOrigin(constr.getProperties()[0]));
295     }
296
297     /** This method is called to get a default component layout constraints
298      * metaobject in case it is not provided (e.g. in addComponents method).
299      * @return the default LayoutConstraints object for the supported layout
300      */

301     protected LayoutConstraints createDefaultConstraints() {
302         return new BorderConstraints(findFreePositions()[0]);
303     }
304
305     // ----------------
306

307     private String JavaDoc[] findFreePositions() {
308         ArrayList positions = new ArrayList(6);
309
310         if (getComponentOnPosition(BorderLayout.CENTER) == -1)
311             positions.add(BorderLayout.CENTER);
312         if (getComponentOnPosition(BorderLayout.PAGE_START) == -1)
313             positions.add(BorderLayout.PAGE_START);
314         if (getComponentOnPosition(BorderLayout.PAGE_END) == -1)
315             positions.add(BorderLayout.PAGE_END);
316         if (getComponentOnPosition(BorderLayout.LINE_END) == -1)
317             positions.add(BorderLayout.LINE_END);
318         if (getComponentOnPosition(BorderLayout.LINE_START) == -1)
319             positions.add(BorderLayout.LINE_START);
320         if (positions.size() == 0)
321             positions.add(BorderLayout.CENTER);
322
323         String JavaDoc[] free = new String JavaDoc[positions.size()];
324         positions.toArray(free);
325         return free;
326     }
327
328     private int getComponentOnPosition(String JavaDoc position) {
329         java.util.List JavaDoc constraints = getConstraintsList();
330         if (constraints == null)
331             return -1;
332         
333         position = (String JavaDoc)toAbsolute(position);
334         for (int i=0, n=constraints.size(); i < n; i++) {
335             LayoutConstraints constr = (LayoutConstraints) constraints.get(i);
336             if (constr != null && position.equals(toAbsolute(constr.getConstraintsObject())))
337                 return i;
338         }
339
340         return -1;
341     }
342
343     private static Object JavaDoc toAbsolute(Object JavaDoc constraint) {
344         if (BorderLayout.LINE_START.equals(constraint)) {
345             constraint = BorderLayout.WEST;
346         } else if (BorderLayout.LINE_END.equals(constraint)) {
347             constraint = BorderLayout.EAST;
348         } else if (BorderLayout.PAGE_START.equals(constraint)) {
349             constraint = BorderLayout.NORTH;
350         } else if (BorderLayout.PAGE_END.equals(constraint)) {
351             constraint = BorderLayout.SOUTH;
352         }
353         return constraint;
354     }
355
356     private int getMargin(int size) {
357         int margin = size/8;
358         if (margin < 10) margin = 10;
359         if (margin > 50) margin = 50;
360         return margin;
361     }
362
363     // ----------------
364

365     /** LayoutConstraints implementation class for component constraints of
366      * BorderLayout.
367      */

368     public static class BorderConstraints implements LayoutConstraints {
369         private String JavaDoc direction;
370
371         private Node.Property[] properties;
372
373         public BorderConstraints(String JavaDoc direction) {
374             this.direction = direction;
375         }
376
377         public Node.Property[] getProperties() {
378             if (properties == null) {
379                 properties = new FormProperty[] {
380                     new FormProperty(
381                             "BorderConstraints direction", // NOI18N
382
String JavaDoc.class,
383                             getBundle().getString("PROP_direction"), // NOI18N
384
getBundle().getString("HINT_direction")) // NOI18N
385
{
386                         public Object JavaDoc getTargetValue() {
387                             return direction;
388                         }
389
390                         public void setTargetValue(Object JavaDoc value) {
391                             direction = (String JavaDoc)value;
392                         }
393
394                         public PropertyEditor getExpliciteEditor() {
395                             return new BorderDirectionEditor();
396                         }
397                         public void setPropertyContext(
398                             org.netbeans.modules.form.FormPropertyContext ctx)
399                         { // disabling this method due to limited persistence
400
} // capabilities (compatibility with previous versions)
401
}
402                 };
403                 properties[0].setValue("NOI18N", Boolean.TRUE); // NOI18N
404
}
405
406             return properties;
407         }
408
409         public Object JavaDoc getConstraintsObject() {
410             return direction;
411         }
412
413         public LayoutConstraints cloneConstraints() {
414             return new BorderConstraints(direction);
415         }
416     }
417
418     // ---------
419

420     /** PropertyEditor for the BorderLayout constraints property.
421      */

422     static class BorderDirectionEditor extends PropertyEditorSupport {
423         private final String JavaDoc[] values = {
424             BorderLayout.CENTER,
425             BorderLayout.LINE_START,
426             BorderLayout.LINE_END,
427             BorderLayout.PAGE_START,
428             BorderLayout.PAGE_END,
429             BorderLayout.WEST,
430             BorderLayout.EAST,
431             BorderLayout.NORTH,
432             BorderLayout.SOUTH
433         };
434         private final String JavaDoc[] javaInitStrings = {
435             "java.awt.BorderLayout.CENTER", // NOI18N
436
"java.awt.BorderLayout.LINE_START", // NOI18N
437
"java.awt.BorderLayout.LINE_END", // NOI18N
438
"java.awt.BorderLayout.PAGE_START", // NOI18N
439
"java.awt.BorderLayout.PAGE_END", // NOI18N
440
"java.awt.BorderLayout.WEST", // NOI18N
441
"java.awt.BorderLayout.EAST", // NOI18N
442
"java.awt.BorderLayout.NORTH", // NOI18N
443
"java.awt.BorderLayout.SOUTH" // NOI18N
444
};
445
446         public String JavaDoc[] getTags() {
447             return values;
448         }
449
450         public String JavaDoc getAsText() {
451             return (String JavaDoc)getValue();
452         }
453
454         public void setAsText(String JavaDoc str) {
455             for (int i = 0; i < values.length; i++)
456                 if (str.equals(values[i])) {
457                     setValue(str);
458                     break;
459                 }
460         }
461
462         public String JavaDoc getJavaInitializationString() {
463             Object JavaDoc value = getValue();
464             for (int i=0; i < values.length; i++)
465                 if (values[i].equals(value))
466                     return javaInitStrings[i];
467             return null;
468         }
469     }
470 }
471
Popular Tags