1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
|
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.ss.util;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.SpreadsheetVersion;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
/**
* <p>
* A {@link PropertyTemplate} is a template that can be applied to any sheet in
* a project. It contains all the border type and color attributes needed to
* draw all the borders for a single sheet. That template can be applied to any
* sheet in any workbook.
*
* This class requires the full spreadsheet to be in memory so
* {@link SWorkbook} Spreadsheets are not supported. The same
* {@link PropertyTemplate} can, however, be applied to both
* {@link org.apache.poi.hssf.usermodel.HSSFWorkbook}, and Workbook objects
* if necessary. Portions of the border that fall outside the max range of the
* {@link HSSFWorkbook} sheet are ignored.
* </p>
*
* <p>
* This would replace {@link RegionUtil}.
* </p>
*/
public final class PropertyTemplate {
/**
* Provides various extents of the properties being added to the template
*/
public enum Extent {
/**
* No properties defined. This can be used to remove existing
* properties.
*/
NONE,
/**
* All borders, that is top, bottom, left and right, including interior
* borders for the range. Does not include diagonals which are different
* and not implemented here.
*/
ALL,
/**
* All inside borders. This is top, bottom, left, and right borders, but
* restricted to the interior borders for the range. For a range of one
* cell, this will produce no borders.
*/
INSIDE,
/**
* All outside borders. That is top, bottom, left and right borders that
* bound the range only.
*/
OUTSIDE,
/**
* This is just the top border for the range. No interior borders will
* be produced.
*/
TOP,
/**
* This is just the bottom border for the range. No interior borders
* will be produced.
*/
BOTTOM,
/**
* This is just the left border for the range, no interior borders will
* be produced.
*/
LEFT,
/**
* This is just the right border for the range, no interior borders will
* be produced.
*/
RIGHT,
/**
* This is all horizontal borders for the range, including interior and
* outside borders.
*/
HORIZONTAL,
/**
* This is just the interior horizontal borders for the range.
*/
INSIDE_HORIZONTAL,
/**
* This is just the outside horizontal borders for the range.
*/
OUTSIDE_HORIZONTAL,
/**
* This is all vertical borders for the range, including interior and
* outside borders.
*/
VERTICAL,
/**
* This is just the interior vertical borders for the range.
*/
INSIDE_VERTICAL,
/**
* This is just the outside vertical borders for the range.
*/
OUTSIDE_VERTICAL
}
/**
* This is a list of cell properties for one shot application to a range of
* cells at a later time.
*/
private Map<CellAddress, Map<String, Object>> _propertyTemplate;
/**
*
*/
public PropertyTemplate() {
_propertyTemplate = new HashMap<CellAddress, Map<String, Object>>();
}
/**
* Draws a group of cell borders for a cell range. The borders are not
* applied to the cells at this time, just the template is drawn. To apply
* the drawn borders to a sheet, use {@link #applyBorders}.
*
* @param range
* - {@link CellRangeAddress} range of cells on which borders are
* drawn.
* @param borderType
* - Type of border to draw. Use BORDER_XXX constants in
* {@link CellStyle}.
* @param extent
* - {@link PropertyTemplate.Extent} of the borders to be
* applied.
*/
public void drawBorders(CellRangeAddress range, short borderType,
Extent extent) {
switch (extent) {
case NONE:
removeBorders(range);
break;
case ALL:
drawHorizontalBorders(range, borderType, Extent.ALL);
drawVerticalBorders(range, borderType, Extent.ALL);
break;
case INSIDE:
drawHorizontalBorders(range, borderType, Extent.INSIDE);
drawVerticalBorders(range, borderType, Extent.INSIDE);
break;
case OUTSIDE:
drawOutsideBorders(range, borderType, Extent.ALL);
break;
case TOP:
drawTopBorder(range, borderType);
break;
case BOTTOM:
drawBottomBorder(range, borderType);
break;
case LEFT:
drawLeftBorder(range, borderType);
break;
case RIGHT:
drawRightBorder(range, borderType);
break;
case HORIZONTAL:
drawHorizontalBorders(range, borderType, Extent.ALL);
break;
case INSIDE_HORIZONTAL:
drawHorizontalBorders(range, borderType, Extent.INSIDE);
break;
case OUTSIDE_HORIZONTAL:
drawOutsideBorders(range, borderType, Extent.HORIZONTAL);
break;
case VERTICAL:
drawVerticalBorders(range, borderType, Extent.ALL);
break;
case INSIDE_VERTICAL:
drawVerticalBorders(range, borderType, Extent.INSIDE);
break;
case OUTSIDE_VERTICAL:
drawOutsideBorders(range, borderType, Extent.VERTICAL);
break;
}
}
/**
* Draws a group of cell borders for a cell range. The borders are not
* applied to the cells at this time, just the template is drawn. To apply
* the drawn borders to a sheet, use {@link #applyBorders}.
*
* @param range
* - {@link CellRangeAddress} range of cells on which borders are
* drawn.
* @param borderType
* - Type of border to draw. Use BORDER_XXX constants in
* {@link CellStyle}.
* @param color
* - Color index from {@link IndexedColors} used to draw the
* borders.
* @param extent
* - {@link PropertyTemplate.Extent} of the borders to be
* applied.
*/
public void drawBorders(CellRangeAddress range, short borderType,
short color, Extent extent) {
drawBorders(range, borderType, extent);
if (borderType != CellStyle.BORDER_NONE) {
drawBorderColors(range, color, extent);
}
}
/**
* <p>
* Draws the top border for a range of cells
* </p>
*
* @param range
* - {@link CellRangeAddress} range of cells on which borders are
* drawn.
* @param borderType
* - Type of border to draw. Use BORDER_XXX constants in
* {@link CellStyle}.
*/
private void drawTopBorder(CellRangeAddress range, short borderType) {
int row = range.getFirstRow();
int firstCol = range.getFirstColumn();
int lastCol = range.getLastColumn();
for (int i = firstCol; i <= lastCol; i++) {
addProperty(row, i, CellUtil.BORDER_TOP, borderType);
if (borderType == CellStyle.BORDER_NONE && row > 0) {
addProperty(row - 1, i, CellUtil.BORDER_BOTTOM, borderType);
}
}
}
/**
* <p>
* Draws the bottom border for a range of cells
* </p>
*
* @param range
* - {@link CellRangeAddress} range of cells on which borders are
* drawn.
* @param borderType
* - Type of border to draw. Use BORDER_XXX constants in
* {@link CellStyle}.
*/
private void drawBottomBorder(CellRangeAddress range, short borderType) {
int row = range.getLastRow();
int firstCol = range.getFirstColumn();
int lastCol = range.getLastColumn();
for (int i = firstCol; i <= lastCol; i++) {
addProperty(row, i, CellUtil.BORDER_BOTTOM, borderType);
if (borderType == CellStyle.BORDER_NONE
&& row < SpreadsheetVersion.EXCEL2007.getMaxRows() - 1) {
addProperty(row + 1, i, CellUtil.BORDER_TOP, borderType);
}
}
}
/**
* <p>
* Draws the left border for a range of cells
* </p>
*
* @param range
* - {@link CellRangeAddress} range of cells on which borders are
* drawn.
* @param borderType
* - Type of border to draw. Use BORDER_XXX constants in
* {@link CellStyle}.
*/
private void drawLeftBorder(CellRangeAddress range, short borderType) {
int firstRow = range.getFirstRow();
int lastRow = range.getLastRow();
int col = range.getFirstColumn();
for (int i = firstRow; i <= lastRow; i++) {
addProperty(i, col, CellUtil.BORDER_LEFT, borderType);
if (borderType == CellStyle.BORDER_NONE && col > 0) {
addProperty(i, col - 1, CellUtil.BORDER_RIGHT, borderType);
}
}
}
/**
* <p>
* Draws the right border for a range of cells
* </p>
*
* @param range
* - {@link CellRangeAddress} range of cells on which borders are
* drawn.
* @param borderType
* - Type of border to draw. Use BORDER_XXX constants in
* {@link CellStyle}.
*/
private void drawRightBorder(CellRangeAddress range, short borderType) {
int firstRow = range.getFirstRow();
int lastRow = range.getLastRow();
int col = range.getLastColumn();
for (int i = firstRow; i <= lastRow; i++) {
addProperty(i, col, CellUtil.BORDER_RIGHT, borderType);
if (borderType == CellStyle.BORDER_NONE
&& col < SpreadsheetVersion.EXCEL2007.getMaxColumns() - 1) {
addProperty(i, col + 1, CellUtil.BORDER_LEFT, borderType);
}
}
}
/**
* <p>
* Draws the outside borders for a range of cells.
* </p>
*
* @param range
* - {@link CellRangeAddress} range of cells on which borders are
* drawn.
* @param borderType
* - Type of border to draw. Use BORDER_XXX constants in
* {@link CellStyle}.
* @param extent
* - {@link PropertyTemplate.Extent} of the borders to be
* applied. Valid Values are:
* <ul>
* <li>CellBorder.Extent.ALL</li>
* <li>CellBorder.Extent.HORIZONTAL</li>
* <li>CellBorder.Extent.VERTICAL</li>
* </ul>
*/
private void drawOutsideBorders(CellRangeAddress range, short borderType,
Extent extent) {
switch (extent) {
case ALL:
case HORIZONTAL:
case VERTICAL:
if (extent == Extent.ALL || extent == Extent.HORIZONTAL) {
drawTopBorder(range, borderType);
drawBottomBorder(range, borderType);
}
if (extent == Extent.ALL || extent == Extent.VERTICAL) {
drawLeftBorder(range, borderType);
drawRightBorder(range, borderType);
}
break;
default:
throw new IllegalArgumentException(
"Unsupported PropertyTemplate.Extent, valid Extents are ALL, HORIZONTAL, and VERTICAL");
}
}
/**
* <p>
* Draws the horizontal borders for a range of cells.
* </p>
*
* @param range
* - {@link CellRangeAddress} range of cells on which borders are
* drawn.
* @param borderType
* - Type of border to draw. Use BORDER_XXX constants in
* {@link CellStyle}.
* @param extent
* - {@link PropertyTemplate.Extent} of the borders to be
* applied. Valid Values are:
* <ul>
* <li>CellBorder.Extent.ALL</li>
* <li>CellBorder.Extent.INSIDE</li>
* </ul>
*/
private void drawHorizontalBorders(CellRangeAddress range, short borderType,
Extent extent) {
switch (extent) {
case ALL:
case INSIDE:
int firstRow = range.getFirstRow();
int lastRow = range.getLastRow();
int firstCol = range.getFirstColumn();
int lastCol = range.getLastColumn();
for (int i = firstRow; i <= lastRow; i++) {
CellRangeAddress row = new CellRangeAddress(i, i, firstCol,
lastCol);
if (extent == Extent.ALL || i > firstRow) {
drawTopBorder(row, borderType);
}
if (extent == Extent.ALL || i < lastRow) {
drawBottomBorder(row, borderType);
}
}
break;
default:
throw new IllegalArgumentException(
"Unsupported PropertyTemplate.Extent, valid Extents are ALL and INSIDE");
}
}
/**
* <p>
* Draws the vertical borders for a range of cells.
* </p>
*
* @param range
* - {@link CellRangeAddress} range of cells on which borders are
* drawn.
* @param borderType
* - Type of border to draw. Use BORDER_XXX constants in
* {@link CellStyle}.
* @param extent
* - {@link PropertyTemplate.Extent} of the borders to be
* applied. Valid Values are:
* <ul>
* <li>CellBorder.Extent.ALL</li>
* <li>CellBorder.Extent.INSIDE</li>
* </ul>
*/
private void drawVerticalBorders(CellRangeAddress range, short borderType,
Extent extent) {
switch (extent) {
case ALL:
case INSIDE:
int firstRow = range.getFirstRow();
int lastRow = range.getLastRow();
int firstCol = range.getFirstColumn();
int lastCol = range.getLastColumn();
for (int i = firstCol; i <= lastCol; i++) {
CellRangeAddress row = new CellRangeAddress(firstRow, lastRow,
i, i);
if (extent == Extent.ALL || i > firstCol) {
drawLeftBorder(row, borderType);
}
if (extent == Extent.ALL || i < lastCol) {
drawRightBorder(row, borderType);
}
}
break;
default:
throw new IllegalArgumentException(
"Unsupported PropertyTemplate.Extent, valid Extents are ALL and INSIDE");
}
}
/**
* Removes all border properties from this {@link PropertyTemplate} for the
* specified range.
*
* @parm range - {@link CellRangeAddress} range of cells to remove borders.
*/
private void removeBorders(CellRangeAddress range) {
Set<String> properties = new HashSet<String>();
properties.add(CellUtil.BORDER_TOP);
properties.add(CellUtil.BORDER_BOTTOM);
properties.add(CellUtil.BORDER_LEFT);
properties.add(CellUtil.BORDER_RIGHT);
for (int row = range.getFirstRow(); row <= range.getLastRow(); row++) {
for (int col = range.getFirstColumn(); col <= range
.getLastColumn(); col++) {
removeProperties(row, col, properties);
}
}
removeBorderColors(range);
}
/**
* Applies the drawn borders to a Sheet. The borders that are applied are
* the ones that have been drawn by the {@link #drawBorders} and
* {@link #drawBorderColors} methods.
*
* @param sheet
* - {@link Sheet} on which to apply borders
*/
public void applyBorders(Sheet sheet) {
Workbook wb = sheet.getWorkbook();
for (Map.Entry<CellAddress, Map<String, Object>> entry : _propertyTemplate
.entrySet()) {
CellAddress cellAddress = entry.getKey();
if (cellAddress.getRow() < wb.getSpreadsheetVersion().getMaxRows()
&& cellAddress.getColumn() < wb.getSpreadsheetVersion()
.getMaxColumns()) {
Map<String, Object> properties = entry.getValue();
Row row = CellUtil.getRow(cellAddress.getRow(), sheet);
Cell cell = CellUtil.getCell(row, cellAddress.getColumn());
CellUtil.setCellStyleProperties(cell, properties);
}
}
}
/**
* Sets the color for a group of cell borders for a cell range. The borders
* are not applied to the cells at this time, just the template is drawn. If
* the borders do not exist, a BORDER_THIN border is used. To apply the
* drawn borders to a sheet, use {@link #applyBorders}.
*
* @param range
* - {@link CellRangeAddress} range of cells on which colors are
* set.
* @param color
* - Color index from {@link IndexedColors} used to draw the
* borders.
* @param extent
* - {@link PropertyTemplate.Extent} of the borders for which
* colors are set.
*/
public void drawBorderColors(CellRangeAddress range, short color,
Extent extent) {
switch (extent) {
case NONE:
removeBorderColors(range);
break;
case ALL:
drawHorizontalBorderColors(range, color, Extent.ALL);
drawVerticalBorderColors(range, color, Extent.ALL);
break;
case INSIDE:
drawHorizontalBorderColors(range, color, Extent.INSIDE);
drawVerticalBorderColors(range, color, Extent.INSIDE);
break;
case OUTSIDE:
drawOutsideBorderColors(range, color, Extent.ALL);
break;
case TOP:
drawTopBorderColor(range, color);
break;
case BOTTOM:
drawBottomBorderColor(range, color);
break;
case LEFT:
drawLeftBorderColor(range, color);
break;
case RIGHT:
drawRightBorderColor(range, color);
break;
case HORIZONTAL:
drawHorizontalBorderColors(range, color, Extent.ALL);
break;
case INSIDE_HORIZONTAL:
drawHorizontalBorderColors(range, color, Extent.INSIDE);
break;
case OUTSIDE_HORIZONTAL:
drawOutsideBorderColors(range, color, Extent.HORIZONTAL);
break;
case VERTICAL:
drawVerticalBorderColors(range, color, Extent.ALL);
break;
case INSIDE_VERTICAL:
drawVerticalBorderColors(range, color, Extent.INSIDE);
break;
case OUTSIDE_VERTICAL:
drawOutsideBorderColors(range, color, Extent.VERTICAL);
break;
}
}
/**
* <p>
* Sets the color of the top border for a range of cells.
* </p>
*
* @param range
* - {@link CellRangeAddress} range of cells on which colors are
* set.
* @param color
* - Color index from {@link IndexedColors} used to draw the
* borders.
*/
private void drawTopBorderColor(CellRangeAddress range, short color) {
int row = range.getFirstRow();
int firstCol = range.getFirstColumn();
int lastCol = range.getLastColumn();
for (int i = firstCol; i <= lastCol; i++) {
if (getTemplateProperty(row, i, CellUtil.BORDER_TOP) == 0) {
drawTopBorder(new CellRangeAddress(row, row, i, i),
CellStyle.BORDER_THIN);
}
addProperty(row, i, CellUtil.TOP_BORDER_COLOR, color);
}
}
/**
* <p>
* Sets the color of the bottom border for a range of cells.
* </p>
*
* @param range
* - {@link CellRangeAddress} range of cells on which colors are
* set.
* @param color
* - Color index from {@link IndexedColors} used to draw the
* borders.
*/
private void drawBottomBorderColor(CellRangeAddress range, short color) {
int row = range.getLastRow();
int firstCol = range.getFirstColumn();
int lastCol = range.getLastColumn();
for (int i = firstCol; i <= lastCol; i++) {
if (getTemplateProperty(row, i, CellUtil.BORDER_BOTTOM) == 0) {
drawBottomBorder(new CellRangeAddress(row, row, i, i),
CellStyle.BORDER_THIN);
}
addProperty(row, i, CellUtil.BOTTOM_BORDER_COLOR, color);
}
}
/**
* <p>
* Sets the color of the left border for a range of cells.
* </p>
*
* @param range
* - {@link CellRangeAddress} range of cells on which colors are
* set.
* @param color
* - Color index from {@link IndexedColors} used to draw the
* borders.
*/
private void drawLeftBorderColor(CellRangeAddress range, short color) {
int firstRow = range.getFirstRow();
int lastRow = range.getLastRow();
int col = range.getFirstColumn();
for (int i = firstRow; i <= lastRow; i++) {
if (getTemplateProperty(i, col, CellUtil.BORDER_LEFT) == 0) {
drawLeftBorder(new CellRangeAddress(i, i, col, col),
CellStyle.BORDER_THIN);
}
addProperty(i, col, CellUtil.LEFT_BORDER_COLOR, color);
}
}
/**
* <p>
* Sets the color of the right border for a range of cells. If the border is
* not drawn, it defaults to BORDER_THIN
* </p>
*
* @param range
* - {@link CellRangeAddress} range of cells on which colors are
* set.
* @param color
* - Color index from {@link IndexedColors} used to draw the
* borders.
*/
private void drawRightBorderColor(CellRangeAddress range, short color) {
int firstRow = range.getFirstRow();
int lastRow = range.getLastRow();
int col = range.getLastColumn();
for (int i = firstRow; i <= lastRow; i++) {
if (getTemplateProperty(i, col, CellUtil.BORDER_RIGHT) == 0) {
drawRightBorder(new CellRangeAddress(i, i, col, col),
CellStyle.BORDER_THIN);
}
addProperty(i, col, CellUtil.RIGHT_BORDER_COLOR, color);
}
}
/**
* <p>
* Sets the color of the outside borders for a range of cells.
* </p>
*
* @param range
* - {@link CellRangeAddress} range of cells on which colors are
* set.
* @param color
* - Color index from {@link IndexedColors} used to draw the
* borders.
* @param extent
* - {@link PropertyTemplate.Extent} of the borders for which
* colors are set. Valid Values are:
* <ul>
* <li>CellBorder.Extent.ALL</li>
* <li>CellBorder.Extent.HORIZONTAL</li>
* <li>CellBorder.Extent.VERTICAL</li>
* </ul>
*/
private void drawOutsideBorderColors(CellRangeAddress range, short color,
Extent extent) {
switch (extent) {
case ALL:
case HORIZONTAL:
case VERTICAL:
if (extent == Extent.ALL || extent == Extent.HORIZONTAL) {
drawTopBorderColor(range, color);
drawBottomBorderColor(range, color);
}
if (extent == Extent.ALL || extent == Extent.VERTICAL) {
drawLeftBorderColor(range, color);
drawRightBorderColor(range, color);
}
break;
default:
throw new IllegalArgumentException(
"Unsupported PropertyTemplate.Extent, valid Extents are ALL, HORIZONTAL, and VERTICAL");
}
}
/**
* <p>
* Sets the color of the horizontal borders for a range of cells.
* </p>
*
* @param range
* - {@link CellRangeAddress} range of cells on which colors are
* set.
* @param color
* - Color index from {@link IndexedColors} used to draw the
* borders.
* @param extent
* - {@link PropertyTemplate.Extent} of the borders for which
* colors are set. Valid Values are:
* <ul>
* <li>CellBorder.Extent.ALL</li>
* <li>CellBorder.Extent.INSIDE</li>
* </ul>
*/
private void drawHorizontalBorderColors(CellRangeAddress range, short color,
Extent extent) {
switch (extent) {
case ALL:
case INSIDE:
int firstRow = range.getFirstRow();
int lastRow = range.getLastRow();
int firstCol = range.getFirstColumn();
int lastCol = range.getLastColumn();
for (int i = firstRow; i <= lastRow; i++) {
CellRangeAddress row = new CellRangeAddress(i, i, firstCol,
lastCol);
if (extent == Extent.ALL || i > firstRow) {
drawTopBorderColor(row, color);
}
if (extent == Extent.ALL || i < lastRow) {
drawBottomBorderColor(row, color);
}
}
break;
default:
throw new IllegalArgumentException(
"Unsupported PropertyTemplate.Extent, valid Extents are ALL and INSIDE");
}
}
/**
* <p>
* Sets the color of the vertical borders for a range of cells.
* </p>
*
* @param range
* - {@link CellRangeAddress} range of cells on which colors are
* set.
* @param color
* - Color index from {@link IndexedColors} used to draw the
* borders.
* @param extent
* - {@link PropertyTemplate.Extent} of the borders for which
* colors are set. Valid Values are:
* <ul>
* <li>CellBorder.Extent.ALL</li>
* <li>CellBorder.Extent.INSIDE</li>
* </ul>
*/
private void drawVerticalBorderColors(CellRangeAddress range, short color,
Extent extent) {
switch (extent) {
case ALL:
case INSIDE:
int firstRow = range.getFirstRow();
int lastRow = range.getLastRow();
int firstCol = range.getFirstColumn();
int lastCol = range.getLastColumn();
for (int i = firstCol; i <= lastCol; i++) {
CellRangeAddress row = new CellRangeAddress(firstRow, lastRow,
i, i);
if (extent == Extent.ALL || i > firstCol) {
drawLeftBorderColor(row, color);
}
if (extent == Extent.ALL || i < lastCol) {
drawRightBorderColor(row, color);
}
}
break;
default:
throw new IllegalArgumentException(
"Unsupported PropertyTemplate.Extent, valid Extents are ALL and INSIDE");
}
}
/**
* Removes all border properties from this {@link PropertyTemplate} for the
* specified range.
*
* @parm range - {@link CellRangeAddress} range of cells to remove borders.
*/
private void removeBorderColors(CellRangeAddress range) {
Set<String> properties = new HashSet<String>();
properties.add(CellUtil.TOP_BORDER_COLOR);
properties.add(CellUtil.BOTTOM_BORDER_COLOR);
properties.add(CellUtil.LEFT_BORDER_COLOR);
properties.add(CellUtil.RIGHT_BORDER_COLOR);
for (int row = range.getFirstRow(); row <= range.getLastRow(); row++) {
for (int col = range.getFirstColumn(); col <= range
.getLastColumn(); col++) {
removeProperties(row, col, properties);
}
}
}
/**
* Adds a property to this {@link PropertyTemplate} for a given cell
*
* @param row
* @param col
* @param property
* @param value
*/
private void addProperty(int row, int col, String property, short value) {
CellAddress cell = new CellAddress(row, col);
Map<String, Object> cellProperties = _propertyTemplate.get(cell);
if (cellProperties == null) {
cellProperties = new HashMap<String, Object>();
}
cellProperties.put(property, Short.valueOf(value));
_propertyTemplate.put(cell, cellProperties);
}
/**
* Removes a set of properties from this {@link PropertyTemplate} for a
* given cell
*
* @param row
* @param col
* @param properties
*/
private void removeProperties(int row, int col, Set<String> properties) {
CellAddress cell = new CellAddress(row, col);
Map<String, Object> cellProperties = _propertyTemplate.get(cell);
if (cellProperties != null) {
cellProperties.keySet().removeAll(properties);
if (cellProperties.isEmpty()) {
_propertyTemplate.remove(cell);
} else {
_propertyTemplate.put(cell, cellProperties);
}
}
}
/**
* Retrieves the number of borders assigned to a cell
*
* @param cell
*/
public int getNumBorders(CellAddress cell) {
Map<String, Object> cellProperties = _propertyTemplate.get(cell);
if (cellProperties == null) {
return 0;
}
int count = 0;
for (String property : cellProperties.keySet()) {
if (property.equals(CellUtil.BORDER_TOP))
count += 1;
if (property.equals(CellUtil.BORDER_BOTTOM))
count += 1;
if (property.equals(CellUtil.BORDER_LEFT))
count += 1;
if (property.equals(CellUtil.BORDER_RIGHT))
count += 1;
}
return count;
}
/**
* Retrieves the number of borders assigned to a cell
*
* @param row
* @param col
*/
public int getNumBorders(int row, int col) {
return getNumBorders(new CellAddress(row, col));
}
/**
* Retrieves the number of border colors assigned to a cell
*
* @param cell
*/
public int getNumBorderColors(CellAddress cell) {
Map<String, Object> cellProperties = _propertyTemplate.get(cell);
if (cellProperties == null) {
return 0;
}
int count = 0;
for (String property : cellProperties.keySet()) {
if (property.equals(CellUtil.TOP_BORDER_COLOR))
count += 1;
if (property.equals(CellUtil.BOTTOM_BORDER_COLOR))
count += 1;
if (property.equals(CellUtil.LEFT_BORDER_COLOR))
count += 1;
if (property.equals(CellUtil.RIGHT_BORDER_COLOR))
count += 1;
}
return count;
}
/**
* Retrieves the number of border colors assigned to a cell
*
* @param row
* @param col
*/
public int getNumBorderColors(int row, int col) {
return getNumBorderColors(new CellAddress(row, col));
}
/**
* Retrieves the border style for a given cell
*
* @param cell
* @param property
*/
public short getTemplateProperty(CellAddress cell, String property) {
short value = 0;
Map<String, Object> cellProperties = _propertyTemplate.get(cell);
if (cellProperties != null) {
Object obj = cellProperties.get(property);
if (obj != null) {
value = getShort(obj);
}
}
return value;
}
/**
* Retrieves the border style for a given cell
*
* @param row
* @param col
* @param property
*/
public short getTemplateProperty(int row, int col, String property) {
return getTemplateProperty(new CellAddress(row, col), property);
}
/**
* Converts a Short object to a short value or 0 if the object is not a
* Short
*
* @param value
* @return
*/
private static short getShort(Object value) {
if (value instanceof Short) {
return ((Short) value).shortValue();
}
return 0;
}
}
|