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: * HSSFFontWrapper.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:
033: import org.apache.poi.hssf.usermodel.HSSFFont;
034:
035: /**
036: * The HSSFFontWrapper is used to store excel style font informations.
037: *
038: * @author Heiko Evermann
039: */
040: public final class HSSFFontWrapper {
041: /**
042: * scale between Excel and awt. With this value it looks fine.
043: */
044: public static final int FONT_FACTOR = 20;
045:
046: /**
047: * the font name.
048: */
049: private final String fontName;
050:
051: /**
052: * the excel color index.
053: */
054: private final short colorIndex;
055:
056: /**
057: * the font size.
058: */
059: private final int fontHeight;
060:
061: /**
062: * the font's bold flag.
063: */
064: private final boolean bold;
065:
066: /**
067: * the font's italic flag.
068: */
069: private final boolean italic;
070:
071: /**
072: * the font's underline flag.
073: */
074: private final boolean underline;
075:
076: /**
077: * the font's strikethrough flag.
078: */
079: private final boolean strikethrough;
080:
081: /**
082: * the cached hashcode.
083: */
084: private int hashCode;
085:
086: /**
087: * Creates a new HSSFFontWrapper for the given font and color.
088: *
089: * @param fontName the name of the wrapped font.
090: * @param fontSize the name of the wrapped font.
091: * @param bold a font style flag.
092: * @param italic a font style flag.
093: * @param underline a font style flag.
094: * @param strikethrough a font style flag.
095: * @param color the foreground color.
096: */
097: public HSSFFontWrapper(final String fontName, final short fontSize,
098: final boolean bold, final boolean italic,
099: final boolean underline, final boolean strikethrough,
100: final Color color) {
101: if (fontName == null) {
102: throw new NullPointerException("FontDefinition is null");
103: }
104: if (color == null) {
105: throw new NullPointerException("Color is null");
106: }
107:
108: if ("SansSerif".equalsIgnoreCase(fontName)) {
109: this .fontName = "Arial";
110: } else if ("Monosspace".equalsIgnoreCase(fontName)) {
111: this .fontName = "Courier New";
112: } else if ("Serif".equalsIgnoreCase(fontName)) {
113: this .fontName = "Times New Roman";
114: } else {
115: this .fontName = fontName;
116: }
117:
118: this .colorIndex = ExcelColorSupport.getNearestColor(color);
119: this .fontHeight = fontSize;
120: this .bold = bold;
121: this .italic = italic;
122: this .underline = underline;
123: this .strikethrough = strikethrough;
124: }
125:
126: /**
127: * Creates a HSSFFontWrapper for the excel font.
128: *
129: * @param font the font.
130: */
131: public HSSFFontWrapper(final HSSFFont font) {
132: if (font == null) {
133: throw new NullPointerException("Font is null");
134: }
135: fontName = font.getFontName();
136: colorIndex = font.getColor();
137: fontHeight = font.getFontHeightInPoints();
138: italic = font.getItalic();
139: bold = (font.getBoldweight() == HSSFFont.BOLDWEIGHT_BOLD);
140: underline = (font.getUnderline() != HSSFFont.U_NONE);
141: strikethrough = font.getStrikeout();
142: }
143:
144: /**
145: * Indicates whether some other object is "equal to" this one.
146: *
147: * @param o the compared object.
148: * @return true, if the font wrapper contains the same font definition, false
149: * otherwise.
150: */
151: public boolean equals(final Object o) {
152: if (this == o) {
153: return true;
154: }
155: if (!(o instanceof HSSFFontWrapper)) {
156: return false;
157: }
158:
159: final HSSFFontWrapper wrapper = (HSSFFontWrapper) o;
160:
161: if (bold != wrapper.bold) {
162: return false;
163: }
164: if (underline != wrapper.strikethrough) {
165: return false;
166: }
167: if (strikethrough != wrapper.strikethrough) {
168: return false;
169: }
170: if (colorIndex != wrapper.colorIndex) {
171: return false;
172: }
173: if (fontHeight != wrapper.fontHeight) {
174: return false;
175: }
176: if (italic != wrapper.italic) {
177: return false;
178: }
179: if (!fontName.equals(wrapper.fontName)) {
180: return false;
181: }
182:
183: return true;
184: }
185:
186: /**
187: * Returns a hash code value for the object. This method is supported for the benefit of
188: * hashtables such as those provided by <code>java.util.Hashtable</code>.
189: *
190: * @return the hash code.
191: */
192: public int hashCode() {
193: if (hashCode == 0) {
194: int result = fontName.hashCode();
195: result = 29 * result + colorIndex;
196: result = 29 * result + fontHeight;
197: result = 29 * result + (bold ? 1 : 0);
198: result = 29 * result + (italic ? 1 : 0);
199: hashCode = result;
200: }
201: return hashCode;
202: }
203:
204: public boolean isBold() {
205: return bold;
206: }
207:
208: public short getColorIndex() {
209: return colorIndex;
210: }
211:
212: public int getFontHeight() {
213: return fontHeight;
214: }
215:
216: public String getFontName() {
217: return fontName;
218: }
219:
220: public int getHashCode() {
221: return hashCode;
222: }
223:
224: public boolean isItalic() {
225: return italic;
226: }
227:
228: public boolean isStrikethrough() {
229: return strikethrough;
230: }
231:
232: public boolean isUnderline() {
233: return underline;
234: }
235: }
|