| 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 10756 2009-12-02 15:58:24Z tmorgner $ |
| 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.font.FontRenderContext; |
| 40 |
import java.awt.font.LineMetrics; |
| 41 |
import java.awt.geom.Rectangle2D; |
| 42 |
|
| 43 |
/** |
| 44 |
* A Drawable object that represents a legend item for a {@link SurveyScale}. |
| 45 |
*/ |
| 46 |
public class SurveyScaleLegendItem |
| 47 |
{ |
| 48 |
|
| 49 |
/** |
| 50 |
* The shape. |
| 51 |
*/ |
| 52 |
private Shape shape; |
| 53 |
|
| 54 |
/** |
| 55 |
* The label. |
| 56 |
*/ |
| 57 |
private String label; |
| 58 |
|
| 59 |
/** |
| 60 |
* Draw the shape? |
| 61 |
*/ |
| 62 |
private boolean draw; |
| 63 |
|
| 64 |
/** |
| 65 |
* Fill the shape? |
| 66 |
*/ |
| 67 |
private boolean fill; |
| 68 |
|
| 69 |
/** |
| 70 |
* The label font. |
| 71 |
*/ |
| 72 |
private Font font; |
| 73 |
|
| 74 |
public SurveyScaleLegendItem () |
| 75 |
{ |
| 76 |
font = new Font("Serif", Font.ITALIC, 10); //$NON-NLS-1$ |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Creates a new legend item. |
| 81 |
* |
| 82 |
* @param shape the shape. |
| 83 |
* @param label the label. |
| 84 |
* @param draw draw the shape? |
| 85 |
* @param fill fill the shape? |
| 86 |
*/ |
| 87 |
public SurveyScaleLegendItem (final Shape shape, |
| 88 |
final String label, |
| 89 |
final boolean draw, |
| 90 |
final boolean fill) |
| 91 |
{ |
| 92 |
this.shape = shape; |
| 93 |
this.label = label; |
| 94 |
this.draw = draw; |
| 95 |
this.fill = fill; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Draws the legend item. |
| 100 |
* |
| 101 |
* @param g2 the graphic device. |
| 102 |
* @param area the area. |
| 103 |
*/ |
| 104 |
public void draw (final Graphics2D g2, final Rectangle2D area) |
| 105 |
{ |
| 106 |
if (shape == null || font == null || label == null) |
| 107 |
{ |
| 108 |
return; |
| 109 |
} |
| 110 |
if (draw == false && fill == false) |
| 111 |
{ |
| 112 |
return; |
| 113 |
} |
| 114 |
|
| 115 |
final Rectangle2D b = this.shape.getBounds2D(); |
| 116 |
double x = area.getMinX() + b.getWidth() / 2.0 + 1.0; |
| 117 |
final double y = area.getCenterY(); |
| 118 |
final Shape s = getShape(); |
| 119 |
g2.translate(x,y); |
| 120 |
g2.setPaint(Color.black); |
| 121 |
if (this.draw) |
| 122 |
{ |
| 123 |
g2.setStroke(new BasicStroke(0.5f)); |
| 124 |
g2.draw(s); |
| 125 |
} |
| 126 |
if (this.fill) |
| 127 |
{ |
| 128 |
g2.fill(s); |
| 129 |
} |
| 130 |
g2.translate(-x, -y); |
| 131 |
x += b.getWidth() / 2.0 + 3.0; |
| 132 |
g2.setFont(this.font); |
| 133 |
|
| 134 |
final FontRenderContext frc = g2.getFontRenderContext(); |
| 135 |
final Font f = g2.getFont(); |
| 136 |
// final FontMetrics fm = g2.getFontMetrics(f); |
| 137 |
final LineMetrics metrics = f.getLineMetrics(label, frc); |
| 138 |
final float ascent = metrics.getAscent(); |
| 139 |
final float halfAscent = ascent / 2.0f; |
| 140 |
g2.drawString(label, (float) x, (float) (y + halfAscent)); |
| 141 |
} |
| 142 |
|
| 143 |
public boolean isDraw () |
| 144 |
{ |
| 145 |
return draw; |
| 146 |
} |
| 147 |
|
| 148 |
public void setDraw (final boolean draw) |
| 149 |
{ |
| 150 |
this.draw = draw; |
| 151 |
} |
| 152 |
|
| 153 |
public boolean isFill () |
| 154 |
{ |
| 155 |
return fill; |
| 156 |
} |
| 157 |
|
| 158 |
public void setFill (final boolean fill) |
| 159 |
{ |
| 160 |
this.fill = fill; |
| 161 |
} |
| 162 |
|
| 163 |
public Font getFont () |
| 164 |
{ |
| 165 |
return font; |
| 166 |
} |
| 167 |
|
| 168 |
public void setFont (final Font font) |
| 169 |
{ |
| 170 |
this.font = font; |
| 171 |
} |
| 172 |
|
| 173 |
public String getLabel () |
| 174 |
{ |
| 175 |
return label; |
| 176 |
} |
| 177 |
|
| 178 |
public void setLabel (final String label) |
| 179 |
{ |
| 180 |
this.label = label; |
| 181 |
} |
| 182 |
|
| 183 |
public Shape getShape () |
| 184 |
{ |
| 185 |
return shape; |
| 186 |
} |
| 187 |
|
| 188 |
public void setShape (final Shape shape) |
| 189 |
{ |
| 190 |
this.shape = shape; |
| 191 |
} |
| 192 |
|
| 193 |
} |