001: /*
002: * $RCSfile: NonSharedNode.java,v $
003: *
004: * @(#)NonSharedNode.java 1.18 99/03/16 14:29:41
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:06:58 $
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 javax.media.j3d.Link;
044:
045: import javax.media.j3d.SharedGroup;
046:
047: /** Description of the Class */
048: public abstract class NonSharedNode extends Node {
049: SharedGroup sharedGroup = null;
050: boolean linkTested = false;
051: boolean linkable = true;
052:
053: /**
054: *Constructor for the NonSharedNode object
055: *
056: *@param loader Description of the Parameter
057: */
058: NonSharedNode(Loader loader) {
059: super (loader);
060: }
061:
062: /**
063: * Description of the Method
064: *
065: *@param scene Description of the Parameter
066: */
067: public void registerUse(Scene scene) {
068: if (loader.debug) {
069: System.out
070: .println("Use of non-sharable tree " + defName
071: + " = " + this .toStringId() + " impl = "
072: + implNode);
073: }
074: // see if the tree is linkable
075: if (!linkTested) {
076: if (Leafer.has(implNode, Leafer.UNLINKABLE)) {
077: linkable = false;
078: }
079: if (loader.debug) {
080: System.out.println("Tested, linkable = " + linkable);
081: }
082: linkTested = true;
083: }
084: if (linkable) {
085: if (sharedGroup == null) {
086: // need to create a shared group and update the reference
087: sharedGroup = new SharedGroup();
088: scene.addSharedGroup(sharedGroup);
089:
090: javax.media.j3d.Node parent;
091: if ((parent = implNode.getParent()) != null) {
092: javax.media.j3d.Group parentGroup = (javax.media.j3d.Group) parent;
093: boolean found = false;
094: for (int i = 0; i < parentGroup.numChildren(); i++) {
095: javax.media.j3d.Node child = (javax.media.j3d.Node) parentGroup
096: .getChild(i);
097: if (child == implNode) {
098: found = true;
099: Link link = new Link(sharedGroup);
100: parentGroup.setChild(link, i);
101: if (loader.debug) {
102: System.out
103: .println("Updated reference to "
104: + implNode
105: + " in parent "
106: + parentGroup
107: + " to link " + link);
108: }
109: }
110: }
111: if (loader.debug && (found == false)) {
112: System.out.println("Could not find " + implNode
113: + " in parent " + parentGroup);
114: }
115: }
116:
117: // now the child should no longer be attached to a parent
118: // (we replaced it with a link), so we can do this without
119: // causing a multiple parent exception
120: sharedGroup.addChild(implNode);
121: if (loader.debug) {
122: System.out.println("nonShared tree: "
123: + this .toStringId()
124: + " is now in SharedGroup " + sharedGroup);
125: }
126: }
127: } else {
128: // Node unlinkable. First use will use implNode, after will
129: // use clones
130: }
131: }
132:
133: /**
134: * Gets the implNode attribute of the NonSharedNode object
135: *
136: *@return The implNode value
137: */
138: public javax.media.j3d.Node getImplNode() {
139: if (linkTested && !linkable) {
140: if (implNode.getParent() == null) {
141: return implNode;
142: } else {
143: // need to return a clone
144: if (loader.debug) {
145: System.out
146: .println("cloning a non linkable subtree:"
147: + implNode);
148: //loader.treePrinter.print(implNode);
149: }
150: javax.media.j3d.Node clone = implNode.cloneTree(false,
151: true);
152: if (implNode.getUserData() != null) {
153: clone.setUserData(implNode.getUserData());
154: }
155: if (loader.debug) {
156: System.out.println("cloning is: " + clone);
157: }
158: return clone;
159: }
160: } else {
161: if (sharedGroup != null) {
162: return (javax.media.j3d.Node) new Link(sharedGroup);
163: } else {
164: return implNode;
165: }
166: }
167: }
168: }
|