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: * CachingRenderableTextFactory.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report.layout.text;
030:
031: import java.util.ArrayList;
032:
033: import org.jfree.report.layout.model.RenderNode;
034: import org.jfree.report.style.StyleKey;
035: import org.jfree.report.style.StyleSheet;
036: import org.jfree.util.ObjectUtilities;
037:
038: /**
039: * This one does not work as expected.
040: *
041: * @author Thomas Morgner
042: */
043: public class CachingRenderableTextFactory implements
044: RenderableTextFactory {
045: public static class CacheEntry {
046: private Object[] styleInformation;
047: private RenderNode[] text;
048:
049: public CacheEntry(final Object[] styleKeys,
050: final RenderNode[] text) {
051: this .styleInformation = styleKeys;
052: this .text = text;
053: }
054:
055: public RenderNode[] getText() {
056: final RenderNode[] textcopy = (RenderNode[]) text.clone();
057: final int length = textcopy.length;
058: for (int i = 0; i < length; i++) {
059: textcopy[i] = textcopy[i].derive(true);
060: }
061: return textcopy;
062: }
063:
064: public boolean isSameStyle(final StyleSheet styleSheet,
065: final StyleKey[] styleKeys) {
066: final int styleCount = styleKeys.length;
067: for (int i = 0; i < styleCount; i++) {
068: final StyleKey key = styleKeys[i];
069: final Object value = styleInformation[key
070: .getIdentifier()];
071: if (value != CacheStyleSheet.UNDEFINED) {
072: final Object styleProperty = styleSheet
073: .getStyleProperty(key, null);
074: if (ObjectUtilities.equal(styleProperty, value) == false) {
075: return false;
076: }
077: }
078: }
079: return true;
080: }
081: }
082:
083: private RenderableTextFactory otherTextFactory;
084: private int keyCount;
085: private ArrayList values;
086: private CacheStyleSheet cacheStyleSheet;
087:
088: public CachingRenderableTextFactory(
089: final RenderableTextFactory otherTextFactory,
090: final int keyCount) {
091: this .otherTextFactory = otherTextFactory;
092: this .keyCount = keyCount;
093: this .values = new ArrayList();
094: }
095:
096: public ArrayList getValues() {
097: return values;
098: }
099:
100: public void startText() {
101: otherTextFactory.startText();
102: values.clear();
103: }
104:
105: /**
106: * The text is given as CodePoints.
107: *
108: * @param text
109: * @return
110: */
111: public RenderNode[] createText(final int[] text, final int offset,
112: final int length, final StyleSheet layoutContext) {
113: if (cacheStyleSheet == null) {
114: cacheStyleSheet = new CacheStyleSheet(layoutContext,
115: keyCount);
116: } else {
117: if (layoutContext != cacheStyleSheet.getParent()) {
118: throw new IllegalStateException();
119: }
120: }
121: final RenderNode[] nodes = otherTextFactory.createText(text,
122: offset, length, cacheStyleSheet);
123: final int nodeCount = nodes.length;
124: for (int i = 0; i < nodeCount; i++) {
125: values.add(nodes[i].derive(true));
126: }
127: return nodes;
128: }
129:
130: public RenderNode[] finishText() {
131: final RenderNode[] nodes = otherTextFactory.finishText();
132: final int length = nodes.length;
133: for (int i = 0; i < length; i++) {
134: values.add(nodes[i].derive(true));
135: }
136: return nodes;
137: }
138:
139: public CacheEntry getCacheEntry() {
140: final RenderNode[] objects = (RenderNode[]) values
141: .toArray(new RenderNode[values.size()]);
142: return new CacheEntry(cacheStyleSheet.getEntries(), objects);
143: }
144:
145: public void clear() {
146: values.clear();
147: if (cacheStyleSheet != null) {
148: cacheStyleSheet.lock();
149: }
150: cacheStyleSheet = null;
151: }
152: }
|