KickJava   Java API By Example, From Geeks To Geeks.

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


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

31
32 public class FlowLayoutSupport extends AbstractLayoutSupport
33 {
34     /** Gets the supported layout manager class - FlowLayout.
35      * @return the class supported by this delegate
36      */

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

58     public int getNewIndex(Container container,
59                            Container containerDelegate,
60                            Component component,
61                            int index,
62                            Point posInCont,
63                            Point posInComp)
64     {
65         if (!(containerDelegate.getLayout() instanceof FlowLayout))
66             return -1;
67
68         int vgap = ((FlowLayout) containerDelegate.getLayout()).getVgap();
69         Component[] components = containerDelegate.getComponents();
70         int[] rowStarts = new int[components.length + 1];
71         int[] rowTops = new int[components.length + 1];
72         for (int i = 0; i < rowStarts.length; i++) {
73             rowStarts[i] = -1;
74         }
75
76         // rowStarts keeps indices of the first components on each row
77
// rowTops keeps y-position of each row
78

79         int lastX = Integer.MAX_VALUE;
80         int rowHeight = - vgap;
81         int r = 0;
82         int i = 0;
83
84         int compIndex = -1;
85         assistantParams = 0;
86         if ((components.length > 1) || (component.getParent() != containerDelegate)) {
87             for (int j = 0; j < components.length; j++) {
88                 Component comp = components[j];
89                 if (comp == component) {
90                     compIndex = j;
91                     comp = components[(j == 0) ? 1 : j-1];
92                 }
93                 int posX = comp.getBounds().x;
94                 if (posX < lastX) {
95                     rowStarts[r] = j;
96                     rowTops[r] = rowHeight + vgap;
97                     rowTops[r] += r > 0 ? rowTops[r-1] :
98                                           containerDelegate.getInsets().top;
99                     r++;
100                     rowHeight = 0;
101                 }
102                 rowHeight = Math.max(rowHeight, comp.getSize().height);
103                 lastX = posX;
104             }
105             if (r > 0) {
106                 rowTops[r] = rowTops[r-1] + rowHeight + vgap;
107             }
108
109             // find which row the pointer falls in
110

111             r = 0;
112             while (rowStarts[i] >= 0) {
113                 if (posInCont.y < rowTops[i]) {
114                     r = i - 1;
115                     break;
116                 }
117                 i++;
118             }
119
120             if (rowStarts[i] < 0) {
121                 if (posInCont.y >= rowTops[i]) {
122                     if (component.getParent() == containerDelegate) assistantParams--;
123                     assistantParams += components.length;
124                     return components.length;
125                 }
126                 else {
127                     r = i - 1;
128                 }
129             }
130
131             int m = (r <= 0) ? 0 : rowStarts[r];
132             if ((compIndex > -1) && (compIndex < m)) assistantParams--;
133             int n = rowStarts[r + 1];
134
135             if (n > components.length || n < 0)
136                 n = components.length;
137
138             for (i = m; i < n; i++) {
139                 Component comp = components[i];
140                 if (comp == component) {
141                     assistantParams--;
142                     comp = components[(i == 0) ? 1 : i-1];
143                 }
144                 Rectangle bounds = comp.getBounds();
145                 int centerX = bounds.x + bounds.width / 2;
146                 if (posInCont.x < centerX)
147                     break;
148             }
149
150             i = i < n ? i : n;
151         }
152         assistantParams += i;
153         return i;
154     }
155
156     private int assistantParams;
157     public String JavaDoc getAssistantContext() {
158         return "flowLayout"; // NOI18N
159
}
160
161     public Object JavaDoc[] getAssistantParams() {
162         return new Object JavaDoc[] {Integer.valueOf(assistantParams+1)};
163     }
164
165     /** This method paints a dragging feedback for a component dragged over
166      * a container (or just for mouse cursor being moved over container,
167      * without any component).
168      * @param container instance of a real container over/in which the
169      * component is dragged
170      * @param containerDelegate effective container delegate of the container
171      * (for layout managers we always use container delegate instead of
172      * the container)
173      * @param component the real component being dragged, not needed here
174      * @param newConstraints component layout constraints to be presented;
175      * not used for FlowLayout
176      * @param newIndex component's index position to be presented
177      * @param g Graphics object for painting (with color and line style set)
178      * @return whether any feedback was painted (true in this case)
179      */

180     public boolean paintDragFeedback(Container container,
181                                      Container containerDelegate,
182                                      Component component,
183                                      LayoutConstraints newConstraints,
184                                      int newIndex,
185                                      Graphics g)
186     {
187         if (!(containerDelegate.getLayout() instanceof FlowLayout))
188             return false;
189         
190         Component[] components = containerDelegate.getComponents();
191         int alignment = ((FlowLayout) containerDelegate.getLayout()).getAlignment();
192         int hgap = ((FlowLayout) containerDelegate.getLayout()).getHgap();
193         int draggedIndex = -1;
194         if (component.getParent() == containerDelegate) {
195             for (int i=0; i<components.length; i++) {
196                 if (component == components[i]) {
197                     draggedIndex = i;
198                 }
199             }
200         }
201
202         int x = 0, y1 = 0, y2 = 0;
203         
204         if ((newIndex <= 0) || ((components.length == 1) && (draggedIndex != -1))) {
205             if ((components.length == 0) || ((components.length == 1) && (draggedIndex != -1))) {
206                 if (alignment == FlowLayout.RIGHT) {
207                     x = containerDelegate.getSize().width;
208                 }
209                 else if (alignment == FlowLayout.LEFT) {
210                     x = 0;
211                 }
212                 else {
213                     x = containerDelegate.getSize().width / 2 - 5;
214                 }
215                 y1 = 0;
216                 y2 = component.getHeight();
217             }
218             else {
219                 Rectangle b = components[(draggedIndex == 0) ? 1 : 0].getBounds();
220                 x = b.x;
221                 y1 = b.y;
222                 y2 = b.y + component.getHeight();
223             }
224         }
225         else if ((newIndex >= components.length) ||
226             ((newIndex == components.length - 1) && (newIndex == draggedIndex))) {
227             int last = components.length - 1;
228             Rectangle b = components[(last == draggedIndex) ? last-1 : last].getBounds();
229             x = b.x + b.width;
230             y1 = b.y;
231             y2 = b.y + component.getHeight();
232         }
233         else {
234             Rectangle b = components[(newIndex == draggedIndex) ? newIndex+1 : newIndex].getBounds();
235             x = b.x;
236             y1 = b.y;
237             y2 = b.y + component.getHeight();
238         }
239         g.drawRect(x - 10 - hgap / 2, y1, 20, y2 - y1);
240         return true;
241     }
242 }
243
Popular Tags