FileDocCategorySizeDatePackage
MetalScrollBarUI.javaAPI DocJava SE 5 API14460Fri Aug 26 14:58:08 BST 2005javax.swing.plaf.metal

MetalScrollBarUI

public class MetalScrollBarUI extends BasicScrollBarUI
Implementation of ScrollBarUI for the Metal Look and Feel

version
1.21 08/26/98
author
Tom Santos
author
Steve Wilson

Fields Summary
private static Color
shadowColor
private static Color
highlightColor
private static Color
darkShadowColor
private static Color
thumbColor
private static Color
thumbShadow
private static Color
thumbHighlightColor
protected MetalBumps
bumps
protected MetalScrollButton
increaseButton
protected MetalScrollButton
decreaseButton
protected int
scrollBarWidth
public static final String
FREE_STANDING_PROP
protected boolean
isFreeStanding
Constructors Summary
Methods Summary
protected voidconfigureScrollBarColors()

        super.configureScrollBarColors();
        shadowColor         = UIManager.getColor("ScrollBar.shadow");
        highlightColor      = UIManager.getColor("ScrollBar.highlight");
        darkShadowColor     = UIManager.getColor("ScrollBar.darkShadow");
        thumbColor          = UIManager.getColor("ScrollBar.thumb");
        thumbShadow         = UIManager.getColor("ScrollBar.thumbShadow");
        thumbHighlightColor = UIManager.getColor("ScrollBar.thumbHighlight");


    
protected javax.swing.JButtoncreateDecreaseButton(int orientation)
Returns the view that represents the decrease view.

        decreaseButton = new MetalScrollButton( orientation, scrollBarWidth, isFreeStanding );
	return decreaseButton;
    
protected javax.swing.JButtoncreateIncreaseButton(int orientation)
Returns the view that represents the increase view.

        increaseButton =  new MetalScrollButton( orientation, scrollBarWidth, isFreeStanding );
	return increaseButton;
    
protected java.beans.PropertyChangeListenercreatePropertyChangeListener()

	return new ScrollBarListener();    
    
public static javax.swing.plaf.ComponentUIcreateUI(javax.swing.JComponent c)


          
    
        return new MetalScrollBarUI();
    
protected java.awt.DimensiongetMinimumThumbSize()

        return new Dimension( scrollBarWidth, scrollBarWidth );
    
public java.awt.DimensiongetPreferredSize(javax.swing.JComponent c)

        if ( scrollbar.getOrientation() == JScrollBar.VERTICAL )
	{
	    return new Dimension( scrollBarWidth, scrollBarWidth * 3 + 10 );
        }
	else  // Horizontal
	{
            return new Dimension( scrollBarWidth * 3 + 10, scrollBarWidth );
        }

    
protected voidinstallDefaults()

	scrollBarWidth = ((Integer)(UIManager.get( "ScrollBar.width" ))).intValue();
	super.installDefaults();
	bumps = new MetalBumps( 10, 10, thumbHighlightColor, thumbShadow, thumbColor );    
    
protected voidinstallListeners()

	super.installListeners();
	((ScrollBarListener)propertyChangeListener).handlePropertyChange( scrollbar.getClientProperty( FREE_STANDING_PROP ) );
    
