001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.editor.view.spi;
043:
044: import java.io.Serializable;
045:
046: /**
047: * Immutable analogy of the {@link java.awt.Insets} based
048: * on floats.
049: * <br>
050: * Any view implementation may benefit from presence
051: * of this class.
052: *
053: * <p>
054: * As the views of a same type should possibly use the same
055: * insets there is a potential for sharing
056: * of <code>ViewInsets</code> instances.
057: *
058: * @author Miloslav Metelka
059: * @version 1.00
060: */
061:
062: public final class ViewInsets implements Serializable {
063:
064: public static final ViewInsets ZERO_INSETS = new ViewInsets(0, 0,
065: 0, 0);
066:
067: private float top;
068:
069: private float left;
070:
071: private float bottom;
072:
073: private float right;
074:
075: /**
076: * Creates and initializes a new <code>ViewInsets</code> object with the
077: * specified top, left, bottom, and right insets.
078: * @param top the inset from the top.
079: * @param left the inset from the left.
080: * @param bottom the inset from the bottom.
081: * @param right the inset from the right.
082: */
083: public ViewInsets(float top, float left, float bottom, float right) {
084: this .top = top;
085: this .left = left;
086: this .bottom = bottom;
087: this .right = right;
088: }
089:
090: public float getTop() {
091: return top;
092: }
093:
094: public float getLeft() {
095: return left;
096: }
097:
098: public float getBottom() {
099: return bottom;
100: }
101:
102: public float getRight() {
103: return right;
104: }
105:
106: public float getLeftRight() {
107: return left + right;
108: }
109:
110: public float getTopBottom() {
111: return top + bottom;
112: }
113:
114: /**
115: * Checks whether two float insets objects are equal. Two instances
116: * of <code>ViewInsets</code> are equal if the four float values
117: * of the fields <code>top</code>, <code>left</code>,
118: * <code>bottom</code>, and <code>right</code> are all equal.
119: * @return <code>true</code> if the two float insets are equal;
120: * otherwise <code>false</code>.
121: */
122: public boolean equals(Object obj) {
123: if (obj instanceof ViewInsets) {
124: ViewInsets insets = (ViewInsets) obj;
125: return ((top == insets.top) && (left == insets.left)
126: && (bottom == insets.bottom) && (right == insets.right));
127: }
128: return false;
129: }
130:
131: /**
132: * Returns the hash code for this Insets.
133: *
134: * @return a hash code for this Insets.
135: */
136: public int hashCode() {
137: float sum1 = left + bottom;
138: float sum2 = right + top;
139: float val1 = sum1 * (sum1 + 1) / 2 + left;
140: float val2 = sum2 * (sum2 + 1) / 2 + top;
141: int sum3 = (int) (val1 + val2);
142: return sum3 * (sum3 + 1) / 2 + (int) val2;
143: }
144:
145: /**
146: * Returns a string representation of this <code>Insets</code> object.
147: * This method is intended to be used only for debugging purposes, and
148: * the content and format of the returned string may vary between
149: * implementations. The returned string may be empty but may not be
150: * <code>null</code>.
151: *
152: * @return a string representation of this <code>Insets</code> object.
153: */
154: public String toString() {
155: return getClass().getName() + "[top=" + top + ",left=" + left // NOI18N
156: + ",bottom=" + bottom + ",right=" + right + "]"; // NOI18N
157: }
158:
159: }
|