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.io.Serializable;
031: import java.util.ArrayList;
032: import java.util.Iterator;
033: import java.util.List;
034: import java.util.ListIterator;
035:
036: /**
037: * Default {@link JRTemplate} implementation.
038: *
039: * @author Lucian Chirita (lucianc@users.sourceforge.net)
040: * @version $Id: JRSimpleTemplate.java 1759 2007-06-20 16:47:34Z lucianc $
041: */
042: public class JRSimpleTemplate implements JRTemplate, Serializable {
043:
044: private static final long serialVersionUID = JRConstants.SERIAL_VERSION_UID;
045:
046: private final List includedTemplates = new ArrayList();
047: private final List styles = new ArrayList();
048: private JRStyle defaultStyle;
049:
050: /**
051: * Adds a style to the template.
052: *
053: * @param style the style to add
054: * @throws JRException when a style with the same name already exists
055: */
056: public void addStyle(JRStyle style) throws JRException {
057: checkExistingName(style.getName());
058:
059: if (style.isDefault()) {
060: defaultStyle = style;
061: }
062:
063: styles.add(style);
064: }
065:
066: protected void checkExistingName(String name) throws JRException {
067: if (getStyle(name) != null) {
068: throw new JRException(
069: "Duplicate declaration of template style : " + name);
070: }
071: }
072:
073: protected boolean nameMatches(JRStyle style, String name) {
074: String styleName = style.getName();
075: return name == null ? styleName == null : name
076: .equals(styleName);
077: }
078:
079: /**
080: * Returns an included style by name.
081: *
082: * @param name the name of the style to be returned
083: * @return the style having the specified name, or <code>null</code> if not found
084: */
085: public JRStyle getStyle(String name) {
086: JRStyle style = null;
087: for (Iterator it = styles.iterator(); it.hasNext();) {
088: JRStyle itStyle = (JRStyle) it.next();
089: if (nameMatches(itStyle, name)) {
090: style = itStyle;
091: break;
092: }
093: }
094: return style;
095: }
096:
097: /**
098: * Removes an included style.
099: *
100: * @param style the style to remove
101: * @return <code>true</code> iff the style has been found and removed
102: */
103: public boolean removeStyle(JRStyle style) {
104: boolean removed = styles.remove(style);
105: if (removed) {
106: if (style.isDefault()) {
107: defaultStyle = null;
108: }
109: }
110: return removed;
111: }
112:
113: /**
114: * Removes an included style.
115: *
116: * @param name the name of the style to be removed
117: * @return the removed style, or <code>null</code> if not found
118: */
119: public JRStyle removeStyle(String name) {
120: JRStyle removed = null;
121: for (ListIterator it = styles.listIterator(); it.hasNext();) {
122: JRStyle style = (JRStyle) it.next();
123: if (nameMatches(style, name)) {
124: if (style.isDefault()) {
125: defaultStyle = null;
126: }
127:
128: removed = style;
129: it.remove();
130: break;
131: }
132: }
133: return removed;
134: }
135:
136: public JRStyle[] getStyles() {
137: return (JRStyle[]) styles.toArray(new JRStyle[styles.size()]);
138: }
139:
140: public JRStyle getDefaultStyle() {
141: return defaultStyle;
142: }
143:
144: public JRReportFont getDefaultFont() {
145: return null;
146: }
147:
148: /**
149: * Adds an included template.
150: *
151: * @param reference the template reference
152: * @see #getIncludedTemplates()
153: */
154: public void addIncludedTemplate(JRTemplateReference reference) {
155: includedTemplates.add(reference);
156: }
157:
158: /**
159: * Adds an included template.
160: *
161: * @param templateLocation the template location
162: * @see #getIncludedTemplates()
163: */
164: public void addIncludedTemplate(String templateLocation) {
165: includedTemplates
166: .add(new JRTemplateReference(templateLocation));
167: }
168:
169: /**
170: * Removes an included template.
171: *
172: * @param reference the template reference to remove
173: * @return <code>true</code> iff the included template has been found and removed
174: */
175: public boolean removeIncludedTemplate(JRTemplateReference reference) {
176: return includedTemplates.remove(reference);
177: }
178:
179: /**
180: * Removes an included template.
181: * <p/>
182: * The first template reference that matches the location is removed.
183: *
184: * @param location the location of the template to remove
185: * @return the removed template reference, or <code>null</code> if not found
186: */
187: public JRTemplateReference removeIncludedTemplate(String location) {
188: JRTemplateReference removed = null;
189: for (ListIterator it = includedTemplates.listIterator(); it
190: .hasNext();) {
191: JRTemplateReference ref = (JRTemplateReference) it.next();
192: if (ref.getLocation().equals(location)) {
193: removed = ref;
194: it.remove();
195: }
196: }
197: return removed;
198: }
199:
200: public JRTemplateReference[] getIncludedTemplates() {
201: return (JRTemplateReference[]) includedTemplates
202: .toArray(new JRTemplateReference[includedTemplates
203: .size()]);
204: }
205:
206: }
|