FileDocCategorySizeDatePackage
MetalToolBarUI.javaAPI DocJava SE 6 API12273Tue Jun 10 00:26:50 BST 2008javax.swing.plaf.metal

MetalToolBarUI

public class MetalToolBarUI extends BasicToolBarUI
A Metal Look and Feel implementation of ToolBarUI. This implementation is a "combined" view/controller.

version
1.42 11/30/05
author
Jeff Shapiro

Fields Summary
private static List
components
An array of WeakReferences that point to JComponents. This will contain instances of JToolBars and JMenuBars and is used to find JToolBars/JMenuBars that border each other.
protected ContainerListener
contListener
This protected field is implemenation specific. Do not access directly or override. Use the create method instead.
protected PropertyChangeListener
rolloverListener
This protected field is implemenation specific. Do not access directly or override. Use the create method instead.
private static Border
nonRolloverBorder
private JMenuBar
lastMenuBar
Last menubar the toolbar touched. This is only useful for ocean.
Constructors Summary
Methods Summary
protected java.awt.event.ContainerListenercreateContainerListener()
Creates a container listener that will be added to the JToolBar. If this method returns null then it will not be added to the toolbar.

return
an instance of a ContainerListener or null

	return null;
    
protected javax.swing.event.MouseInputListenercreateDockingListener()

	return new MetalDockingListener( toolBar );
    
protected javax.swing.border.BordercreateNonRolloverBorder()

        return super.createNonRolloverBorder();
    
private javax.swing.border.BordercreateNonRolloverToggleBorder()
Creates a non rollover border for Toggle buttons in the toolbar.

	return createNonRolloverBorder();
    
protected javax.swing.border.BordercreateRolloverBorder()

	return super.createRolloverBorder();
    
protected java.beans.PropertyChangeListenercreateRolloverListener()
Creates a property change listener that will be added to the JToolBar. If this method returns null then it will not be added to the toolbar.

return
an instance of a PropertyChangeListener or null

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

	return new MetalToolBarUI();
    
static booleandoesMenuBarBorderToolBar(javax.swing.JMenuBar c)
Returns true if the passed in JMenuBar is above a horizontal JToolBar.

        JToolBar tb = (JToolBar)MetalToolBarUI.
                    findRegisteredComponentOfType(c, JToolBar.class);
        if (tb != null && tb.getOrientation() == JToolBar.HORIZONTAL) {
            JRootPane rp = SwingUtilities.getRootPane(c);
            Point point = new Point(0, 0);
            point = SwingUtilities.convertPoint(c, point, rp);
            int menuX = point.x;
            int menuY = point.y;
            point.x = point.y = 0;
            point = SwingUtilities.convertPoint(tb, point, rp);
            return (point.x == menuX && menuY + c.getHeight() == point.y &&
                    c.getWidth() == tb.getWidth());
        }
        return false;
    
static synchronized java.lang.ObjectfindRegisteredComponentOfType(javax.swing.JComponent from, java.lang.Class target)
Finds a previously registered component of class target that shares the JRootPane ancestor of from.

        JRootPane rp = SwingUtilities.getRootPane(from);
        if (rp != null) {
            for (int counter = components.size() - 1; counter >= 0; counter--){
                Object component = ((WeakReference)components.get(counter)).
                                   get();

                if (component == null) {
                    // WeakReference has gone away, remove the WeakReference
                    components.remove(counter);
                }
                else if (target.isInstance(component) && SwingUtilities.
                         getRootPane((Component)component) == rp) {
                    return component;
                }
            }
        }
        return null;
    
protected voidinstallListeners()

        super.installListeners();

	contListener = createContainerListener();
	if (contListener != null) {
	    toolBar.addContainerListener(contListener);
	}
	rolloverListener = createRolloverListener();
	if (rolloverListener != null) {
	    toolBar.addPropertyChangeListener(rolloverListener);
	}
    
public voidinstallUI(javax.swing.JComponent c)

        super.installUI( c );
        register(c);
    
static synchronized voidregister(javax.swing.JComponent c)
Registers the specified component.


             
         
        if (c == null) {
            // Exception is thrown as convenience for callers that are
            // typed to throw an NPE.
            throw new NullPointerException("JComponent must be non-null");
        }
        components.add(new WeakReference(c));
    
