1 /*
   2  * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javax.swing.plaf.basic;
  27 
  28 import sun.swing.DefaultLookup;
  29 import sun.swing.UIAction;
  30 import java.awt.*;
  31 import java.awt.event.*;
  32 import java.beans.*;
  33 import javax.swing.*;
  34 import javax.swing.event.*;
  35 import javax.swing.plaf.ActionMapUIResource;
  36 import javax.swing.plaf.ButtonUI;
  37 import javax.swing.plaf.ComponentInputMapUIResource;
  38 
  39 /**
  40  * Button Listener
  41  *
  42  * @author Jeff Dinkins
  43  * @author Arnaud Weber (keyboard UI support)
  44  */
  45 
  46 public class BasicButtonListener implements MouseListener, MouseMotionListener,
  47                                    FocusListener, ChangeListener, PropertyChangeListener
  48 {
  49     private long lastPressedTimestamp = -1;
  50     private boolean shouldDiscardRelease = false;
  51 
  52     /**
  53      * Populates Buttons actions.
  54      */
  55     static void loadActionMap(LazyActionMap map) {
  56         map.put(new Actions(Actions.PRESS));
  57         map.put(new Actions(Actions.RELEASE));
  58     }
  59 
  60 
  61     /**
  62      * Constructs a new instance of {@code BasicButtonListener}.
  63      *
  64      * @param b an abstract button
  65      */
  66     public BasicButtonListener(AbstractButton b) {
  67     }
  68 
  69     public void propertyChange(PropertyChangeEvent e) {
  70         String prop = e.getPropertyName();
  71         if(prop == AbstractButton.MNEMONIC_CHANGED_PROPERTY) {
  72             updateMnemonicBinding((AbstractButton)e.getSource());
  73         }
  74         else if(prop == AbstractButton.CONTENT_AREA_FILLED_CHANGED_PROPERTY) {
  75             checkOpacity((AbstractButton) e.getSource() );
  76         }
  77         else if(prop == AbstractButton.TEXT_CHANGED_PROPERTY ||
  78                 "font" == prop || "foreground" == prop) {
  79             AbstractButton b = (AbstractButton) e.getSource();
  80             BasicHTML.updateRenderer(b, b.getText());
  81         }
  82     }
  83 
  84     /**
  85      * Checks the opacity of the {@code AbstractButton}.
  86      *
  87      * @param b an abstract button
  88      */
  89     protected void checkOpacity(AbstractButton b) {
  90         b.setOpaque( b.isContentAreaFilled() );
  91     }
  92 
  93     /**
  94      * Register default key actions: pressing space to "click" a
  95      * button and registering the keyboard mnemonic (if any).
  96      *
  97      * @param c a component
  98      */
  99     public void installKeyboardActions(JComponent c) {
 100         AbstractButton b = (AbstractButton)c;
 101         // Update the mnemonic binding.
 102         updateMnemonicBinding(b);
 103 
 104         LazyActionMap.installLazyActionMap(c, BasicButtonListener.class,
 105                                            "Button.actionMap");
 106 
 107         InputMap km = getInputMap(JComponent.WHEN_FOCUSED, c);
 108 
 109         SwingUtilities.replaceUIInputMap(c, JComponent.WHEN_FOCUSED, km);
 110     }
 111 
 112     /**
 113      * Unregister default key actions.
 114      *
 115      * @param c a component
 116      */
 117     public void uninstallKeyboardActions(JComponent c) {
 118         SwingUtilities.replaceUIInputMap(c, JComponent.
 119                                          WHEN_IN_FOCUSED_WINDOW, null);
 120         SwingUtilities.replaceUIInputMap(c, JComponent.WHEN_FOCUSED, null);
 121         SwingUtilities.replaceUIActionMap(c, null);
 122     }
 123 
 124     /**
 125      * Returns the InputMap for condition <code>condition</code>. Called as
 126      * part of <code>installKeyboardActions</code>.
 127      */
 128     InputMap getInputMap(int condition, JComponent c) {
 129         if (condition == JComponent.WHEN_FOCUSED) {
 130             BasicButtonUI ui = (BasicButtonUI)BasicLookAndFeel.getUIOfType(
 131                          ((AbstractButton)c).getUI(), BasicButtonUI.class);
 132             if (ui != null) {
 133                 return (InputMap)DefaultLookup.get(
 134                              c, ui, ui.getPropertyPrefix() + "focusInputMap");
 135             }
 136         }
 137         return null;
 138     }
 139 
 140     /**
 141      * Resets the binding for the mnemonic in the WHEN_IN_FOCUSED_WINDOW
 142      * UI InputMap.
 143      */
 144     void updateMnemonicBinding(AbstractButton b) {
 145         int m = b.getMnemonic();
 146         if(m != 0) {
 147             InputMap map = SwingUtilities.getUIInputMap(
 148                                 b, JComponent.WHEN_IN_FOCUSED_WINDOW);
 149 
 150             if (map == null) {
 151                 map = new ComponentInputMapUIResource(b);
 152                 SwingUtilities.replaceUIInputMap(b,
 153                                JComponent.WHEN_IN_FOCUSED_WINDOW, map);
 154             }
 155             map.clear();
 156             map.put(KeyStroke.getKeyStroke(m, BasicLookAndFeel.getFocusAcceleratorKeyMask(), false),
 157                     "pressed");
 158             map.put(KeyStroke.getKeyStroke(m, BasicLookAndFeel.getFocusAcceleratorKeyMask(), true),
 159                     "released");
 160             map.put(KeyStroke.getKeyStroke(m, 0, true), "released");
 161         }
 162         else {
 163             InputMap map = SwingUtilities.getUIInputMap(b, JComponent.
 164                                              WHEN_IN_FOCUSED_WINDOW);
 165             if (map != null) {
 166                 map.clear();
 167             }
 168         }
 169     }
 170 
 171     public void stateChanged(ChangeEvent e) {
 172         AbstractButton b = (AbstractButton) e.getSource();
 173         b.repaint();
 174     }
 175 
 176     public void focusGained(FocusEvent e) {
 177         AbstractButton b = (AbstractButton) e.getSource();
 178         if (b instanceof JButton && ((JButton)b).isDefaultCapable()) {
 179             JRootPane root = b.getRootPane();
 180             if (root != null) {
 181                BasicButtonUI ui = (BasicButtonUI)BasicLookAndFeel.getUIOfType(
 182                          b.getUI(), BasicButtonUI.class);
 183                if (ui != null && DefaultLookup.getBoolean(b, ui,
 184                                    ui.getPropertyPrefix() +
 185                                    "defaultButtonFollowsFocus", true)) {
 186                    root.putClientProperty("temporaryDefaultButton", b);
 187                    root.setDefaultButton((JButton)b);
 188                    root.putClientProperty("temporaryDefaultButton", null);
 189                }
 190             }
 191         }
 192         b.repaint();
 193     }
 194 
 195     public void focusLost(FocusEvent e) {
 196         AbstractButton b = (AbstractButton) e.getSource();
 197         JRootPane root = b.getRootPane();
 198         if (root != null) {
 199            JButton initialDefault = (JButton)root.getClientProperty("initialDefaultButton");
 200            if (b != initialDefault) {
 201                BasicButtonUI ui = (BasicButtonUI)BasicLookAndFeel.getUIOfType(
 202                          b.getUI(), BasicButtonUI.class);
 203                if (ui != null && DefaultLookup.getBoolean(b, ui,
 204                                    ui.getPropertyPrefix() +
 205                                    "defaultButtonFollowsFocus", true)) {
 206                    root.setDefaultButton(initialDefault);
 207                }
 208            }
 209         }
 210 
 211         ButtonModel model = b.getModel();
 212         model.setPressed(false);
 213         model.setArmed(false);
 214         b.repaint();
 215     }
 216 
 217     public void mouseMoved(MouseEvent e) {
 218     }
 219 
 220 
 221     public void mouseDragged(MouseEvent e) {
 222     }
 223 
 224     public void mouseClicked(MouseEvent e) {
 225     }
 226 
 227     public void mousePressed(MouseEvent e) {
 228        if (SwingUtilities.isLeftMouseButton(e) ) {
 229           AbstractButton b = (AbstractButton) e.getSource();
 230 
 231           if(b.contains(e.getX(), e.getY())) {
 232               long multiClickThreshhold = b.getMultiClickThreshhold();
 233               long lastTime = lastPressedTimestamp;
 234               long currentTime = lastPressedTimestamp = e.getWhen();
 235               if (lastTime != -1 && currentTime - lastTime < multiClickThreshhold) {
 236                   shouldDiscardRelease = true;
 237                   return;
 238               }
 239 
 240              ButtonModel model = b.getModel();
 241              if (!model.isEnabled()) {
 242                 // Disabled buttons ignore all input...
 243                 return;
 244              }
 245              if (!model.isArmed()) {
 246                 // button not armed, should be
 247                 model.setArmed(true);
 248              }
 249              model.setPressed(true);
 250              if(!b.hasFocus() && b.isRequestFocusEnabled()) {
 251                 b.requestFocus();
 252              }
 253           }
 254        }
 255     }
 256 
 257     public void mouseReleased(MouseEvent e) {
 258         if (SwingUtilities.isLeftMouseButton(e)) {
 259             // Support for multiClickThreshhold
 260             if (shouldDiscardRelease) {
 261                 shouldDiscardRelease = false;
 262                 return;
 263             }
 264             AbstractButton b = (AbstractButton) e.getSource();
 265             ButtonModel model = b.getModel();
 266             model.setPressed(false);
 267             model.setArmed(false);
 268         }
 269     }
 270 
 271     public void mouseEntered(MouseEvent e) {
 272         AbstractButton b = (AbstractButton) e.getSource();
 273         ButtonModel model = b.getModel();
 274         if (b.isRolloverEnabled() && !SwingUtilities.isLeftMouseButton(e)) {
 275             model.setRollover(true);
 276         }
 277         if (model.isPressed())
 278                 model.setArmed(true);
 279     }
 280 
 281     public void mouseExited(MouseEvent e) {
 282         AbstractButton b = (AbstractButton) e.getSource();
 283         ButtonModel model = b.getModel();
 284         if(b.isRolloverEnabled()) {
 285             model.setRollover(false);
 286         }
 287         model.setArmed(false);
 288     }
 289 
 290 
 291     /**
 292      * Actions for Buttons. Two types of action are supported:
 293      * pressed: Moves the button to a pressed state
 294      * released: Disarms the button.
 295      */
 296     private static class Actions extends UIAction {
 297         private static final String PRESS = "pressed";
 298         private static final String RELEASE = "released";
 299 
 300         Actions(String name) {
 301             super(name);
 302         }
 303 
 304         public void actionPerformed(ActionEvent e) {
 305             AbstractButton b = (AbstractButton)e.getSource();
 306             String key = getName();
 307             if (key == PRESS) {
 308                 ButtonModel model = b.getModel();
 309                 model.setArmed(true);
 310                 model.setPressed(true);
 311                 if(!b.hasFocus()) {
 312                     b.requestFocus();
 313                 }
 314             }
 315             else if (key == RELEASE) {
 316                 ButtonModel model = b.getModel();
 317                 model.setPressed(false);
 318                 model.setArmed(false);
 319             }
 320         }
 321 
 322         public boolean isEnabled(Object sender) {
 323             if(sender != null && (sender instanceof AbstractButton) &&
 324                       !((AbstractButton)sender).getModel().isEnabled()) {
 325                 return false;
 326             } else {
 327                 return true;
 328             }
 329         }
 330     }
 331 }
--- EOF ---