jdk/src/share/classes/java/awt/Insets.java
author martin
Mon, 10 Mar 2008 14:32:51 -0700
changeset 48 dc5744ca15ea
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
4960438: (process) Need IO redirection API for subprocesses Reviewed-by: alanb, iris
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1995-2003 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.awt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * An <code>Insets</code> object is a representation of the borders
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * of a container. It specifies the space that a container must leave
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * at each of its edges. The space can be a border, a blank space, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * a title.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * @author      Arthur van Hoff
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * @author      Sami Shaio
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * @see         java.awt.LayoutManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * @see         java.awt.Container
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @since       JDK1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
public class Insets implements Cloneable, java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
     * The inset from the top.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
     * This value is added to the Top of the rectangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     * to yield a new location for the Top.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     * @see #clone()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    public int top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * The inset from the left.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * This value is added to the Left of the rectangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * to yield a new location for the Left edge.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * @see #clone()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    public int left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * The inset from the bottom.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * This value is subtracted from the Bottom of the rectangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * to yield a new location for the Bottom.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * @see #clone()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    public int bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * The inset from the right.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * This value is subtracted from the Right of the rectangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * to yield a new location for the Right edge.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * @see #clone()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    public int right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * JDK 1.1 serialVersionUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private static final long serialVersionUID = -2272572637695466749L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        /* ensure that the necessary native libraries are loaded */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        Toolkit.loadLibraries();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        if (!GraphicsEnvironment.isHeadless()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * Creates and initializes a new <code>Insets</code> object with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * specified top, left, bottom, and right insets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @param       top   the inset from the top.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * @param       left   the inset from the left.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * @param       bottom   the inset from the bottom.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @param       right   the inset from the right.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    public Insets(int top, int left, int bottom, int right) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        this.top = top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        this.left = left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        this.bottom = bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        this.right = right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * Set top, left, bottom, and right to the specified values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @param       top   the inset from the top.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @param       left   the inset from the left.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @param       bottom   the inset from the bottom.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @param       right   the inset from the right.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    public void set(int top, int left, int bottom, int right) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        this.top = top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        this.left = left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        this.bottom = bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        this.right = right;
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
     * Checks whether two insets objects are equal. Two instances
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * of <code>Insets</code> are equal if the four integer values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * of the fields <code>top</code>, <code>left</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * <code>bottom</code>, and <code>right</code> are all equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @return      <code>true</code> if the two insets are equal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *                          otherwise <code>false</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @since       JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        if (obj instanceof Insets) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            Insets insets = (Insets)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            return ((top == insets.top) && (left == insets.left) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                    (bottom == insets.bottom) && (right == insets.right));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * Returns the hash code for this Insets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @return    a hash code for this Insets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        int sum1 = left + bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        int sum2 = right + top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        int val1 = sum1 * (sum1 + 1)/2 + left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        int val2 = sum2 * (sum2 + 1)/2 + top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        int sum3 = val1 + val2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        return sum3 * (sum3 + 1)/2 + val2;
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 representation of this <code>Insets</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * This method is intended to be used only for debugging purposes, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * the content and format of the returned string may vary between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * implementations. The returned string may be empty but may not be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @return  a string representation of this <code>Insets</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        return getClass().getName() + "[top="  + top + ",left=" + left + ",bottom=" + bottom + ",right=" + right + "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * Create a copy of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * @return     a copy of this <code>Insets</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            return super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        } catch (CloneNotSupportedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            // this shouldn't happen, since we are Cloneable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * Initialize JNI field and method IDs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    private static native void initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
}