001: /*
002: * ============================================================================
003: * GNU Lesser General Public License
004: * ============================================================================
005: *
006: * JasperReports - Free Java report-generating library.
007: * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
008: *
009: * This library is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public
011: * License as published by the Free Software Foundation; either
012: * 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,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
022: *
023: * JasperSoft Corporation
024: * 303 Second Street, Suite 450 North
025: * San Francisco, CA 94107
026: * http://www.jaspersoft.com
027: */
028: package net.sf.jasperreports.engine;
029:
030: import java.awt.Polygon;
031: import java.awt.Rectangle;
032: import java.awt.Shape;
033: import java.awt.geom.Ellipse2D;
034: import java.util.HashMap;
035: import java.util.Map;
036:
037: /**
038: * An area on an image.
039: *
040: * @author Lucian Chirita (lucianc@users.sourceforge.net)
041: * @version $Id: JRPrintImageArea.java 1370 2006-09-01 14:28:03Z lucianc $
042: * @see JRPrintImageAreaHyperlink
043: */
044: public class JRPrintImageArea {
045:
046: public final static byte SHAPE_DEFAULT = 0;
047: public final static byte SHAPE_RECTANGLE = 1;
048: public final static byte SHAPE_CIRCLE = 2;
049: public final static byte SHAPE_POLYGON = 3;
050:
051: public final static String SHAPE_HTML_DEFAULT = "default";
052: public final static String SHAPE_HTML_RECTANGLE = "rect";
053: public final static String SHAPE_HTML_CIRCLE = "circle";
054: public final static String SHAPE_HTML_POLYGON = "poly";
055:
056: private final static Map htmlShapes;
057:
058: static {
059: htmlShapes = new HashMap();
060: htmlShapes.put(SHAPE_HTML_DEFAULT, new Byte(SHAPE_DEFAULT));
061: htmlShapes.put(SHAPE_HTML_RECTANGLE, new Byte(SHAPE_RECTANGLE));
062: htmlShapes.put(SHAPE_HTML_CIRCLE, new Byte(SHAPE_CIRCLE));
063: htmlShapes.put(SHAPE_HTML_POLYGON, new Byte(SHAPE_POLYGON));
064: }
065:
066: /**
067: * Returns the shape constant corresponding the HTML are shape type.
068: *
069: * @param htmlShape the HTML are shape type
070: * @return the corresponding shape constant
071: */
072: public static byte getShape(String htmlShape) {
073: Byte shape = (Byte) htmlShapes.get(htmlShape.toLowerCase());
074: if (shape == null) {
075: throw new JRRuntimeException(
076: "Unknown HTML image area shape \"" + htmlShape
077: + "\"");
078: }
079: return shape.byteValue();
080: }
081:
082: /**
083: * Returns the HTML shape type corresponding to a shape type.
084: *
085: * @param shape the shape type
086: * @return the HTML shape type
087: */
088: public static String getHtmlShape(byte shape) {
089: String htmlShape;
090: switch (shape) {
091: case SHAPE_DEFAULT:
092: htmlShape = SHAPE_HTML_DEFAULT;
093: break;
094: case SHAPE_RECTANGLE:
095: htmlShape = SHAPE_HTML_RECTANGLE;
096: break;
097: case SHAPE_CIRCLE:
098: htmlShape = SHAPE_HTML_CIRCLE;
099: break;
100: case SHAPE_POLYGON:
101: htmlShape = SHAPE_HTML_POLYGON;
102: break;
103: default:
104: throw new JRRuntimeException("Unknown image area shape "
105: + shape + "");
106: }
107: return htmlShape;
108: }
109:
110: private byte shape = SHAPE_DEFAULT;
111: private int[] coordinates;
112:
113: private transient Shape cachedAWTShape;
114:
115: /**
116: * Creates a blank image area.
117: */
118: public JRPrintImageArea() {
119: }
120:
121: /**
122: * Returns the shape type.
123: *
124: * @return the shape type
125: */
126: public byte getShape() {
127: return shape;
128: }
129:
130: /**
131: * Sets the area shape type.
132: *
133: * @param shape the shape type, one of
134: * <ul>
135: * <li>{@link #SHAPE_DEFAULT SHAPE_DEFAULT}</li>
136: * <li>{@link #SHAPE_RECTANGLE SHAPE_RECTANGLE}</li>
137: * <li>{@link #SHAPE_POLYGON SHAPE_POLYGON}</li>
138: * <li>{@link #SHAPE_CIRCLE SHAPE_CIRCLE}</li>
139: * </ul>
140: */
141: public void setShape(byte shape) {
142: this .shape = shape;
143: }
144:
145: /**
146: * Returns the shape coordinates.
147: *
148: * @return the shape coordinates
149: */
150: public int[] getCoordinates() {
151: return coordinates;
152: }
153:
154: /**
155: * Sets the shape coordinates.
156: *
157: * @param coordinates the shape coordinates
158: */
159: public void setCoordinates(int[] coordinates) {
160: this .coordinates = coordinates;
161: }
162:
163: /**
164: * Decides whether a specific point is inside this area.
165: *
166: * @param x the X coordinate of the point
167: * @param y the Y coordinate of the point
168: * @return whether the point is inside this area
169: */
170: public boolean containsPoint(int x, int y) {
171: boolean contains;
172: if (hasAWTShape()) {
173: ensureAWTShape();
174: contains = cachedAWTShape.contains(x, y);
175: } else {
176: contains = true;
177: }
178: return contains;
179: }
180:
181: protected void ensureAWTShape() {
182: if (cachedAWTShape == null) {
183: cachedAWTShape = createAWTShape();
184: }
185: }
186:
187: protected boolean hasAWTShape() {
188: return shape != SHAPE_DEFAULT;
189: }
190:
191: protected Shape createAWTShape() {
192: Shape awtShape;
193: switch (shape) {
194: case SHAPE_RECTANGLE:
195: awtShape = createAWTRectangle();
196: break;
197: case SHAPE_CIRCLE:
198: awtShape = createAWTCircle();
199: break;
200: case SHAPE_POLYGON:
201: awtShape = createAWTPolygon();
202: break;
203: default:
204: awtShape = null;
205: break;
206: }
207: return awtShape;
208: }
209:
210: protected Shape createAWTRectangle() {
211: if (coordinates == null || coordinates.length != 4) {
212: throw new JRRuntimeException(
213: "A rectangle must have exactly 4 coordinates");
214: }
215:
216: return new Rectangle(coordinates[0], coordinates[1],
217: coordinates[2] - coordinates[0], coordinates[3]
218: - coordinates[1]);
219: }
220:
221: private Shape createAWTCircle() {
222: if (coordinates == null || coordinates.length != 3) {
223: throw new JRRuntimeException(
224: "A circle must have exactly 4 coordinates");
225: }
226:
227: return new Ellipse2D.Float(coordinates[0], coordinates[1],
228: coordinates[2], coordinates[2]);
229: }
230:
231: private Shape createAWTPolygon() {
232: if (coordinates == null || coordinates.length == 0
233: || coordinates.length % 2 != 0) {
234: throw new JRRuntimeException(
235: "A polygon must have an even number of coordinates");
236: }
237:
238: Polygon polygon = new Polygon();
239:
240: int i;
241: for (i = 0; i < coordinates.length - 2; i += 2) {
242: polygon.addPoint(coordinates[i], coordinates[i + 1]);
243: }
244: if (coordinates[i] != coordinates[0]
245: || coordinates[i + 1] != coordinates[1]) {
246: polygon.addPoint(coordinates[i], coordinates[i + 1]);
247: }
248:
249: return polygon;
250: }
251:
252: }
|