001: /*
002:
003: Licensed to the Apache Software Foundation (ASF) under one or more
004: contributor license agreements. See the NOTICE file distributed with
005: this work for additional information regarding copyright ownership.
006: The ASF licenses this file to You under the Apache License, Version 2.0
007: (the "License"); you may not use this file except in compliance with
008: the License. You may obtain a copy of the License at
009:
010: http://www.apache.org/licenses/LICENSE-2.0
011:
012: Unless required by applicable law or agreed to in writing, software
013: distributed under the License is distributed on an "AS IS" BASIS,
014: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: See the License for the specific language governing permissions and
016: limitations under the License.
017:
018: */
019: package org.apache.batik.gvt.event;
020:
021: import java.awt.event.InputEvent;
022: import java.awt.event.KeyEvent;
023: import java.awt.event.MouseWheelEvent;
024: import java.awt.event.MouseWheelListener;
025: import java.util.EventObject;
026:
027: import org.apache.batik.gvt.GraphicsNode;
028:
029: /**
030: * A concrete version of {@link org.apache.batik.gvt.event.AWTEventDispatcher}.
031: *
032: * This class is used for JDKs >= 1.4, which have MouseWheelEvent
033: * support. For JDKs < 1.4, the file
034: * sources-1.3/org/apache/batik/gvt/event/AWTEventDispatcher defines a
035: * version of this class that does not support MouseWheelEvents.
036: *
037: * @author <a href="mailto:cam%40mcc%2eid%2eau">Cameron McCormack</a>
038: * @version $Id: AWTEventDispatcher.java 498514 2007-01-22 03:03:41Z cam $
039: */
040: public class AWTEventDispatcher extends AbstractAWTEventDispatcher
041: implements MouseWheelListener {
042:
043: /**
044: * Dispatches the specified AWT mouse wheel event down to the GVT tree.
045: * The mouse wheel event is mutated to a GraphicsNodeMouseWheelEvent.
046: * @param evt the mouse event to propagate
047: */
048: public void mouseWheelMoved(MouseWheelEvent evt) {
049: dispatchEvent(evt);
050: }
051:
052: /**
053: * Dispatches the specified AWT event.
054: * @param evt the event to dispatch
055: */
056: public void dispatchEvent(EventObject evt) {
057: if (evt instanceof MouseWheelEvent) {
058: if (root == null) // No root do not store anything.
059: return;
060: if (!eventDispatchEnabled) {
061: if (eventQueueMaxSize > 0) {
062: eventQueue.add(evt);
063: while (eventQueue.size() > eventQueueMaxSize)
064: // Limit how many events we queue - don't want
065: // user waiting forever for them to clear.
066: eventQueue.remove(0);
067: }
068: return;
069: }
070: dispatchMouseWheelEvent((MouseWheelEvent) evt);
071: } else {
072: super .dispatchEvent(evt);
073: }
074: }
075:
076: /**
077: * Dispatches the specified AWT mouse wheel event.
078: * @param evt the mouse wheel event to dispatch
079: */
080: protected void dispatchMouseWheelEvent(MouseWheelEvent evt) {
081: if (lastHit != null) {
082: processMouseWheelEvent(new GraphicsNodeMouseWheelEvent(
083: lastHit, evt.getID(), evt.getWhen(), evt
084: .getModifiersEx(), getCurrentLockState(),
085: evt.getWheelRotation()));
086: }
087: }
088:
089: /**
090: * Processes the specified event by firing the 'global' listeners
091: * attached to this event dispatcher.
092: * @param evt the event to process
093: */
094: protected void processMouseWheelEvent(
095: GraphicsNodeMouseWheelEvent evt) {
096: if (glisteners != null) {
097: GraphicsNodeMouseWheelListener[] listeners = (GraphicsNodeMouseWheelListener[]) getListeners(GraphicsNodeMouseWheelListener.class);
098: for (int i = 0; i < listeners.length; i++) {
099: listeners[i].mouseWheelMoved(evt);
100: }
101: }
102: }
103:
104: /**
105: * Dispatches the specified AWT key event.
106: * @param evt the key event to dispatch
107: */
108: protected void dispatchKeyEvent(KeyEvent evt) {
109: currentKeyEventTarget = lastHit;
110: GraphicsNode target = currentKeyEventTarget == null ? root
111: : currentKeyEventTarget;
112: processKeyEvent(new GraphicsNodeKeyEvent(target, evt.getID(),
113: evt.getWhen(), evt.getModifiersEx(),
114: getCurrentLockState(), evt.getKeyCode(), evt
115: .getKeyChar(), evt.getKeyLocation()));
116: }
117:
118: /**
119: * Returns the modifiers mask for this event. This just calls
120: * {@link InputEvent#getModifiersEx()} on <code>evt</code>.
121: */
122: protected int getModifiers(InputEvent evt) {
123: return evt.getModifiersEx();
124: }
125:
126: /**
127: * Returns whether the meta key is down according to the given modifiers
128: * bitfield.
129: */
130: protected static boolean isMetaDown(int modifiers) {
131: return (modifiers & (1 << 8)) != 0; /* META_DOWN_MASK */
132: }
133: }
|