FileDocCategorySizeDatePackage
BasicButtonListener.javaAPI DocJava SE 5 API9402Fri Aug 26 14:58:02 BST 2005javax.swing.plaf.basic

BasicButtonListener

public class BasicButtonListener extends Object implements MouseListener, PropertyChangeListener, ChangeListener, MouseMotionListener, FocusListener
Button Listener
version
1.63 01/19/04
author
Jeff Dinkins
author
Arnaud Weber (keyboard UI support)

Fields Summary
private long
lastPressedTimestamp
private boolean
shouldDiscardRelease
Constructors Summary
public BasicButtonListener(AbstractButton b)

    
Methods Summary
protected voidcheckOpacity(javax.swing.AbstractButton b)

	b.setOpaque( b.isContentAreaFilled() );
    
public voidfocusGained(java.awt.event.FocusEvent e)

 
	AbstractButton b = (AbstractButton) e.getSource();
        if (b instanceof JButton && ((JButton)b).isDefaultCapable()) {
            JRootPane root = b.getRootPane();
            if (root != null) {
               BasicButtonUI ui = (BasicButtonUI)BasicLookAndFeel.getUIOfType(
                         ((AbstractButton)b).getUI(), BasicButtonUI.class);
               if (ui != null && DefaultLookup.getBoolean(b, ui,
                                   ui.getPropertyPrefix() +
                                   "defaultButtonFollowsFocus", true)) {
                   root.putClientProperty("temporaryDefaultButton", b);
                   root.setDefaultButton((JButton)b);
                   root.putClientProperty("temporaryDefaultButton", null);
               }
            }
        }
	b.repaint();
    
public voidfocusLost(java.awt.event.FocusEvent e)

	AbstractButton b = (AbstractButton) e.getSource();
	JRootPane root = b.getRootPane();
	if (root != null) {
	   JButton initialDefault = (JButton)root.getClientProperty("initialDefaultButton");
	   if (b != initialDefault) {
               BasicButtonUI ui = (BasicButtonUI)BasicLookAndFeel.getUIOfType(
                         ((AbstractButton)b).getUI(), BasicButtonUI.class);
               if (ui != null && DefaultLookup.getBoolean(b, ui,
                                   ui.getPropertyPrefix() +
                                   "defaultButtonFollowsFocus", true)) {
                   root.setDefaultButton(initialDefault);
               }
	   }
	}

        b.getModel().setArmed(false);

	b.repaint();
    
javax.swing.InputMapgetInputMap(int condition, javax.swing.JComponent c)
Returns the InputMap for condition condition. Called as part of installKeyboardActions.

	if (condition == JComponent.WHEN_FOCUSED) {
            BasicButtonUI ui = (BasicButtonUI)BasicLookAndFeel.getUIOfType(
                         ((AbstractButton)c).getUI(), BasicButtonUI.class);
	    if (ui != null) {
                return (InputMap)DefaultLookup.get(
                             c, ui, ui.getPropertyPrefix() + "focusInputMap");
	    }
	}
	return null;
    
public voidinstallKeyboardActions(javax.swing.JComponent c)
Register default key actions: pressing space to "click" a button and registring the keyboard mnemonic (if any).

	AbstractButton b = (AbstractButton)c;	
	// Update the mnemonic binding.
	updateMnemonicBinding(b);

        LazyActionMap.installLazyActionMap(c, BasicButtonListener.class,
                                           "Button.actionMap");

	InputMap km = getInputMap(JComponent.WHEN_FOCUSED, c);

	SwingUtilities.replaceUIInputMap(c, JComponent.WHEN_FOCUSED, km);
    
static voidloadActionMap(javax.swing.plaf.basic.LazyActionMap map)
Populates Buttons actions.


            
        
        map.put(new Actions(Actions.PRESS));
	map.put(new Actions(Actions.RELEASE));
    
public voidmouseClicked(java.awt.event.MouseEvent e)

    
public voidmouseDragged(java.awt.event.MouseEvent e)

    
public voidmouseEntered(java.awt.event.MouseEvent e)

	AbstractButton b = (AbstractButton) e.getSource();
        ButtonModel model = b.getModel();
        if (b.isRolloverEnabled() && !SwingUtilities.isLeftMouseButton(e)) {
            model.setRollover(true);
        }
        if (model.isPressed())
		model.setArmed(true);
    
