001: /*
002: * $RCSfile: WakeupOnAWTEvent.java,v $
003: *
004: * Copyright 1997-2008 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
006: *
007: * This code is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License version 2 only, as
009: * published by the Free Software Foundation. Sun designates this
010: * particular file as subject to the "Classpath" exception as provided
011: * by Sun in the LICENSE file that accompanied this code.
012: *
013: * This code is distributed in the hope that it will be useful, but WITHOUT
014: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: * version 2 for more details (a copy is included in the LICENSE file that
017: * accompanied this code).
018: *
019: * You should have received a copy of the GNU General Public License version
020: * 2 along with this work; if not, write to the Free Software Foundation,
021: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
022: *
023: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
024: * CA 95054 USA or visit www.sun.com if you need additional information or
025: * have any questions.
026: *
027: * $Revision: 1.6 $
028: * $Date: 2008/02/28 20:17:33 $
029: * $State: Exp $
030: */
031:
032: package javax.media.j3d;
033:
034: import java.awt.AWTEvent;
035: import java.awt.event.*;
036: import java.util.Vector;
037:
038: /**
039: * Class that specifies a Behavior wakeup when a specific AWT event occurs.
040: */
041: public final class WakeupOnAWTEvent extends WakeupCriterion {
042:
043: // different types of WakeupIndexedList that use in BehaviorStructure
044: static final int COND_IN_BS_LIST = 0;
045:
046: // total number of different IndexedUnorderedSet types
047: static final int TOTAL_INDEXED_UNORDER_SET_TYPES = 1;
048:
049: // one of these two variables must be equal to zero
050: int AwtId;
051: long EventMask;
052: long enableAWTEventTS = 0L;
053: Vector events = new Vector();
054:
055: /**
056: * Constructs a new WakeupOnAWTEvent object that informs the Java 3D
057: * scheduler to wake up the specified Behavior object whenever the
058: * specified AWT event occurs.
059: * @param AWTId the AWT ids that this behavior wishes to intercept
060: */
061: public WakeupOnAWTEvent(int AWTId) {
062: this .AwtId = AWTId;
063: this .EventMask = 0L;
064: WakeupIndexedList.init(this , TOTAL_INDEXED_UNORDER_SET_TYPES);
065: }
066:
067: /**
068: * Constructs a new WakeupOnAWTEvent using Ored EVENT_MASK values.
069: * @param eventMask the AWT EVENT_MASK values Ored together
070: */
071: public WakeupOnAWTEvent(long eventMask) {
072: this .EventMask = eventMask;
073: this .AwtId = 0;
074: WakeupIndexedList.init(this , TOTAL_INDEXED_UNORDER_SET_TYPES);
075: }
076:
077: /**
078: * Retrieves the array of consecutive AWT event that triggered this wakeup.
079: * A value of null implies that this event was not the trigger for the
080: * behavior wakeup.
081: * @return either null (if not resposible for wakeup) or the array of
082: * AWTEvents responsible for the wakeup.
083: */
084: public AWTEvent[] getAWTEvent() {
085: AWTEvent eventArray[];
086:
087: synchronized (events) {
088: eventArray = new AWTEvent[events.size()];
089: events.copyInto(eventArray);
090: events.removeAllElements();
091: }
092:
093: return eventArray;
094: }
095:
096: /**
097: * Sets the AWT event that will cause a behavior wakeup.
098: * @param event The event causing this wakeup
099: */
100: void addAWTEvent(AWTEvent event) {
101: events.addElement(event);
102: this .setTriggered();
103: }
104:
105: /**
106: * This is a callback from BehaviorStructure. It is
107: * used to add wakeupCondition to behavior structure.
108: */
109: void addBehaviorCondition(BehaviorStructure bs) {
110: resetBehaviorCondition(bs);
111: bs.wakeupOnAWTEvent.add(this );
112: }
113:
114: /**
115: * This is a callback from BehaviorStructure. It is
116: * used to remove wakeupCondition from behavior structure.
117: */
118: void removeBehaviorCondition(BehaviorStructure bs) {
119: bs.wakeupOnAWTEvent.remove(this );
120: }
121:
122: /**
123: * Perform task in addBehaviorCondition() that has to be
124: * set every time the condition met.
125: */
126: void resetBehaviorCondition(BehaviorStructure bs) {
127: if (enableAWTEventTS != bs.awtEventTimestamp) {
128: if ((AwtId >= ComponentEvent.COMPONENT_FIRST && AwtId <= ComponentEvent.COMPONENT_LAST)
129: || (EventMask & AWTEvent.COMPONENT_EVENT_MASK) != 0) {
130: behav.universe.enableComponentEvents();
131: }
132: if ((AwtId >= FocusEvent.FOCUS_FIRST && AwtId <= FocusEvent.FOCUS_LAST)
133: || (EventMask & AWTEvent.FOCUS_EVENT_MASK) != 0) {
134: behav.universe.enableFocusEvents();
135: }
136: if ((AwtId >= KeyEvent.KEY_FIRST && AwtId <= KeyEvent.KEY_LAST)
137: || (EventMask & AWTEvent.KEY_EVENT_MASK) != 0) {
138: behav.universe.enableKeyEvents();
139: }
140: if ((AwtId >= MouseEvent.MOUSE_FIRST)
141: && (AwtId <= MouseEvent.MOUSE_LAST)) {
142: if ((AwtId == MouseEvent.MOUSE_DRAGGED)
143: || (AwtId == MouseEvent.MOUSE_MOVED)) {
144: behav.universe.enableMouseMotionEvents();
145: } else if (AwtId == MouseEvent.MOUSE_WHEEL) {
146: behav.universe.enableMouseWheelEvents();
147: } else if (AwtId == MouseEvent.MOUSE_CLICKED
148: || AwtId == MouseEvent.MOUSE_ENTERED
149: || AwtId == MouseEvent.MOUSE_EXITED
150: || AwtId == MouseEvent.MOUSE_PRESSED
151: || AwtId == MouseEvent.MOUSE_RELEASED) {
152: behav.universe.enableMouseEvents();
153: }
154: } else {
155: if ((EventMask & AWTEvent.MOUSE_EVENT_MASK) != 0) {
156: behav.universe.enableMouseEvents();
157: }
158: if ((EventMask & AWTEvent.MOUSE_MOTION_EVENT_MASK) != 0) {
159: behav.universe.enableMouseMotionEvents();
160: }
161: if ((EventMask & AWTEvent.MOUSE_WHEEL_EVENT_MASK) != 0) {
162: behav.universe.enableMouseWheelEvents();
163: }
164: }
165: enableAWTEventTS = bs.awtEventTimestamp;
166: }
167: }
168: }
|