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