001: /*
002: * $Header: /cvs/j3dfly/J3dFly/src/org/jdesktop/j3dfly/utils/developmenttools/DevelopmentLocale.java,v 1.1 2005/04/20 21:04:51 paulby Exp $
003: *
004: * Sun Public License Notice
005: *
006: * The contents of this file are subject to the Sun Public License Version
007: * 1.0 (the "License"). You may not use this file except in compliance with
008: * the License. A copy of the License is available at http://www.sun.com/
009: *
010: * The Original Code is Java 3D(tm) Fly Through.
011: * The Initial Developer of the Original Code is Paul Byrne.
012: * Portions created by Paul Byrne are Copyright (C) 2002.
013: * All Rights Reserved.
014: *
015: * Contributor(s): Paul Byrne.
016: *
017: **/
018: package org.jdesktop.j3dfly.utils.developmenttools;
019:
020: import java.util.ArrayList;
021: import java.util.Iterator;
022:
023: import javax.media.j3d.VirtualUniverse;
024: import javax.media.j3d.HiResCoord;
025: import javax.media.j3d.BranchGroup;
026: import javax.media.j3d.Group;
027:
028: /**
029: * A Locale for use in Development/Debug tools.
030: *
031: * This locale provide a setLive call which will either make an entire
032: * Locale live or 'un'live. It also provides a listener system to notify
033: * user code of changes. Any changes to the locale (ie the addition or
034: * removal of a BranchGraph) will be notified to all listeners automatically.
035: *
036: * Changes within an existing Graph are all notified to listeners when one of the
037: * notify mehtods is called.
038: *
039: *
040: * @author Paul Byrne
041: * @version $Id: DevelopmentLocale.java,v 1.1 2005/04/20 21:04:51 paulby Exp $
042: */
043: public class DevelopmentLocale extends javax.media.j3d.Locale {
044:
045: protected ArrayList liveListeners = new ArrayList();
046: protected ArrayList changeListeners = new ArrayList();
047:
048: protected ArrayList branchGraphs = new ArrayList();
049: protected BranchGroup branchGraphArray[] = new BranchGroup[0];
050: protected BranchGroup hiddenBranchGraph = null;
051: protected boolean isLive = true;
052:
053: /** Creates new Locale */
054: public DevelopmentLocale(VirtualUniverse universe) {
055: super (universe);
056: init();
057: }
058:
059: public DevelopmentLocale(VirtualUniverse universe, HiResCoord hiRes) {
060: super (universe, hiRes);
061: init();
062: }
063:
064: public DevelopmentLocale(VirtualUniverse universe, int[] x,
065: int[] y, int[] z) {
066: super (universe, x, y, z);
067: init();
068: }
069:
070: private void init() {
071: isLive = true;
072: }
073:
074: /**
075: * Hidden BranchGraph is always the last branchgraph in the locale.
076: * It is not returned by getBranchGraphs(), it is not included in numBranchGraphs
077: * and no listeners are notified when it is set
078: * The Hidden BranchGraph is made live and not live when setLive is called
079: */
080: public void setHiddenBranchGraph(BranchGroup hiddenBranchGraph) {
081: this .hiddenBranchGraph = hiddenBranchGraph;
082: hiddenBranchGraph.setCapability(BranchGroup.ALLOW_DETACH);
083: if (isLive)
084: super .addBranchGraph(hiddenBranchGraph);
085: }
086:
087: /**
088: * Return the hidden branch graph
089: */
090: public BranchGroup getHiddenBranchGraph() {
091: return hiddenBranchGraph;
092: }
093:
094: public void addBranchGraph(BranchGroup branchGroup) {
095:
096: //System.out.println("Adding "+branchGroup );
097:
098: if (branchGroup.isCompiled())
099: javax.swing.JOptionPane
100: .showMessageDialog(
101: null,
102: "Compiled Branch Group's can not be viewed, remove the .compile() from your source code.",
103: "Compiled Branch Group",
104: javax.swing.JOptionPane.WARNING_MESSAGE);
105: else {
106: for (int i = 0; i < changeListeners.size(); i++)
107: ((SceneGraphChangeListener) changeListeners.get(i))
108: .graphAdded(this , branchGroup);
109:
110: branchGroup.setCapability(BranchGroup.ALLOW_DETACH);
111: branchGraphs.add(branchGroup);
112: }
113:
114: updateBranchGraphArray();
115:
116: if (isLive)
117: super .addBranchGraph(branchGroup);
118:
119: }
120:
121: public void removeBranchGraph(BranchGroup branchGroup) {
122:
123: branchGraphs.remove(branchGraphs.indexOf(branchGroup));
124:
125: updateBranchGraphArray();
126:
127: if (isLive)
128: super .removeBranchGraph(branchGroup);
129:
130: for (int i = 0; i < changeListeners.size(); i++)
131: ((SceneGraphChangeListener) changeListeners.get(i))
132: .graphRemoved(this , branchGroup);
133:
134: }
135:
136: public void replaceBranchGraph(BranchGroup oldGroup,
137: BranchGroup newGroup) {
138: for (int i = 0; i < changeListeners.size(); i++)
139: ((SceneGraphChangeListener) changeListeners.get(i))
140: .graphReplaced(this , oldGroup, newGroup);
141:
142: newGroup.setCapability(BranchGroup.ALLOW_DETACH);
143: branchGraphs.set(branchGraphs.indexOf(oldGroup), newGroup);
144:
145: updateBranchGraphArray();
146: if (isLive)
147: super .replaceBranchGraph(oldGroup, newGroup);
148: }
149:
150: /**
151: * Replace the BranchGraph without notifying listeners
152: */
153: public void silentReplaceBranchGraph(BranchGroup oldGroup,
154: BranchGroup newGroup) {
155: branchGraphs.set(branchGraphs.indexOf(oldGroup), newGroup);
156: super .replaceBranchGraph(oldGroup, newGroup);
157: }
158:
159: /**
160: * Return the number of BranchGraphs in this locale
161: */
162: public int numBranchGraphs() {
163: return branchGraphs.size();
164: }
165:
166: public synchronized void setLive(boolean live) {
167: if (isLive == live)
168: return;
169:
170: //System.out.println("SetLive "+live);
171: //Thread.dumpStack();
172:
173: BranchGroup bg;
174:
175: Iterator it = branchGraphs.iterator();
176: while (it.hasNext()) {
177: bg = (BranchGroup) it.next();
178: //System.out.println("BranchGroup "+bg);
179: if (live) {
180: super .addBranchGraph(bg);
181: } else {
182: bg.detach();
183: }
184: }
185:
186: if (hiddenBranchGraph != null) {
187: if (live)
188: super .addBranchGraph(hiddenBranchGraph);
189: else
190: hiddenBranchGraph.detach();
191: }
192:
193: isLive = live;
194:
195: for (int i = 0; i < liveListeners.size(); i++)
196: ((SceneGraphLiveListener) liveListeners.get(i))
197: .sceneGraphLive(live);
198:
199: }
200:
201: public boolean isLive() {
202: return isLive;
203: }
204:
205: /**
206: * @deprecated use isLive
207: */
208: public boolean getLive() {
209: return isLive;
210: }
211:
212: public BranchGroup[] getBranchGraphs() {
213: return branchGraphArray;
214: }
215:
216: public void addLiveListener(SceneGraphLiveListener listener) {
217: liveListeners.add(listener);
218: }
219:
220: public void addGraphChangeListener(SceneGraphChangeListener listener) {
221: changeListeners.add(listener);
222: }
223:
224: public void removeLiveListener(SceneGraphLiveListener listener) {
225: if (!liveListeners.remove(listener))
226: throw new RuntimeException("Removing unknown listener");
227: }
228:
229: public void removeGraphChangeListener(
230: SceneGraphChangeListener listener) {
231: if (!changeListeners.remove(listener))
232: throw new RuntimeException("Removing unknown listener");
233: }
234:
235: /**
236: * Returns a copy of the list of SceneGraphChangeListeners
237: */
238: public ArrayList getSceneGraphChangeListeners() {
239: return (ArrayList) changeListeners.clone();
240: }
241:
242: /**
243: * Overwrites the current set of listeners with the supplied set of listeners
244: */
245: public void setSceneGraphChangeListeners(ArrayList listeners) {
246: changeListeners = (ArrayList) listeners.clone();
247: }
248:
249: public void notifyGraphChanged(BranchGroup graph,
250: javax.media.j3d.Node node) {
251: if (node instanceof javax.media.j3d.Group
252: || node instanceof javax.media.j3d.Behavior
253: || node instanceof javax.media.j3d.Link) {
254: for (int i = 0; i < changeListeners.size(); i++) {
255: ((SceneGraphChangeListener) changeListeners.get(i))
256: .graphChanged(this , graph, node);
257: }
258: } else
259: throw new RuntimeException(
260: "Node MUST be either Group, Behavior or Link");
261: }
262:
263: /**
264: * Notify all listeners that child is about to be added to parent
265: */
266: public void notifyGroupAddChild(Group parent, BranchGroup child) {
267: for (int i = 0; i < changeListeners.size(); i++)
268: ((SceneGraphChangeListener) changeListeners.get(i))
269: .groupAddChild(this , parent, child);
270: }
271:
272: /**
273: * Notify all listeners that child is about to be set in the parent
274: */
275: public void notifyGroupSetChild(Group parent, BranchGroup child,
276: int index) {
277: for (int i = 0; i < changeListeners.size(); i++)
278: ((SceneGraphChangeListener) changeListeners.get(i))
279: .groupSetChild(this , parent, child, index);
280: }
281:
282: /**
283: * Notify all listeners that all children are about to be removed
284: */
285: public void notifyGroupRemoveAllChildren(Group parent) {
286: for (int i = 0; i < changeListeners.size(); i++)
287: ((SceneGraphChangeListener) changeListeners.get(i))
288: .groupRemoveAllChildren(this , parent);
289: }
290:
291: /**
292: * Notify all listeners that all children are about to be removed
293: */
294: public void notifyGroupRemoveChild(Group parent, BranchGroup child) {
295: for (int i = 0; i < changeListeners.size(); i++)
296: ((SceneGraphChangeListener) changeListeners.get(i))
297: .groupRemoveChild(this , parent, child);
298: }
299:
300: protected void updateBranchGraphArray() {
301: branchGraphs.trimToSize();
302: if (branchGraphs.size() != branchGraphArray.length)
303: branchGraphArray = new BranchGroup[branchGraphs.size()];
304: branchGraphArray = (BranchGroup[]) branchGraphs
305: .toArray(branchGraphArray);
306: }
307:
308: }
|