KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.*;
24 import org.netbeans.modules.form.layoutsupport.*;
25 import org.netbeans.modules.form.codestructure.*;
26
27 /**
28  * Support class for GridLayout. This is an example of very simple layout
29  * with no constraints; just basic drag & drop is implemented.
30  *
31  * @author Tran Duc Trung, Tomas Pavek
32  */

33
34 public class GridLayoutSupport extends AbstractLayoutSupport
35 {
36     /** Gets the supported layout manager class - GridLayout.
37      * @return the class supported by this delegate
38      */

39     public Class JavaDoc getSupportedClass() {
40         return GridLayout.class;
41     }
42
43     /** This method calculates position (index) for a component dragged
44      * over a container (or just for mouse cursor being moved over container,
45      * without any component).
46      * @param container instance of a real container over/in which the
47      * component is dragged
48      * @param containerDelegate effective container delegate of the container
49      * (for layout managers we always use container delegate instead of
50      * the container)
51      * @param component the real component being dragged; not needed here
52      * @param index position (index) of the component in its current container;
53      * not needed here
54      * @param posInCont position of mouse in the container delegate
55      * @param posInComp position of mouse in the dragged component;
56      * not needed here
57      * @return index corresponding to the position of the component in the
58      * container
59      */

60     public int getNewIndex(Container container,
61                            Container containerDelegate,
62                            Component component,
63                            int index,
64                            Point posInCont,
65                            Point posInComp)
66     {
67         if (!(containerDelegate.getLayout() instanceof GridLayout))
68             return -1;
69
70         Component[] components = containerDelegate.getComponents();
71         GridLayout layout = (GridLayout) containerDelegate.getLayout();
72         int nrows = layout.getRows();
73         int ncols = layout.getColumns();
74
75         if ((nrows <= 0 && ncols <= 0) || components.length == 0)
76             return components.length;
77         
78         if (nrows != 0)
79             ncols = (components.length + nrows - 1) / nrows;
80         else
81             nrows = (components.length + ncols - 1) / ncols;
82
83         Dimension sz = containerDelegate.getSize();
84         Insets insets = containerDelegate.getInsets();
85         sz.width -= insets.left + insets.right;
86         sz.height -= insets.top + insets.bottom;
87
88         int colwidth = sz.width / ncols;
89         if (colwidth <= 0) {
90             assistantParams = components.length;
91             return components.length;
92         }
93         int col = (posInCont.x - insets.left + colwidth / 2) / colwidth;
94         
95         int rowheight = sz.height / nrows;
96         if (rowheight <= 0) {
97             assistantParams = components.length;
98             return components.length;
99         }
100         int row = (posInCont.y - insets.top) / rowheight;
101
102         int newIndex = row * ncols + col;
103         newIndex = newIndex >= components.length ? components.length : newIndex;
104         assistantParams = newIndex;
105         return newIndex;
106     }
107
108     private int assistantParams;
109     public String JavaDoc getAssistantContext() {
110         return "gridLayout"; // NOI18N
111
}
112
113     public Object JavaDoc[] getAssistantParams() {
114         return new Object JavaDoc[] {Integer.valueOf(assistantParams+1)};
115     }
116
117     /** This method paints a dragging feedback for a component dragged over
118      * a container (or just for mouse cursor being moved over container,
119      * without any component).
120      * @param container instance of a real container over/in which the
121      * component is dragged
122      * @param containerDelegate effective container delegate of the container;
123      * for layout managers we always use container delegate instead of
124      * the container
125      * @param component the real component being dragged, not needed here
126      * @param newConstraints component layout constraints to be presented;
127      * not used for GridLayout
128      * @param newIndex component's index position to be presented
129      * @param g Graphics object for painting (with color and line style set)
130      * @return whether any feedback was painted (true in this case)
131      */

132     public boolean paintDragFeedback(Container container,
133                                      Container containerDelegate,
134                                      Component component,
135                                      LayoutConstraints newConstraints,
136                                      int newIndex,
137                                      Graphics g)
138     {
139         if (!(containerDelegate.getLayout() instanceof GridLayout))
140             return false;
141
142         Component[] components = containerDelegate.getComponents();
143         GridLayout layout = (GridLayout) containerDelegate.getLayout();
144         int dx = 12 + layout.getHgap() / 2;
145         int x = 0, w = 24, y = 0, h = 0;
146         
147         if ((newIndex <= 0) || ((components.length == 1) && (components[0] == component))) {
148             if ((components.length > 1) || ((components.length == 1) && (components[0] != component))) {
149                 Component comp = components[0];
150                 if (comp == component) {
151                     comp = components[1];
152                 }
153                 Rectangle b = comp.getBounds();
154                 x = b.x - dx ;
155                 y = b.y;
156                 h = b.height;
157             }
158             else {
159                 Insets ins = containerDelegate.getInsets();
160                 x = ins.left + 1;
161                 w = containerDelegate.getWidth() - ins.right - ins.left - 2;
162                 y = ins.top + 1;
163                 h = containerDelegate.getHeight() - ins.bottom - ins.top - 2;
164             }
165         }
166         else if ((newIndex >= components.length) ||
167             ((newIndex == components.length - 1) && (components[newIndex] == component))) {
168             Component comp = components[components.length-1];
169             if (comp == component) {
170                 comp = components[components.length-2];
171             }
172             Rectangle b = comp.getBounds();
173             x = b.x + b.width - dx;
174             y = b.y;
175             h = b.height;
176         }
177         else {
178             Component comp = components[newIndex];
179             if (comp == component) {
180                 comp = components[newIndex+1];
181             }
182             Rectangle b = comp.getBounds();
183             x = b.x - dx;
184             y = b.y;
185             h = b.height;
186         }
187
188         g.drawRect(x, y, w, h);
189         return true;
190     }
191
192     // ------------
193

194     /** This method is called from readLayoutCode to read the layout manager
195      * bean code (i.e. code for constructor and properties). This method is
196      * overridden here because "rows" and "columns" properties are mutually
197      * dependent (so not true JavaBean properties).
198      * @param layoutExp CodeExpressin of the layout manager
199      * @param initLayoutCode CodeGroup to be filled with relevant
200      * initialization code
201      */

202     protected void readInitLayoutCode(CodeExpression layoutExp,
203                                       CodeGroup initLayoutCode)
204     {
205         CodeExpression[] params = layoutExp.getOrigin().getCreationParameters();
206         if (params.length > 0) {
207             Object JavaDoc rowsValue = params[0].getOrigin().getValue();
208             if (rowsValue instanceof Integer JavaDoc
209                 && ((Integer JavaDoc)rowsValue).intValue() == 0)
210             { // number of rows is to be set to 0, we must preset
211
// columns property to something else than 0
212
try {
213                     getProperty("columns").setValue(new Integer JavaDoc(1));
214                 }
215                 catch (Exception JavaDoc ex) {} // ignore
216
}
217         }
218
219         super.readInitLayoutCode(layoutExp, initLayoutCode);
220     }
221 }
222
Popular Tags