001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2008, 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 * LineBorder.java
029 * ---------------
030 * (C) Copyright 2007, 2008, by Christo Zietsman and Contributors.
031 *
032 * Original Author:  Christo Zietsman;
033 * Contributor(s):   David Gilbert (for Object Refinery Limited);
034 *
035 * Changes:
036 * --------
037 * 16-Mar-2007 : Version 1, contributed by Christo Zietsman with
038 *               modifications by DG (DG);
039 * 13-Jun-2007 : Don't draw if area doesn't have positive dimensions (DG);
040 *
041 */
042
043package org.jfree.chart.block;
044
045import java.awt.BasicStroke;
046import java.awt.Color;
047import java.awt.Graphics2D;
048import java.awt.Paint;
049import java.awt.Stroke;
050import java.awt.geom.Line2D;
051import java.awt.geom.Rectangle2D;
052import java.io.IOException;
053import java.io.ObjectInputStream;
054import java.io.ObjectOutputStream;
055import java.io.Serializable;
056
057import org.jfree.io.SerialUtilities;
058import org.jfree.ui.RectangleInsets;
059import org.jfree.util.ObjectUtilities;
060import org.jfree.util.PaintUtilities;
061
062/**
063 * A line border for any {@link AbstractBlock}.
064 *
065 * @since 1.0.5
066 */
067public class LineBorder implements BlockFrame, Serializable {
068
069    /** For serialization. */
070    static final long serialVersionUID = 4630356736707233924L;
071
072    /** The line color. */
073    private transient Paint paint;
074
075    /** The line stroke. */
076    private transient Stroke stroke;
077
078    /** The insets. */
079    private RectangleInsets insets;
080
081    /**
082     * Creates a default border.
083     */
084    public LineBorder() {
085        this(Color.black, new BasicStroke(1.0f), new RectangleInsets(1.0, 1.0,
086                1.0, 1.0));
087    }
088
089    /**
090     * Creates a new border with the specified color.
091     *
092     * @param paint  the color (<code>null</code> not permitted).
093     * @param stroke  the border stroke (<code>null</code> not permitted).
094     * @param insets  the insets (<code>null</code> not permitted).
095     */
096    public LineBorder(Paint paint, Stroke stroke, RectangleInsets insets) {
097        if (paint == null) {
098            throw new IllegalArgumentException("Null 'paint' argument.");
099        }
100        if (stroke == null) {
101            throw new IllegalArgumentException("Null 'stroke' argument.");
102        }
103        if (insets == null) {
104            throw new IllegalArgumentException("Null 'insets' argument.");
105        }
106        this.paint = paint;
107        this.stroke = stroke;
108        this.insets = insets;
109    }
110
111    /**
112     * Returns the paint.
113     *
114     * @return The paint (never <code>null</code>).
115     */
116    public Paint getPaint() {
117        return this.paint;
118    }
119
120    /**
121     * Returns the insets.
122     *
123     * @return The insets (never <code>null</code>).
124     */
125    public RectangleInsets getInsets() {
126        return this.insets;
127    }
128
129    /**
130     * Returns the stroke.
131     *
132     * @return The stroke (never <code>null</code>).
133     */
134    public Stroke getStroke() {
135        return this.stroke;
136    }
137
138    /**
139     * Draws the border by filling in the reserved space (in black).
140     *
141     * @param g2  the graphics device.
142     * @param area  the area.
143     */
144    public void draw(Graphics2D g2, Rectangle2D area) {
145        double w = area.getWidth();
146        double h = area.getHeight();
147        // if the area has zero height or width, we shouldn't draw anything
148        if (w <= 0.0 || h <= 0.0) {
149            return;
150        }
151        double t = this.insets.calculateTopInset(h);
152        double b = this.insets.calculateBottomInset(h);
153        double l = this.insets.calculateLeftInset(w);
154        double r = this.insets.calculateRightInset(w);
155        double x = area.getX();
156        double y = area.getY();
157        double x0 = x + l / 2.0;
158        double x1 = x + w - r / 2.0;
159        double y0 = y + h - b / 2.0;
160        double y1 = y + t / 2.0;
161        g2.setPaint(getPaint());
162        g2.setStroke(getStroke());
163        Line2D line = new Line2D.Double();
164        if (t > 0.0) {
165            line.setLine(x0, y1, x1, y1);
166            g2.draw(line);
167        }
168        if (b > 0.0) {
169            line.setLine(x0, y0, x1, y0);
170            g2.draw(line);
171        }
172        if (l > 0.0) {
173            line.setLine(x0, y0, x0, y1);
174            g2.draw(line);
175        }
176        if (r > 0.0) {
177            line.setLine(x1, y0, x1, y1);
178            g2.draw(line);
179        }
180    }
181
182    /**
183     * Tests this border for equality with an arbitrary instance.
184     *
185     * @param obj  the object (<code>null</code> permitted).
186     *
187     * @return A boolean.
188     */
189    public boolean equals(Object obj) {
190        if (obj == this) {
191            return true;
192        }
193        if (!(obj instanceof LineBorder)) {
194            return false;
195        }
196        LineBorder that = (LineBorder) obj;
197        if (!PaintUtilities.equal(this.paint, that.paint)) {
198            return false;
199        }
200        if (!ObjectUtilities.equal(this.stroke, that.stroke)) {
201            return false;
202        }
203        if (!this.insets.equals(that.insets)) {
204            return false;
205        }
206        return true;
207    }
208
209    /**
210     * Provides serialization support.
211     *
212     * @param stream  the output stream.
213     *
214     * @throws IOException  if there is an I/O error.
215     */
216    private void writeObject(ObjectOutputStream stream) throws IOException {
217        stream.defaultWriteObject();
218        SerialUtilities.writePaint(this.paint, stream);
219        SerialUtilities.writeStroke(this.stroke, stream);
220    }
221
222    /**
223     * Provides serialization support.
224     *
225     * @param stream  the input stream.
226     *
227     * @throws IOException  if there is an I/O error.
228     * @throws ClassNotFoundException  if there is a classpath problem.
229     */
230    private void readObject(ObjectInputStream stream)
231        throws IOException, ClassNotFoundException {
232        stream.defaultReadObject();
233        this.paint = SerialUtilities.readPaint(stream);
234        this.stroke = SerialUtilities.readStroke(stream);
235    }
236}
237
238