private voidoceanPaintThumb(java.awt.Graphics g, javax.swing.JComponent c, java.awt.Rectangle thumbBounds)

        boolean leftToRight = MetalUtils.isLeftToRight(c);

        g.translate(thumbBounds.x, thumbBounds.y);

        if (scrollbar.getOrientation() == JScrollBar.VERTICAL) {
            if (!isFreeStanding) {
                thumbBounds.width += 2;
                if (!leftToRight) {
                    g.translate(-1, 0);
                }
            }

            if (thumbColor != null) {
                g.setColor(thumbColor);
                g.fillRect(0, 0, thumbBounds.width - 2,thumbBounds.height - 1);
            }

            g.setColor(thumbShadow);
            g.drawRect(0, 0, thumbBounds.width - 2, thumbBounds.height - 1);

            g.setColor(thumbHighlightColor);
            g.drawLine(1, 1, thumbBounds.width - 3, 1);
            g.drawLine(1, 1, 1, thumbBounds.height - 2);

            MetalUtils.drawGradient(c, g, "ScrollBar.gradient", 2, 2,
                                    thumbBounds.width - 4,
                                    thumbBounds.height - 3, false);

            int gripSize = thumbBounds.width - 8;
            if (gripSize > 2 && thumbBounds.height >= 10) {
                g.setColor(MetalLookAndFeel.getPrimaryControlDarkShadow());
                int gripY = thumbBounds.height / 2 - 2;
                for (int counter = 0; counter < 6; counter += 2) {
                    g.fillRect(4, counter + gripY, gripSize, 1);
                }

                g.setColor(MetalLookAndFeel.getWhite());
                gripY++;
                for (int counter = 0; counter < 6; counter += 2) {
                    g.fillRect(5, counter + gripY, gripSize, 1);
                }
            }
            if (!isFreeStanding) {
                thumbBounds.width -= 2;
                if (!leftToRight) {
                    g.translate(1, 0);
                }
            }
        }
        else { // HORIZONTAL
            if (!isFreeStanding) {
                thumbBounds.height += 2;
            }

            if (thumbColor != null) {
                g.setColor(thumbColor);
                g.fillRect(0, 0, thumbBounds.width - 1,thumbBounds.height - 2);
            }

            g.setColor(thumbShadow);
            g.drawRect(0, 0, thumbBounds.width - 1, thumbBounds.height - 2);

            g.setColor(thumbHighlightColor);
            g.drawLine(1, 1, thumbBounds.width - 2, 1);
            g.drawLine(1, 1, 1, thumbBounds.height - 3);

            MetalUtils.drawGradient(c, g, "ScrollBar.gradient", 2, 2,
                                    thumbBounds.width - 3,
                                    thumbBounds.height - 4, true);

            int gripSize = thumbBounds.height - 8;
            if (gripSize > 2 && thumbBounds.width >= 10) {
                g.setColor(MetalLookAndFeel.getPrimaryControlDarkShadow());
                int gripX = thumbBounds.width / 2 - 2;
                for (int counter = 0; counter < 6; counter += 2) {
                    g.fillRect(gripX + counter, 4, 1, gripSize);
                }

                g.setColor(MetalLookAndFeel.getWhite());
                gripX++;
                for (int counter = 0; counter < 6; counter += 2) {
                    g.fillRect(gripX + counter, 5, 1, gripSize);
                }
            }

            if (!isFreeStanding) {
                thumbBounds.height -= 2;
            }
        }

        g.translate( -thumbBounds.x, -thumbBounds.y );
    
protected voidpaintThumb(java.awt.Graphics g, javax.swing.JComponent c, java.awt.Rectangle thumbBounds)

        if (!c.isEnabled()) {
	    return;
	}

        if (MetalLookAndFeel.usingOcean()) {
            oceanPaintThumb(g, c, thumbBounds);
            return;
        }

        boolean leftToRight = MetalUtils.isLeftToRight(c);

        g.translate( thumbBounds.x, thumbBounds.y );

	if ( scrollbar.getOrientation() == JScrollBar.VERTICAL )
	{
	    if ( !isFreeStanding ) {
                thumbBounds.width += 2;
                if ( !leftToRight ) {
                    g.translate( -1, 0 );
                }
	    }

	    g.setColor( thumbColor );
	    g.fillRect( 0, 0, thumbBounds.width - 2, thumbBounds.height - 1 );

	    g.setColor( thumbShadow );
	    g.drawRect( 0, 0, thumbBounds.width - 2, thumbBounds.height - 1 );
	
	    g.setColor( thumbHighlightColor );
	    g.drawLine( 1, 1, thumbBounds.width - 3, 1 );
	    g.drawLine( 1, 1, 1, thumbBounds.height - 2 );

	    bumps.setBumpArea( thumbBounds.width - 6, thumbBounds.height - 7 );
	    bumps.paintIcon( c, g, 3, 4 );

	    if ( !isFreeStanding ) {
                thumbBounds.width -= 2;
                if ( !leftToRight ) {
                    g.translate( 1, 0 );
                }
	    }
	}
	else  // HORIZONTAL
	{
	    if ( !isFreeStanding ) {
	        thumbBounds.height += 2;
	    }

	    g.setColor( thumbColor );
	    g.fillRect( 0, 0, thumbBounds.width - 1, thumbBounds.height - 2 );

	    g.setColor( thumbShadow );
	    g.drawRect( 0, 0, thumbBounds.width - 1, thumbBounds.height - 2 );

	    g.setColor( thumbHighlightColor );
	    g.drawLine( 1, 1, thumbBounds.width - 3, 1 );
	    g.drawLine( 1, 1, 1, thumbBounds.height - 3 );

	    bumps.setBumpArea( thumbBounds.width - 7, thumbBounds.height - 6 );
	    bumps.paintIcon( c, g, 4, 3 );

	    if ( !isFreeStanding ) {
	        thumbBounds.height -= 2;
	    }
	}

        g.translate( -thumbBounds.x, -thumbBounds.y );
    
