FileDocCategorySizeDatePackage
Java3dTree.javaAPI DocExample11821Tue Jan 04 18:38:26 GMT 2000com.tornadolabs.j3dtree

Java3dTree

public class Java3dTree extends JFrame implements TreeSelectionListener
Java3dTree Display a Java 3D scenegraph in a Swing Tree control
author
Daniel Selman
version
1.0

Fields Summary
private final String
m_kszLineStyle
private JEditorPane
m_TextPane
private JSplitPane
m_SplitPane
private VirtualUniverse
m_Universe
private JTree
m_Tree
private final String
m_kszPackageName
private Hashtable
m_ObjectInfoTable
Constructors Summary
public Java3dTree()

	
	 
	
		super( "Java 3D Scenegraph" );
		buildObjectInfoTable();
   
Methods Summary
private voidaddObjectInfoTableItem(java.lang.String szClass)

		// strip the package name from szClass (everything before the final ".")
		int nIndex = szClass.lastIndexOf( "." );
		
		String szTrimedClass = szClass;
		
		if( nIndex > -1 )
			szTrimedClass = szClass.substring( nIndex+1, szClass.length() );
		
		szTrimedClass = m_kszPackageName + szTrimedClass;
		szTrimedClass += "_Info";
		
		// System.out.println( szClass + "-->" + szTrimedClass );
		
		Class classObject = getClass( szTrimedClass );
		
		try
		{
			if( classObject != null )
			{
				m_ObjectInfoTable.put( szClass, classObject.newInstance() );
				return;
			}
		}
		catch( Exception e )
		{
			System.err.println( e.toString() );
		}

		System.err.println( "Unable to create a new instance of info class for: " + szClass );
	
private voidbuildObjectInfoTable()

		m_ObjectInfoTable = new Hashtable( 100 );
				
		addObjectInfoTableItem( "javax.media.j3d.Alpha" );
		addObjectInfoTableItem( "javax.media.j3d.AmbientLight" );
		addObjectInfoTableItem( "javax.media.j3d.Appearance" );
		addObjectInfoTableItem( "javax.media.j3d.Background" );
		addObjectInfoTableItem( "javax.media.j3d.Behavior" );
		addObjectInfoTableItem( "javax.media.j3d.Bounds" );
		addObjectInfoTableItem( "javax.media.j3d.BoundingLeaf" );
		addObjectInfoTableItem( "javax.media.j3d.BranchGroup" );		
		addObjectInfoTableItem( "javax.media.j3d.ColoringAttributes" );
		addObjectInfoTableItem( "javax.media.j3d.Clip" );
		addObjectInfoTableItem( "javax.media.j3d.DirectionalLight" );
		addObjectInfoTableItem( "javax.media.j3d.Fog" );
		addObjectInfoTableItem( "javax.media.j3d.Group" );
		addObjectInfoTableItem( "javax.media.j3d.Interpolator" );
		addObjectInfoTableItem( "javax.media.j3d.Leaf" );
		addObjectInfoTableItem( "javax.media.j3d.Light" );
		addObjectInfoTableItem( "javax.media.j3d.LineAttributes" );
		addObjectInfoTableItem( "javax.media.j3d.Link" );
		addObjectInfoTableItem( "javax.media.j3d.Locale" );
		addObjectInfoTableItem( "javax.media.j3d.Material" );
		addObjectInfoTableItem( "javax.media.j3d.Morph" );
		addObjectInfoTableItem( "javax.media.j3d.OrderedGroup" );
		addObjectInfoTableItem( "javax.media.j3d.PointAttributes" );
		addObjectInfoTableItem( "javax.media.j3d.PointLight" );
		addObjectInfoTableItem( "javax.media.j3d.PolygonAttributes" );
		addObjectInfoTableItem( "com.sun.j3d.utils.geometry.Primitive" );
		addObjectInfoTableItem( "javax.media.j3d.RenderingAttributes" );
		addObjectInfoTableItem( "javax.media.j3d.SceneGraphObject" );
		addObjectInfoTableItem( "javax.media.j3d.Shape3D" );
		addObjectInfoTableItem( "javax.media.j3d.SharedGroup" );
		addObjectInfoTableItem( "com.sun.j3d.utils.universe.SimpleUniverse" );
		addObjectInfoTableItem( "javax.media.j3d.Sound" );
		addObjectInfoTableItem( "javax.media.j3d.Soundscape" );
		addObjectInfoTableItem( "javax.media.j3d.SpotLight" );
		addObjectInfoTableItem( "javax.media.j3d.Switch" );
		addObjectInfoTableItem( "javax.media.j3d.TexCoordGeneration" );
		addObjectInfoTableItem( "javax.media.j3d.Texture" );
		addObjectInfoTableItem( "javax.media.j3d.TextureAttributes" );
		addObjectInfoTableItem( "javax.media.j3d.Transform3D" );
		addObjectInfoTableItem( "javax.media.j3d.TransformGroup" );
		addObjectInfoTableItem( "javax.media.j3d.TransparencyAttributes" );
		addObjectInfoTableItem( "com.sun.j3d.utils.universe.ViewingPlatform" );
		addObjectInfoTableItem( "javax.media.j3d.ViewPlatform" );
		addObjectInfoTableItem( "javax.media.j3d.VirtualUniverse" );
	
