001: /* ========================================================================
002: * JCommon : a free general purpose class library for the Java(tm) platform
003: * ========================================================================
004: *
005: * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
006: *
007: * Project Info: http://www.jfree.org/jcommon/index.html
008: *
009: * This library is free software; you can redistribute it and/or modify it
010: * under the terms of the GNU Lesser General Public License as published by
011: * the Free Software Foundation; either version 2.1 of the License, or
012: * (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but
015: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017: * License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
022: * USA.
023: *
024: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025: * in the United States and other countries.]
026: *
027: * -------------
028: * RectangleEdge
029: * -------------
030: * (C) Copyright 2003-2005, by Object Refinery Limited.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: RectangleEdge.java,v 1.4 2005/10/18 13:18:34 mungady Exp $
036: *
037: * Changes:
038: * --------
039: * 14-Jul-2003 (DG);
040: *
041: */
042:
043: package org.jfree.ui;
044:
045: import java.awt.geom.Rectangle2D;
046: import java.io.ObjectStreamException;
047: import java.io.Serializable;
048:
049: /**
050: * Used to indicate the edge of a rectangle.
051: *
052: * @author David Gilbert
053: */
054: public final class RectangleEdge implements Serializable {
055:
056: /** For serialization. */
057: private static final long serialVersionUID = -7400988293691093548L;
058:
059: /** Top. */
060: public static final RectangleEdge TOP = new RectangleEdge(
061: "RectangleEdge.TOP");
062:
063: /** Bottom. */
064: public static final RectangleEdge BOTTOM = new RectangleEdge(
065: "RectangleEdge.BOTTOM");
066:
067: /** Left. */
068: public static final RectangleEdge LEFT = new RectangleEdge(
069: "RectangleEdge.LEFT");
070:
071: /** Right. */
072: public static final RectangleEdge RIGHT = new RectangleEdge(
073: "RectangleEdge.RIGHT");
074:
075: /** The name. */
076: private String name;
077:
078: /**
079: * Private constructor.
080: *
081: * @param name the name.
082: */
083: private RectangleEdge(final String name) {
084: this .name = name;
085: }
086:
087: /**
088: * Returns a string representing the object.
089: *
090: * @return The string.
091: */
092: public String toString() {
093: return this .name;
094: }
095:
096: /**
097: * Returns <code>true</code> if this object is equal to the specified
098: * object, and <code>false</code> otherwise.
099: *
100: * @param o the other object.
101: *
102: * @return A boolean.
103: */
104: public boolean equals(final Object o) {
105:
106: if (this == o) {
107: return true;
108: }
109: if (!(o instanceof RectangleEdge)) {
110: return false;
111: }
112:
113: final RectangleEdge order = (RectangleEdge) o;
114: if (!this .name.equals(order.name)) {
115: return false;
116: }
117:
118: return true;
119:
120: }
121:
122: /**
123: * Returns a hash code value for the object.
124: *
125: * @return the hashcode
126: */
127: public int hashCode() {
128: return this .name.hashCode();
129: }
130:
131: /**
132: * Returns <code>true</code> if the edge is <code>TOP</code> or
133: * <code>BOTTOM</code>, and <code>false</code> otherwise.
134: *
135: * @param edge the edge.
136: *
137: * @return A boolean.
138: */
139: public static boolean isTopOrBottom(final RectangleEdge edge) {
140: return (edge == RectangleEdge.TOP || edge == RectangleEdge.BOTTOM);
141: }
142:
143: /**
144: * Returns <code>true</code> if the edge is <code>LEFT</code> or
145: * <code>RIGHT</code>, and <code>false</code> otherwise.
146: *
147: * @param edge the edge.
148: *
149: * @return A boolean.
150: */
151: public static boolean isLeftOrRight(final RectangleEdge edge) {
152: return (edge == RectangleEdge.LEFT || edge == RectangleEdge.RIGHT);
153: }
154:
155: /**
156: * Returns the opposite edge.
157: *
158: * @param edge an edge.
159: *
160: * @return The opposite edge.
161: */
162: public static RectangleEdge opposite(final RectangleEdge edge) {
163: RectangleEdge result = null;
164: if (edge == RectangleEdge.TOP) {
165: result = RectangleEdge.BOTTOM;
166: } else if (edge == RectangleEdge.BOTTOM) {
167: result = RectangleEdge.TOP;
168: } else if (edge == RectangleEdge.LEFT) {
169: result = RectangleEdge.RIGHT;
170: } else if (edge == RectangleEdge.RIGHT) {
171: result = RectangleEdge.LEFT;
172: }
173: return result;
174: }
175:
176: /**
177: * Returns the x or y coordinate of the specified edge.
178: *
179: * @param rectangle the rectangle.
180: * @param edge the edge.
181: *
182: * @return The coordinate.
183: */
184: public static double coordinate(final Rectangle2D rectangle,
185: final RectangleEdge edge) {
186: double result = 0.0;
187: if (edge == RectangleEdge.TOP) {
188: result = rectangle.getMinY();
189: } else if (edge == RectangleEdge.BOTTOM) {
190: result = rectangle.getMaxY();
191: } else if (edge == RectangleEdge.LEFT) {
192: result = rectangle.getMinX();
193: } else if (edge == RectangleEdge.RIGHT) {
194: result = rectangle.getMaxX();
195: }
196: return result;
197: }
198:
199: /**
200: * Ensures that serialization returns the unique instances.
201: *
202: * @return The object.
203: *
204: * @throws ObjectStreamException if there is a problem.
205: */
206: private Object readResolve() throws ObjectStreamException {
207: RectangleEdge result = null;
208: if (this.equals(RectangleEdge.TOP)) {
209: result = RectangleEdge.TOP;
210: } else if (this.equals(RectangleEdge.BOTTOM)) {
211: result = RectangleEdge.BOTTOM;
212: } else if (this.equals(RectangleEdge.LEFT)) {
213: result = RectangleEdge.LEFT;
214: } else if (this.equals(RectangleEdge.RIGHT)) {
215: result = RectangleEdge.RIGHT;
216: }
217: return result;
218: }
219:
220: }
|