summaryrefslogtreecommitdiff
path: root/prototypes/miglayout/net/miginfocom/layout/DimConstraint.java
blob: 47c1021ccbc87d56f8545b2e65ca9c4cf1beba21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
package net.miginfocom.layout;

import java.io.*;
/*
 * License (BSD):
 * ==============
 *
 * Copyright (c) 2004, Mikael Grev, MiG InfoCom AB. (miglayout (at) miginfocom (dot) com)
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright notice, this list
 * of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice, this
 * list of conditions and the following disclaimer in the documentation and/or other
 * materials provided with the distribution.
 * Neither the name of the MiG InfoCom AB nor the names of its contributors may be
 * used to endorse or promote products derived from this software without specific
 * prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
 * OF SUCH DAMAGE.
 *
 * @version 1.0
 * @author Mikael Grev, MiG InfoCom AB
 *         Date: 2006-sep-08
 */

/** A simple value holder for a constraint for one dimension.
 */
public final class DimConstraint implements Externalizable
{
	/** How this entity can be resized in the dimension that this constraint represents.
	 */
	final ResizeConstraint resize = new ResizeConstraint();

	// Look at the properties' getter/setter methods for exmplanation

	private String sizeGroup = null;            // A "context" compared with equals.

	private BoundSize size = null;            // Min, pref, max. May be null.

	private BoundSize gapBefore = null, gapAfter = null;

	private UnitValue align = null;


	// **************  Only applicable on components! *******************

	private String endGroup = null;            // A "context" compared with equals.


	// **************  Only applicable on rows/columns! *******************

	private boolean fill = false;

	private boolean noGrid = false;

	/** Empty constructor.
	 */
	public DimConstraint()
	{
	}

	/** Returns the grow priority. Relative priority is used for determining which entities gets the extra space first.
	 * <p>
	 * For a more thorough explanation of what this constraint does see the white paper or cheat Sheet at www.migcomponents.com.
	 * @return The grow priority.
	 */
	public int getGrowPriority()
	{
		return resize.growPrio;
	}

	/** Sets the grow priority. Relative priority is used for determining which entities gets the extra space first.
	 * <p>
	 * For a more thorough explanation of what this constraint does see the white paper or cheat Sheet at www.migcomponents.com.
	 * @param p The new grow priority.
	 */
	public void setGrowPriority(int p)
	{
		resize.growPrio = p;
	}

	/** Returns the grow weight.<p>
	 * Grow weight is how flexilble the entity should be, relative to other entities, when it comes to growing. <code>null</code> or
	 * zero mean it will never grow. An entity that has twise the grow weight compared to another entity will get twice
	 * as much of available space.
	 * <p>
	 * GrowWeight are only compared within the same GrowPrio.
	 * <p>
	 * For a more thorough explanation of what this constraint does see the white paper or cheat Sheet at www.migcomponents.com.
	 * @return The current grow weight.
	 */
	public Float getGrow()
	{
		return resize.grow;
	}

	/** Sets the grow weight.<p>
	 * Grow weight is how flexilble the entity should be, relative to other entities, when it comes to growing. <code>null</code> or
	 * zero mean it will never grow. An entity that has twise the grow weight compared to another entity will get twice
	 * as much of available space.
	 * <p>
	 * GrowWeight are only compared within the same GrowPrio.
	 * <p>
	 * For a more thorough explanation of what this constraint does see the white paper or cheat Sheet at www.migcomponents.com.
	 * @param weight The new grow weight.
	 */
	public void setGrow(Float weight)
	{
		resize.grow = weight;
	}

	/** Returns the shrink priority. Relative priority is used for determining which entities gets smaller first when space is scarse.
	 * <p>
	 * For a more thorough explanation of what this constraint does see the white paper or cheat Sheet at www.migcomponents.com.
	 * @return The shrink priority.
	 */
	public int getShrinkPriority()
	{
		return resize.shrinkPrio;
	}

	/** Sets the shrink priority. Relative priority is used for determining which entities gets smaller first when space is scarse.
	 * <p>
	 * For a more thorough explanation of what this constraint does see the white paper or cheat Sheet at www.migcomponents.com.
	 * @param p The new shrink priority.
	 */
	public void setShrinkPriority(int p)
	{
		resize.shrinkPrio = p;
	}

