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: * ExcelFontFactory.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.HashMap;
033:
034: import org.apache.poi.hssf.usermodel.HSSFFont;
035: import org.apache.poi.hssf.usermodel.HSSFWorkbook;
036:
037: /**
038: * This class keeps track of all fonts that we have used so far in our Excel file.
039: * <p/>
040: * Excel fonts should never be created directly, as excel does not like the idea of having
041: * too many font definitions.
042: *
043: * @author Heiko Evermann
044: */
045: public class ExcelFontFactory {
046: /**
047: * The list of fonts that we have used so far.
048: */
049: private HashMap fonts;
050:
051: /**
052: * The workbook that is used to create the font.
053: */
054: private final HSSFWorkbook workbook;
055:
056: /**
057: * Constructor for ExcelFontFactory.
058: *
059: * @param workbook the workbook.
060: */
061: public ExcelFontFactory(final HSSFWorkbook workbook) {
062: this .fonts = new HashMap();
063: this .workbook = workbook;
064:
065: // read the fonts from the workbook ...
066: // Funny one: Please note that the layout will be broken if the first
067: // font is not 'Arial 10'.
068: final short numberOfFonts = this .workbook.getNumberOfFonts();
069: for (int i = 0; i < numberOfFonts; i++) {
070: final HSSFFont font = workbook.getFontAt((short) i);
071: this .fonts.put(new HSSFFontWrapper(font), font);
072: }
073:
074: // add the default font
075: // this MUST be the first one, that is created.
076: // oh, I hate Excel ...
077: final HSSFFontWrapper wrapper = new HSSFFontWrapper("Arial",
078: (short) 10, false, false, false, false, Color.black);
079: getExcelFont(wrapper);
080: }
081:
082: /**
083: * Creates a HSSFFont. The created font is cached and reused later, if a similiar font
084: * is requested.
085: *
086: * @param wrapper the font information that should be used to produce the excel font
087: * @return the created or a cached HSSFFont instance
088: */
089: public HSSFFont getExcelFont(final HSSFFontWrapper wrapper) {
090: if (fonts.containsKey(wrapper)) {
091: return (HSSFFont) fonts.get(wrapper);
092: }
093:
094: // ok, we need a new one ...
095: final HSSFFont excelFont = createFont(wrapper);
096: fonts.put(wrapper, excelFont);
097: return excelFont;
098: }
099:
100: /**
101: * Returns the excel font stored in this wrapper.
102: *
103: * @param wrapper the font wrapper that holds all font information from
104: * the repagination.
105: * @return the created font.
106: */
107: private HSSFFont createFont(final HSSFFontWrapper wrapper) {
108: final HSSFFont font = workbook.createFont();
109: if (wrapper.isBold()) {
110: font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
111: } else {
112: font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
113: }
114: font.setColor(wrapper.getColorIndex());
115: font.setFontName(wrapper.getFontName());
116: font.setFontHeightInPoints((short) wrapper.getFontHeight());
117: font.setItalic(wrapper.isItalic());
118: font.setStrikeout(wrapper.isStrikethrough());
119: if (wrapper.isUnderline()) {
120: font.setUnderline(HSSFFont.U_SINGLE);
121: } else {
122: font.setUnderline(HSSFFont.U_NONE);
123: }
124: return font;
125: }
126:
127: }
|