jdk/src/share/classes/javax/swing/SizeRequirements.java
author dav
Mon, 07 Apr 2008 14:53:51 +0400
changeset 438 2ae294e4518c
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6613529: Avoid duplicate object creation within JDK packages Summary: avoid using constructors when unique values are not necessary Reviewed-by: volk, igor, peterz
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1997-2006 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package javax.swing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * For the convenience of layout managers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * calculates information about the size and position of components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * All size and position calculation methods are class methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * that take arrays of SizeRequirements as arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * The SizeRequirements class supports two types of layout:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <dt> tiled
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <dd> The components are placed end-to-end,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *      starting either at coordinate 0 (the leftmost or topmost position)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *      or at the coordinate representing the end of the allocated span
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *      (the rightmost or bottommost position).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <dt> aligned
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <dd> The components are aligned as specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *      by each component's X or Y alignment value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * </dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * Each SizeRequirements object contains information
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * about either the width (and X alignment)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * or height (and Y alignment)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * of a single component or a group of components:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * <dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * <dt> <code>minimum</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * <dd> The smallest reasonable width/height of the component
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *      or component group, in pixels.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * <dt> <code>preferred</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <dd> The natural width/height of the component
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *      or component group, in pixels.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * <dt> <code>maximum</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * <dd> The largest reasonable width/height of the component
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *      or component group, in pixels.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * <dt> <code>alignment</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * <dd> The X/Y alignment of the component
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *      or component group.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * </dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * the same version of Swing.  As of 1.4, support for long term storage
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * of all JavaBeans<sup><font size="-2">TM</font></sup>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * @see Component#getMinimumSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * @see Component#getPreferredSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * @see Component#getMaximumSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * @see Component#getAlignmentX
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * @see Component#getAlignmentY
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * @author Timothy Prinzing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
public class SizeRequirements implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * The minimum size required.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * For a component <code>comp</code>, this should be equal to either
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * <code>comp.getMinimumSize().width</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * <code>comp.getMinimumSize().height</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    public int minimum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * The preferred (natural) size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * For a component <code>comp</code>, this should be equal to either
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * <code>comp.getPreferredSize().width</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * <code>comp.getPreferredSize().height</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    public int preferred;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * The maximum size allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * For a component <code>comp</code>, this should be equal to either
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * <code>comp.getMaximumSize().width</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * <code>comp.getMaximumSize().height</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    public int maximum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * The alignment, specified as a value between 0.0 and 1.0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * inclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * To specify centering, the alignment should be 0.5.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    public float alignment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * Creates a SizeRequirements object with the minimum, preferred,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * and maximum sizes set to zero and an alignment value of 0.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * (centered).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    public SizeRequirements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        minimum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        preferred = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        maximum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        alignment = 0.5f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * Creates a SizeRequirements object with the specified minimum, preferred,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * and maximum sizes and the specified alignment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @param min the minimum size >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @param pref the preferred size >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @param max the maximum size >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @param a the alignment >= 0.0f && <= 1.0f
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    public SizeRequirements(int min, int pref, int max, float a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        minimum = min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        preferred = pref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        maximum = max;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        alignment = a > 1.0f ? 1.0f : a < 0.0f ? 0.0f : a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * Returns a string describing the minimum, preferred, and maximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * size requirements, along with the alignment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @return the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        return "[" + minimum + "," + preferred + "," + maximum + "]@" + alignment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * Determines the total space necessary to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * place a set of components end-to-end.  The needs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * of each component in the set are represented by an entry in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * passed-in SizeRequirements array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * The returned SizeRequirements object has an alignment of 0.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * (centered).  The space requirement is never more than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * Integer.MAX_VALUE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @param children  the space requirements for a set of components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     *   The vector may be of zero length, which will result in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     *   default SizeRequirements object instance being passed back.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * @return  the total space requirements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    public static SizeRequirements getTiledSizeRequirements(SizeRequirements[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                                                            children) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        SizeRequirements total = new SizeRequirements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        for (int i = 0; i < children.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            SizeRequirements req = children[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            total.minimum = (int) Math.min((long) total.minimum + (long) req.minimum, Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            total.preferred = (int) Math.min((long) total.preferred + (long) req.preferred, Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            total.maximum = (int) Math.min((long) total.maximum + (long) req.maximum, Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        return total;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * Determines the total space necessary to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * align a set of components.  The needs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * of each component in the set are represented by an entry in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * passed-in SizeRequirements array.  The total space required will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * never be more than Integer.MAX_VALUE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @param children  the set of child requirements.  If of zero length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *  the returns result will be a default instance of SizeRequirements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * @return  the total space requirements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    public static SizeRequirements getAlignedSizeRequirements(SizeRequirements[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                                                              children) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        SizeRequirements totalAscent = new SizeRequirements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        SizeRequirements totalDescent = new SizeRequirements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        for (int i = 0; i < children.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            SizeRequirements req = children[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            int ascent = (int) (req.alignment * req.minimum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            int descent = req.minimum - ascent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            totalAscent.minimum = Math.max(ascent, totalAscent.minimum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            totalDescent.minimum = Math.max(descent, totalDescent.minimum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            ascent = (int) (req.alignment * req.preferred);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            descent = req.preferred - ascent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            totalAscent.preferred = Math.max(ascent, totalAscent.preferred);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            totalDescent.preferred = Math.max(descent, totalDescent.preferred);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            ascent = (int) (req.alignment * req.maximum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            descent = req.maximum - ascent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            totalAscent.maximum = Math.max(ascent, totalAscent.maximum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            totalDescent.maximum = Math.max(descent, totalDescent.maximum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        int min = (int) Math.min((long) totalAscent.minimum + (long) totalDescent.minimum, Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        int pref = (int) Math.min((long) totalAscent.preferred + (long) totalDescent.preferred, Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        int max = (int) Math.min((long) totalAscent.maximum + (long) totalDescent.maximum, Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        float alignment = 0.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        if (min > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            alignment = (float) totalAscent.minimum / min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            alignment = alignment > 1.0f ? 1.0f : alignment < 0.0f ? 0.0f : alignment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        return new SizeRequirements(min, pref, max, alignment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * Creates a set of offset/span pairs representing how to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * lay out a set of components end-to-end.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * This method requires that you specify
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * the total amount of space to be allocated,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * the size requirements for each component to be placed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * (specified as an array of SizeRequirements), and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * the total size requirement of the set of components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * You can get the total size requirement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * by invoking the getTiledSizeRequirements method.  The components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * will be tiled in the forward direction with offsets increasing from 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * @param allocated the total span to be allocated >= 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * @param total     the total of the children requests.  This argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     *  is optional and may be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * @param children  the size requirements for each component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * @param offsets   the offset from 0 for each child where
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     *   the spans were allocated (determines placement of the span).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * @param spans     the span allocated for each child to make the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     *   total target span.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    public static void calculateTiledPositions(int allocated,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                                               SizeRequirements total,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                                               SizeRequirements[] children,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                                               int[] offsets,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                                               int[] spans) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        calculateTiledPositions(allocated, total, children, offsets, spans, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * Creates a set of offset/span pairs representing how to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * lay out a set of components end-to-end.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * This method requires that you specify
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * the total amount of space to be allocated,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * the size requirements for each component to be placed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * (specified as an array of SizeRequirements), and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * the total size requirement of the set of components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * You can get the total size requirement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * by invoking the getTiledSizeRequirements method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * This method also requires a flag indicating whether components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * should be tiled in the forward direction (offsets increasing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * from 0) or reverse direction (offsets decreasing from the end
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * of the allocated space).  The forward direction represents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * components tiled from left to right or top to bottom.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * reverse direction represents components tiled from right to left
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * or bottom to top.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * @param allocated the total span to be allocated >= 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * @param total     the total of the children requests.  This argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     *  is optional and may be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * @param children  the size requirements for each component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * @param offsets   the offset from 0 for each child where
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     *   the spans were allocated (determines placement of the span).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * @param spans     the span allocated for each child to make the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     *   total target span.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * @param forward   tile with offsets increasing from 0 if true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     *   and with offsets decreasing from the end of the allocated space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     *   if false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    public static void calculateTiledPositions(int allocated,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                                               SizeRequirements total,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                                               SizeRequirements[] children,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                                               int[] offsets,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                                               int[] spans,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                                               boolean forward) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        // The total argument turns out to be a bad idea since the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        // total of all the children can overflow the integer used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        // hold the total.  The total must therefore be calculated and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        // stored in long variables.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        long min = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        long pref = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        long max = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        for (int i = 0; i < children.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            min += children[i].minimum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            pref += children[i].preferred;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            max += children[i].maximum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        if (allocated >= pref) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            expandedTile(allocated, min, pref, max, children, offsets, spans, forward);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            compressedTile(allocated, min, pref, max, children, offsets, spans, forward);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    private static void compressedTile(int allocated, long min, long pref, long max,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                                       SizeRequirements[] request,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                                       int[] offsets, int[] spans,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                                       boolean forward) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        // ---- determine what we have to work with ----
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        float totalPlay = Math.min(pref - allocated, pref - min);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        float factor = (pref - min == 0) ? 0.0f : totalPlay / (pref - min);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        // ---- make the adjustments ----
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        int totalOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        if( forward ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            // lay out with offsets increasing from 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            totalOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            for (int i = 0; i < spans.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                offsets[i] = totalOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                SizeRequirements req = request[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                float play = factor * (req.preferred - req.minimum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                spans[i] = (int)(req.preferred - play);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                totalOffset = (int) Math.min((long) totalOffset + (long) spans[i], Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            // lay out with offsets decreasing from the end of the allocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            totalOffset = allocated;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            for (int i = 0; i < spans.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                SizeRequirements req = request[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                float play = factor * (req.preferred - req.minimum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                spans[i] = (int)(req.preferred - play);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                offsets[i] = totalOffset - spans[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                totalOffset = (int) Math.max((long) totalOffset - (long) spans[i], 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    private static void expandedTile(int allocated, long min, long pref, long max,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                                     SizeRequirements[] request,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                                     int[] offsets, int[] spans,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                                     boolean forward) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        // ---- determine what we have to work with ----
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        float totalPlay = Math.min(allocated - pref, max - pref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        float factor = (max - pref == 0) ? 0.0f : totalPlay / (max - pref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        // ---- make the adjustments ----
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        int totalOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        if( forward ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            // lay out with offsets increasing from 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            totalOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            for (int i = 0; i < spans.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                offsets[i] = totalOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                SizeRequirements req = request[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                int play = (int)(factor * (req.maximum - req.preferred));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                spans[i] = (int) Math.min((long) req.preferred + (long) play, Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                totalOffset = (int) Math.min((long) totalOffset + (long) spans[i], Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            // lay out with offsets decreasing from the end of the allocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            totalOffset = allocated;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            for (int i = 0; i < spans.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                SizeRequirements req = request[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                int play = (int)(factor * (req.maximum - req.preferred));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                spans[i] = (int) Math.min((long) req.preferred + (long) play, Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                offsets[i] = totalOffset - spans[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                totalOffset = (int) Math.max((long) totalOffset - (long) spans[i], 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * Creates a bunch of offset/span pairs specifying how to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * lay out a set of components with the specified alignments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * The resulting span allocations will overlap, with each one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * fitting as well as possible into the given total allocation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * This method requires that you specify
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * the total amount of space to be allocated,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * the size requirements for each component to be placed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * (specified as an array of SizeRequirements), and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * the total size requirements of the set of components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * (only the alignment field of which is actually used).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * You can get the total size requirement by invoking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * getAlignedSizeRequirements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * Normal alignment will be done with an alignment value of 0.0f
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * representing the left/top edge of a component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * @param allocated the total span to be allocated >= 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * @param total     the total of the children requests.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * @param children  the size requirements for each component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * @param offsets   the offset from 0 for each child where
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     *   the spans were allocated (determines placement of the span).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * @param spans     the span allocated for each child to make the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     *   total target span.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    public static void calculateAlignedPositions(int allocated,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                                                 SizeRequirements total,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                                                 SizeRequirements[] children,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                                                 int[] offsets,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                                                 int[] spans) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        calculateAlignedPositions( allocated, total, children, offsets, spans, true );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * Creates a set of offset/span pairs specifying how to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * lay out a set of components with the specified alignments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * The resulting span allocations will overlap, with each one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * fitting as well as possible into the given total allocation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * This method requires that you specify
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * the total amount of space to be allocated,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * the size requirements for each component to be placed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * (specified as an array of SizeRequirements), and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * the total size requirements of the set of components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * (only the alignment field of which is actually used)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * You can get the total size requirement by invoking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * getAlignedSizeRequirements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * This method also requires a flag indicating whether normal or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * reverse alignment should be performed.  With normal alignment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * the value 0.0f represents the left/top edge of the component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * to be aligned.  With reverse alignment, 0.0f represents the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * right/bottom edge.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * @param allocated the total span to be allocated >= 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * @param total     the total of the children requests.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * @param children  the size requirements for each component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * @param offsets   the offset from 0 for each child where
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     *   the spans were allocated (determines placement of the span).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * @param spans     the span allocated for each child to make the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     *   total target span.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * @param normal    when true, the alignment value 0.0f means
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     *   left/top; when false, it means right/bottom.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    public static void calculateAlignedPositions(int allocated,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                                                 SizeRequirements total,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                                                 SizeRequirements[] children,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                                                 int[] offsets,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                                                 int[] spans,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                                                 boolean normal) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        float totalAlignment = normal ? total.alignment : 1.0f - total.alignment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        int totalAscent = (int)(allocated * totalAlignment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        int totalDescent = allocated - totalAscent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        for (int i = 0; i < children.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            SizeRequirements req = children[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            float alignment = normal ? req.alignment : 1.0f - req.alignment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            int maxAscent = (int)(req.maximum * alignment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            int maxDescent = req.maximum - maxAscent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            int ascent = Math.min(totalAscent, maxAscent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            int descent = Math.min(totalDescent, maxDescent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            offsets[i] = totalAscent - ascent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            spans[i] = (int) Math.min((long) ascent + (long) descent, Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    // This method was used by the JTable - which now uses a different technique.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * Adjust a specified array of sizes by a given amount.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * @param delta     an int specifying the size difference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * @param children  an array of SizeRequirements objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * @return an array of ints containing the final size for each item
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    public static int[] adjustSizes(int delta, SizeRequirements[] children) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
      return new int[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
}