001: /*
002: * $RCSfile: ProtoInstance.java,v $
003: *
004: * @(#)ProtoInstance.java 1.15 99/03/24 15:33:33
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:00 $
034: * $State: Exp $
035: */
036: /*
037: * @Author: Rick Goldberg
038: * @Author: Doug Gehringer
039: */
040: package org.jdesktop.j3d.loaders.vrml97.impl;
041:
042: import java.util.Enumeration;
043: import java.util.Hashtable;
044: import java.util.Vector;
045:
046: /** Description of the Class */
047: public class ProtoInstance extends Node implements Namespace {
048:
049: String name;
050: Proto proto;
051: Hashtable defTable = new Hashtable();
052: BaseNode instanceNode;
053: Vector routes;
054: Hashtable fieldMapping = new Hashtable();
055: Hashtable nodeMapping = new Hashtable();
056: // need to get the updateParent() correct
057: boolean containsSensor = false;
058: Vector sensors;
059:
060: /**
061: *Constructor for the ProtoInstance object
062: *
063: *@param loader Description of the Parameter
064: *@param name Description of the Parameter
065: *@param proto Description of the Parameter
066: */
067: ProtoInstance(Loader loader, String name, Proto proto) {
068: super (loader);
069: this .name = name;
070: this .proto = proto;
071: this .routes = proto.routes;
072: }
073:
074: /**
075: * Adds a feature to the Field attribute of the ProtoInstance object
076: *
077: *@param field The feature to be added to the Field attribute
078: *@return Description of the Return Value
079: */
080: Field addField(Field field) {
081: Field newField;
082: if (!fieldMapping.containsKey(field)) {
083: newField = (Field) field.clone();
084: fieldMapping.put(field, newField);
085: newField.init(this , FieldSpec, field.fieldType,
086: field.fieldName);
087: } else {
088: newField = (Field) fieldMapping.get(field);
089: }
090: return newField;
091: }
092:
093: /** Description of the Method */
094: void initFields() {
095: // clone the Proto's fields and event mappings
096: // handle the fields first
097: for (Enumeration e = proto.FieldSpec.keys(); e
098: .hasMoreElements();) {
099: String fieldName = (String) e.nextElement();
100: Field field = (Field) proto.FieldSpec.get(fieldName);
101: //System.err.println("ProtoInstance.initFields(): got field " +
102: // "named " + fieldName + " with value " + field);
103: Field newField;
104: if ((field instanceof ConstField)) {
105: newField = addField(((ConstField) field).ownerField);
106: FieldSpec.put(fieldName, newField.constify());
107: } else {
108: newField = addField(field);
109: FieldSpec.put(fieldName, newField);
110: }
111: }
112: }
113:
114: /**
115: * Description of the Method
116: *
117: *@param defName Description of the Parameter
118: *@param node Description of the Parameter
119: */
120: public void define(String defName, BaseNode node) {
121: //System.out.println("ProtoInstance.define() name: " + defName +
122: // "value: " + node);
123: defTable.put(defName, node);
124: }
125:
126: /**
127: * Description of the Method
128: *
129: *@param defName Description of the Parameter
130: *@return Description of the Return Value
131: */
132: public BaseNode use(String defName) {
133: BaseNode retval = (BaseNode) defTable.get(defName);
134: //System.out.println("ProtoInstance.use(" + defName + ") " +
135: // " returned " + " node " + retval.toStringId());
136: retval.registerUse(loader.scene);
137: return retval;
138: }
139:
140: /**
141: * Description of the Method
142: *
143: *@param node Description of the Parameter
144: *@return Description of the Return Value
145: */
146: BaseNode updateNode(BaseNode node) {
147: BaseNode retval;
148: if (node == null) {
149: return this ;
150: }
151: retval = (BaseNode) nodeMapping.get(node);
152: //System.out.println("some strange method called updateNode "+node);
153: //System.out.println("mapped to "+retval);
154: if (retval == null) {
155: System.err.println("ProtoInstance: Unable to update node "
156: + "reference: " + node.toStringId());
157: }
158: return retval;
159: }
160:
161: /** Description of the Method */
162: void applyRoutes() {
163: // apply the routes, updating the nodes
164: //System.out.println("ProtoInstance: applying routes:");
165: for (Enumeration e = routes.elements(); e.hasMoreElements();) {
166: Route route = (Route) e.nextElement();
167: BaseNode fromNode;
168: BaseNode toNode;
169: fromNode = updateNode(route.fromNode);
170: toNode = updateNode(route.toNode);
171: //System.out.println("Add route: " + fromNode + " . " +
172: //route.fromField + "\n to " + toNode + " . " + route.toField);
173:
174: // bug: after fixing 4221802 the multinoded protodef somehow
175: // a "?" field was trying to get addRouted
176: if (!route.fromField.equals("?")
177: && !route.toField.equals("?")) {
178: loader.connect(fromNode, route.fromField, toNode,
179: route.toField, false);
180: }
181: }
182: }
183:
184: /**
185: * Description of the Method
186: *
187: *@return Description of the Return Value
188: */
189: BaseNode instanceNode() {
190: return instanceNode;
191: }
192:
193: /**
194: * Gets the implNode attribute of the ProtoInstance object
195: *
196: *@return The implNode value
197: */
198: public javax.media.j3d.Node getImplNode() {
199: return instanceNode.getImplNode();
200: }
201:
202: // dummy methods to satisfy BaseNode interface
203: /**
204: * Description of the Method
205: *
206: *@param eventInName Description of the Parameter
207: *@param time Description of the Parameter
208: */
209: public void notifyMethod(String eventInName, double time) {
210: }
211:
212: /**
213: * Gets the type attribute of the ProtoInstance object
214: *
215: *@return The type value
216: */
217: public String getType() {
218: return name;
219: }
220:
221: /**
222: * Description of the Method
223: *
224: *@return Description of the Return Value
225: */
226: public Object clone() {
227: System.err.println("Warning: clone called on a ProtoInstance!");
228: return null;
229: }
230:
231: /**
232: * Description of the Method
233: *
234: *@return Description of the Return Value
235: */
236: public String toStringBody() {
237: return "PROTO " + name + " {\n" + /* TODO: fields here */"}\n";
238: }
239: }
|