View Javadoc

1   /* -------------------------------------------------------------------
2    * Java source file for the class Shape3D
3    * 
4    * Copyright (c), 2003, Masahiro Takatsuka.
5    * All Rights Researved.
6    * 
7    * Original Author: Masahiro Takatsuka (masa@jbeans.net)
8    * $Author: takatsukam $
9    * 
10   * $Date: 2004/03/03 11:53:06 $
11   * 
12   * $Id: Shape3D.java,v 1.3 2004/03/03 11:53:06 takatsukam Exp $
13   * 
14   * Reference:		Document no:
15   * ___				___
16   * 
17   * To Do:
18   * ___
19   * 
20  ------------------------------------------------------------------- */
21  
22  /* --------------------------- Package ---------------------------- */
23  package net.jbeans.j3d.scenegraph;
24  
25  /* ------------------ Import classes (packages) ------------------- *//package-summary/html">class="comment"> ------------------ Import classes (packages) ------------------- *//package-summary.html">class="comment">/* ------------------ Import classes (packages) ------------------- *//package-summary.html">class="comment"> ------------------ Import classes (packages) ------------------- */
26  import java.io.*;
27  import java.lang.reflect.*;
28  import java.util.*;
29  import javax.swing.event.*;
30  import javax.media.j3d.Geometry;
31  import javax.media.j3d.Node;
32  
33  import net.jbeans.lang.*;
34  import net.jbeans.j3d.event.*;
35  import net.jbeans.util.debug.*;
36  
37  /*====================================================================
38                     Implementation of class Shape3D                    
39  ====================================================================*/
40  /***
41   * generally describe Shape3D in here
42   * 
43   * @version $Revision: 1.3 $
44   * @author Masahiro Takatsuka (masa@jbeans.net)
45   * @see javax.media.j3d.Shape3D
46   * @see Serializable
47   */
48  
49  public class Shape3D extends javax.media.j3d.Shape3D implements Serializable {
50  	private static final boolean DEBUG = Debug.getDebugFlag(Shape3D.class);
51  
52  	
53  	/* ------------------------ not serialized ------------------------ */
54  	transient private EventListenerList sceneGraphObjectListeners;
55  	transient private EventObject myEvent;
56  
57      public Shape3D() {
58  		super();
59  		initialize();
60      }
61  	
62      public Shape3D(Geometry geometry) {
63          super(geometry);
64  		initialize();		
65      }
66  	
67      public Shape3D(Geometry geometry, Appearance appearance) {
68  		super(geometry, appearance);
69  		initialize();		
70      }
71  
72  	protected void initialize() {
73  		this.sceneGraphObjectListeners = new EventListenerList();
74  		this.myEvent = new EventObject(this);
75  
76  		setCapability(Node.ALLOW_BOUNDS_READ);
77  		setCapability(Node.ALLOW_LOCAL_TO_VWORLD_READ);
78  		setCapability(Node.ALLOW_AUTO_COMPUTE_BOUNDS_READ);
79  		setCapability(Node.ALLOW_AUTO_COMPUTE_BOUNDS_WRITE);		
80  		setCapability(Node.ALLOW_BOUNDS_READ);
81  		setCapability(Node.ALLOW_BOUNDS_WRITE);		
82  		setCapability(Node.ALLOW_COLLIDABLE_READ);
83  		setCapability(Node.ALLOW_COLLIDABLE_WRITE);		
84  		setCapability(Node.ALLOW_PICKABLE_READ);
85  		setCapability(Node.ALLOW_PICKABLE_WRITE);		
86  		setCapability(Shape3D.ALLOW_APPEARANCE_OVERRIDE_READ);
87  		setCapability(Shape3D.ALLOW_APPEARANCE_OVERRIDE_WRITE);		
88  		setCapability(Shape3D.ALLOW_APPEARANCE_READ);
89  		setCapability(Shape3D.ALLOW_APPEARANCE_WRITE);		
90  		setCapability(Shape3D.ALLOW_COLLISION_BOUNDS_READ);
91  		setCapability(Shape3D.ALLOW_COLLISION_BOUNDS_WRITE);		
92  		setCapability(Shape3D.ALLOW_GEOMETRY_READ);
93  		setCapability(Shape3D.ALLOW_GEOMETRY_WRITE);
94  
95  		setBoundsAutoCompute(true);
96  
97  		/* this is for temp */
98  		setAppearance(createMaterialAppearance());
99  	}
100 
101 	private boolean contains(Geometry geometry) {
102 		int cnt = numGeometries();
103 		for (int i = 0; i < cnt; i++) {
104 			if (geometry == getGeometry(i)) {
105 				if (DEBUG) {
106 					System.out.println("duplicate geometry");
107 				}
108 				return true;
109 			}
110 		}
111 		return false;
112 	}
113 	
114 	/***
115 	 *
116 	 */
117     public void addGeometry(Geometry geometry) {
118 		if (contains(geometry)) {
119 			return;
120 		}
121 		if (DEBUG) {
122 			System.out.println("Shape3D : adding " + geometry);
123 		}
124 		super.addGeometry(geometry);
125 		fireSceneGraphObjectChanged();
126     }
127 
128 	/***
129 	 *
130 	 */
131     public void insertGeometry(Geometry geometry, int i) {
132 		if (contains(geometry)) {
133 			return;
134 		}
135 		super.insertGeometry(geometry, i);
136 		fireSceneGraphObjectChanged();
137 	}
138 
139 	/***
140 	 *
141 	 */
142     public void setGeometry(Geometry geometry) {
143 		super.setGeometry(geometry);
144 		fireSceneGraphObjectChanged();
145 	}
146 	
147 	/***
148 	 *
149 	 */
150     public void setGeometry(Geometry geometry, int i) {
151 		super.setGeometry(geometry, i);
152 		fireSceneGraphObjectChanged();
153 	}
154 	
155 	/***
156 	 *
157 	 */
158 	public void removeGeometry(Geometry geom) {
159 		/*
160 		 * I could use Enumerator to go throgh all child but
161 		 * direct access might be faster..
162 		 */
163 		int allCnt = numGeometries();
164 		int i = -1;
165 		for (i = 0; i < allCnt; i++) {
166 			if (geom.equals(getGeometry(i))) {
167 				removeGeometry(i);
168 				return;
169 			}
170 		}
171 	}
172 
173 	/***
174 	 *
175 	 */
176 	public void removeAllGeometry() {
177 		int allCnt = numGeometries();
178 		for (int i = 0; i < allCnt; i++) {
179 			removeGeometry(i);
180 		}
181 	}
182 
183 	/***
184 	* 
185 	*/
186 	public void setGeometries(Geometry[] geometries) {
187 		for (int i = 0; i < geometries.length; i++) {
188 			super.setGeometry(geometries[i], i);
189 		}
190 		fireSceneGraphObjectChanged();
191 	}
192 
193 	/***
194 	* 
195 	*/
196 	public void addGeometries(Geometry[] geometries) {
197 		for (int i = 0; i < geometries.length; i++) {
198 			super.addGeometry(geometries[i]);
199 		}
200 		fireSceneGraphObjectChanged();
201 	}
202 
203 	/***
204      * adds an SceneGraphObjectListener to the shape
205      */
206     public void addSceneGraphObjectListener(SceneGraphObjectListener l) {
207         this.sceneGraphObjectListeners.add(SceneGraphObjectListener.class, l);
208     }
209 
210     /***
211      * removes an GeometryListener from the shape.
212      */
213     public void removeSceneGraphObjectListener(SceneGraphObjectListener l) {
214 		this.sceneGraphObjectListeners.remove(SceneGraphObjectListener.class, l);
215 		/*
216 		 * This code is no longer used since Shape3D is added to
217 		 * BranchGroup or some other Group object using "self" connector.
218 		 * Masahiro Takatsuka/2001-Jul-14
219 		 */
220 		/* the lister "l" might have a reference of this object.
221 		   so, remove it!
222 		*/
223 //  		if (l instanceof Proxy) {
224 //  			InvocationHandler handler = Proxy.getInvocationHandler(l);
225 //  			Object target = AdapterHandler.getTarget(handler);
226 //  			if (target != null) {
227 //  				try {
228 //  					Object[] args = {this};
229 //  					Class[] argTypes = {Node.class};
230 //  					ClassUtil.makeObjectPerform(target, "removeChild", args, argTypes);						
231 //  				} catch (Exception e) {
232 //  					if (DEBUG) {
233 //  						e.printStackTrace();
234 //  					}
235 //  				}
236 //  			}
237 //  		}
238     }
239 
240     /***
241      * Notify all listeners that have registered interest for
242      * notification on this event type.
243      */
244     public void fireSceneGraphObjectChanged() {
245         Object[] listeners = this.sceneGraphObjectListeners.getListenerList();
246         // Process the listeners last to first, notifying
247         // those that are interested in this event
248         for (int i = listeners.length - 2; i >= 0; i -= 2) {
249             if (listeners[i] == SceneGraphObjectListener.class) {
250                 // Lazily create the event:
251                 ((SceneGraphObjectListener)listeners[i+1]).objectChanged(this.myEvent);
252             }
253         }
254     }
255 
256     /***
257      * Serialization methods
258      */
259     private void readObject(ObjectInputStream s) throws ClassNotFoundException, IOException {
260 		initialize();
261     }
262 
263     /***
264      * Serialization methods
265      */
266     private void writeObject(ObjectOutputStream s) throws IOException {
267 		// NOP.
268     }
269 
270 	/* this is for temp */
271     private static Appearance createMaterialAppearance(){
272 
273         Appearance materialAppear = new Appearance();
274 		setCapabilities(materialAppear);	
275         PolygonAttributes polyAttrib = new PolygonAttributes();
276         polyAttrib.setCullFace(PolygonAttributes.CULL_NONE);
277         materialAppear.setPolygonAttributes(polyAttrib);
278 
279   		Material material = new Material();
280 		material.setLightingEnable(true);
281 		materialAppear.setMaterial(material);
282 
283         return materialAppear;
284     }
285 	
286 	private static void setCapabilities(Appearance appear) {
287 		appear.setCapability(Appearance.ALLOW_COLORING_ATTRIBUTES_READ);
288 		appear.setCapability(Appearance.ALLOW_COLORING_ATTRIBUTES_WRITE);
289 		appear.setCapability(Appearance.ALLOW_LINE_ATTRIBUTES_READ);
290 		appear.setCapability(Appearance.ALLOW_LINE_ATTRIBUTES_WRITE);
291 		appear.setCapability(Appearance.ALLOW_MATERIAL_READ);
292 		appear.setCapability(Appearance.ALLOW_MATERIAL_WRITE);
293 		appear.setCapability(Appearance.ALLOW_POINT_ATTRIBUTES_READ);
294 		appear.setCapability(Appearance.ALLOW_POINT_ATTRIBUTES_WRITE);
295 		appear.setCapability(Appearance.ALLOW_POLYGON_ATTRIBUTES_READ);
296 		appear.setCapability(Appearance.ALLOW_POLYGON_ATTRIBUTES_WRITE);
297 		appear.setCapability(Appearance.ALLOW_RENDERING_ATTRIBUTES_READ);
298 		appear.setCapability(Appearance.ALLOW_RENDERING_ATTRIBUTES_WRITE);
299 		appear.setCapability(Appearance.ALLOW_TEXGEN_READ);
300 		appear.setCapability(Appearance.ALLOW_TEXGEN_WRITE);
301 		appear.setCapability(Appearance.ALLOW_TEXTURE_ATTRIBUTES_READ);
302 		appear.setCapability(Appearance.ALLOW_TEXTURE_ATTRIBUTES_WRITE);
303 		appear.setCapability(Appearance.ALLOW_TEXTURE_READ);
304 		appear.setCapability(Appearance.ALLOW_TEXTURE_WRITE);
305 		appear.setCapability(Appearance.ALLOW_TEXTURE_UNIT_STATE_READ);
306 		appear.setCapability(Appearance.ALLOW_TEXTURE_UNIT_STATE_WRITE);
307 		appear.setCapability(Appearance.ALLOW_TRANSPARENCY_ATTRIBUTES_READ);
308 		appear.setCapability(Appearance.ALLOW_TRANSPARENCY_ATTRIBUTES_WRITE);
309 	}
310 }