	/** Returns the shrink priority. Relative priority is used for determining which entities gets smaller first when space is scarse.
	 * Shrink weight is how flexilble the entity should be, relative to other entities, when it comes to shrinking. <code>null</code> or
	 * zero mean it will never shrink (default). An entity that has twise the shrink weight compared to another entity will get twice
	 * as much of available space.
	 * <p>
	 * Shrink(Weight) are only compared within the same ShrinkPrio.
	 * <p>
	 * For a more thorough explanation of what this constraint does see the white paper or cheat Sheet at www.migcomponents.com.
	 * @return The current shrink weight.
	 */
	public Float getShrink()
	{
		return resize.shrink;
	}

	/** Sets the shrink priority. Relative priority is used for determining which entities gets smaller first when space is scarse.
	 * Shrink weight is how flexilble the entity should be, relative to other entities, when it comes to shrinking. <code>null</code> or
	 * zero mean it will never shrink (default). An entity that has twise the shrink weight compared to another entity will get twice
	 * as much of available space.
	 * <p>
	 * Shrink(Weight) are only compared within the same ShrinkPrio.
	 * <p>
	 * For a more thorough explanation of what this constraint does see the white paper or cheat Sheet at www.migcomponents.com.
	 * @param weight The new shrink weight.
	 */
	public void setShrink(Float weight)
	{
		resize.shrink = weight;
	}

	public UnitValue getAlignOrDefault(boolean isCols)
	{
		if (align != null)
			return align;

		return isCols ? UnitValue.LEADING : UnitValue.CENTER;
	}

	/** Returns the alignment used either as a default value for sub-entities or for this entity.
	 * <p>
	 * For a more thorough explanation of what this constraint does see the white paper or cheat Sheet at www.migcomponents.com.
	 * @return The alignment.
	 */
	public UnitValue getAlign()
	{
		return align;
	}

	/** Sets the alignment used wither as a default value for sub-entities or for this entity.
	 * <p>
	 * For a more thorough explanation of what this constraint does see the white paper or cheat Sheet at www.migcomponents.com.
	 * @param uv The new shrink priority. E.g. {@link UnitValue#CENTER} or {@link net.miginfocom.layout.UnitValue#LEADING}.
	 */
	public void setAlign(UnitValue uv)
	{
		this.align = uv;
	}

	/** Returns the gap after this entitiy. The gap is an empty space and can have a min/preferred/maximum size so that it can shrink and
	 * grow depending on available space. Gaps are against other entities' edges and not against other entities' gaps.
	 * <p>
	 * For a more thorough explanation of what this constraint does see the white paper or cheat Sheet at www.migcomponents.com.
	 * @return The gap after this entitiy
	 */
	public BoundSize getGapAfter()
	{
		return gapAfter;
	}

	/** Sets the gap after this entitiy. The gap is an empty space and can have a min/preferred/maximum size so that it can shrink and
	 * grow depending on available space. Gaps are against other entities' edges and not against other entities' gaps.
	 * <p>
	 * For a more thorough explanation of what this constraint does see the white paper or cheat Sheet at www.migcomponents.com.
	 * @param size The new gap.
	 * @see net.miginfocom.layout.ConstraintParser#parseBoundSize(String, boolean, boolean).
	 */
	public void setGapAfter(BoundSize size)
	{
		this.gapAfter = size;
	}

	boolean hasGapAfter()
	{
		return gapAfter != null && gapAfter.isUnset() == false;
	}

	boolean isGapAfterPush()
	{
		return gapAfter != null && gapAfter.getGapPush();
	}

	/** Returns the gap before this entitiy. The gap is an empty space and can have a min/preferred/maximum size so that it can shrink and
	 * grow depending on available space. Gaps are against other entities' edges and not against other entities' gaps.
	 * <p>
	 * For a more thorough explanation of what this constraint does see the white paper or cheat Sheet at www.migcomponents.com.
	 * @return The gap before this entitiy
	 */
	public BoundSize getGapBefore()
	{
		return gapBefore;
	}

	/** Sets the gap before this entitiy. The gap is an empty space and can have a min/preferred/maximum size so that it can shrink and
	 * grow depending on available space. Gaps are against other entities' edges and not against other entities' gaps.
	 * <p>
	 * For a more thorough explanation of what this constraint does see the white paper or cheat Sheet at www.migcomponents.com.
	 * @param size The new gap.
	 * @see net.miginfocom.layout.ConstraintParser#parseBoundSize(String, boolean, boolean).
	 */
	public void setGapBefore(BoundSize size)
	{
		this.gapBefore = size;
	}

