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