KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > swingwtx > swing > BoxLayout


1 /*
2    SwingWT
3    Copyright(c)2003-2004, R. Rawson-Tetley
4  
5    For more information on distributing and using this program, please
6    see the accompanying "COPYING" file.
7  
8    Contact me by electronic mail: bobintetley@users.sourceforge.net
9  
10    $Log: BoxLayout.java,v $
11    Revision 1.6 2004/05/05 12:43:21 bobintetley
12    Patches/new files from Laurent Martell
13
14    Revision 1.5 2004/04/20 16:36:14 bobintetley
15    Code cleanup
16
17    Revision 1.4 2004/03/30 14:22:27 bobintetley
18    Fix to Component min/max sizing code, fix to JPanel insets with titled
19    borders (all of which indirectly fix problems with BoxLayout). Addition
20    of ComponentAdapter
21
22    Revision 1.3 2004/01/26 08:11:00 bobintetley
23    Many bugfixes and addition of SwingSet
24
25    Revision 1.2 2004/01/06 08:28:02 bobintetley
26    Header fixes
27
28  
29  */

30 package swingwtx.swing;
31
32 import swingwt.awt.*;
33
34 public class BoxLayout implements LayoutManager2 {
35     
36     public static final int X_AXIS = 0;
37     public static final int Y_AXIS = 1;
38     public static final int LINE_AXIS = 2;
39     public static final int PAGE_AXIS = 3;
40     
41     protected SizeRequirements[] childWidth;
42     protected SizeRequirements[] childHeight;
43     protected SizeRequirements totalWidth;
44     protected SizeRequirements totalHeight;
45     
46     private int axis;
47     private Component target = null;
48     
49     public BoxLayout(Component target, int axis) {
50         if (axis != X_AXIS && axis != Y_AXIS && axis != LINE_AXIS && axis != PAGE_AXIS) {
51             throw new Error JavaDoc("Invalid axis");
52         }
53         this.axis = axis;
54         this.target = target;
55     }
56     
57     public BoxLayout(Container target, int axis) {
58         this((Component) target, axis);
59     }
60     
61     public synchronized void invalidateLayout(Container target) {
62         childWidth = null;
63         childHeight = null;
64         totalWidth = null;
65         totalHeight = null;
66     }
67     
68     public void addLayoutComponent(String JavaDoc name, Component comp) {
69     }
70     
71     public void removeLayoutComponent(Component comp) {
72     }
73     
74     public void addLayoutComponent(Component comp, Object JavaDoc constraints) {
75     }
76     
77     public Dimension preferredLayoutSize(Container target) {
78
79         calculate();
80         Dimension size = null;
81         if (totalWidth == null)
82             size = new Dimension(0, 0);
83         else
84             size = new Dimension(totalWidth.preferred, totalHeight.preferred);
85         
86         Insets insets = target.getInsets();
87         size.width = (int) Math.min( (long) size.width + (long) insets.left + (long) insets.right, Integer.MAX_VALUE);
88         size.height = (int) Math.min( (long) size.height + (long) insets.top + (long) insets.bottom, Integer.MAX_VALUE);
89         return size;
90     }
91     
92     public Dimension minimumLayoutSize(Container target) {
93
94         calculate();
95         Dimension size = new Dimension(totalWidth.minimum, totalHeight.minimum);
96         
97         Insets insets = target.getInsets();
98         size.width =
99         (int) Math.min( (long) size.width + (long) insets.left + (long) insets.right, Integer.MAX_VALUE);
100         size.height = (int) Math.min( (long) size.height + (long) insets.top + (long) insets.bottom, Integer.MAX_VALUE);
101         return size;
102     }
103     
104     public Dimension maximumLayoutSize(Container target) {
105         
106         calculate();
107         Dimension size = new Dimension(totalWidth.maximum, totalHeight.maximum);
108         
109         Insets insets = target.getInsets();
110         size.width =
111         (int) Math.min( (long) size.width + (long) insets.left + (long) insets.right, Integer.MAX_VALUE);
112         size.height = (int) Math.min( (long) size.height + (long) insets.top + (long) insets.bottom, Integer.MAX_VALUE);
113         return size;
114     }
115     
116     public synchronized float getLayoutAlignmentX(Container target) {
117         calculate();
118         return totalWidth.alignment;
119     }
120     
121     public synchronized float getLayoutAlignmentY(Container target) {
122         calculate();
123         return totalHeight.alignment;
124     }
125     
126     public void layoutContainer(Container target) {
127         
128         int nChildren = target.getComponentCount();
129         
130         int[] xOffsets = new int[nChildren];
131         int[] xSpans = new int[nChildren];
132         int[] yOffsets = new int[nChildren];
133         int[] ySpans = new int[nChildren];
134         
135         Dimension alloc = target.getSize();
136         Insets in = target.getInsets();
137         alloc.width -= in.left + in.right;
138         alloc.height -= in.top + in.bottom;
139         
140         
141         int absoluteAxis = X_AXIS;
142
143         if (axis == LINE_AXIS)
144             absoluteAxis = target.getComponentOrientation().isHorizontal() ? X_AXIS : Y_AXIS;
145         else if (axis == PAGE_AXIS)
146             absoluteAxis = target.getComponentOrientation().isHorizontal() ? Y_AXIS : X_AXIS;
147         else
148             absoluteAxis = axis;
149         
150         boolean ltr = (absoluteAxis != axis) ? target.getComponentOrientation().isLeftToRight() : true;
151         
152         // Determine where children go
153
calculate();
154
155         nChildren = childWidth.length;
156         xOffsets = new int[nChildren];
157         xSpans = new int[nChildren];
158         yOffsets = new int[nChildren];
159         ySpans = new int[nChildren];
160
161         if (absoluteAxis == X_AXIS) {
162             SizeRequirements.calculateAlignedPositions(alloc.height, totalHeight, childHeight, yOffsets, ySpans);
163             SizeRequirements.calculateTiledPositions(alloc.width, totalWidth, childWidth, xOffsets, xSpans, ltr);
164         }
165         else {
166             SizeRequirements.calculateTiledPositions(alloc.height, totalHeight, childHeight, yOffsets, ySpans);
167             SizeRequirements.calculateAlignedPositions(alloc.width, totalWidth, childWidth, xOffsets, xSpans, ltr);
168         }
169         
170         for (int i = 0; i < nChildren; i++) {
171             Component c = target.getComponent(i);
172             c.setBounds(
173             (int) Math.min( (long) in.left + (long) xOffsets[i], Integer.MAX_VALUE),
174             (int) Math.min( (long) in.top + (long) yOffsets[i], Integer.MAX_VALUE),
175             xSpans[i],
176             ySpans[i]);
177             
178         }
179     }
180     
181     protected void calculate() {
182         if (!(target instanceof Container))
183             return;
184         Container tg = (Container) target;
185         int n = tg.getComponentCount();
186         childWidth = new SizeRequirements[n];
187         childHeight = new SizeRequirements[n];
188         for (int i = 0; i < n; i++) {
189             Component c = tg.getComponent(i);
190             if (!c.isVisible()) {
191                 childWidth[i] =
192                 new SizeRequirements(0, 0, 0, c.getAlignmentX());
193                 childHeight[i] =
194                 new SizeRequirements(0, 0, 0, c.getAlignmentY());
195                 continue;
196             }
197             Dimension min = c.getMinimumSize();
198             Dimension typ = c.getPreferredSize();
199             Dimension max = c.getMaximumSize();
200             childWidth[i] =
201             new SizeRequirements(
202             min.width,
203             typ.width,
204             max.width,
205             c.getAlignmentX());
206             childHeight[i] =
207             new SizeRequirements(
208             min.height,
209             typ.height,
210             max.height,
211             c.getAlignmentY());
212         }
213
214         int absoluteAxis = X_AXIS;
215
216         if (axis == LINE_AXIS)
217             absoluteAxis = tg.getComponentOrientation().isHorizontal() ? X_AXIS : Y_AXIS;
218         else if (axis == PAGE_AXIS)
219             absoluteAxis = tg.getComponentOrientation().isHorizontal() ? Y_AXIS : X_AXIS;
220         else
221             absoluteAxis = axis;
222
223         if (absoluteAxis == X_AXIS) {
224             totalWidth = SizeRequirements.getTiledSizeRequirements(childWidth);
225             totalHeight = SizeRequirements.getAlignedSizeRequirements(childHeight);
226         } else {
227             totalWidth = SizeRequirements.getAlignedSizeRequirements(childWidth);
228             totalHeight = SizeRequirements.getTiledSizeRequirements(childHeight);
229         }
230     }
231     
232 }
233
Popular Tags