1 | package org.greenstone.gs3client;
|
---|
2 | import java.awt.*;
|
---|
3 | import java.util.Hashtable;
|
---|
4 |
|
---|
5 | /**
|
---|
6 | * The <code>GraphPaperLayout</code> class is a layout manager that
|
---|
7 | * lays out a container's components in a rectangular grid, similar
|
---|
8 | * to GridLayout. Unlike GridLayout, however, components can take
|
---|
9 | * up multiple rows and/or columns. The layout manager acts as a
|
---|
10 | * sheet of graph paper. When a component is added to the layout
|
---|
11 | * manager, the location and relative size of the component are
|
---|
12 | * simply supplied by the constraints as a Rectangle.
|
---|
13 | * <p><code><pre>
|
---|
14 | * import java.awt.*;
|
---|
15 | * import java.applet.Applet;
|
---|
16 | * public class ButtonGrid extends Applet {
|
---|
17 | * public void init() {
|
---|
18 | * setLayout(new GraphPaperLayout(new Dimension(5,5)));
|
---|
19 | * // Add a 1x1 Rect at (0,0)
|
---|
20 | * add(new Button("1"), new Rectangle(0,0,1,1));
|
---|
21 | * // Add a 2x1 Rect at (2,0)
|
---|
22 | * add(new Button("2"), new Rectangle(2,0,2,1));
|
---|
23 | * // Add a 1x2 Rect at (1,1)
|
---|
24 | * add(new Button("3"), new Rectangle(1,1,1,2));
|
---|
25 | * // Add a 2x2 Rect at (3,2)
|
---|
26 | * add(new Button("4"), new Rectangle(3,2,2,2));
|
---|
27 | * // Add a 1x1 Rect at (0,4)
|
---|
28 | * add(new Button("5"), new Rectangle(0,4,1,1));
|
---|
29 | * // Add a 1x2 Rect at (2,3)
|
---|
30 | * add(new Button("6"), new Rectangle(2,3,1,2));
|
---|
31 | * }
|
---|
32 | * }
|
---|
33 | * </pre></code>
|
---|
34 | *
|
---|
35 | * @author Michael Martak
|
---|
36 | */
|
---|
37 |
|
---|
38 | public class GraphPaperLayout implements LayoutManager2 {
|
---|
39 | int hgap; //horizontal gap
|
---|
40 | int vgap; //vertical gap
|
---|
41 | Dimension gridSize; //grid size in logical units (n x m)
|
---|
42 | Hashtable compTable; //constraints (Rectangles)
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Creates a graph paper layout with a default of a 1 x 1 graph, with no
|
---|
46 | * vertical or horizontal padding.
|
---|
47 | */
|
---|
48 | public GraphPaperLayout() {
|
---|
49 | this(new Dimension(1,1));
|
---|
50 | }
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * Creates a graph paper layout with the given grid size, with no vertical
|
---|
54 | * or horizontal padding.
|
---|
55 | */
|
---|
56 | public GraphPaperLayout(Dimension gridSize) {
|
---|
57 | this(gridSize, 0, 0);
|
---|
58 | }
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Creates a graph paper layout with the given grid size and padding.
|
---|
62 | * @param gridSize size of the graph paper in logical units (n x m)
|
---|
63 | * @param hgap horizontal padding
|
---|
64 | * @param vgap vertical padding
|
---|
65 | */
|
---|
66 | public GraphPaperLayout(Dimension gridSize, int hgap, int vgap) {
|
---|
67 | if ((gridSize.width <= 0) || (gridSize.height <= 0)) {
|
---|
68 | throw new IllegalArgumentException(
|
---|
69 | "dimensions must be greater than zero");
|
---|
70 | }
|
---|
71 | this.gridSize = new Dimension(gridSize);
|
---|
72 | this.hgap = hgap;
|
---|
73 | this.vgap = vgap;
|
---|
74 | compTable = new Hashtable();
|
---|
75 | }
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * @return the size of the graph paper in logical units (n x m)
|
---|
79 | */
|
---|
80 | public Dimension getGridSize() {
|
---|
81 | return new Dimension( gridSize );
|
---|
82 | }
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * Set the size of the graph paper in logical units (n x m)
|
---|
86 | */
|
---|
87 | public void setGridSize( Dimension d ) {
|
---|
88 | setGridSize( d.width, d.height );
|
---|
89 | }
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * Set the size of the graph paper in logical units (n x m)
|
---|
93 | */
|
---|
94 | public void setGridSize( int width, int height ) {
|
---|
95 | gridSize = new Dimension( width, height );
|
---|
96 | }
|
---|
97 |
|
---|
98 | public void setConstraints(Component comp, Rectangle constraints) {
|
---|
99 | compTable.put(comp, new Rectangle(constraints));
|
---|
100 | }
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * Adds the specified component with the specified name to
|
---|
104 | * the layout. This does nothing in GraphPaperLayout, since constraints
|
---|
105 | * are required.
|
---|
106 | */
|
---|
107 | public void addLayoutComponent(String name, Component comp) {
|
---|
108 | }
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * Removes the specified component from the layout.
|
---|
112 | * @param comp the component to be removed
|
---|
113 | */
|
---|
114 | public void removeLayoutComponent(Component comp) {
|
---|
115 | compTable.remove(comp);
|
---|
116 | }
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * Calculates the preferred size dimensions for the specified
|
---|
120 | * panel given the components in the specified parent container.
|
---|
121 | * @param parent the component to be laid out
|
---|
122 | *
|
---|
123 | * @see #minimumLayoutSize
|
---|
124 | */
|
---|
125 | public Dimension preferredLayoutSize(Container parent) {
|
---|
126 | return getLayoutSize(parent, true);
|
---|
127 | }
|
---|
128 |
|
---|
129 | /**
|
---|
130 | * Calculates the minimum size dimensions for the specified
|
---|
131 | * panel given the components in the specified parent container.
|
---|
132 | * @param parent the component to be laid out
|
---|
133 | * @see #preferredLayoutSize
|
---|
134 | */
|
---|
135 | public Dimension minimumLayoutSize(Container parent) {
|
---|
136 | return getLayoutSize(parent, false);
|
---|
137 | }
|
---|
138 |
|
---|
139 | /**
|
---|
140 | * Algorithm for calculating layout size (minimum or preferred).
|
---|
141 | * <p>
|
---|
142 | * The width of a graph paper layout is the largest cell width
|
---|
143 | * (calculated in <code>getLargestCellSize()</code> times the number of
|
---|
144 | * columns, plus the horizontal padding times the number of columns
|
---|
145 | * plus one, plus the left and right insets of the target container.
|
---|
146 | * <p>
|
---|
147 | * The height of a graph paper layout is the largest cell height
|
---|
148 | * (calculated in <code>getLargestCellSize()</code> times the number of
|
---|
149 | * rows, plus the vertical padding times the number of rows
|
---|
150 | * plus one, plus the top and bottom insets of the target container.
|
---|
151 | *
|
---|
152 | * @param parent the container in which to do the layout.
|
---|
153 | * @param isPreferred true for calculating preferred size, false for
|
---|
154 | * calculating minimum size.
|
---|
155 | * @return the dimensions to lay out the subcomponents of the specified
|
---|
156 | * container.
|
---|
157 | * @see GraphPaperLayout#getLargestCellSize
|
---|
158 | */
|
---|
159 | protected Dimension getLayoutSize(Container parent, boolean isPreferred) {
|
---|
160 | Dimension largestSize = getLargestCellSize(parent, isPreferred);
|
---|
161 | Insets insets = parent.getInsets();
|
---|
162 | largestSize.width = ( largestSize.width * gridSize.width ) +
|
---|
163 | ( hgap * ( gridSize.width + 1 ) ) + insets.left + insets.right;
|
---|
164 | largestSize.height = ( largestSize.height * gridSize.height ) +
|
---|
165 | ( vgap * ( gridSize.height + 1 ) ) + insets.top + insets.bottom;
|
---|
166 | return largestSize;
|
---|
167 | }
|
---|
168 |
|
---|
169 | /**
|
---|
170 | * Algorithm for calculating the largest minimum or preferred cell size.
|
---|
171 | * <p>
|
---|
172 | * Largest cell size is calculated by getting the applicable size of each
|
---|
173 | * component and keeping the maximum value, dividing the component's width
|
---|
174 | * by the number of columns it is specified to occupy and dividing the
|
---|
175 | * component's height by the number of rows it is specified to occupy.
|
---|
176 | *
|
---|
177 | * @param parent the container in which to do the layout.
|
---|
178 | * @param isPreferred true for calculating preferred size, false for
|
---|
179 | * calculating minimum size.
|
---|
180 | * @return the largest cell size required.
|
---|
181 | */
|
---|
182 | protected Dimension getLargestCellSize(Container parent,
|
---|
183 | boolean isPreferred) {
|
---|
184 | int ncomponents = parent.getComponentCount();
|
---|
185 | Dimension maxCellSize = new Dimension(0,0);
|
---|
186 | for ( int i = 0; i < ncomponents; i++ ) {
|
---|
187 | Component c = parent.getComponent(i);
|
---|
188 | Rectangle rect = (Rectangle)compTable.get(c);
|
---|
189 | if ( c != null && rect != null ) {
|
---|
190 | Dimension componentSize;
|
---|
191 | if ( isPreferred ) {
|
---|
192 | componentSize = c.getPreferredSize();
|
---|
193 | } else {
|
---|
194 | componentSize = c.getMinimumSize();
|
---|
195 | }
|
---|
196 | // Note: rect dimensions are already asserted to be > 0 when the
|
---|
197 | // component is added with constraints
|
---|
198 | maxCellSize.width = Math.max(maxCellSize.width,
|
---|
199 | componentSize.width / rect.width);
|
---|
200 | maxCellSize.height = Math.max(maxCellSize.height,
|
---|
201 | componentSize.height / rect.height);
|
---|
202 | }
|
---|
203 | }
|
---|
204 | return maxCellSize;
|
---|
205 | }
|
---|
206 |
|
---|
207 | /**
|
---|
208 | * Lays out the container in the specified container.
|
---|
209 | * @param parent the component which needs to be laid out
|
---|
210 | */
|
---|
211 | public void layoutContainer(Container parent) {
|
---|
212 | synchronized (parent.getTreeLock()) {
|
---|
213 | Insets insets = parent.getInsets();
|
---|
214 | int ncomponents = parent.getComponentCount();
|
---|
215 |
|
---|
216 | if (ncomponents == 0) {
|
---|
217 | return;
|
---|
218 | }
|
---|
219 |
|
---|
220 | // Total parent dimensions
|
---|
221 | Dimension size = parent.getSize();
|
---|
222 | int totalW = size.width - (insets.left + insets.right);
|
---|
223 | int totalH = size.height - (insets.top + insets.bottom);
|
---|
224 |
|
---|
225 | // Cell dimensions, including padding
|
---|
226 | int totalCellW = totalW / gridSize.width;
|
---|
227 | int totalCellH = totalH / gridSize.height;
|
---|
228 |
|
---|
229 | // Cell dimensions, without padding
|
---|
230 | int cellW = (totalW - ( (gridSize.width + 1) * hgap) )
|
---|
231 | / gridSize.width;
|
---|
232 | int cellH = (totalH - ( (gridSize.height + 1) * vgap) )
|
---|
233 | / gridSize.height;
|
---|
234 |
|
---|
235 | for ( int i = 0; i < ncomponents; i++ ) {
|
---|
236 | Component c = parent.getComponent(i);
|
---|
237 | Rectangle rect = (Rectangle)compTable.get(c);
|
---|
238 | if ( rect != null ) {
|
---|
239 | int x = insets.left + ( totalCellW * rect.x ) + hgap;
|
---|
240 | int y = insets.top + ( totalCellH * rect.y ) + vgap;
|
---|
241 | int w = ( cellW * rect.width ) - hgap;
|
---|
242 | int h = ( cellH * rect.height ) - vgap;
|
---|
243 | c.setBounds(x, y, w, h);
|
---|
244 | }
|
---|
245 | }
|
---|
246 | }
|
---|
247 | }
|
---|
248 |
|
---|
249 | // LayoutManager2 /////////////////////////////////////////////////////////
|
---|
250 |
|
---|
251 | /**
|
---|
252 | * Adds the specified component to the layout, using the specified
|
---|
253 | * constraint object.
|
---|
254 | * @param comp the component to be added
|
---|
255 | * @param constraints where/how the component is added to the layout.
|
---|
256 | */
|
---|
257 | public void addLayoutComponent(Component comp, Object constraints) {
|
---|
258 | if (constraints instanceof Rectangle) {
|
---|
259 | Rectangle rect = (Rectangle)constraints;
|
---|
260 | if ( rect.width <= 0 || rect.height <= 0 ) {
|
---|
261 | throw new IllegalArgumentException(
|
---|
262 | "cannot add to layout: rectangle must have positive width and height");
|
---|
263 | }
|
---|
264 | if ( rect.x < 0 || rect.y < 0 ) {
|
---|
265 | throw new IllegalArgumentException(
|
---|
266 | "cannot add to layout: rectangle x and y must be >= 0");
|
---|
267 | }
|
---|
268 | setConstraints(comp, rect);
|
---|
269 | } else if (constraints != null) {
|
---|
270 | throw new IllegalArgumentException(
|
---|
271 | "cannot add to layout: constraint must be a Rectangle");
|
---|
272 | }
|
---|
273 | }
|
---|
274 |
|
---|
275 | /**
|
---|
276 | * Returns the maximum size of this component.
|
---|
277 | * @see java.awt.Component#getMinimumSize()
|
---|
278 | * @see java.awt.Component#getPreferredSize()
|
---|
279 | * @see LayoutManager
|
---|
280 | */
|
---|
281 | public Dimension maximumLayoutSize(Container target) {
|
---|
282 | return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
|
---|
283 | }
|
---|
284 |
|
---|
285 | /**
|
---|
286 | * Returns the alignment along the x axis. This specifies how
|
---|
287 | * the component would like to be aligned relative to other
|
---|
288 | * components. The value should be a number between 0 and 1
|
---|
289 | * where 0 represents alignment along the origin, 1 is aligned
|
---|
290 | * the furthest away from the origin, 0.5 is centered, etc.
|
---|
291 | */
|
---|
292 | public float getLayoutAlignmentX(Container target) {
|
---|
293 | return 0.5f;
|
---|
294 | }
|
---|
295 |
|
---|
296 | /**
|
---|
297 | * Returns the alignment along the y axis. This specifies how
|
---|
298 | * the component would like to be aligned relative to other
|
---|
299 | * components. The value should be a number between 0 and 1
|
---|
300 | * where 0 represents alignment along the origin, 1 is aligned
|
---|
301 | * the furthest away from the origin, 0.5 is centered, etc.
|
---|
302 | */
|
---|
303 | public float getLayoutAlignmentY(Container target) {
|
---|
304 | return 0.5f;
|
---|
305 | }
|
---|
306 |
|
---|
307 | /**
|
---|
308 | * Invalidates the layout, indicating that if the layout manager
|
---|
309 | * has cached information it should be discarded.
|
---|
310 | */
|
---|
311 | public void invalidateLayout(Container target) {
|
---|
312 | // Do nothing
|
---|
313 | }
|
---|
314 | }
|
---|