Save This Page
Home » openjdk-7 » javax » swing » plaf » basic » [javadoc | source]
    1   /*
    2    * Copyright (c) 1997, 2010, 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       public BasicButtonListener(AbstractButton b) {
   62       }
   63   
   64       public void propertyChange(PropertyChangeEvent e) {
   65           String prop = e.getPropertyName();
   66           if(prop == AbstractButton.MNEMONIC_CHANGED_PROPERTY) {
   67               updateMnemonicBinding((AbstractButton)e.getSource());
   68           }
   69           else if(prop == AbstractButton.CONTENT_AREA_FILLED_CHANGED_PROPERTY) {
   70               checkOpacity((AbstractButton) e.getSource() );
   71           }
   72           else if(prop == AbstractButton.TEXT_CHANGED_PROPERTY ||
   73                   "font" == prop || "foreground" == prop) {
   74               AbstractButton b = (AbstractButton) e.getSource();
   75               BasicHTML.updateRenderer(b, b.getText());
   76           }
   77       }
   78   
   79       protected void checkOpacity(AbstractButton b) {
   80           b.setOpaque( b.isContentAreaFilled() );
   81       }
   82   
   83       /**
   84        * Register default key actions: pressing space to "click" a
   85        * button and registring the keyboard mnemonic (if any).
   86        */
   87       public void installKeyboardActions(JComponent c) {
   88           AbstractButton b = (AbstractButton)c;
   89           // Update the mnemonic binding.
   90           updateMnemonicBinding(b);
   91   
   92           LazyActionMap.installLazyActionMap(c, BasicButtonListener.class,
   93                                              "Button.actionMap");
   94   
   95           InputMap km = getInputMap(JComponent.WHEN_FOCUSED, c);
   96   
   97           SwingUtilities.replaceUIInputMap(c, JComponent.WHEN_FOCUSED, km);
   98       }
   99   
  100       /**
  101        * Unregister's default key actions
  102        */
  103       public void uninstallKeyboardActions(JComponent c) {
  104           SwingUtilities.replaceUIInputMap(c, JComponent.
  105                                            WHEN_IN_FOCUSED_WINDOW, null);
  106           SwingUtilities.replaceUIInputMap(c, JComponent.WHEN_FOCUSED, null);
  107           SwingUtilities.replaceUIActionMap(c, null);
  108       }
  109   
  110       /**
  111        * Returns the InputMap for condition <code>condition</code>. Called as
  112        * part of <code>installKeyboardActions</code>.
  113        */
  114       InputMap getInputMap(int condition, JComponent c) {
  115           if (condition == JComponent.WHEN_FOCUSED) {
  116               BasicButtonUI ui = (BasicButtonUI)BasicLookAndFeel.getUIOfType(
  117                            ((AbstractButton)c).getUI(), BasicButtonUI.class);
  118               if (ui != null) {
  119                   return (InputMap)DefaultLookup.get(
  120                                c, ui, ui.getPropertyPrefix() + "focusInputMap");
  121               }
  122           }
  123           return null;
  124       }
  125   
  126       /**
  127        * Resets the binding for the mnemonic in the WHEN_IN_FOCUSED_WINDOW
  128        * UI InputMap.
  129        */
  130       void updateMnemonicBinding(AbstractButton b) {
  131           int m = b.getMnemonic();
  132           if(m != 0) {
  133               InputMap map = SwingUtilities.getUIInputMap(
  134                                   b, JComponent.WHEN_IN_FOCUSED_WINDOW);
  135   
  136               if (map == null) {
  137                   map = new ComponentInputMapUIResource(b);
  138                   SwingUtilities.replaceUIInputMap(b,
  139                                  JComponent.WHEN_IN_FOCUSED_WINDOW, map);
  140               }
  141               map.clear();
  142               map.put(KeyStroke.getKeyStroke(m, InputEvent.ALT_MASK, false),
  143                       "pressed");
  144               map.put(KeyStroke.getKeyStroke(m, InputEvent.ALT_MASK, true),
  145                       "released");
  146               map.put(KeyStroke.getKeyStroke(m, 0, true), "released");
  147           }
  148           else {
  149               InputMap map = SwingUtilities.getUIInputMap(b, JComponent.
  150                                                WHEN_IN_FOCUSED_WINDOW);
  151               if (map != null) {
  152                   map.clear();
  153               }
  154           }
  155       }
  156   
  157       public void stateChanged(ChangeEvent e) {
  158           AbstractButton b = (AbstractButton) e.getSource();
  159           b.repaint();
  160       }
  161   
  162       public void focusGained(FocusEvent e) {
  163           AbstractButton b = (AbstractButton) e.getSource();
  164           if (b instanceof JButton && ((JButton)b).isDefaultCapable()) {
  165               JRootPane root = b.getRootPane();
  166               if (root != null) {
  167                  BasicButtonUI ui = (BasicButtonUI)BasicLookAndFeel.getUIOfType(
  168                            b.getUI(), BasicButtonUI.class);
  169                  if (ui != null && DefaultLookup.getBoolean(b, ui,
  170                                      ui.getPropertyPrefix() +
  171                                      "defaultButtonFollowsFocus", true)) {
  172                      root.putClientProperty("temporaryDefaultButton", b);
  173                      root.setDefaultButton((JButton)b);
  174                      root.putClientProperty("temporaryDefaultButton", null);
  175                  }
  176               }
  177           }
  178           b.repaint();
  179       }
  180   
  181       public void focusLost(FocusEvent e) {
  182           AbstractButton b = (AbstractButton) e.getSource();
  183           JRootPane root = b.getRootPane();
  184           if (root != null) {
  185              JButton initialDefault = (JButton)root.getClientProperty("initialDefaultButton");
  186              if (b != initialDefault) {
  187                  BasicButtonUI ui = (BasicButtonUI)BasicLookAndFeel.getUIOfType(
  188                            b.getUI(), BasicButtonUI.class);
  189                  if (ui != null && DefaultLookup.getBoolean(b, ui,
  190                                      ui.getPropertyPrefix() +
  191                                      "defaultButtonFollowsFocus", true)) {
  192                      root.setDefaultButton(initialDefault);
  193                  }
  194              }
  195           }
  196   
  197           ButtonModel model = b.getModel();
  198           model.setPressed(false);
  199           model.setArmed(false);
  200           b.repaint();
  201       }
  202   
  203       public void mouseMoved(MouseEvent e) {
  204       }
  205   
  206   
  207       public void mouseDragged(MouseEvent e) {
  208       }
  209   
  210       public void mouseClicked(MouseEvent e) {
  211       }
  212   
  213       public void mousePressed(MouseEvent e) {
  214          if (SwingUtilities.isLeftMouseButton(e) ) {
  215             AbstractButton b = (AbstractButton) e.getSource();
  216   
  217             if(b.contains(e.getX(), e.getY())) {
  218                 long multiClickThreshhold = b.getMultiClickThreshhold();
  219                 long lastTime = lastPressedTimestamp;
  220                 long currentTime = lastPressedTimestamp = e.getWhen();
  221                 if (lastTime != -1 && currentTime - lastTime < multiClickThreshhold) {
  222                     shouldDiscardRelease = true;
  223                     return;
  224                 }
  225   
  226                ButtonModel model = b.getModel();
  227                if (!model.isEnabled()) {
  228                   // Disabled buttons ignore all input...
  229                   return;
  230                }
  231                if (!model.isArmed()) {
  232                   // button not armed, should be
  233                   model.setArmed(true);
  234                }
  235                model.setPressed(true);
  236                if(!b.hasFocus() && b.isRequestFocusEnabled()) {
  237                   b.requestFocus();
  238                }
  239             }
  240          }
  241       }
  242   
  243       public void mouseReleased(MouseEvent e) {
  244           if (SwingUtilities.isLeftMouseButton(e)) {
  245               // Support for multiClickThreshhold
  246               if (shouldDiscardRelease) {
  247                   shouldDiscardRelease = false;
  248                   return;
  249               }
  250               AbstractButton b = (AbstractButton) e.getSource();
  251               ButtonModel model = b.getModel();
  252               model.setPressed(false);
  253               model.setArmed(false);
  254           }
  255       }
  256   
  257       public void mouseEntered(MouseEvent e) {
  258           AbstractButton b = (AbstractButton) e.getSource();
  259           ButtonModel model = b.getModel();
  260           if (b.isRolloverEnabled() && !SwingUtilities.isLeftMouseButton(e)) {
  261               model.setRollover(true);
  262           }
  263           if (model.isPressed())
  264                   model.setArmed(true);
  265       }
  266   
  267       public void mouseExited(MouseEvent e) {
  268           AbstractButton b = (AbstractButton) e.getSource();
  269           ButtonModel model = b.getModel();
  270           if(b.isRolloverEnabled()) {
  271               model.setRollover(false);
  272           }
  273           model.setArmed(false);
  274       }
  275   
  276   
  277       /**
  278        * Actions for Buttons. Two types of action are supported:
  279        * pressed: Moves the button to a pressed state
  280        * released: Disarms the button.
  281        */
  282       private static class Actions extends UIAction {
  283           private static final String PRESS = "pressed";
  284           private static final String RELEASE = "released";
  285   
  286           Actions(String name) {
  287               super(name);
  288           }
  289   
  290           public void actionPerformed(ActionEvent e) {
  291               AbstractButton b = (AbstractButton)e.getSource();
  292               String key = getName();
  293               if (key == PRESS) {
  294                   ButtonModel model = b.getModel();
  295                   model.setArmed(true);
  296                   model.setPressed(true);
  297                   if(!b.hasFocus()) {
  298                       b.requestFocus();
  299                   }
  300               }
  301               else if (key == RELEASE) {
  302                   ButtonModel model = b.getModel();
  303                   model.setPressed(false);
  304                   model.setArmed(false);
  305               }
  306           }
  307   
  308           public boolean isEnabled(Object sender) {
  309               if(sender != null && (sender instanceof AbstractButton) &&
  310                         !((AbstractButton)sender).getModel().isEnabled()) {
  311                   return false;
  312               } else {
  313                   return true;
  314               }
  315           }
  316       }
  317   }

Save This Page
Home » openjdk-7 » javax » swing » plaf » basic » [javadoc | source]