Source for org.jfree.chart.axis.MarkerAxisBand

   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:  * MarkerAxisBand.java
  29:  * -------------------
  30:  * (C) Copyright 2000-2007, by Object Refinery Limited.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   -;
  34:  *
  35:  * Changes
  36:  * -------
  37:  * 03-Sep-2002 : Updated Javadoc comments (DG);
  38:  * 01-Oct-2002 : Fixed errors reported by Checkstyle (DG);
  39:  * 08-Nov-2002 : Moved to new package com.jrefinery.chart.axis (DG);
  40:  * 26-Mar-2003 : Implemented Serializable (DG);
  41:  * 13-May-2003 : Renamed HorizontalMarkerAxisBand --> MarkerAxisBand (DG);
  42:  * 29-Oct-2003 : Added workaround for font alignment in PDF output (DG);
  43:  * 21-Jan-2004 : Update for renamed method in ValueAxis (DG);
  44:  * 07-Apr-2004 : Changed text bounds calculation (DG);
  45:  *
  46:  */
  47: 
  48: package org.jfree.chart.axis;
  49: 
  50: import java.awt.AlphaComposite;
  51: import java.awt.Color;
  52: import java.awt.Composite;
  53: import java.awt.Font;
  54: import java.awt.FontMetrics;
  55: import java.awt.Graphics2D;
  56: import java.awt.font.LineMetrics;
  57: import java.awt.geom.Rectangle2D;
  58: import java.io.Serializable;
  59: import java.util.Iterator;
  60: import java.util.List;
  61: 
  62: import org.jfree.chart.plot.IntervalMarker;
  63: import org.jfree.text.TextUtilities;
  64: import org.jfree.ui.RectangleEdge;
  65: import org.jfree.util.ObjectUtilities;
  66: 
  67: /**
  68:  * A band that can be added to a number axis to display regions.
  69:  */
  70: public class MarkerAxisBand implements Serializable {
  71: 
  72:     /** For serialization. */
  73:     private static final long serialVersionUID = -1729482413886398919L;
  74:     
  75:     /** The axis that the band belongs to. */
  76:     private NumberAxis axis;
  77: 
  78:     /** The top outer gap. */
  79:     private double topOuterGap;
  80: 
  81:     /** The top inner gap. */
  82:     private double topInnerGap;
  83: 
  84:     /** The bottom outer gap. */
  85:     private double bottomOuterGap;
  86: 
  87:     /** The bottom inner gap. */
  88:     private double bottomInnerGap;
  89: 
  90:     /** The font. */
  91:     private Font font;
  92: 
  93:     /** Storage for the markers. */
  94:     private List markers;
  95: 
  96:     /**
  97:      * Constructs a new axis band.
  98:      *
  99:      * @param axis  the owner.
 100:      * @param topOuterGap  the top outer gap.
 101:      * @param topInnerGap  the top inner gap.
 102:      * @param bottomOuterGap  the bottom outer gap.
 103:      * @param bottomInnerGap  the bottom inner gap.
 104:      * @param font  the font.
 105:      */
 106:     public MarkerAxisBand(NumberAxis axis,
 107:                           double topOuterGap, double topInnerGap,
 108:                           double bottomOuterGap, double bottomInnerGap,
 109:                           Font font) {
 110:         this.axis = axis;
 111:         this.topOuterGap = topOuterGap;
 112:         this.topInnerGap = topInnerGap;
 113:         this.bottomOuterGap = bottomOuterGap;
 114:         this.bottomInnerGap = bottomInnerGap;
 115:         this.font = font;
 116:         this.markers = new java.util.ArrayList();
 117:     }
 118: 
 119:     /**
 120:      * Adds a marker to the band.
 121:      *
 122:      * @param marker  the marker.
 123:      */
 124:     public void addMarker(IntervalMarker marker) {
 125:         this.markers.add(marker);
 126:     }
 127: 
 128:     /**
 129:      * Returns the height of the band.
 130:      *
 131:      * @param g2  the graphics device.
 132:      *
 133:      * @return The height of the band.
 134:      */
 135:     public double getHeight(Graphics2D g2) {
 136: 
 137:         double result = 0.0;
 138:         if (this.markers.size() > 0) {
 139:             LineMetrics metrics = this.font.getLineMetrics(
 140:                 "123g", g2.getFontRenderContext()
 141:             );
 142:             result = this.topOuterGap + this.topInnerGap + metrics.getHeight()
 143:                      + this.bottomInnerGap + this.bottomOuterGap;
 144:         }
 145:         return result;
 146: 
 147:     }
 148: 
 149:     /**
 150:      * A utility method that draws a string inside a rectangle.
 151:      *
 152:      * @param g2  the graphics device.
 153:      * @param bounds  the rectangle.
 154:      * @param font  the font.
 155:      * @param text  the text.
 156:      */
 157:     private void drawStringInRect(Graphics2D g2, Rectangle2D bounds, Font font,
 158:                                   String text) {
 159: 
 160:         g2.setFont(font);
 161:         FontMetrics fm = g2.getFontMetrics(font);
 162:         Rectangle2D r = TextUtilities.getTextBounds(text, g2, fm);
 163:         double x = bounds.getX();
 164:         if (r.getWidth() < bounds.getWidth()) {
 165:             x = x + (bounds.getWidth() - r.getWidth()) / 2;
 166:         }
 167:         LineMetrics metrics = font.getLineMetrics(
 168:             text, g2.getFontRenderContext()
 169:         );
 170:         g2.drawString(
 171:             text, (float) x, (float) (bounds.getMaxY() 
 172:                 - this.bottomInnerGap - metrics.getDescent())
 173:         );
 174:     }
 175: 
 176:     /**
 177:      * Draws the band.
 178:      *
 179:      * @param g2  the graphics device.
 180:      * @param plotArea  the plot area.
 181:      * @param dataArea  the data area.
 182:      * @param x  the x-coordinate.
 183:      * @param y  the y-coordinate.
 184:      */
 185:     public void draw(Graphics2D g2, Rectangle2D plotArea, Rectangle2D dataArea,
 186:                      double x, double y) {
 187: 
 188:         double h = getHeight(g2);
 189:         Iterator iterator = this.markers.iterator();
 190:         while (iterator.hasNext()) {
 191:             IntervalMarker marker = (IntervalMarker) iterator.next();
 192:             double start =  Math.max(
 193:                 marker.getStartValue(), this.axis.getRange().getLowerBound()
 194:             );
 195:             double end = Math.min(
 196:                 marker.getEndValue(), this.axis.getRange().getUpperBound()
 197:             );
 198:             double s = this.axis.valueToJava2D(
 199:                 start, dataArea, RectangleEdge.BOTTOM
 200:             );
 201:             double e = this.axis.valueToJava2D(
 202:                 end, dataArea, RectangleEdge.BOTTOM
 203:             );
 204:             Rectangle2D r = new Rectangle2D.Double(
 205:                 s, y + this.topOuterGap, e - s, 
 206:                 h - this.topOuterGap - this.bottomOuterGap
 207:             );
 208: 
 209:             Composite originalComposite = g2.getComposite();
 210:             g2.setComposite(AlphaComposite.getInstance(
 211:                 AlphaComposite.SRC_OVER, marker.getAlpha())
 212:             );
 213:             g2.setPaint(marker.getPaint());
 214:             g2.fill(r);
 215:             g2.setPaint(marker.getOutlinePaint());
 216:             g2.draw(r);
 217:             g2.setComposite(originalComposite);
 218: 
 219:             g2.setPaint(Color.black);
 220:             drawStringInRect(g2, r, this.font, marker.getLabel());
 221:         }
 222: 
 223:     }
 224: 
 225:     /**
 226:      * Tests this axis for equality with another object.  Note that the axis 
 227:      * that the band belongs to is ignored in the test.
 228:      *
 229:      * @param obj  the object (<code>null</code> permitted).
 230:      *
 231:      * @return <code>true</code> or <code>false</code>.
 232:      */
 233:     public boolean equals(Object obj) {
 234:         if (obj == this) {
 235:             return true;
 236:         }
 237:         if (!(obj instanceof MarkerAxisBand)) {
 238:             return false;
 239:         }
 240:         MarkerAxisBand that = (MarkerAxisBand) obj;
 241:         if (this.topOuterGap != that.topOuterGap) {
 242:             return false;
 243:         }
 244:         if (this.topInnerGap != that.topInnerGap) {
 245:             return false;
 246:         }
 247:         if (this.bottomInnerGap != that.bottomInnerGap) {
 248:             return false;
 249:         }
 250:         if (this.bottomOuterGap != that.bottomOuterGap) {
 251:             return false;
 252:         }
 253:         if (!ObjectUtilities.equal(this.font, that.font)) {
 254:             return false;
 255:         }
 256:         if (!ObjectUtilities.equal(this.markers, that.markers)) {
 257:             return false;
 258:         }
 259:         return true;
 260:     }
 261:     
 262:     /**
 263:      * Returns a hash code for the object.
 264:      * 
 265:      * @return A hash code.
 266:      */
 267:     public int hashCode() {
 268:         int result = 37;
 269:         result = 19 * result + this.font.hashCode();
 270:         result = 19 * result + this.markers.hashCode();
 271:         return result;
 272:     }
 273: 
 274: }