001: /*
002: * $RCSfile: Switch.java,v $
003: *
004: * @(#)Switch.java 1.21 98/11/05 20:35:24
005: *
006: * Copyright (c) 1996-1998 Sun Microsystems, Inc. All Rights Reserved.
007: *
008: * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
009: * modify and redistribute this software in source and binary code form,
010: * provided that i) this copyright notice and license appear on all copies of
011: * the software; and ii) Licensee does not utilize the software in a manner
012: * which is disparaging to Sun.
013: *
014: * This software is provided "AS IS," without a warranty of any kind. ALL
015: * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
016: * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
017: * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
018: * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
019: * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
020: * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
021: * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
022: * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
023: * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
024: * POSSIBILITY OF SUCH DAMAGES.
025: *
026: * This software is not designed or intended for use in on-line control of
027: * aircraft, air traffic, aircraft navigation or aircraft communications; or in
028: * the design, construction, operation or maintenance of any nuclear
029: * facility. Licensee represents and warrants that it will not use or
030: * redistribute the Software for such purposes.
031: *
032: * $Revision: 1.2 $
033: * $Date: 2005/02/03 23:07:02 $
034: * $State: Exp $
035: */
036: /*
037: * @Author: Rick Goldberg
038: * @Author: Doug Gehringer
039: *
040: */
041: package org.jdesktop.j3d.loaders.vrml97.impl;
042:
043: import java.util.Vector;
044:
045: /** Description of the Class */
046: public class Switch extends NonSharedNode {
047: // exposedField
048: MFNode choice;
049:
050: // field
051: SFInt32 whichChoice;
052:
053: javax.media.j3d.Switch impl;
054:
055: /**
056: *Constructor for the Switch object
057: *
058: *@param loader Description of the Parameter
059: */
060: public Switch(Loader loader) {
061: super (loader);
062: choice = new MFNode();
063: whichChoice = new SFInt32(-1);
064: initFields();
065: }
066:
067: /**
068: *Constructor for the Switch object
069: *
070: *@param loader Description of the Parameter
071: *@param choice Description of the Parameter
072: *@param whichChoice Description of the Parameter
073: */
074: Switch(Loader loader, MFNode choice, SFInt32 whichChoice) {
075: super (loader);
076: this .choice = choice;
077: this .whichChoice = whichChoice;
078: initFields();
079: }
080:
081: /** Description of the Method */
082: void initImpl() {
083: if (loader.debug) {
084: System.out.println("Switch.initImpl() called");
085: }
086: impl = new javax.media.j3d.Switch();
087: impl.setCapability(javax.media.j3d.Switch.ALLOW_SWITCH_READ);
088: impl.setCapability(javax.media.j3d.Switch.ALLOW_SWITCH_WRITE);
089: impl.setCapability(javax.media.j3d.Group.ALLOW_CHILDREN_READ);
090: impl.setCapability(javax.media.j3d.Group.ALLOW_CHILDREN_WRITE);
091: impl.setCapability(javax.media.j3d.Group.ALLOW_CHILDREN_EXTEND);
092: impl.setUserData(new Vector());
093: replaceChoices();
094: setWhichChild();
095: if (loader.debug) {
096: System.out.println("Switch " + toStringId() + " impl is "
097: + impl);
098: }
099: implNode = impl;
100: implReady = true;
101: }
102:
103: /** Sets the whichChild attribute of the Switch object */
104: void setWhichChild() {
105: if (browser.debug) {
106: System.out.println("Switch: setting " + whichChoice.value);
107: }
108: if (whichChoice.value < 0
109: || whichChoice.value > impl.numChildren()) {
110: impl.setWhichChild(javax.media.j3d.Switch.CHILD_NONE);
111: } else {
112: impl.setWhichChild(whichChoice.value);
113: }
114: }
115:
116: // the choice field has been updated. Remove any nodes on the impl
117: // and add the current list to the impl
118: /** Description of the Method */
119: void replaceChoices() {
120: int numChildren;
121: if ((numChildren = impl.numChildren()) != 0) {
122: for (int i = 0; i < numChildren; i++) {
123: impl.removeChild(0);// should shift the rest down...
124: }
125: }
126: for (int i = 0; i < choice.nodes.length; i++) {
127: BaseNode child = choice.nodes[i];
128:
129: // let the new child know this is their parent
130: child.updateParent(impl);
131:
132: // add the child's impl to the j3d group
133: javax.media.j3d.Node implNode = child.getImplNode();
134: if (loader.debug) {
135: System.out.println(this .toStringId() + ": index = "
136: + i + " child node type is " + child.getType()
137: + " " + child.toStringId()
138: + " gets implNoded to " + implNode);
139: }
140: if (implNode != null) {
141: if (implNode.getParent() == null) {
142: impl.addChild(implNode);
143: } else {
144: impl.addChild(implNode.cloneNode(true));
145: }
146: if (child instanceof DirectionalLight) {
147: DirectionalLight dirLight = (DirectionalLight) child;
148: dirLight.setScope(impl);
149: }
150: }
151: }
152: }
153:
154: // return the number of tris in choice 0
155: /**
156: * Gets the numTris attribute of the Switch object
157: *
158: *@return The numTris value
159: */
160: public int getNumTris() {
161: int numTris = 0;
162: if ((choice != null) && (choice.nodes != null)
163: && (choice.nodes.length > 0)) {
164: numTris += choice.nodes[0].getNumTris();
165: }
166: return numTris;
167: }
168:
169: /**
170: * Description of the Method
171: *
172: *@param eventInName Description of the Parameter
173: *@param time Description of the Parameter
174: */
175: public void notifyMethod(String eventInName, double time) {
176: if (eventInName.equals("choice")) {
177: replaceChoices();
178: } else if (eventInName.equals("whichChoice")) {
179: setWhichChild();
180: }
181: }
182:
183: /**
184: * Description of the Method
185: *
186: *@return Description of the Return Value
187: */
188: public Object clone() {
189: return new Switch(loader, (MFNode) choice.clone(),
190: (SFInt32) whichChoice.clone());
191: }
192:
193: /**
194: * Gets the type attribute of the Switch object
195: *
196: *@return The type value
197: */
198: public String getType() {
199: return "Switch";
200: }
201:
202: /** Description of the Method */
203: void initFields() {
204: choice.init(this , FieldSpec, Field.EXPOSED_FIELD, "choice");
205: whichChoice.init(this , FieldSpec, Field.EXPOSED_FIELD,
206: "whichChoice");
207: }
208:
209: }
|