jdk/src/share/classes/javax/swing/SizeRequirements.java
author dmarkov
Wed, 16 Apr 2014 12:51:25 +0400
changeset 24184 4da2f6ec4dab
parent 22574 7f8ce0c8c20a
child 25201 4adc75e0c4e5
permissions -rw-r--r--
8032874: ArrayIndexOutOfBoundsException in JTable while clearing data in JTable Reviewed-by: alexp, alexsch
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 20458
diff changeset
     2
 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
2
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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
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
20458
f2423fb3fd19 8025840: Fix all the doclint warnings about trademark
cl
parents: 20157
diff changeset
    84
 * of all JavaBeans&trade;
2
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
 */
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 20458
diff changeset
    96
@SuppressWarnings("serial") // Same-version serialization only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
public class SizeRequirements implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * The minimum size required.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * For a component <code>comp</code>, this should be equal to either
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * <code>comp.getMinimumSize().width</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * <code>comp.getMinimumSize().height</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    public int minimum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * The preferred (natural) size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * For a component <code>comp</code>, this should be equal to either
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * <code>comp.getPreferredSize().width</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * <code>comp.getPreferredSize().height</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    public int preferred;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * The maximum size allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * For a component <code>comp</code>, this should be equal to either
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * <code>comp.getMaximumSize().width</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * <code>comp.getMaximumSize().height</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    public int maximum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * The alignment, specified as a value between 0.0 and 1.0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * inclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * To specify centering, the alignment should be 0.5.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    public float alignment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * Creates a SizeRequirements object with the minimum, preferred,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * and maximum sizes set to zero and an alignment value of 0.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * (centered).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    public SizeRequirements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        minimum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        preferred = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        maximum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        alignment = 0.5f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * Creates a SizeRequirements object with the specified minimum, preferred,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * and maximum sizes and the specified alignment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     *
20157
cafca01a8e28 8025230: [cleanup] some more javadoc formatting fixes for swing
yan
parents: 5506
diff changeset
   146
     * @param min the minimum size &gt;= 0
cafca01a8e28 8025230: [cleanup] some more javadoc formatting fixes for swing
yan
parents: 5506
diff changeset
   147
     * @param pref the preferred size &gt;= 0
cafca01a8e28 8025230: [cleanup] some more javadoc formatting fixes for swing
yan
parents: 5506
diff changeset
   148
     * @param max the maximum size &gt;= 0