protected voidsetBorderToNonRollover(java.awt.Component c)

	if (c instanceof JToggleButton && !(c instanceof JCheckBox)) {
	    // 4735514, 4886944: The method createNonRolloverToggleBorder() is
	    // private in BasicToolBarUI so we can't override it. We still need
	    // to call super from this method so that it can save away the
	    // original border and then we install ours.

	    // Before calling super we get a handle to the old border, because
	    // super will install a non-UIResource border that we can't
	    // distinguish from one provided by an application.
	    JToggleButton b = (JToggleButton)c;
	    Border border = b.getBorder();
	    super.setBorderToNonRollover(c);
	    if (border instanceof UIResource) {
		if (nonRolloverBorder == null) {
		    nonRolloverBorder = createNonRolloverToggleBorder();
		}
		b.setBorder(nonRolloverBorder);
	    }
	} else {
	    super.setBorderToNonRollover(c);
	}
    
protected voidsetDragOffset(java.awt.Point p)

	if (!GraphicsEnvironment.isHeadless()) {
	    if (dragWindow == null) {
		dragWindow = createDragWindow(toolBar);
	    }
	    dragWindow.setOffset(p);
	}
    
private voidsetLastMenuBar(javax.swing.JMenuBar lastMenuBar)

        if (MetalLookAndFeel.usingOcean()) {
            if (this.lastMenuBar != lastMenuBar) {
                // The menubar we previously touched has changed, force it
                // to repaint.
                if (this.lastMenuBar != null) {
                    this.lastMenuBar.repaint();
                }
                if (lastMenuBar != null) {
                    lastMenuBar.repaint();
                }
                this.lastMenuBar = lastMenuBar;
            }
        }
    
protected voiduninstallListeners()

        super.uninstallListeners();

	if (contListener != null) {
	    toolBar.removeContainerListener(contListener);
	}
	rolloverListener = createRolloverListener();
	if (rolloverListener != null) {
	    toolBar.removePropertyChangeListener(rolloverListener);
	}
    
public voiduninstallUI(javax.swing.JComponent c)

        super.uninstallUI( c );
	nonRolloverBorder = null;
        unregister(c);
    
static synchronized voidunregister(javax.swing.JComponent c)
Unregisters the specified component.

        for (int counter = components.size() - 1; counter >= 0; counter--) {
            // Search for the component, removing any flushed references
            // along the way.
            WeakReference ref = (WeakReference)components.get(counter);
            Object target = ((WeakReference)components.get(counter)).get();

            if (target == c || target == null) {
                components.remove(counter);
            }
        }
    
public voidupdate(java.awt.Graphics g, javax.swing.JComponent c)
If necessary paints the background of the component, then invokes paint.

param
g Graphics to paint to
param
c JComponent painting on
throws
NullPointerException if g or c is null
see
javax.swing.plaf.ComponentUI#update
see
javax.swing.plaf.ComponentUI#paint
since
1.5

        if (g == null) {
            throw new NullPointerException("graphics must be non-null");
        }
        if (c.isOpaque() && (c.getBackground() instanceof UIResource) &&
                            ((JToolBar)c).getOrientation() ==
                      JToolBar.HORIZONTAL && UIManager.get(
                     "MenuBar.gradient") != null) {
            JRootPane rp = SwingUtilities.getRootPane(c);
            JMenuBar mb = (JMenuBar)findRegisteredComponentOfType(
                                    c, JMenuBar.class);
            if (mb != null && mb.isOpaque() &&
                              (mb.getBackground() instanceof UIResource)) {
                Point point = new Point(0, 0);
                point = SwingUtilities.convertPoint(c, point, rp);
                int x = point.x;
                int y = point.y;
                point.x = point.y = 0;
                point = SwingUtilities.convertPoint(mb, point, rp);
                if (point.x == x && y == point.y + mb.getHeight() &&
                     mb.getWidth() == c.getWidth() &&
                     MetalUtils.drawGradient(c, g, "MenuBar.gradient",
                     0, -mb.getHeight(), c.getWidth(), c.getHeight() +
                     mb.getHeight(), true)) {
                    setLastMenuBar(mb);
                    paint(g, c);
                    return;
                }
            }
            if (MetalUtils.drawGradient(c, g, "MenuBar.gradient",
                           0, 0, c.getWidth(), c.getHeight(), true)) {
                setLastMenuBar(null);
                paint(g, c);
                return;
            }
        }
        setLastMenuBar(null);
        super.update(g, c);