View Javadoc

1   /*
2    * XMLCV - Gestion de CV sous forme XML
3    * 
4    * Copyright (C) 2004-2005, Damien Raude-Morvan
5    * 
6    * This file is part of XMLCV.
7    *
8    * XMLCV is free software; you can redistribute it and/or modify
9    * it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation; either version 2 of the License, or
11   * (at your option) any later version.
12   *
13   * XMLCV is distributed in the hope that it will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   * GNU General Public License for more details.
17   *
18   * You should have received a copy of the GNU General Public License
19   * along with XMLCV; if not, write to the Free Software
20   * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21   * 
22   * Contacts :
23   * - Sébastien Mathy <smathy@tuxfamily.org>
24   * - Emmanuel Berre <eberre@tuxfamily.org>
25   * - Damien Raude-Morvan <drazzib@drazzib.com>
26   */
27  package org.xmlcv.util;
28  
29  import java.awt.Component;
30  import java.awt.Container;
31  
32  import javax.swing.Spring;
33  import javax.swing.SpringLayout;
34  
35  /***
36   * A 1.4 file that provides utility methods for creating form- or grid-style
37   * layouts with SpringLayout. These utilities are used by several programs, such
38   * as SpringBox and SpringCompactGrid.
39   * 
40   * @version $Revision: 114 $
41   * @author <a href="mailto:drazzib@drazzib.com">Damien Raude-Morvan </a>
42   */
43  public class SpringUtilities {
44  
45  	/* Used by makeCompactGrid. */
46  	private static SpringLayout.Constraints getConstraintsForCell(int row,
47  			int col, Container parent, int cols) {
48  		SpringLayout layout = (SpringLayout) parent.getLayout();
49  		Component c = parent.getComponent(row * cols + col);
50  		return layout.getConstraints(c);
51  	}
52  
53  	/***
54  	 * Aligns the first <code>rows</code>*<code>cols</code> components of
55  	 * <code>parent</code> in a grid. Each component in a column is as wide as
56  	 * the maximum preferred width of the components in that column; height is
57  	 * similarly determined for each row. The parent is made just big enough to
58  	 * fit them all.
59  	 * 
60  	 * @param parent
61  	 *            container of the components
62  	 * @param rows
63  	 *            number of rows
64  	 * @param cols
65  	 *            number of columns
66  	 * @param initialX
67  	 *            x location to start the grid at
68  	 * @param initialY
69  	 *            y location to start the grid at
70  	 * @param xPad
71  	 *            x padding between cells
72  	 * @param yPad
73  	 *            y padding between cells
74  	 */
75  	public static void makeCompactGrid(Container parent, int rows, int cols,
76  			int initialX, int initialY, int xPad, int yPad) {
77  		SpringLayout layout;
78  		try {
79  			layout = (SpringLayout) parent.getLayout();
80  		} catch (ClassCastException exc) {
81  			System.err
82  					.println("The first argument to makeCompactGrid must use SpringLayout."); //$NON-NLS-1$
83  			return;
84  		}
85  
86  		// Align all cells in each column and make them the same width.
87  		Spring x = Spring.constant(initialX);
88  		for (int c = 0; c < cols; c++) {
89  			Spring width = Spring.constant(0);
90  			for (int r = 0; r < rows; r++) {
91  				width = Spring.max(width, getConstraintsForCell(r, c, parent,
92  						cols).getWidth());
93  			}
94  			for (int r = 0; r < rows; r++) {
95  				SpringLayout.Constraints constraints = getConstraintsForCell(r,
96  						c, parent, cols);
97  				constraints.setX(x);
98  				constraints.setWidth(width);
99  			}
100 			x = Spring.sum(x, Spring.sum(width, Spring.constant(xPad)));
101 		}
102 
103 		// Align all cells in each row and make them the same height.
104 		Spring y = Spring.constant(initialY);
105 		for (int r = 0; r < rows; r++) {
106 			Spring height = Spring.constant(0);
107 			for (int c = 0; c < cols; c++) {
108 				height = Spring.max(height, getConstraintsForCell(r, c, parent,
109 						cols).getHeight());
110 			}
111 			for (int c = 0; c < cols; c++) {
112 				SpringLayout.Constraints constraints = getConstraintsForCell(r,
113 						c, parent, cols);
114 				constraints.setY(y);
115 				constraints.setHeight(height);
116 			}
117 			y = Spring.sum(y, Spring.sum(height, Spring.constant(yPad)));
118 		}
119 
120 		// Set the parent's size.
121 		// SpringLayout.Constraints pCons = layout.getConstraints(parent);
122 		// pCons.setConstraint(SpringLayout.SOUTH, y);
123 		// pCons.setConstraint(SpringLayout.EAST, x);
124 	}
125 }