cafca01a8e28 8025230: [cleanup] some more javadoc formatting fixes for swing
yan
parents: 5506
diff changeset
   149
     * @param a the alignment &gt;= 0.0f &amp;&amp; &lt;= 1.0f
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    public SizeRequirements(int min, int pref, int max, float a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        minimum = min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        preferred = pref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        maximum = max;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        alignment = a > 1.0f ? 1.0f : a < 0.0f ? 0.0f : a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * Returns a string describing the minimum, preferred, and maximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * size requirements, along with the alignment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @return the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        return "[" + minimum + "," + preferred + "," + maximum + "]@" + alignment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * Determines the total space necessary to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * place a set of components end-to-end.  The needs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * of each component in the set are represented by an entry in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * passed-in SizeRequirements array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * The returned SizeRequirements object has an alignment of 0.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * (centered).  The space requirement is never more than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * Integer.MAX_VALUE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @param children  the space requirements for a set of components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     *   The vector may be of zero length, which will result in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     *   default SizeRequirements object instance being passed back.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @return  the total space requirements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    public static SizeRequirements getTiledSizeRequirements(SizeRequirements[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                                                            children) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        SizeRequirements total = new SizeRequirements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        for (int i = 0; i < children.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            SizeRequirements req = children[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            total.minimum = (int) Math.min((long) total.minimum + (long) req.minimum, Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            total.preferred = (int) Math.min((long) total.preferred + (long) req.preferred, Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            total.maximum = (int) Math.min((long) total.maximum + (long) req.maximum, Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        return total;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * Determines the total space necessary to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * align a set of components.  The needs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * of each component in the set are represented by an entry in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * passed-in SizeRequirements array.  The total space required will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * never be more than Integer.MAX_VALUE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * @param children  the set of child requirements.  If of zero length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     *  the returns result will be a default instance of SizeRequirements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * @return  the total space requirements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    public static SizeRequirements getAlignedSizeRequirements(SizeRequirements[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                                                              children) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        SizeRequirements totalAscent = new SizeRequirements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        SizeRequirements totalDescent = new SizeRequirements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        for (int i = 0; i < children.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            SizeRequirements req = children[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            int ascent = (int) (req.alignment * req.minimum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            int descent = req.minimum - ascent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            totalAscent.minimum = Math.max(ascent, totalAscent.minimum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            totalDescent.minimum = Math.max(descent, totalDescent.minimum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            ascent = (int) (req.alignment * req.preferred);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            descent = req.preferred - ascent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            totalAscent.preferred = Math.max(ascent, totalAscent.preferred);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            totalDescent.preferred = Math.max(descent, totalDescent.preferred);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            ascent = (int) (req.alignment * req.maximum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            descent = req.maximum - ascent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            totalAscent.maximum = Math.max(ascent, totalAscent.maximum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            totalDescent.maximum = Math.max(descent, totalDescent.maximum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        int min = (int) Math.min((long) totalAscent.minimum + (long) totalDescent.minimum, Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        int pref = (int) Math.min((long) totalAscent.preferred + (long) totalDescent.preferred, Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        int max = (int) Math.min((long) totalAscent.maximum + (long) totalDescent.maximum, Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        float alignment = 0.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        if (min > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            alignment = (float) totalAscent.minimum / min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            alignment = alignment > 1.0f ? 1.0f : alignment < 0.0f ? 0.0f : alignment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        return new SizeRequirements(min, pref, max, alignment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * Creates a set of offset/span pairs representing how to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * lay out a set of components end-to-end.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * This method requires that you specify
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * the total amount of space to be allocated,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * the size requirements for each component to be placed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * (specified as an array of SizeRequirements), and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * the total size requirement of the set of components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * You can get the total size requirement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * by invoking the getTiledSizeRequirements method.  The components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * will be tiled in the forward direction with offsets increasing from 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     *
20157
cafca01a8e28 8025230: [cleanup] some more javadoc formatting fixes for swing
yan
parents: 5506
diff changeset
   250
     * @param allocated the total span to be allocated &gt;= 0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * @param total     the total of the children requests.  This argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     *  is optional and may be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * @param children  the size requirements for each component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * @param offsets   the offset from 0 for each child where
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     *   the spans were allocated (determines placement of the span).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * @param spans     the span allocated for each child to make the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     *   total target span.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    public static void calculateTiledPositions(int allocated,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                                               SizeRequirements total,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                                               SizeRequirements[] children,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                                               int[] offsets,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                                               int[] spans) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        calculateTiledPositions(allocated, total, children, offsets, spans, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * Creates a set of offset/span pairs representing how to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * lay out a set of components end-to-end.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * This method requires that you specify
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * the total amount of space to be allocated,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * the size requirements for each component to be placed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * (specified as an array of SizeRequirements), and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * the total size requirement of the set of components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * You can get the total size requirement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * by invoking the getTiledSizeRequirements method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * This method also requires a flag indicating whether components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * should be tiled in the forward direction (offsets increasing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * from 0) or reverse direction (offsets decreasing from the end
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * of the allocated space).  The forward direction represents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * components tiled from left to right or top to bottom.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * reverse direction represents components tiled from right to left
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * or bottom to top.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     *
20157
cafca01a8e28 8025230: [cleanup] some more javadoc formatting fixes for swing
yan
parents: 5506
diff changeset
   286
     * @param allocated the total span to be allocated &gt;= 0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * @param total     the total of the children requests.  This argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     *  is optional and may be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * @param children  the size requirements for each component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * @param offsets   the offset from 0 for each child where
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     *   the spans were allocated (determines placement of the span).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * @param spans     the span allocated for each child to make the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *   total target span.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * @param forward   tile with offsets increasing from 0 if true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     *   and with offsets decreasing from the end of the allocated space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     *   if false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    public static void calculateTiledPositions(int allocated,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                                               SizeRequirements total,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                                               SizeRequirements[] children,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                                               int[] offsets,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                                               int[] spans,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                                               boolean forward) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        // The total argument turns out to be a bad idea since the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        // total of all the children can overflow the integer used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        // hold the total.  The total must therefore be calculated and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        // stored in long variables.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        long min = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        long pref = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        long max = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        for (int i = 0; i < children.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            min += children[i].minimum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            pref += children[i].preferred;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            max += children[i].maximum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        if (allocated >= pref) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            expandedTile(allocated, min, pref, max, children, offsets, spans, forward);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            compressedTile(allocated, min, pref, max, children, offsets, spans, forward);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    private static void compressedTile(int allocated, long min, long pref, long max,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                                       SizeRequirements[] request,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                                       int[] offsets, int[] spans,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                                       boolean forward) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        // ---- determine what we have to work with ----
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        float totalPlay = Math.min(pref - allocated, pref - min);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        float factor = (pref - min == 0) ? 0.0f : totalPlay / (pref - min);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        // ---- make the adjustments ----
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        int totalOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        if( forward ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            // lay out with offsets increasing from 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            totalOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            for (int i = 0; i < spans.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                offsets[i] = totalOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                SizeRequirements req = request[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                float play = factor * (req.preferred - req.minimum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                spans[i] = (int)(req.preferred - play);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                totalOffset = (int) Math.min((long) totalOffset + (long) spans[i], Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            // lay out with offsets decreasing from the end of the allocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            totalOffset = allocated;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            for (int i = 0; i < spans.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                SizeRequirements req = request[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                float play = factor * (req.preferred - req.minimum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                spans[i] = (int)(req.preferred - play);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                offsets[i] = totalOffset - spans[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                totalOffset = (int) Math.max((long) totalOffset - (long) spans[i], 0);
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    private static void expandedTile(int allocated, long min, long pref, long max,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                                     SizeRequirements[] request,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                                     int[] offsets, int[] spans,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                                     boolean forward) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        // ---- determine what we have to work with ----
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        float totalPlay = Math.min(allocated - pref, max - pref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        float factor = (max - pref == 0) ? 0.0f : totalPlay / (max - pref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        // ---- make the adjustments ----
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        int totalOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        if( forward ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            // lay out with offsets increasing from 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            totalOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            for (int i = 0; i < spans.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                offsets[i] = totalOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                SizeRequirements req = request[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                int play = (int)(factor * (req.maximum - req.preferred));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                spans[i] = (int) Math.min((long) req.preferred + (long) play, Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                totalOffset = (int) Math.min((long) totalOffset + (long) spans[i], Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            // lay out with offsets decreasing from the end of the allocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            totalOffset = allocated;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            for (int i = 0; i < spans.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                SizeRequirements req = request[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                int play = (int)(factor * (req.maximum - req.preferred));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                spans[i] = (int) Math.min((long) req.preferred + (long) play, Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                offsets[i] = totalOffset - spans[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                totalOffset = (int) Math.max((long) totalOffset - (long) spans[i], 0);
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * Creates a bunch of offset/span pairs specifying how to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * lay out a set of components with the specified alignments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * The resulting span allocations will overlap, with each one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * fitting as well as possible into the given total allocation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * This method requires that you specify
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * the total amount of space to be allocated,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * the size requirements for each component to be placed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * (specified as an array of SizeRequirements), and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * the total size requirements of the set of components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * (only the alignment field of which is actually used).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * You can get the total size requirement by invoking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * getAlignedSizeRequirements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * Normal alignment will be done with an alignment value of 0.0f
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * representing the left/top edge of a component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     *
20157
cafca01a8e28 8025230: [cleanup] some more javadoc formatting fixes for swing
yan
parents: 5506
diff changeset
   409
     * @param allocated the total span to be allocated &gt;= 0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * @param total     the total of the children requests.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * @param children  the size requirements for each component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * @param offsets   the offset from 0 for each child where
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     *   the spans were allocated (determines placement of the span).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * @param spans     the span allocated for each child to make the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     *   total target span.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    public static void calculateAlignedPositions(int allocated,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                                                 SizeRequirements total,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                                                 SizeRequirements[] children,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                                                 int[] offsets,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                                                 int[] spans) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        calculateAlignedPositions( allocated, total, children, offsets, spans, true );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * Creates a set of offset/span pairs specifying how to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * lay out a set of components with the specified alignments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * The resulting span allocations will overlap, with each one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * fitting as well as possible into the given total allocation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * This method requires that you specify
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * the total amount of space to be allocated,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * the size requirements for each component to be placed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * (specified as an array of SizeRequirements), and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * the total size requirements of the set of components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * (only the alignment field of which is actually used)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * You can get the total size requirement by invoking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * getAlignedSizeRequirements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * This method also requires a flag indicating whether normal or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * reverse alignment should be performed.  With normal alignment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * the value 0.0f represents the left/top edge of the component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * to be aligned.  With reverse alignment, 0.0f represents the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * right/bottom edge.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     *
20157
cafca01a8e28 8025230: [cleanup] some more javadoc formatting fixes for swing
yan
parents: 5506
diff changeset
   445
     * @param allocated the total span to be allocated &gt;= 0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * @param total     the total of the children requests.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * @param children  the size requirements for each component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * @param offsets   the offset from 0 for each child where
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     *   the spans were allocated (determines placement of the span).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * @param spans     the span allocated for each child to make the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     *   total target span.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * @param normal    when true, the alignment value 0.0f means
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     *   left/top; when false, it means right/bottom.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    public static void calculateAlignedPositions(int allocated,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                                                 SizeRequirements total,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                                                 SizeRequirements[] children,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                                                 int[] offsets,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                                                 int[] spans,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                                                 boolean normal) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        float totalAlignment = normal ? total.alignment : 1.0f - total.alignment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        int totalAscent = (int)(allocated * totalAlignment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        int totalDescent = allocated - totalAscent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        for (int i = 0; i < children.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            SizeRequirements req = children[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            float alignment = normal ? req.alignment : 1.0f - req.alignment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            int maxAscent = (int)(req.maximum * alignment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            int maxDescent = req.maximum - maxAscent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            int ascent = Math.min(totalAscent, maxAscent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            int descent = Math.min(totalDescent, maxDescent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            offsets[i] = totalAscent - ascent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            spans[i] = (int) Math.min((long) ascent + (long) descent, Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    // This method was used by the JTable - which now uses a different technique.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * Adjust a specified array of sizes by a given amount.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * @param delta     an int specifying the size difference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * @param children  an array of SizeRequirements objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * @return an array of ints containing the final size for each item
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    public static int[] adjustSizes(int delta, SizeRequirements[] children) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
      return new int[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
}