KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hssf > usermodel > HSSFCellStyle


1 /* ====================================================================
2    Copyright 2002-2004 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
18 package org.apache.poi.hssf.usermodel;
19
20 import org.apache.poi.hssf.record.ExtendedFormatRecord;
21
22 /**
23  * High level representation of the style of a cell in a sheet of a workbook.
24  *
25  * @version 1.0-pre
26  *
27  * @author Andrew C. Oliver (acoliver at apache dot org)
28  * @author Jason Height (jheight at chariot dot net dot au)
29  * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#createCellStyle()
30  * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#getCellStyleAt(short)
31  * @see org.apache.poi.hssf.usermodel.HSSFCell#setCellStyle(HSSFCellStyle)
32  */

33
34 public class HSSFCellStyle
35 {
36     private ExtendedFormatRecord format = null;
37     private short index = 0;
38     private short fontindex = 0;
39
40     /**
41      * general (normal) horizontal alignment
42      */

43
44     public final static short ALIGN_GENERAL = 0x0;
45
46     /**
47      * left-justified horizontal alignment
48      */

49
50     public final static short ALIGN_LEFT = 0x1;
51
52     /**
53      * center horizontal alignment
54      */

55
56     public final static short ALIGN_CENTER = 0x2;
57
58     /**
59      * right-justified horizontal alignment
60      */

61
62     public final static short ALIGN_RIGHT = 0x3;
63
64     /**
65      * fill? horizontal alignment
66      */

67
68     public final static short ALIGN_FILL = 0x4;
69
70     /**
71      * justified horizontal alignment
72      */

73
74     public final static short ALIGN_JUSTIFY = 0x5;
75
76     /**
77      * center-selection? horizontal alignment
78      */

79
80     public final static short ALIGN_CENTER_SELECTION = 0x6;
81
82     /**
83      * top-aligned vertical alignment
84      */

85
86     public final static short VERTICAL_TOP = 0x0;
87
88     /**
89      * center-aligned vertical alignment
90      */

91
92     public final static short VERTICAL_CENTER = 0x1;
93
94     /**
95      * bottom-aligned vertical alignment
96      */

97
98     public final static short VERTICAL_BOTTOM = 0x2;
99
100     /**
101      * vertically justified vertical alignment
102      */

103
104     public final static short VERTICAL_JUSTIFY = 0x3;
105
106     /**
107      * No border
108      */

109
110     public final static short BORDER_NONE = 0x0;
111
112     /**
113      * Thin border
114      */

115
116     public final static short BORDER_THIN = 0x1;
117
118     /**
119      * Medium border
120      */

121
122     public final static short BORDER_MEDIUM = 0x2;
123
124     /**
125      * dash border
126      */

127
128     public final static short BORDER_DASHED = 0x3;
129
130     /**
131      * dot border
132      */

133
134     public final static short BORDER_HAIR = 0x4;
135
136     /**
137      * Thick border
138      */

139
140     public final static short BORDER_THICK = 0x5;
141
142     /**
143      * double-line border
144      */

145
146     public final static short BORDER_DOUBLE = 0x6;
147
148     /**
149      * hair-line border
150      */

151
152     public final static short BORDER_DOTTED = 0x7;
153
154     /**
155      * Medium dashed border
156      */

157
158     public final static short BORDER_MEDIUM_DASHED = 0x8;
159
160     /**
161      * dash-dot border
162      */

163
164     public final static short BORDER_DASH_DOT = 0x9;
165
166     /**
167      * medium dash-dot border
168      */

169
170     public final static short BORDER_MEDIUM_DASH_DOT = 0xA;
171
172     /**
173      * dash-dot-dot border
174      */

175
176     public final static short BORDER_DASH_DOT_DOT = 0xB;
177
178     /**
179      * medium dash-dot-dot border
180      */

181
182     public final static short BORDER_MEDIUM_DASH_DOT_DOT = 0xC;
183
184     /**
185      * slanted dash-dot border
186      */

187
188     public final static short BORDER_SLANTED_DASH_DOT = 0xD;
189
190     /** No background */
191     public final static short NO_FILL = 0 ;
192     /** Solidly filled */
193     public final static short SOLID_FOREGROUND = 1 ;
194     /** Small fine dots */
195     public final static short FINE_DOTS = 2 ;
196     /** Wide dots */
197     public final static short ALT_BARS = 3 ;
198     /** Sparse dots */
199     public final static short SPARSE_DOTS = 4 ;
200     /** Thick horizontal bands */
201     public final static short THICK_HORZ_BANDS = 5 ;
202     /** Thick vertical bands */
203     public final static short THICK_VERT_BANDS = 6 ;
204     /** Thick backward facing diagonals */
205     public final static short THICK_BACKWARD_DIAG = 7 ;
206     /** Thick forward facing diagonals */
207     public final static short THICK_FORWARD_DIAG = 8 ;
208     /** Large spots */
209     public final static short BIG_SPOTS = 9 ;
210     /** Brick-like layout */
211     public final static short BRICKS = 10 ;
212     /** Thin horizontal bands */
213     public final static short THIN_HORZ_BANDS = 11 ;
214     /** Thin vertical bands */
215     public final static short THIN_VERT_BANDS = 12 ;
216     /** Thin backward diagonal */
217     public final static short THIN_BACKWARD_DIAG = 13 ;
218     /** Thin forward diagonal */
219     public final static short THIN_FORWARD_DIAG = 14 ;
220     /** Squares */
221     public final static short SQUARES = 15 ;
222     /** Diamonds */
223     public final static short DIAMONDS = 16 ;
224
225
226     /** Creates new HSSFCellStyle why would you want to do this?? */
227
228     protected HSSFCellStyle(short index, ExtendedFormatRecord rec)
229     {
230         this.index = index;
231         format = rec;
232     }
233
234     /**
235      * get the index within the HSSFWorkbook (sequence within the collection of ExtnededFormat objects)
236      * @return unique index number of the underlying record this style represents (probably you don't care
237      * unless you're comparing which one is which)
238      */

239
240     public short getIndex()
241     {
242         return index;
243     }
244
245     /**
246      * set the data format (must be a valid format)
247      * @see org.apache.poi.hssf.usermodel.HSSFDataFormat
248      */

249
250     public void setDataFormat(short fmt)
251     {
252         format.setFormatIndex(fmt);
253     }
254
255     /**
256      * get the index of the format
257      * @see org.apache.poi.hssf.usermodel.HSSFDataFormat
258      */

259
260     public short getDataFormat()
261     {
262         return format.getFormatIndex();
263     }
264
265     /**
266      * set the font for this style
267      * @param font a font object created or retreived from the HSSFWorkbook object
268      * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#createFont()
269      * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#getFontAt(short)
270      */

271
272     public void setFont(HSSFFont font)
273     {
274         format.setIndentNotParentFont(true);
275         fontindex = font.getIndex();
276         format.setFontIndex(fontindex);
277     }
278
279     public short getFontIndex()
280     {
281         return format.getFontIndex();
282     }
283
284     /**
285      * set the cell's using this style to be hidden
286      * @param hidden - whether the cell using this style should be hidden
287      */

288
289     public void setHidden(boolean hidden)
290     {
291         format.setIndentNotParentCellOptions(true);
292         format.setHidden(hidden);
293     }
294
295     /**
296      * get whether the cell's using this style are to be hidden
297      * @return hidden - whether the cell using this style should be hidden
298      */

299
300     public boolean getHidden()
301     {
302         return format.isHidden();
303     }
304
305     /**
306      * set the cell's using this style to be locked
307      * @param locked - whether the cell using this style should be locked
308      */

309
310     public void setLocked(boolean locked)
311     {
312         format.setIndentNotParentCellOptions(true);
313         format.setLocked(locked);
314     }
315
316     /**
317      * get whether the cell's using this style are to be locked
318      * @return hidden - whether the cell using this style should be locked
319      */

320
321     public boolean getLocked()
322     {
323         return format.isLocked();
324     }
325
326     /**
327      * set the type of horizontal alignment for the cell
328      * @param align - the type of alignment
329      * @see #ALIGN_GENERAL
330      * @see #ALIGN_LEFT
331      * @see #ALIGN_CENTER
332      * @see #ALIGN_RIGHT
333      * @see #ALIGN_FILL
334      * @see #ALIGN_JUSTIFY
335      * @see #ALIGN_CENTER_SELECTION
336      */

337
338     public void setAlignment(short align)
339     {
340         format.setIndentNotParentAlignment(true);
341         format.setAlignment(align);
342     }
343
344     /**
345      * get the type of horizontal alignment for the cell
346      * @return align - the type of alignment
347      * @see #ALIGN_GENERAL
348      * @see #ALIGN_LEFT
349      * @see #ALIGN_CENTER
350      * @see #ALIGN_RIGHT
351      * @see #ALIGN_FILL
352      * @see #ALIGN_JUSTIFY
353      * @see #ALIGN_CENTER_SELECTION
354      */

355
356     public short getAlignment()
357     {
358         return format.getAlignment();
359     }
360
361     /**
362      * get whether this cell is to be part of a merged block of cells
363      *
364      * @returns merged or not
365      */

366
367 // public boolean getMergeCells()
368
// {
369
// return format.getMergeCells();
370
// }
371

372     /**
373      * set whether this cell is to be part of a merged block of cells
374      *
375      * @param merge merged or not
376      */

377
378 // public void setMergeCells(boolean merge)
379
// {
380
// format.setMergeCells(merge);
381
// }
382

383     /**
384      * set whether the text should be wrapped
385      * @param wrapped wrap text or not
386      */

387
388     public void setWrapText(boolean wrapped)
389     {
390         format.setIndentNotParentAlignment(true);
391         format.setWrapText(wrapped);
392     }
393
394     /**
395      * get whether the text should be wrapped
396      * @return wrap text or not
397      */

398
399     public boolean getWrapText()
400     {
401         return format.getWrapText();
402     }
403
404     /**
405      * set the type of vertical alignment for the cell
406      * @param align the type of alignment
407      * @see #VERTICAL_TOP
408      * @see #VERTICAL_CENTER
409      * @see #VERTICAL_BOTTOM
410      * @see #VERTICAL_JUSTIFY
411      */

412
413     public void setVerticalAlignment(short align)
414     {
415         format.setVerticalAlignment(align);
416     }
417
418     /**
419      * get the type of vertical alignment for the cell
420      * @return align the type of alignment
421      * @see #VERTICAL_TOP
422      * @see #VERTICAL_CENTER
423      * @see #VERTICAL_BOTTOM
424      * @see #VERTICAL_JUSTIFY
425      */

426
427     public short getVerticalAlignment()
428     {
429         return format.getVerticalAlignment();
430     }
431
432     /**
433      * set the degree of rotation for the text in the cell
434      * @param rotation degrees (between -90 and 90 degrees)
435      */

436
437     public void setRotation(short rotation)
438     {
439       if ((rotation < 0)&&(rotation >= -90)) {
440         //Take care of the funny 4th quadrant issue
441
//The 4th quadrant (-1 to -90) is stored as (91 to 180)
442
rotation = (short)(90 - rotation);
443       }
444       else if ((rotation < -90) ||(rotation > 90))
445         //Do not allow an incorrect rotation to be set
446
throw new IllegalArgumentException JavaDoc("The rotation must be between -90 and 90 degrees");
447         format.setRotation(rotation);
448     }
449
450     /**
451      * get the degree of rotation for the text in the cell
452      * @return rotation degrees (between -90 and 90 degrees)
453      */

454
455     public short getRotation()
456     {
457       short rotation = format.getRotation();
458       if (rotation > 90)
459         //This is actually the 4th quadrant
460
rotation = (short)(90-rotation);
461       return rotation;
462     }
463
464     /**
465      * set the number of spaces to indent the text in the cell
466      * @param indent - number of spaces
467      */

468
469     public void setIndention(short indent)
470     {
471         format.setIndent(indent);
472     }
473
474     /**
475      * get the number of spaces to indent the text in the cell
476      * @return indent - number of spaces
477      */

478
479     public short getIndention()
480     {
481         return format.getIndent();
482     }
483
484     /**
485      * set the type of border to use for the left border of the cell
486      * @param border type
487      * @see #BORDER_NONE
488      * @see #BORDER_THIN
489      * @see #BORDER_MEDIUM
490      * @see #BORDER_DASHED
491      * @see #BORDER_DOTTED
492      * @see #BORDER_THICK
493      * @see #BORDER_DOUBLE
494      * @see #BORDER_HAIR
495      * @see #BORDER_MEDIUM_DASHED
496      * @see #BORDER_DASH_DOT
497      * @see #BORDER_MEDIUM_DASH_DOT
498      * @see #BORDER_DASH_DOT_DOT
499      * @see #BORDER_MEDIUM_DASH_DOT_DOT
500      * @see #BORDER_SLANTED_DASH_DOT
501      */

502
503     public void setBorderLeft(short border)
504     {
505         format.setIndentNotParentBorder(true);
506         format.setBorderLeft(border);
507     }
508
509     /**
510      * get the type of border to use for the left border of the cell
511      * @return border type
512      * @see #BORDER_NONE
513      * @see #BORDER_THIN
514      * @see #BORDER_MEDIUM
515      * @see #BORDER_DASHED
516      * @see #BORDER_DOTTED
517      * @see #BORDER_THICK
518      * @see #BORDER_DOUBLE
519      * @see #BORDER_HAIR
520      * @see #BORDER_MEDIUM_DASHED
521      * @see #BORDER_DASH_DOT
522      * @see #BORDER_MEDIUM_DASH_DOT
523      * @see #BORDER_DASH_DOT_DOT
524      * @see #BORDER_MEDIUM_DASH_DOT_DOT
525      * @see #BORDER_SLANTED_DASH_DOT
526      */

527
528     public short getBorderLeft()
529     {
530         return format.getBorderLeft();
531     }
532
533     /**
534      * set the type of border to use for the right border of the cell
535      * @param border type
536      * @see #BORDER_NONE
537      * @see #BORDER_THIN
538      * @see #BORDER_MEDIUM
539      * @see #BORDER_DASHED
540      * @see #BORDER_DOTTED
541      * @see #BORDER_THICK
542      * @see #BORDER_DOUBLE
543      * @see #BORDER_HAIR
544      * @see #BORDER_MEDIUM_DASHED
545      * @see #BORDER_DASH_DOT
546      * @see #BORDER_MEDIUM_DASH_DOT
547      * @see #BORDER_DASH_DOT_DOT
548      * @see #BORDER_MEDIUM_DASH_DOT_DOT
549      * @see #BORDER_SLANTED_DASH_DOT
550      */

551
552     public void setBorderRight(short border)
553     {
554         format.setIndentNotParentBorder(true);
555         format.setBorderRight(border);
556     }
557
558     /**
559      * get the type of border to use for the right border of the cell
560      * @return border type
561      * @see #BORDER_NONE
562      * @see #BORDER_THIN
563      * @see #BORDER_MEDIUM
564      * @see #BORDER_DASHED
565      * @see #BORDER_DOTTED
566      * @see #BORDER_THICK
567      * @see #BORDER_DOUBLE
568      * @see #BORDER_HAIR
569      * @see #BORDER_MEDIUM_DASHED
570      * @see #BORDER_DASH_DOT
571      * @see #BORDER_MEDIUM_DASH_DOT
572      * @see #BORDER_DASH_DOT_DOT
573      * @see #BORDER_MEDIUM_DASH_DOT_DOT
574      * @see #BORDER_SLANTED_DASH_DOT
575      */

576
577     public short getBorderRight()
578     {
579         return format.getBorderRight();
580     }
581
582     /**
583      * set the type of border to use for the top border of the cell
584      * @param border type
585      * @see #BORDER_NONE
586      * @see #BORDER_THIN
587      * @see #BORDER_MEDIUM
588      * @see #BORDER_DASHED
589      * @see #BORDER_DOTTED
590      * @see #BORDER_THICK
591      * @see #BORDER_DOUBLE
592      * @see #BORDER_HAIR
593      * @see #BORDER_MEDIUM_DASHED
594      * @see #BORDER_DASH_DOT
595      * @see #BORDER_MEDIUM_DASH_DOT
596      * @see #BORDER_DASH_DOT_DOT
597      * @see #BORDER_MEDIUM_DASH_DOT_DOT
598      * @see #BORDER_SLANTED_DASH_DOT
599      */

600
601     public void setBorderTop(short border)
602     {
603         format.setIndentNotParentBorder(true);
604         format.setBorderTop(border);
605     }
606
607     /**
608      * get the type of border to use for the top border of the cell
609      * @return border type
610      * @see #BORDER_NONE
611      * @see #BORDER_THIN
612      * @see #BORDER_MEDIUM
613      * @see #BORDER_DASHED
614      * @see #BORDER_DOTTED
615      * @see #BORDER_THICK
616      * @see #BORDER_DOUBLE
617      * @see #BORDER_HAIR
618      * @see #BORDER_MEDIUM_DASHED
619      * @see #BORDER_DASH_DOT
620      * @see #BORDER_MEDIUM_DASH_DOT
621      * @see #BORDER_DASH_DOT_DOT
622      * @see #BORDER_MEDIUM_DASH_DOT_DOT
623      * @see #BORDER_SLANTED_DASH_DOT
624      */

625
626     public short getBorderTop()
627     {
628         return format.getBorderTop();
629     }
630
631     /**
632      * set the type of border to use for the bottom border of the cell
633      * @param border type
634      * @see #BORDER_NONE
635      * @see #BORDER_THIN
636      * @see #BORDER_MEDIUM
637      * @see #BORDER_DASHED
638      * @see #BORDER_DOTTED
639      * @see #BORDER_THICK
640      * @see #BORDER_DOUBLE
641      * @see #BORDER_HAIR
642      * @see #BORDER_MEDIUM_DASHED
643      * @see #BORDER_DASH_DOT
644      * @see #BORDER_MEDIUM_DASH_DOT
645      * @see #BORDER_DASH_DOT_DOT
646      * @see #BORDER_MEDIUM_DASH_DOT_DOT
647      * @see #BORDER_SLANTED_DASH_DOT
648      */

649
650     public void setBorderBottom(short border)
651     {
652         format.setIndentNotParentBorder(true);
653         format.setBorderBottom(border);
654     }
655
656     /**
657      * get the type of border to use for the bottom border of the cell
658      * @return border type
659      * @see #BORDER_NONE
660      * @see #BORDER_THIN
661      * @see #BORDER_MEDIUM
662      * @see #BORDER_DASHED
663      * @see #BORDER_DOTTED
664      * @see #BORDER_THICK
665      * @see #BORDER_DOUBLE
666      * @see #BORDER_HAIR
667      * @see #BORDER_MEDIUM_DASHED
668      * @see #BORDER_DASH_DOT
669      * @see #BORDER_MEDIUM_DASH_DOT
670      * @see #BORDER_DASH_DOT_DOT
671      * @see #BORDER_MEDIUM_DASH_DOT_DOT
672      * @see #BORDER_SLANTED_DASH_DOT
673      */

674
675     public short getBorderBottom()
676     {
677         return format.getBorderBottom();
678     }
679
680     /**
681      * set the color to use for the left border
682      * @param color
683      */

684
685     public void setLeftBorderColor(short color)
686     {
687         format.setLeftBorderPaletteIdx(color);
688     }
689
690     /**
691      * get the color to use for the left border
692      * @return color
693      */

694
695     public short getLeftBorderColor()
696     {
697         return format.getLeftBorderPaletteIdx();
698     }
699
700     /**
701      * set the color to use for the right border
702      * @param color
703      */

704
705     public void setRightBorderColor(short color)
706     {
707         format.setRightBorderPaletteIdx(color);
708     }
709
710     /**
711      * get the color to use for the left border
712      * @return color
713      */

714
715     public short getRightBorderColor()
716     {
717         return format.getRightBorderPaletteIdx();
718     }
719
720     /**
721      * set the color to use for the top border
722      * @param color
723      */

724
725     public void setTopBorderColor(short color)
726     {
727         format.setTopBorderPaletteIdx(color);
728     }
729
730     /**
731      * get the color to use for the top border
732      * @return color
733      */

734
735     public short getTopBorderColor()
736     {
737         return format.getTopBorderPaletteIdx();
738     }
739
740     /**
741      * set the color to use for the bottom border
742      * @param color
743      */

744
745     public void setBottomBorderColor(short color)
746     {
747         format.setBottomBorderPaletteIdx(color);
748     }
749
750     /**
751      * get the color to use for the left border
752      * @return color
753      */

754
755     public short getBottomBorderColor()
756     {
757         return format.getBottomBorderPaletteIdx();
758     }
759
760     /**
761      * setting to one fills the cell with the foreground color... No idea about
762      * other values
763      *
764      * @see #NO_FILL
765      * @see #SOLID_FOREGROUND
766      * @see #FINE_DOTS
767      * @see #ALT_BARS
768      * @see #SPARSE_DOTS
769      * @see #THICK_HORZ_BANDS
770      * @see #THICK_VERT_BANDS
771      * @see #THICK_BACKWARD_DIAG
772      * @see #THICK_FORWARD_DIAG
773      * @see #BIG_SPOTS
774      * @see #BRICKS
775      * @see #THIN_HORZ_BANDS
776      * @see #THIN_VERT_BANDS
777      * @see #THIN_BACKWARD_DIAG
778      * @see #THIN_FORWARD_DIAG
779      * @see #SQUARES
780      * @see #DIAMONDS
781      *
782      * @param fp fill pattern (set to 1 to fill w/foreground color)
783      */

784     public void setFillPattern(short fp)
785     {
786         format.setAdtlFillPattern(fp);
787     }
788
789     /**
790      * get the fill pattern (??) - set to 1 to fill with foreground color
791      * @return fill pattern
792      */

793
794     public short getFillPattern()
795     {
796         return format.getAdtlFillPattern();
797     }
798
799     /**
800      * set the background fill color.
801      * <p>
802      * For example:
803      * <pre>
804      * cs.setFillPattern(HSSFCellStyle.FINE_DOTS );
805      * cs.setFillBackgroundColor(HSSFCellStyle.RED);
806      * </pre>
807      * or, for the special case of SOLID_FILL:
808      * <pre>
809      * cs.setFillPattern(HSSFCellStyle.SOLID_FILL );
810      * cs.setFillForgroundColor(HSSFSeCellStyle.RED);
811      * </pre>
812      * It is necessary to set the fill style in order
813      * for the color to be shown in the cell.
814      *
815      * @param bg color
816      */

817
818     public void setFillBackgroundColor(short bg)
819     {
820         format.setFillBackground(bg);
821     }
822
823     /**
824      * get the background fill color
825      * @return fill color
826      */

827
828     public short getFillBackgroundColor()
829     {
830         return format.getFillBackground();
831     }
832
833     /**
834      * set the foreground fill color
835      * @param bg color
836      */

837
838     public void setFillForegroundColor(short bg)
839     {
840         format.setFillForeground(bg);
841     }
842
843     /**
844      * get the foreground fill color
845      * @return fill color
846      */

847
848     public short getFillForegroundColor()
849     {
850         return format.getFillForeground();
851     }
852
853 }
854
Popular Tags