KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hssf > contrib > view > SVBorder


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

17         
18 package org.apache.poi.hssf.contrib.view;
19
20 import java.awt.*;
21
22 import javax.swing.border.AbstractBorder JavaDoc;
23
24 import org.apache.poi.hssf.usermodel.HSSFCellStyle;
25
26 /**
27  * This is an attempt to implement Excel style borders for the SheetViewer.
28  * Mostly just overrides stuff so the javadoc won't appear here but will
29  * appear in the generated stuff.
30  *
31  * @author Andrew C. Oliver (acoliver at apache dot org)
32  * @author Jason Height
33  */

34 public class SVBorder extends AbstractBorder JavaDoc {
35   private Color northColor = null;
36   private Color eastColor = null;
37   private Color southColor = null;
38   private Color westColor = null;
39   private int northBorderType = HSSFCellStyle.BORDER_NONE;
40   private int eastBorderType =HSSFCellStyle.BORDER_NONE;
41   private int southBorderType = HSSFCellStyle.BORDER_NONE;
42   private int westBorderType = HSSFCellStyle.BORDER_NONE;
43   private boolean northBorder=false;
44   private boolean eastBorder=false;
45   private boolean southBorder=false;
46   private boolean westBorder=false;
47   private boolean selected = false;
48
49    public void setBorder(Color northColor, Color eastColor,
50                          Color southColor, Color westColor,
51                          int northBorderType, int eastBorderType,
52                          int southBorderType, int westBorderType,
53                          boolean selected) {
54      this.eastColor = eastColor;
55      this.southColor = southColor;
56      this.westColor = westColor;
57      this.northBorderType = northBorderType;
58      this.eastBorderType = eastBorderType;
59      this.southBorderType = southBorderType;
60      this.westBorderType = westBorderType;
61      this.northBorder=northBorderType != HSSFCellStyle.BORDER_NONE;
62      this.eastBorder=eastBorderType != HSSFCellStyle.BORDER_NONE;
63      this.southBorder=southBorderType != HSSFCellStyle.BORDER_NONE;
64      this.westBorder=westBorderType != HSSFCellStyle.BORDER_NONE;
65      this.selected = selected;
66    }
67
68    public void paintBorder(Component c, Graphics g, int x, int y, int width,
69                            int height) {
70       Color oldColor = g.getColor();
71
72
73      paintSelectedBorder(g, x, y, width, height);
74      paintNormalBorders(g, x, y, width, height);
75      paintDottedBorders(g, x, y, width, height);
76      paintDashedBorders(g, x, y, width, height);
77      paintDoubleBorders(g, x, y, width, height);
78      paintDashDotDotBorders(g, x, y, width, height);
79
80
81      g.setColor(oldColor);
82    }
83
84    /**
85     * Called by paintBorder to paint the border of a selected cell.
86     * The paramaters are the Graphics object, location and dimensions of the
87     * cell.
88     */

89    private void paintSelectedBorder(Graphics g, int x, int y, int width,
90                                   int height) {
91      if (selected) {
92        //Need to setup thickness of 2
93
g.setColor(Color.black);
94        //paint the border
95
g.drawRect(x,y,width-1,height-1);
96
97        //paint the filled rectangle at the bottom left hand position
98
g.fillRect(x+width-5, y+height-5, 5, 5);
99      }
100    }
101
102
103    /**
104     * Called by paintBorder to paint the various versions of normal line
105     * borders for a cell.
106     */

107    private void paintNormalBorders(Graphics g, int x, int y, int width,
108                                   int height) {
109
110       if (northBorder &&
111              ((northBorderType == HSSFCellStyle.BORDER_THIN) ||
112               (northBorderType == HSSFCellStyle.BORDER_MEDIUM) ||
113               (northBorderType == HSSFCellStyle.BORDER_THICK)
114              )
115          ) {
116
117         int thickness = getThickness(northBorderType);
118
119         g.setColor(northColor);
120
121         for (int k=0; k < thickness; k++) {
122            g.drawLine(x,y+k,width,y+k);
123         }
124       }
125
126       if (eastBorder &&
127              ((eastBorderType == HSSFCellStyle.BORDER_THIN) ||
128               (eastBorderType == HSSFCellStyle.BORDER_MEDIUM) ||
129               (eastBorderType == HSSFCellStyle.BORDER_THICK)
130              )
131          ) {
132
133         int thickness = getThickness(eastBorderType);
134
135         g.setColor(eastColor);
136
137         for (int k=0; k < thickness; k++) {
138            g.drawLine(width-k,y,width-k,height);
139         }
140       }
141
142       if (southBorder &&
143               ((southBorderType == HSSFCellStyle.BORDER_THIN) ||
144                (southBorderType == HSSFCellStyle.BORDER_MEDIUM) ||
145                (southBorderType == HSSFCellStyle.BORDER_THICK)
146               )
147          ) {
148
149         int thickness = getThickness(southBorderType);
150
151         g.setColor(southColor);
152         for (int k=0; k < thickness; k++) {
153            g.drawLine(x,height - k,width,height - k);
154         }
155       }
156
157       if (westBorder &&
158              ((westBorderType == HSSFCellStyle.BORDER_THIN) ||
159               (westBorderType == HSSFCellStyle.BORDER_MEDIUM) ||
160               (westBorderType == HSSFCellStyle.BORDER_THICK)
161              )
162          ) {
163
164         int thickness = getThickness(westBorderType);
165
166         g.setColor(westColor);
167
168         for (int k=0; k < thickness; k++) {
169            g.drawLine(x+k,y,x+k,height);
170         }
171       }
172    }
173
174    /**
175     * Called by paintBorder to paint the dotted line
176     * borders for a cell.
177     */

178    private void paintDottedBorders(Graphics g, int x, int y, int width,
179                                   int height) {
180       if (northBorder &&
181              northBorderType == HSSFCellStyle.BORDER_DOTTED) {
182         int thickness = getThickness(northBorderType);
183
184         g.setColor(northColor);
185
186         for (int k=0; k < thickness; k++) {
187            for (int xc = x; xc < width; xc=xc+2) {
188              g.drawLine(xc,y+k,xc,y+k);
189            }
190         }
191       }
192
193       if (eastBorder &&
194               eastBorderType == HSSFCellStyle.BORDER_DOTTED
195          ) {
196
197         int thickness = getThickness(eastBorderType);
198         thickness++; //need for dotted borders to show up east
199

200         g.setColor(eastColor);
201
202         for (int k=0; k < thickness; k++) {
203            for (int yc=y;yc < height; yc=yc+2) {
204                 g.drawLine(width-k,yc,width-k,yc);
205            }
206         }
207       }
208
209       if (southBorder &&
210               southBorderType == HSSFCellStyle.BORDER_DOTTED
211          ) {
212
213         int thickness = getThickness(southBorderType);
214         thickness++;
215         g.setColor(southColor);
216         for (int k=0; k < thickness; k++) {
217            for (int xc = x; xc < width; xc=xc+2) {
218              g.drawLine(xc,height-k,xc,height-k);
219            }
220         }
221       }
222
223       if (westBorder &&
224             westBorderType == HSSFCellStyle.BORDER_DOTTED
225          ) {
226
227         int thickness = getThickness(westBorderType);
228 // thickness++;
229

230         g.setColor(westColor);
231
232         for (int k=0; k < thickness; k++) {
233            for (int yc=y;yc < height; yc=yc+2) {
234                 g.drawLine(x+k,yc,x+k,yc);
235            }
236         }
237       }
238    }
239
240    /**
241     * Called by paintBorder to paint the various versions of dotted line
242     * borders for a cell.
243     */

244    private void paintDashedBorders(Graphics g, int x, int y, int width,
245                                   int height) {
246       if (northBorder &&
247              ((northBorderType == HSSFCellStyle.BORDER_DASHED) ||
248               (northBorderType == HSSFCellStyle.BORDER_HAIR))
249          ) {
250         int thickness = getThickness(northBorderType);
251
252         int dashlength = 1;
253
254         if (northBorderType == HSSFCellStyle.BORDER_DASHED)
255            dashlength = 2;
256
257         g.setColor(northColor);
258
259         for (int k=0; k < thickness; k++) {
260            for (int xc = x; xc < width; xc=xc+5) {
261              g.drawLine(xc,y+k,xc+dashlength,y+k);
262            }
263         }
264       }
265
266       if (eastBorder &&
267               ((eastBorderType == HSSFCellStyle.BORDER_DASHED) ||
268                (eastBorderType == HSSFCellStyle.BORDER_HAIR))
269          ) {
270
271         int thickness = getThickness(eastBorderType);
272         thickness++; //need for dotted borders to show up east
273

274
275         int dashlength = 1;
276
277         if (eastBorderType == HSSFCellStyle.BORDER_DASHED)
278            dashlength = 2;
279
280         g.setColor(eastColor);
281
282         for (int k=0; k < thickness; k++) {
283            for (int yc=y;yc < height; yc=yc+5) {
284                 g.drawLine(width-k,yc,width-k,yc+dashlength);
285            }
286         }
287       }
288
289       if (southBorder &&
290               ((southBorderType == HSSFCellStyle.BORDER_DASHED) ||
291                (southBorderType == HSSFCellStyle.BORDER_HAIR))
292          ) {
293
294         int thickness = getThickness(southBorderType);
295         thickness++;
296
297         int dashlength = 1;
298
299         if (southBorderType == HSSFCellStyle.BORDER_DASHED)
300            dashlength = 2;
301
302         g.setColor(southColor);
303         for (int k=0; k < thickness; k++) {
304            for (int xc = x; xc < width; xc=xc+5) {
305              g.drawLine(xc,height-k,xc+dashlength,height-k);
306            }
307         }
308       }
309
310       if (westBorder &&
311             ((westBorderType == HSSFCellStyle.BORDER_DASHED) ||
312              (westBorderType == HSSFCellStyle.BORDER_HAIR))
313          ) {
314
315         int thickness = getThickness(westBorderType);
316 // thickness++;
317

318         int dashlength = 1;
319
320         if (westBorderType == HSSFCellStyle.BORDER_DASHED)
321            dashlength = 2;
322
323         g.setColor(westColor);
324
325         for (int k=0; k < thickness; k++) {
326            for (int yc=y;yc < height; yc=yc+5) {
327                 g.drawLine(x+k,yc,x+k,yc+dashlength);
328            }
329         }
330       }
331    }
332
333    /**
334     * Called by paintBorder to paint the double line
335     * borders for a cell.
336     */

337    private void paintDoubleBorders(Graphics g, int x, int y, int width,
338                                   int height) {
339       if (northBorder &&
340              northBorderType == HSSFCellStyle.BORDER_DOUBLE) {
341
342         g.setColor(northColor);
343
344         int leftx=x;
345         int rightx=width;
346
347                 // if there are borders on the west or east then
348
// the second line shouldn't cross them
349
if (westBorder)
350            leftx = x+3;
351
352         if (eastBorder)
353            rightx = width-3;
354
355            g.drawLine(x,y,width,y);
356            g.drawLine(leftx,y+2,rightx,y+2);
357       }
358
359       if (eastBorder &&
360               eastBorderType == HSSFCellStyle.BORDER_DOUBLE
361          ) {
362
363         int thickness = getThickness(eastBorderType);
364         thickness++; //need for dotted borders to show up east
365

366         g.setColor(eastColor);
367
368         int topy=y;
369         int bottomy=height;
370
371         if (northBorder)
372           topy=y+3;
373
374         if (southBorder)
375             bottomy=height-3;
376
377         g.drawLine(width-1,y,width-1,height);
378         g.drawLine(width-3,topy,width-3,bottomy);
379       }
380
381       if (southBorder &&
382               southBorderType == HSSFCellStyle.BORDER_DOUBLE
383          ) {
384
385         g.setColor(southColor);
386
387         int leftx=y;
388         int rightx=width;
389
390         if (westBorder)
391            leftx=x+3;
392
393         if (eastBorder)
394            rightx=width-3;
395
396
397         g.drawLine(x,height - 1,width,height - 1);
398         g.drawLine(leftx,height - 3,rightx,height - 3);
399       }
400
401       if (westBorder &&
402             westBorderType == HSSFCellStyle.BORDER_DOUBLE
403          ) {
404
405         int thickness = getThickness(westBorderType);
406 // thickness++;
407

408         g.setColor(westColor);
409
410         int topy=y;
411         int bottomy=height-3;
412
413         if (northBorder)
414            topy=y+2;
415
416         if (southBorder)
417            bottomy=height-3;
418
419         g.drawLine(x,y,x,height);
420         g.drawLine(x+2,topy,x+2,bottomy);
421       }
422    }
423
424    /**
425     * Called by paintBorder to paint the various versions of dash dot dot line
426     * borders for a cell.
427     */

428    private void paintDashDotDotBorders(Graphics g, int x, int y, int width,
429                                   int height) {
430       if (northBorder &&
431              ((northBorderType == HSSFCellStyle.BORDER_DASH_DOT_DOT) ||
432               (northBorderType == HSSFCellStyle.BORDER_MEDIUM_DASH_DOT_DOT))
433          ) {
434         int thickness = getThickness(northBorderType);
435
436         g.setColor(northColor);
437         for (int l=x; l < width;) {
438           l=l+drawDashDotDot(g, l, y, thickness, true, true);
439         }
440
441       }
442
443       if (eastBorder &&
444               ((eastBorderType == HSSFCellStyle.BORDER_DASH_DOT_DOT) ||
445                (eastBorderType == HSSFCellStyle.BORDER_MEDIUM_DASH_DOT_DOT))
446          ) {
447
448         int thickness = getThickness(eastBorderType);
449
450         g.setColor(eastColor);
451
452         for (int l=y;l < height;) {
453           //System.err.println("drawing east");
454
l=l+drawDashDotDot(g,width-1,l,thickness,false,false);
455         }
456       }
457
458       if (southBorder &&
459               ((southBorderType == HSSFCellStyle.BORDER_DASH_DOT_DOT) ||
460                (southBorderType == HSSFCellStyle.BORDER_MEDIUM_DASH_DOT_DOT))
461          ) {
462
463         int thickness = getThickness(southBorderType);
464
465         g.setColor(southColor);
466
467         for (int l=x; l < width;) {
468           //System.err.println("drawing south");
469
l=l+drawDashDotDot(g, l, height-1, thickness, true, false);
470         }
471       }
472
473       if (westBorder &&
474             ((westBorderType == HSSFCellStyle.BORDER_DASH_DOT_DOT) ||
475              (westBorderType == HSSFCellStyle.BORDER_MEDIUM_DASH_DOT_DOT))
476          ) {
477
478         int thickness = getThickness(westBorderType);
479
480         g.setColor(westColor);
481
482         for (int l=y;l < height;) {
483           //System.err.println("drawing west");
484
l=l+drawDashDotDot(g,x,l,thickness,false,true);
485         }
486
487       }
488    }
489
490    /**
491     * Draws one dash dot dot horizontally or vertically with thickness drawn
492     * incrementally to either the right or left.
493     *
494     * @param g graphics object for drawing with
495     * @param x the x origin of the line
496     * @param y the y origin of the line
497     * @param thickness the thickness of the line
498     * @param horizontal or vertical (true for horizontal)
499     * @param right/bottom or left/top thickness (true for right or top),
500     * if true then the x or y origin will be incremented to provide
501     * thickness, if false, they'll be decremented. For vertical
502     * borders, x is incremented or decremented, for horizontal its y.
503     * Just set to true for north and west, and false for east and
504     * south.
505     * @returns length - returns the length of the line.
506     */

507    private int drawDashDotDot(Graphics g,int x, int y, int thickness,
508                               boolean horizontal,
509                               boolean rightBottom) {
510
511       for (int t=0; t < thickness; t++) {
512          if (!rightBottom) {
513             t = 0 - t; //add negative thickness so we go the other way
514
//then we'll decrement instead of increment.
515
}
516          if (horizontal) {
517             g.drawLine(x,y+t,x+5,y+t);
518             g.drawLine(x+8,y+t,x+10,y+t);
519             g.drawLine(x+13,y+t,x+15,y+t);
520          } else {
521             g.drawLine(x+t,y,x+t,y+5);
522             g.drawLine(x+t,y+8,x+t,y+10);
523             g.drawLine(x+t,y+13,x+t,y+15);
524          }
525       }
526       return 18;
527    }
528
529    /**
530     * @returns the line thickness for a border based on border type
531     */

532    private int getThickness(int thickness) {
533        int retval=1;
534        switch (thickness) {
535            case HSSFCellStyle.BORDER_THIN:
536              retval=2;
537              break;
538            case HSSFCellStyle.BORDER_MEDIUM:
539              retval=3;
540              break;
541            case HSSFCellStyle.BORDER_THICK:
542              retval=4;
543              break;
544            case HSSFCellStyle.BORDER_DASHED:
545              retval=1;
546              break;
547            case HSSFCellStyle.BORDER_DASH_DOT_DOT:
548              retval=1;
549              break;
550            case HSSFCellStyle.BORDER_MEDIUM_DASH_DOT_DOT:
551              retval=3;
552              break;
553            case HSSFCellStyle.BORDER_HAIR:
554              retval=1;
555              break;
556            default:
557              retval=1;
558        }
559        return retval;
560    }
561
562
563 }
564
Popular Tags