    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
    061            = new RectangleEdge("RectangleEdge.TOP");
    062
    063        /** Bottom. */
    064        public static final RectangleEdge BOTTOM
    065            = new RectangleEdge("RectangleEdge.BOTTOM");
    066
    067        /** Left. */
    068        public static final RectangleEdge LEFT
    069            = new RectangleEdge("RectangleEdge.LEFT");
    070
    071        /** Right. */
    072        public static final RectangleEdge RIGHT
    073            = new RectangleEdge("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            }
    167            else if (edge == RectangleEdge.BOTTOM) {
    168                result = RectangleEdge.TOP;
    169            }
    170            else if (edge == RectangleEdge.LEFT) {
    171                result = RectangleEdge.RIGHT;
    172            }
    173            else if (edge == RectangleEdge.RIGHT) {
    174                result = RectangleEdge.LEFT;
    175            }
    176            return result;
    177        }
    178
    179        /**
    180         * Returns the x or y coordinate of the specified edge.
    181         *
    182         * @param rectangle  the rectangle.
    183         * @param edge  the edge.
    184         *
    185         * @return The coordinate.
    186         */
    187        public static double coordinate(final Rectangle2D rectangle,
    188                                        final RectangleEdge edge) {
    189            double result = 0.0;
    190            if (edge == RectangleEdge.TOP) {
    191                result = rectangle.getMinY();
    192            }
    193            else if (edge == RectangleEdge.BOTTOM) {
    194                result = rectangle.getMaxY();
    195            }
    196            else if (edge == RectangleEdge.LEFT) {
    197                result = rectangle.getMinX();
    198            }
    199            else if (edge == RectangleEdge.RIGHT) {
    200                result = rectangle.getMaxX();
    201            }
    202            return result;
    203        }
    204
    205        /**
    206         * Ensures that serialization returns the unique instances.
    207         *
    208         * @return The object.
    209         *
    210         * @throws ObjectStreamException if there is a problem.
    211         */
    212        private Object readResolve() throws ObjectStreamException {
    213            RectangleEdge result = null;
    214            if (this.equals(RectangleEdge.TOP)) {
    215                result = RectangleEdge.TOP;
    216            }
    217            else if (this.equals(RectangleEdge.BOTTOM)) {
    218                result = RectangleEdge.BOTTOM;
    219            }
    220            else if (this.equals(RectangleEdge.LEFT)) {
    221                result = RectangleEdge.LEFT;
    222            }
    223            else if (this.equals(RectangleEdge.RIGHT)) {
    224                result = RectangleEdge.RIGHT;
    225            }
    226            return result;
    227        }
    228
    229    }