private voiddisplayText(java.lang.String szText)

		m_TextPane.setText( szText );
	
java.lang.ClassgetClass(java.lang.String szClass)

		Class classObject = null;
		
		try
		{
			classObject = Class.forName( szClass );
		}
		catch( Exception e )
		{
			try
			{
				classObject = Class.forName( m_kszPackageName + "ObjectInfo" );
			}
			catch( Exception e2 )
			{
				classObject = null;
			}
		}
		
		return classObject;
	
ObjectInfogetObjectInfo(java.lang.String szClass)

		// look it up, maybe we have a direct match...		
		// System.out.println( "Looking up: " + szClass );
		
		ObjectInfo objReturn = (ObjectInfo) m_ObjectInfoTable.get( szClass );
		// System.out.println( "Direct Match: " + szClass );
		
		Class classObject = null;
		
		try
		{
			classObject = Class.forName( szClass );
		}
		catch( Exception e )
		{
		}
		
		Class parentClass = null;
		
		if( classObject != null )
			parentClass = classObject.getSuperclass();
		
		while( objReturn == null && parentClass != null )
		{			
			// if we did not get a match we should walk
			// up the inheritance tree and look for a match on a base classe
			objReturn = (ObjectInfo) m_ObjectInfoTable.get( parentClass.getName() );			
			parentClass = parentClass.getSuperclass();
			
			// System.out.println( "Parent Class: " + parentClass );
		}
		
		// System.out.println( "Returning: " + objReturn );
		
		return objReturn;
	
public voidrecurseObject(java.lang.Object obj, javax.swing.tree.DefaultMutableTreeNode parent)

		if( obj != null )
		{
			ObjectInfo objInfo = getObjectInfo( obj.getClass().getName() );
			
			if( objInfo != null )
				objInfo.addToTree( this, parent, obj );
		}
		else
			System.err.println( "Warning: ignored null object in recurseObject." );
	
public voidrecursiveApplyCapability(java.lang.Object obj)

		try
		{
			if( obj != null )
			{			
				ObjectInfo objInfo = getObjectInfo( obj.getClass().getName() );
				
				if( objInfo != null )
					objInfo.setCapabilityBits( this, obj );
			}
			else
				System.err.println( "Warning: ignored null object in recursiveApplyCapability." );
		}
		catch( Exception e )
		{
			System.err.println( "Warning: caught Exception applying capabilities: " + e.toString() );
		}
	
public voidupdateNodes(VirtualUniverse universe)

		m_Universe = universe;
		
		//Create the top level parent node.
		DefaultMutableTreeNode top = new DefaultMutableTreeNode( "Scenegraph" );
		
		recurseObject( m_Universe, top );
		
		//Create a tree that allows one selection at a time.
		m_Tree = new JTree(top);
		m_Tree.getSelectionModel().setSelectionMode( TreeSelectionModel.SINGLE_TREE_SELECTION );
		
		//Listen for when the selection changes.
		m_Tree.addTreeSelectionListener( this );

		//Create the scroll pane and add the tree to it. 
		JScrollPane treeView = new JScrollPane(m_Tree);
		Dimension minimumSize = new Dimension(100, 50);
		treeView.setMinimumSize(minimumSize);
				
		//Add the scroll panes to a split pane.
		if( m_SplitPane == null )
		{
			//Create the viewing pane.
			m_TextPane = new JEditorPane();
			m_TextPane.setEditable(false);
			JScrollPane htmlView = new JScrollPane(m_TextPane);

			m_SplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
			
			m_SplitPane.setTopComponent(treeView);
			m_SplitPane.setBottomComponent(htmlView);
		
			htmlView.setMinimumSize(minimumSize);
			m_SplitPane.setDividerLocation(100);
		
			m_SplitPane.setPreferredSize(new Dimension(500, 300));
		
			//Add the split pane to this frame.
			getContentPane().add( m_SplitPane, BorderLayout.CENTER);
			
			pack();
			setVisible( true );
		}
		else
			m_SplitPane.setTopComponent(treeView);
	
public voidvalueChanged(javax.swing.event.TreeSelectionEvent e)

		DefaultMutableTreeNode node = (DefaultMutableTreeNode) m_Tree.getLastSelectedPathComponent();
		
		if (node == null) 
			return;
		
		Object sceneGraphObject = node.getUserObject();
		
		// get the _Info object for the node type
		ObjectInfo objInfo = getObjectInfo( sceneGraphObject.getClass().getName() );
		
		try
		{
			if( objInfo != null )
			{
				String szText = objInfo.getInfo( sceneGraphObject );
				// System.out.println( szText );
				displayText( szText );
			}
			else
				displayText( "No Info Handler for:" + sceneGraphObject );
		}
		catch( Exception ex )
		{
			displayText( ex.toString() );
		}