001: /*
002: * $RCSfile: Group.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.6 $
028: * $Date: 2008/02/28 20:17:23 $
029: * $State: Exp $
030: */
031:
032: package javax.media.j3d;
033:
034: import java.util.BitSet;
035: import java.util.Vector;
036: import java.util.Hashtable;
037: import java.util.Enumeration;
038:
039: /**
040: * The Group node object is a general-purpose grouping node. Group
041: * nodes have exactly one parent and an arbitrary number of children
042: * that are rendered in an unspecified order (or in parallel). Null
043: * children are allowed; no operation is performed on a null child
044: * node. Operations on Group node objects include adding, removing,
045: * and enumerating the children of the Group node. The subclasses of
046: * Group node add additional semantics.
047: */
048:
049: public class Group extends Node {
050: /**
051: * Specifies that this Group node allows reading its children.
052: */
053: public static final int ALLOW_CHILDREN_READ = CapabilityBits.GROUP_ALLOW_CHILDREN_READ;
054:
055: /**
056: * Specifies that this Group node allows writing its children.
057: */
058: public static final int ALLOW_CHILDREN_WRITE = CapabilityBits.GROUP_ALLOW_CHILDREN_WRITE;
059:
060: /**
061: * Specifies that this Group node allows adding new children.
062: */
063: public static final int ALLOW_CHILDREN_EXTEND = CapabilityBits.GROUP_ALLOW_CHILDREN_EXTEND;
064:
065: /**
066: * Specifies that this Group node allows reading its collision Bounds
067: */
068: public static final int ALLOW_COLLISION_BOUNDS_READ = CapabilityBits.GROUP_ALLOW_COLLISION_BOUNDS_READ;
069:
070: /**
071: * Specifies that this Group node allows writing its collision Bounds
072: */
073: public static final int ALLOW_COLLISION_BOUNDS_WRITE = CapabilityBits.GROUP_ALLOW_COLLISION_BOUNDS_WRITE;
074:
075: // Array for setting default read capabilities
076: private static final int[] readCapabilities = {
077: ALLOW_CHILDREN_READ, ALLOW_COLLISION_BOUNDS_READ };
078:
079: /**
080: * Creates the retained mode GroupRetained object that this
081: * Group component object will point to.
082: */
083: void createRetained() {
084: retained = new GroupRetained();
085: retained.setSource(this );
086: }
087:
088: /**
089: * Sets the collision bounds of a node.
090: * @param bounds the collision bounding object for a node
091: * @exception CapabilityNotSetException if appropriate capability is
092: * not set and this object is part of live or compiled scene graph
093: */
094: public void setCollisionBounds(Bounds bounds) {
095: if (isLiveOrCompiled())
096: if (!this .getCapability(ALLOW_COLLISION_BOUNDS_WRITE))
097: throw new CapabilityNotSetException(J3dI18N
098: .getString("Group0"));
099:
100: ((GroupRetained) this .retained).setCollisionBounds(bounds);
101: }
102:
103: /**
104: * Returns the collision bounding object of this node.
105: * @return the node's collision bounding object
106: * @exception CapabilityNotSetException if appropriate capability is
107: * not set and this object is part of live or compiled scene graph
108: */
109: public Bounds getCollisionBounds() {
110: if (isLiveOrCompiled())
111: if (!this .getCapability(ALLOW_COLLISION_BOUNDS_READ))
112: throw new CapabilityNotSetException(J3dI18N
113: .getString("Group1"));
114:
115: return ((GroupRetained) this .retained).getCollisionBounds();
116: }
117:
118: /**
119: * Replaces the child node at the specified index in this
120: * group node's list of children with the specified child.
121: * @param child the new child
122: * @param index which child to replace. The <code>index</code> must
123: * be a value
124: * greater than or equal to 0 and less than <code>numChildren()</code>.
125: * @exception CapabilityNotSetException if the appropriate capability is
126: * not set and this group node is part of live or compiled scene graph
127: * @exception RestrictedAccessException if this group node is part of
128: * live or compiled scene graph and the child node being set is not
129: * a BranchGroup node
130: * @exception MultipleParentException if <code>child</code> has already
131: * been added as a child of another group node
132: * @exception IndexOutOfBoundsException if <code>index</code> is invalid
133: */
134: public void setChild(Node child, int index) {
135: if (child instanceof SharedGroup) {
136: throw new IllegalArgumentException(J3dI18N
137: .getString("Group2"));
138: }
139:
140: if (isLiveOrCompiled()) {
141: Node oldchild = (Node) ((GroupRetained) this .retained)
142: .getChild(index);
143: if (!(child instanceof BranchGroup))
144: throw new RestrictedAccessException(J3dI18N
145: .getString("Group3"));
146:
147: if (!getCapability(ALLOW_CHILDREN_WRITE))
148: throw new CapabilityNotSetException(J3dI18N
149: .getString("Group13"));
150:
151: if ((oldchild != null)
152: && (!((BranchGroup) oldchild)
153: .getCapability(BranchGroup.ALLOW_DETACH))) {
154: throw new CapabilityNotSetException(J3dI18N
155: .getString("Group4"));
156: }
157: }
158:
159: ((GroupRetained) retained).setChild(child, index);
160: }
161:
162: /**
163: * Inserts the specified child node in this group node's list of
164: * children at the specified index.
165: * @param child the new child
166: * @param index at which location to insert. The <code>index</code>
167: * must be a value
168: * greater than or equal to 0 and less than or equal to
169: * <code>numChildren()</code>.
170: * @exception CapabilityNotSetException if the appropriate capability is
171: * not set and this group node is part of live or compiled scene graph
172: * @exception RestrictedAccessException if this group node is part of
173: * live
174: * or compiled scene graph and the child node being inserted is not
175: * a BranchGroup node
176: * @exception MultipleParentException if <code>child</code> has already
177: * been added as a child of another group node.
178: * @exception IndexOutOfBoundsException if <code>index</code> is invalid.
179: */
180: public void insertChild(Node child, int index) {
181: if (child instanceof SharedGroup) {
182: throw new IllegalArgumentException(J3dI18N
183: .getString("Group2"));
184: }
185:
186: if (isLiveOrCompiled()) {
187: if (!(child instanceof BranchGroup))
188: throw new RestrictedAccessException(J3dI18N
189: .getString("Group6"));
190:
191: if (!this .getCapability(ALLOW_CHILDREN_WRITE))
192: throw new CapabilityNotSetException(J3dI18N
193: .getString("Group14"));
194: }
195:
196: ((GroupRetained) this .retained).insertChild(child, index);
197: }
198:
199: /**
200: * Removes the child node at the specified index from this group node's
201: * list of children.
202: * @param index which child to remove. The <code>index</code>
203: * must be a value
204: * greater than or equal to 0 and less than <code>numChildren()</code>.
205: * @exception CapabilityNotSetException if the appropriate capability is
206: * not set and this group node is part of live or compiled scene graph
207: * @exception RestrictedAccessException if this group node is part of
208: * live or compiled scene graph and the child node being removed is not
209: * a BranchGroup node
210: * @exception IndexOutOfBoundsException if <code>index</code> is invalid.
211: */
212: public void removeChild(int index) {
213: if (isLiveOrCompiled()) {
214: Node child = ((GroupRetained) this .retained)
215: .getChild(index);
216: if (!(child instanceof BranchGroup)) {
217: throw new RestrictedAccessException(J3dI18N
218: .getString("Group7"));
219: }
220:
221: if (!this .getCapability(ALLOW_CHILDREN_WRITE)) {
222: throw new CapabilityNotSetException(J3dI18N
223: .getString("Group15"));
224: }
225:
226: if (!((BranchGroup) child)
227: .getCapability(BranchGroup.ALLOW_DETACH)) {
228: throw new CapabilityNotSetException(J3dI18N
229: .getString("Group4"));
230: }
231: }
232:
233: ((GroupRetained) this .retained).removeChild(index);
234: }
235:
236: /**
237: * Retrieves the child node at the specified index in
238: * this group node's list of children.
239: * @param index which child to return.
240: * @return the children at location index. The <code>index</code>
241: * must be a value
242: * greater than or equal to 0 and less than <code>numChildren()</code>.
243: * @exception CapabilityNotSetException if the appropriate capability is
244: * not set and this group node is part of live or compiled scene graph
245: * @exception IndexOutOfBoundsException if <code>index</code> is invalid.
246: */
247: public Node getChild(int index) {
248: if (isLiveOrCompiled())
249: if (!this .getCapability(ALLOW_CHILDREN_READ))
250: throw new CapabilityNotSetException(J3dI18N
251: .getString("Group9"));
252:
253: return (Node) ((GroupRetained) this .retained).getChild(index);
254: }
255:
256: /**
257: * Returns an Enumeration object of this group node's list of children.
258: * @return an Enumeration object of all the children
259: * @exception CapabilityNotSetException if the appropriate capability is
260: * not set and this group node is part of live or compiled scene graph
261: */
262: public Enumeration getAllChildren() {
263: if (isLiveOrCompiled())
264: if (!this .getCapability(ALLOW_CHILDREN_READ))
265: throw new CapabilityNotSetException(J3dI18N
266: .getString("Group9"));
267:
268: return (Enumeration) ((GroupRetained) this .retained)
269: .getAllChildren();
270: }
271:
272: /**
273: * Appends the specified child node to this group node's list of children.
274: * @param child the child to add to this node's list of children
275: * @exception CapabilityNotSetException if the appropriate capability is
276: * not set and this group node is part of live or compiled scene graph
277: * @exception RestrictedAccessException if this group node is part
278: * of live
279: * or compiled scene graph and the child node being added is not
280: * a BranchGroup node
281: * @exception MultipleParentException if <code>child</code> has already
282: * been added as a child of another group node.
283: */
284: public void addChild(Node child) {
285: if (child instanceof SharedGroup) {
286: throw new IllegalArgumentException(J3dI18N
287: .getString("Group2"));
288: }
289:
290: if (isLiveOrCompiled()) {
291: if (!(child instanceof BranchGroup))
292: throw new RestrictedAccessException(J3dI18N
293: .getString("Group12"));
294:
295: if (!this .getCapability(ALLOW_CHILDREN_EXTEND))
296: throw new CapabilityNotSetException(J3dI18N
297: .getString("Group16"));
298: }
299:
300: ((GroupRetained) this .retained).addChild(child);
301: }
302:
303: /**
304: * Moves the specified branch group node from its existing location to
305: * the end of this group node's list of children.
306: * @param branchGroup the branch group node to move to this node's list
307: * of children
308: * @exception CapabilityNotSetException if the appropriate capability is
309: * not set and this group node is part of live or compiled scene graph
310: */
311: public void moveTo(BranchGroup branchGroup) {
312: if (isLiveOrCompiled()) {
313: if (!this .getCapability(ALLOW_CHILDREN_EXTEND))
314: throw new CapabilityNotSetException(J3dI18N
315: .getString("Group16"));
316:
317: if (!branchGroup.getCapability(BranchGroup.ALLOW_DETACH)) {
318: throw new CapabilityNotSetException(J3dI18N
319: .getString("Group4"));
320: }
321: }
322:
323: ((GroupRetained) this .retained).moveTo(branchGroup);
324: }
325:
326: /**
327: * Returns a count of this group node's children.
328: * @return the number of children descendant from this node.
329: * @exception CapabilityNotSetException if the appropriate capability is
330: * not set and this group node is part of live or compiled scene graph
331: */
332: public int numChildren() {
333: if (isLiveOrCompiled())
334: if (!this .getCapability(ALLOW_CHILDREN_READ))
335: throw new CapabilityNotSetException(J3dI18N
336: .getString("Group9"));
337:
338: return ((GroupRetained) this .retained).numChildren();
339: }
340:
341: /**
342: * Retrieves the index of the specified child node in
343: * this group node's list of children.
344: *
345: * @param child the child node to be looked up.
346: * @return the index of the specified child node;
347: * returns -1 if the object is not in the list.
348: * @exception CapabilityNotSetException if the appropriate capability is
349: * not set and this group node is part of live or compiled scene graph
350: *
351: * @since Java 3D 1.3
352: */
353: public int indexOfChild(Node child) {
354:
355: if (isLiveOrCompiled())
356: if (!this .getCapability(ALLOW_CHILDREN_READ))
357: throw new CapabilityNotSetException(J3dI18N
358: .getString("Group9"));
359:
360: return ((GroupRetained) this .retained).indexOfChild(child);
361: }
362:
363: /**
364: * Removes the specified child node from this group node's
365: * list of children.
366: * If the specified object is not in the list, the list is not modified.
367: *
368: * @param child the child node to be removed.
369: * @exception CapabilityNotSetException if appropriate capability is
370: * not set and this object is part of live or compiled scene graph
371: * @exception RestrictedAccessException if this group node is part of
372: * live or compiled scene graph and the child node being removed is not
373: * a BranchGroup node
374: *
375: * @since Java 3D 1.3
376: */
377: public void removeChild(Node child) {
378:
379: if (isLiveOrCompiled()) {
380: if (!(child instanceof BranchGroup)) {
381: throw new RestrictedAccessException(J3dI18N
382: .getString("Group7"));
383: }
384:
385: if (!this .getCapability(ALLOW_CHILDREN_WRITE)) {
386: throw new CapabilityNotSetException(J3dI18N
387: .getString("Group15"));
388: }
389:
390: if (!((BranchGroup) child)
391: .getCapability(BranchGroup.ALLOW_DETACH)) {
392: throw new CapabilityNotSetException(J3dI18N
393: .getString("Group4"));
394: }
395: }
396:
397: ((GroupRetained) retained).removeChild(child);
398: }
399:
400: /**
401: * Removes all children from this Group node.
402: *
403: * @exception CapabilityNotSetException if appropriate capability is
404: * not set and this object is part of live or compiled scene graph
405: * @exception RestrictedAccessException if this group node is part of
406: * live or compiled scene graph and any of the children being removed are
407: * not BranchGroup nodes
408: *
409: * @since Java 3D 1.3
410: */
411: public void removeAllChildren() {
412:
413: if (isLiveOrCompiled()) {
414: GroupRetained groupR = (GroupRetained) this .retained;
415: for (int index = groupR.numChildren() - 1; index >= 0; index--) {
416: Node child = groupR.getChild(index);
417: if (!(child instanceof BranchGroup))
418: throw new RestrictedAccessException(J3dI18N
419: .getString("Group7"));
420:
421: if (!this .getCapability(ALLOW_CHILDREN_WRITE))
422: throw new CapabilityNotSetException(J3dI18N
423: .getString("Group15"));
424:
425: if (!((BranchGroup) child)
426: .getCapability(BranchGroup.ALLOW_DETACH)) {
427: throw new CapabilityNotSetException(J3dI18N
428: .getString("Group4"));
429: }
430: }
431: }
432:
433: ((GroupRetained) retained).removeAllChildren();
434: }
435:
436: /**
437: * Causes this Group node to be reported as the collision target when
438: * collision is being used and this node or any of its children is in
439: * a collision. The default value is false. For collision with
440: * USE_GEOMETRY set, the collision traverser will check the geometry
441: * of all the Group node's leaf descendants; for collision with
442: * USE_BOUNDS set, the collision traverser will only check the bounds
443: * at this Group node. In both cases, if there is a collision, this
444: * Group node will be reported as the colliding object in the
445: * SceneGraphPath. This reporting is done regardless of whether
446: * ENABLE_COLLISION_REPORTING
447: * is set for this group node (setting alternate collision target to
448: * true implies collision reporting).
449: * @param target Indicates whether this Group node can be the target
450: * of a collision.
451: * @see WakeupOnCollisionEntry
452: * @see WakeupOnCollisionMovement
453: * @see WakeupOnCollisionExit
454: */
455: public void setAlternateCollisionTarget(boolean target) {
456: ((GroupRetained) this .retained)
457: .setAlternateCollisionTarget(target);
458: }
459:
460: /**
461: * Returns the collision target state.
462: * @return Indicates whether this Group node can be the target of a
463: * collision.
464: */
465: public boolean getAlternateCollisionTarget() {
466: return ((GroupRetained) this .retained)
467: .getAlternateCollisionTarget();
468: }
469:
470: /**
471: * Duplicates all the nodes of the specified sub-graph. For Group Nodes
472: * the group node is duplicated via a call to <code>cloneNode</code> and
473: * then <code>cloneTree</code> is called for each child node. For
474: * Leaf Nodes, component
475: * data can either be duplicated or be made a reference to the original
476: * data. Leaf Node cloneTree behavior is determined by the
477: * <code>duplicateOnCloneTree</code> flag found in every Leaf Node's
478: * component data class and by the <code>forceDuplicate</code> paramter.
479: *
480: * @param forceDuplicate when set to <code>true</code>, causes the
481: * <code>duplicateOnCloneTree</code>
482: * flag to be ignored. When <code>false</code>, the value of each
483: * node's
484: * <code>duplicateOnCloneTree</code> determines whether data is
485: * duplicated or copied.
486: *
487: *
488: * @return a reference to the cloned scene graph.
489: *
490: * @see NodeComponent#setDuplicateOnCloneTree
491: */
492: Node cloneTree(boolean forceDuplicate, Hashtable nodeHashtable) {
493: Group g = (Group) super
494: .cloneTree(forceDuplicate, nodeHashtable);
495: GroupRetained rt = (GroupRetained) retained;
496:
497: int nChildren = rt.numChildren();
498: // call cloneTree on all child nodes
499: for (int i = 0; i < nChildren; i++) {
500: Node n = rt.getChild(i);
501: Node clonedN = n.cloneTree(forceDuplicate, nodeHashtable);
502: // add the cloned child to the cloned group node
503: ((GroupRetained) g.retained).addChild(clonedN);
504:
505: }
506: return g;
507: }
508:
509: /**
510: * Copies all Node information from
511: * <code>originalNode</code> into
512: * the current node. This method is called from the
513: * <code>cloneNode</code> method which is, in turn, called by the
514: * <code>cloneTree</code> method.<P>
515: *
516: * @param originalNode the original node to duplicate.
517: * @param forceDuplicate when set to <code>true</code>, causes the
518: * <code>duplicateOnCloneTree</code> flag to be ignored. When
519: * <code>false</code>, the value of each node's
520: * <code>duplicateOnCloneTree</code> variable determines whether
521: * NodeComponent data is duplicated or copied.
522: *
523: * @exception RestrictedAccessException if this object is part of a live
524: * or compiled scenegraph.
525: *
526: * @see Node#duplicateNode
527: * @see Node#cloneTree
528: * @see NodeComponent#setDuplicateOnCloneTree
529: */
530: void duplicateAttributes(Node originalNode, boolean forceDuplicate) {
531: super .duplicateAttributes(originalNode, forceDuplicate);
532:
533: GroupRetained attr = (GroupRetained) originalNode.retained;
534: GroupRetained rt = (GroupRetained) retained;
535:
536: rt.setCollisionBounds(attr.getCollisionBounds());
537: rt.setAlternateCollisionTarget(attr
538: .getAlternateCollisionTarget());
539: // throw away any child create before, since some node such as
540: // Sphere has already created its own branch
541: // Without doing this, we may end up with two branches with exactly
542: // the same content when cloneTree() is invoked.
543: rt.children.clear();
544: }
545:
546: /**
547: * Used to create a new instance of the node. This routine is called
548: * by <code>cloneTree</code> to duplicate the current node.
549: * @param forceDuplicate when set to <code>true</code>, causes the
550: * <code>duplicateOnCloneTree</code> flag to be ignored. When
551: * <code>false</code>, the value of each node's
552: * <code>duplicateOnCloneTree</code> variable determines whether
553: * NodeComponent data is duplicated or copied.
554: *
555: * @see Node#cloneTree
556: * @see Node#cloneNode
557: * @see Node#duplicateNode
558: * @see NodeComponent#setDuplicateOnCloneTree
559: */
560: public Node cloneNode(boolean forceDuplicate) {
561: Group g = new Group();
562: g.duplicateNode(this , forceDuplicate);
563: return g;
564: }
565:
566: /**
567: * Constructs a Group node with default parameters. The default
568: * values are as follows:
569: * <ul>
570: * collision bounds : null<br>
571: * alternate collision target : false<br>
572: * </ul>
573: */
574: public Group() {
575: // set default read capabilities
576: setDefaultReadCapabilities(readCapabilities);
577: }
578: }
|