View Javadoc

1   /*
2    * Copyright 2006-2016 The JGUIraffe Team.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License")
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package net.sf.jguiraffe.gui.platform.swing.builder.event;
17  
18  import java.awt.event.MouseEvent;
19  import java.awt.event.MouseListener;
20  
21  import net.sf.jguiraffe.gui.builder.event.FormEventManager;
22  import net.sf.jguiraffe.gui.builder.event.FormListenerType;
23  import net.sf.jguiraffe.gui.builder.event.FormMouseEvent;
24  import net.sf.jguiraffe.gui.builder.event.FormMouseListener;
25  import net.sf.jguiraffe.gui.forms.ComponentHandler;
26  
27  /**
28   * <p>
29   * A specific Swing event adapter implementation that deals with mouse events.
30   * </p>
31   *
32   * @author Oliver Heger
33   * @version $Id: MouseEventAdapter.java 205 2012-01-29 18:29:57Z oheger $
34   */
35  public class MouseEventAdapter extends SwingEventAdapter implements MouseListener
36  {
37      /**
38       * Creates a new instance of {@code MouseEventAdapter} that passes the
39       * events it receives to the specified {@code FormMouseListener}.
40       *
41       * @param l the {@code FormMouseListener} (must not be <b>null</b>)
42       * @param handler the {@code ComponentHandler}
43       * @param name the name of the component
44       * @throws IllegalArgumentException if the {@code FormMouseListener} is
45       *         <b>null</b>
46       */
47      public MouseEventAdapter(FormMouseListener l, ComponentHandler<?> handler,
48              String name)
49      {
50          super(l, handler, name);
51      }
52  
53      /**
54       * Creates a new instance of {@code MouseEventAdapter} that passes the
55       * events it receives to the {@code FormEventManager}.
56       *
57       * @param eventManager the {@code FormEventManager} (must not be
58       *        <b>null</b>)
59       * @param handler the {@code ComponentHandler}
60       * @param name the name of the component
61       * @throws IllegalArgumentException if the {@code FormEventManager} is
62       *         <b>null</b>
63       */
64      public MouseEventAdapter(FormEventManager eventManager,
65              ComponentHandler<?> handler, String name)
66      {
67          super(eventManager, handler, name);
68      }
69  
70      /**
71       * Notifies this listener about a mouse entered event. This implementation
72       * checks the {@code clickCount} property to find out whether this is a
73       * normal click or a double click. This determines the type of the
74       * corresponding {@code FormMouseEvent}. Then such a {@code FormMouseEvent}
75       * is created and passed to the
76       * {@link #fireEvent(net.sf.jguiraffe.gui.builder.event.FormEvent)} method.
77       *
78       * @param event the Swing mouse event
79       */
80      public void mouseClicked(MouseEvent event)
81      {
82          fireEvent(createEvent(
83                  event,
84                  (event.getClickCount() > 1) ? FormMouseEvent.Type.MOUSE_DOUBLE_CLICKED
85                          : FormMouseEvent.Type.MOUSE_CLICKED));
86      }
87  
88      /**
89       * Notifies this listener about a mouse entered event. This implementation
90       * creates a corresponding {@code FormMouseEvent} and passes it to the
91       * {@link #fireEvent(net.sf.jguiraffe.gui.builder.event.FormEvent)} method.
92       *
93       * @param event the Swing mouse event
94       */
95      public void mouseEntered(MouseEvent event)
96      {
97          fireEvent(createEvent(event, FormMouseEvent.Type.MOUSE_ENTERED));
98      }
99  
100     /**
101      * Notifies this listener about a mouse exited event. This implementation
102      * creates a corresponding {@code FormMouseEvent} and passes it to the
103      * {@link #fireEvent(net.sf.jguiraffe.gui.builder.event.FormEvent)} method.
104      *
105      * @param event the Swing mouse event
106      */
107     public void mouseExited(MouseEvent event)
108     {
109         fireEvent(createEvent(event, FormMouseEvent.Type.MOUSE_EXITED));
110     }
111 
112     /**
113      * Notifies this listener about a mouse pressed event. This implementation
114      * creates a corresponding {@code FormMouseEvent} and passes it to the
115      * {@link #fireEvent(net.sf.jguiraffe.gui.builder.event.FormEvent)} method.
116      *
117      * @param event the Swing mouse event
118      */
119     public void mousePressed(MouseEvent event)
120     {
121         fireEvent(createEvent(event, FormMouseEvent.Type.MOUSE_PRESSED));
122     }
123 
124     /**
125      * Notifies this listener about a mouse released event. This implementation
126      * creates a corresponding {@code FormMouseEvent} and passes it to the
127      * {@link #fireEvent(net.sf.jguiraffe.gui.builder.event.FormEvent)} method.
128      *
129      * @param event the Swing mouse event
130      */
131     public void mouseReleased(MouseEvent event)
132     {
133         fireEvent(createEvent(event, FormMouseEvent.Type.MOUSE_RELEASED));
134     }
135 
136     /**
137      * Returns the {@code FormListenerType} for this event adapter. This
138      * implementation returns the type for mouse listeners.
139      *
140      * @return the {@code FormListenerType}
141      */
142     @Override
143     protected FormListenerType getListenerType()
144     {
145         return FormListenerType.MOUSE;
146     }
147 
148     /**
149      * Creates a {@code FormMouseEvent} from the specified source Swing event
150      * using the given event type.
151      *
152      * @param srcEvent the source Swing event
153      * @param type the event type
154      * @return the new {@code FormMouseEvent}
155      */
156     protected FormMouseEvent createEvent(MouseEvent srcEvent,
157             FormMouseEvent.Type type)
158     {
159         return new FormMouseEvent(srcEvent, getHandler(), getName(), type,
160                 srcEvent.getX(), srcEvent.getY(), SwingEventConstantMapper
161                         .convertSwingButtons(srcEvent.getButton()),
162                 SwingEventConstantMapper.convertSwingModifiers(srcEvent
163                         .getModifiers()));
164     }
165 }