1 2 17 18 package org.apache.poi.hssf.contrib.view; 19 20 import java.awt.*; 21 22 import javax.swing.border.AbstractBorder ; 23 24 import org.apache.poi.hssf.usermodel.HSSFCellStyle; 25 26 34 public class SVBorder extends AbstractBorder { 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 89 private void paintSelectedBorder(Graphics g, int x, int y, int width, 90 int height) { 91 if (selected) { 92 g.setColor(Color.black); 94 g.drawRect(x,y,width-1,height-1); 96 97 g.fillRect(x+width-5, y+height-5, 5, 5); 99 } 100 } 101 102 103 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 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++; 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 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 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++; 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 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 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 (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++; 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 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 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 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 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 l=l+drawDashDotDot(g,x,l,thickness,false,true); 485 } 486 487 } 488 } 489 490 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; } 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 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 |