RE: JTree

[email protected] Tue, 1 Mar 2005 13:55:24 -0800
Newsgroups gmane.comp.embedded.carlsbad-cubes
Message-ID <BB7491BA8B7C53409CBD58F7F79D8C3B181C85@DED01EXVS01.wcbradley.local>
Very Cool!

I agree with all your points.  I can create the TreeNode outside of the
swixML file and it probably makes more sense to do that.  I had
originally wanted to serialize the TreeModel into XML as well but I may
not need to.  I followed your suggestion of creating a class which
creates the TreeNode and then using the initclass attribute to
initialize the tree. 

For those who might be interested, here is the example code I created to
test the functionality (or figure out how it works).  

public class AvailableTreeNodes extends DefaultTreeModel {
	private DefaultMutableTreeNode rootNode = null;

	public AvailableTreeNodes() {
		super(new DefaultMutableTreeNode("Root Node"));
		rootNode = (DefaultMutableTreeNode)this.getRoot();
		rootNode.add(new DefaultMutableTreeNode("Node 1 Level
1"));
		( (DefaultMutableTreeNode)rootNode.getFirstChild()
).add(new DefaultMutableTreeNode("Node 1 Level 2"));
		( (DefaultMutableTreeNode)rootNode.getFirstChild()
).add(new DefaultMutableTreeNode("Node 2 Level 2"));

		rootNode.add(new DefaultMutableTreeNode("Node 2 Level
1"));
		rootNode.add(new DefaultMutableTreeNode("Node 3 Level
1"));
		rootNode.add(new DefaultMutableTreeNode("Node 4 Level
1"));
	}
}

I placed the following line in my swixML file:

<tree initclass="AvailableTreeNodes" />

I'll have to play with this within the context of the application I'm
building.  

Thanks Frank!

Andrew

-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of
[email protected]
Sent: Tuesday, March 01, 2005 3:54 PM
To: List for Users of Carlsbad Cubes' Technologies and Products
Subject: Re: [Forum] JTree

[email protected] wrote:
> Hello All,
> 
> Has anyone been successful creating a tree with it's nodes from an XML

> descriptor file?  I've been playing around with this today and can not

> get around the fact that DefaultMutableTreeNode, TreeNode, etc. do not

> extend from either Component or Container.

This is generally not supported, because it is the model you would like
to describe with the xml. I might say SwixMl is about 5% M, 90% V and 5%
C of the MVC acronym. The 5% of the M goes IMHO only to the initclass
but does not help *that* much for your intended goal. The only thing is
to write a complete model for this, which you have to specify in the
initclass attribute.


> The addChild method of the
> Parser object requires that the object you are adding be a Component 
> or inherited from Component.  I would like to have an xml document 
> that is something like the following:
> 
> <tree>
>         <node>text1</node>
>         <node>text2</node>
> </tree>
> 
> I have messed a little with trying to use the initclass attribute of 
> the tree tag but no luck so far.  Any suggestion would be appreciated.

Try this:
1. Write a tree model which creates the appropriate tree in a standalone
application.
2. Specify this model-class in the initclass attribute of your tree in
the SwixMl file


 >  Does
> anyone know if this functionality is scheduled for the next release?  
> If not I am going to try and adjust the addChild method myself in the 
> following manor:
> 
> 1. Ensure the appropriate packages are imported for 
> DefaultMutableTreeNode 2. Change the component parameter of the 
> addChild method to an Object type 3. Create an if else block that is 
> true for (component instanceof
> DefaultMutableTreeNode)
> 4. Populate this block with the appropriate code to generate a node 
> structure for the parent (Container) tree.
> 5. Make addChild throw an exception on no match of any if or if else
block.
> 6. Adjust the calling method, getSwing, to handle the exception.

You may do as you wish but I wouldn't recommend you doing so. This is
really a hack, it may look like a tree but not act like a real one. The
correct approach is to modify the model of the underlying tree otherwise
the changes on your tree are lost at random, you don't get informed
about tree changes and the like.

> 
> There may be more steps required once I dive deeper.  Of course, if 
> someelse has a better solution or is working on this please let me
know.

If you really need it for rapid prototyping, you may be better with the
following approach: Write a class which converts a given file name to a
valid TreeModel. Turn this class into a SwixMl Converter class and give
the filename to your intended tree in the model attribute of your tree.

As you mentioned, this may rise additional difficulties along
implementation, but it seems a reasonable way to do it.

Yours,

Frank

> 
> Thanks,
> Andrew
> 
> 
> ----------------------------------------------------------------------
> --
> 
> _______________________________________________
> Forum mailing list
> [email protected]
> http://carlsbadcubes.com/mailman/listinfo/forum_carlsbadcubes.com


_______________________________________________
Forum mailing list
[email protected]
http://carlsbadcubes.com/mailman/listinfo/forum_carlsbadcubes.com