001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2017, 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 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
025 * Other names may be trademarks of their respective owners.]
026 *
027 * ----------------
028 * BlockBorder.java
029 * ----------------
030 * (C) Copyright 2004-2017, by Object Refinery Limited.
031 *
032 * Original Author:  David Gilbert (for Object Refinery Limited);
033 * Contributor(s):   -;
034 *
035 * Changes:
036 * --------
037 * 22-Oct-2004 : Version 1 (DG);
038 * 04-Feb-2005 : Added equals() and implemented Serializable (DG);
039 * 23-Feb-2005 : Added attribute for border color (DG);
040 * 06-May-2005 : Added new convenience constructors (DG);
041 * ------------- JFREECHART 1.0.x ---------------------------------------------
042 * 16-Mar-2007 : Implemented BlockFrame (DG);
043 * 02-Jul-2013 : Use ParamChecks (DG);
044 *
045 */
046
047package org.jfree.chart.block;
048
049import java.awt.Color;
050import java.awt.Graphics2D;
051import java.awt.Paint;
052import java.awt.geom.Rectangle2D;
053import java.io.IOException;
054import java.io.ObjectInputStream;
055import java.io.ObjectOutputStream;
056import java.io.Serializable;
057import org.jfree.chart.ui.RectangleInsets;
058import org.jfree.chart.util.PaintUtils;
059import org.jfree.chart.util.Args;
060import org.jfree.chart.util.SerialUtils;
061
062/**
063 * A border for a block.  This class is immutable.
064 */
065public class BlockBorder implements BlockFrame, Serializable {
066
067    /** For serialization. */
068    private static final long serialVersionUID = 4961579220410228283L;
069
070    /** An empty border. */
071    public static final BlockBorder NONE = new BlockBorder(
072            RectangleInsets.ZERO_INSETS, Color.WHITE);
073
074    /** The space reserved for the border. */
075    private RectangleInsets insets;
076
077    /** The border color. */
078    private transient Paint paint;
079
080    /**
081     * Creates a default border.
082     */
083    public BlockBorder() {
084        this(Color.BLACK);
085    }
086
087    /**
088     * Creates a new border with the specified color.
089     *
090     * @param paint  the color ({@code null} not permitted).
091     */
092    public BlockBorder(Paint paint) {
093        this(new RectangleInsets(1, 1, 1, 1), paint);
094    }
095
096    /**
097     * Creates a new border with the specified line widths (in black).
098     *
099     * @param top  the width of the top border.
100     * @param left  the width of the left border.
101     * @param bottom  the width of the bottom border.
102     * @param right  the width of the right border.
103     */
104    public BlockBorder(double top, double left, double bottom, double right) {
105        this(new RectangleInsets(top, left, bottom, right), Color.BLACK);
106    }
107
108    /**
109     * Creates a new border with the specified line widths (in black).
110     *
111     * @param top  the width of the top border.
112     * @param left  the width of the left border.
113     * @param bottom  the width of the bottom border.
114     * @param right  the width of the right border.
115     * @param paint  the border paint ({@code null} not permitted).
116     */
117    public BlockBorder(double top, double left, double bottom, double right,
118                       Paint paint) {
119        this(new RectangleInsets(top, left, bottom, right), paint);
120    }
121
122    /**
123     * Creates a new border.
124     *
125     * @param insets  the border insets ({@code null} not permitted).
126     * @param paint  the paint ({@code null} not permitted).
127     */
128    public BlockBorder(RectangleInsets insets, Paint paint) {
129        Args.nullNotPermitted(insets, "insets");
130        Args.nullNotPermitted(paint, "paint");
131        this.insets = insets;
132        this.paint = paint;
133    }
134
135    /**
136     * Returns the space reserved for the border.
137     *
138     * @return The space (never {@code null}).
139     */
140    @Override
141    public RectangleInsets getInsets() {
142        return this.insets;
143    }
144
145    /**
146     * Returns the paint used to draw the border.
147     *
148     * @return The paint (never {@code null}).
149     */
150    public Paint getPaint() {
151        return this.paint;
152    }
153
154    /**
155     * Draws the border by filling in the reserved space.
156     *
157     * @param g2  the graphics device.
158     * @param area  the area.
159     */
160    @Override
161    public void draw(Graphics2D g2, Rectangle2D area) {
162        // this default implementation will just fill the available
163        // border space with a single color
164        double t = this.insets.calculateTopInset(area.getHeight());
165        double b = this.insets.calculateBottomInset(area.getHeight());
166        double l = this.insets.calculateLeftInset(area.getWidth());
167        double r = this.insets.calculateRightInset(area.getWidth());
168        double x = area.getX();
169        double y = area.getY();
170        double w = area.getWidth();
171        double h = area.getHeight();
172        g2.setPaint(this.paint);
173        Rectangle2D rect = new Rectangle2D.Double();
174        if (t > 0.0) {
175            rect.setRect(x, y, w, t);
176            g2.fill(rect);
177        }
178        if (b > 0.0) {
179            rect.setRect(x, y + h - b, w, b);
180            g2.fill(rect);
181        }
182        if (l > 0.0) {
183            rect.setRect(x, y, l, h);
184            g2.fill(rect);
185        }
186        if (r > 0.0) {
187            rect.setRect(x + w - r, y, r, h);
188            g2.fill(rect);
189        }
190    }
191
192    /**
193     * Tests this border for equality with an arbitrary instance.
194     *
195     * @param obj  the object ({@code null} permitted).
196     *
197     * @return A boolean.
198     */
199    @Override
200    public boolean equals(Object obj) {
201        if (obj == this) {
202            return true;
203        }
204        if (!(obj instanceof BlockBorder)) {
205            return false;
206        }
207        BlockBorder that = (BlockBorder) obj;
208        if (!this.insets.equals(that.insets)) {
209            return false;
210        }
211        if (!PaintUtils.equal(this.paint, that.paint)) {
212            return false;
213        }
214        return true;
215    }
216
217    /**
218     * Provides serialization support.
219     *
220     * @param stream  the output stream.
221     *
222     * @throws IOException  if there is an I/O error.
223     */
224    private void writeObject(ObjectOutputStream stream) throws IOException {
225        stream.defaultWriteObject();
226        SerialUtils.writePaint(this.paint, stream);
227    }
228
229    /**
230     * Provides serialization support.
231     *
232     * @param stream  the input stream.
233     *
234     * @throws IOException  if there is an I/O error.
235     * @throws ClassNotFoundException  if there is a classpath problem.
236     */
237    private void readObject(ObjectInputStream stream)
238        throws IOException, ClassNotFoundException {
239        stream.defaultReadObject();
240        this.paint = SerialUtils.readPaint(stream);
241    }
242
243}