waffel’s Weblog

September 9, 2008

expanding richfaces tree on datamodel changes

Filed under: java,JSF — Thomas Wabner @ 2:19 nachmittags
Tags: , , ,

Sometimes you will select a tree node in a richfaces tree, triggered by your datamodel. There are several hints how to do this with a tree state advisor, but I have found another way to do this.

My main problem was, that I do not have a relation between my datamodel and the richfaces UITree components (nodes). This problem was introduced because I use the recursiveTreeAdaptor. I have not found a way to get a TreeRowData object from the UITree for my datamodel object.

I have searched in the richfaces implementation, if there is another way to work with my datamodel which have to expand the tree, if the datamodel is changed.

Lets clearify what I want to do: I have a datamodel with some root nodes and “normal” nodes in a tree like structure. I have also (in my TreeManager) a parameter which holds a selectedNode. A node can selected from the UI (the user clicks on a tree node and causes a node selection event) or from my datamodel, which calls a method in the TreeManager to select a specific tree node.

The tree manager contains a method to select a specific node like this:

public void select(final Node nextStepNode) {
  this.selectedNode = nextStepNode;

}

To expand the tree to the selected node, I have changed to select method:

public void select(final Node nextStepNode) {
  this.selectedNode = nextStepNode;
  try {
      // walk over the tree which expands the tree and uses a complete tree
      // range
      tree.walk(FacesContext.getCurrentInstance(), expandingDataVisitor, new CompleteTreeRange(), null, null);
    } catch (final IOException e) {
      if (LOG.isErrorEnabled()) {
        LOG.error(String.format("problem %s", e.getMessage()));
      }
    }
}

The tree walk method walks over the tree using my own data visitor and tree range. The tree range implementation tolds the tree to walk also over hidden nodes. You remember? The tree should expand all nodes to the selected node, also if the selected node is hidden.

The tree range implementation looks like:

public class CompleteTreeRange implements TreeRange {

  /**
   * {@inheritDoc}
   */
  @SuppressWarnings("unchecked")
  public boolean processChildren(final TreeRowKey rowKey) {
    return true;
  }

  /**
   * {@inheritDoc}
   */
  @SuppressWarnings("unchecked")
  public boolean processNode(final TreeRowKey rowKey) {
    return true;
  }

Now comes the hard stuff on my solution, the expandingDataVisitor:

public class ExpandingDataVisitor implements DataVisitor {
  public void process(final FacesContext theContext, final Object theRowKey, final Object theArgument)
      throws IOException {
    if (selectedNode.equals(tree.getRowData(theRowKey))) {
      tree.queueNodeExpand((TreeRowKey) tree.getParentRowKey(theRowKey));
    }
}

You can queue node expanding with a tree rowKey to the node, which you want to expand. You don’t need to queue all nodes on the path. The queueNodeExpand method works fine with one rowkey. The row describes itself the complete path to the node. If you use standard jsf ID’s, a possible treeRowKey looks like “jsp123:jsp124:jsp125″ wich is the complete path to your node (row key).

You have to know, that you don’t want expand the selected node (this is in most cases a leafe). You want to expand the tree to the parent of the selected node.

About these ads

12 Kommentare »

  1. [...] in Dienstag, 9. September 2008 von Andreas Höhmann Waffel beschreibt in seinem neuesten Blogbeitrag wie man den Richfaces Tree auch aus dem System heraus ansprechen kann um eine bestimmte Node zu [...]

    Pingback von Richfaces internal Tree Node Selection Howto « Andreas Höhmann’s Weblog — September 9, 2008 @ 11:12 nachmittags | Antwort

  2. Hello. This is a great solution and I need exactly that. Is it possible to get the complete source code of these classes ?

    Thank you in advance.

    V.

    Kommentar von Vincent Crepin — September 19, 2008 @ 7:43 nachmittags | Antwort

  3. Hello

    Have you ever tried to remove treeNode from your Tree… I just can’t seem to get it with removeChild()

    Ravi

    Kommentar von ravi — Oktober 16, 2008 @ 11:31 nachmittags | Antwort

  4. Hi. Thanks for these tips.
    Which richfaces jar file is these tips meant for? Richfaces 3.2.2?

    tree.getParentRowKey(theRowKey) this method is only availble in this release.

    Is there anyway to expand tree down to selected node in any other way? I am using seam and it’s not yet compatible with 3.2.2.

    Kommentar von Tomas — Oktober 22, 2008 @ 3:31 nachmittags | Antwort

  5. Fine….but the source code where is it?

    Thanks….

    Kommentar von garve — März 17, 2009 @ 11:41 nachmittags | Antwort

  6. Hi!
    That’s an interesting post.
    But is there anywhere sourcecode or an example available?!

    Thank you in advance.

    Kommentar von Schmu — Mai 18, 2009 @ 3:11 nachmittags | Antwort

    • Hi,

      I have not prepared a complete source code example yet. If you have a specific question don’t hesitate to ask me. I’tt try to clearyfy this then in the article.

      Evtl. I’ll try to create a more complete example with source code but currently I have no time to do this … sorry.

      Kommentar von Thomas Wabner — Mai 28, 2009 @ 2:59 nachmittags | Antwort

  7. Hi !!!

    I have 0-level tree and set of links (each link is ‘mapped’ to every tree node)
    Please tell how can I do next task -> when I click on link the appropriate tree node is selected.
    I know there’s setSelected() method in UITree class, but this method don’t work.

    PS. Sorry for my bad English :-)

    Kommentar von Timur — Juli 28, 2009 @ 8:52 nachmittags | Antwort

  8. how about default expanded rich:tree? How can it be possible=

    Kommentar von Mustafa Sait Özen — August 17, 2009 @ 1:16 nachmittags | Antwort

  9. hi

    Is it possible to expand only the selected node upto its leaf node????????????????

    Kommentar von Devika — September 8, 2010 @ 1:07 nachmittags | Antwort

  10. Hello. This is a great solution and I need exactly that. Is it possible to get the complete source code of these classes ?

    Thank you in advance.

    Martha.

    Kommentar von Martha — November 9, 2010 @ 7:01 nachmittags | Antwort

  11. [...] The busiest day of the year was February 5th with 128 views. The most popular post that day was expanding richfaces tree on datamodel changes. [...]

    Pingback von 2010 in review « waffel’s Weblog — Januar 3, 2011 @ 1:04 nachmittags | Antwort


RSS-Feed für Kommentare zu diesem Beitrag. TrackBack URI

Kommentar verfassen

Trage deine Daten unten ein oder klicke ein Icon um dich einzuloggen:

WordPress.com-Logo

Du kommentierst mit Deinem WordPress.com-Konto. Abmelden / Ändern )

Twitter-Bild

Du kommentierst mit Deinem Twitter-Konto. Abmelden / Ändern )

Facebook-Foto

Du kommentierst mit Deinem Facebook-Konto. Abmelden / Ändern )

Google+ photo

Du kommentierst mit Deinem Google+-Konto. Abmelden / Ändern )

Verbinde mit %s

The Rubric Theme. Bloggen Sie auf WordPress.com.

Folgen

Erhalte jeden neuen Beitrag in deinen Posteingang.

Schließe dich 28 Followern an

%d Bloggern gefällt das: