KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > editor > view > spi > ViewInsets


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.editor.view.spi;
21
22 import java.io.Serializable JavaDoc;
23
24 /**
25  * Immutable analogy of the {@link java.awt.Insets} based
26  * on floats.
27  * <br>
28  * Any view implementation may benefit from presence
29  * of this class.
30  *
31  * <p>
32  * As the views of a same type should possibly use the same
33  * insets there is a potential for sharing
34  * of <code>ViewInsets</code> instances.
35  *
36  * @author Miloslav Metelka
37  * @version 1.00
38  */

39
40 public final class ViewInsets implements Serializable JavaDoc {
41
42     public static final ViewInsets ZERO_INSETS = new ViewInsets(0, 0, 0, 0);
43     
44     private float top;
45     
46     private float left;
47     
48     private float bottom;
49     
50     private float right;
51     
52     /**
53      * Creates and initializes a new <code>ViewInsets</code> object with the
54      * specified top, left, bottom, and right insets.
55      * @param top the inset from the top.
56      * @param left the inset from the left.
57      * @param bottom the inset from the bottom.
58      * @param right the inset from the right.
59      */

60     public ViewInsets(float top, float left, float bottom, float right) {
61         this.top = top;
62         this.left = left;
63         this.bottom = bottom;
64         this.right = right;
65     }
66
67     public float getTop() {
68         return top;
69     }
70     
71     public float getLeft() {
72         return left;
73     }
74     
75     public float getBottom() {
76         return bottom;
77     }
78     
79     public float getRight() {
80         return right;
81     }
82
83     public float getLeftRight() {
84         return left + right;
85     }
86     
87     public float getTopBottom() {
88         return top + bottom;
89     }
90
91     /**
92      * Checks whether two float insets objects are equal. Two instances
93      * of <code>ViewInsets</code> are equal if the four float values
94      * of the fields <code>top</code>, <code>left</code>,
95      * <code>bottom</code>, and <code>right</code> are all equal.
96      * @return <code>true</code> if the two float insets are equal;
97      * otherwise <code>false</code>.
98      */

99     public boolean equals(Object JavaDoc obj) {
100         if (obj instanceof ViewInsets) {
101             ViewInsets insets = (ViewInsets)obj;
102             return ((top == insets.top) && (left == insets.left) &&
103             (bottom == insets.bottom) && (right == insets.right));
104         }
105         return false;
106     }
107     
108     /**
109      * Returns the hash code for this Insets.
110      *
111      * @return a hash code for this Insets.
112      */

113     public int hashCode() {
114         float sum1 = left + bottom;
115         float sum2 = right + top;
116         float val1 = sum1 * (sum1 + 1)/2 + left;
117         float val2 = sum2 * (sum2 + 1)/2 + top;
118         int sum3 = (int)(val1 + val2);
119         return sum3 * (sum3 + 1)/2 + (int)val2;
120     }
121     
122     /**
123      * Returns a string representation of this <code>Insets</code> object.
124      * This method is intended to be used only for debugging purposes, and
125      * the content and format of the returned string may vary between
126      * implementations. The returned string may be empty but may not be
127      * <code>null</code>.
128      *
129      * @return a string representation of this <code>Insets</code> object.
130      */

131     public String JavaDoc toString() {
132         return getClass().getName()
133             + "[top=" + top + ",left=" + left // NOI18N
134
+ ",bottom=" + bottom + ",right=" + right + "]"; // NOI18N
135
}
136     
137 }
138
Popular Tags