You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by tsvetelin <ts...@rushmore-digital.com> on 2004/06/08 08:42:54 UTC

Contrib:Tree - try to describe some parts of it

Hi Scot and All,

The default implementation of the ITreeStateModel (class that contains the
expanded nodes) use a Set object to store the IDs of expanded nodes. At the
other hand the SimpleTreeDataModel return javax.swing.tree.TreePath as node
ID. In this case you should implement the hashcode and equals of your nodes
because TreePath use the CTreeNode hashcode() and equals() methods.

Tsvetelin.

PS. I will try to describe some parts of the contrib tree.

First of all : the tree model structure is designed in a way that you don't
need to extend any classes from the tree packages if you have the existed
tree objects data structure. In this case the only thing that have to do is
to make custom implementation ITreeDataModel. There are methods in this
interface that describe the necessary aspects of your custom data structure
that are required for normal working of the tree component.

At the other hand there is the default implementation of the tree object
data structure (CTreeNode) and corresponded implementation of the
ITreeDataModel: SimpleTreeDataModel. The simple implementation can be used
as a base for customs data structures if this is possible.

There are a few things that are very important in communication between
TreeComponent and it data structure. The default implementation of the node
doesn't contains the ID property. The reason is that there are unlimited
number of possible types of node identifiers. For example in a file system
example, the ID of the folder is it's name but this name is unique only in
corresponded parent. You can have the folder with the same name in another
parent folder. In this case the unique ID of the particular folder is the
TreePath from Root to the current folder that contains the list of IDs for
each folder. At the other hand if you have a database tree node structure,
it is possible to have a unique number property of tree node. In this case
the node ID that will be returned be ITreeDataModel is long value.

To cover the previous cases SimpleTreeDataModel return
javax.swing.tree.TreePath object as node identifier. The TreePath contains
the list of node objects from root to particular node and use the equals and
hashcode methods of the node data objects when the node IDs are compared
usually during the rendering the nodes when the tree determine which of the
nodes are expanded. This is the "worst" case, because the node ID is a
complex object structure but it cover all requirements.

Some best practices:
1. If you are using the default implementation of tree data structure
override the equals and hashcode methods in CTreeNode.
2. Return as much as possible short types of node ID, because it is
serialized in node html links as a parameter. For example in the file system
demo the node ID is a TreePath of file names but if you have long field that
is unique in whole tree structure (some database id) use it as node ID.
3. If you have a custom tree data structure, make your own implementation of
ITreeDataModel this is the only thing that is required.

Tsvetelin


-----Original Message-----
From: Scott Ellsworth [mailto:scott@alodar.com]
Sent: 08 Þíè 2004 ã. 05:40
To: Tapestry users
Subject: Re: Documentation/Example for a tree?



On Jun 7, 2004, at 7:16 PM, Scott Ellsworth wrote:

> On Jun 7, 2004, at 5:47 PM, Scott Ellsworth wrote:
>
>> I seem to recall that Tapestry has a tree component lurking in it.
>> For the life of me, I cannot find docs on it.  My hope is to have
>> several trees on the main page, something like:
>
> Ok, I have dissected the Workshop example, and now have some trees
> showing.
>
> I implemented simple extensions to SimpleTreeDataModel and TreeNode
> for my datab model and my nodes in the tree.  The TreeModel is just a
> SimpleTreeModel created from the SimpleTreeDataModel.
>
> One strangeness - when I click on the + sign or the underlined text
> for my tree, it does not expand.  It seems to understand that it has
> children, but they are not showing.  I very likely just deleted
> something important while adapting code.  Is there an obvious place to
> start looking?

Realizing that some actual code might help, here is the code that
creates my top level TreeNode:

public class QueueTreeNode extends TreeNode {
	private Queue queue;

	public QueueTreeNode(Queue queue) {
		this.queue = queue;
		for (Iterator iterator = queue.getProtocols().iterator();
iterator.hasNext();) {
			Protocol protocol = (Protocol) iterator.next();
			ProtocolTreeNode protocolTreeNode = new ProtocolTreeNode(protocol);
			insert(protocolTreeNode);
			protocolTreeNode.setParent(this);
		}
	}

	public String toString() {
		return queue.toString();
	}
}

In other words, I am creating some children and adding them with
insert, as well as explicitly setting the parent of the children.
(Even if I take that out, it does not make a difference in behavior.)
These nodes are showing up correctly, but their children do not show up
when clicked.

The protocol tree nodes, which we do not see, are created with:

public class ProtocolTreeNode extends TreeNode {
	private Protocol protocol;

	public ProtocolTreeNode(Protocol p) {
		protocol = p;
		for (Iterator iterator = protocol.getInputResources().iterator();
iterator.hasNext();) {
			Resource resource = (Resource) iterator.next();
			insert(new ResourceTreeNode(resource));
		}
	}

	public String toString() {
		return protocol.toString();
	}
}


My tree data model:

public class QueueDataModel extends SimpleTreeDataModel
		implements Serializable {
	public QueueDataModel(Queue queue) {
		super(new QueueTreeNode(queue));
	}
}

It does not get much simpler than that.

The init:

	public void initTableModel() {
		activeQueueDataModel = new QueueDataModel(activeQueue);
		activeQueueTreeModel = new SimpleTreeModel(activeQueueDataModel);
	}


I have not implemented a sessionStateManager, nor a treeStateListener.

Scott


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.691 / Virus Database: 452 - Release Date: 26.5.2004 ã.

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.691 / Virus Database: 452 - Release Date: 26.5.2004 ã.


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org