001: /*
002: * $Header: /cvs/j3dfly/J3dEditor/src/org/jdesktop/j3dedit/scenegrapheditor/CapabilityPersistance.java,v 1.1 2005/04/20 22:20:44 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.scenegrapheditor;
019:
020: import java.util.HashMap;
021: import java.util.Vector;
022: import java.lang.reflect.Field;
023: import javax.media.j3d.SceneGraphObject;
024: import javax.media.j3d.Node;
025: import javax.media.j3d.Appearance;
026:
027: import org.jdesktop.j3d.utils.scenegraph.traverser.TreeScan;
028: import org.jdesktop.j3d.utils.scenegraph.traverser.NodeChangeProcessor;
029:
030: /**
031: * Keeps a record of the capabilities required for a node
032: * and all associated NodeComponents
033: *
034: * @author paulby
035: */
036: public class CapabilityPersistance extends Object {
037:
038: // Key is SceneGraphObject
039: // Vector of Strings of capability names
040: protected HashMap capabilities;
041:
042: /** Creates new CapabilityControl */
043: public CapabilityPersistance() {
044: capabilities = new HashMap();
045: }
046:
047: /**
048: * Store the capability settings of this node, all it's children
049: * and all associated NodeComponents
050: */
051: /*
052: public void storeTreeCapabilities( Node node ) {
053: NodeChangeProcessor proc = new NodeChangeProcessor() {
054: public void changeNode( Node node ) {
055: doStore( node );
056: }
057: };
058:
059: TreeScan.findNode( node, javax.media.j3d.Node.class, proc, false, true );
060: }
061: **/
062:
063: /**
064: * Restore the capabilities of this node, all it's children and
065: * all associated NodeComponents
066: */
067: /*
068: public void restoreTreeCapabilities( Node node ) {
069: NodeChangeProcessor proc = new NodeChangeProcessor() {
070: public void changeNode( Node node ) {
071: doRestore( node );
072: }
073: };
074:
075: TreeScan.findNode( node, javax.media.j3d.Node.class, proc, false, true );
076: }
077: */
078:
079: /**
080: * Store the capabilities of this node and it's node Components
081: */
082: public void storeCapabilities(SceneGraphObject node) {
083: if (node instanceof javax.media.j3d.Shape3D)
084: doStore((javax.media.j3d.Shape3D) node);
085: else
086: doStore(node);
087: }
088:
089: /**
090: * Restore the capabilities of this node and it's node components
091: */
092: public void restoreCapabilities(SceneGraphObject node) {
093: if (node instanceof javax.media.j3d.Shape3D)
094: doRestore((javax.media.j3d.Shape3D) node);
095: else
096: doRestore(node);
097: }
098:
099: /**
100: * Return an array of strings of the capability names stored for this node
101: */
102: public String[] getCapabilitiesAsStrings(SceneGraphObject obj) {
103: Object o = capabilities.get(obj);
104: if (o == null)
105: return null;
106:
107: Vector caps = (Vector) o;
108: String[] ret = new String[caps.size()];
109:
110: for (int i = 0; i < caps.size(); i++) {
111: ret[i] = (String) caps.get(i);
112: }
113:
114: return ret;
115: }
116:
117: /**
118: * Forget all the currently stored capabilities
119: */
120: public void clearPersistance() {
121: capabilities.clear();
122: }
123:
124: /**
125: * Actually store the capability of this node and it's NodeComponents
126: */
127: protected void doStore(SceneGraphObject node) {
128: rememberCapabilities(node);
129:
130: // Ensure the node being worked has been checked to ensure there
131: // are no NodeComponents
132: // When Development is finished remove this check
133: // TODO - Remove Check
134: if (node instanceof javax.media.j3d.Light)
135: return;
136: else if (node instanceof javax.media.j3d.Behavior)
137: return;
138: else if (node instanceof javax.media.j3d.Group)
139: return;
140: else if (node instanceof javax.media.j3d.ViewPlatform)
141: return;
142: else if (node instanceof javax.media.j3d.Link)
143: return;
144:
145: System.out.println("WARNING - Default do-store for "
146: + node.getClass().getName());
147: }
148:
149: /**
150: * Actually restore the capability of this node and it's NodeComponents
151: */
152: protected void doRestore(SceneGraphObject node) {
153: resetCapabilities(node);
154: }
155:
156: protected void doStore(javax.media.j3d.Shape3D node) {
157: rememberCapabilities(node);
158:
159: Appearance app = node.getAppearance();
160: if (app != null) {
161: rememberCapabilities(app);
162: rememberCapabilities(app.getColoringAttributes());
163: rememberCapabilities(app.getLineAttributes());
164: rememberCapabilities(app.getMaterial());
165: rememberCapabilities(app.getPointAttributes());
166: rememberCapabilities(app.getPolygonAttributes());
167: rememberCapabilities(app.getRenderingAttributes());
168: rememberCapabilities(app.getTexCoordGeneration());
169: rememberCapabilities(app.getTexture());
170: rememberCapabilities(app.getTextureAttributes());
171: rememberCapabilities(app.getTransparencyAttributes());
172: }
173:
174: rememberCapabilities(node.getGeometry());
175: }
176:
177: protected void doRestore(javax.media.j3d.Shape3D node) {
178: resetCapabilities(node);
179:
180: Appearance app = node.getAppearance();
181: if (app != null) {
182: resetCapabilities(app);
183: resetCapabilities(app.getColoringAttributes());
184: resetCapabilities(app.getLineAttributes());
185: resetCapabilities(app.getMaterial());
186: resetCapabilities(app.getPointAttributes());
187: resetCapabilities(app.getPolygonAttributes());
188: resetCapabilities(app.getRenderingAttributes());
189: resetCapabilities(app.getTexCoordGeneration());
190: resetCapabilities(app.getTexture());
191: resetCapabilities(app.getTextureAttributes());
192: resetCapabilities(app.getTransparencyAttributes());
193: }
194: resetCapabilities(node.getGeometry());
195: }
196:
197: /**
198: * If any capabilities are set in obj, remember them
199: */
200: private void rememberCapabilities(SceneGraphObject obj) {
201: if (obj == null)
202: return;
203:
204: Vector caps = extractCapabilities(obj);
205: if (caps != null) {
206: capabilities.put(obj, caps);
207: /*
208: System.out.println( obj );
209: for(int i=0; i<caps.size(); i++)
210: System.out.println( caps.get(i) );
211:
212: System.out.println();
213: */
214: }
215: }
216:
217: /**
218: * Restore the capability of obj to the state stored by
219: * rememberCapabilities
220: */
221: private void resetCapabilities(SceneGraphObject obj) {
222: if (obj == null)
223: return;
224:
225: clearAllCapabilities(obj);
226:
227: Object o = capabilities.get(obj);
228: if (o == null)
229: return;
230:
231: Vector caps = (Vector) o;
232:
233: for (int i = 0; i < caps.size(); i++) {
234: setCapability(obj, (String) caps.get(i));
235: //System.out.println( caps.get(i) );
236: }
237: }
238:
239: /**
240: * Add the capability to those stored for node
241: */
242: public void addCapability(SceneGraphObject node, String capability) {
243: Object obj = capabilities.get(node);
244:
245: //System.out.println("Adding Capability "+node+" "+capability);
246:
247: if (obj == null) {
248: Vector v = new Vector();
249: v.add(capability);
250: capabilities.put(node, v);
251: } else {
252: ((Vector) obj).add(capability);
253: capabilities.put(node, obj);
254: }
255: }
256:
257: /**
258: * Remove the capability from those store for this node
259: */
260: public void removeCapability(SceneGraphObject node,
261: String capability) {
262: Object obj = capabilities.get(node);
263: if (obj == null)
264: throw new RuntimeException("Node not found");
265: else {
266: ((Vector) obj).remove(capability);
267: }
268:
269: }
270:
271: /**
272: * Set this capabilitiy
273: */
274: protected void setCapability(SceneGraphObject node,
275: String capability) {
276: try {
277: Class cl = node.getClass();
278: Field field = cl.getField(capability);
279:
280: node.setCapability(field.getInt(node));
281: } catch (NoSuchFieldException e) {
282: e.printStackTrace();
283: System.exit(1);
284: } catch (IllegalAccessException ex) {
285: ex.printStackTrace();
286: System.exit(1);
287: }
288: }
289:
290: /**
291: * Clear all the capabilities for this node
292: */
293: protected void clearAllCapabilities(SceneGraphObject node) {
294: int value;
295: String str;
296: Class cl = node.getClass();
297:
298: Field[] fields = cl.getFields();
299:
300: try {
301: for (int i = 0; i < fields.length; i++) {
302: str = fields[i].getName();
303: if (str.startsWith("ALLOW_")
304: || str.startsWith("ENABLE_")) {
305: value = fields[i].getInt(fields[i]);
306: node.clearCapability(value);
307: }
308: }
309: } catch (Exception e) {
310: e.printStackTrace();
311: //System.exit(1);
312: }
313:
314: }
315:
316: /**
317: * Return a String[] of the names of all capabilitities set in this
318: * object. If no capabilities are set null is returned
319: */
320: protected Vector extractCapabilities(SceneGraphObject obj) {
321:
322: int value;
323: String str;
324: Class cl = obj.getClass();
325: Vector capabilityStrings = new Vector();
326:
327: Field[] fields = cl.getFields();
328:
329: try {
330: for (int i = 0; i < fields.length; i++) {
331: str = fields[i].getName();
332: if (fields[i].getType() == Integer.TYPE
333: && (str.startsWith("ALLOW_") || str
334: .startsWith("ENABLE_"))) {
335: value = fields[i].getInt(fields[i]);
336: if (obj.getCapability(value))
337: capabilityStrings.add(str);
338: }
339: }
340: } catch (Exception e) {
341: e.printStackTrace();
342: System.exit(1);
343: }
344:
345: if (capabilityStrings.size() == 0)
346: return null;
347:
348: return capabilityStrings;
349: }
350:
351: }
|