001: /**
002: * ===========================================
003: * JFreeReport : a free Java reporting library
004: * ===========================================
005: *
006: * Project Info: http://reporting.pentaho.org/
007: *
008: * (C) Copyright 2001-2007, by Object Refinery Ltd, 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: * ShapeElement.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report;
030:
031: import java.awt.BasicStroke;
032: import java.awt.Stroke;
033:
034: import org.jfree.report.style.ElementDefaultStyleSheet;
035: import org.jfree.report.style.ElementStyleKeys;
036: import org.jfree.report.style.StyleKey;
037:
038: /**
039: * Used to draw shapes (typically lines and boxes) on a report band. The drawing style of
040: * the shapes contained in that element can be controled by using the StyleKeys FILL_SHAPE
041: * and DRAW_SHAPE.
042: *
043: * @author David Gilbert
044: * @author Thomas Morgner
045: */
046: public class ShapeElement extends Element {
047: /**
048: * The default stroke.
049: */
050: public static final BasicStroke DEFAULT_STROKE = new BasicStroke(
051: 0.5f);
052:
053: /**
054: * A key for the 'fill-shape' style.
055: */
056: public static final StyleKey FILL_SHAPE = ElementStyleKeys.FILL_SHAPE;
057:
058: /**
059: * A key for the 'draw-shape' style.
060: */
061: public static final StyleKey DRAW_SHAPE = ElementStyleKeys.DRAW_SHAPE;
062:
063: /**
064: * A default style sheet for shape elements. This defined a default stroke for all
065: * shapes.
066: */
067: private static class ShapeElementDefaultStyleSheet extends
068: ElementDefaultStyleSheet {
069: /**
070: * Creates a new style-sheet. The stylesheet is not modifiable
071: */
072: protected ShapeElementDefaultStyleSheet() {
073: super ("GlobalShapeElementDefault");
074: // unlock the write protection
075: setLocked(false);
076: setStyleProperty(ElementStyleKeys.STROKE,
077: ShapeElement.DEFAULT_STROKE);
078: setBooleanStyleProperty(ElementStyleKeys.FILL_SHAPE, false);
079: setBooleanStyleProperty(ElementStyleKeys.DRAW_SHAPE, false);
080: // and lock the stylesheet again...
081: setLocked(true);
082: }
083: }
084:
085: /**
086: * A shared default style sheet for shape elements.
087: */
088: private static ElementDefaultStyleSheet defaultShapeStyle;
089:
090: /**
091: * Returns the default style-sheet for shape elements.
092: *
093: * @return a default style sheet that can be shared among shape elements.
094: */
095: public static synchronized ElementDefaultStyleSheet getDefaultStyle() {
096: if (defaultShapeStyle == null) {
097: defaultShapeStyle = new ShapeElementDefaultStyleSheet();
098: }
099: return defaultShapeStyle;
100: }
101:
102: /**
103: * Constructs a shape element.
104: */
105: public ShapeElement() {
106: }
107:
108: /**
109: * Returns the ShapeElement specific global stylesheet.
110: *
111: * @return the global stylesheet.
112: */
113: protected ElementDefaultStyleSheet createGlobalDefaultStyle() {
114: return ShapeElement.getDefaultStyle();
115: }
116:
117: /**
118: * Returns a string describing the element. Useful for debugging.
119: *
120: * @return the string.
121: */
122: public String toString() {
123: final StringBuffer b = new StringBuffer();
124: b.append("Shape={ name=");
125: b.append(getName());
126: b.append('}');
127:
128: return b.toString();
129: }
130:
131: /**
132: * Returns true if the element outline should be drawn, and false otherwise.
133: * <p/>
134: * This is determined by the element's style-sheet.
135: *
136: * @return true or false.
137: */
138: public boolean isShouldDraw() {
139: return getStyle().getBooleanStyleProperty(
140: ElementStyleKeys.DRAW_SHAPE);
141: }
142:
143: /**
144: * Returns true of the element should be filled, and false otherwise.
145: * <p/>
146: * This is determined by the element's style-sheet.
147: *
148: * @return true or false.
149: */
150: public boolean isShouldFill() {
151: return getStyle().getBooleanStyleProperty(
152: ElementStyleKeys.FILL_SHAPE);
153: }
154:
155: /**
156: * Sets a flag that controls whether or not the outline of the shape is drawn.
157: *
158: * @param shouldDraw the flag.
159: */
160: public void setShouldDraw(final boolean shouldDraw) {
161: getStyle().setStyleProperty(ElementStyleKeys.DRAW_SHAPE,
162: shouldDraw ? Boolean.TRUE : Boolean.FALSE);
163: }
164:
165: /**
166: * Sets a flag that controls whether or not the area of the shape is filled.
167: *
168: * @param shouldFill the flag.
169: */
170: public void setShouldFill(final boolean shouldFill) {
171: getStyle().setStyleProperty(ElementStyleKeys.FILL_SHAPE,
172: shouldFill ? Boolean.TRUE : Boolean.FALSE);
173: }
174:
175: /**
176: * Returns true if the shape should be scaled, and false otherwise.
177: * <p/>
178: * This is determined by the element's style-sheet.
179: *
180: * @return true or false.
181: */
182: public boolean isScale() {
183: return getStyle().getBooleanStyleProperty(
184: ElementStyleKeys.SCALE);
185: }
186:
187: /**
188: * Sets a flag that controls whether the shape should be scaled to fit the element
189: * bounds.
190: *
191: * @param scale the flag.
192: */
193: public void setScale(final boolean scale) {
194: getStyle().setStyleProperty(ElementStyleKeys.SCALE,
195: scale ? Boolean.TRUE : Boolean.FALSE);
196: }
197:
198: /**
199: * Returns true if the shape's aspect ratio should be preserved, and false otherwise.
200: * <p/>
201: * This is determined by the element's style-sheet.
202: *
203: * @return true or false.
204: */
205: public boolean isKeepAspectRatio() {
206: return getStyle().getBooleanStyleProperty(
207: ElementStyleKeys.KEEP_ASPECT_RATIO);
208: }
209:
210: /**
211: * Sets a flag that controls whether the shape should be scaled to fit the element
212: * bounds.
213: *
214: * @param kar the flag.
215: */
216: public void setKeepAspectRatio(final boolean kar) {
217: getStyle().setStyleProperty(ElementStyleKeys.KEEP_ASPECT_RATIO,
218: kar ? Boolean.TRUE : Boolean.FALSE);
219: }
220:
221: /**
222: * A string for the content type.
223: */
224: public static final String CONTENT_TYPE = "shape/generic";
225:
226: /**
227: * Returns the content type, in this case 'shape/generic'.
228: *
229: * @return the content type.
230: */
231: public String getContentType() {
232: return ShapeElement.CONTENT_TYPE;
233: }
234:
235: /**
236: * Returns the stroke.
237: *
238: * @return the stroke.
239: */
240: public Stroke getStroke() {
241: return (Stroke) getStyle().getStyleProperty(
242: ElementStyleKeys.STROKE);
243: }
244:
245: /**
246: * Sets the stroke.
247: *
248: * @param stroke the stroke.
249: */
250: public void setStroke(final Stroke stroke) {
251: getStyle().setStyleProperty(ElementStyleKeys.STROKE, stroke);
252: }
253: }
|