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 gnu.classpath.SystemProperties;
  42: 
  43: import java.awt.event.ActionEvent;
  44: import java.awt.event.FocusEvent;
  45: import java.awt.event.FocusListener;
  46: import java.awt.event.MouseEvent;
  47: import java.awt.event.MouseListener;
  48: import java.awt.event.MouseMotionListener;
  49: import java.awt.font.FontRenderContext;
  50: import java.awt.font.TextLayout;
  51: import java.awt.geom.AffineTransform;
  52: import java.beans.PropertyChangeEvent;
  53: import java.beans.PropertyChangeListener;
  54: 
  55: import javax.swing.AbstractAction;
  56: import javax.swing.AbstractButton;
  57: import javax.swing.ButtonModel;
  58: import javax.swing.JComponent;
  59: import javax.swing.SwingUtilities;
  60: import javax.swing.event.ChangeEvent;
  61: import javax.swing.event.ChangeListener;
  62: 
  63: public class BasicButtonListener implements MouseListener, MouseMotionListener,
  64:   FocusListener, ChangeListener, PropertyChangeListener
  65: {
  66:   public BasicButtonListener(AbstractButton b)
  67:   {
  68:     // Do nothing here.
  69:   }
  70:   
  71:   public void propertyChange(PropertyChangeEvent e)
  72:   {
  73:     // Store the TextLayout for this in a client property for speed-up
  74:     // painting of the label.
  75:     String property = e.getPropertyName();
  76:     if ((property.equals(AbstractButton.TEXT_CHANGED_PROPERTY)
  77:          || property.equals("font"))
  78:         && SystemProperties.getProperty("gnu.javax.swing.noGraphics2D")
  79:         == null)
  80:       {
  81:         AbstractButton b = (AbstractButton) e.getSource();
  82:         String text = b.getText();
  83:         if (text == null)
  84:           text = "";
  85:         FontRenderContext frc = new FontRenderContext(new AffineTransform(),
  86:                                                       false, false);
  87:         TextLayout layout = new TextLayout(text, b.getFont(), frc);
  88:         b.putClientProperty(BasicGraphicsUtils.CACHED_TEXT_LAYOUT, layout);
  89:       }
  90:   }
  91:   
  92:   protected void checkOpacity(AbstractButton b) 
  93:   {    
  94:     // TODO: What should be done here?
  95:   }
  96:   
  97:   public void focusGained(FocusEvent e) 
  98:   {    
  99:     if (e.getSource() instanceof AbstractButton)
 100:       {
 101:         AbstractButton button = (AbstractButton) e.getSource();
 102:         if (button.isFocusPainted())
 103:           button.repaint();   
 104:       }
 105:   }
 106:   
 107:   public void focusLost(FocusEvent e)
 108:   {
 109:     if (e.getSource() instanceof AbstractButton)
 110:       {
 111:         AbstractButton button = (AbstractButton) e.getSource();
 112:         if (button.isFocusPainted())
 113:           button.repaint();   
 114:       }
 115:   }
 116:   
 117:   public void installKeyboardActions(JComponent c)
 118:   {
 119:     c.getActionMap().put("pressed", 
 120:                          new AbstractAction() 
 121:                          {
 122:                            public void actionPerformed(ActionEvent e)          
 123:                            {
 124:                              AbstractButton button = (AbstractButton) e.getSource();
 125:                              ButtonModel model = button.getModel();
 126:                              // It is important that these transitions happen in this order.
 127:                              model.setArmed(true);
 128:                              model.setPressed(true);
 129:                            }
 130:                          });
 131:     
 132:     c.getActionMap().put("released", 
 133:                          new AbstractAction() 
 134:                          {
 135:                            public void actionPerformed(ActionEvent e)          
 136:                            {
 137:                              AbstractButton button = (AbstractButton) e.getSource();
 138:                              ButtonModel model = button.getModel();
 139:                              // It is important that these transitions happen in this order.
 140:                              model.setPressed(false);
 141:                              model.setArmed(false);
 142:                            }
 143:                        });    
 144:   }
 145:   
 146:   public void uninstallKeyboardActions(JComponent c)
 147:   {
 148:     c.getActionMap().put("pressed", null);
 149:     c.getActionMap().put("released", null);
 150:   }
 151:   
 152:   public void stateChanged(ChangeEvent e)
 153:   {
 154:     // TODO: What should be done here, if anything?
 155:   }
 156:   
 157:   public void mouseMoved(MouseEvent e)
 158:   {
 159:     // TODO: What should be done here, if anything?
 160:   }
 161:   
 162:   public void mouseDragged(MouseEvent e)
 163:   {
 164:     // TODO: What should be done here, if anything?
 165:   }
 166:   
 167:   public void mouseClicked(MouseEvent e)
 168:   {
 169:     // TODO: What should be done here, if anything?
 170:   }
 171: 
 172:   /**
 173:    * Accept a mouse press event and arm the button.
 174:    *
 175:    * @param e The mouse press event to accept
 176:    */
 177:   public void mousePressed(MouseEvent e)
 178:   {
 179:     if (e.getSource() instanceof AbstractButton)
 180:       {
 181:         AbstractButton button = (AbstractButton) e.getSource();
 182:         ButtonModel model = button.getModel();
 183:         if (SwingUtilities.isLeftMouseButton(e))
 184:           {
 185:             // It is important that these transitions happen in this order.
 186:             model.setArmed(true);
 187:             model.setPressed(true);
 188: 
 189:             if (! button.isFocusOwner() && button.isRequestFocusEnabled())
 190:               button.requestFocus();
 191:           }
 192:       }
 193:   }
 194: 
 195:   /**
 196:    * Accept a mouse release event and set the button's 
 197:    * "pressed" property to <code>true</code>, if the model
 198:    * is armed. If the model is not armed, ignore the event.
 199:    *
 200:    * @param e The mouse release event to accept
 201:    */
 202:   public void mouseReleased(MouseEvent e)
 203:   {
 204:     if (e.getSource() instanceof AbstractButton)
 205:       {
 206:         AbstractButton button = (AbstractButton) e.getSource();
 207:         ButtonModel model = button.getModel();
 208:         if (e.getButton() == MouseEvent.BUTTON1)
 209:           {
 210:             // It is important that these transitions happen in this order.
 211:             model.setPressed(false);
 212:             model.setArmed(false);
 213:           }
 214:       }
 215:   }
 216: 
 217:   /**
 218:    * Accept a mouse enter event and set the button's "rollover" property to
 219:    * <code>true</code>, if the button's "rolloverEnabled" property is
 220:    * <code>true</code>. If the button is currently armed and the mouse
 221:    * button is not held down, this enter event will also disarm the model.
 222:    *
 223:    * @param e The mouse enter event to accept
 224:    */
 225:   public void mouseEntered(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:             && ! SwingUtilities.isLeftMouseButton(e))
 233:           model.setRollover(true);
 234: 
 235:         if (model.isPressed())
 236:           model.setArmed(true);
 237:       }
 238:   }
 239: 
 240:   /**
 241:    * Accept a mouse exit event and set the button's model's "rollover"
 242:    * property to <code>false</code>, if it's "rolloverEnabled" property is
 243:    * <code>true</code>. Also disarm the button.
 244:    *
 245:    * @param e The mouse exit event to accept
 246:    */
 247:   public void mouseExited(MouseEvent e)
 248:   {
 249:     if (e.getSource() instanceof AbstractButton)
 250:       {
 251:         AbstractButton button = (AbstractButton) e.getSource();
 252:         ButtonModel model = button.getModel();
 253:         if (button.isRolloverEnabled())
 254:           model.setRollover(false);
 255:         model.setArmed(false);
 256:       }
 257:   }
 258: }