	boolean hasGapBefore()
	{
		return gapBefore != null && gapBefore.isUnset() == false;
	}

	boolean isGapBeforePush()
	{
		return gapBefore != null && gapBefore.getGapPush();
	}

	/** Returns the min/prerefferd/max size for the entity in the dimension that this object describes.
	 * <p>
	 * For a more thorough explanation of what this constraint does see the white paper or cheat Sheet at www.migcomponents.com.
	 * @return The current size. May be <code>null</code>.
	 * @see net.miginfocom.layout.ConstraintParser#parseBoundSize(String, boolean, boolean).
	 */
	public BoundSize getSize()
	{
		return size;
	}

	/** Sets the min/prerefferd/max size for the eintity in the dimension that this object describes.
	 * <p>
	 * For a more thorough explanation of what this constraint does see the white paper or cheat Sheet at www.migcomponents.com.
	 * @param size The new size. May be <code>null</code>.
	 */
	public void setSize(BoundSize size)
	{
		this.size = size;
	}

	/** Returns the size group that this entitiy should be in for the demension that this object is describing.
	 * If this constraint is in a size group that is specified here. <code>null</code> means no size group
	 * and all other values are legal. Comparison with .equals(). Components/columnss/rows in the same size group
	 * will have the same min/preferred/max size; that of the largest in the group for the first two and the
	 * smallest for max.
	 * <p>
	 * For a more thorough explanation of what this constraint does see the white paper or cheat Sheet at www.migcomponents.com.
	 * @return The current size group. May be <code>null</code>.
	 */
	public String getSizeGroup()
	{
		return sizeGroup;
	}

	/** Sets the size group that this entitiy should be in for the demension that this object is describing.
	 * If this constraint is in a size group that is specified here. <code>null</code> means no size group
	 * and all other values are legal. Comparison with .equals(). Components/columnss/rows in the same size group
	 * will have the same min/preferred/max size; that of the largest in the group for the first two and the
	 * smallest for max.
	 * <p>
	 * For a more thorough explanation of what this constraint does see the white paper or cheat Sheet at www.migcomponents.com.
	 * @param s The new size group. <code>null</code> disables size grouping.
	 */
	public void setSizeGroup(String s)
	{
		sizeGroup = s;
	}

	// **************  Only applicable on components ! *******************

	/** Returns the end group that this entitiy should be in for the demension that this object is describing.
	 * If this constraint is in an end group that is specified here. <code>null</code> means no end group
	 * and all other values are legal. Comparison with .equals(). Components in the same end group
	 * will have the same end coordinate.
	 * <p>
	 * For a more thorough explanation of what this constraint does see the white paper or cheat Sheet at www.migcomponents.com.
	 * @return The current end group. <code>null</code> may be returned.
	 */
	public String getEndGroup()
	{
		return endGroup;
	}

	/** Sets the end group that this entitiy should be in for the demension that this object is describing.
	 * If this constraint is in an end group that is specified here. <code>null</code> means no end group
	 * and all other values are legal. Comparison with .equals(). Components in the same end group
	 * will have the same end coordinate.
	 * <p>
	 * For a more thorough explanation of what this constraint does see the white paper or cheat Sheet at www.migcomponents.com.
	 * @param s The new end group. <code>null</code> disables end grouping.
	 */
	public void setEndGroup(String s)
	{
		endGroup = s;
	}

	// **************  Not applicable on components below ! *******************

	/** Returns if the component in the row/column that this constraint should default be grown in the same dimension that
	 * this constraint represents (width for column and height for a row).
	 * <p>
	 * For a more thorough explanation of what this constraint does see the white paper or cheat Sheet at www.migcomponents.com.
	 * @return code>true</code> means that components should grow.
	 */
	public boolean isFill()
	{
		return fill;
	}

	/** Sets if the component in the row/column that this constraint should default be grown in the same dimension that
	 * this constraint represents (width for column and height for a row).
	 * <p>
	 * For a more thorough explanation of what this constraint does see the white paper or cheat Sheet at www.migcomponents.com.
	 * @param b <code>true</code> means that components should grow.
	 */
	public void setFill(boolean b)
	{
		fill = b;
	}

