1 package net.miginfocom.layout;
3 //import java.beans.Encoder;
4 //import java.beans.Expression;
5 //import java.beans.PersistenceDelegate;
6 import java.io.Serializable;
11 * Copyright (c) 2004, Mikael Grev, MiG InfoCom AB. (miglayout (at) miginfocom (dot) com)
12 * All rights reserved.
14 * Redistribution and use in source and binary forms, with or without modification,
15 * are permitted provided that the following conditions are met:
16 * Redistributions of source code must retain the above copyright notice, this list
17 * of conditions and the following disclaimer.
18 * Redistributions in binary form must reproduce the above copyright notice, this
19 * list of conditions and the following disclaimer in the documentation and/or other
20 * materials provided with the distribution.
21 * Neither the name of the MiG InfoCom AB nor the names of its contributors may be
22 * used to endorse or promote products derived from this software without specific
23 * prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
27 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
29 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
31 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
32 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
37 * @author Mikael Grev, MiG InfoCom AB
42 * A size that contains minimum, preferred and maximum size of type
45 * This class is a simple value container and it is immutable.
47 * If a size is missing (i.e., <code>null</code>) that boundary should be
48 * considered "not in use".
50 * You can create a BoundSize from a String with the use of
51 * {@link ConstraintParser#parseBoundSize(String, boolean, boolean)}
53 public class BoundSize implements Serializable
55 public static final BoundSize NULL_SIZE = new BoundSize(null, null);
57 public static final BoundSize ZERO_PIXEL = new BoundSize(UnitValue.ZERO,
60 private final transient UnitValue min;
62 private final transient UnitValue pref;
64 private final transient UnitValue max;
66 private final transient boolean gapPush;
69 * Constructor that use the same value for min/preferred/max size.
72 * The value to use for min/preferred/max size.
74 * The string used to create the BoundsSize.
76 public BoundSize(UnitValue minMaxPref, String createString)
78 this(minMaxPref, minMaxPref, minMaxPref, createString);
82 * Constructor. <b>This method is here for serialization only and should
83 * normally not be used.</b> Use
84 * {@link ConstraintParser#parseBoundSize(String, boolean, boolean)} instead.
87 * The minimum size. May be <code>null</code>.
89 * The preferred size. May be <code>null</code>.
91 * The maximum size. May be <code>null</code>.
93 * The string used to create the BoundsSize.
95 public BoundSize(UnitValue min, UnitValue preferred, UnitValue max,
96 String createString) // Bound to old delegate!!!!!
98 this(min, preferred, max, false, createString);
102 * Constructor. <b>This method is here for serialization only and should
103 * normally not be used.</b> Use
104 * {@link ConstraintParser#parseBoundSize(String, boolean, boolean)} instead.
107 * The minimum size. May be <code>null</code>.
109 * The preferred size. May be <code>null</code>.
111 * The maximum size. May be <code>null</code>.
113 * If the size should be hinted as "pushing" and thus want to occupy
114 * free space if no one else is claiming it.
115 * @param createString
116 * The string used to create the BoundsSize.
118 public BoundSize(UnitValue min, UnitValue preferred, UnitValue max,
119 boolean gapPush, String createString)
122 this.pref = preferred;
124 this.gapPush = gapPush;
126 LayoutUtil.putCCString(this, createString); // this escapes!!
130 * Returns the minimum size as sent into the constructor.
132 * @return The minimum size as sent into the constructor. May be
135 public final UnitValue getMin()
141 * Returns the preferred size as sent into the constructor.
143 * @return The preferred size as sent into the constructor. May be
146 public final UnitValue getPreferred()
152 * Returns the maximum size as sent into the constructor.
154 * @return The maximum size as sent into the constructor. May be
157 public final UnitValue getMax()
163 * If the size should be hinted as "pushing" and thus want to occupy free
164 * space if no one else is claiming it.
168 public boolean getGapPush()
174 * Returns if this bound size has no min, preferred and maximum size set (they
175 * are all <code>null</code>)
179 public boolean isUnset()
181 // Most common case by far is this == ZERO_PIXEL...
182 return this == ZERO_PIXEL || (pref == null && min == null && max == null
183 && gapPush == false);
187 * Makes sure that <code>size</code> is within min and max of this size.
190 * The size to constrain.
192 * The reference to use for relative sizes.
194 * The parent container.
195 * @return The size, constrained within min and max.
197 public int constrain(int size, float refValue, ContainerWrapper parent)
200 size = Math.min(size, max.getPixels(refValue, parent, parent));
202 size = Math.max(size, min.getPixels(refValue, parent, parent));
207 * Returns the minimum, preferred or maximum size for this bounded size.
210 * The type. <code>LayoutUtil.MIN</code>,
211 * <code>LayoutUtil.PREF</code> or <code>LayoutUtil.MAX</code>.
214 final UnitValue getSize(int sizeType)
220 case LayoutUtil.PREF:
225 throw new IllegalArgumentException("Unknown size: " + sizeType);
230 * Convert the bound sizes to pixels.
232 * <code>null</code> bound sizes will be 0 for min and preferred and
233 * {@link net.miginfocom.layout.LayoutUtil#INF} for max.
236 * The reference size.
238 * The parent. Not <code>null</code>.
240 * The component, if applicable, can be <code>null</code>.
241 * @return An array of length three (min,pref,max).
243 final int[] getPixelSizes(float refSize, ContainerWrapper parent,
244 ComponentWrapper comp)
247 min != null ? min.getPixels(refSize, parent, comp) : 0,
248 pref != null ? pref.getPixels(refSize, parent, comp) : 0,
249 max != null ? max.getPixels(refSize, parent, comp)
254 * Returns the a constraint string that can be re-parsed to be the exact same
257 * @return A String. Never <code>null</code>.
259 String getConstraintString()
261 String cs = LayoutUtil.getCCString(this);
265 if (min == pref && pref == max)
266 return min != null ? (min.getConstraintString() + "!") : "null";
268 StringBuilder sb = new StringBuilder(16);
271 sb.append(min.getConstraintString()).append(':');
275 if (min == null && max != null)
277 sb.append(pref.getConstraintString());
279 else if (min != null)
285 sb.append(sb.length() == 0 ? "::" : ":")
286 .append(max.getConstraintString());
295 return sb.toString();
298 void checkNotLinked()
301 throw new IllegalArgumentException("Size may not contain links");
306 return min != null && min.isLinkedDeep()
307 || pref != null && pref.isLinkedDeep()
308 || max != null && max.isLinkedDeep();
313 return (min == null || min.isAbsoluteDeep())
314 && (pref == null || pref.isAbsoluteDeep())
315 && (max == null || max.isAbsoluteDeep());
318 public String toString()
320 return "BoundSize{" + "min=" + min + ", pref=" + pref + ", max=" + max
321 + ", gapPush=" + gapPush + '}';
326 // if (LayoutUtil.HAS_BEANS)
328 // LayoutUtil.setDelegate(BoundSize.class, new PersistenceDelegate()
331 // protected Expression instantiate(Object oldInstance, Encoder out)
333 // BoundSize bs = (BoundSize) oldInstance;
334 // if (Grid.TEST_GAPS)
336 // return new Expression(oldInstance, BoundSize.class, "new",
338 // { bs.getMin(), bs.getPreferred(), bs.getMax(),
339 // bs.getGapPush(), bs.getConstraintString() });
343 // return new Expression(oldInstance, BoundSize.class, "new",
345 // { bs.getMin(), bs.getPreferred(), bs.getMax(),
346 // bs.getConstraintString() });
353 // // ************************************************
354 // // Persistence Delegate and Serializable combined.
355 // // ************************************************
357 // private static final long serialVersionUID = 1L;
359 // protected Object readResolve() throws ObjectStreamException
361 // return LayoutUtil.getSerializedObject(this);
364 // private void writeObject(ObjectOutputStream out) throws IOException
366 // if (getClass() == BoundSize.class)
367 // LayoutUtil.writeAsXML(out, this);
370 // private void readObject(ObjectInputStream in) throws IOException,
371 // ClassNotFoundException
373 // LayoutUtil.setSerializedObject(this, LayoutUtil.readAsXML(in));