1 7 8 package javax.swing.plaf.metal; 9 10 import java.awt.Component ; 11 import java.awt.Container ; 12 import java.awt.LayoutManager ; 13 import java.awt.Adjustable ; 14 import java.awt.event.AdjustmentListener ; 15 import java.awt.event.AdjustmentEvent ; 16 import java.awt.event.ActionListener ; 17 import java.awt.event.ActionEvent ; 18 import java.awt.event.MouseListener ; 19 import java.awt.event.MouseMotionListener ; 20 import java.awt.event.MouseAdapter ; 21 import java.awt.event.MouseEvent ; 22 import java.awt.Graphics ; 23 import java.awt.Dimension ; 24 import java.awt.Rectangle ; 25 import java.awt.Point ; 26 import java.awt.Insets ; 27 import java.awt.Color ; 28 import java.awt.IllegalComponentStateException ; 29 30 import java.beans.*; 31 32 import javax.swing.*; 33 import javax.swing.event.*; 34 35 import javax.swing.plaf.*; 36 import javax.swing.plaf.basic.BasicScrollBarUI ; 37 38 39 47 public class MetalScrollBarUI extends BasicScrollBarUI 48 { 49 private static Color shadowColor; 50 private static Color highlightColor; 51 private static Color darkShadowColor; 52 private static Color thumbColor; 53 private static Color thumbShadow; 54 private static Color thumbHighlightColor; 55 56 57 protected MetalBumps bumps; 58 59 protected MetalScrollButton increaseButton; 60 protected MetalScrollButton decreaseButton; 61 62 protected int scrollBarWidth; 63 64 public static final String FREE_STANDING_PROP = "JScrollBar.isFreeStanding"; 65 protected boolean isFreeStanding = true; 66 67 public static ComponentUI createUI( JComponent c ) 68 { 69 return new MetalScrollBarUI (); 70 } 71 72 protected void installDefaults() { 73 scrollBarWidth = ((Integer )(UIManager.get( "ScrollBar.width" ))).intValue(); 74 super.installDefaults(); 75 bumps = new MetalBumps ( 10, 10, thumbHighlightColor, thumbShadow, thumbColor ); 76 } 77 78 protected void installListeners(){ 79 super.installListeners(); 80 ((ScrollBarListener)propertyChangeListener).handlePropertyChange( scrollbar.getClientProperty( FREE_STANDING_PROP ) ); 81 } 82 83 protected PropertyChangeListener createPropertyChangeListener(){ 84 return new ScrollBarListener(); 85 } 86 87 protected void configureScrollBarColors() 88 { 89 super.configureScrollBarColors(); 90 shadowColor = UIManager.getColor("ScrollBar.shadow"); 91 highlightColor = UIManager.getColor("ScrollBar.highlight"); 92 darkShadowColor = UIManager.getColor("ScrollBar.darkShadow"); 93 thumbColor = UIManager.getColor("ScrollBar.thumb"); 94 thumbShadow = UIManager.getColor("ScrollBar.thumbShadow"); 95 thumbHighlightColor = UIManager.getColor("ScrollBar.thumbHighlight"); 96 97 98 } 99 100 public Dimension getPreferredSize( JComponent c ) 101 { 102 if ( scrollbar.getOrientation() == JScrollBar.VERTICAL ) 103 { 104 return new Dimension ( scrollBarWidth, scrollBarWidth * 3 + 10 ); 105 } 106 else { 108 return new Dimension ( scrollBarWidth * 3 + 10, scrollBarWidth ); 109 } 110 111 } 112 113 115 protected JButton createDecreaseButton( int orientation ) 116 { 117 decreaseButton = new MetalScrollButton ( orientation, scrollBarWidth, isFreeStanding ); 118 return decreaseButton; 119 } 120 121 122 protected JButton createIncreaseButton( int orientation ) 123 { 124 increaseButton = new MetalScrollButton ( orientation, scrollBarWidth, isFreeStanding ); 125 return increaseButton; 126 } 127 128 protected void paintTrack( Graphics g, JComponent c, Rectangle trackBounds ) 129 { 130 g.translate( trackBounds.x, trackBounds.y ); 131 132 boolean leftToRight = MetalUtils.isLeftToRight(c); 133 134 if ( scrollbar.getOrientation() == JScrollBar.VERTICAL ) 135 { 136 if ( !isFreeStanding ) { 137 trackBounds.width += 2; 138 if ( !leftToRight ) { 139 g.translate( -1, 0 ); 140 } 141 } 142 143 if ( c.isEnabled() ) { 144 g.setColor( darkShadowColor ); 145 g.drawLine( 0, 0, 0, trackBounds.height - 1 ); 146 g.drawLine( trackBounds.width - 2, 0, trackBounds.width - 2, trackBounds.height - 1 ); 147 g.drawLine( 2, trackBounds.height - 1, trackBounds.width - 1, trackBounds.height - 1); 148 g.drawLine( 2, 0, trackBounds.width - 2, 0 ); 149 150 g.setColor( shadowColor ); 151 g.drawLine( 1, 1, 1, trackBounds.height - 2 ); 153 g.drawLine( 1, 1, trackBounds.width - 3, 1 ); 154 if (scrollbar.getValue() != scrollbar.getMaximum()) { int y = thumbRect.y + thumbRect.height - trackBounds.y; 156 g.drawLine( 1, y, trackBounds.width-1, y); 157 } 158 g.setColor(highlightColor); 159 g.drawLine( trackBounds.width - 1, 0, trackBounds.width - 1, trackBounds.height - 1 ); 160 } else { 161 MetalUtils.drawDisabledBorder(g, 0, 0, trackBounds.width, trackBounds.height ); 162 } 163 164 if ( !isFreeStanding ) { 165 trackBounds.width -= 2; 166 if ( !leftToRight ) { 167 g.translate( 1, 0 ); 168 } 169 } 170 } 171 else { 173 if ( !isFreeStanding ) { 174 trackBounds.height += 2; 175 } 176 177 if ( c.isEnabled() ) { 178 g.setColor( darkShadowColor ); 179 g.drawLine( 0, 0, trackBounds.width - 1, 0 ); g.drawLine( 0, 2, 0, trackBounds.height - 2 ); g.drawLine( 0, trackBounds.height - 2, trackBounds.width - 1, trackBounds.height - 2 ); g.drawLine( trackBounds.width - 1, 2, trackBounds.width - 1, trackBounds.height - 1 ); 184 g.setColor( shadowColor ); 185 g.drawLine( 1, 1, trackBounds.width - 2, 1 ); g.drawLine( 1, 1, 1, trackBounds.height - 3 ); g.drawLine( 0, trackBounds.height - 1, trackBounds.width - 1, trackBounds.height - 1 ); if (scrollbar.getValue() != scrollbar.getMaximum()) { int x = thumbRect.x + thumbRect.width - trackBounds.x; 191 g.drawLine( x, 1, x, trackBounds.height-1); 192 } 193 } else { 194 MetalUtils.drawDisabledBorder(g, 0, 0, trackBounds.width, trackBounds.height ); 195 } 196 197 if ( !isFreeStanding ) { 198 trackBounds.height -= 2; 199 } 200 } 201 202 g.translate( -trackBounds.x, -trackBounds.y ); 203 } 204 205 protected void paintThumb( Graphics g, JComponent c, Rectangle thumbBounds ) 206 { 207 if (!c.isEnabled()) { 208 return; 209 } 210 211 if (MetalLookAndFeel.usingOcean()) { 212 oceanPaintThumb(g, c, thumbBounds); 213 return; 214 } 215 216 boolean leftToRight = MetalUtils.isLeftToRight(c); 217 218 g.translate( thumbBounds.x, thumbBounds.y ); 219 220 if ( scrollbar.getOrientation() == JScrollBar.VERTICAL ) 221 { 222 if ( !isFreeStanding ) { 223 thumbBounds.width += 2; 224 if ( !leftToRight ) { 225 g.translate( -1, 0 ); 226 } 227 } 228 229 g.setColor( thumbColor ); 230 g.fillRect( 0, 0, thumbBounds.width - 2, thumbBounds.height - 1 ); 231 232 g.setColor( thumbShadow ); 233 g.drawRect( 0, 0, thumbBounds.width - 2, thumbBounds.height - 1 ); 234 235 g.setColor( thumbHighlightColor ); 236 g.drawLine( 1, 1, thumbBounds.width - 3, 1 ); 237 g.drawLine( 1, 1, 1, thumbBounds.height - 2 ); 238 239 bumps.setBumpArea( thumbBounds.width - 6, thumbBounds.height - 7 ); 240 bumps.paintIcon( c, g, 3, 4 ); 241 242 if ( !isFreeStanding ) { 243 thumbBounds.width -= 2; 244 if ( !leftToRight ) { 245 g.translate( 1, 0 ); 246 } 247 } 248 } 249 else { 251 if ( !isFreeStanding ) { 252 thumbBounds.height += 2; 253 } 254 255 g.setColor( thumbColor ); 256 g.fillRect( 0, 0, thumbBounds.width - 1, thumbBounds.height - 2 ); 257 258 g.setColor( thumbShadow ); 259 g.drawRect( 0, 0, thumbBounds.width - 1, thumbBounds.height - 2 ); 260 261 g.setColor( thumbHighlightColor ); 262 g.drawLine( 1, 1, thumbBounds.width - 3, 1 ); 263 g.drawLine( 1, 1, 1, thumbBounds.height - 3 ); 264 265 bumps.setBumpArea( thumbBounds.width - 7, thumbBounds.height - 6 ); 266 bumps.paintIcon( c, g, 4, 3 ); 267 268 if ( !isFreeStanding ) { 269 thumbBounds.height -= 2; 270 } 271 } 272 273 g.translate( -thumbBounds.x, -thumbBounds.y ); 274 } 275 276 private void oceanPaintThumb(Graphics g, JComponent c, 277 Rectangle thumbBounds) { 278 boolean leftToRight = MetalUtils.isLeftToRight(c); 279 280 g.translate(thumbBounds.x, thumbBounds.y); 281 282 if (scrollbar.getOrientation() == JScrollBar.VERTICAL) { 283 if (!isFreeStanding) { 284 thumbBounds.width += 2; 285 if (!leftToRight) { 286 g.translate(-1, 0); 287 } 288 } 289 290 if (thumbColor != null) { 291 g.setColor(thumbColor); 292 g.fillRect(0, 0, thumbBounds.width - 2,thumbBounds.height - 1); 293 } 294 295 g.setColor(thumbShadow); 296 g.drawRect(0, 0, thumbBounds.width - 2, thumbBounds.height - 1); 297 298 g.setColor(thumbHighlightColor); 299 g.drawLine(1, 1, thumbBounds.width - 3, 1); 300 g.drawLine(1, 1, 1, thumbBounds.height - 2); 301 302 MetalUtils.drawGradient(c, g, "ScrollBar.gradient", 2, 2, 303 thumbBounds.width - 4, 304 thumbBounds.height - 3, false); 305 306 int gripSize = thumbBounds.width - 8; 307 if (gripSize > 2 && thumbBounds.height >= 10) { 308 g.setColor(MetalLookAndFeel.getPrimaryControlDarkShadow()); 309 int gripY = thumbBounds.height / 2 - 2; 310 for (int counter = 0; counter < 6; counter += 2) { 311 g.fillRect(4, counter + gripY, gripSize, 1); 312 } 313 314 g.setColor(MetalLookAndFeel.getWhite()); 315 gripY++; 316 for (int counter = 0; counter < 6; counter += 2) { 317 g.fillRect(5, counter + gripY, gripSize, 1); 318 } 319 } 320 if (!isFreeStanding) { 321 thumbBounds.width -= 2; 322 if (!leftToRight) { 323 g.translate(1, 0); 324 } 325 } 326 } 327 else { if (!isFreeStanding) { 329 thumbBounds.height += 2; 330 } 331 332 if (thumbColor != null) { 333 g.setColor(thumbColor); 334 g.fillRect(0, 0, thumbBounds.width - 1,thumbBounds.height - 2); 335 } 336 337 g.setColor(thumbShadow); 338 g.drawRect(0, 0, thumbBounds.width - 1, thumbBounds.height - 2); 339 340 g.setColor(thumbHighlightColor); 341 g.drawLine(1, 1, thumbBounds.width - 2, 1); 342 g.drawLine(1, 1, 1, thumbBounds.height - 3); 343 344 MetalUtils.drawGradient(c, g, "ScrollBar.gradient", 2, 2, 345 thumbBounds.width - 3, 346 thumbBounds.height - 4, true); 347 348 int gripSize = thumbBounds.height - 8; 349 if (gripSize > 2 && thumbBounds.width >= 10) { 350 g.setColor(MetalLookAndFeel.getPrimaryControlDarkShadow()); 351 int gripX = thumbBounds.width / 2 - 2; 352 for (int counter = 0; counter < 6; counter += 2) { 353 g.fillRect(gripX + counter, 4, 1, gripSize); 354 } 355 356 g.setColor(MetalLookAndFeel.getWhite()); 357 gripX++; 358 for (int counter = 0; counter < 6; counter += 2) { 359 g.fillRect(gripX + counter, 5, 1, gripSize); 360 } 361 } 362 363 if (!isFreeStanding) { 364 thumbBounds.height -= 2; 365 } 366 } 367 368 g.translate( -thumbBounds.x, -thumbBounds.y ); 369 } 370 371 protected Dimension getMinimumThumbSize() 372 { 373 return new Dimension ( scrollBarWidth, scrollBarWidth ); 374 } 375 376 380 protected void setThumbBounds(int x, int y, int width, int height) 381 { 382 384 if ((thumbRect.x == x) && 385 (thumbRect.y == y) && 386 (thumbRect.width == width) && 387 (thumbRect.height == height)) { 388 return; 389 } 390 391 394 int minX = Math.min(x, thumbRect.x); 395 int minY = Math.min(y, thumbRect.y); 396 int maxX = Math.max(x + width, thumbRect.x + thumbRect.width); 397 int maxY = Math.max(y + height, thumbRect.y + thumbRect.height); 398 399 thumbRect.setBounds(x, y, width, height); 400 scrollbar.repaint(minX, minY, (maxX - minX)+1, (maxY - minY)+1); 401 } 402 403 404 405 class ScrollBarListener extends BasicScrollBarUI.PropertyChangeHandler 406 { 407 public void propertyChange(PropertyChangeEvent e) 408 { 409 String name = e.getPropertyName(); 410 if ( name.equals( FREE_STANDING_PROP ) ) 411 { 412 handlePropertyChange( e.getNewValue() ); 413 } 414 else { 415 super.propertyChange( e ); 416 } 417 } 418 419 public void handlePropertyChange( Object newValue ) 420 { 421 if ( newValue != null ) 422 { 423 boolean temp = ((Boolean )newValue).booleanValue(); 424 boolean becameFlush = temp == false && isFreeStanding == true; 425 boolean becameNormal = temp == true && isFreeStanding == false; 426 427 isFreeStanding = temp; 428 429 if ( becameFlush ) { 430 toFlush(); 431 } 432 else if ( becameNormal ) { 433 toFreeStanding(); 434 } 435 } 436 else 437 { 438 439 if ( !isFreeStanding ) { 440 isFreeStanding = true; 441 toFreeStanding(); 442 } 443 444 451 } 452 453 if ( increaseButton != null ) 454 { 455 increaseButton.setFreeStanding( isFreeStanding ); 456 } 457 if ( decreaseButton != null ) 458 { 459 decreaseButton.setFreeStanding( isFreeStanding ); 460 } 461 } 462 463 protected void toFlush() { 464 scrollBarWidth -= 2; 465 } 466 467 protected void toFreeStanding() { 468 scrollBarWidth += 2; 469 } 470 } } 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 | Popular Tags |