KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > elementprocessor > impl > poi > hssf > elements > EPFont


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.cocoon.components.elementprocessor.impl.poi.hssf.elements;
18
19 import org.apache.cocoon.components.elementprocessor.types.Attribute;
20 import org.apache.cocoon.components.elementprocessor.ElementProcessor;
21
22 import org.apache.cocoon.components.elementprocessor.types.BooleanConverter;
23 import org.apache.cocoon.components.elementprocessor.types.BooleanResult;
24 import org.apache.cocoon.components.elementprocessor.types.NumericConverter;
25 import org.apache.cocoon.components.elementprocessor.types.NumericResult;
26 import org.apache.cocoon.components.elementprocessor.types.Validator;
27
28 import org.apache.poi.hssf.usermodel.HSSFCellStyle;
29 import org.apache.poi.hssf.usermodel.HSSFFont;
30 import org.apache.poi.hssf.util.HSSFColor;
31
32 import java.io.IOException JavaDoc;
33 import java.util.Hashtable JavaDoc;
34
35 /**
36  * No-op implementation of ElementProcessor to handle the "Font" tag
37  *
38  * This element has five attributes and also holds the name of the
39  * font as element content.
40  *
41  * This element is not used in HSSFSerializer 1.0
42  *
43  * @author Marc Johnson (marc_johnson27591@hotmail.com)
44  * @author Andrew C. Oliver (acoliver2@users.sourceforge.net)
45  * @version CVS $Id: EPFont.java 30932 2004-07-29 17:35:38Z vgritsenko $
46  */

47 public class EPFont extends BaseElementProcessor {
48     private NumericResult _unit;
49     private BooleanResult _bold;
50     private BooleanResult _italic;
51     private NumericResult _underline;
52     private BooleanResult _strike_through;
53     private String JavaDoc _font;
54     private HSSFFont hssfFont;
55     private static final String JavaDoc _unit_attribute = "Unit";
56     private static final String JavaDoc _bold_attribute = "Bold";
57     private static final String JavaDoc _italic_attribute = "Italic";
58     private static final String JavaDoc _underline_attribute = "Underline";
59     private static final String JavaDoc _strike_through_attribute = "StrikeThrough";
60     private static final Validator _underline_type_validator =
61         new Validator() {
62             public IOException JavaDoc validate(final Number JavaDoc number) {
63                 return UnderlineType.isValid(number.intValue()) ? null
64                   : new IOException JavaDoc("\"" + number + "\" is not a legal value");
65         }
66     };
67
68     /**
69      * constructor
70      */

71     public EPFont() {
72         super(null);
73         _unit = null;
74         _bold = null;
75         _italic = null;
76         _underline = null;
77         _strike_through = null;
78         _font = null;
79     }
80
81     /**
82      * Override of Initialize() implementation
83      * @param attributes the array of Attribute instances; may be empty, will
84      * never be null
85      * @param parent the parent ElementProcessor; may be null
86      * @exception IOException if anything is wrong
87      */

88     public void initialize(final Attribute[] attributes,
89             final ElementProcessor parent) throws IOException JavaDoc {
90         super.initialize(attributes, parent);
91         EPStyle pstyle = (EPStyle)parent;
92         if (pstyle.isValid()) {
93             Hashtable JavaDoc colorhash = pstyle.getColorHash();
94             HSSFColor color = null;
95
96             HSSFCellStyle style = pstyle.getStyle();
97             //style.setFillForegroundColor(
98
Workbook workbook = getWorkbook();
99             HSSFFont font = workbook.createFont();
100             style.setFont(font);
101             font.setFontHeightInPoints((short)getUnit());
102             //font.setFontName(getFont());
103
font.setItalic(getItalic());
104             if (getBold()) {
105                 font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
106             } else {
107                 font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
108             }
109             font.setUnderline((byte)getUnderline());
110             font.setStrikeout(getStrikeThrough());
111
112             color = (HSSFColor)colorhash.get(
113                     pstyle.getForegroundColor().toString());
114             //System.out.println(pstyle.getForegroundColor().toString());
115
if (color == null)
116                 color = new HSSFColor.BLACK();
117             font.setColor(color.getIndex());
118             hssfFont = font;
119         }
120     }
121
122     /**
123      * @return unit
124      * @exception IOException
125      */

126     public double getUnit() throws IOException JavaDoc {
127         if (_unit == null) {
128             _unit = NumericConverter.extractDouble(getValue(_unit_attribute));
129         }
130         return _unit.doubleValue();
131     }
132
133     /**
134      * @return bold
135      * @exception IOException
136      */

137     public boolean getBold() throws IOException JavaDoc {
138         if (_bold == null) {
139             _bold = BooleanConverter.extractBoolean(getValue(_bold_attribute));
140         }
141         return _bold.booleanValue();
142     }
143
144     /**
145      * @return italic
146      * @exception IOException
147      */

148     public boolean getItalic() throws IOException JavaDoc {
149         if (_italic == null) {
150             _italic =
151                 BooleanConverter.extractBoolean(getValue(_italic_attribute));
152         }
153         return _italic.booleanValue();
154     }
155
156     /**
157      * @return underline as one of the public variables in UnderlineType
158      * @exception IOException
159      */

160     public int getUnderline() throws IOException JavaDoc {
161         if (_underline == null) {
162             _underline = NumericConverter.extractInteger(
163                     getValue(_underline_attribute), _underline_type_validator);
164         }
165         return _underline.intValue();
166     }
167
168     /**
169      * @return strikeThrough
170      * @exception IOException
171      */

172
173     public boolean getStrikeThrough() throws IOException JavaDoc {
174         if (_strike_through == null) {
175             _strike_through = BooleanConverter.extractBoolean(
176                     getValue(_strike_through_attribute));
177         }
178         return _strike_through.booleanValue();
179     }
180
181     /**
182      * @return name of the font
183      */

184
185     public String JavaDoc getFont() {
186         if (_font == null) {
187             try {
188                 _font = getData();
189             } catch (NullPointerException JavaDoc ignored) { /* VOID */}
190         }
191
192         return _font;
193     }
194
195     private HSSFFont getHSSFFont() {
196         return hssfFont;
197     }
198
199     /**
200      * push the data into the font
201      * @exception IOException
202      */

203
204     public void endProcessing() throws IOException JavaDoc {
205         String JavaDoc thefont = getFont();
206         if (thefont != null && !thefont.trim().equals("")
207                 && getHSSFFont() != null) {
208             getHSSFFont().setFontName(thefont);
209         } else if (getHSSFFont() != null) {
210             getHSSFFont().setFontName("Arial"); //default excel font
211
}
212     }
213 } // end public class EPFont
214
Popular Tags