001 /** 002 * ======================================== 003 * JFreeReport : a free Java report library 004 * ======================================== 005 * 006 * Project Info: http://reporting.pentaho.org/ 007 * 008 * (C) Copyright 2000-2007, by Object Refinery Limited, Pentaho Corporation and Contributors. 009 * 010 * This library is free software; you can redistribute it and/or modify it under the terms 011 * of the GNU Lesser General Public License as published by the Free Software Foundation; 012 * either version 2.1 of the License, or (at your option) any later version. 013 * 014 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 015 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 016 * See the GNU Lesser General Public License for more details. 017 * 018 * You should have received a copy of the GNU Lesser General Public License along with this 019 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, 020 * Boston, MA 02111-1307, USA. 021 * 022 * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 023 * in the United States and other countries.] 024 * 025 * ------------ 026 * $Id: SurveyScaleLegendItem.java 3048 2007-07-28 18:02:42Z tmorgner $ 027 * ------------ 028 * (C) Copyright 2000-2005, by Object Refinery Limited. 029 * (C) Copyright 2005-2007, by Pentaho Corporation. 030 */ 031 032 package org.jfree.report.modules.misc.survey; 033 034 import java.awt.BasicStroke; 035 import java.awt.Color; 036 import java.awt.Font; 037 import java.awt.Graphics2D; 038 import java.awt.Shape; 039 import java.awt.geom.Rectangle2D; 040 041 import org.jfree.text.TextUtilities; 042 import org.jfree.ui.Drawable; 043 import org.jfree.ui.TextAnchor; 044 045 /** 046 * A {@link Drawable} object that represents a legend item for a {@link SurveyScale}. 047 */ 048 public class SurveyScaleLegendItem implements Drawable 049 { 050 051 /** 052 * The shape. 053 */ 054 private Shape shape; 055 056 /** 057 * The label. 058 */ 059 private String label; 060 061 /** 062 * Draw the shape? 063 */ 064 private boolean draw; 065 066 /** 067 * Fill the shape? 068 */ 069 private boolean fill; 070 071 /** 072 * The label font. 073 */ 074 private Font font; 075 076 public SurveyScaleLegendItem () 077 { 078 font = new Font("Serif", Font.ITALIC, 10); 079 } 080 081 /** 082 * Creates a new legend item. 083 * 084 * @param shape the shape. 085 * @param label the label. 086 * @param draw draw the shape? 087 * @param fill fill the shape? 088 */ 089 public SurveyScaleLegendItem (final Shape shape, 090 final String label, 091 final boolean draw, 092 final boolean fill) 093 { 094 this.shape = shape; 095 this.label = label; 096 this.draw = draw; 097 this.fill = fill; 098 } 099 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 }