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: * StaticImageElementFactory.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report.elementfactory;
030:
031: import java.awt.Image;
032: import java.io.IOException;
033:
034: import org.jfree.report.DefaultImageReference;
035: import org.jfree.report.Element;
036: import org.jfree.report.ImageContainer;
037: import org.jfree.report.ImageElement;
038: import org.jfree.report.LocalImageContainer;
039: import org.jfree.report.filter.StaticDataSource;
040:
041: /**
042: * A factory to create static image elements. These element contain an immutable image.
043: * The image should not be modified.
044: *
045: * @author Thomas Morgner
046: */
047: public class StaticImageElementFactory extends ImageElementFactory {
048: /**
049: * The image reference is the static content of the new element.
050: */
051: private ImageContainer imageContainer;
052:
053: /**
054: * Default Constructor.
055: */
056: public StaticImageElementFactory() {
057: }
058:
059: /**
060: * Returns the image assigned to this element.
061: *
062: * @return the element's image.
063: */
064: public Image getImage() {
065: if (imageContainer == null) {
066: return null;
067: }
068: if (imageContainer instanceof LocalImageContainer) {
069: final LocalImageContainer lc = (LocalImageContainer) imageContainer;
070: return lc.getImage();
071: }
072: return null;
073: }
074:
075: /**
076: * Defines the image for this image element factory.
077: *
078: * @param image the image.
079: * @throws IOException if the given image could not be completly loaded.
080: */
081: public void setImage(final Image image) throws IOException {
082: this .imageContainer = new DefaultImageReference(image);
083: }
084:
085: /**
086: * Returns the image reference instance of the element.
087: *
088: * @return the image reference containing the image data.
089: */
090: public ImageContainer getImageContainer() {
091: return imageContainer;
092: }
093:
094: /**
095: * Defines the image reference instance of the element.
096: *
097: * @param imageReference the image reference containing the image data.
098: * @deprecated use setImageContainer instead
099: */
100: public void setImageReference(final ImageContainer imageReference) {
101: this .imageContainer = imageReference;
102: }
103:
104: /**
105: * Assigns the image container for this image element.
106: *
107: * @param imageReference the image container for this element.
108: */
109: public void setImageContainer(final ImageContainer imageReference) {
110: this .imageContainer = imageReference;
111: }
112:
113: /**
114: * Creates the image element.
115: *
116: * @return the generated image element.
117: *
118: * @throws IllegalStateException if the image is not defined.
119: * @see org.jfree.report.elementfactory.ElementFactory#createElement()
120: */
121: public Element createElement() {
122: if (getImage() == null) {
123: throw new IllegalStateException("Content is not set.");
124: }
125:
126: final StaticDataSource datasource = new StaticDataSource(
127: getImageContainer());
128: final ImageElement element = new ImageElement();
129: applyElementName(element);
130: applyStyle(element.getStyle());
131: element.setDataSource(datasource);
132:
133: return element;
134: }
135: }
|