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: * ExcelTextExtractor.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report.modules.output.table.xls.helper;
030:
031: import java.awt.Color;
032: import java.util.ArrayList;
033:
034: import org.apache.poi.hssf.usermodel.HSSFRichTextString;
035: import org.jfree.report.layout.model.InlineRenderBox;
036: import org.jfree.report.layout.model.RenderBox;
037: import org.jfree.report.layout.output.OutputProcessorMetaData;
038: import org.jfree.report.modules.output.table.base.DefaultTextExtractor;
039: import org.jfree.report.style.ElementStyleKeys;
040: import org.jfree.report.style.StyleSheet;
041: import org.jfree.report.style.TextStyleKeys;
042:
043: /**
044: * Creation-Date: 10.05.2007, 19:49:46
045: *
046: * @author Thomas Morgner
047: */
048: public class ExcelTextExtractor extends DefaultTextExtractor {
049: private static class RichTextFormat {
050: private int position;
051: private HSSFFontWrapper font;
052:
053: protected RichTextFormat(final int position,
054: final HSSFFontWrapper font) {
055: this .position = position;
056: this .font = font;
057: }
058:
059: public int getPosition() {
060: return position;
061: }
062:
063: public HSSFFontWrapper getFont() {
064: return font;
065: }
066:
067: }
068:
069: private ArrayList buffer;
070:
071: public ExcelTextExtractor(final OutputProcessorMetaData metaData) {
072: super (metaData);
073: buffer = new ArrayList();
074: }
075:
076: public Object compute(final RenderBox paraBox,
077: final ExcelFontFactory fontFactory) {
078: buffer.clear();
079: super .compute(paraBox);
080:
081: final String text = getText();
082: if (buffer.size() <= 1) {
083: // A simple result. So there's no need to create a rich-text string.
084: final Object rawResult = getRawResult();
085: if (rawResult != null) {
086: return rawResult;
087: }
088: if (text.length() > 0) {
089: return text;
090: }
091: return null;
092: } else if (text.length() > 0) {
093: // There's rich text.
094: final HSSFRichTextString rtStr = new HSSFRichTextString(
095: text);
096: for (int i = 0; i < buffer.size(); i++) {
097: final RichTextFormat o = (RichTextFormat) buffer.get(i);
098: final int position = o.getPosition();
099: final HSSFFontWrapper font = o.getFont();
100: if (i == (buffer.size() - 1)) {
101: // Last element ..
102: rtStr.applyFont(position, text.length() - position,
103: fontFactory.getExcelFont(font));
104: } else {
105: final RichTextFormat next = (RichTextFormat) buffer
106: .get(i + 1);
107: rtStr.applyFont(position, next.getPosition()
108: - position, fontFactory.getExcelFont(font));
109: }
110: }
111: return rtStr;
112: }
113: return null;
114: }
115:
116: protected boolean startInlineBox(final InlineRenderBox box) {
117: if (box.isBoxVisible(getParagraphBounds()) == false) {
118: return false;
119: }
120:
121: final StyleSheet styleSheet = box.getStyleSheet();
122: final Color textColor = (Color) styleSheet
123: .getStyleProperty(ElementStyleKeys.PAINT);
124: final String fontName = (String) styleSheet
125: .getStyleProperty(TextStyleKeys.FONT);
126: final short fontSize = (short) styleSheet.getIntStyleProperty(
127: TextStyleKeys.FONTSIZE, 0);
128: final boolean bold = styleSheet
129: .getBooleanStyleProperty(TextStyleKeys.BOLD);
130: final boolean italic = styleSheet
131: .getBooleanStyleProperty(TextStyleKeys.ITALIC);
132: final boolean underline = styleSheet
133: .getBooleanStyleProperty(TextStyleKeys.UNDERLINED);
134: final boolean strikethrough = styleSheet
135: .getBooleanStyleProperty(TextStyleKeys.STRIKETHROUGH);
136: final HSSFFontWrapper wrapper = new HSSFFontWrapper(fontName,
137: fontSize, bold, italic, underline, strikethrough,
138: textColor);
139: final RichTextFormat rtf = new RichTextFormat(getTextLength(),
140: wrapper);
141:
142: // Check the style.
143: if (buffer.isEmpty()) {
144: buffer.add(rtf);
145: } else {
146: final RichTextFormat lastRtf = (RichTextFormat) buffer
147: .get(buffer.size() - 1);
148: if (lastRtf.getFont().equals(rtf.getFont()) == false) {
149: buffer.add(rtf);
150: }
151: }
152:
153: return true;
154: }
155:
156: }
|