The Tree component is really cool! Thanks for this!
The problem I mention below is that I was trying to use the Tree
component in a DataView. I have no particular problems with DataView,
and the Tree is great by itself.
Originally, since I did not want the root node to be shown, I thought
I'd make a list Tree components. That's what my question was about. I
later noticed that I can just make the root node invisible, which is a
much easier approach.
Just out of curiosity, though, what would I have to do to get this kind
of multi-level approach to work (not that it's all that useful...)?
In the Tree component, this is not yet implemented:
setSelectionModel( TreeSelectionModel.SINGLE_TREE_SELECTION );
I hacked together a patch (attached to this mail). Would you mind adding
it, or something similar? I put the collapseAll() into a separate
protected method so that it can be overridden (which I need to do so I
can purge nodes no longer used from memory).
By the way, the only way I was able to modify the model (i.e. by adding
and removing nodes) was by calling setTreeModel() with the updated
model. Is this intended? It somehow doesn't seem all that natural... I
also get the impression that it requires a lot of extra (i.e. not
needed) processing. WDTY?
Cheers,
Dave
On Tue, 2006-05-30 at 17:59 +0900, David Leangen wrote:
> This is great! Thanks!!
>
> BTW, sorry to ask such a basic question... It's about models again.
>
> I noticed that the event is being triggered and the state of the object
> is being correctly changed. However, upon the subsequent request, the
> changed state was not "persisted" (so to speak) in the model, and the
> tree stays unchanged.
>
> This is what I have (largely "inspired" from the examples):
>
> add
> (
> new DataView( "categoryTrees",
> new TreeCategoryDataProvider( getServiceManager() ) )
> {
> private static final long serialVersionUID = 1L;
>
> protected void populateItem( final Item item )
> {
> MyTreeNode treeNode = (MyTreeNode)item.getModelObject();
> TreeModel treeModel = convertToTreeModel( treeNode );
> Tree tree = new MeshTree( "categoryTree", treeModel )
> {
> private static final long serialVersionUID = 1L;
>
> protected String getNodeLabel( DefaultMutableTreeNode
> node )
> {
> final MyTreeNode treeNode =
> (MyTreeNode)node.getUserObject();
> return treeNode.getLabel();
> }
> };
>
> item.add( tree );
> }
> }
> );
>
>
> Convert to TreeModel looks like this:
>
> private TreeModel convertToTreeModel( final MyTreeNode node )
> {
> try
> {
> final DefaultMutableTreeNode rootNode = new
> DefaultMutableTreeNode( node );
> List<MyTreeNode> children = null;
>
> // Get the children from the DB
> children = node.retrieveChildren();
> for( MyTreeNode child : children )
> {
> rootNode.add( new DefaultMutableTreeNode( child ) );
> }
>
> final TreeModel model = new DefaultTreeModel( rootNode );
> return model;
> }
> catch ( FinderException e )
> {
> throw new RuntimeException( e );
> }
> }
>
>
>
> Wondering if you can tell me what I'm doing wrong here...
>
> Thanks so much!
>
>
>
>
> On Mon, 2006-05-29 at 23:02 -0700, Eelco Hillenius wrote:
> > That was something that was not build in yet. I added this to the tree
> > component:
> >
> > /**
> > * Expand or collapse all nodes.
> > *
> > * @param expand
> > * If true, expand all nodes in the tree. Else collapse all
> > nodes
> > * in the tree.
> > */
> > public void expandAll(boolean expand)
> > {
> > TreeNode root = (TreeNode)getTreeState().getModel().getRoot();
> > expandAll(new TreePath(root), expand);
> > }
> >
> > private final void expandAll(TreePath parent, boolean expand)
> > {
> > TreeNode node = (TreeNode)parent.getLastPathComponent();
> > if (node.getChildCount() >= 0)
> > {
> > for (Enumeration e = node.children();
> > e.hasMoreElements();)
> > {
> > TreeNode n = (TreeNode)e.nextElement();
> > TreePath path = parent.pathByAddingChild(n);
> > expandAll(path, expand);
> > }
> > }
> >
> > if (expand)
> > {
> > expandPath(parent);
> > }
> > else
> > {
> > collapsePath(parent);
> > }
> > }
> >
> > /**
> > * Ensures that the node identified by the specified path is expanded
> > and
> > * viewable. If the last item in the path is a leaf, this will have no
> > * effect.
> > *
> > * @param path
> > * the <code>TreePath</code> identifying a node
> > */
> > public void expandPath(TreePath path)
> > {
> > // Only expand if not leaf!
> > TreeModel model = getTreeState().getModel();
> >
> > if (path != null && model != null &&
> > !model.isLeaf(path.getLastPathComponent()))
> > {
> > setExpandedState(path, true);
> > }
> > }
> >
> > /**
> > * Ensures that the node identified by the specified path is collapsed
> > and
> > * viewable.
> > *
> > * @param path
> > * the <code>TreePath</code> identifying a node
> > */
> > public void collapsePath(TreePath path)
> > {
> > setExpandedState(path, false);
> > }
> >
> > and I added two links that use this in the nested example.
> >
> > Have fun with it.
> >
> > Eelco
> >
> >
> > On 5/29/06, David Leangen <[EMAIL PROTECTED]> wrote:
> > >
> > > Hello!
> > >
> > > My assumption when using the TreeModel is that the
> > > wicket.markup.html.Tree is analogous to javax.swing.tree.JTree.
> > >
> > > Although I'm not so familiar with JTree (or anything Swing for that
> > > matter), there are many examples available out there.
> > >
> > >
> > > However, there are a few things that are not so obvious to me right now
> > > with the Wicket TreeModel.
> > >
> > > 1. How do I collapse branches by default?
> > >
> > > 2. I tried the following, but it did not seem to do anything:
> > > getTreeState().getSelectionModel().setSelectionMode(
> > > TreeSelectionModel.SINGLE_TREE_SELECTION );
> > >
> > >
> > > Any hints would be great!
> > >
> > >
> > > Thank you!
> > > Dave
> > >
> > >
> > >
> > > -------------------------------------------------------
> > > All the advantages of Linux Managed Hosting--Without the Cost and Risk!
> > > Fully trained technicians. The highest number of Red Hat certifications in
> > > the hosting industry. Fanatical Support. Click to learn more
> > > http://sel.as-us.falkag.net/sel?cmd=lnk&kid=107521&bid=248729&dat=121642
> > > _______________________________________________
> > > Wicket-user mailing list
> > > [email protected]
> > > https://lists.sourceforge.net/lists/listinfo/wicket-user
> > >
> >
> >
> > -------------------------------------------------------
> > All the advantages of Linux Managed Hosting--Without the Cost and Risk!
> > Fully trained technicians. The highest number of Red Hat certifications in
> > the hosting industry. Fanatical Support. Click to learn more
> > http://sel.as-us.falkag.net/sel?cmd=lnk&kid=107521&bid=248729&dat=121642
> > _______________________________________________
> > Wicket-user mailing list
> > [email protected]
> > https://lists.sourceforge.net/lists/listinfo/wicket-user
>
>
>
> -------------------------------------------------------
> All the advantages of Linux Managed Hosting--Without the Cost and Risk!
> Fully trained technicians. The highest number of Red Hat certifications in
> the hosting industry. Fanatical Support. Click to learn more
> http://sel.as-us.falkag.net/sel?cmd=lnk&kid=107521&bid=248729&dat=121642
> _______________________________________________
> Wicket-user mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/wicket-user
Index: src/java/wicket/markup/html/tree/AbstractTree.java
===================================================================
--- src/java/wicket/markup/html/tree/AbstractTree.java (revision 5944)
+++ src/java/wicket/markup/html/tree/AbstractTree.java (working copy)
@@ -185,9 +185,39 @@
{
final TreePath selection = new TreePath(node.getPath());
setExpandedState(selection, (!treeState.isExpanded(selection))); // inverse
+
+ // If set to SINGLE_TREE_SELECTION, collapse all sibling nodes
+ final int selectionType = getTreeState().getSelectionModel().getSelectionMode();
+ if(TreeSelectionModel.SINGLE_TREE_SELECTION == selectionType)
+ {
+ collapseSiblings(node);
+ }
}
/**
+ * Collapses all the siblings of a given node
+ */
+ protected void collapseSiblings(final DefaultMutableTreeNode node)
+ {
+ // Collapse all previous siblings
+ DefaultMutableTreeNode previousNode = node.getPreviousSibling();
+ while( null != previousNode )
+ {
+ final TreePath siblingSelection = new TreePath(previousNode.getPath());
+ setExpandedState(siblingSelection, false); // inverse
+ previousNode = previousNode.getPreviousSibling();
+ }
+ // Collapse all following siblings
+ DefaultMutableTreeNode nextNode = node.getNextSibling();
+ while( null != nextNode )
+ {
+ final TreePath siblingSelection = new TreePath(nextNode.getPath());
+ setExpandedState(siblingSelection, false); // inverse
+ nextNode = previousNode.getNextSibling();
+ }
+ }
+
+ /**
* Sets the expanded property in the stree state for selection.
*
* @param selection