Frames | No Frames |
1: /* =========================================================== 2: * JFreeChart : a free chart library for the Java(tm) platform 3: * =========================================================== 4: * 5: * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors. 6: * 7: * Project Info: http://www.jfree.org/jfreechart/index.html 8: * 9: * This library is free software; you can redistribute it and/or modify it 10: * under the terms of the GNU Lesser General Public License as published by 11: * the Free Software Foundation; either version 2.1 of the License, or 12: * (at your option) any later version. 13: * 14: * This library is distributed in the hope that it will be useful, but 15: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 16: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 17: * License for more details. 18: * 19: * You should have received a copy of the GNU Lesser General Public 20: * License along with this library; if not, write to the Free Software 21: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 22: * USA. 23: * 24: * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 25: * in the United States and other countries.] 26: * 27: * ------------------------- 28: * ExtendedCategoryAxis.java 29: * ------------------------- 30: * (C) Copyright 2003-2007, by Object Refinery Limited and Contributors. 31: * 32: * Original Author: David Gilbert (for Object Refinery Limited); 33: * Contributor(s): -; 34: * 35: * Changes 36: * ------- 37: * 07-Nov-2003 : Version 1 (DG); 38: * 07-Jan-2004 : Updated the createLabel() method (DG); 39: * 29-Jan-2004 : Added paint attribute (DG); 40: * ------------- JFREECHART 1.0.x --------------------------------------------- 41: * 21-Mar-2007 : Implemented equals(), clone() and fixed serialization (DG); 42: * 43: */ 44: 45: package org.jfree.chart.axis; 46: 47: import java.awt.Color; 48: import java.awt.Font; 49: import java.awt.Graphics2D; 50: import java.awt.Paint; 51: import java.io.IOException; 52: import java.io.ObjectInputStream; 53: import java.io.ObjectOutputStream; 54: import java.util.HashMap; 55: import java.util.Map; 56: 57: import org.jfree.chart.event.AxisChangeEvent; 58: import org.jfree.io.SerialUtilities; 59: import org.jfree.text.TextBlock; 60: import org.jfree.text.TextFragment; 61: import org.jfree.text.TextLine; 62: import org.jfree.ui.RectangleEdge; 63: import org.jfree.util.PaintUtilities; 64: 65: /** 66: * An extended version of the {@link CategoryAxis} class that supports 67: * sublabels on the axis. 68: */ 69: public class ExtendedCategoryAxis extends CategoryAxis { 70: 71: /** For serialization. */ 72: static final long serialVersionUID = -3004429093959826567L; 73: 74: /** Storage for the sublabels. */ 75: private Map sublabels; 76: 77: /** The sublabel font. */ 78: private Font sublabelFont; 79: 80: /** The sublabel paint. */ 81: private transient Paint sublabelPaint; 82: 83: /** 84: * Creates a new axis. 85: * 86: * @param label the axis label. 87: */ 88: public ExtendedCategoryAxis(String label) { 89: super(label); 90: this.sublabels = new HashMap(); 91: this.sublabelFont = new Font("SansSerif", Font.PLAIN, 10); 92: this.sublabelPaint = Color.black; 93: } 94: 95: /** 96: * Returns the font for the sublabels. 97: * 98: * @return The font (never <code>null</code>). 99: * 100: * @see #setSubLabelFont(Font) 101: */ 102: public Font getSubLabelFont() { 103: return this.sublabelFont; 104: } 105: 106: /** 107: * Sets the font for the sublabels and sends an {@link AxisChangeEvent} to 108: * all registered listeners. 109: * 110: * @param font the font (<code>null</code> not permitted). 111: * 112: * @see #getSubLabelFont() 113: */ 114: public void setSubLabelFont(Font font) { 115: if (font == null) { 116: throw new IllegalArgumentException("Null 'font' argument."); 117: } 118: this.sublabelFont = font; 119: notifyListeners(new AxisChangeEvent(this)); 120: } 121: 122: /** 123: * Returns the paint for the sublabels. 124: * 125: * @return The paint (never <code>null</code>). 126: * 127: * @see #setSubLabelPaint(Paint) 128: */ 129: public Paint getSubLabelPaint() { 130: return this.sublabelPaint; 131: } 132: 133: /** 134: * Sets the paint for the sublabels and sends an {@link AxisChangeEvent} 135: * to all registered listeners. 136: * 137: * @param paint the paint (<code>null</code> not permitted). 138: * 139: * @see #getSubLabelPaint() 140: */ 141: public void setSubLabelPaint(Paint paint) { 142: if (paint == null) { 143: throw new IllegalArgumentException("Null 'paint' argument."); 144: } 145: this.sublabelPaint = paint; 146: notifyListeners(new AxisChangeEvent(this)); 147: } 148: 149: /** 150: * Adds a sublabel for a category. 151: * 152: * @param category the category. 153: * @param label the label. 154: */ 155: public void addSubLabel(Comparable category, String label) { 156: this.sublabels.put(category, label); 157: } 158: 159: /** 160: * Overrides the default behaviour by adding the sublabel to the text 161: * block that is used for the category label. 162: * 163: * @param category the category. 164: * @param width the width (not used yet). 165: * @param edge the location of the axis. 166: * @param g2 the graphics device. 167: * 168: * @return A label. 169: */ 170: protected TextBlock createLabel(Comparable category, float width, 171: RectangleEdge edge, Graphics2D g2) { 172: TextBlock label = super.createLabel(category, width, edge, g2); 173: String s = (String) this.sublabels.get(category); 174: if (s != null) { 175: if (edge == RectangleEdge.TOP || edge == RectangleEdge.BOTTOM) { 176: TextLine line = new TextLine(s, this.sublabelFont, 177: this.sublabelPaint); 178: label.addLine(line); 179: } 180: else if (edge == RectangleEdge.LEFT 181: || edge == RectangleEdge.RIGHT) { 182: TextLine line = label.getLastLine(); 183: if (line != null) { 184: line.addFragment(new TextFragment(" " + s, 185: this.sublabelFont, this.sublabelPaint)); 186: } 187: } 188: } 189: return label; 190: } 191: 192: /** 193: * Tests this axis for equality with an arbitrary object. 194: * 195: * @param obj the object (<code>null</code> permitted). 196: * 197: * @return A boolean. 198: */ 199: public boolean equals(Object obj) { 200: if (obj == this) { 201: return true; 202: } 203: if (!(obj instanceof ExtendedCategoryAxis)) { 204: return false; 205: } 206: ExtendedCategoryAxis that = (ExtendedCategoryAxis) obj; 207: if (!this.sublabelFont.equals(that.sublabelFont)) { 208: return false; 209: } 210: if (!PaintUtilities.equal(this.sublabelPaint, that.sublabelPaint)) { 211: return false; 212: } 213: if (!this.sublabels.equals(that.sublabels)) { 214: return false; 215: } 216: return super.equals(obj); 217: } 218: 219: /** 220: * Returns a clone of this axis. 221: * 222: * @return A clone. 223: * 224: * @throws CloneNotSupportedException if there is a problem cloning. 225: */ 226: public Object clone() throws CloneNotSupportedException { 227: ExtendedCategoryAxis clone = (ExtendedCategoryAxis) super.clone(); 228: clone.sublabels = new HashMap(this.sublabels); 229: return clone; 230: } 231: 232: /** 233: * Provides serialization support. 234: * 235: * @param stream the output stream. 236: * 237: * @throws IOException if there is an I/O error. 238: */ 239: private void writeObject(ObjectOutputStream stream) throws IOException { 240: stream.defaultWriteObject(); 241: SerialUtilities.writePaint(this.sublabelPaint, stream); 242: } 243: 244: /** 245: * Provides serialization support. 246: * 247: * @param stream the input stream. 248: * 249: * @throws IOException if there is an I/O error. 250: * @throws ClassNotFoundException if there is a classpath problem. 251: */ 252: private void readObject(ObjectInputStream stream) 253: throws IOException, ClassNotFoundException { 254: stream.defaultReadObject(); 255: this.sublabelPaint = SerialUtilities.readPaint(stream); 256: } 257: 258: }