1 19 24 25 package org.netbeans.swing.tabcontrol.plaf; 26 27 import javax.swing.*; 28 import java.awt.*; 29 import java.awt.geom.Area ; 30 import java.awt.geom.Point2D ; 31 import org.netbeans.swing.tabcontrol.TabDisplayer; 32 33 38 final class WinXPEditorTabCellRenderer extends AbstractTabCellRenderer { 39 private static final int TOP_INSET = 0; 41 private static final int LEFT_INSET = 3; 42 private static final int RIGHT_INSET = 0; 43 static final int BOTTOM_INSET = 3; 44 45 private static final TabPainter leftClip = new WinXPLeftClipPainter(); 48 private static final TabPainter rightClip = new WinXPRightClipPainter(); 49 private static final TabPainter normal = new WinXPPainter(); 50 51 54 public WinXPEditorTabCellRenderer() { 55 super(leftClip, normal, rightClip, new Dimension(32, 42)); 56 } 57 58 61 public int getPixelsToAddToSelection() { 62 return 4; 63 } 64 65 public Dimension getPadding() { 66 Dimension d = super.getPadding(); 67 d.width = isShowCloseButton() && !Boolean.getBoolean("nb.tabs.suppressCloseButton") ? 32 : 16; 68 return d; 69 } 70 71 private static final Color getTopInactiveSelectedColor() { 72 Color result = UIManager.getColor("tab_sel_fill_bright"); if (result == null) { 74 result = new Color(252, 250, 244); 75 } 76 return result; 77 } 78 79 private static final Color getBottomInactiveSelectedColor() { 80 Color result = UIManager.getColor("tab_sel_fill_dark"); if (result == null) { 82 result = new Color(243, 241, 224); 83 } 84 return result; 85 } 86 87 private static final Color getTopActiveSelectedColor() { 88 Color result = UIManager.getColor("tab_focus_fill_bright"); if (result == null) { 90 result = new Color(210, 220, 243); 91 } 92 return result; 93 } 94 95 private static final Color getBottomActiveSelectedColor() { 96 Color result = UIManager.getColor("tab_focus_fill_dark"); if (result == null) { 98 result = new Color(238, 242, 253); 99 } 100 return result; 101 } 102 103 private static final Color getTopUnselectedColor() { 104 Color result = UIManager.getColor("tab_unsel_fill_bright"); if (result == null) { 106 result = Color.white; 107 } 108 return result; 109 } 110 111 private static final Color getBottomUnselectedColor() { 112 Color result = UIManager.getColor("tab_unsel_fill_dark"); if (result == null) { 114 result = new Color(236, 235, 229); 115 } 116 return result; 117 } 118 119 static final Color getBorderColor() { 120 Color result = UIManager.getColor("tab_bottom_border"); if (result == null) { 122 result = new Color(127, 187, 185); 123 } 124 return result; 125 } 126 127 private static final Color getCloseButtonColor( 128 WinXPEditorTabCellRenderer ren) { 129 String key = ren.inCloseButton() ? 130 "close_button_highlight" : "close_button"; Color result = UIManager.getColor(key); 132 if (result == null) { 133 result = ren.inCloseButton() ? 134 new Color(172, 57, 28) : Color.black; 135 } 136 return result; 137 } 138 139 static final Color getSelectedTabBottomLineColor() { 140 Color result = UIManager.getColor("tab_sel_bottom_border"); if (result == null) { 142 result = new Color(238, 235, 218); 143 } 144 return result; 145 } 146 147 private static final Color getShadowBorderColor( 148 WinXPEditorTabCellRenderer ren) { 149 return getBorderColor(); 152 } 153 154 private static final Color getHighlightBorderColor( 155 WinXPEditorTabCellRenderer ren) { 156 return getBorderColor(); 159 } 160 161 private static final Color getHighlightColor() { 162 Color result = UIManager.getColor("TabbedPane.selectionIndicator"); if (result == null) { 164 result = new Color(255, 199, 60); } 166 return result; 167 } 168 169 private static final Color getTopHighlightColor() { 170 Color result = UIManager.getColor("tab_highlight_header"); if (result == null) { 172 result = new Color(230, 139, 44); 173 } 174 return result; 175 } 176 177 public Color getSelectedActivatedForeground() { 178 Color result = UIManager.getColor("textText"); if (result == null) { 180 result = Color.BLACK; 181 } 182 return result; 183 } 184 185 private static final Color getRightEdgeSelectedShadow() { 186 Color result = UIManager.getColor("close_button_border_focus"); if (result == null) { 188 result = new Color(181, 201, 243); 189 } 190 return result; 191 } 192 193 public Color getSelectedActivatedBackground() { 194 Color top = UIManager.getColor("tab_focus_fill_bright"); Color bot = UIManager.getColor("tab_focus_fill_dark"); if (top == null) { 197 top = new Color(238, 242, 253); 198 } 199 if (bot == null) { 200 bot = new Color(210, 220, 243); 201 } 202 Color result = ColorUtil.getMiddle(top, bot); 203 return result; 204 } 205 206 public static Color getCloseButtonAAColor(WinXPEditorTabCellRenderer ren) { 207 Color towards = ren.getBackground(); 208 Color base = getCloseButtonColor(ren); 209 210 Color result = org.netbeans.swing.tabcontrol.plaf.ColorUtil.getMiddle( 211 base, towards); 212 int factor = ren.inCloseButton() ? 35 : 74; 213 factor *= ColorUtil.isBrighter(towards, base) ? 1 : -1; 214 result = ColorUtil.adjustBy(result, factor); 215 return result; 216 } 217 218 private static Color getCloseButtonBorderColor( 219 WinXPEditorTabCellRenderer ren) { 220 String key = ren.isActive() && ren.isSelected() ? "close_button_border_focus" : ren.isSelected() ? 221 "close_button_border_selected" : "close_button_border_unsel"; Color result = UIManager.getColor(key); 223 if (result == null) { 224 result = ren.isActive() && ren.isSelected() ? new Color(181, 201, 225 243) : ren.isSelected() ? 226 new Color(203, 202, 187) : new Color(200, 201, 192); 227 } 228 return result; 229 } 230 231 public static Color getCloseButtonHighlight(WinXPEditorTabCellRenderer ren) { 232 Color result = ren.isPressed() && ren.inCloseButton() ? getCloseButtonBorderColor( 233 ren) : ren.isActive() && ren.isSelected() ? 234 UIManager.getColor("tab_sel_fill_dark") : 235 UIManager.getColor("tab_sel_fill_bright"); 236 if (result == null) { 237 result = Color.white; 238 } 239 return result; 240 } 241 242 public static Color getCloseButtonShadow(WinXPEditorTabCellRenderer ren) { 243 return ren.isPressed() && ren.inCloseButton() ? 244 Color.WHITE : getCloseButtonBorderColor(ren); 245 } 246 247 248 private static final Paint getPaint(WinXPEditorTabCellRenderer ren, 249 TabPainter p) { 250 Insets ins = p.getBorderInsets(ren); 251 int xTop = ins.left; 252 int yTop = ins.top; 253 int xBot = ins.left; 254 int yBot = ren.getHeight() - (ins.top + ins.bottom + 1); 255 if (ren.isSelected() || ren.isArmed()) { 256 yTop += 3; 257 } 258 return getPaint(xTop, yTop, xBot, yBot, ren); 259 } 260 261 private static final Paint getPaint(int xTop, int yTop, int xBot, int yBot, 262 WinXPEditorTabCellRenderer ren) { 263 264 if (!ren.isSelected() && !ren.isPressed() && ren.isAttention()) { 265 Color a = new Color (255, 255, 128); 266 Color b = new Color (230, 200, 64); 267 return ColorUtil.getGradientPaint(xTop, yTop, a, xBot, 268 yBot, b); 269 } else if (ren.isSelected() || (ren.isPressed() && !ren.inCloseButton())) { 270 if (ren.isActive()) { 271 return ColorUtil.getGradientPaint(xTop, yTop, 272 getTopActiveSelectedColor(), 273 xBot, yBot, 274 getBottomActiveSelectedColor()); 275 } else { 276 Color a = getTopInactiveSelectedColor(); 277 Color b = getBottomInactiveSelectedColor(); 278 if (a == b) { 279 return a; 280 } else { 281 return ColorUtil.getGradientPaint(xTop, yTop, a, xBot, 282 yBot, b); 283 } 284 } 285 } else { 286 return ColorUtil.getGradientPaint(xTop, yTop, 287 getTopUnselectedColor(), xBot, 288 yBot, 289 getBottomUnselectedColor()); 290 } 291 } 292 293 private static final void paintGradient(Graphics g, 294 WinXPEditorTabCellRenderer ren, 295 TabPainter p) { 296 Graphics2D g2d = (Graphics2D) g; 297 Insets ins = p.getBorderInsets(ren); 299 Paint gp = getPaint(ren, p); 300 g2d.setPaint(gp); 301 Polygon poly = p.getInteriorPolygon(ren); 302 g.fillPolygon(poly); 303 if (ren.isArmed() || ren.isSelected()) { 304 paintTopLine(g, ren, p); 305 } 306 } 307 308 private static final void paintTopLine(Graphics g, 309 WinXPEditorTabCellRenderer ren, 310 TabPainter p) { 311 Polygon poly = p.getInteriorPolygon(ren); 312 ((Graphics2D) g).setPaint(getHighlightColor()); 313 g.setColor(getHighlightColor()); 314 Shape clip = g.getClip(); 315 Insets ins = p.getBorderInsets(ren); 316 try { 317 if (clip != null) { 318 Area a = new Area (clip); 319 a.intersect(new Area (poly)); 320 g.setClip(a); 321 } else { 322 g.setClip(poly); 323 } 324 g.fillRect(0, ins.top, ren.getWidth(), 3); 325 } finally { 326 g.setClip(clip); 327 } 328 } 329 330 protected int getCaptionYAdjustment() { 331 return 1; 332 } 333 334 private static class WinXPPainter implements TabPainter { 335 336 public Insets getBorderInsets(Component c) { 337 return new Insets(TOP_INSET, LEFT_INSET, BOTTOM_INSET, RIGHT_INSET); 338 } 339 340 public void getCloseButtonRectangle(JComponent jc, 341 final Rectangle rect, 342 Rectangle bounds) { 343 344 WinXPEditorTabCellRenderer ren = (WinXPEditorTabCellRenderer) jc; 345 if (!ren.isShowCloseButton()) { 346 rect.x = -100; 347 rect.y = -100; 348 rect.width = 0; 349 rect.height = 0; 350 return; 351 } 352 String iconPath = findIconPath(ren); 353 Icon icon = TabControlButtonFactory.getIcon(iconPath); 354 int iconWidth = icon.getIconWidth(); 355 int iconHeight = icon.getIconHeight(); 356 rect.x = bounds.x + bounds.width - iconWidth - 2; 357 rect.y = bounds.y + (Math.max(0, bounds.height / 2 - iconHeight / 2)); 358 rect.width = iconWidth; 359 rect.height = iconHeight; 360 } 361 362 public Polygon getInteriorPolygon(Component c) { 363 WinXPEditorTabCellRenderer ren = (WinXPEditorTabCellRenderer) c; 364 365 Insets ins = getBorderInsets(c); 366 Polygon p = new Polygon(); 367 int x = 0; 369 int y = 0; 370 371 int h = c.getHeight() - ins.bottom; 372 373 int width = ren.isRightmost() ? c.getWidth() - 1 : c.getWidth(); 374 int height = ren.isSelected() ? h + 1 : h; 375 376 p.addPoint(x, y + ins.top + 2); 377 p.addPoint(x + 2, y + ins.top); 378 p.addPoint(x + width - 2, y + ins.top); 379 p.addPoint(x + width, y + ins.top + 2); 380 p.addPoint(x + width, y + height - 1); 381 p.addPoint(x, y + height - 1); 382 return p; 383 } 384 385 public boolean isBorderOpaque() { 386 return true; 387 } 388 389 public void paintBorder(Component c, Graphics g, int x, int y, 390 int width, int height) { 391 WinXPEditorTabCellRenderer ren = (WinXPEditorTabCellRenderer) c; 392 Polygon p = getInteriorPolygon(c); 393 g.setColor(getHighlightBorderColor(ren)); 394 395 int[] xpoints = p.xpoints; 396 int[] ypoints = p.ypoints; 397 398 g.drawLine(xpoints[0], ypoints[0], xpoints[p.npoints - 1], 399 ypoints[p.npoints - 1]); 400 401 Color prev = null; 402 boolean topColor = ren.isArmed() || ren.isSelected(); 403 for (int i = 0; i < p.npoints - 1; i++) { 404 if (i == 0 && topColor) { 405 prev = g.getColor(); 406 g.setColor(getTopHighlightColor()); 407 } else if (i == 3 && topColor) { 408 g.setColor(prev); 409 } 410 g.drawLine(xpoints[i], ypoints[i], xpoints[i + 1], 411 ypoints[i + 1]); 412 } 413 414 g.setColor(ColorUtil.adjustComponentsTowards(topColor ? 416 getTopHighlightColor() : 417 getHighlightBorderColor( 418 ren), 419 UIManager.getColor( 420 "control"))); 422 g.drawLine(xpoints[0] - 1, ypoints[0], xpoints[1] - 1, 424 ypoints[1]); 425 g.drawLine(xpoints[2] + 1, ypoints[2], xpoints[3] + 1, 426 ypoints[3]); 427 428 if (ren.isSelected()) { 429 g.setColor(getRightEdgeSelectedShadow()); 430 Insets ins = getBorderInsets(c); 431 g.drawLine(ren.getWidth() - (ren.isRightmost() ? 2 : 1), 432 ins.top + 3, width - (ren.isRightmost() ? 2 : 1), 433 height - ins.bottom); 434 435 GradientPaint paint = (GradientPaint) getPaint(ins.top + 3, 0, height - (ins.top 436 + ins.bottom 437 + 2), 1, 438 ren); 439 ((Graphics2D) g).setPaint(paint); 440 Point2D p1 = paint.getPoint1(); 442 Point2D p2 = paint.getPoint2(); 443 paint = 444 ColorUtil.getGradientPaint(Math.round(p2.getX()), 445 Math.round(p2.getY()), 446 paint.getColor1(), 447 Math.round(p1.getX()), 448 Math.round(p1.getY()), 449 paint.getColor2(), false); 450 int rpos = ren.isLeftmost() ? x + 2 : x + 1; 452 g.fillRect(rpos, y + ins.top + 3, rpos, 453 (height - (ins.top + ins.bottom + 3))); 454 455 g.setColor(getSelectedTabBottomLineColor()); 456 g.drawLine(1, height - ins.bottom, width - 2, 457 height - ins.bottom); 458 } 459 } 460 461 462 public void paintInterior(Graphics g, Component c) { 463 WinXPEditorTabCellRenderer ren = (WinXPEditorTabCellRenderer) c; 464 paintGradient(g, ren, this); 466 467 Rectangle r = new Rectangle(); 468 getCloseButtonRectangle(ren, r, new Rectangle(0, 0, 470 ren.getWidth(), 471 ren.getHeight())); 472 473 if (!g.hitClip(r.x, r.y, r.width, r.height)) { 474 return; 475 } 476 477 String iconPath = findIconPath( ren ); 479 Icon icon = TabControlButtonFactory.getIcon( iconPath ); 480 icon.paintIcon(ren, g, r.x, r.y); 481 } 482 483 487 private String findIconPath( WinXPEditorTabCellRenderer renderer ) { 488 if( renderer.inCloseButton() && renderer.isPressed() ) { 489 return "org/netbeans/swing/tabcontrol/resources/xp_close_pressed.png"; } 491 if( renderer.inCloseButton() ) { 492 return "org/netbeans/swing/tabcontrol/resources/xp_close_rollover.png"; } 494 return "org/netbeans/swing/tabcontrol/resources/xp_close_enabled.png"; } 496 497 public boolean supportsCloseButton(JComponent renderer) { 498 return renderer instanceof TabDisplayer ? 499 ((TabDisplayer) renderer).isShowCloseButton() : true; 500 } 501 502 } 503 504 private static class WinXPLeftClipPainter implements TabPainter { 505 506 public Insets getBorderInsets(Component c) { 507 return new Insets(TOP_INSET, LEFT_INSET, BOTTOM_INSET, RIGHT_INSET); 508 } 509 510 public Polygon getInteriorPolygon(Component c) { 511 WinXPEditorTabCellRenderer ren = (WinXPEditorTabCellRenderer) c; 512 513 Insets ins = getBorderInsets(c); 514 Polygon p = new Polygon(); 515 int x = -3; 516 int y = 0; 517 518 int h = c.getHeight() - ins.bottom; 519 520 int width = c.getWidth() + 3; 521 int height = ren.isSelected() ? h + 1 : h; 522 523 p.addPoint(x, y + ins.top + 2); 524 p.addPoint(x + 2, y + ins.top); 525 p.addPoint(x + width - 2, y + ins.top); 526 p.addPoint(x + width, y + ins.top + 2); 527 p.addPoint(x + width, y + height - 1); 528 p.addPoint(x, y + height - 1); 529 return p; 530 } 531 532 public void paintBorder(Component c, Graphics g, int x, int y, 533 int width, int height) { 534 WinXPEditorTabCellRenderer ren = (WinXPEditorTabCellRenderer) c; 535 Polygon p = getInteriorPolygon(c); 536 g.setColor(getHighlightBorderColor(ren)); 537 538 int[] xpoints = p.xpoints; 539 int[] ypoints = p.ypoints; 540 541 g.drawLine(xpoints[0], ypoints[0], xpoints[p.npoints - 1], 542 ypoints[p.npoints - 1]); 543 Color prev = null; 544 boolean topColor = ren.isArmed() || ren.isSelected(); 545 for (int i = 0; i < p.npoints - 1; i++) { 546 if (i == 1 && topColor) { 547 prev = g.getColor(); 548 g.setColor(getTopHighlightColor()); 549 } else if (i == 2 && topColor) { 550 g.setColor(prev); 551 } 552 g.drawLine(xpoints[i], ypoints[i], xpoints[i + 1], 553 ypoints[i + 1]); 554 if (i == p.npoints - 4) { 555 g.setColor(getShadowBorderColor(ren)); 556 g.drawLine(xpoints[i] + 1, ypoints[i] + 1, 557 xpoints[i] + 2, ypoints[i] + 2); 558 } 559 } 560 g.setColor(ColorUtil.adjustComponentsTowards(topColor ? 562 getTopHighlightColor() : 563 getHighlightBorderColor( 564 ren), 565 UIManager.getColor( 566 "control"))); 568 g.drawLine(xpoints[0] - 1, ypoints[0], xpoints[1] - 1, 570 ypoints[1]); 571 g.drawLine(xpoints[2] + 1, ypoints[2], xpoints[3] + 1, 572 ypoints[3]); 573 574 if (ren.isSelected()) { 575 g.setColor(getRightEdgeSelectedShadow()); 576 Insets ins = getBorderInsets(c); 577 g.drawLine(ren.getWidth() - 1, ins.top + 3, width - 1, 578 height - ins.bottom); 579 g.setColor(getSelectedTabBottomLineColor()); 580 g.drawLine(0, height - ins.bottom, width - 2, 581 height - ins.bottom); 582 583 } 584 } 585 586 public void paintInterior(Graphics g, Component c) { 587 WinXPEditorTabCellRenderer ren = (WinXPEditorTabCellRenderer) c; 588 paintGradient(g, ren, this); 589 } 590 591 public boolean isBorderOpaque() { 592 return true; 593 } 594 595 public boolean supportsCloseButton(JComponent renderer) { 596 return false; 597 } 598 599 public void getCloseButtonRectangle(JComponent jc, 600 final Rectangle rect, 601 Rectangle bounds) { 602 rect.setBounds(-20, -20, 0, 0); 603 } 604 605 } 606 607 private static class WinXPRightClipPainter implements TabPainter { 608 609 public Insets getBorderInsets(Component c) { 610 return new Insets(TOP_INSET, LEFT_INSET, BOTTOM_INSET, RIGHT_INSET); 611 } 612 613 public boolean isBorderOpaque() { 614 return true; 615 } 616 617 public Polygon getInteriorPolygon(Component c) { 618 WinXPEditorTabCellRenderer ren = (WinXPEditorTabCellRenderer) c; 619 620 Insets ins = getBorderInsets(c); 621 Polygon p = new Polygon(); 622 int x = 0; 623 int y = 0; 624 625 int h = c.getHeight() - ins.bottom; 626 627 int width = c.getWidth() + 3; 628 int height = ren.isSelected() ? h + 1 : h; 630 p.addPoint(x, y + ins.top + 2); 631 p.addPoint(x + 2, y + ins.top); 632 p.addPoint(x + width, y + ins.top); 633 p.addPoint(x + width, y + height - 1); 634 p.addPoint(x, y + height - 1); 635 return p; 636 } 637 638 public void paintBorder(Component c, Graphics g, int x, int y, 639 int width, int height) { 640 WinXPEditorTabCellRenderer ren = (WinXPEditorTabCellRenderer) c; 641 Polygon p = getInteriorPolygon(c); 642 g.setColor(getHighlightBorderColor(ren)); 643 644 int[] xpoints = p.xpoints; 645 int[] ypoints = p.ypoints; 646 647 g.drawLine(xpoints[0], ypoints[0], xpoints[p.npoints - 1], 648 ypoints[p.npoints - 1]); 649 650 Color prev = null; 651 boolean topColor = ren.isArmed() || ren.isSelected(); 652 for (int i = 0; i < p.npoints - 1; i++) { 653 if (i == 1 && topColor) { 654 prev = g.getColor(); 655 g.setColor(getTopHighlightColor()); 656 } else if (i == 2 && topColor) { 657 g.setColor(prev); 658 } 659 g.drawLine(xpoints[i], ypoints[i], xpoints[i + 1], 660 ypoints[i + 1]); 661 } 662 663 g.setColor(ColorUtil.adjustComponentsTowards(topColor ? 665 getTopHighlightColor() : 666 getHighlightBorderColor( 667 ren), 668 UIManager.getColor( 669 "control"))); 671 g.drawLine(xpoints[0] - 1, ypoints[0], xpoints[1] - 1, 673 ypoints[1]); 674 676 677 if (ren.isSelected()) { 678 Insets ins = getBorderInsets(c); 679 680 GradientPaint paint = (GradientPaint) getPaint(ins.top + 3, 0, height - (ins.top 681 + ins.bottom 682 + 2), 1, 683 ren); 684 ((Graphics2D) g).setPaint(paint); 685 Point2D p1 = paint.getPoint1(); 687 Point2D p2 = paint.getPoint2(); 688 paint = 689 ColorUtil.getGradientPaint(Math.round(p2.getX()), 690 Math.round(p2.getY()), 691 paint.getColor1(), 692 Math.round(p1.getX()), 693 Math.round(p1.getY()), 694 paint.getColor2(), false); 695 g.fillRect(x + 1, y + ins.top + 3, x + 1, 697 (height - (ins.top + ins.bottom + 3))); 698 699 g.setColor(getSelectedTabBottomLineColor()); 700 g.drawLine(1, height - ins.bottom, width - 1, 701 height - ins.bottom); 702 } 703 } 704 705 public void paintInterior(Graphics g, Component c) { 706 WinXPEditorTabCellRenderer ren = (WinXPEditorTabCellRenderer) c; 707 paintGradient(g, ren, this); 708 } 709 710 public boolean supportsCloseButton(JComponent renderer) { 711 return false; 712 } 713 714 public void getCloseButtonRectangle(JComponent jc, 715 final Rectangle rect, 716 Rectangle bounds) { 717 rect.setBounds(-20, -20, 0, 0); 718 } 719 } 720 } 721 | Popular Tags |