Here's how to do a message for a node selected by a user that was deleted.
First create a resource bundle for the component. Something like
Message.properties in the resources directory is good. Add a key for the
message:

    com.myfaces.tree2.ITEM_DELETED=The selected item no longer exists.

Optionally you can add a detail message:

    com.myfaces.tree2.ITEM_DELETED_detail=The selected item was deleted from
the tree. \
                        Please select another item.

In the component where you want to set the message, first get the resource
bundle for the application:

    Application application = context.getApplication();
    String bundleName = application.getMessageBundle();
    Locale locale =
FacesContext.getCurrentInstance().getViewRoot().getLocale();
    ResourceBundle rb = ResourceBundle.getBundle(bundleName, locale);

Then get the component's bundle (note I shorted the package path for
brevity):

    ResourceBundle crb =
        ResourceBundle.getBundle("com.myfaces...tree2.resource.Messages",
locale);

Always check the application bundle first for the message so the end user
can override the message with their own version.

    String msg = rb.getString("com.myfaces.tree2.ITEM_DELETED");
    String dmsg = rb.getString("com.myfaces.tree2.ITEM_DELETED_detail");
    if (msg == null) msg = crb.getString("com.myfaces.tree2.ITEM_DELETED");
    if (dmsg == null) dmsg =
crb.getString("com.myfaces.tree2.ITEM_DELETED_detail");

Create a FacesMessage and add it to the FacesContext message queue:

    FacesMessage fmsg = new FacesMessage.SEVERITY_INFO, msg, dmsg);
    FacesContext.getCurrentInstance().addMessage(tree2Id, fmsg);

The tree2Id param can be the id attribute of the tree2 tag to associate the
message with the tree.


Rob


----- Original Message ----- 
From: "Rob Decker" <[EMAIL PROTECTED]>
To: "MyFaces Discussion" <[email protected]>
Sent: Friday, April 01, 2005 10:20 PM
Subject: Re: tree2 deleted nodes


> I always set 3rd party jar logging to error and I wouldn't want to see
that.
> It seems more like debug if anything. The tree component created the id
> string it parses based on the TreeNode it got. If it isn't there when it
> looks for it later it's not the tree component's fault. You can log a
debug
> message so end user's who want to test their use of the component can turn
> on debug logging for it and see it.
>
> I'm doing a JSF crash course this weekend with the o'reilly book so if I
> find anything out about using a bundle to associate the message key for
the
> component I'll let you know.
>
>
> ----- Original Message ----- 
> From: "Sean Schofield" <[EMAIL PROTECTED]>
> To: "MyFaces Discussion" <[email protected]>
> Sent: Friday, April 01, 2005 9:27 PM
> Subject: Re: tree2 deleted nodes
>
>
> > > How about just FacesContext.addmessage("The selected item no longer
> > > exists."). Then the end user can display the message or not. Just
> because
> > > one branch or leaf was deleted doesn't mean the tree is gone (and even
> it it
> > > was that's a use case). The above is way to handle errors recommended
in
> the
> > > J2EE Tutorial JSF chapters.
> >
> > That might be a good option.  What about a message in addition to a
> > log statement.  This might be the kind of thing the developer would
> > care about as well (if its not expected.)
> >
> > > I have to read up on bundles but I'd think there was a way to add a
> message
> > > as a key so an end user can just put it in their bundle and get a
custom
> > > message out.
> >
> > I don't know much about it either.  But no better time to learn than
> > the present.  Also, I believe you can associate the message with a
> > specific component.
> >
> > > The reason that I encountered it is that I wrote example code that
> randomly
> > > generates the tree every time the page loads so I could test whether
or
> not
> > > the tree would survive in my actual application.
> >
> > Well that explains it.  I figured there was a logical explanation :-)
> >
> > > Rob
> >
> > sean
>

Reply via email to