001: /*
002: * $RCSfile: WakeupOnElapsedTime.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: /**
035: * Class specifying a wakeup when a specific number of milliseconds
036: * have elapsed.
037: *
038: */
039: public final class WakeupOnElapsedTime extends WakeupCriterion {
040:
041: long wait;
042:
043: /**
044: * This represents the triggered time
045: */
046: long triggeredTime;
047:
048: /**
049: * Constructs a new WakeupOnElapsedTime criterion.
050: * @param milliseconds the number of milliseconds to the wakeup. A value
051: * of zero or less will cause an IllegalArgumentException to be thrown.
052: */
053: public WakeupOnElapsedTime(long milliseconds) {
054: if (milliseconds <= 0L)
055: throw new IllegalArgumentException(J3dI18N
056: .getString("WakeupOnElapsedTime0"));
057: this .wait = milliseconds;
058: }
059:
060: /**
061: * Retrieve the WakeupCriterion's elapsed time value that was used when
062: * constructing this object.
063: * @return the elapsed time specified when constructing this object
064: */
065: public long getElapsedFrameTime() {
066: return wait;
067: }
068:
069: /**
070: * This is a callback from BehaviorStructure. It is
071: * used to add wakeupCondition to behavior structure.
072: */
073: void addBehaviorCondition(BehaviorStructure bs) {
074: this .triggeredTime = wait + J3dClock.currentTimeMillis();
075: behav.wakeupArray[BehaviorRetained.WAKEUP_TIME_INDEX]++;
076: behav.wakeupMask |= BehaviorRetained.WAKEUP_TIME;
077: VirtualUniverse.mc.timerThread.add(this );
078: }
079:
080: /**
081: * This is a callback from BehaviorStructure. It is
082: * used to remove wakeupCondition from behavior structure.
083: */
084: void removeBehaviorCondition(BehaviorStructure bs) {
085: behav.wakeupArray[BehaviorRetained.WAKEUP_TIME_INDEX]--;
086: if (behav.wakeupArray[BehaviorRetained.WAKEUP_TIME_INDEX] == 0) {
087: behav.wakeupMask &= ~BehaviorRetained.WAKEUP_TIME;
088: }
089: VirtualUniverse.mc.timerThread.remove(this );
090: }
091:
092: /**
093: * This is invoked when Behavior processStimulus can't schedule
094: * to run because behav.active = false. In this case we must
095: * reinsert the wakeupOnElapseTime condition back to the
096: * TimerThread wakeup heap
097: */
098: void reInsertElapseTimeCond() {
099: super .reInsertElapseTimeCond();
100: this .triggeredTime = wait + J3dClock.currentTimeMillis();
101: VirtualUniverse.mc.timerThread.add(this );
102: }
103:
104: /**
105: * Perform task in addBehaviorCondition() that has to be
106: * set every time the condition met.
107: */
108: void resetBehaviorCondition(BehaviorStructure bs) {
109: this.triggeredTime = wait + J3dClock.currentTimeMillis();
110: VirtualUniverse.mc.timerThread.add(this);
111: }
112: }
|