KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jasperreports > engine > JRPrintImageArea


1 /*
2  * ============================================================================
3  * GNU Lesser General Public License
4  * ============================================================================
5  *
6  * JasperReports - Free Java report-generating library.
7  * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * 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,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * JasperSoft Corporation
24  * 303 Second Street, Suite 450 North
25  * San Francisco, CA 94107
26  * http://www.jaspersoft.com
27  */

28 package net.sf.jasperreports.engine;
29
30 import java.awt.Polygon JavaDoc;
31 import java.awt.Rectangle JavaDoc;
32 import java.awt.Shape JavaDoc;
33 import java.awt.geom.Ellipse2D JavaDoc;
34 import java.util.HashMap JavaDoc;
35 import java.util.Map JavaDoc;
36
37
38 /**
39  * An area on an image.
40  *
41  * @author Lucian Chirita (lucianc@users.sourceforge.net)
42  * @version $Id: JRPrintImageArea.java 1370 2006-09-01 17:28:03 +0300 (Fri, 01 Sep 2006) lucianc $
43  * @see JRPrintImageAreaHyperlink
44  */

45 public class JRPrintImageArea
46 {
47
48     public final static byte SHAPE_DEFAULT = 0;
49     public final static byte SHAPE_RECTANGLE = 1;
50     public final static byte SHAPE_CIRCLE = 2;
51     public final static byte SHAPE_POLYGON = 3;
52     
53     public final static String JavaDoc SHAPE_HTML_DEFAULT = "default";
54     public final static String JavaDoc SHAPE_HTML_RECTANGLE = "rect";
55     public final static String JavaDoc SHAPE_HTML_CIRCLE = "circle";
56     public final static String JavaDoc SHAPE_HTML_POLYGON = "poly";
57     
58     private final static Map JavaDoc htmlShapes;
59     
60     static
61     {
62         htmlShapes = new HashMap JavaDoc();
63         htmlShapes.put(SHAPE_HTML_DEFAULT, new Byte JavaDoc(SHAPE_DEFAULT));
64         htmlShapes.put(SHAPE_HTML_RECTANGLE, new Byte JavaDoc(SHAPE_RECTANGLE));
65         htmlShapes.put(SHAPE_HTML_CIRCLE, new Byte JavaDoc(SHAPE_CIRCLE));
66         htmlShapes.put(SHAPE_HTML_POLYGON, new Byte JavaDoc(SHAPE_POLYGON));
67     }
68     
69     
70     /**
71      * Returns the shape constant corresponding the HTML are shape type.
72      *
73      * @param htmlShape the HTML are shape type
74      * @return the corresponding shape constant
75      */

76     public static byte getShape(String JavaDoc htmlShape)
77     {
78         Byte JavaDoc shape = (Byte JavaDoc) htmlShapes.get(htmlShape.toLowerCase());
79         if (shape == null)
80         {
81             throw new JRRuntimeException("Unknown HTML image area shape \"" + htmlShape + "\"");
82         }
83         return shape.byteValue();
84     }
85     
86     
87     /**
88      * Returns the HTML shape type corresponding to a shape type.
89      *
90      * @param shape the shape type
91      * @return the HTML shape type
92      */

93     public static String JavaDoc getHtmlShape(byte shape)
94     {
95         String JavaDoc htmlShape;
96         switch (shape)
97         {
98             case SHAPE_DEFAULT:
99                 htmlShape = SHAPE_HTML_DEFAULT;
100                 break;
101             case SHAPE_RECTANGLE:
102                 htmlShape = SHAPE_HTML_RECTANGLE;
103                 break;
104             case SHAPE_CIRCLE:
105                 htmlShape = SHAPE_HTML_CIRCLE;
106                 break;
107             case SHAPE_POLYGON:
108                 htmlShape = SHAPE_HTML_POLYGON;
109                 break;
110             default:
111                 throw new JRRuntimeException("Unknown image area shape " + shape + "");
112         }
113         return htmlShape;
114     }
115     
116     private byte shape = SHAPE_DEFAULT;
117     private int[] coordinates;
118     
119     private transient Shape JavaDoc cachedAWTShape;
120     
121     /**
122      * Creates a blank image area.
123      */

124     public JRPrintImageArea()
125     {
126     }
127
128
129     /**
130      * Returns the shape type.
131      *
132      * @return the shape type
133      */

134     public byte getShape()
135     {
136         return shape;
137     }
138
139     
140     /**
141      * Sets the area shape type.
142      *
143      * @param shape the shape type, one of
144      * <ul>
145      * <li>{@link #SHAPE_DEFAULT SHAPE_DEFAULT}</li>
146      * <li>{@link #SHAPE_RECTANGLE SHAPE_RECTANGLE}</li>
147      * <li>{@link #SHAPE_POLYGON SHAPE_POLYGON}</li>
148      * <li>{@link #SHAPE_CIRCLE SHAPE_CIRCLE}</li>
149      * </ul>
150      */

151     public void setShape(byte shape)
152     {
153         this.shape = shape;
154     }
155
156     
157     /**
158      * Returns the shape coordinates.
159      *
160      * @return the shape coordinates
161      */

162     public int[] getCoordinates()
163     {
164         return coordinates;
165     }
166
167     
168     /**
169      * Sets the shape coordinates.
170      *
171      * @param coordinates the shape coordinates
172      */

173     public void setCoordinates(int[] coordinates)
174     {
175         this.coordinates = coordinates;
176     }
177     
178     
179     /**
180      * Decides whether a specific point is inside this area.
181      *
182      * @param x the X coordinate of the point
183      * @param y the Y coordinate of the point
184      * @return whether the point is inside this area
185      */

186     public boolean containsPoint(int x, int y)
187     {
188         boolean contains;
189         if (hasAWTShape())
190         {
191             ensureAWTShape();
192             contains = cachedAWTShape.contains(x, y);
193         }
194         else
195         {
196             contains = true;
197         }
198         return contains;
199     }
200     
201     
202     protected void ensureAWTShape()
203     {
204         if (cachedAWTShape == null)
205         {
206             cachedAWTShape = createAWTShape();
207         }
208     }
209     
210     
211     protected boolean hasAWTShape()
212     {
213         return shape != SHAPE_DEFAULT;
214     }
215     
216     
217     protected Shape JavaDoc createAWTShape()
218     {
219         Shape JavaDoc awtShape;
220         switch (shape)
221         {
222             case SHAPE_RECTANGLE:
223                 awtShape = createAWTRectangle();
224                 break;
225             case SHAPE_CIRCLE:
226                 awtShape = createAWTCircle();
227                 break;
228             case SHAPE_POLYGON:
229                 awtShape = createAWTPolygon();
230                 break;
231             default:
232                 awtShape = null;
233                 break;
234         }
235         return awtShape;
236     }
237
238
239     protected Shape JavaDoc createAWTRectangle()
240     {
241         if (coordinates == null || coordinates.length != 4)
242         {
243             throw new JRRuntimeException("A rectangle must have exactly 4 coordinates");
244         }
245         
246         return new Rectangle JavaDoc(
247                 coordinates[0],
248                 coordinates[1],
249                 coordinates[2] - coordinates[0],
250                 coordinates[3] - coordinates[1]);
251     }
252
253
254     private Shape JavaDoc createAWTCircle()
255     {
256         if (coordinates == null || coordinates.length != 3)
257         {
258             throw new JRRuntimeException("A circle must have exactly 4 coordinates");
259         }
260         
261         return new Ellipse2D.Float JavaDoc(coordinates[0], coordinates[1], coordinates[2], coordinates[2]);
262     }
263
264
265     private Shape JavaDoc createAWTPolygon()
266     {
267         if (coordinates == null || coordinates.length == 0 || coordinates.length % 2 != 0)
268         {
269             throw new JRRuntimeException("A polygon must have an even number of coordinates");
270         }
271         
272         Polygon JavaDoc polygon = new Polygon JavaDoc();
273         
274         int i;
275         for (i = 0; i < coordinates.length - 2; i += 2)
276         {
277             polygon.addPoint(coordinates[i], coordinates[i + 1]);
278         }
279         if (coordinates[i] != coordinates[0] || coordinates[i + 1] != coordinates[1])
280         {
281             polygon.addPoint(coordinates[i], coordinates[i + 1]);
282         }
283
284         return polygon;
285     }
286     
287 }
288
Popular Tags