    001    /* ===========================================================
    002     * JFreeChart : a free chart 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/jfreechart/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     * TextAnnotation.java
    029     * -------------------
    030     * (C) Copyright 2002-2005, by Object Refinery Limited.
    031     *
    032     * Original Author:  David Gilbert (for Object Refinery Limited);
    033     * Contributor(s):   -;
    034     *
    035     * $Id: TextAnnotation.java,v 1.6.2.2 2005/10/25 16:51:15 mungady Exp $
    036     *
    037     * Changes:
    038     * --------
    039     * 28-Aug-2002 : Version 1 (DG);
    040     * 07-Nov-2002 : Fixed errors reported by Checkstyle, added accessor
    041     *               methods (DG);
    042     * 13-Jan-2003 : Reviewed Javadocs (DG);
    043     * 26-Mar-2003 : Implemented Serializable (DG);
    044     * 02-Jun-2003 : Added anchor and rotation settings (DG);
    045     * 19-Aug-2003 : Added equals() method and implemented Cloneable (DG);
    046     * 29-Sep-2004 : Updated equals() method (DG);
    047     * 06-Jun-2005 : Fixed equals() method to work with GradientPaint (DG);
    048     *
    049     */
    050
    051    package org.jfree.chart.annotations;
    052
    053    import java.awt.Color;
    054    import java.awt.Font;
    055    import java.awt.Paint;
    056    import java.io.IOException;
    057    import java.io.ObjectInputStream;
    058    import java.io.ObjectOutputStream;
    059    import java.io.Serializable;
    060
    061    import org.jfree.io.SerialUtilities;
    062    import org.jfree.ui.TextAnchor;
    063    import org.jfree.util.ObjectUtilities;
    064    import org.jfree.util.PaintUtilities;
    065
    066    /**
    067     * A base class for text annotations.  This class records the content but not
    068     * the location of the annotation.
    069     */
    070    public class TextAnnotation implements Serializable {
    071
    072        /** For serialization. */
    073        private static final long serialVersionUID = 7008912287533127432L;
    074
    075        /** The default font. */
    076        public static final Font DEFAULT_FONT
    077            = new Font("SansSerif", Font.PLAIN, 10);
    078
    079        /** The default paint. */
    080        public static final Paint DEFAULT_PAINT = Color.black;
    081
    082        /** The default text anchor. */
    083        public static final TextAnchor DEFAULT_TEXT_ANCHOR = TextAnchor.CENTER;
    084
    085        /** The default rotation anchor. */
    086        public static final TextAnchor DEFAULT_ROTATION_ANCHOR = TextAnchor.CENTER;
    087
    088        /** The default rotation angle. */
    089        public static final double DEFAULT_ROTATION_ANGLE = 0.0;
    090
    091        /** The text. */
    092        private String text;
    093
    094        /** The font. */
    095        private Font font;
    096
    097        /** The paint. */
    098        private transient Paint paint;
    099
    100        /** The text anchor. */
    101        private TextAnchor textAnchor;
    102
    103        /** The rotation anchor. */
    104        private TextAnchor rotationAnchor;
    105
    106        /** The rotation angle. */
    107        private double rotationAngle;
    108
    109        /**
    110         * Creates a text annotation with default settings.
    111         *
    112         * @param text  the text (<code>null</code> not permitted).
    113         */
    114        protected TextAnnotation(String text) {
    115            if (text == null) {
    116                throw new IllegalArgumentException("Null 'text' argument.");
    117            }
    118            this.text = text;
    119            this.font = DEFAULT_FONT;
    120            this.paint = DEFAULT_PAINT;
    121            this.textAnchor = DEFAULT_TEXT_ANCHOR;
    122            this.rotationAnchor = DEFAULT_ROTATION_ANCHOR;
    123            this.rotationAngle = DEFAULT_ROTATION_ANGLE;
    124        }
    125
    126        /**
    127         * Returns the text for the annotation.
    128         *
    129         * @return The text (never <code>null</code>).
    130         */
    131        public String getText() {
    132            return this.text;
    133        }
    134
    135        /**
    136         * Sets the text for the annotation.
    137         *
    138         * @param text  the text (<code>null</code> not permitted).
    139         */
    140        public void setText(String text) {
    141            this.text = text;
    142        }
    143
    144        /**
    145         * Returns the font for the annotation.
    146         *
    147         * @return The font.
    148         */
    149        public Font getFont() {
    150            return this.font;
    151        }
    152
    153        /**
    154         * Sets the font for the annotation.
    155         *
    156         * @param font  the font.
    157         */
    158        public void setFont(Font font) {
    159            this.font = font;
    160        }
    161
    162        /**
    163         * Returns the paint for the annotation.
    164         *
    165         * @return The paint.
    166         */
    167        public Paint getPaint() {
    168            return this.paint;
    169        }
    170
    171        /**
    172         * Sets the paint for the annotation.
    173         *
    174         * @param paint  the paint.
    175         */
    176        public void setPaint(Paint paint) {
    177            this.paint = paint;
    178        }
    179
    180        /**
    181         * Returns the text anchor.
    182         *
    183         * @return The text anchor.
    184         */
    185        public TextAnchor getTextAnchor() {
    186            return this.textAnchor;
    187        }
    188
    189        /**
    190         * Sets the text anchor (the point on the text bounding rectangle that is
    191         * aligned to the (x, y) coordinate of the annotation).
    192         *
    193         * @param anchor  the anchor point.
    194         */
    195        public void setTextAnchor(TextAnchor anchor) {
    196            this.textAnchor = anchor;
    197        }
    198
    199        /**
    200         * Returns the rotation anchor.
    201         *
    202         * @return The rotation anchor point.
    203         */
    204        public TextAnchor getRotationAnchor() {
    205            return this.rotationAnchor;
    206        }
    207
    208        /**
    209         * Sets the rotation anchor point.
    210         *
    211         * @param anchor  the anchor.
    212         */
    213        public void setRotationAnchor(TextAnchor anchor) {
    214            this.rotationAnchor = anchor;
    215        }
    216
    217        /**
    218         * Returns the rotation angle.
    219         *
    220         * @return The rotation angle.
    221         */
    222        public double getRotationAngle() {
    223            return this.rotationAngle;
    224        }
    225
    226        /**
    227         * Sets the rotation angle.
    228         * <p>
    229         * The angle is measured clockwise in radians.
    230         *
    231         * @param angle  the angle (in radians).
    232         */
    233        public void setRotationAngle(double angle) {
    234            this.rotationAngle = angle;
    235        }
    236
    237        /**
    238         * Tests this object for equality with an arbitrary object.
    239         *
    240         * @param obj  the object (<code>null</code> permitted).
    241         *
    242         * @return <code>true</code> or <code>false</code>.
    243         */
    244        public boolean equals(Object obj) {
    245            if (obj == this) {
    246                return true;
    247            }
    248            // now try to reject equality...
    249            if (!(obj instanceof TextAnnotation)) {
    250                return false;
    251            }
    252            TextAnnotation that = (TextAnnotation) obj;
    253            if (!ObjectUtilities.equal(this.text, that.getText())) {
    254                return false;
    255            }
    256            if (!ObjectUtilities.equal(this.font, that.getFont())) {
    257                return false;
    258            }
    259            if (!PaintUtilities.equal(this.paint, that.getPaint())) {
    260                return false;
    261            }
    262            if (!ObjectUtilities.equal(this.textAnchor, that.getTextAnchor())) {
    263                return false;
    264            }
    265            if (!ObjectUtilities.equal(
    266                this.rotationAnchor, that.getRotationAnchor()
    267            )) {
    268                return false;
    269            }
    270            if (this.rotationAngle != that.getRotationAngle()) {
    271                return false;
    272            }
    273
    274            // seem to be the same...
    275            return true;
    276
    277        }
    278
    279        /**
    280         * Returns a hash code for this instance.
    281         *
    282         * @return A hash code.
    283         */
    284        public int hashCode() {
    285            // TODO: this needs work
    286            return this.text.hashCode();
    287        }
    288
    289        /**
    290         * Provides serialization support.
    291         *
    292         * @param stream  the output stream.
    293         *
    294         * @throws IOException if there is an I/O error.
    295         */
    296        private void writeObject(ObjectOutputStream stream) throws IOException {
    297            stream.defaultWriteObject();
    298            SerialUtilities.writePaint(this.paint, stream);
    299        }
    300
    301        /**
    302         * Provides serialization support.
    303         *
    304         * @param stream  the input stream.
    305         *
    306         * @throws IOException  if there is an I/O error.
    307         * @throws ClassNotFoundException  if there is a classpath problem.
    308         */
    309        private void readObject(ObjectInputStream stream)
    310            throws IOException, ClassNotFoundException {
    311            stream.defaultReadObject();
    312            this.paint = SerialUtilities.readPaint(stream);
    313        }
    314
    315    }