Source for org.jfree.report.modules.misc.survey.SurveyScaleLegendItem

   1: /**
   2:  * ========================================
   3:  * JFreeReport : a free Java report library
   4:  * ========================================
   5:  *
   6:  * Project Info:  http://reporting.pentaho.org/
   7:  *
   8:  * (C) Copyright 2000-2007, by Object Refinery Limited, Pentaho Corporation and Contributors.
   9:  *
  10:  * This library is free software; you can redistribute it and/or modify it under the terms
  11:  * of the GNU Lesser General Public License as published by the Free Software Foundation;
  12:  * either version 2.1 of the License, or (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  15:  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16:  * See the GNU Lesser General Public License for more details.
  17:  *
  18:  * You should have received a copy of the GNU Lesser General Public License along with this
  19:  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  20:  * Boston, MA 02111-1307, USA.
  21:  *
  22:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  23:  * in the United States and other countries.]
  24:  *
  25:  * ------------
  26:  * $Id: SurveyScaleLegendItem.java 2725 2007-04-01 18:49:29Z taqua $
  27:  * ------------
  28:  * (C) Copyright 2000-2005, by Object Refinery Limited.
  29:  * (C) Copyright 2005-2007, by Pentaho Corporation.
  30:  */
  31: 
  32: package org.jfree.report.modules.misc.survey;
  33: 
  34: import java.awt.BasicStroke;
  35: import java.awt.Color;
  36: import java.awt.Font;
  37: import java.awt.Graphics2D;
  38: import java.awt.Shape;
  39: import java.awt.geom.Rectangle2D;
  40: 
  41: import org.jfree.text.TextUtilities;
  42: import org.jfree.ui.Drawable;
  43: import org.jfree.ui.TextAnchor;
  44: 
  45: /**
  46:  * A {@link Drawable} object that represents a legend item for a {@link SurveyScale}.
  47:  */
  48: public class SurveyScaleLegendItem implements Drawable
  49: {
  50: 
  51:   /**
  52:    * The shape.
  53:    */
  54:   private Shape shape;
  55: 
  56:   /**
  57:    * The label.
  58:    */
  59:   private String label;
  60: 
  61:   /**
  62:    * Draw the shape?
  63:    */
  64:   private boolean draw;
  65: 
  66:   /**
  67:    * Fill the shape?
  68:    */
  69:   private boolean fill;
  70: 
  71:   /**
  72:    * The label font.
  73:    */
  74:   private Font font;
  75: 
  76:   public SurveyScaleLegendItem ()
  77:   {
  78:     font = new Font("Serif", Font.ITALIC, 10);
  79:   }
  80: 
  81:   /**
  82:    * Creates a new legend item.
  83:    *
  84:    * @param shape the shape.
  85:    * @param label the label.
  86:    * @param draw  draw the shape?
  87:    * @param fill  fill the shape?
  88:    */
  89:   public SurveyScaleLegendItem (final Shape shape,
  90:                                 final String label,
  91:                                 final boolean draw,
  92:                                 final boolean fill)
  93:   {
  94:     this.shape = shape;
  95:     this.label = label;
  96:     this.draw = draw;
  97:     this.fill = fill;
  98:   }
  99: 
 100:   /**
 101:    * Draws the legend item.
 102:    *
 103:    * @param g2   the graphic device.
 104:    * @param area the area.
 105:    */
 106:   public void draw (final Graphics2D g2, final Rectangle2D area)
 107:   {
 108:     if (shape == null || font == null || label == null)
 109:     {
 110:       return;
 111:     }
 112:     if (draw == false && fill == false)
 113:     {
 114:       return;
 115:     }
 116: 
 117:     final Rectangle2D b = this.shape.getBounds2D();
 118:     double x = area.getMinX() + b.getWidth() / 2.0 + 1.0;
 119:     final double y = area.getCenterY();
 120:     final Shape s = getShape();
 121:     g2.translate(x,y);
 122:     g2.setPaint(Color.black);
 123:     if (this.draw)
 124:     {
 125:       g2.setStroke(new BasicStroke(0.5f));
 126:       g2.draw(s);
 127:     }
 128:     if (this.fill)
 129:     {
 130:       g2.fill(s);
 131:     }
 132:     g2.translate(-x, -y);
 133:     x += b.getWidth() / 2.0 + 3.0;
 134:     g2.setFont(this.font);
 135:     TextUtilities.drawAlignedString
 136:             (this.label, g2, (float) x, (float) y, TextAnchor.HALF_ASCENT_LEFT);
 137:   }
 138: 
 139:   public boolean isDraw ()
 140:   {
 141:     return draw;
 142:   }
 143: 
 144:   public void setDraw (final boolean draw)
 145:   {
 146:     this.draw = draw;
 147:   }
 148: 
 149:   public boolean isFill ()
 150:   {
 151:     return fill;
 152:   }
 153: 
 154:   public void setFill (final boolean fill)
 155:   {
 156:     this.fill = fill;
 157:   }
 158: 
 159:   public Font getFont ()
 160:   {
 161:     return font;
 162:   }
 163: 
 164:   public void setFont (final Font font)
 165:   {
 166:     this.font = font;
 167:   }
 168: 
 169:   public String getLabel ()
 170:   {
 171:     return label;
 172:   }
 173: 
 174:   public void setLabel (final String label)
 175:   {
 176:     this.label = label;
 177:   }
 178: 
 179:   public Shape getShape ()
 180:   {
 181:     return shape;
 182:   }
 183: 
 184:   public void setShape (final Shape shape)
 185:   {
 186:     this.shape = shape;
 187:   }
 188: 
 189: }