001: /*
002: * $RCSfile: KeyNavigatorBehavior.java,v $
003: *
004: * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * - Redistribution of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * - Redistribution in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * Neither the name of Sun Microsystems, Inc. or the names of
019: * contributors may be used to endorse or promote products derived
020: * from this software without specific prior written permission.
021: *
022: * This software is provided "AS IS," without a warranty of any
023: * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
024: * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
025: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
026: * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
027: * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
028: * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
029: * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR
030: * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
031: * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
032: * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
033: * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
034: * POSSIBILITY OF SUCH DAMAGES.
035: *
036: * You acknowledge that this software is not designed, licensed or
037: * intended for use in the design, construction, operation or
038: * maintenance of any nuclear facility.
039: *
040: * $Revision: 1.4 $
041: * $Date: 2007/02/09 17:20:12 $
042: * $State: Exp $
043: */
044:
045: package com.sun.j3d.utils.behaviors.keyboard;
046:
047: import java.awt.event.*;
048: import java.awt.AWTEvent;
049: import java.util.Enumeration;
050: import java.awt.Component;
051: import java.util.LinkedList;
052: import javax.vecmath.*;
053: import javax.media.j3d.*;
054: import com.sun.j3d.internal.J3dUtilsI18N;
055:
056: /**
057: * This class is a simple behavior that invokes the KeyNavigator
058: * to modify the view platform transform.
059: */
060: public class KeyNavigatorBehavior extends Behavior implements
061: KeyListener {
062: private WakeupCriterion w1 = new WakeupOnAWTEvent(
063: KeyEvent.KEY_PRESSED);
064: private WakeupCriterion w2 = new WakeupOnAWTEvent(
065: KeyEvent.KEY_RELEASED);
066: private WakeupOnElapsedFrames w3 = new WakeupOnElapsedFrames(0);
067: private WakeupCriterion[] warray = { w1, w2, w3 };
068: private WakeupCondition w = new WakeupOr(warray);
069: private KeyEvent eventKey;
070: private KeyNavigator keyNavigator;
071: private boolean listener = false;
072:
073: private LinkedList eventq;
074:
075: /**
076: * Override Behavior's initialize method to setup wakeup criteria.
077: */
078: public void initialize() {
079: // Establish initial wakeup criteria
080: if (listener) {
081: w1 = new WakeupOnBehaviorPost(this , KeyEvent.KEY_PRESSED);
082: w2 = new WakeupOnBehaviorPost(this , KeyEvent.KEY_RELEASED);
083: warray[0] = w1;
084: warray[1] = w2;
085: w = new WakeupOr(warray);
086: eventq = new LinkedList();
087: }
088: wakeupOn(w);
089: }
090:
091: /**
092: * Override Behavior's stimulus method to handle the event.
093: */
094: public void processStimulus(Enumeration criteria) {
095: WakeupOnAWTEvent ev;
096: WakeupCriterion genericEvt;
097: AWTEvent[] events;
098: boolean sawFrame = false;
099:
100: while (criteria.hasMoreElements()) {
101: genericEvt = (WakeupCriterion) criteria.nextElement();
102: if (genericEvt instanceof WakeupOnAWTEvent) {
103: ev = (WakeupOnAWTEvent) genericEvt;
104: events = ev.getAWTEvent();
105: processAWTEvent(events);
106: } else if (genericEvt instanceof WakeupOnElapsedFrames
107: && eventKey != null) {
108: sawFrame = true;
109: } else if ((genericEvt instanceof WakeupOnBehaviorPost)) {
110: while (true) {
111: // access to the queue must be synchronized
112: synchronized (eventq) {
113: if (eventq.isEmpty())
114: break;
115: eventKey = (KeyEvent) eventq.remove(0);
116: if (eventKey.getID() == KeyEvent.KEY_PRESSED
117: || eventKey.getID() == KeyEvent.KEY_RELEASED) {
118: keyNavigator.processKeyEvent(eventKey);
119: }
120: }
121: }
122: }
123: }
124: if (sawFrame)
125: keyNavigator.integrateTransformChanges();
126:
127: // Set wakeup criteria for next time
128: wakeupOn(w);
129: }
130:
131: /**
132: * Process a keyboard event
133: */
134: private void processAWTEvent(AWTEvent[] events) {
135: for (int loop = 0; loop < events.length; loop++) {
136: if (events[loop] instanceof KeyEvent) {
137: eventKey = (KeyEvent) events[loop];
138: // change the transformation; for example to zoom
139: if (eventKey.getID() == KeyEvent.KEY_PRESSED
140: || eventKey.getID() == KeyEvent.KEY_RELEASED) {
141: //System.out.println("Keyboard is hit! " + eventKey);
142: keyNavigator.processKeyEvent(eventKey);
143: }
144: }
145: }
146: }
147:
148: /**
149: * Adds this behavior as a KeyListener to the specified component.
150: * This method can only be called if
151: * the behavior was created with one of the constructors that takes
152: * a Component as a parameter.
153: * @param c The component to add the KeyListener to.
154: * @exception IllegalStateException if the behavior was not created
155: * as a listener
156: * @since Java 3D 1.2.1
157: */
158: public void addListener(Component c) {
159: if (!listener) {
160: throw new IllegalStateException(J3dUtilsI18N
161: .getString("Behavior0"));
162: }
163: c.addKeyListener(this );
164: }
165:
166: /**
167: * Constructs a new key navigator behavior node that operates
168: * on the specified transform group.
169: * @param targetTG the target transform group
170: */
171: public KeyNavigatorBehavior(TransformGroup targetTG) {
172: keyNavigator = new KeyNavigator(targetTG);
173: }
174:
175: /**
176: * Constructs a key navigator behavior that uses AWT listeners
177: * and behavior posts rather than WakeupOnAWTEvent. The behavior
178: * is added to the specified Component and works on the given
179: * TransformGroup. A null component can be passed to specify
180: * the behavior should use listeners. Components can then be added
181: * to the behavior with the addListener(Component c) method.
182: * @param c The component to add the KeyListener to.
183: * @param targetTG The target transform group.
184: * @since Java 3D 1.2.1
185: */
186: public KeyNavigatorBehavior(Component c, TransformGroup targetTG) {
187: this (targetTG);
188: if (c != null) {
189: c.addKeyListener(this );
190: }
191: listener = true;
192: }
193:
194: public void keyPressed(KeyEvent evt) {
195: // System.out.println("keyPressed");
196:
197: // add new event to the queue
198: // must be MT safe
199: synchronized (eventq) {
200: eventq.add(evt);
201: // only need to post if this is the only event in the queue
202: if (eventq.size() == 1)
203: postId(KeyEvent.KEY_PRESSED);
204: }
205: }
206:
207: public void keyReleased(KeyEvent evt) {
208: // System.out.println("keyReleased");
209:
210: // add new event to the queue
211: // must be MT safe
212: synchronized (eventq) {
213: eventq.add(evt);
214: // only need to post if this is the only event in the queue
215: if (eventq.size() == 1)
216: postId(KeyEvent.KEY_RELEASED);
217: }
218: }
219:
220: public void keyTyped(KeyEvent evt) {
221: }
222:
223: }
|