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: * RectangleAnchor.java
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: RectangleAnchor.java,v 1.6 2005/10/18 13:18:34 mungady Exp $
036: *
037: * Changes:
038: * --------
039: * 31-Oct-2003 (DG);
040: * 01-Apr-2004 : Changed java.awt.geom.Dimension2D to org.jfree.ui.Size2D
041: * because of JDK bug 4976448 which persists on JDK 1.3.1 (DG);
042: * 21-Jan-2005 : Changed return type of coordinates() method (DG);
043: *
044: */
045:
046: package org.jfree.ui;
047:
048: import java.awt.geom.Point2D;
049: import java.awt.geom.Rectangle2D;
050: import java.io.ObjectStreamException;
051: import java.io.Serializable;
052:
053: /**
054: * Used to indicate an anchor point for a rectangle.
055: *
056: * @author David Gilbert
057: */
058: public final class RectangleAnchor implements Serializable {
059:
060: /** For serialization. */
061: private static final long serialVersionUID = -2457494205644416327L;
062:
063: /** Center. */
064: public static final RectangleAnchor CENTER = new RectangleAnchor(
065: "RectangleAnchor.CENTER");
066:
067: /** Top. */
068: public static final RectangleAnchor TOP = new RectangleAnchor(
069: "RectangleAnchor.TOP");
070:
071: /** Top-Left. */
072: public static final RectangleAnchor TOP_LEFT = new RectangleAnchor(
073: "RectangleAnchor.TOP_LEFT");
074:
075: /** Top-Right. */
076: public static final RectangleAnchor TOP_RIGHT = new RectangleAnchor(
077: "RectangleAnchor.TOP_RIGHT");
078:
079: /** Bottom. */
080: public static final RectangleAnchor BOTTOM = new RectangleAnchor(
081: "RectangleAnchor.BOTTOM");
082:
083: /** Bottom-Left. */
084: public static final RectangleAnchor BOTTOM_LEFT = new RectangleAnchor(
085: "RectangleAnchor.BOTTOM_LEFT");
086:
087: /** Bottom-Right. */
088: public static final RectangleAnchor BOTTOM_RIGHT = new RectangleAnchor(
089: "RectangleAnchor.BOTTOM_RIGHT");
090:
091: /** Left. */
092: public static final RectangleAnchor LEFT = new RectangleAnchor(
093: "RectangleAnchor.LEFT");
094:
095: /** Right. */
096: public static final RectangleAnchor RIGHT = new RectangleAnchor(
097: "RectangleAnchor.RIGHT");
098:
099: /** The name. */
100: private String name;
101:
102: /**
103: * Private constructor.
104: *
105: * @param name the name.
106: */
107: private RectangleAnchor(final String name) {
108: this .name = name;
109: }
110:
111: /**
112: * Returns a string representing the object.
113: *
114: * @return The string.
115: */
116: public String toString() {
117: return this .name;
118: }
119:
120: /**
121: * Returns <code>true</code> if this object is equal to the specified
122: * object, and <code>false</code> otherwise.
123: *
124: * @param obj the other object (<code>null</code> permitted).
125: *
126: * @return A boolean.
127: */
128: public boolean equals(final Object obj) {
129:
130: if (this == obj) {
131: return true;
132: }
133: if (!(obj instanceof RectangleAnchor)) {
134: return false;
135: }
136:
137: final RectangleAnchor order = (RectangleAnchor) obj;
138: if (!this .name.equals(order.name)) {
139: return false;
140: }
141:
142: return true;
143: }
144:
145: /**
146: * Returns a hash code value for the object.
147: *
148: * @return The hashcode
149: */
150: public int hashCode() {
151: return this .name.hashCode();
152: }
153:
154: /**
155: * Returns the (x, y) coordinates of the specified anchor.
156: *
157: * @param rectangle the rectangle.
158: * @param anchor the anchor.
159: *
160: * @return The (x, y) coordinates.
161: */
162: public static Point2D coordinates(final Rectangle2D rectangle,
163: final RectangleAnchor anchor) {
164: Point2D result = new Point2D.Double();
165: if (anchor == RectangleAnchor.CENTER) {
166: result.setLocation(rectangle.getCenterX(), rectangle
167: .getCenterY());
168: } else if (anchor == RectangleAnchor.TOP) {
169: result.setLocation(rectangle.getCenterX(), rectangle
170: .getMinY());
171: } else if (anchor == RectangleAnchor.BOTTOM) {
172: result.setLocation(rectangle.getCenterX(), rectangle
173: .getMaxY());
174: } else if (anchor == RectangleAnchor.LEFT) {
175: result.setLocation(rectangle.getMinX(), rectangle
176: .getCenterY());
177: } else if (anchor == RectangleAnchor.RIGHT) {
178: result.setLocation(rectangle.getMaxX(), rectangle
179: .getCenterY());
180: } else if (anchor == RectangleAnchor.TOP_LEFT) {
181: result
182: .setLocation(rectangle.getMinX(), rectangle
183: .getMinY());
184: } else if (anchor == RectangleAnchor.TOP_RIGHT) {
185: result
186: .setLocation(rectangle.getMaxX(), rectangle
187: .getMinY());
188: } else if (anchor == RectangleAnchor.BOTTOM_LEFT) {
189: result
190: .setLocation(rectangle.getMinX(), rectangle
191: .getMaxY());
192: } else if (anchor == RectangleAnchor.BOTTOM_RIGHT) {
193: result
194: .setLocation(rectangle.getMaxX(), rectangle
195: .getMaxY());
196: }
197: return result;
198: }
199:
200: /**
201: * Creates a new rectangle with the specified dimensions that is aligned to
202: * the given anchor point <code>(anchorX, anchorY)</code>.
203: *
204: * @param dimensions the dimensions (<code>null</code> not permitted).
205: * @param anchorX the x-anchor.
206: * @param anchorY the y-anchor.
207: * @param anchor the anchor (<code>null</code> not permitted).
208: *
209: * @return A rectangle.
210: */
211: public static Rectangle2D createRectangle(final Size2D dimensions,
212: final double anchorX, final double anchorY,
213: final RectangleAnchor anchor) {
214: Rectangle2D result = null;
215: final double w = dimensions.getWidth();
216: final double h = dimensions.getHeight();
217: if (anchor == RectangleAnchor.CENTER) {
218: result = new Rectangle2D.Double(anchorX - w / 2.0, anchorY
219: - h / 2.0, w, h);
220: } else if (anchor == RectangleAnchor.TOP) {
221: result = new Rectangle2D.Double(anchorX - w / 2.0, anchorY
222: - h / 2.0, w, h);
223: } else if (anchor == RectangleAnchor.BOTTOM) {
224: result = new Rectangle2D.Double(anchorX - w / 2.0, anchorY
225: - h / 2.0, w, h);
226: } else if (anchor == RectangleAnchor.LEFT) {
227: result = new Rectangle2D.Double(anchorX, anchorY - h / 2.0,
228: w, h);
229: } else if (anchor == RectangleAnchor.RIGHT) {
230: result = new Rectangle2D.Double(anchorX - w, anchorY - h
231: / 2.0, w, h);
232: } else if (anchor == RectangleAnchor.TOP_LEFT) {
233: result = new Rectangle2D.Double(anchorX - w / 2.0, anchorY
234: - h / 2.0, w, h);
235: } else if (anchor == RectangleAnchor.TOP_RIGHT) {
236: result = new Rectangle2D.Double(anchorX - w / 2.0, anchorY
237: - h / 2.0, w, h);
238: } else if (anchor == RectangleAnchor.BOTTOM_LEFT) {
239: result = new Rectangle2D.Double(anchorX - w / 2.0, anchorY
240: - h / 2.0, w, h);
241: } else if (anchor == RectangleAnchor.BOTTOM_RIGHT) {
242: result = new Rectangle2D.Double(anchorX - w / 2.0, anchorY
243: - h / 2.0, w, h);
244: }
245: return result;
246: }
247:
248: /**
249: * Ensures that serialization returns the unique instances.
250: *
251: * @return The object.
252: *
253: * @throws ObjectStreamException if there is a problem.
254: */
255: private Object readResolve() throws ObjectStreamException {
256: RectangleAnchor result = null;
257: if (this.equals(RectangleAnchor.CENTER)) {
258: result = RectangleAnchor.CENTER;
259: } else if (this.equals(RectangleAnchor.TOP)) {
260: result = RectangleAnchor.TOP;
261: } else if (this.equals(RectangleAnchor.BOTTOM)) {
262: result = RectangleAnchor.BOTTOM;
263: } else if (this.equals(RectangleAnchor.LEFT)) {
264: result = RectangleAnchor.LEFT;
265: } else if (this.equals(RectangleAnchor.RIGHT)) {
266: result = RectangleAnchor.RIGHT;
267: } else if (this.equals(RectangleAnchor.TOP_LEFT)) {
268: result = RectangleAnchor.TOP_LEFT;
269: } else if (this.equals(RectangleAnchor.TOP_RIGHT)) {
270: result = RectangleAnchor.TOP_RIGHT;
271: } else if (this.equals(RectangleAnchor.BOTTOM_LEFT)) {
272: result = RectangleAnchor.BOTTOM_LEFT;
273: } else if (this.equals(RectangleAnchor.BOTTOM_RIGHT)) {
274: result = RectangleAnchor.BOTTOM_RIGHT;
275: }
276: return result;
277: }
278:
279: }
|