1 7 8 package com.sun.java.swing.plaf.motif; 9 10 import com.sun.java.swing.SwingUtilities2; 11 import javax.swing.*; 12 import javax.swing.border.*; 13 import javax.swing.plaf.*; 14 15 import java.awt.Color ; 16 import java.awt.Component ; 17 import java.awt.Dimension ; 18 import java.awt.Font ; 19 import java.awt.FontMetrics ; 20 import java.awt.Graphics ; 21 import java.awt.Insets ; 22 import java.awt.Point ; 23 import java.awt.Rectangle ; 24 25 import java.io.Serializable ; 26 27 40 public class MotifBorders { 41 42 public static class BevelBorder extends AbstractBorder implements UIResource { 43 private Color darkShadow = UIManager.getColor("controlShadow"); 44 private Color lightShadow = UIManager.getColor("controlLtHighlight"); 45 private boolean isRaised; 46 47 public BevelBorder(boolean isRaised, Color darkShadow, Color lightShadow) { 48 this.isRaised = isRaised; 49 this.darkShadow = darkShadow; 50 this.lightShadow = lightShadow; 51 } 52 53 public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) { 54 g.setColor((isRaised) ? lightShadow : darkShadow); 55 g.drawLine(x, y, x+w-1, y); g.drawLine(x, y+h-1, x, y+1); 58 g.setColor((isRaised) ? darkShadow : lightShadow); 59 g.drawLine(x+1, y+h-1, x+w-1, y+h-1); g.drawLine(x+w-1, y+h-1, x+w-1, y+1); } 62 63 public Insets getBorderInsets(Component c) { 64 return getBorderInsets(c, new Insets (0,0,0,0)); 65 } 66 67 public Insets getBorderInsets(Component c, Insets insets) { 68 insets.top = insets.left = insets.bottom = insets.right = 1; 69 return insets; 70 } 71 72 public boolean isOpaque(Component c) { 73 return true; 74 } 75 76 } 77 78 79 public static class FocusBorder extends AbstractBorder implements UIResource { 80 private Color focus; 81 private Color control; 82 83 public FocusBorder(Color control, Color focus) { 84 this.control = control; 85 this.focus = focus; 86 } 87 88 public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) { 89 if (((JComponent)c).hasFocus()) { 90 g.setColor(focus); 91 g.drawRect(x, y, w-1, h-1); 92 } else { 93 g.setColor(control); 94 g.drawRect(x, y, w-1, h-1); 95 } 96 } 97 98 public Insets getBorderInsets(Component c) { 99 return getBorderInsets(c, new Insets (0,0,0,0)); 100 } 101 102 public Insets getBorderInsets(Component c, Insets insets) { 103 insets.top = insets.left = insets.bottom = insets.right = 1; 104 return insets; 105 } 106 } 107 108 109 public static class ButtonBorder extends AbstractBorder implements UIResource { 110 protected Color focus = UIManager.getColor("activeCaptionBorder"); 111 protected Color shadow = UIManager.getColor("Button.shadow"); 112 protected Color highlight = UIManager.getColor("Button.light"); 113 protected Color darkShadow; 114 115 public ButtonBorder(Color shadow, Color highlight, Color darkShadow, Color focus) { 116 this.shadow = shadow; 117 this.highlight = highlight; 118 this.darkShadow = darkShadow; 119 this.focus = focus; 120 } 121 122 public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) { 123 boolean isPressed = false; 124 boolean hasFocus = false; 125 boolean canBeDefault = false; 126 boolean isDefault = false; 127 128 if (c instanceof AbstractButton) { 129 AbstractButton b = (AbstractButton)c; 130 ButtonModel model = b.getModel(); 131 132 isPressed = (model.isArmed() && model.isPressed()); 133 hasFocus = (model.isArmed() && isPressed) || 134 (b.isFocusPainted() && b.hasFocus()); 135 if (b instanceof JButton) { 136 canBeDefault = ((JButton)b).isDefaultCapable(); 137 isDefault = ((JButton)b).isDefaultButton(); 138 } 139 } 140 int bx1 = x+1; 141 int by1 = y+1; 142 int bx2 = x+w-2; 143 int by2 = y+h-2; 144 145 if (canBeDefault) { 146 if (isDefault) { 147 g.setColor(shadow); 148 g.drawLine(x+3, y+3, x+3, y+h-4); 149 g.drawLine(x+3, y+3, x+w-4, y+3); 150 151 g.setColor(highlight); 152 g.drawLine(x+4, y+h-4, x+w-4, y+h-4); 153 g.drawLine(x+w-4, y+3, x+w-4, y+h-4); 154 } 155 bx1 +=6; 156 by1 += 6; 157 bx2 -= 6; 158 by2 -= 6; 159 } 160 161 if (hasFocus) { 162 g.setColor(focus); 163 if (isDefault) { 164 g.drawRect(x, y, w-1, h-1); 165 } else { 166 g.drawRect(bx1-1, by1-1, bx2-bx1+2, by2-by1+2); 167 } 168 } 169 170 g.setColor(isPressed? shadow : highlight); 171 g.drawLine(bx1, by1, bx2, by1); 172 g.drawLine(bx1, by1, bx1, by2); 173 174 g.setColor(isPressed? highlight : shadow); 175 g.drawLine(bx2, by1+1, bx2, by2); 176 g.drawLine(bx1+1, by2, bx2, by2); 177 } 178 179 public Insets getBorderInsets(Component c) { 180 return getBorderInsets(c, new Insets (0,0,0,0)); 181 } 182 183 public Insets getBorderInsets(Component c, Insets insets) { 184 int thickness = (c instanceof JButton && ((JButton)c).isDefaultCapable())? 8 : 2; 185 insets.top = insets.left = insets.bottom = insets.right = thickness; 186 return insets; 187 } 188 189 } 190 191 public static class ToggleButtonBorder extends ButtonBorder { 192 193 public ToggleButtonBorder(Color shadow, Color highlight, Color darkShadow, Color focus) { 194 super(shadow, highlight, darkShadow, focus); 195 } 196 197 public void paintBorder(Component c, Graphics g, int x, int y, 198 int width, int height) { 199 if (c instanceof AbstractButton) { 200 AbstractButton b = (AbstractButton)c; 201 ButtonModel model = b.getModel(); 202 203 if (model.isArmed() && model.isPressed() || model.isSelected()) { 204 drawBezel(g, x, y, width, height, 205 (model.isPressed() || model.isSelected()), 206 b.isFocusPainted() && b.hasFocus(), shadow, highlight, darkShadow, focus); 207 } else { 208 drawBezel(g, x, y, width, height, 209 false, b.isFocusPainted() && b.hasFocus(), 210 shadow, highlight, darkShadow, focus); 211 } 212 } else { 213 drawBezel(g, x, y, width, height, false, false, 214 shadow, highlight, darkShadow, focus); 215 } 216 } 217 218 public Insets getBorderInsets(Component c) { 219 return new Insets (2, 2, 3, 3); 220 } 221 222 public Insets getBorderInsets(Component c, Insets insets) { 223 insets.top = insets.left = 2; 224 insets.bottom = insets.right = 3; 225 return insets; 226 } 227 } 228 229 public static class MenuBarBorder extends ButtonBorder { 230 231 public MenuBarBorder(Color shadow, Color highlight, Color darkShadow, Color focus) { 232 super(shadow, highlight, darkShadow, focus); 233 } 234 235 public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) { 236 JMenuBar menuBar = (JMenuBar)c; 237 if (menuBar.isBorderPainted() == true) { 238 Dimension size = menuBar.getSize(); 240 drawBezel(g,x,y,size.width,size.height,false,false, 241 shadow, highlight, darkShadow, focus); 242 } 243 } 244 245 public Insets getBorderInsets(Component c) { 246 return getBorderInsets(c, new Insets (0,0,0,0)); 247 } 248 249 public Insets getBorderInsets(Component c, Insets insets) { 250 insets.top = insets.left = insets.bottom = insets.right = 6; 251 return insets; 252 } 253 } 254 255 public static class FrameBorder extends AbstractBorder implements UIResource { 256 257 JComponent jcomp; 258 Color frameHighlight; 259 Color frameColor; 260 Color frameShadow; 261 262 public final static int BORDER_SIZE = 5; 264 265 267 public FrameBorder(JComponent comp) { 268 jcomp = comp; 269 } 270 271 273 public void setComponent(JComponent comp) { 274 jcomp = comp; 275 } 276 277 280 public JComponent component() { 281 return jcomp; 282 } 283 284 protected Color getFrameHighlight() { 285 return frameHighlight; 286 } 287 288 protected Color getFrameColor() { 289 return frameColor; 290 } 291 292 protected Color getFrameShadow() { 293 return frameShadow; 294 } 295 296 static Insets insets = new Insets (BORDER_SIZE, BORDER_SIZE, 297 BORDER_SIZE, BORDER_SIZE); 298 299 public Insets getBorderInsets(Component c) { 300 return insets; 301 } 302 303 public Insets getBorderInsets(Component c, Insets newInsets) { 304 newInsets.top = insets.top; 305 newInsets.left = insets.left; 306 newInsets.bottom = insets.bottom; 307 newInsets.right = insets.right; 308 return newInsets; 309 } 310 311 313 protected boolean drawTopBorder(Component c, Graphics g, 314 int x, int y, int width, int height) { 315 Rectangle titleBarRect = new Rectangle (x, y, width, BORDER_SIZE); 316 if (!g.getClipBounds().intersects(titleBarRect)) { 317 return false; 318 } 319 320 int maxX = width - 1; 321 int maxY = BORDER_SIZE - 1; 322 323 g.setColor(frameColor); 325 g.drawLine(x, y + 2, maxX - 2, y + 2); 326 g.drawLine(x, y + 3, maxX - 2, y + 3); 327 g.drawLine(x, y + 4, maxX - 2, y + 4); 328 329 g.setColor(frameHighlight); 331 g.drawLine(x, y, maxX, y); 332 g.drawLine(x, y + 1, maxX, y + 1); 333 g.drawLine(x, y + 2, x, y + 4); 334 g.drawLine(x + 1, y + 2, x + 1, y + 4); 335 336 g.setColor(frameShadow); 338 g.drawLine(x + 4, y + 4, maxX - 4, y + 4); 339 g.drawLine(maxX, y + 1, maxX, maxY); 340 g.drawLine(maxX - 1, y + 2, maxX - 1, maxY); 341 342 return true; 343 } 344 345 347 protected boolean drawLeftBorder(Component c, Graphics g, int x, int y, 348 int width, int height) { 349 Rectangle borderRect = 350 new Rectangle (0, 0, getBorderInsets(c).left, height); 351 if (!g.getClipBounds().intersects(borderRect)) { 352 return false; 353 } 354 355 int startY = BORDER_SIZE; 356 357 g.setColor(frameHighlight); 358 g.drawLine(x, startY, x, height - 1); 359 g.drawLine(x + 1, startY, x + 1, height - 2); 360 361 g.setColor(frameColor); 362 g.fillRect(x + 2, startY, x + 2, height - 3); 363 364 g.setColor(frameShadow); 365 g.drawLine(x + 4, startY, x + 4, height - 5); 366 367 return true; 368 } 369 370 372 protected boolean drawRightBorder(Component c, Graphics g, int x, int y, 373 int width, int height) { 374 Rectangle borderRect = new Rectangle ( 375 width - getBorderInsets(c).right, 0, 376 getBorderInsets(c).right, height); 377 if (!g.getClipBounds().intersects(borderRect)) { 378 return false; 379 } 380 381 int startX = width - getBorderInsets(c).right; 382 int startY = BORDER_SIZE; 383 384 g.setColor(frameColor); 385 g.fillRect(startX + 1, startY, 2, height - 1); 386 387 g.setColor(frameShadow); 388 g.fillRect(startX + 3, startY, 2, height - 1); 389 390 g.setColor(frameHighlight); 391 g.drawLine(startX, startY, startX, height - 1); 392 393 return true; 394 } 395 396 398 protected boolean drawBottomBorder(Component c, Graphics g, int x, int y, 399 int width, int height) { 400 Rectangle borderRect; 401 int marginHeight, startY; 402 403 borderRect = new Rectangle (0, height - getBorderInsets(c).bottom, 404 width, getBorderInsets(c).bottom); 405 if (!g.getClipBounds().intersects(borderRect)) { 406 return false; 407 } 408 409 startY = height - getBorderInsets(c).bottom; 410 411 g.setColor(frameShadow); 412 g.drawLine(x + 1, height - 1, width - 1, height - 1); 413 g.drawLine(x + 2, height - 2, width - 2, height - 2); 414 415 g.setColor(frameColor); 416 g.fillRect(x + 2, startY + 1, width - 4, 2); 417 418 g.setColor(frameHighlight); 419 g.drawLine(x + 5, startY, width - 5, startY); 420 421 return true; 422 } 423 424 protected boolean isActiveFrame() { 426 return jcomp.hasFocus(); 427 } 428 429 433 public void paintBorder(Component c, Graphics g, 434 int x, int y, int width, int height) { 435 if (isActiveFrame()) { 436 frameColor = UIManager.getColor("activeCaptionBorder"); 437 } else { 438 frameColor = UIManager.getColor("inactiveCaptionBorder"); 439 } 440 frameHighlight = frameColor.brighter(); 441 frameShadow = frameColor.darker().darker(); 442 443 drawTopBorder(c, g, x, y, width, height); 444 drawLeftBorder(c, g, x, y, width, height); 445 drawRightBorder(c, g, x, y, width, height); 446 drawBottomBorder(c, g, x, y, width, height); 447 } 448 } 449 450 public static class InternalFrameBorder extends FrameBorder { 451 452 JInternalFrame frame; 453 454 public final static int CORNER_SIZE = 24; 456 457 460 public InternalFrameBorder(JInternalFrame aFrame) { 461 super(aFrame); 462 frame = aFrame; 463 } 464 465 467 public void setFrame(JInternalFrame aFrame) { 468 frame = aFrame; 469 } 470 471 474 public JInternalFrame frame() { 475 return frame; 476 } 477 478 486 public int resizePartWidth() { 487 if (!frame.isResizable()) { 488 return 0; 489 } 490 return FrameBorder.BORDER_SIZE; 491 } 492 493 495 protected boolean drawTopBorder(Component c, Graphics g, 496 int x, int y, int width, int height) { 497 if (super.drawTopBorder(c, g, x, y, width, height) && 498 frame.isResizable()) { 499 g.setColor(getFrameShadow()); 500 g.drawLine(CORNER_SIZE - 1, y + 1, CORNER_SIZE - 1, y + 4); 501 g.drawLine(width - CORNER_SIZE - 1, y + 1, 502 width - CORNER_SIZE - 1, y + 4); 503 504 g.setColor(getFrameHighlight()); 505 g.drawLine(CORNER_SIZE, y, CORNER_SIZE, y + 4); 506 g.drawLine(width - CORNER_SIZE, y, width - CORNER_SIZE, y + 4); 507 return true; 508 } 509 return false; 510 } 511 512 514 protected boolean drawLeftBorder(Component c, Graphics g, int x, int y, 515 int width, int height) { 516 if (super.drawLeftBorder(c, g, x, y, width, height) && 517 frame.isResizable()) { 518 g.setColor(getFrameHighlight()); 519 int topY = y + CORNER_SIZE; 520 g.drawLine(x, topY, x + 4, topY); 521 int bottomY = height - CORNER_SIZE; 522 g.drawLine(x + 1, bottomY, x + 5, bottomY); 523 g.setColor(getFrameShadow()); 524 g.drawLine(x + 1, topY - 1, x + 5, topY - 1); 525 g.drawLine(x + 1, bottomY - 1, x + 5, bottomY - 1); 526 return true; 527 } 528 return false; 529 } 530 531 533 protected boolean drawRightBorder(Component c, Graphics g, int x, int y, 534 int width, int height) { 535 if (super.drawRightBorder(c, g, x, y, width, height) && 536 frame.isResizable()) { 537 int startX = width - getBorderInsets(c).right; 538 g.setColor(getFrameHighlight()); 539 int topY = y + CORNER_SIZE; 540 g.drawLine(startX, topY, width - 2, topY); 541 int bottomY = height - CORNER_SIZE; 542 g.drawLine(startX + 1, bottomY, startX + 3, bottomY); 543 g.setColor(getFrameShadow()); 544 g.drawLine(startX + 1, topY - 1, width - 2, topY - 1); 545 g.drawLine(startX + 1, bottomY - 1, startX + 3, bottomY - 1); 546 return true; 547 } 548 return false; 549 } 550 551 553 protected boolean drawBottomBorder(Component c, Graphics g, int x, int y, 554 int width, int height) { 555 if (super.drawBottomBorder(c, g, x, y, width, height) && 556 frame.isResizable()) { 557 int startY = height - getBorderInsets(c).bottom; 558 559 g.setColor(getFrameShadow()); 560 g.drawLine(CORNER_SIZE - 1, startY + 1, 561 CORNER_SIZE - 1, height - 1); 562 g.drawLine(width - CORNER_SIZE, startY + 1, 563 width - CORNER_SIZE, height - 1); 564 565 g.setColor(getFrameHighlight()); 566 g.drawLine(CORNER_SIZE, startY, CORNER_SIZE, height - 2); 567 g.drawLine(width - CORNER_SIZE + 1, startY, 568 width - CORNER_SIZE + 1, height - 2); 569 return true; 570 } 571 return false; 572 } 573 574 protected boolean isActiveFrame() { 576 return frame.isSelected(); 577 } 578 } 579 580 public static void drawBezel(Graphics g, int x, int y, int w, int h, 581 boolean isPressed, boolean hasFocus, 582 Color shadow, Color highlight, 583 Color darkShadow, Color focus) { 584 585 Color oldColor = g.getColor(); 586 g.translate(x, y); 587 588 if (isPressed) { 589 if (hasFocus){ 590 g.setColor(focus); 591 g.drawRect(0, 0, w-1, h-1); 592 } 593 g.setColor(shadow); g.drawRect(1, 1, w-3, h-3); 595 596 g.setColor(highlight); g.drawLine(2, h-3, w-3, h-3); 598 g.drawLine(w-3, 2, w-3, h-4); 599 600 } else { 601 if (hasFocus) { 602 g.setColor(focus); 603 g.drawRect(0, 0, w-1, h-1); 604 605 g.setColor(highlight); g.drawLine(1, 1, 1, h-3); 607 g.drawLine(2, 1, w-4, 1); 608 609 g.setColor(shadow); 610 g.drawLine(2, h-3, w-3, h-3); 611 g.drawLine(w-3, 1, w-3, h-4); 612 613 g.setColor(darkShadow); g.drawLine(1, h-2, w-2, h-2); 615 g.drawLine(w-2, h-2, w-2, 1); 616 } else { 617 g.setColor(highlight); g.drawLine(1,1,1,h-3); 619 g.drawLine(2,1,w-4,1); 620 g.setColor(shadow); 621 g.drawLine(2,h-3,w-3,h-3); 622 g.drawLine(w-3,1,w-3,h-4); 623 624 g.setColor(darkShadow); g.drawLine(1,h-2,w-2,h-2); 626 g.drawLine(w-2,h-2,w-2,0); 627 628 } 629 g.translate(-x, -y); 630 } 631 g.setColor(oldColor); 632 } 633 634 public static class MotifPopupMenuBorder extends AbstractBorder implements UIResource { 635 protected Font font; 636 protected Color background; 637 protected Color foreground; 638 protected Color shadowColor; 639 protected Color highlightColor; 640 641 static protected final int TEXT_SPACING = 2; 643 644 static protected final int GROOVE_HEIGHT = 2; 646 647 651 public MotifPopupMenuBorder( 652 Font titleFont, 653 Color bgColor, 654 Color fgColor, 655 Color shadow, 656 Color highlight) { 657 this.font = titleFont; 658 this.background = bgColor; 659 this.foreground = fgColor; 660 this.shadowColor = shadow; 661 this.highlightColor = highlight; 662 } 663 664 674 public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) { 675 676 Font origFont = g.getFont(); 677 Color origColor = g.getColor(); 678 JPopupMenu popup = (JPopupMenu)c; 679 680 String title = popup.getLabel(); 681 if (title == null) { 682 return; 683 } 684 685 g.setFont(font); 686 687 FontMetrics fm = SwingUtilities2.getFontMetrics(popup, g, font); 688 int fontHeight = fm.getHeight(); 689 int descent = fm.getDescent(); 690 int ascent = fm.getAscent(); 691 Point textLoc = new Point (); 692 int stringWidth = SwingUtilities2.stringWidth(popup, fm, 693 title); 694 695 textLoc.y = y + ascent + TEXT_SPACING; 696 textLoc.x = x + ((width - stringWidth) / 2); 697 698 g.setColor(background); 699 g.fillRect(textLoc.x - TEXT_SPACING, textLoc.y - (fontHeight-descent), 700 stringWidth + (2 * TEXT_SPACING), fontHeight - descent); 701 g.setColor(foreground); 702 SwingUtilities2.drawString(popup, g, title, textLoc.x, textLoc.y); 703 704 MotifGraphicsUtils.drawGroove(g, x, textLoc.y + TEXT_SPACING, 705 width, GROOVE_HEIGHT, 706 shadowColor, highlightColor); 707 708 g.setFont(origFont); 709 g.setColor(origColor); 710 } 711 712 716 public Insets getBorderInsets(Component c) { 717 return getBorderInsets(c, new Insets (0, 0, 0, 0)); 718 } 719 720 725 public Insets getBorderInsets(Component c, Insets insets) { 726 FontMetrics fm; 727 int descent = 0; 728 int ascent = 16; 729 730 String title = ((JPopupMenu)c).getLabel(); 731 if (title == null) { 732 insets.left = insets.top = insets.right = insets.bottom = 0; 733 return insets; 734 } 735 736 fm = c.getFontMetrics(font); 737 738 if(fm != null) { 739 descent = fm.getDescent(); 740 ascent = fm.getAscent(); 741 } 742 743 insets.top += ascent + descent + TEXT_SPACING + GROOVE_HEIGHT; 744 return insets; 745 } 746 747 } 748 749 } 750 | Popular Tags |