	/** Returns if the row/column should default to flow and not to grid behaviour. This means that the whole row/column
	 * will be one cell and all components will end up in that cell.
	 * <p>
	 * For a more thorough explanation of what this constraint does see the white paper or cheat Sheet at www.migcomponents.com.
	 * @return <code>true</code> means that the whole row/column should be one cell.
	 */
	public boolean isNoGrid()
	{
		return noGrid;
	}

	/** Sets if the row/column should default to flow and not to grid behaviour. This means that the whole row/column
	 * will be one cell and all components will end up in that cell.
	 * <p>
	 * For a more thorough explanation of what this constraint does see the white paper or cheat Sheet at www.migcomponents.com.
	 * @param b <code>true</code> means that the whole row/column should be one cell.
	 */
	public void setNoGrid(boolean b)
	{
		this.noGrid = b;
	}

	/** Returns the gaps as pixel values.
	 * @param parent The parent. Used to get the pixel values.
	 * @param defGap The default gap to use if there is no gap set on this object (i.e. it is null).
	 * @param refSize The reference size used to get the pixel sizes.
	 * @param before IF it is the gap before rather than the gap after to return.
	 * @return The [min,preferred,max] sizes for the specified gap. Uses {@link net.miginfocom.layout.LayoutUtil#NOT_SET}
	 * for gap sizes that are <code>null</code>. Returns <code>null</code> if there was no gap specified. A new and free to use array.
	 */
	int[] getRowGaps(ContainerWrapper parent, BoundSize defGap, int refSize, boolean before)
	{
		BoundSize gap = before ? gapBefore : gapAfter;
		if (gap == null || gap.isUnset())
			gap = defGap;

		if (gap == null || gap.isUnset())
			return null;

		int[] ret = new int[3];
		for (int i = LayoutUtil.MIN; i <= LayoutUtil.MAX; i++) {
			UnitValue uv = gap.getSize(i);
			ret[i] = uv != null ? uv.getPixels(refSize, parent, null) : LayoutUtil.NOT_SET;
		}
		return ret;
	}

	/** Returns the gaps as pixel values.
	 * @param parent The parent. Used to get the pixel values.
	 * @param comp The component that the gap is for. If not for a component it is <code>null</code>.
	 * @param adjGap The gap that the adjacent component, if any, has towards <code>comp</code>.
	 * @param adjacentComp The adjacent component if any. May be <code>null</code>.
	 * @param refSize The reference size used to get the pixel sizes.
	 * @param adjacentSide Whan side the <code>adjacentComp</code> is on. 0 = top, 1 = left, 2 = bottom, 3 = right.
	 * @param tag The tag string that the component might be tagged with in the component constraints. May be <code>null</code>.
	 * @param isLTR If it is left-to-right.
	 * @return The [min,preferred,max] sizes for the specified gap. Uses {@link net.miginfocom.layout.LayoutUtil#NOT_SET}
	 * for gap sizes that are <code>null</code>. Returns <code>null</code> if there was no gap specified. A new and free to use array.
	 */
	int[] getComponentGaps(ContainerWrapper parent, ComponentWrapper comp, BoundSize adjGap, ComponentWrapper adjacentComp, String tag, int refSize, int adjacentSide, boolean isLTR)
	{
		BoundSize gap = adjacentSide < 2 ? gapBefore : gapAfter;

		boolean hasGap = gap != null && gap.getGapPush();
		if ((gap == null || gap.isUnset()) && (adjGap == null || adjGap.isUnset()) && comp != null)
			gap = PlatformDefaults.getDefaultComponentGap(comp, adjacentComp, adjacentSide + 1, tag, isLTR);

		if (gap == null)
			return hasGap ? new int[] {0, 0, LayoutUtil.NOT_SET} : null;

		int[] ret = new int[3];
		for (int i = LayoutUtil.MIN; i <= LayoutUtil.MAX; i++) {
			UnitValue uv = gap.getSize(i);
			ret[i] = uv != null ? uv.getPixels(refSize, parent, null) : LayoutUtil.NOT_SET;
		}
		return ret;
	}

	// ************************************************
	// Persistence Delegate and Serializable combined.
	// ************************************************

	private Object readResolve() throws ObjectStreamException
	{
		return LayoutUtil.getSerializedObject(this);
	}

	public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
	{
		LayoutUtil.setSerializedObject(this, LayoutUtil.readAsXML(in));
	}

	public void writeExternal(ObjectOutput out) throws IOException
	{
		if (getClass() == DimConstraint.class)
			LayoutUtil.writeAsXML(out, this);
	}
}