KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.IOException JavaDoc;
20 import java.util.Hashtable JavaDoc;
21
22 import org.apache.cocoon.components.elementprocessor.ElementProcessor;
23 import org.apache.cocoon.components.elementprocessor.types.Attribute;
24 import org.apache.cocoon.components.elementprocessor.types.BooleanConverter;
25 import org.apache.cocoon.components.elementprocessor.types.BooleanResult;
26 import org.apache.cocoon.components.elementprocessor.types.NumericConverter;
27 import org.apache.cocoon.components.elementprocessor.types.NumericResult;
28 import org.apache.cocoon.components.elementprocessor.types.Validator;
29
30 import org.apache.poi.hssf.usermodel.HSSFCellStyle;
31 import org.apache.poi.hssf.usermodel.HSSFDataFormat;
32 import org.apache.poi.hssf.util.HSSFColor;
33
34 /**
35  * No-op implementation of ElementProcessor to handle the "Style" tag
36  *
37  * This element is a container of other elements and has several
38  * attributes
39  *
40  * This element is not used in HSSFSerializer 1.0
41  *
42  * @author Marc Johnson (marc_johnson27591@hotmail.com)
43  * @author Andrew C. Oliver (acoliver2@users.sourceforge.net)
44  * @version CVS $Id: EPStyle.java 37373 2004-09-02 12:54:22Z vgritsenko $
45  */

