001: /*
002: * $RCSfile: LOD.java,v $
003: *
004: * Copyright 1996-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.5 $
028: * $Date: 2008/02/28 20:17:25 $
029: * $State: Exp $
030: */
031:
032: package javax.media.j3d;
033:
034: import java.util.Enumeration;
035: import java.util.Vector;
036:
037: /**
038: * An LOD leaf node is an abstract behavior class that operates on
039: * a list of Switch group nodes to select one of the children of the
040: * Switch nodes.
041: * The LOD class is extended to implement various selection criteria.
042: */
043:
044: public abstract class LOD extends Behavior {
045:
046: /**
047: * Wakeup condition for all LOD nodes
048: */
049: WakeupOnElapsedFrames wakeupFrame = new WakeupOnElapsedFrames(0,
050: true);
051:
052: /**
053: * The LOD Node's vector of switch nodes.
054: */
055: Vector switches = new Vector(5);
056:
057: /**
058: * Constructs and initializes an LOD node.
059: */
060: public LOD() {
061: }
062:
063: /**
064: * Appends the specified switch node to this LOD's list of switches.
065: * @param switchNode the switch node to add to this LOD's list of switches
066: */
067: public void addSwitch(Switch switchNode) {
068: switches.addElement(switchNode);
069: }
070:
071: /**
072: * Replaces the specified switch node with the switch node provided.
073: * @param switchNode the new switch node
074: * @param index which switch node to replace
075: */
076: public void setSwitch(Switch switchNode, int index) {
077: Switch sw = getSwitch(index);
078: switches.setElementAt(switchNode, index);
079: }
080:
081: /**
082: * Inserts the specified switch node at specified index.
083: * @param switchNode the new switch node
084: * @param index position to insert new switch node at
085: */
086: public void insertSwitch(Switch switchNode, int index) {
087: switches.insertElementAt(switchNode, index);
088: }
089:
090: /**
091: * Removes the switch node at specified index.
092: * @param index which switch node to remove
093: */
094: public void removeSwitch(int index) {
095: Switch sw = getSwitch(index);
096: switches.removeElementAt(index);
097: }
098:
099: /**
100: * Returns the switch node specified by the index.
101: * @param index which switch node to return
102: * @return the switch node at location index
103: */
104: public Switch getSwitch(int index) {
105: return (Switch) switches.elementAt(index);
106: }
107:
108: /**
109: * Returns the enumeration object of all switches.
110: * @return the enumeration object of all switches
111: */
112: public Enumeration getAllSwitches() {
113: return switches.elements();
114: }
115:
116: /**
117: * Returns a count of this LOD's switches.
118: * @return the number of switches controlled by this LOD
119: */
120: public int numSwitches() {
121: return switches.size();
122: }
123:
124: /**
125: * Retrieves the index of the specified switch node in
126: * this LOD node's list of switches.
127: *
128: * @param switchNode the switch node to be looked up.
129: * @return the index of the specified switch node;
130: * returns -1 if the object is not in the list.
131: *
132: * @since Java 3D 1.3
133: */
134: public int indexOfSwitch(Switch switchNode) {
135: return switches.indexOf(switchNode);
136: }
137:
138: /**
139: * Removes the specified switch node from this LOD node's
140: * list of switches.
141: * If the specified object is not in the list, the list is not modified.
142: *
143: * @param switchNode the switch node to be removed.
144: *
145: * @since Java 3D 1.3
146: */
147: public void removeSwitch(Switch switchNode) {
148: int index = switches.indexOf(switchNode);
149: if (index >= 0)
150: removeSwitch(index);
151: }
152:
153: /**
154: * Removes all switch nodes from this LOD node.
155: *
156: * @since Java 3D 1.3
157: */
158: public void removeAllSwitches() {
159: int numSwitches = switches.size();
160:
161: // Remove in reverse order to ensure valid indices
162: for (int index = numSwitches - 1; index >= 0; index--) {
163: removeSwitch(index);
164: }
165: }
166:
167: /**
168: * Copies all LOD information from
169: * <code>originalNode</code> into
170: * the current node. This method is called from the
171: * <code>cloneNode</code> method which is, in turn, called by the
172: * <code>cloneTree</code> method.<P>
173: *
174: * @param originalNode the original node to duplicate.
175: * @param forceDuplicate when set to <code>true</code>, causes the
176: * <code>duplicateOnCloneTree</code> flag to be ignored. When
177: * <code>false</code>, the value of each node's
178: * <code>duplicateOnCloneTree</code> variable determines whether
179: * NodeComponent data is duplicated or copied.
180: *
181: * @exception RestrictedAccessException if this object is part of a live
182: * or compiled scenegraph.
183: *
184: * @see Node#duplicateNode
185: * @see Node#cloneTree
186: * @see NodeComponent#setDuplicateOnCloneTree
187: */
188: void duplicateAttributes(Node originalNode, boolean forceDuplicate) {
189: super .duplicateAttributes(originalNode, forceDuplicate);
190:
191: LOD lod = (LOD) originalNode;
192:
193: int numSwitch = lod.numSwitches();
194: for (int i = 0; i < numSwitch; i++) {
195: addSwitch(lod.getSwitch(i));
196: }
197: }
198:
199: /**
200: * Callback used to allow a node to check if any nodes referenced
201: * by that node have been duplicated via a call to <code>cloneTree</code>.
202: * This method is called by <code>cloneTree</code> after all nodes in
203: * the sub-graph have been duplicated. The cloned Leaf node's method
204: * will be called and the Leaf node can then look up any node references
205: * by using the <code>getNewObjectReference</code> method found in the
206: * <code>NodeReferenceTable</code> object. If a match is found, a
207: * reference to the corresponding Node in the newly cloned sub-graph
208: * is returned. If no corresponding reference is found, either a
209: * DanglingReferenceException is thrown or a reference to the original
210: * node is returned depending on the value of the
211: * <code>allowDanglingReferences</code> parameter passed in the
212: * <code>cloneTree</code> call.
213: * <p>
214: * NOTE: Applications should <i>not</i> call this method directly.
215: * It should only be called by the cloneTree method.
216: *
217: * @param referenceTable a NodeReferenceTableObject that contains the
218: * <code>getNewObjectReference</code> method needed to search for
219: * new object instances.
220: * @see NodeReferenceTable
221: * @see Node#cloneTree
222: * @see DanglingReferenceException
223: */
224: public void updateNodeReferences(NodeReferenceTable referenceTable) {
225: int numSwitch = numSwitches();
226:
227: for (int i = 0; i < numSwitch; i++) {
228: Switch curSwitch = getSwitch(i);
229: if (curSwitch != null) {
230: setSwitch((Switch) referenceTable
231: .getNewObjectReference(curSwitch), i);
232: }
233: }
234: }
235: }
|