Coverage Report - net.miginfocom.layout.DimConstraint
 
Classes in this File Line Coverage Branch Coverage Complexity
DimConstraint
N/A
N/A
1.914
 
 1  
 package net.miginfocom.layout;
 2  
 
 3  
 import java.io.*;
 4  
 /*
 5  
  * License (BSD):
 6  
  * ==============
 7  
  *
 8  
  * Copyright (c) 2004, Mikael Grev, MiG InfoCom AB. (miglayout (at) miginfocom (dot) com)
 9  
  * All rights reserved.
 10  
  *
 11  
  * Redistribution and use in source and binary forms, with or without modification,
 12  
  * are permitted provided that the following conditions are met:
 13  
  * Redistributions of source code must retain the above copyright notice, this list
 14  
  * of conditions and the following disclaimer.
 15  
  * Redistributions in binary form must reproduce the above copyright notice, this
 16  
  * list of conditions and the following disclaimer in the documentation and/or other
 17  
  * materials provided with the distribution.
 18  
  * Neither the name of the MiG InfoCom AB nor the names of its contributors may be
 19  
  * used to endorse or promote products derived from this software without specific
 20  
  * prior written permission.
 21  
  *
 22  
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 23  
  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 24  
  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 25  
  * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 26  
  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 27  
  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 28  
  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 29  
  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 30  
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
 31  
  * OF SUCH DAMAGE.
 32  
  *
 33  
  * @version 1.0
 34  
  * @author Mikael Grev, MiG InfoCom AB
 35  
  *         Date: 2006-sep-08
 36  
  */
 37  
 
 38  
 /** A simple value holder for a constraint for one dimension.
 39  
  */
 40  
 public final class DimConstraint implements Externalizable
 41  
 {
 42  
         /** How this entity can be resized in the dimension that this constraint represents.
 43  
          */
 44  
         final ResizeConstraint resize = new ResizeConstraint();
 45  
 
 46  
         // Look at the properties' getter/setter methods for explanation
 47  
 
 48  
         private String sizeGroup = null;            // A "context" compared with equals.
 49  
 
 50  
         private BoundSize size = BoundSize.NULL_SIZE;     // Min, pref, max. Never null, but sizes can be null.
 51  
 
 52  
         private BoundSize gapBefore = null, gapAfter = null;
 53  
 
 54  
         private UnitValue align = null;
 55  
 
 56  
 
 57  
         // **************  Only applicable on components! *******************
 58  
 
 59  
         private String endGroup = null;            // A "context" compared with equals.
 60  
 
 61  
 
 62  
         // **************  Only applicable on rows/columns! *******************
 63  
 
 64  
         private boolean fill = false;
 65  
 
 66  
         private boolean noGrid = false;
 67  
 
 68  
         /** Empty constructor.
 69  
          */
 70  
         public DimConstraint()
 71  
         {
 72  
         }
 73  
 
 74  
         /** Returns the grow priority. Relative priority is used for determining which entities gets the extra space first.
 75  
          * <p>
 76  
          * For a more thorough explanation of what this constraint does see the white paper or cheat Sheet at www.migcomponents.com.
 77  
          * @return The grow priority.
 78  
          */
 79  
         public int getGrowPriority()
 80  
         {
 81  
                 return resize.growPrio;
 82  
         }
 83  
 
 84  
         /** Sets the grow priority. Relative priority is used for determining which entities gets the extra space first.
 85  
          * <p>
 86  
          * For a more thorough explanation of what this constraint does see the white paper or cheat Sheet at www.migcomponents.com.
 87  
          * @param p The new grow priority.
 88  
          */
 89  
         public void setGrowPriority(int p)
 90  
         {
 91  
                 resize.growPrio = p;
 92  
         }
 93  
 
 94  
         /** Returns the grow weight.<p>
 95  
          * Grow weight is how flexible the entity should be, relative to other entities, when it comes to growing. <code>null</code> or
 96  
          * zero mean it will never grow. An entity that has twice the grow weight compared to another entity will get twice
 97  
          * as much of available space.
 98  
          * <p>
 99  
          * GrowWeight are only compared within the same GrowPrio.
 100  
          * <p>
 101  
          * For a more thorough explanation of what this constraint does see the white paper or cheat Sheet at www.migcomponents.com.
 102  
          * @return The current grow weight.
 103  
          */
 104  
         public Float getGrow()
 105  
         {
 106  
                 return resize.grow;
 107  
         }
 108  
 
 109  
         /** Sets the grow weight.<p>
 110  
          * Grow weight is how flexible the entity should be, relative to other entities, when it comes to growing. <code>null</code> or
 111  
          * zero mean it will never grow. An entity that has twice the grow weight compared to another entity will get twice
 112  
          * as much of available space.
 113  
          * <p>
 114  
          * GrowWeight are only compared within the same GrowPrio.
 115  
          * <p>
 116  
          * For a more thorough explanation of what this constraint does see the white paper or cheat Sheet at www.migcomponents.com.
 117  
          * @param weight The new grow weight.
 118  
          */
 119  
         public void setGrow(Float weight)
 120  
         {
 121  
                 resize.grow = weight;
 122  
         }
 123  
 
 124  
         /** Returns the shrink priority. Relative priority is used for determining which entities gets smaller first when space is scarce.
 125  
          * <p>
 126  
          * For a more thorough explanation of what this constraint does see the white paper or cheat Sheet at www.migcomponents.com.
 127  
          * @return The shrink priority.
 128  
          */
 129  
         public int getShrinkPriority()
 130  
         {
 131  
                 return resize.shrinkPrio;
 132  
         }
 133  
 
 134  
         /** Sets the shrink priority. Relative priority is used for determining which entities gets smaller first when space is scarce.
 135  
          * <p>
 136  
          * For a more thorough explanation of what this constraint does see the white paper or cheat Sheet at www.migcomponents.com.
 137  
          * @param p The new shrink priority.
 138  
          */
 139  
         public void setShrinkPriority(int p)
 140  
         {
 141  
                 resize.shrinkPrio = p;
 142  
         }
 143  
 
 144  
         /** Returns the shrink priority. Relative priority is used for determining which entities gets smaller first when space is scarce.
 145  
          * Shrink weight is how flexible the entity should be, relative to other entities, when it comes to shrinking. <code>null</code> or
 146  
          * zero mean it will never shrink (default). An entity that has twice the shrink weight compared to another entity will get twice
 147  
          * as much of available space.
 148  
          * <p>
 149  
          * Shrink(Weight) are only compared within the same ShrinkPrio.
 150  
          * <p>
 151  
          * For a more thorough explanation of what this constraint does see the white paper or cheat Sheet at www.migcomponents.com.
 152  
          * @return The current shrink weight.
 153  
          */
 154  
         public Float getShrink()
 155  
         {
 156  
                 return resize.shrink;
 157  
         }
 158  
 
 159  
         /** Sets the shrink priority. Relative priority is used for determining which entities gets smaller first when space is scarce.
 160  
          * Shrink weight is how flexible the entity should be, relative to other entities, when it comes to shrinking. <code>null</code> or
 161  
          * zero mean it will never shrink (default). An entity that has twice the shrink weight compared to another entity will get twice
 162  
          * as much of available space.
 163  
          * <p>
 164  
          * Shrink(Weight) are only compared within the same ShrinkPrio.
 165  
          * <p>
 166  
          * For a more thorough explanation of what this constraint does see the white paper or cheat Sheet at www.migcomponents.com.
 167  
          * @param weight The new shrink weight.
 168  
          */
 169  
         public void setShrink(Float weight)
 170  
         {
 171  
                 resize.shrink = weight;
 172  
         }
 173  
 
 174  
         public UnitValue getAlignOrDefault(boolean isCols)
 175  
         {
 176  
                 if (align != null)
 177  
                         return align;
 178  
 
 179  
                 if (isCols)
 180  
                         return UnitValue.LEADING;
 181  
 
 182  
                 return fill || PlatformDefaults.getDefaultRowAlignmentBaseline() == false ? UnitValue.CENTER : UnitValue.BASELINE_IDENTITY;
 183  
         }
 184  
 
 185  
         /** Returns the alignment used either as a default value for sub-entities or for this entity.
 186  
          * <p>
 187  
          * For a more thorough explanation of what this constraint does see the white paper or cheat Sheet at www.migcomponents.com.
 188  
          * @return The alignment.
 189  
          */
 190  
         public UnitValue getAlign()
 191  
         {
 192  
                 return align;
 193  
         }
 194  
 
 195  
         /** Sets the alignment used wither as a default value for sub-entities or for this entity.
 196  
          * <p>
 197  
          * For a more thorough explanation of what this constraint does see the white paper or cheat Sheet at www.migcomponents.com.
 198  
          * @param uv The new shrink priority. E.g. {@link UnitValue#CENTER} or {@link net.miginfocom.layout.UnitValue#LEADING}.
 199  
          */
 200  
         public void setAlign(UnitValue uv)
 201  
         {
 202  
                 this.align = uv;
 203  
         }
 204  
 
 205  
         /** Returns the gap after this entity. The gap is an empty space and can have a min/preferred/maximum size so that it can shrink and
 206  
          * grow depending on available space. Gaps are against other entities' edges and not against other entities' gaps.
 207  
          * <p>
 208  
          * For a more thorough explanation of what this constraint does see the white paper or cheat Sheet at www.migcomponents.com.
 209  
          * @return The gap after this entity
 210  
          */
 211  
         public BoundSize getGapAfter()
 212  
         {
 213  
                 return gapAfter;
 214  
         }
 215  
 
 216  
         /** Sets the gap after this entity. The gap is an empty space and can have a min/preferred/maximum size so that it can shrink and
 217  
          * grow depending on available space. Gaps are against other entities' edges and not against other entities' gaps.
 218  
          * <p>
 219  
          * For a more thorough explanation of what this constraint does see the white paper or cheat Sheet at www.migcomponents.com.
 220  
          * @param size The new gap.
 221  
          * @see net.miginfocom.layout.ConstraintParser#parseBoundSize(String, boolean, boolean).
 222  
          */
 223  
         public void setGapAfter(BoundSize size)
 224  
         {
 225  
                 this.gapAfter = size;
 226  
         }
 227  
 
 228  
         boolean hasGapAfter()
 229  
         {
 230  
                 return gapAfter != null && gapAfter.isUnset() == false;
 231  
         }
 232  
 
 233  
         boolean isGapAfterPush()
 234  
         {
 235  
                 return gapAfter != null && gapAfter.getGapPush();
 236  
         }
 237  
 
 238  
         /** Returns the gap before this entity. The gap is an empty space and can have a min/preferred/maximum size so that it can shrink and
 239  
          * grow depending on available space. Gaps are against other entities' edges and not against other entities' gaps.
 240  
          * <p>
 241  
          * For a more thorough explanation of what this constraint does see the white paper or cheat Sheet at www.migcomponents.com.
 242  
          * @return The gap before this entity
 243  
          */
 244  
         public BoundSize getGapBefore()
 245  
         {
 246  
                 return gapBefore;
 247  
         }
 248  
 
 249  
         /** Sets the gap before this entity. The gap is an empty space and can have a min/preferred/maximum size so that it can shrink and
 250  
          * grow depending on available space. Gaps are against other entities' edges and not against other entities' gaps.
 251  
          * <p>
 252  
          * For a more thorough explanation of what this constraint does see the white paper or cheat Sheet at www.migcomponents.com.
 253  
          * @param size The new gap.
 254  
          * @see net.miginfocom.layout.ConstraintParser#parseBoundSize(String, boolean, boolean).
 255  
          */
 256  
         public void setGapBefore(BoundSize size)
 257  
         {
 258  
                 this.gapBefore = size;
 259  
         }
 260  
 
 261  
         boolean hasGapBefore()
 262  
         {
 263  
                 return gapBefore != null && gapBefore.isUnset() == false;
 264  
         }
 265  
 
 266  
         boolean isGapBeforePush()
 267  
         {
 268  
                 return gapBefore != null && gapBefore.getGapPush();
 269  
         }
 270  
 
 271  
         /** Returns the min/preferred/max size for the entity in the dimension that this object describes.
 272  
          * <p>
 273  
          * For a more thorough explanation of what this constraint does see the white paper or cheat Sheet at www.migcomponents.com.
 274  
          * @return The current size. Never <code>null</code> since v3.5.
 275  
          * @see net.miginfocom.layout.ConstraintParser#parseBoundSize(String, boolean, boolean).
 276  
          */
 277  
         public BoundSize getSize()
 278  
         {
 279  
                 return size;
 280  
         }
 281  
 
 282  
         /** Sets the min/preferred/max size for the entity in the dimension that this object describes.
 283  
          * <p>
 284  
          * For a more thorough explanation of what this constraint does see the white paper or cheat Sheet at www.migcomponents.com.
 285  
          * @param size The new size. May be <code>null</code>.
 286  
          */
 287  
         public void setSize(BoundSize size)
 288  
         {
 289  
                 if (size != null)
 290  
                         size.checkNotLinked();
 291  
                 this.size = size;
 292  
         }
 293  
 
 294  
         /** Returns the size group that this entity should be in for the dimension that this object is describing.
 295  
          * If this constraint is in a size group that is specified here. <code>null</code> means no size group
 296  
          * and all other values are legal. Comparison with .equals(). Components/columnss/rows in the same size group
 297  
          * will have the same min/preferred/max size; that of the largest in the group for the first two and the
 298  
          * smallest for max.
 299  
          * <p>
 300  
          * For a more thorough explanation of what this constraint does see the white paper or cheat Sheet at www.migcomponents.com.
 301  
          * @return The current size group. May be <code>null</code>.
 302  
          */
 303  
         public String getSizeGroup()
 304  
         {
 305  
                 return sizeGroup;
 306  
         }
 307  
 
 308  
         /** Sets the size group that this entity should be in for the dimension that this object is describing.
 309  
          * If this constraint is in a size group that is specified here. <code>null</code> means no size group
 310  
          * and all other values are legal. Comparison with .equals(). Components/columnss/rows in the same size group
 311  
          * will have the same min/preferred/max size; that of the largest in the group for the first two and the
 312  
          * smallest for max.
 313  
          * <p>
 314  
          * For a more thorough explanation of what this constraint does see the white paper or cheat Sheet at www.migcomponents.com.
 315  
          * @param s The new size group. <code>null</code> disables size grouping.
 316  
          */
 317  
         public void setSizeGroup(String s)
 318  
         {
 319  
                 sizeGroup = s;
 320  
         }
 321  
 
 322  
         // **************  Only applicable on components ! *******************
 323  
 
 324  
         /** Returns the end group that this entity should be in for the demension that this object is describing.
 325  
          * If this constraint is in an end group that is specified here. <code>null</code> means no end group
 326  
          * and all other values are legal. Comparison with .equals(). Components in the same end group
 327  
          * will have the same end coordinate.
 328  
          * <p>
 329  
          * For a more thorough explanation of what this constraint does see the white paper or cheat Sheet at www.migcomponents.com.
 330  
          * @return The current end group. <code>null</code> may be returned.
 331  
          */
 332  
         public String getEndGroup()
 333  
         {
 334  
                 return endGroup;
 335  
         }
 336  
 
 337  
         /** Sets the end group that this entity should be in for the demension that this object is describing.
 338  
          * If this constraint is in an end group that is specified here. <code>null</code> means no end group
 339  
          * and all other values are legal. Comparison with .equals(). Components in the same end group
 340  
          * will have the same end coordinate.
 341  
          * <p>
 342  
          * For a more thorough explanation of what this constraint does see the white paper or cheat Sheet at www.migcomponents.com.
 343  
          * @param s The new end group. <code>null</code> disables end grouping.
 344  
          */
 345  
         public void setEndGroup(String s)
 346  
         {
 347  
                 endGroup = s;
 348  
         }
 349  
 
 350  
         // **************  Not applicable on components below ! *******************
 351  
 
 352  
         /** Returns if the component in the row/column that this constraint should default be grown in the same dimension that
 353  
          * this constraint represents (width for column and height for a row).
 354  
          * <p>
 355  
          * For a more thorough explanation of what this constraint does see the white paper or cheat Sheet at www.migcomponents.com.
 356  
          * @return code>true</code> means that components should grow.
 357  
          */
 358  
         public boolean isFill()
 359  
         {
 360  
                 return fill;
 361  
         }
 362  
 
 363  
         /** Sets if the component in the row/column that this constraint should default be grown in the same dimension that
 364  
          * this constraint represents (width for column and height for a row).
 365  
          * <p>
 366  
          * For a more thorough explanation of what this constraint does see the white paper or cheat Sheet at www.migcomponents.com.
 367  
          * @param b <code>true</code> means that components should grow.
 368  
          */
 369  
         public void setFill(boolean b)
 370  
         {
 371  
                 fill = b;
 372  
         }
 373  
 
 374  
         /** Returns if the row/column should default to flow and not to grid behaviour. This means that the whole row/column
 375  
          * will be one cell and all components will end up in that cell.
 376  
          * <p>
 377  
          * For a more thorough explanation of what this constraint does see the white paper or cheat Sheet at www.migcomponents.com.
 378  
          * @return <code>true</code> means that the whole row/column should be one cell.
 379  
          */
 380  
         public boolean isNoGrid()
 381  
         {
 382  
                 return noGrid;
 383  
         }
 384  
 
 385  
         /** Sets if the row/column should default to flow and not to grid behaviour. This means that the whole row/column
 386  
          * will be one cell and all components will end up in that cell.
 387  
          * <p>
 388  
          * For a more thorough explanation of what this constraint does see the white paper or cheat Sheet at www.migcomponents.com.
 389  
          * @param b <code>true</code> means that the whole row/column should be one cell.
 390  
          */
 391  
         public void setNoGrid(boolean b)
 392  
         {
 393  
                 this.noGrid = b;
 394  
         }
 395  
 
 396  
         /** Returns the gaps as pixel values.
 397  
          * @param parent The parent. Used to get the pixel values.
 398  
          * @param defGap The default gap to use if there is no gap set on this object (i.e. it is null).
 399  
          * @param refSize The reference size used to get the pixel sizes.
 400  
          * @param before IF it is the gap before rather than the gap after to return.
 401  
          * @return The [min,preferred,max] sizes for the specified gap. Uses {@link net.miginfocom.layout.LayoutUtil#NOT_SET}
 402  
          * 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.
 403  
          */
 404  
         int[] getRowGaps(ContainerWrapper parent, BoundSize defGap, int refSize, boolean before)
 405  
         {
 406  
                 BoundSize gap = before ? gapBefore : gapAfter;
 407  
                 if (gap == null || gap.isUnset())
 408  
                         gap = defGap;
 409  
 
 410  
                 if (gap == null || gap.isUnset())
 411  
                         return null;
 412  
 
 413  
                 int[] ret = new int[3];
 414  
                 for (int i = LayoutUtil.MIN; i <= LayoutUtil.MAX; i++) {
 415  
                         UnitValue uv = gap.getSize(i);
 416  
                         ret[i] = uv != null ? uv.getPixels(refSize, parent, null) : LayoutUtil.NOT_SET;
 417  
                 }
 418  
                 return ret;
 419  
         }
 420  
 
 421  
         /** Returns the gaps as pixel values.
 422  
          * @param parent The parent. Used to get the pixel values.
 423  
          * @param comp The component that the gap is for. If not for a component it is <code>null</code>.
 424  
          * @param adjGap The gap that the adjacent component, if any, has towards <code>comp</code>.
 425  
          * @param adjacentComp The adjacent component if any. May be <code>null</code>.
 426  
          * @param refSize The reference size used to get the pixel sizes.
 427  
          * @param adjacentSide What side the <code>adjacentComp</code> is on. 0 = top, 1 = left, 2 = bottom, 3 = right.
 428  
          * @param tag The tag string that the component might be tagged with in the component constraints. May be <code>null</code>.
 429  
          * @param isLTR If it is left-to-right.
 430  
          * @return The [min,preferred,max] sizes for the specified gap. Uses {@link net.miginfocom.layout.LayoutUtil#NOT_SET}
 431  
          * 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.
 432  
          */
 433  
         int[] getComponentGaps(ContainerWrapper parent, ComponentWrapper comp, BoundSize adjGap, ComponentWrapper adjacentComp, String tag, int refSize, int adjacentSide, boolean isLTR)
 434  
         {
 435  
                 BoundSize gap = adjacentSide < 2 ? gapBefore : gapAfter;
 436  
 
 437  
                 boolean hasGap = gap != null && gap.getGapPush();
 438  
                 if ((gap == null || gap.isUnset()) && (adjGap == null || adjGap.isUnset()) && comp != null)
 439  
                         gap = PlatformDefaults.getDefaultComponentGap(comp, adjacentComp, adjacentSide + 1, tag, isLTR);
 440  
 
 441  
                 if (gap == null)
 442  
                         return hasGap ? new int[] {0, 0, LayoutUtil.NOT_SET} : null;
 443  
 
 444  
                 int[] ret = new int[3];
 445  
                 for (int i = LayoutUtil.MIN; i <= LayoutUtil.MAX; i++) {
 446  
                         UnitValue uv = gap.getSize(i);
 447  
                         ret[i] = uv != null ? uv.getPixels(refSize, parent, null) : LayoutUtil.NOT_SET;
 448  
                 }
 449  
                 return ret;
 450  
         }
 451  
 
 452  
         // ************************************************
 453  
         // Persistence Delegate and Serializable combined.
 454  
         // ************************************************
 455  
 
 456  
         private Object readResolve() throws ObjectStreamException
 457  
         {
 458  
                 return LayoutUtil.getSerializedObject(this);
 459  
         }
 460  
 
 461  
         public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
 462  
         {
 463  
                 LayoutUtil.setSerializedObject(this, LayoutUtil.readAsXML(in));
 464  
         }
 465  
 
 466  
         public void writeExternal(ObjectOutput out) throws IOException
 467  
         {
 468  
                 if (getClass() == DimConstraint.class)
 469  
                         LayoutUtil.writeAsXML(out, this);
 470  
         }
 471  
 }