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.SwingUtilities;
  56: import javax.swing.event.ChangeEvent;
  57: import javax.swing.event.ChangeListener;
  58: 
  59: public class BasicButtonListener implements MouseListener, MouseMotionListener,
  60:   FocusListener, ChangeListener, PropertyChangeListener
  61: {
  62:   public BasicButtonListener(AbstractButton b)
  63:   {
  64:     // Do nothing here.
  65:   }
  66:   
  67:   public void propertyChange(PropertyChangeEvent e)
  68:   {
  69:     // TODO: What should be done here, if anything?
  70:   }
  71:   
  72:   protected void checkOpacity(AbstractButton b) 
  73:   {    
  74:     // TODO: What should be done here?
  75:   }
  76:   
  77:   public void focusGained(FocusEvent e) 
  78:   {    
  79:     if (e.getSource() instanceof AbstractButton)
  80:       {
  81:         AbstractButton button = (AbstractButton) e.getSource();
  82:         if (button.isFocusPainted())
  83:           button.repaint();   
  84:       }
  85:   }
  86:   
  87:   public void focusLost(FocusEvent e)
  88:   {
  89:     if (e.getSource() instanceof AbstractButton)
  90:       {
  91:         AbstractButton button = (AbstractButton) e.getSource();
  92:         if (button.isFocusPainted())
  93:           button.repaint();   
  94:       }
  95:   }
  96:   
  97:   public void installKeyboardActions(JComponent c)
  98:   {
  99:     c.getActionMap().put("pressed", 
 100:                          new AbstractAction() 
 101:                          {
 102:                            public void actionPerformed(ActionEvent e)          
 103:                            {
 104:                              AbstractButton button = (AbstractButton) e.getSource();
 105:                              ButtonModel model = button.getModel();
 106:                              // It is important that these transitions happen in this order.
 107:                              model.setArmed(true);
 108:                              model.setPressed(true);
 109:                            }
 110:                          });
 111:     
 112:     c.getActionMap().put("released", 
 113:                          new AbstractAction() 
 114:                          {
 115:                            public void actionPerformed(ActionEvent e)          
 116:                            {
 117:                              AbstractButton button = (AbstractButton) e.getSource();
 118:                              ButtonModel model = button.getModel();
 119:                              // It is important that these transitions happen in this order.
 120:                              model.setPressed(false);
 121:                              model.setArmed(false);
 122:                            }
 123:                        });    
 124:   }
 125:   
 126:   public void uninstallKeyboardActions(JComponent c)
 127:   {
 128:     c.getActionMap().put("pressed", null);
 129:     c.getActionMap().put("released", null);
 130:   }
 131:   
 132:   public void stateChanged(ChangeEvent e)
 133:   {
 134:     // TODO: What should be done here, if anything?
 135:   }
 136:   
 137:   public void mouseMoved(MouseEvent e)
 138:   {
 139:     // TODO: What should be done here, if anything?
 140:   }
 141:   
 142:   public void mouseDragged(MouseEvent e)
 143:   {
 144:     // TODO: What should be done here, if anything?
 145:   }
 146:   
 147:   public void mouseClicked(MouseEvent e)
 148:   {
 149:     // TODO: What should be done here, if anything?
 150:   }
 151: 
 152:   /**
 153:    * Accept a mouse press event and arm the button.
 154:    *
 155:    * @param e The mouse press event to accept
 156:    */
 157:   public void mousePressed(MouseEvent e)
 158:   {
 159:     if (e.getSource() instanceof AbstractButton)
 160:       {
 161:         AbstractButton button = (AbstractButton) e.getSource();
 162:         ButtonModel model = button.getModel();
 163:         if (e.getButton() == MouseEvent.BUTTON1)
 164:           {
 165:             // It is important that these transitions happen in this order.
 166:             model.setArmed(true);
 167:             model.setPressed(true);
 168:           }
 169:       }
 170:   }
 171: 
 172:   /**
 173:    * Accept a mouse release event and set the button's 
 174:    * "pressed" property to <code>true</code>, if the model
 175:    * is armed. If the model is not armed, ignore the event.
 176:    *
 177:    * @param e The mouse release event to accept
 178:    */
 179:   public void mouseReleased(MouseEvent e)
 180:   {
 181:     if (e.getSource() instanceof AbstractButton)
 182:       {
 183:         AbstractButton button = (AbstractButton) e.getSource();
 184:         ButtonModel model = button.getModel();
 185:         if (e.getButton() == MouseEvent.BUTTON1)
 186:           {
 187:             // It is important that these transitions happen in this order.
 188:             model.setPressed(false);
 189:             model.setArmed(false);
 190:           }
 191:       }
 192:   }
 193: 
 194:   /**
 195:    * Accept a mouse enter event and set the button's "rollover" property to
 196:    * <code>true</code>, if the button's "rolloverEnabled" property is
 197:    * <code>true</code>. If the button is currently armed and the mouse
 198:    * button is not held down, this enter event will also disarm the model.
 199:    *
 200:    * @param e The mouse enter event to accept
 201:    */
 202:   public void mouseEntered(MouseEvent e)
 203:   {
 204:     if (e.getSource() instanceof AbstractButton)
 205:       {
 206:         AbstractButton button = (AbstractButton) e.getSource();
 207:         ButtonModel model = button.getModel();
 208:         if (button.isRolloverEnabled()
 209:             && ! SwingUtilities.isLeftMouseButton(e))
 210:           model.setRollover(true);
 211: 
 212:         if (model.isPressed())
 213:           model.setArmed(true);
 214:       }
 215:   }
 216: 
 217:   /**
 218:    * Accept a mouse exit event and set the button's model's "rollover"
 219:    * property to <code>false</code>, if it's "rolloverEnabled" property is
 220:    * <code>true</code>. Also disarm the button.
 221:    *
 222:    * @param e The mouse exit event to accept
 223:    */
 224:   public void mouseExited(MouseEvent e)
 225:   {
 226:     if (e.getSource() instanceof AbstractButton)
 227:       {
 228:         AbstractButton button = (AbstractButton) e.getSource();
 229:         ButtonModel model = button.getModel();
 230:         if (button.isRolloverEnabled())
 231:           model.setRollover(false);
 232:         model.setArmed(false);
 233:       }
 234:   }
 235: }