protected voidpaintTrack(java.awt.Graphics g, javax.swing.JComponent c, java.awt.Rectangle trackBounds)

        g.translate( trackBounds.x, trackBounds.y );

        boolean leftToRight = MetalUtils.isLeftToRight(c);

	if ( scrollbar.getOrientation() == JScrollBar.VERTICAL )
	{
	    if ( !isFreeStanding ) {
                trackBounds.width += 2;
                if ( !leftToRight ) {
                    g.translate( -1, 0 );
                }
	    }

	    if ( c.isEnabled() ) {
	        g.setColor( darkShadowColor );
		g.drawLine( 0, 0, 0, trackBounds.height - 1 );
		g.drawLine( trackBounds.width - 2, 0, trackBounds.width - 2, trackBounds.height - 1 );
		g.drawLine( 2, trackBounds.height - 1, trackBounds.width - 1, trackBounds.height - 1);
		g.drawLine( 2, 0, trackBounds.width - 2, 0 );

		g.setColor( shadowColor );
		//	g.setColor( Color.red);
		g.drawLine( 1, 1, 1, trackBounds.height - 2 );
		g.drawLine( 1, 1, trackBounds.width - 3, 1 );
		if (scrollbar.getValue() != scrollbar.getMaximum()) {  // thumb shadow
		    int y = thumbRect.y + thumbRect.height - trackBounds.y;
		    g.drawLine( 1, y, trackBounds.width-1, y);
		}
		g.setColor(highlightColor);
		g.drawLine( trackBounds.width - 1, 0, trackBounds.width - 1, trackBounds.height - 1 );
	    } else {
		MetalUtils.drawDisabledBorder(g, 0, 0, trackBounds.width, trackBounds.height );
	    }

	    if ( !isFreeStanding ) {
                trackBounds.width -= 2;
                if ( !leftToRight ) {
                    g.translate( 1, 0 );
                }
	    }
	}
	else  // HORIZONTAL
	{
	    if ( !isFreeStanding ) {
	        trackBounds.height += 2;
	    }

	    if ( c.isEnabled() ) {
	        g.setColor( darkShadowColor );
		g.drawLine( 0, 0, trackBounds.width - 1, 0 );  // top
		g.drawLine( 0, 2, 0, trackBounds.height - 2 ); // left
		g.drawLine( 0, trackBounds.height - 2, trackBounds.width - 1, trackBounds.height - 2 ); // bottom
		g.drawLine( trackBounds.width - 1, 2, trackBounds.width - 1, trackBounds.height - 1 ); // right

		g.setColor( shadowColor );
		//	g.setColor( Color.red);
		g.drawLine( 1, 1, trackBounds.width - 2, 1 );  // top
		g.drawLine( 1, 1, 1, trackBounds.height - 3 ); // left
		g.drawLine( 0, trackBounds.height - 1, trackBounds.width - 1, trackBounds.height - 1 ); // bottom
		if (scrollbar.getValue() != scrollbar.getMaximum()) {  // thumb shadow
		    int x = thumbRect.x + thumbRect.width - trackBounds.x;
		    g.drawLine( x, 1, x, trackBounds.height-1);
		}
	    } else {
	        MetalUtils.drawDisabledBorder(g, 0, 0, trackBounds.width, trackBounds.height );
	    }

	    if ( !isFreeStanding ) {
	        trackBounds.height -= 2;
	    }
	}

        g.translate( -trackBounds.x, -trackBounds.y );
    
protected voidsetThumbBounds(int x, int y, int width, int height)
This is overridden only to increase the invalid area. This ensures that the "Shadow" below the thumb is invalidated

	/* If the thumbs bounds haven't changed, we're done.
	 */
	if ((thumbRect.x == x) && 
	    (thumbRect.y == y) && 
	    (thumbRect.width == width) && 
	    (thumbRect.height == height)) {
	    return;
	}

	/* Update thumbRect, and repaint the union of x,y,w,h and 
	 * the old thumbRect.
	 */
	int minX = Math.min(x, thumbRect.x);
	int minY = Math.min(y, thumbRect.y);
	int maxX = Math.max(x + width, thumbRect.x + thumbRect.width);
	int maxY = Math.max(y + height, thumbRect.y + thumbRect.height);

	thumbRect.setBounds(x, y, width, height);
	scrollbar.repaint(minX, minY, (maxX - minX)+1, (maxY - minY)+1);