1 package fr.ove.palette;
2
3 import java.awt.*;
4 import java.io.Serializable;
5
6 /***
7 * A layout manager for component to be laid out as a palette.<BR>
8 * We can specified the number max of element allowed by row or by column according to
9 * the constraint chosen.
10 * For example PaletteLayout(PaletteLayout.COLUMN_CONSTRAINT, 3) will lay a container with
11 * the constraint that, for the most, we can have 3 components by column.<BR>
12 * All components have the same size, which corresponds to the biggest "preferredWidth"
13 * and the biggest "preferredHeight".
14 *
15 * @author © 1999 DIRAT Laurent
16 * @version 1.0 17/09/1999
17 */
18 public class PaletteLayout implements LayoutManager2, Serializable {
19 /***
20 * The different kinds of constraint.
21 */
22
23 /***
24 * The specified number of elements correspond to the number max by row.
25 */
26 public static final byte ROW_CONSTRAINT = 0;
27
28 /***
29 * The specified number of elements correspond to the number max by row.
30 */
31 public static final byte COLUMN_CONSTRAINT = 1;
32
33 /***
34 * The constraint type.
35 */
36 private byte constraint;
37
38 /***
39 * The number max of element by row or by column according to the type of constraint.
40 */
41 private int nbElement;
42
43 /***
44 * The preferred size
45 */
46 private Dimension prefSize;
47
48 /***
49 * The default constructor.
50 * The default is a container laid out with 3, for the most, elements by row.
51 */
52 public PaletteLayout() {
53 this(ROW_CONSTRAINT, 3);
54 }
55
56 /***
57 * The constructor.
58 * @constraint the constraint (<CODE>ROW_CONSTRAINT</CODE> or <CODE>COLUMN_CONSTRAINT</CODE>) for ordering
59 * the components in the container laid out.
60 * @param nbElement the number max of element allowed by row (resp. column) with the <CODE>ROW_CONSTRAINT</CODE>
61 * (resp. <CODE>COLUMN_CONSTRAINT</CODE>) constraint.
62 */
63 public PaletteLayout(byte constraint, int nbElement) {
64 if ((constraint == ROW_CONSTRAINT) || (constraint == COLUMN_CONSTRAINT))
65 this.constraint = constraint;
66 else
67 this.constraint = ROW_CONSTRAINT;
68
69 if (nbElement > 0)
70 this.nbElement = nbElement;
71 else
72 this.nbElement = 3;
73 }
74
75 /***
76 * Sets the constraint (<CODE>ROW_CONSTRAINT</CODE> or <CODE>COLUMN_CONSTRAINT</CODE>) for ordering
77 * the components in the container laid out.<BR>
78 * After a call to this method, a call to <CODE>validate</CODE> on the container laid out should be
79 * performed to see the change take effect.
80 * @param constraint the constraint to set.
81 */
82 public void setConstraint(byte constraint) {
83 if ((constraint == ROW_CONSTRAINT) || (constraint == COLUMN_CONSTRAINT))
84 this.constraint = constraint;
85 else
86 this.constraint = ROW_CONSTRAINT;
87 }
88
89 /***
90 * Returns the constraint put on the container laid out.
91 */
92 public byte getConstraint() {
93 return constraint;
94 }
95
96 /***
97 * Sets the number max of element allowed by row (resp. column) with the <CODE>ROW_CONSTRAINT</CODE>
98 * (resp. <CODE>COLUMN_CONSTRAINT</CODE>) constraint.
99 * After a call to this method, a call to <CODE>validate</CODE> on the container laid out should be
100 * performed to see the change take effect.
101 * @param nbElement the number of element allowed.
102 */
103 public void setNbElement(int nbElement) {
104 if (nbElement > 0)
105 this.nbElement = nbElement;
106 }
107
108 /***
109 * Returns the number max of element allowed by row (resp. column) with the <CODE>ROW_CONSTRAINT</CODE>
110 * (resp. <CODE>COLUMN_CONSTRAINT</CODE>) constraint.
111 */
112 public int getNbElement() {
113 return nbElement;
114 }
115
116
117
118
119 /***
120 * Adds the specified component with the specified name to
121 * the layout.
122 * @param name the component name
123 * @param comp the component to be added
124 */
125 public void addLayoutComponent(String name, Component comp) {
126 }
127
128 /***
129 * Lays out the container in the specified panel.
130 * @param parent the component which needs to be laid out
131 */
132 public void layoutContainer(Container parent) {
133 int count = parent.getComponentCount();
134 int maxWidth = 0;
135 int maxHeight = 0;
136 Dimension dim;
137 Component comp;
138 for (int i = 0; i < count; i++) {
139 comp = parent.getComponent(i);
140 comp.setSize(0, 0);
141 dim = comp.getPreferredSize();
142 maxWidth = (int) Math.max(maxWidth, dim.width);
143 maxHeight = (int) Math.max(maxHeight, dim.height);
144 }
145
146 Dimension size = parent.getSize();
147
148 if (size == null)
149 size = prefSize;
150
151 int startX = 0;
152 int startY = 0;
153
154 if ((size != null) && (prefSize != null)) {
155 startX = (size.width - prefSize.width) / 2;
156 startY = (size.height - prefSize.height) / 2;
157 }
158
159 int shiftX = startX;
160 int shiftY = startY;
161
162 if (constraint == COLUMN_CONSTRAINT) {
163 for (int i = 0; i < count; i++) {
164 comp = parent.getComponent(i);
165 comp.setBounds(shiftX, shiftY, maxWidth, maxHeight);
166
167 if (((i != 0) && (((i + 1) % nbElement) == 0)) || (nbElement == 1)) {
168 shiftX = startX;
169 shiftY += maxHeight;
170 }
171 else
172 shiftX += maxWidth;
173 }
174 }
175 else {
176 for (int i = 0; i < count; i++) {
177 comp = parent.getComponent(i);
178 comp.setBounds(shiftX, shiftY, maxWidth, maxHeight);
179
180 if (((i != 0) && (((i + 1) % nbElement) == 0)) || (nbElement == 1)) {
181 shiftY = startY;
182 shiftX += maxWidth;
183 }
184 else
185 shiftY += maxHeight;
186 }
187 }
188
189 }
190
191 /***
192 * Calculates the minimum size dimensions for the specified
193 * panel given the components in the specified parent container.
194 * @param parent the component to be laid out
195 * @see #preferredLayoutSize
196 */
197 public Dimension minimumLayoutSize(Container parent) {
198 return preferredLayoutSize(parent);
199 }
200
201 /***
202 * Calculates the preferred size dimensions for the specified
203 * panel given the components in the specified parent container.
204 * @param parent the component to be laid out
205 */
206 public Dimension preferredLayoutSize(Container parent) {
207 int count = parent.getComponentCount();
208 int maxWidth = 0;
209 int maxHeight = 0;
210 Dimension dim = new Dimension();
211
212 for (int i = 0; i < count; i++) {
213 dim = parent.getComponent(i).getPreferredSize();
214 maxWidth = (int) Math.max(maxWidth, dim.width);
215 maxHeight = (int) Math.max(maxHeight, dim.height);
216 }
217
218 int factor = 1;
219
220 if (nbElement > 1) {
221 factor = count / nbElement;
222 factor = ((count % nbElement) == 0) ? factor : factor + 1;
223 }
224
225 if (constraint == COLUMN_CONSTRAINT) {
226 if (nbElement > 1) {
227 dim.width = (count < nbElement) ? count*maxWidth : nbElement*maxWidth;
228 dim.height = factor*maxHeight;
229 }
230 else {
231 dim.width = maxWidth;
232 dim.height = count*maxHeight;
233 }
234 }
235 else {
236 if (nbElement > 1) {
237 dim.width = factor*maxWidth;
238 dim.height = (count < nbElement) ? count*maxHeight : nbElement*maxHeight;
239 }
240 else {
241 dim.width = count*maxWidth;
242 dim.height = maxHeight;
243 }
244 }
245
246 prefSize = new Dimension(dim);
247 return dim;
248 }
249
250 /***
251 * Removes the specified component from the layout.
252 * @param comp the component ot be removed
253 */
254 public void removeLayoutComponent(Component comp) {
255 }
256
257
258
259 /***
260 * Adds the specified component to the layout, using the specified
261 * constraint object.
262 * @param comp the component to be added
263 * @param constraints where/how the component is added to the layout.
264 */
265 public void addLayoutComponent(Component comp, Object constraints) {
266 }
267
268 /***
269 * Returns the maximum size of this component.
270 * @see java.awt.Component#getMinimumSize()
271 * @see java.awt.Component#getPreferredSize()
272 * @see LayoutManager
273 */
274 public Dimension maximumLayoutSize(Container target) {
275 return preferredLayoutSize(target);
276 }
277
278 /***
279 * Returns the alignment along the x axis. This specifies how
280 * the component would like to be aligned relative to other
281 * components. The value should be a number between 0 and 1
282 * where 0 represents alignment along the origin, 1 is aligned
283 * the furthest away from the origin, 0.5 is centered, etc.
284 */
285 public float getLayoutAlignmentX(Container target) {
286 return 0.0f;
287 }
288
289 /***
290 * Returns the alignment along the y axis. This specifies how
291 * the component would like to be aligned relative to other
292 * components. The value should be a number between 0 and 1
293 * where 0 represents alignment along the origin, 1 is aligned
294 * the furthest away from the origin, 0.5 is centered, etc.
295 */
296 public float getLayoutAlignmentY(Container target) {
297 return 0.0f;
298 }
299
300 /***
301 * Invalidates the layout, indicating that if the layout manager
302 * has cached information it should be discarded.
303 */
304 public void invalidateLayout(Container target) {
305 }
306
307 }
308