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