1 7 8 package javax.swing.plaf.basic; 9 10 import javax.swing.*; 11 import javax.swing.border.*; 12 import javax.swing.plaf.*; 13 import javax.swing.text.JTextComponent ; 14 15 import java.awt.Component ; 16 import java.awt.Insets ; 17 import java.awt.Dimension ; 18 import java.awt.Rectangle ; 19 import java.awt.Color ; 20 import java.awt.Graphics ; 21 import java.io.Serializable ; 22 23 24 30 31 public class BasicBorders { 32 33 public static Border getButtonBorder() { 34 UIDefaults table = UIManager.getLookAndFeelDefaults(); 35 Border buttonBorder = new BorderUIResource.CompoundBorderUIResource( 36 new BasicBorders.ButtonBorder ( 37 table.getColor("Button.shadow"), 38 table.getColor("Button.darkShadow"), 39 table.getColor("Button.light"), 40 table.getColor("Button.highlight")), 41 new MarginBorder()); 42 return buttonBorder; 43 } 44 45 public static Border getRadioButtonBorder() { 46 UIDefaults table = UIManager.getLookAndFeelDefaults(); 47 Border radioButtonBorder = new BorderUIResource.CompoundBorderUIResource( 48 new BasicBorders.RadioButtonBorder ( 49 table.getColor("RadioButton.shadow"), 50 table.getColor("RadioButton.darkShadow"), 51 table.getColor("RadioButton.light"), 52 table.getColor("RadioButton.highlight")), 53 new MarginBorder()); 54 return radioButtonBorder; 55 } 56 57 public static Border getToggleButtonBorder() { 58 UIDefaults table = UIManager.getLookAndFeelDefaults(); 59 Border toggleButtonBorder = new BorderUIResource.CompoundBorderUIResource( 60 new BasicBorders.ToggleButtonBorder ( 61 table.getColor("ToggleButton.shadow"), 62 table.getColor("ToggleButton.darkShadow"), 63 table.getColor("ToggleButton.light"), 64 table.getColor("ToggleButton.highlight")), 65 new MarginBorder()); 66 return toggleButtonBorder; 67 } 68 69 public static Border getMenuBarBorder() { 70 UIDefaults table = UIManager.getLookAndFeelDefaults(); 71 Border menuBarBorder = new BasicBorders.MenuBarBorder ( 72 table.getColor("MenuBar.shadow"), 73 table.getColor("MenuBar.highlight") 74 ); 75 return menuBarBorder; 76 } 77 78 public static Border getSplitPaneBorder() { 79 UIDefaults table = UIManager.getLookAndFeelDefaults(); 80 Border splitPaneBorder = new BasicBorders.SplitPaneBorder ( 81 table.getColor("SplitPane.highlight"), 82 table.getColor("SplitPane.darkShadow")); 83 return splitPaneBorder; 84 } 85 86 90 public static Border getSplitPaneDividerBorder() { 91 UIDefaults table = UIManager.getLookAndFeelDefaults(); 92 Border splitPaneBorder = new BasicBorders.SplitPaneDividerBorder ( 93 table.getColor("SplitPane.highlight"), 94 table.getColor("SplitPane.darkShadow")); 95 return splitPaneBorder; 96 } 97 98 public static Border getTextFieldBorder() { 99 UIDefaults table = UIManager.getLookAndFeelDefaults(); 100 Border textFieldBorder = new BasicBorders.FieldBorder ( 101 table.getColor("TextField.shadow"), 102 table.getColor("TextField.darkShadow"), 103 table.getColor("TextField.light"), 104 table.getColor("TextField.highlight")); 105 return textFieldBorder; 106 } 107 108 public static Border getProgressBarBorder() { 109 UIDefaults table = UIManager.getLookAndFeelDefaults(); 110 Border progressBarBorder = new BorderUIResource.LineBorderUIResource(Color.green, 2); 111 return progressBarBorder; 112 } 113 114 public static Border getInternalFrameBorder() { 115 UIDefaults table = UIManager.getLookAndFeelDefaults(); 116 Border internalFrameBorder = new BorderUIResource.CompoundBorderUIResource( 117 new BevelBorder(BevelBorder.RAISED, 118 table.getColor("InternalFrame.borderLight"), 119 table.getColor("InternalFrame.borderHighlight"), 120 table.getColor("InternalFrame.borderDarkShadow"), 121 table.getColor("InternalFrame.borderShadow")), 122 BorderFactory.createLineBorder( 123 table.getColor("InternalFrame.borderColor"), 1)); 124 125 return internalFrameBorder; 126 } 127 128 131 public static class RolloverButtonBorder extends ButtonBorder { 132 133 public RolloverButtonBorder(Color shadow, Color darkShadow, 134 Color highlight, Color lightHighlight) { 135 super(shadow, darkShadow, highlight, lightHighlight); 136 } 137 138 public void paintBorder( Component c, Graphics g, int x, int y, int w, int h ) { 139 AbstractButton b = (AbstractButton) c; 140 ButtonModel model = b.getModel(); 141 142 Color shade = shadow; 143 Component p = b.getParent(); 144 if (p != null && p.getBackground().equals(shadow)) { 145 shade = darkShadow; 146 } 147 148 if ((model.isRollover() && !(model.isPressed() && !model.isArmed())) || 149 model.isSelected()) { 150 151 Color oldColor = g.getColor(); 152 g.translate(x, y); 153 154 if (model.isPressed() && model.isArmed() || model.isSelected()) { 155 g.setColor(shade); 157 g.drawRect(0, 0, w-1, h-1); 158 g.setColor(lightHighlight); 159 g.drawLine(w-1, 0, w-1, h-1); 160 g.drawLine(0, h-1, w-1, h-1); 161 } else { 162 g.setColor(lightHighlight); 164 g.drawRect(0, 0, w-1, h-1); 165 g.setColor(shade); 166 g.drawLine(w-1, 0, w-1, h-1); 167 g.drawLine(0, h-1, w-1, h-1); 168 } 169 g.translate(-x, -y); 170 g.setColor(oldColor); 171 } 172 } 173 } 174 175 176 183 static class RolloverMarginBorder extends EmptyBorder { 184 185 public RolloverMarginBorder() { 186 super(3,3,3,3); } 188 189 public Insets getBorderInsets(Component c) { 190 return getBorderInsets(c, new Insets (0,0,0,0)); 191 } 192 193 public Insets getBorderInsets(Component c, Insets insets) { 194 Insets margin = null; 195 196 if (c instanceof AbstractButton) { 197 margin = ((AbstractButton)c).getMargin(); 198 } 199 if (margin == null || margin instanceof UIResource) { 200 insets.left = left; 202 insets.top = top; 203 insets.right = right; 204 insets.bottom = bottom; 205 } else { 206 insets.left = margin.left; 208 insets.top = margin.top; 209 insets.right = margin.right; 210 insets.bottom = margin.bottom; 211 } 212 return insets; 213 } 214 } 215 216 public static class ButtonBorder extends AbstractBorder implements UIResource { 217 protected Color shadow; 218 protected Color darkShadow; 219 protected Color highlight; 220 protected Color lightHighlight; 221 222 public ButtonBorder(Color shadow, Color darkShadow, 223 Color highlight, Color lightHighlight) { 224 this.shadow = shadow; 225 this.darkShadow = darkShadow; 226 this.highlight = highlight; 227 this.lightHighlight = lightHighlight; 228 } 229 230 public void paintBorder(Component c, Graphics g, int x, int y, 231 int width, int height) { 232 boolean isPressed = false; 233 boolean isDefault = false; 234 235 if (c instanceof AbstractButton) { 236 AbstractButton b = (AbstractButton)c; 237 ButtonModel model = b.getModel(); 238 239 isPressed = model.isPressed() && model.isArmed(); 240 241 if (c instanceof JButton) { 242 isDefault = ((JButton)c).isDefaultButton(); 243 } 244 } 245 BasicGraphicsUtils.drawBezel(g, x, y, width, height, 246 isPressed, isDefault, shadow, 247 darkShadow, highlight, lightHighlight); 248 } 249 250 public Insets getBorderInsets(Component c) { 251 return getBorderInsets(c, new Insets (0,0,0,0)); 252 } 253 254 public Insets getBorderInsets(Component c, Insets insets) { 255 insets.top = 2; 257 insets.left = insets.bottom = insets.right = 3; 258 return insets; 259 } 260 261 } 262 263 public static class ToggleButtonBorder extends ButtonBorder { 264 265 public ToggleButtonBorder(Color shadow, Color darkShadow, 266 Color highlight, Color lightHighlight) { 267 super(shadow, darkShadow, highlight, lightHighlight); 268 } 269 270 public void paintBorder(Component c, Graphics g, int x, int y, 271 int width, int height) { 272 BasicGraphicsUtils.drawBezel(g, x, y, width, height, 273 false, false, 274 shadow, darkShadow, 275 highlight, lightHighlight); 276 } 277 278 public Insets getBorderInsets(Component c) { 279 return new Insets (2, 2, 2, 2); 280 } 281 282 public Insets getBorderInsets(Component c, Insets insets) { 283 insets.top = insets.left = insets.bottom = insets.right = 2; 284 return insets; 285 } 286 } 287 288 public static class RadioButtonBorder extends ButtonBorder { 289 290 public RadioButtonBorder(Color shadow, Color darkShadow, 291 Color highlight, Color lightHighlight) { 292 super(shadow, darkShadow, highlight, lightHighlight); 293 } 294 295 296 public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) { 297 298 if (c instanceof AbstractButton) { 299 AbstractButton b = (AbstractButton)c; 300 ButtonModel model = b.getModel(); 301 302 if (model.isArmed() && model.isPressed() || model.isSelected()) { 303 BasicGraphicsUtils.drawLoweredBezel(g, x, y, width, height, 304 shadow, darkShadow, 305 highlight, lightHighlight); 306 } else { 307 BasicGraphicsUtils.drawBezel(g, x, y, width, height, 308 false, b.isFocusPainted() && b.hasFocus(), 309 shadow, darkShadow, 310 highlight, lightHighlight); 311 } 312 } else { 313 BasicGraphicsUtils.drawBezel(g, x, y, width, height, false, false, 314 shadow, darkShadow, highlight, lightHighlight); 315 } 316 } 317 318 public Insets getBorderInsets(Component c) { 319 return getBorderInsets(c, new Insets (0,0,0,0)); 320 } 321 322 public Insets getBorderInsets(Component c, Insets insets) { 323 insets.top = insets.left = insets.bottom = insets.right = 2; 324 return insets; 325 } 326 } 327 328 public static class MenuBarBorder extends AbstractBorder implements UIResource { 329 private Color shadow; 330 private Color highlight; 331 332 public MenuBarBorder(Color shadow, Color highlight) { 333 this.shadow = shadow; 334 this.highlight = highlight; 335 } 336 337 public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) { 338 Color oldColor = g.getColor(); 339 g.translate(x, y); 340 g.setColor(shadow); 341 g.drawLine(0, height-2, width, height-2); 342 g.setColor(highlight); 343 g.drawLine(0, height-1, width, height-1); 344 g.translate(-x,-y); 345 g.setColor(oldColor); 346 } 347 348 public Insets getBorderInsets(Component c) { 349 return getBorderInsets(c, new Insets (0,0,0,0)); 350 } 351 352 public Insets getBorderInsets(Component c, Insets insets) { 353 insets.top = 0; 354 insets.left = 0; 355 insets.bottom = 2; 356 insets.right = 0; 357 return insets; 358 } 359 } 360 361 public static class MarginBorder extends AbstractBorder implements UIResource { 362 363 public Insets getBorderInsets(Component c) { 364 return getBorderInsets(c, new Insets (0,0,0,0)); 365 } 366 367 public Insets getBorderInsets(Component c, Insets insets) { 368 Insets margin = null; 369 if (c instanceof AbstractButton) { 375 AbstractButton b = (AbstractButton)c; 376 margin = b.getMargin(); 377 } else if (c instanceof JToolBar) { 378 JToolBar t = (JToolBar)c; 379 margin = t.getMargin(); 380 } else if (c instanceof JTextComponent ) { 381 JTextComponent t = (JTextComponent )c; 382 margin = t.getMargin(); 383 } 384 insets.top = margin != null? margin.top : 0; 385 insets.left = margin != null? margin.left : 0; 386 insets.bottom = margin != null? margin.bottom : 0; 387 insets.right = margin != null? margin.right : 0; 388 389 return insets; 390 } 391 } 392 393 public static class FieldBorder extends AbstractBorder implements UIResource { 394 protected Color shadow; 395 protected Color darkShadow; 396 protected Color highlight; 397 protected Color lightHighlight; 398 399 public FieldBorder(Color shadow, Color darkShadow, 400 Color highlight, Color lightHighlight) { 401 this.shadow = shadow; 402 this.highlight = highlight; 403 this.darkShadow = darkShadow; 404 this.lightHighlight = lightHighlight; 405 } 406 407 public void paintBorder(Component c, Graphics g, int x, int y, 408 int width, int height) { 409 BasicGraphicsUtils.drawEtchedRect(g, x, y, width, height, 410 shadow, darkShadow, 411 highlight, lightHighlight); 412 } 413 414 public Insets getBorderInsets(Component c) { 415 return getBorderInsets(c, new Insets (0,0,0,0)); 416 } 417 418 public Insets getBorderInsets(Component c, Insets insets) { 419 Insets margin = null; 420 if (c instanceof JTextComponent ) { 421 margin = ((JTextComponent )c).getMargin(); 422 } 423 insets.top = margin != null? 2+margin.top : 2; 424 insets.left = margin != null? 2+margin.left : 2; 425 insets.bottom = margin != null? 2+margin.bottom : 2; 426 insets.right = margin != null? 2+margin.right : 2; 427 428 return insets; 429 } 430 } 431 432 433 438 static class SplitPaneDividerBorder implements Border, UIResource { 439 Color highlight; 440 Color shadow; 441 442 SplitPaneDividerBorder(Color highlight, Color shadow) { 443 this.highlight = highlight; 444 this.shadow = shadow; 445 } 446 447 public void paintBorder(Component c, Graphics g, int x, int y, 448 int width, int height) { 449 Component child; 450 Rectangle cBounds; 451 JSplitPane splitPane = ((BasicSplitPaneDivider )c). 452 getBasicSplitPaneUI().getSplitPane(); 453 Dimension size = c.getSize(); 454 455 child = splitPane.getLeftComponent(); 456 g.setColor(c.getBackground()); 459 g.drawRect(x, y, width - 1, height - 1); 460 if(splitPane.getOrientation() == JSplitPane.HORIZONTAL_SPLIT) { 461 if(child != null) { 462 g.setColor(highlight); 463 g.drawLine(0, 0, 0, size.height); 464 } 465 child = splitPane.getRightComponent(); 466 if(child != null) { 467 g.setColor(shadow); 468 g.drawLine(size.width - 1, 0, size.width - 1, size.height); 469 } 470 } else { 471 if(child != null) { 472 g.setColor(highlight); 473 g.drawLine(0, 0, size.width, 0); 474 } 475 child = splitPane.getRightComponent(); 476 if(child != null) { 477 g.setColor(shadow); 478 g.drawLine(0, size.height - 1, size.width, 479 size.height - 1); 480 } 481 } 482 } 483 public Insets getBorderInsets(Component c) { 484 Insets insets = new Insets (0,0,0,0); 485 if (c instanceof BasicSplitPaneDivider ) { 486 BasicSplitPaneUI bspui = ((BasicSplitPaneDivider )c). 487 getBasicSplitPaneUI(); 488 489 if (bspui != null) { 490 JSplitPane splitPane = bspui.getSplitPane(); 491 492 if (splitPane != null) { 493 if (splitPane.getOrientation() == 494 JSplitPane.HORIZONTAL_SPLIT) { 495 insets.top = insets.bottom = 0; 496 insets.left = insets.right = 1; 497 return insets; 498 } 499 insets.top = insets.bottom = 1; 501 insets.left = insets.right = 0; 502 return insets; 503 } 504 } 505 } 506 insets.top = insets.bottom = insets.left = insets.right = 1; 507 return insets; 508 } 509 public boolean isBorderOpaque() { return true; } 510 } 511 512 513 517 public static class SplitPaneBorder implements Border, UIResource { 518 protected Color highlight; 519 protected Color shadow; 520 521 public SplitPaneBorder(Color highlight, Color shadow) { 522 this.highlight = highlight; 523 this.shadow = shadow; 524 } 525 526 public void paintBorder(Component c, Graphics g, int x, int y, 527 int width, int height) { 528 544 Component child; 545 Rectangle cBounds; 546 547 JSplitPane splitPane = (JSplitPane)c; 548 549 child = splitPane.getLeftComponent(); 550 g.setColor(c.getBackground()); 553 g.drawRect(x, y, width - 1, height - 1); 554 if(splitPane.getOrientation() == JSplitPane.HORIZONTAL_SPLIT) { 555 if(child != null) { 556 cBounds = child.getBounds(); 557 g.setColor(shadow); 558 g.drawLine(0, 0, cBounds.width + 1, 0); 559 g.drawLine(0, 1, 0, cBounds.height + 2); 560 561 g.setColor(highlight); 562 g.drawLine(1, cBounds.height + 1, cBounds.width + 1, 563 cBounds.height + 1); 564 } 565 child = splitPane.getRightComponent(); 566 if(child != null) { 567 cBounds = child.getBounds(); 568 569 int maxX = cBounds.x + cBounds.width; 570 int maxY = cBounds.y + cBounds.height; 571 572 g.setColor(shadow); 573 g.drawLine(cBounds.x - 1, 0, maxX, 0); 574 g.drawLine(cBounds.x - 1, maxY, cBounds.x, maxY); 575 g.setColor(highlight); 576 g.drawLine(cBounds.x, maxY, maxX, maxY); 577 g.drawLine(maxX, 0, maxX, maxY + 1); 578 } 579 } else { 580 if(child != null) { 581 cBounds = child.getBounds(); 582 g.setColor(shadow); 583 g.drawLine(0, 0, cBounds.width + 1, 0); 584 g.drawLine(0, 1, 0, cBounds.height); 585 g.setColor(highlight); 586 g.drawLine(1 + cBounds.width, 0, 1 + cBounds.width, 587 cBounds.height + 1); 588 g.drawLine(0, cBounds.height + 1, 0, cBounds.height + 1); 589 } 590 child = splitPane.getRightComponent(); 591 if(child != null) { 592 cBounds = child.getBounds(); 593 594 int maxX = cBounds.x + cBounds.width; 595 int maxY = cBounds.y + cBounds.height; 596 597 g.setColor(shadow); 598 g.drawLine(0, cBounds.y - 1, 0, maxY); 599 g.drawLine(maxX, cBounds.y - 1, maxX, cBounds.y - 1); 600 g.setColor(highlight); 601 g.drawLine(0, maxY, cBounds.width + 1, maxY); 602 g.drawLine(maxX, cBounds.y, maxX, maxY); 603 } 604 } 605 } 606 public Insets getBorderInsets(Component c) { 607 return new Insets (1, 1, 1, 1); 608 } 609 public boolean isBorderOpaque() { return true; } 610 } 611 612 } 613 | Popular Tags |