46 public class EPStyle extends BaseElementProcessor {
47     private static final String JavaDoc _general_format = "General";
48     private HorizontalAlignment _h_align;
49     private VerticalAlignment _v_align;
50     private BooleanResult _wrap_text;
51     private StyleOrientation _orient;
52     private NumericResult _shade;
53     private NumericResult _indent;
54     private ColorCode _fore;
55     private ColorCode _back;
56     private ColorCode _pattern_color;
57     private String JavaDoc _format;
58     private static final String JavaDoc _h_align_attribute = "HAlign";
59     private static final String JavaDoc _v_align_attribute = "VAlign";
60     private static final String JavaDoc _wrap_text_attribute = "WrapText";
61     private static final String JavaDoc _orient_attribute = "Orient";
62     private static final String JavaDoc _shade_attribute = "Shade";
63     private static final String JavaDoc _indent_attribute = "Indent";
64     private static final String JavaDoc _fore_attribute = "Fore";
65     private static final String JavaDoc _back_attribute = "Back";
66     private static final String JavaDoc _pattern_color_attribute = "PatternColor";
67     private static final String JavaDoc _format_attribute = "Format";
68
69     private boolean invalid;
70
71     private static final Validator _shade_validator = new Validator() {
72         public IOException JavaDoc validate(final Number JavaDoc number) {
73             return StyleShading.isValid(number.intValue()) ? null
74                 : new IOException JavaDoc("\"" + number + "\" is not a legal value");
75         }
76     };
77
78     /**
79      * constructor
80      */

81     public EPStyle() {
82         super(null);
83         _h_align = null;
84         _v_align = null;
85         _wrap_text = null;
86         _orient = null;
87         _shade = null;
88         _indent = null;
89         _fore = null;
90         _back = null;
91         _pattern_color = null;
92         _format = null;
93     }
94
95     /**
96      * Override of Initialize() implementation
97      * @param attributes the array of Attribute instances; may be empty, will
98      * never be null
99      * @param parent the parent ElementProcessor; may be null
100      * @exception IOException if anything is wrong
101      */

102     public void initialize(final Attribute[] attributes,
103                     final ElementProcessor parent) throws IOException JavaDoc {
104         super.initialize(attributes, parent);
105
106         EPStyleRegion sregion = (EPStyleRegion)parent;
107
108         if (sregion.isValid()) {
109             Hashtable JavaDoc colorhash = sregion.getColorHash();
110
111             HSSFCellStyle style = sregion.getStyle();
112             short cnvhalign =
113                 convertAlignment(getHorizontalAlignment().getCode());
114             style.setAlignment(cnvhalign);
115             short cnvvalign =
116                 convertVAlignment(getVerticalAlignment().getCode());
117             style.setVerticalAlignment(cnvvalign);
118             style.setFillPattern((short)getShade());
119
120             Workbook workbook = getWorkbook();
121             HSSFDataFormat dataformat = workbook.createDataFormat();
122             if (getShade() == 1) {
123                 // TODO: change to constant when upgrade to new HSSF
124
// solid w/foreground, bg doesn't matter
125
if (getLogger().isDebugEnabled()) {
126                     getLogger().debug("shade = 1");
127                 }
128                 HSSFColor color =
129                     (HSSFColor)colorhash.get(getBackgroundColor().toString());
130                 if (color == null) {
131                     if (getLogger().isDebugEnabled()) {
132                         getLogger().debug("s1 BG couldn't find color for "
133                                 + getBackgroundColor().toString());
134                     }
135                     color = new HSSFColor.WHITE();
136                 }
137                 style.setFillForegroundColor(color.getIndex());
138                 color = (HSSFColor)colorhash.get(getPatternColor().toString());
139                 if (color == null) {
140                     if (getLogger().isDebugEnabled()) {
141                         getLogger().debug("s1 PC couldn't find color for "
142                                 + getPatternColor().toString());
143                     }
144                     color = new HSSFColor.BLACK();
145                 }
146                 style.setFillBackgroundColor(color.getIndex());
147             } else {
148                 HSSFColor color =
149                     (HSSFColor)colorhash.get(getBackgroundColor().toString());
150                 if (color == null) {
151                     if (getLogger().isDebugEnabled()) {
152                         getLogger().debug(
153                             "BG couldn't find color for "
154                                 + getBackgroundColor().toString());
155                     }
156                     color = new HSSFColor.BLACK();
157                 }
158                 style.setFillBackgroundColor(color.getIndex());
159                 color = (HSSFColor)colorhash.get(getPatternColor().toString());
160                 if (color == null) {
161                     if (getLogger().isDebugEnabled()) {
162                         getLogger().debug("PC couldn't find color for "
163                                 + getPatternColor().toString());
164                     }
165                     color = new HSSFColor.WHITE();
166                 }
167                 style.setFillForegroundColor(color.getIndex());
168             }
169             style.setWrapText(getWrapText());
170             style.setLocked(true);
171
172             String JavaDoc format = null;
173             try {
174                 format = getFormat();
175             } catch (NullPointerException JavaDoc e) {
176                 format = _general_format;
177             }
178
179             if (!_general_format.equals(format)) {
180                 short valuenumber;
181                 format = kludgeForGnumericMisformats(format);
182                 format = kludgeForGnumericDateDivergence(format);
183                 if (getLogger().isDebugEnabled()) {
184                     getLogger().debug("setting format to " + format);
185                 }
186                 Object JavaDoc o =
187                     workbook.getValidate(format, dataformat.getFormat(format));
188                 Short JavaDoc sh = null;
189                 sh = (Short JavaDoc)o;
190                 valuenumber = sh.shortValue();
191                 style.setDataFormat(valuenumber);
192             }
193         } else {
194             invalid = true;
195         }
196     }
197
198     /**
199      * @return true if horizontal alignment general bit is set
200      * @exception IOException
201      */

202     public boolean isHorizontalGeneral() throws IOException JavaDoc {
203         return getHorizontalAlignment().isGeneral();
204     }
205
206     /**
207      * @return true if horizontal alignment left bit is set
208      * @exception IOException
209      */

210     public boolean isHorizontalLeft() throws IOException JavaDoc {
211         return getHorizontalAlignment().isLeft();
212     }
213
214     /**
215      * @return true if horizontal alignment right bit is set
216      * @exception IOException
217      */

218     public boolean isHorizontalRight() throws IOException JavaDoc {
219         return getHorizontalAlignment().isRight();
220     }
221
222     /**
223      * @return true if horizontal alignment center bit is set
224      * @exception IOException
225      */

226     public boolean isHorizontalCenter() throws IOException JavaDoc {
227         return getHorizontalAlignment().isCenter();
228     }
229
230     /**
231      * @return true if horizontal alignment fill bit is set
232      * @exception IOException
233      */

234     public boolean isHorizontalFill() throws IOException JavaDoc {
235         return getHorizontalAlignment().isFill();
236     }
237
238     /**
239      * @return true if horizontal alignment justify bit is set
240      * @exception IOException
241      */

242     public boolean isHorizontalJustify() throws IOException JavaDoc {
243         return getHorizontalAlignment().isJustify();
244     }
245
246     /**
247      * @return true if horizontal alignment center across selection bit is set
248      * @exception IOException
249      */

250     public boolean isHorizontalCenterAcrossSelection() throws IOException JavaDoc {
251         return getHorizontalAlignment().isCenterAcrossSelection();
252     }
253
254     /**
255      * @return true if vertical alignment top bit is set
256      * @exception IOException
257      */

258     public boolean isVerticalTop() throws IOException JavaDoc {
259         return getVerticalAlignment().isTop();
260     }
261
262     /**
263      * @return true if vertical alignment bottom bit is set
264      * @exception IOException
265      */

266     public boolean isVerticalBottom() throws IOException JavaDoc {
267         return getVerticalAlignment().isBottom();
268     }
269
270     /**
271      * @return true if vertical alignment center bit is set
272      * @exception IOException
273      */

274     public boolean isVerticalCenter() throws IOException JavaDoc {
275         return getVerticalAlignment().isCenter();
276     }
277
278     /**
279      * @return true if vertical alignment justify bit is set
280      * @exception IOException
281      */

282     public boolean isVerticalJustify() throws IOException JavaDoc {
283         return getVerticalAlignment().isJustify();
284     }
285
286     /**
287      * @return true if wrap text is enabled
288      * @exception IOException
289      */

290     public boolean getWrapText() throws IOException JavaDoc {
291         if (_wrap_text == null) {
292             _wrap_text =
293                 BooleanConverter.extractBoolean(getValue(_wrap_text_attribute));
294         }
295         return _wrap_text.booleanValue();
296     }
297
298     /**
299      * @return true if style orientation horiz bit is set
300      * @exception IOException
301      */

302     public boolean isStyleOrientationHoriz() throws IOException JavaDoc {
303         return getStyleOrientation().isHoriz();
304     }
305
306     /**
307      * @return true if style orientation vert horiz text bit is set
308      * @exception IOException
309      */

310     public boolean isStyleOrientationVertHorizText() throws IOException JavaDoc {
311         return getStyleOrientation().isVertHorizText();
312     }
313
314     /**
315      * @return true if style orientation vert vert text bit is set
316      * @exception IOException
317      */

318     public boolean isStyleOrientationVertVertText() throws IOException JavaDoc {
319         return getStyleOrientation().isVertVertText();
320     }
321
322     /**
323      * @return true if style orientation vert vert text2 bit is set
324      * @exception IOException
325      */

326     public boolean isStyleOrientationVertVertText2() throws IOException JavaDoc {
327         return getStyleOrientation().isVertVertText2();
328     }
329
330     /**
331      * @return shade as one of the public variables in StyleShading
332      * @exception IOException
333      */

334     public int getShade() throws IOException JavaDoc {
335         if (_shade == null) {
336             _shade = NumericConverter.extractInteger(
337                     getValue(_shade_attribute), _shade_validator);
338         }
339         return _shade.intValue();
340     }
341
342     /**
343      * @return indent
344      * @exception IOException
345      */

346     public int getIndent() throws IOException JavaDoc {
347         if (_indent == null) {
348             _indent =
349                 NumericConverter.extractInteger(getValue(_indent_attribute));
350         }
351         return _indent.intValue();
352     }
353
354     /**
355      * @return foreground color
356      * @exception IOException
357      */

358     public ColorCode getForegroundColor() throws IOException JavaDoc {
359         if (_fore == null) {
360             _fore = new ColorCode(getValue(_fore_attribute));
361         }
362         return _fore;
363     }
364
365     /**
366      * @return background color
367      * @exception IOException
368      */

369     public ColorCode getBackgroundColor() throws IOException JavaDoc {
370         if (_back == null) {
371             _back = new ColorCode(getValue(_back_attribute));
372         }
373         return _back;
374     }
375
376     /**
377      * @return pattern color
378      * @exception IOException
379      */

380     public ColorCode getPatternColor() throws IOException JavaDoc {
381         if (_pattern_color == null) {
382             _pattern_color = new ColorCode(getValue(_pattern_color_attribute));
383         }
384         return _pattern_color;
385     }
386
387     /**
388      * @return format string
389      * @exception IOException
390      */

391     public String JavaDoc getFormat() throws IOException JavaDoc {
392         if (_format == null) {
393             _format = getValue(_format_attribute);
394             /*
395              * if (_format == null) { throw new IOException("missing " +
396              * _format_attribute + " attribute");
397              */

398         }
399         return _format;
400     }
401
402     private HorizontalAlignment getHorizontalAlignment() throws IOException JavaDoc {
403         if (_h_align == null) {
404             _h_align = new HorizontalAlignment(getValue(_h_align_attribute));
405         }
406         return _h_align;
407     }
408
409     private VerticalAlignment getVerticalAlignment() throws IOException JavaDoc {
410         if (_v_align == null) {
411             _v_align = new VerticalAlignment(getValue(_v_align_attribute));
412         }
413         return _v_align;
414     }
415
416     private StyleOrientation getStyleOrientation() throws IOException JavaDoc {
417         if (_orient == null) {
418             _orient = new StyleOrientation(getValue(_orient_attribute));
419         }
420         return _orient;
421     }
422
423     /**
424      * @return instance created in the EPStyles instance from
425      * HSSFColor.getTripletHash();
426      * @see org.apache.poi.hssf.util.HSSFColor#getTripletHash()
427      */

428     Hashtable JavaDoc getColorHash() {
429         return ((EPStyleRegion)getAncestor(EPStyleRegion.class)).getColorHash();
430     }
431
432     /**
433      * @return the HSSFCellStyle object associated with this style region.
434      */

435     HSSFCellStyle getStyle() {
436         return ((EPStyleRegion)getAncestor(EPStyleRegion.class)).getStyle();
437     }
438
439     /**
440      * @return validity (used to determine whether this is a big wasteful
441      * region with no purpose (gnumeric does this)
442      */

443     public boolean isValid() {
444         return (!invalid);
445     }
446
447     /**
448      * deal with mismatch between gnumeric align and Excel
449      */

450     private short convertAlignment(short alignment) {
451         short retval = HSSFCellStyle.ALIGN_GENERAL; // its 0
452

453         switch (alignment) {
454             case 1 :
455                 retval = HSSFCellStyle.ALIGN_GENERAL;
456                 break;
457             case 2 :
458                 retval = HSSFCellStyle.ALIGN_LEFT;
459                 break;
460             case 4 :
461                 retval = HSSFCellStyle.ALIGN_RIGHT;
462                 break;
463             case 8 :
464                 retval = HSSFCellStyle.ALIGN_CENTER;
465                 break;
466             case 16 :
467                 retval = HSSFCellStyle.ALIGN_FILL;
468                 break;
469             case 32 :
470                 retval = HSSFCellStyle.ALIGN_JUSTIFY;
471                 break;
472             case 64 :
473                 retval = HSSFCellStyle.ALIGN_CENTER_SELECTION;
474                 break;
475             default :
476                 retval = HSSFCellStyle.ALIGN_GENERAL;
477         }
478         return retval;
479     }
480
481     /**
482      * deal with mismatch between gnumeric valign and Excel
483      */

484     private short convertVAlignment(short alignment) {
485         short retval = HSSFCellStyle.VERTICAL_TOP; // its 0
486

487         switch (alignment) {
488             case 1 :
489                 retval = HSSFCellStyle.VERTICAL_TOP;
490                 break;
491             case 2 :
492                 retval = HSSFCellStyle.VERTICAL_BOTTOM;
493                 break;
494             case 4 :
495                 retval = HSSFCellStyle.VERTICAL_CENTER;
496                 break;
497             case 8 :
498                 retval = HSSFCellStyle.VERTICAL_JUSTIFY;
499                 break;
500             default :
501                 retval = HSSFCellStyle.VERTICAL_TOP;
502         }
503         return retval;
504     }
505
506     /**
507      * Takes in a Gnumeric format string and applies some rules to it. Some
508      * versions of Gnumeric seem to leave off the first parenthesis which
509      * causes them not to match the Excel-style format string. (which of course
510      * makes it a little hard to match)
511      */

512     private String JavaDoc kludgeForGnumericMisformats(String JavaDoc format) {
513         String JavaDoc retval = format;
514         if (getLogger().isDebugEnabled()) {
515             getLogger().debug("going out of the format kludger " + retval);
516             getLogger().debug("first )=" + format.indexOf(')'));
517             getLogger().debug("first (=" + format.indexOf('('));
518         }
519         if (format.indexOf(')') < format.indexOf('(')
520             && (format.indexOf(')') != -1)) {
521             retval = "(" + format;
522         }
523         if (getLogger().isDebugEnabled()) {
524             getLogger().debug("going out of the format kludger " + retval);
525         }
526         return retval;
527     }
528
529     private String JavaDoc kludgeForGnumericDateDivergence(String JavaDoc format) {
530         String JavaDoc retval = format;
531         if (getLogger().isDebugEnabled()) {
532             getLogger().debug(
533                 "going into the format kludgeForGnumericDateDivergence"
534                     + retval);
535         }
536
537         if (retval.equals("mm/dd/yy")) {
538             retval = "m/d/yy";
539         } else if (retval.equals("dd-mmm-yy")) {
540             retval = "d-mmm-yy";
541         } else if (retval.equals("dd-mmm")) {
542             retval = "d-mmm";
543         }
544         if (getLogger().isDebugEnabled()) {
545             getLogger().debug(
546                 "going out of the format kludgeForGnumericDateDivergence"
547                     + retval);
548         }
549         return retval;
550     }
551
552 } // end public class EPStyle
553
Popular Tags