jdk/src/share/classes/javax/swing/LayoutStyle.java
author dmarkov
Wed, 16 Apr 2014 12:51:25 +0400
changeset 24184 4da2f6ec4dab
parent 5506 202f599c92aa
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
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2005, 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
import java.awt.Container;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import javax.swing.plaf.ComponentUI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import sun.awt.AppContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * <code>LayoutStyle</code> provides information about how to position
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * components.  This class is primarily useful for visual tools and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * layout managers.  Most developers will not need to use this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * You typically don't set or create a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <code>LayoutStyle</code>.  Instead use the static method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <code>getInstance</code> to obtain the current instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
public abstract class LayoutStyle {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
     * Sets the shared instance of <code>LayoutStyle</code>.  Specifying
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     * <code>null</code> results in using the <code>LayoutStyle</code> from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * the current <code>LookAndFeel</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     * @param style the <code>LayoutStyle</code>, or <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * @see #getInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    public static void setInstance(LayoutStyle style) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        synchronized(LayoutStyle.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            if (style == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                AppContext.getAppContext().remove(LayoutStyle.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                AppContext.getAppContext().put(LayoutStyle.class, style);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * Returns the shared instance of <code>LayoutStyle</code>.  If an instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * has not been specified in <code>setInstance</code>, this will return
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * the <code>LayoutStyle</code> from the current <code>LookAndFeel</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * @see LookAndFeel#getLayoutStyle
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * @return the shared instance of <code>LayoutStyle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    public static LayoutStyle getInstance() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        LayoutStyle style;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        synchronized(LayoutStyle.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            style = (LayoutStyle)AppContext.getAppContext().
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                    get(LayoutStyle.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        if (style == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            return UIManager.getLookAndFeel().getLayoutStyle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        return style;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * <code>ComponentPlacement</code> is an enumeration of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * possible ways two components can be placed relative to each
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * other.  <code>ComponentPlacement</code> is used by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * <code>LayoutStyle</code> method <code>getPreferredGap</code>.  Refer to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * <code>LayoutStyle</code> for more information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * @see LayoutStyle#getPreferredGap(JComponent,JComponent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     *      ComponentPlacement,int,Container)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    public enum ComponentPlacement {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
         * Enumeration value indicating the two components are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
         * visually related and will be placed in the same parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
         * For example, a <code>JLabel</code> providing a label for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
         * <code>JTextField</code> is typically visually associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
         * with the <code>JTextField</code>; the constant <code>RELATED</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
         * is used for this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        RELATED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
         * Enumeration value indicating the two components are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
         * visually unrelated and will be placed in the same parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
         * For example, groupings of components are usually visually
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
         * separated; the constant <code>UNRELATED</code> is used for this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        UNRELATED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
         * Enumeration value indicating the distance to indent a component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
         * is being requested.  For example, often times the children of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
         * a label will be horizontally indented from the label.  To determine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
         * the preferred distance for such a gap use the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
         * <code>INDENT</code> type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
         * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
         * This value is typically only useful with a direction of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
         * <code>EAST</code> or <code>WEST</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        INDENT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * Creates a new <code>LayoutStyle</code>.  You generally don't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * create a <code>LayoutStyle</code>.  Instead use the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * <code>getInstance</code> to obtain the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * <code>LayoutStyle</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    public LayoutStyle() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * Returns the amount of space to use between two components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * The return value indicates the distance to place
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * <code>component2</code> relative to <code>component1</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * For example, the following returns the amount of space to place
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * between <code>component2</code> and <code>component1</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * when <code>component2</code> is placed vertically above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * <code>component1</code>:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     *   int gap = getPreferredGap(component1, component2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *                             ComponentPlacement.RELATED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *                             SwingConstants.NORTH, parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * The <code>type</code> parameter indicates the relation between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * the two components.  If the two components will be contained in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * the same parent and are showing similar logically related
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * items, use <code>RELATED</code>.  If the two components will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * contained in the same parent but show logically unrelated items
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * use <code>UNRELATED</code>.  Some look and feels may not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * distinguish between the <code>RELATED</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * <code>UNRELATED</code> types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * The return value is not intended to take into account the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * current size and position of <code>component2</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * <code>component1</code>.  The return value may take into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * consideration various properties of the components.  For
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * example, the space may vary based on font size, or the preferred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * size of the component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @param component1 the <code>JComponent</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *               <code>component2</code> is being placed relative to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @param component2 the <code>JComponent</code> being placed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * @param position the position <code>component2</code> is being placed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     *        relative to <code>component1</code>; one of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     *        <code>SwingConstants.NORTH</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     *        <code>SwingConstants.SOUTH</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     *        <code>SwingConstants.EAST</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     *        <code>SwingConstants.WEST</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @param type how the two components are being placed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * @param parent the parent of <code>component2</code>; this may differ
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     *        from the actual parent and it may be <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @return the amount of space to place between the two components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @throws NullPointerException if <code>component1</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     *         <code>component2</code> or <code>type</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     *         <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @throws IllegalArgumentException if <code>position</code> is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     *         one of <code>SwingConstants.NORTH</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     *         <code>SwingConstants.SOUTH</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     *         <code>SwingConstants.EAST</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     *         <code>SwingConstants.WEST</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * @see LookAndFeel#getLayoutStyle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    public abstract int getPreferredGap(JComponent component1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                                        JComponent component2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                                        ComponentPlacement type, int position,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                                        Container parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * Returns the amount of space to place between the component and specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * edge of its parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * @param component the <code>JComponent</code> being positioned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * @param position the position <code>component</code> is being placed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     *        relative to its parent; one of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *        <code>SwingConstants.NORTH</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     *        <code>SwingConstants.SOUTH</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     *        <code>SwingConstants.EAST</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     *        <code>SwingConstants.WEST</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * @param parent the parent of <code>component</code>; this may differ
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     *        from the actual parent and may be <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * @return the amount of space to place between the component and specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     *         edge
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * @throws IllegalArgumentException if <code>position</code> is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     *         one of <code>SwingConstants.NORTH</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     *         <code>SwingConstants.SOUTH</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     *         <code>SwingConstants.EAST</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     *         <code>SwingConstants.WEST</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    public abstract int getContainerGap(JComponent component, int position,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                                        Container parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
}