OSDN Git Service

350db3bedd886f7c7c7242cc27febdec519875fc
[pf3gnuchains/gcc-fork.git] / libjava / javax / swing / plaf / basic / BasicButtonListener.java
1 /* BasicButtonListener.java --
2    Copyright (C) 2004 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., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 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         ButtonModel model = button.getModel();
91         model.setArmed(false);
92
93         if (button.isFocusPainted())
94           button.repaint();   
95       }
96   }
97   
98   public void installKeyboardActions(JComponent c)
99   {
100     c.getActionMap().put("pressed", 
101                          new AbstractAction() 
102                          {
103                            public void actionPerformed(ActionEvent e)          
104                            {
105                              AbstractButton button = (AbstractButton) e.getSource();
106                              ButtonModel model = button.getModel();
107                              // It is important that these transitions happen in this order.
108                              model.setArmed(true);
109                              model.setPressed(true);
110                            }
111                          });
112     
113     c.getActionMap().put("released", 
114                          new AbstractAction() 
115                          {
116                            public void actionPerformed(ActionEvent e)          
117                            {
118                              AbstractButton button = (AbstractButton) e.getSource();
119                              ButtonModel model = button.getModel();
120                              // It is important that these transitions happen in this order.
121                              model.setPressed(false);
122                              model.setArmed(false);
123                            }
124                        });    
125   }
126   
127   public void uninstallKeyboardActions(JComponent c)
128   {
129     c.getActionMap().put("pressed", null);
130     c.getActionMap().put("released", null);
131   }
132   
133   public void stateChanged(ChangeEvent e)
134   {
135   }
136   
137   public void mouseMoved(MouseEvent e)
138   {
139   }
140   
141   public void mouseDragged(MouseEvent e)
142   {
143   }
144   
145   public void mouseClicked(MouseEvent e)
146   {
147   }
148
149   /**
150    * Accept a mouse press event and arm the button.
151    *
152    * @param e The mouse press event to accept
153    */
154   public void mousePressed(MouseEvent e)
155   {
156     if (e.getSource() instanceof AbstractButton)
157       {
158         AbstractButton button = (AbstractButton) e.getSource();
159         ButtonModel model = button.getModel();
160         if (e.getButton() == MouseEvent.BUTTON1)
161           {
162             // It is important that these transitions happen in this order.
163             model.setArmed(true);
164             model.setPressed(true);
165           }
166       }
167   }
168
169   /**
170    * Accept a mouse release event and set the button's 
171    * "pressed" property to <code>true</code>, if the model
172    * is armed. If the model is not armed, ignore the event.
173    *
174    * @param e The mouse release event to accept
175    */
176   public void mouseReleased(MouseEvent e)
177   {
178     if (e.getSource() instanceof AbstractButton)
179       {
180         AbstractButton button = (AbstractButton) e.getSource();
181         ButtonModel model = button.getModel();
182         if (e.getButton() == MouseEvent.BUTTON1)
183           {
184             // It is important that these transitions happen in this order.
185             model.setPressed(false);
186             model.setArmed(false);
187           }
188       }
189   }
190
191   /**
192    * Accept a mouse enter event and set the button's "rollover" property to
193    * <code>true</code>, if the button's "rolloverEnabled" property is
194    * <code>true</code>. If the button is currently armed and the mouse
195    * button is not held down, this enter event will also disarm the model.
196    *
197    * @param e The mouse enter event to accept
198    */
199   public void mouseEntered(MouseEvent e)
200   {
201     if (e.getSource() instanceof AbstractButton)
202       {
203         AbstractButton button = (AbstractButton) e.getSource();
204         ButtonModel model = button.getModel();
205         if (button.isRolloverEnabled())
206           model.setRollover(true);
207         
208         if (model.isPressed() 
209             && (e.getModifiersEx() & InputEvent.BUTTON1_DOWN_MASK) != 0)
210           model.setArmed(true);
211         else
212           model.setArmed(false);
213       }
214   }
215
216   /**
217    * Accept a mouse exit event and set the button's model's "rollover"
218    * property to <code>false</code>, if it's "rolloverEnabled" property is
219    * <code>true</code>. Also disarm the button.
220    *
221    * @param e The mouse exit event to accept
222    */
223   public void mouseExited(MouseEvent e)
224   {
225     if (e.getSource() instanceof AbstractButton)
226       {
227         AbstractButton button = (AbstractButton) e.getSource();
228         ButtonModel model = button.getModel();
229         if (button.isRolloverEnabled())
230           model.setRollover(false);
231         model.setArmed(false);
232       }
233   }
234 }