Ah, I just had a sudden inspiration about your problem. I just
understood why you use an action _and_ an actionListener. The
actionListener, i.e. "t.setNodeSelected" is what has to be done for
every one of your nodes, the action, i.e."treeBacker.processFolder" is
what makes the difference for this peculiar node. Took me some time, but
I guess/hope I got it now ;).
On this basis one more time my suggestion to change you code:
- create a basic/super implementation that implements the interface
"ClickableBaseNode" as mentioned below where you do the selection of the
node
- create the other implementations by deriving from that super class
- thereby you only have one location for your code that is to be
processed when a node is clicked
- the backing bean for your tree remains the same as before although the
code of your method "processFolder" has moved to you node.
Did I answer all your questions? Do you approve the changes?
Good night,
Christopher
Christopher Cudennec schrieb:
Hi,
no, that's not what I meant. If the method of your actionListener
currently points to a backing bean method, that's fine. The approach
that I described in my earlier post is a bit more "object-centric".
The object that is manipulated (i.e. your tree node) knows how to
react to an event. My suggestion is to only use one single method,
preferrably the actionListener method. The code that I moved to
"MyBaseNode1" can stay in your backing bean, if your prefer that
solution. Anyway, if it is one of your requisites to have many
different actions that are to be processed when a node is clicked, you
could use the interface to generalize the behaviour and have better
reusablity. But maybe that's more than your application has to do
right now ;).
Cheers,
Christopher
kewldude schrieb:
Hi Chris,
If I get what you mean by this approach, does this mean my backing
bean for
the tree itself will be MyBaseNode1 object because it is from that
object
where i will point my actionListener, myBaseNode1.onClick, is that
right?
Christopher Cudennec wrote:
Hi!
as long as the code of method "setNodeSelected" does not depend on
having an instance of ActionEvent you could simple pass "null" as a
parameter. In my opinion you should do some refactoring because you
use "action" and "actionListener" at the same time. The "action"
method is basically used for navigation and that is not what you
want to do here. You probably just return "null", right? Why not
doing throwing away the action and doing something like:
interface ClickableBaseNode {
public void onClick(ActionEvent event);
}
class MyBaseNode1 extends TreeNodeBase implements ClickableBaseNode {
public void onClick(ActionEvent event) {
setNodeSelected(event);
ClickableBaseNode anotherNode = findAnotherNode();
anotherNode.onClick();
}
}
class MyBaseNode2 extends TreeNodeBase implements ClickableBaseNode {
public void onClick(ActionEvent event) {
setNodeSelected(event);
getTreeBacker().processFolder();
}
}
You just have to create proper implementations of
"ClickableBaseNode" that suit your needs.
Christopher
kewldude schrieb:
Thanks Christopher, I got the idea, but this is where I am really
struggling.
Together with executing the action associated with the node, I have to
change its styleclass to reflect that it was as if the node was
clicked.
To
achieve that using the normal way, my command link has this:
<a4j:commandLink id="a4jLink" styleClass="#{t.nodeSelected?
'documentSelected':'document'}" action="#{treeBacker.processFolder}"
actionListener="#{t.setNodeSelected}" reRender="a4jGroup" >
To do that programmatically, I think I have to invoke the
setNodeSelected
in
the backing bean, the problem now is the setNodeSelected is associated
with
an ActionEvent, I dont have an ActionEvent object because there was no
actual action that generates the event , I'm just simulating the
"event"
programmatically. Any more
hints?...
Christopher Cudennec wrote:
Hi there,
you could implement your own TreeNode adding a new method
"onClick" (or whatsoever) that can be called when you found the
match in your backing bean. Does that solve your problem? (Look
here for a start:
http://myfaces.apache.org/tomahawk/apidocs/org/apache/myfaces/custom/tree2/TreeNodeBase.html)
Cheers,
Christopher
kewldude schrieb:
Here goes the situation, I have a textbox that can search through
the
nodes
in the tree2 component. When there is a match, I need to expand the
specific
node. I can do that part no problem. But together with expanding
that
node,
the action associated to that node should also be executed (it
was as
if
that node was clicked), how can I do that? or is it possible to
do that
all?
any hints...thanks.