public voidmouseExited(java.awt.event.MouseEvent e)

	AbstractButton b = (AbstractButton) e.getSource();
        ButtonModel model = b.getModel();
        if(b.isRolloverEnabled()) {
            model.setRollover(false);
        }
        model.setArmed(false);
    
public voidmouseMoved(java.awt.event.MouseEvent e)

    
public voidmousePressed(java.awt.event.MouseEvent e)

       if (SwingUtilities.isLeftMouseButton(e) ) {
	  AbstractButton b = (AbstractButton) e.getSource();

	  if(b.contains(e.getX(), e.getY())) {
	      long multiClickThreshhold = b.getMultiClickThreshhold();
	      long lastTime = lastPressedTimestamp;
	      long currentTime = lastPressedTimestamp = e.getWhen();
	      if (lastTime != -1 && currentTime - lastTime < multiClickThreshhold) {
		  shouldDiscardRelease = true;
		  return;
	      }

	     ButtonModel model = b.getModel();
	     if (!model.isEnabled()) {
	        // Disabled buttons ignore all input...
	   	return;
	     }
	     if (!model.isArmed()) {
		// button not armed, should be
                model.setArmed(true);
	     }
	     model.setPressed(true);
	     if(!b.hasFocus() && b.isRequestFocusEnabled()) {
	        b.requestFocus();
	     }            
	  } 
       }
    
public voidmouseReleased(java.awt.event.MouseEvent e)

	if (SwingUtilities.isLeftMouseButton(e)) {
	    // Support for multiClickThreshhold
            if (shouldDiscardRelease) {
	        shouldDiscardRelease = false;
	        return;
	    }
	    AbstractButton b = (AbstractButton) e.getSource();
	    ButtonModel model = b.getModel();
	    model.setPressed(false);
	    model.setArmed(false);
        }
    
public voidpropertyChange(java.beans.PropertyChangeEvent e)

	String prop = e.getPropertyName();
	if(prop == AbstractButton.MNEMONIC_CHANGED_PROPERTY) {
	    updateMnemonicBinding((AbstractButton)e.getSource());
	}
        else if(prop == AbstractButton.CONTENT_AREA_FILLED_CHANGED_PROPERTY) {
	    checkOpacity((AbstractButton) e.getSource() );
	}
	else if(prop == AbstractButton.TEXT_CHANGED_PROPERTY ||
                "font" == prop || "foreground" == prop) {
	    AbstractButton b = (AbstractButton) e.getSource();
	    BasicHTML.updateRenderer(b, b.getText());
	}
    
public voidstateChanged(javax.swing.event.ChangeEvent e)

	AbstractButton b = (AbstractButton) e.getSource();
        b.repaint();
    
public voiduninstallKeyboardActions(javax.swing.JComponent c)
Unregister's default key actions

        SwingUtilities.replaceUIInputMap(c, JComponent.
                                         WHEN_IN_FOCUSED_WINDOW, null);
	SwingUtilities.replaceUIInputMap(c, JComponent.WHEN_FOCUSED, null);
	SwingUtilities.replaceUIActionMap(c, null);
    
voidupdateMnemonicBinding(javax.swing.AbstractButton b)
Resets the binding for the mnemonic in the WHEN_IN_FOCUSED_WINDOW UI InputMap.

	int m = b.getMnemonic();
	if(m != 0) {
	    InputMap map = SwingUtilities.getUIInputMap(
                                b, JComponent.WHEN_IN_FOCUSED_WINDOW);

            if (map == null) {
		map = new ComponentInputMapUIResource(b);
		SwingUtilities.replaceUIInputMap(b,
			       JComponent.WHEN_IN_FOCUSED_WINDOW, map);
	    }
            map.clear();
            map.put(KeyStroke.getKeyStroke(m, InputEvent.ALT_MASK, false),
                    "pressed");
            map.put(KeyStroke.getKeyStroke(m, InputEvent.ALT_MASK, true),
                    "released");
            map.put(KeyStroke.getKeyStroke(m, 0, true), "released");
	} 
        else {
	    InputMap map = SwingUtilities.getUIInputMap(b, JComponent.
					     WHEN_IN_FOCUSED_WINDOW);
	    if (map != null) {
		map.clear();
	    }
	}