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: * StyleCache.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report.layout;
030:
031: import java.util.HashMap;
032:
033: import org.jfree.report.layout.style.NonPaddingWrapperStyleSheet;
034: import org.jfree.report.layout.style.SimpleStyleSheet;
035: import org.jfree.report.style.StyleKey;
036: import org.jfree.report.style.StyleSheet;
037: import org.jfree.report.util.InstanceID;
038:
039: /**
040: * Creation-Date: 26.04.2007, 20:47:23
041: *
042: * @author Thomas Morgner
043: */
044: public class StyleCache {
045: private static class CacheCarrier {
046: private long changeTracker;
047: private SimpleStyleSheet styleSheet;
048:
049: protected CacheCarrier(final long changeTracker,
050: final SimpleStyleSheet styleSheet) {
051: this .changeTracker = changeTracker;
052: this .styleSheet = styleSheet;
053: }
054:
055: public long getChangeTracker() {
056: return changeTracker;
057: }
058:
059: public SimpleStyleSheet getStyleSheet() {
060: return styleSheet;
061: }
062: }
063:
064: private HashMap cache;
065: private StyleKey[] definedStyleKeys;
066: private boolean omitPadding;
067: private NonPaddingWrapperStyleSheet nonPaddingWrapperStyleSheet;
068:
069: public StyleCache(final StyleKey[] definedStyleKeys,
070: final boolean omitPadding) {
071: this .definedStyleKeys = definedStyleKeys;
072: this .omitPadding = omitPadding;
073: this .cache = new HashMap();
074: this .nonPaddingWrapperStyleSheet = new NonPaddingWrapperStyleSheet();
075: }
076:
077: public SimpleStyleSheet getStyleSheet(final StyleSheet parent) {
078: if (omitPadding) {
079: // this only works, because we know that the created SimpleStyleSheet will not hold any references
080: // to this wrapper-stylesheet.
081: synchronized (this ) {
082: nonPaddingWrapperStyleSheet.setParent(parent);
083:
084: try {
085: final InstanceID id = nonPaddingWrapperStyleSheet
086: .getId();
087: final CacheCarrier cc = (CacheCarrier) cache
088: .get(id);
089: if (cc == null) {
090: final SimpleStyleSheet styleSheet = new SimpleStyleSheet(
091: nonPaddingWrapperStyleSheet,
092: definedStyleKeys);
093: cache.put(id,
094: new CacheCarrier(
095: nonPaddingWrapperStyleSheet
096: .getChangeTracker(),
097: styleSheet));
098: return styleSheet;
099: }
100:
101: if (cc.getChangeTracker() != nonPaddingWrapperStyleSheet
102: .getChangeTracker()) {
103: final SimpleStyleSheet styleSheet = new SimpleStyleSheet(
104: nonPaddingWrapperStyleSheet,
105: definedStyleKeys);
106: cache.put(id,
107: new CacheCarrier(
108: nonPaddingWrapperStyleSheet
109: .getChangeTracker(),
110: styleSheet));
111: return styleSheet;
112: }
113:
114: return cc.getStyleSheet();
115: } finally {
116: nonPaddingWrapperStyleSheet.setParent(null);
117: }
118: }
119: }
120:
121: final InstanceID id = parent.getId();
122: final CacheCarrier cc = (CacheCarrier) cache.get(id);
123: if (cc == null) {
124: final SimpleStyleSheet styleSheet = new SimpleStyleSheet(
125: parent, definedStyleKeys);
126: cache.put(id, new CacheCarrier(parent.getChangeTracker(),
127: styleSheet));
128: return styleSheet;
129: }
130:
131: if (cc.getChangeTracker() != parent.getChangeTracker()) {
132: final SimpleStyleSheet styleSheet = new SimpleStyleSheet(
133: parent, definedStyleKeys);
134: cache.put(id, new CacheCarrier(parent.getChangeTracker(),
135: styleSheet));
136: return styleSheet;
137: }
138:
139: return cc.getStyleSheet();
140: }
141: }
|