001: /*
002: * $Header: /cvs/j3dfly/J3dEditor/src/org/jdesktop/j3dedit/interposer/InterposerLocale.java,v 1.1 2005/04/20 22:20:32 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 the Java 3D(tm) Scene Graph Editor.
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.j3dedit.interposer;
019:
020: import org.jdesktop.j3dfly.utils.developmenttools.SceneGraphChangeListener;
021: import org.jdesktop.j3dfly.utils.developmenttools.SceneGraphLiveListener;
022:
023: import javax.media.j3d.VirtualUniverse;
024: import javax.media.j3d.Locale;
025: import javax.media.j3d.BranchGroup;
026: import java.util.Iterator;
027: import java.util.ArrayList;
028:
029: /**
030: * This is called
031: *
032: * @author paulby
033: */
034: public class InterposerLocale extends
035: org.jdesktop.j3dfly.utils.developmenttools.DevelopmentLocale {
036:
037: private Locale locale;
038: protected ArrayList branchGraphs = new ArrayList();
039: protected BranchGroup branchGraphArray[] = new BranchGroup[0];
040: boolean silent = false;
041:
042: /** Creates a new instance of InterposerLocale */
043: public InterposerLocale(Locale locale) {
044: super (new VirtualUniverse());
045:
046: //This seems to get the wrong universe, ie it get this.getVirtualUniverse
047: //super.getVirtualUniverse().removeAllLocales();
048:
049: System.out.println("Creating Interposer Locale " + this + " "
050: + locale);
051:
052: this .locale = locale;
053: }
054:
055: public Locale getActualLocale() {
056: return locale;
057: }
058:
059: public VirtualUniverse getVirtualUniverse() {
060: return locale.getVirtualUniverse();
061: }
062:
063: /**
064: * Hidden BranchGraph is always the last branchgraph in the locale.
065: * It is not returned by getBranchGraphs(), it is not included in numBranchGraphs
066: * and no listeners are notified when it is set
067: * The Hidden BranchGraph is made live and not live when setLive is called
068: */
069: public void setHiddenBranchGraph(BranchGroup hiddenBranchGraph) {
070: this .hiddenBranchGraph = hiddenBranchGraph;
071: hiddenBranchGraph.setCapability(BranchGroup.ALLOW_DETACH);
072: if (isLive)
073: locale.addBranchGraph(hiddenBranchGraph);
074: }
075:
076: public synchronized void addBranchGraph(BranchGroup branchGroup) {
077: if (branchGroup.isCompiled())
078: javax.swing.JOptionPane
079: .showMessageDialog(
080: null,
081: "Compiled Branch Group's can not be viewed, remove the .compile() from your source code.",
082: "Compiled Branch Group",
083: javax.swing.JOptionPane.WARNING_MESSAGE);
084: else {
085: for (int i = 0; i < changeListeners.size(); i++)
086: ((SceneGraphChangeListener) changeListeners.get(i))
087: .graphAdded(this , branchGroup);
088:
089: branchGroup.setCapability(BranchGroup.ALLOW_DETACH);
090: branchGraphs.add(branchGroup);
091: }
092:
093: updateBranchGraphArray();
094:
095: }
096:
097: public synchronized void removeBranchGraph(BranchGroup branchGroup) {
098: System.out.println("RemoveBG " + this );
099: branchGraphs.remove(branchGraphs.indexOf(branchGroup));
100:
101: updateBranchGraphArray();
102:
103: for (int i = 0; i < changeListeners.size(); i++)
104: ((SceneGraphChangeListener) changeListeners.get(i))
105: .graphRemoved(this , branchGroup);
106: }
107:
108: public synchronized void replaceBranchGraph(BranchGroup oldGroup,
109: BranchGroup newGroup) {
110: if (silent)
111: return;
112:
113: for (int i = 0; i < changeListeners.size(); i++)
114: ((SceneGraphChangeListener) changeListeners.get(i))
115: .graphReplaced(this , oldGroup, newGroup);
116:
117: newGroup.setCapability(BranchGroup.ALLOW_DETACH);
118: branchGraphs.set(branchGraphs.indexOf(oldGroup), newGroup);
119:
120: updateBranchGraphArray();
121:
122: }
123:
124: /**
125: * Replace the BranchGraph without notifying listeners
126: */
127: public synchronized void silentReplaceBranchGraph(
128: BranchGroup oldGroup, BranchGroup newGroup) {
129: silent = true;
130: branchGraphs.set(branchGraphs.indexOf(oldGroup), newGroup);
131: locale.replaceBranchGraph(oldGroup, newGroup);
132: silent = false;
133: }
134:
135: public synchronized void setLive(boolean live) {
136: if (isLive == live)
137: return;
138:
139: //System.out.println("SetLive "+live);
140: //Thread.dumpStack();
141:
142: BranchGroup bg;
143:
144: Iterator it = branchGraphs.iterator();
145: while (it.hasNext()) {
146: bg = (BranchGroup) it.next();
147: //System.out.println("BranchGroup "+bg);
148: if (live) {
149: locale.addBranchGraph(bg);
150: } else {
151: bg.detach();
152: }
153: }
154:
155: if (hiddenBranchGraph != null) {
156: if (live)
157: locale.addBranchGraph(hiddenBranchGraph);
158: else
159: hiddenBranchGraph.detach();
160: }
161:
162: isLive = live;
163:
164: for (int i = 0; i < liveListeners.size(); i++)
165: ((SceneGraphLiveListener) liveListeners.get(i))
166: .sceneGraphLive(live);
167:
168: }
169:
170: public int numBranchGraphs() {
171: return locale.numBranchGraphs();
172: }
173:
174: public java.util.Enumeration getAllBranchGraphs() {
175: return locale.getAllBranchGraphs();
176: }
177:
178: protected void updateBranchGraphArray() {
179: branchGraphs.trimToSize();
180: if (branchGraphs.size() != branchGraphArray.length)
181: branchGraphArray = new BranchGroup[branchGraphs.size()];
182: branchGraphArray = (BranchGroup[]) branchGraphs
183: .toArray(branchGraphArray);
184: }
185:
186: }
|