Source for javax.swing.plaf.basic.BasicButtonListener

   1: /* BasicButtonListener.java --
   2:    Copyright (C) 2004, 2005 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package javax.swing.plaf.basic;
  40: 
  41: import java.awt.event.ActionEvent;
  42: import java.awt.event.FocusEvent;
  43: import java.awt.event.FocusListener;
  44: import java.awt.event.InputEvent;
  45: import java.awt.event.MouseEvent;
  46: import java.awt.event.MouseListener;
  47: import java.awt.event.MouseMotionListener;
  48: import java.beans.PropertyChangeEvent;
  49: import java.beans.PropertyChangeListener;
  50: 
  51: import javax.swing.AbstractAction;
  52: import javax.swing.AbstractButton;
  53: import javax.swing.ButtonModel;
  54: import javax.swing.JComponent;
  55: import javax.swing.event.ChangeEvent;
  56: import javax.swing.event.ChangeListener;
  57: 
  58: public class BasicButtonListener
  59:   implements MouseListener, MouseMotionListener, FocusListener, 
  60:              ChangeListener, PropertyChangeListener
  61: {
  62:   public BasicButtonListener(AbstractButton b)
  63:   {
  64:     // Do nothing here.
  65:   }
  66:   
  67:   public void propertyChange(PropertyChangeEvent e)
  68:   {
  69:   }
  70:   
  71:   protected void checkOpacity(AbstractButton b) 
  72:   {    
  73:   }
  74:   
  75:   public void focusGained(FocusEvent e) 
  76:   {    
  77:     if (e.getSource() instanceof AbstractButton)
  78:       {
  79:         AbstractButton button = (AbstractButton) e.getSource();
  80:         if (button.isFocusPainted())
  81:           button.repaint();   
  82:       }
  83:   }
  84:   
  85:   public void focusLost(FocusEvent e)
  86:   {
  87:     if (e.getSource() instanceof AbstractButton)
  88:       {
  89:         AbstractButton button = (AbstractButton) e.getSource();
  90:         if (button.isFocusPainted())
  91:           button.repaint();   
  92:       }
  93:   }
  94:   
  95:   public void installKeyboardActions(JComponent c)
  96:   {
  97:     c.getActionMap().put("pressed", 
  98:                          new AbstractAction() 
  99:                          {
 100:                            public void actionPerformed(ActionEvent e)          
 101:                            {
 102:                              AbstractButton button = (AbstractButton) e.getSource();
 103:                              ButtonModel model = button.getModel();
 104:                              // It is important that these transitions happen in this order.
 105:                              model.setArmed(true);
 106:                              model.setPressed(true);
 107:                            }
 108:                          });
 109:     
 110:     c.getActionMap().put("released", 
 111:                          new AbstractAction() 
 112:                          {
 113:                            public void actionPerformed(ActionEvent e)          
 114:                            {
 115:                              AbstractButton button = (AbstractButton) e.getSource();
 116:                              ButtonModel model = button.getModel();
 117:                              // It is important that these transitions happen in this order.
 118:                              model.setPressed(false);
 119:                              model.setArmed(false);
 120:                            }
 121:                        });    
 122:   }
 123:   
 124:   public void uninstallKeyboardActions(JComponent c)
 125:   {
 126:     c.getActionMap().put("pressed", null);
 127:     c.getActionMap().put("released", null);
 128:   }
 129:   
 130:   public void stateChanged(ChangeEvent e)
 131:   {
 132:   }
 133:   
 134:   public void mouseMoved(MouseEvent e)
 135:   {
 136:   }
 137:   
 138:   public void mouseDragged(MouseEvent e)
 139:   {
 140:   }
 141:   
 142:   public void mouseClicked(MouseEvent e)
 143:   {
 144:   }
 145: 
 146:   /**
 147:    * Accept a mouse press event and arm the button.
 148:    *
 149:    * @param e The mouse press event to accept
 150:    */
 151:   public void mousePressed(MouseEvent e)
 152:   {
 153:     if (e.getSource() instanceof AbstractButton)
 154:       {
 155:         AbstractButton button = (AbstractButton) e.getSource();
 156:         ButtonModel model = button.getModel();
 157:         if (e.getButton() == MouseEvent.BUTTON1)
 158:           {
 159:             // It is important that these transitions happen in this order.
 160:             model.setArmed(true);
 161:             model.setPressed(true);
 162:           }
 163:       }
 164:   }
 165: 
 166:   /**
 167:    * Accept a mouse release event and set the button's 
 168:    * "pressed" property to <code>true</code>, if the model
 169:    * is armed. If the model is not armed, ignore the event.
 170:    *
 171:    * @param e The mouse release event to accept
 172:    */
 173:   public void mouseReleased(MouseEvent e)
 174:   {
 175:     if (e.getSource() instanceof AbstractButton)
 176:       {
 177:         AbstractButton button = (AbstractButton) e.getSource();
 178:         ButtonModel model = button.getModel();
 179:         if (e.getButton() == MouseEvent.BUTTON1)
 180:           {
 181:             // It is important that these transitions happen in this order.
 182:             model.setPressed(false);
 183:             model.setArmed(false);
 184:           }
 185:       }
 186:   }
 187: 
 188:   /**
 189:    * Accept a mouse enter event and set the button's "rollover" property to
 190:    * <code>true</code>, if the button's "rolloverEnabled" property is
 191:    * <code>true</code>. If the button is currently armed and the mouse
 192:    * button is not held down, this enter event will also disarm the model.
 193:    *
 194:    * @param e The mouse enter event to accept
 195:    */
 196:   public void mouseEntered(MouseEvent e)
 197:   {
 198:     if (e.getSource() instanceof AbstractButton)
 199:       {
 200:         AbstractButton button = (AbstractButton) e.getSource();
 201:         ButtonModel model = button.getModel();
 202:         if (button.isRolloverEnabled())
 203:           model.setRollover(true);
 204:         
 205:         if (model.isPressed() 
 206:             && (e.getModifiersEx() & InputEvent.BUTTON1_DOWN_MASK) != 0)
 207:           model.setArmed(true);
 208:         else
 209:           model.setArmed(false);
 210:       }
 211:   }
 212: 
 213:   /**
 214:    * Accept a mouse exit event and set the button's model's "rollover"
 215:    * property to <code>false</code>, if it's "rolloverEnabled" property is
 216:    * <code>true</code>. Also disarm the button.
 217:    *
 218:    * @param e The mouse exit event to accept
 219:    */
 220:   public void mouseExited(MouseEvent e)
 221:   {
 222:     if (e.getSource() instanceof AbstractButton)
 223:       {
 224:         AbstractButton button = (AbstractButton) e.getSource();
 225:         ButtonModel model = button.getModel();
 226:         if (button.isRolloverEnabled())
 227:           model.setRollover(false);
 228:         model.setArmed(false);
 229:       }
 230:   }
 231: }