001: /*
002: * $RCSfile: GroupBase.java,v $
003: *
004: * @(#)GroupBase.java 1.30 99/03/24 15:32:53
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:56 $
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.*;
044:
045: /** Description of the Class */
046: public abstract class GroupBase extends NonSharedNode {
047:
048: // eventIn
049: MFNode addChildren;// to comply with the spec, these are really
050: MFNode removeChildren;// supposed to only be write only.
051:
052: // exposedField
053: MFNode children;
054:
055: // field
056: SFVec3f bboxCenter;
057: SFVec3f bboxSize;
058:
059: javax.media.j3d.Group implGroup;
060:
061: /**
062: *Constructor for the GroupBase object
063: *
064: *@param loader Description of the Parameter
065: */
066: public GroupBase(Loader loader) {
067: super (loader);
068: children = new MFNode();
069: bboxCenter = new SFVec3f();
070: bboxSize = new SFVec3f(-1.0f, -1.0f, -1.0f);
071: addChildren = new MFNode();
072: removeChildren = new MFNode();
073: initGroupBaseFields();
074: }
075:
076: /**
077: *Constructor for the GroupBase object
078: *
079: *@param loader Description of the Parameter
080: *@param children Description of the Parameter
081: *@param bboxCenter Description of the Parameter
082: *@param bboxSize Description of the Parameter
083: */
084: GroupBase(Loader loader, MFNode children, SFVec3f bboxCenter,
085: SFVec3f bboxSize) {
086: super (loader);
087: this .children = children;
088: this .bboxCenter = bboxCenter;
089: this .bboxSize = bboxSize;
090: addChildren = new MFNode();
091: removeChildren = new MFNode();
092: initGroupBaseFields();
093: }
094:
095: /**
096: * Description of the Method
097: *
098: *@param eventInName Description of the Parameter
099: *@param time Description of the Parameter
100: */
101: public void notifyMethod(String eventInName, double time) {
102: if (eventInName.equals("addChildren")) {
103: MFNode newKids = (MFNode) FieldSpec.get(eventInName);
104: doAddChildren(newKids);
105: } else if (eventInName.equals("removeChildren")) {
106: MFNode badKids = (MFNode) FieldSpec.get(eventInName);
107: doRemoveChildren(badKids);
108: } else if (eventInName.equals("children")) {
109: replaceChildren();
110: } else if (eventInName.equals("route_children")) {
111: implGroup
112: .setCapability(javax.media.j3d.Group.ALLOW_CHILDREN_READ);
113: implGroup
114: .setCapability(javax.media.j3d.Group.ALLOW_CHILDREN_WRITE);
115: implGroup
116: .setCapability(javax.media.j3d.Group.ALLOW_CHILDREN_EXTEND);
117: } else {
118: System.out.println("GroupBase: unknown eventInName: "
119: + eventInName);
120: }
121:
122: }
123:
124: // the children field has been updated. Remove any nodes on the implGroup
125: // and add the current list to the implGroup
126: /** Description of the Method */
127: void replaceChildren() {
128: int numChildren;
129: if ((numChildren = implGroup.numChildren()) != 0) {
130: for (int i = 0; i < numChildren; i++) {
131: implGroup.removeChild(0);// should shift the rest down...
132: }
133: }
134: for (int i = 0; i < children.nodes.length; i++) {
135: BaseNode child = children.nodes[i];
136:
137: child.updateParent(implGroup);
138: // let the new child know this is their vrml parent
139: child.parent = this ;
140: boolean doit = false;
141: if (child instanceof VrmlSensor) {
142: doit = true;
143: }
144: if (child instanceof ProtoInstance) {
145: if (((ProtoInstance) child).containsSensor) {
146: Enumeration e = (((ProtoInstance) child).sensors)
147: .elements();
148: while (e.hasMoreElements()) {
149: BaseNode vs = (BaseNode) (e.nextElement());
150: vs.updateParent(implGroup);
151: }
152: doit = true;
153: }
154: }
155: if (doit) {
156: implGroup
157: .setCapability(javax.media.j3d.Group.ENABLE_PICK_REPORTING);
158: implGroup
159: .setCapability(javax.media.j3d.Group.ALLOW_BOUNDS_READ);
160: implGroup
161: .setCapability(javax.media.j3d.Group.ALLOW_AUTO_COMPUTE_BOUNDS_READ);
162: implGroup
163: .setCapability(javax.media.j3d.Group.ALLOW_LOCAL_TO_VWORLD_READ);
164: implGroup.setPickable(true);
165: }
166:
167: // add the child's impl to the j3d group
168: javax.media.j3d.Node implNode = child.getImplNode();
169: if (loader.debug) {
170: System.out.println(this .toStringId() + ": index = "
171: + i + " child node type is " + child.getType()
172: + " " + child.toStringId()
173: + " gets implNoded to " + implNode);
174: }
175: if (implNode != null) {
176: if (implNode.getParent() == null) {
177: implGroup.addChild(implNode);
178: } else {
179: implGroup.addChild(implNode.cloneNode(true));
180: }
181: if (child instanceof DirectionalLight) {
182: DirectionalLight dirLight = (DirectionalLight) child;
183: dirLight.setScope(implGroup);
184: }
185: }
186: }
187: }
188:
189: /**
190: * Gets the numTris attribute of the GroupBase object
191: *
192: *@return The numTris value
193: */
194: public int getNumTris() {
195: int numTris = 0;
196: for (int i = 0; i < children.nodes.length; i++) {
197: BaseNode child = children.nodes[i];
198: numTris += child.getNumTris();
199: }
200: return numTris;
201: }
202:
203: /**
204: * Description of the Method
205: *
206: *@param newChildren Description of the Parameter
207: */
208: void doAddChildren(MFNode newChildren) {
209: BaseNode[] newCh = newChildren.getValue();
210: BaseNode[] oldCh = children.getValue();
211:
212: Vector v = new Vector(newCh.length + oldCh.length);
213: for (int i = 0; i < oldCh.length; i++) {
214: v.addElement(oldCh[i]);
215: }
216:
217: for (int i = 0; i < newCh.length; i++) {
218: if (loader.debug) {
219: System.out.println(this + " doAddChildren " + newCh[i]);
220: }
221: v.addElement(newCh[i]);
222: }
223:
224: BaseNode[] newFamily = new BaseNode[newCh.length + oldCh.length];
225: v.copyInto(newFamily);
226:
227: children = new MFNode(newFamily);
228: FieldSpec.remove("children");
229: children.init(this , FieldSpec, Field.EXPOSED_FIELD, "children");
230:
231: replaceChildren();
232: }
233:
234: /**
235: * Description of the Method
236: *
237: *@param removeChildren Description of the Parameter
238: */
239: void doRemoveChildren(MFNode removeChildren) {
240: BaseNode[] oldCh = children.getValue();// where do new children
241: // get thier values from? MTV
242: BaseNode[] removeCh = removeChildren.getValue();
243:
244: Vector v = new Vector(children.getValue().length);
245:
246: for (int i = 0; i < oldCh.length; i++) {
247: v.addElement(oldCh[i]);
248: }
249:
250: for (int i = 0; i < removeCh.length; i++) {
251: // unfortunately these children don't
252: // go to the prinicpals office!
253: // ever see soilent green?
254: if (loader.debug) {
255: System.out.println("doRemoveChildren " + removeCh[i]);
256: }
257: v.removeElement(removeCh[i]);
258: }
259:
260: BaseNode[] newFamily = new BaseNode[children.getSize()
261: - removeCh.length];
262: v.copyInto(newFamily);
263:
264: children = new MFNode(newFamily);
265: FieldSpec.remove("children");
266: FieldSpec.put("children", children);
267: replaceChildren();
268: }
269:
270: /**
271: * Gets the type attribute of the GroupBase object
272: *
273: *@return The type value
274: */
275: public String getType() {
276: return "Group";
277: }
278:
279: // this may be overridden by subclasses
280: /** Description of the Method */
281: void initFields() {
282: }
283:
284: /** Description of the Method */
285: void initGroupBaseFields() {
286: addChildren
287: .init(this , FieldSpec, Field.EVENT_IN, "addChildren");
288: removeChildren.init(this , FieldSpec, Field.EVENT_IN,
289: "removeChildren");
290: children.init(this , FieldSpec, Field.EXPOSED_FIELD, "children");
291: bboxCenter.init(this , FieldSpec, Field.FIELD, "bboxCenter");
292: bboxSize.init(this , FieldSpec, Field.FIELD, "bboxSize");
293: }
294:
295: /**
296: * Description of the Method
297: *
298: *@return Description of the Return Value
299: */
300: public String toStringBodyS() {
301: String retval = "children " + children;
302: if ((bboxCenter.value[0] != 0.0)
303: || (bboxCenter.value[1] != 0.0)
304: || (bboxCenter.value[2] != 0.0)) {
305: retval += "bboxCenter " + bboxCenter;
306: }
307: if ((bboxSize.value[0] != -1.0) || (bboxSize.value[1] != -1.0)
308: || (bboxSize.value[2] != -1.0)) {
309: retval += "bboxSize " + bboxSize;
310: }
311: return retval;
312: }
313: }
|