You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by triswork <tr...@gmail.com> on 2009/03/16 19:21:02 UTC

Panel data interchange - concept validation

I have written some code that *seems* to work fine, but I am a little
concerned over whether my approach is (a) valid and (b) optimal. I am hoping
that some of the experienced Wicket programmers can provide me with some
feedback.

Basically, I have a page with four panels on it that is very similar to the
Gmail "contacts" page. The first panel displays a list of user groups. The
second displays a list of the users in the selected group. The third and
fourth contain contextual information and can be ignored for the purposes of
this discussion.

The basic use-case here is to click on an Ajaxified link in the one panel
(such as the user groups) and have the other panels update accordingly.

My approach looks something like this:

The Page class just instantiates the panels and wraps each of them in a
WebMarkupContainer with setOutputMarkupId(true); Nothing too interesting.

The UserPanel is reasonably straight-forward. The only thing worth
mentioning is that I hold a reference to userListModel so that I can ask it
to detach itself when I perform an update.

public class UserPanel extends Panel {
  @SpringBean
  private UserDao userDao;
  private UserGroup group; // A reference to the user group so I can extract
bits of info from it
  public IModel userListModel; // A reference to my model so that I can ask
it to update itself.

  public UserPanel(String id, final UserGroup group) {
    super(id);
    this.group = group;

    userListModel = new LoadableDetachableModel() {
      @Override
      protected Object load() {
        return userDao.findByGroup(UserPanel.this.group);
      }
    };
    ListView users = new ListView("users", userListModel) {
      // List the users here
    };

The last thing I add to this class is the setGroup(Group g) accessor method
that allows me to set the group when it is changed by the user.

Pretty simple really and I am reasonably confident that there isn't too much
wrong so far :)
The next class (the GroupPanel) is a bit more complicated and this is where
I feel I am on really shaky ground...

It starts of pretty much the same as UserPanel...

class GroupPanel extends Panel
{
  @SpringBean
  private GroupDao groupDao; 
  private Panel userPanel; // Reference to the user panel so that I can ask
it to update itself.

  IModel userGroupListModel = new LoadableDetachableModel()  {
    @Override
    protected Object load() {
      return userGroupDao.find(null); // Return all the groups
    }
};


Now for the hard part...

ListView groups = new ListView("groups", userGroupListModel) {
  @Override
  protected void populateItem(ListItem item) {
    final Group group = (Group)item.getModelObject();
    Link groupSelectorLink = new AjaxFallbackLink("groupSelector",
item.getModel()) {
       @Override
       public void onClick(AjaxRequestTarget target) {
         UserPanel panel = (UserPanel)GroupPanel.this.userPanel; // Really
ugly 
         panel.userListModel.detach();
         panel.setUserGroup(group);
         target.addComponent(container); // "container" refers to the WMC
set in the Page class
       }
    };
 

As I said earlier, this code seems to work fine, but I have a couple of
nagging worries:
1) Is is advisable/wise to pass object references around between panels. I
saw a post elsewhere on this list that mentioned it may be a bad idea
because the Serialization will break these references. It doesn't seem to,
but I am only a single user running on a development server at the moment. I
would hate for everything to break at a later stage.
If this *is* a bad practice, what is the correct way to do this?

2) Is manually calling the detach() method of the model the best way to get
it to refresh when the Ajaxified link is clicked on? It seems like a bit of
a bodge, but I can't think of any other way :(

3) I have passed references to my WMC containers in the constructors of my
Panels (not shown in the above code). This is really ugly and I am convinced
there must be a better way. I originally tried calling getParent() assuming
it must refer to the component above my Panel in the component hierarchy. As
it turns out, it doesn't. Can anyone give me a definitive answer on what
getParent() refers to? I couldn't figure it out from the API docs :(

If you got this far, thanks for reading though all this. I know it is a bit
long-winded :)
Any feedback will be *greatly* appreciated.

-- 
View this message in context: http://www.nabble.com/Panel-data-interchange---concept-validation-tp22541467p22541467.html
Sent from the Wicket - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Panel data interchange - concept validation

Posted by triswork <tr...@gmail.com>.
Well, after a reasonable nights sleep, I have figured out the answers to most
of my own questions. Here they are for anyone that comes along in the
future...


triswork wrote:
> 
> 2) Is manually calling the detach() method of the model the best way to
> get it to refresh when the Ajaxified link is clicked on? It seems like a
> bit of a bodge, but I can't think of any other way :(
> 
You don't have to call detach manually. The Ajax call is a new request and
Wicket handles the detach() and load() methods fine without any help.


triswork wrote:
> 
> 3) I have passed references to my WMC containers in the constructors of my
> Panels (not shown in the above code). This is really ugly and I am
> convinced there must be a better way. I originally tried calling
> getParent() assuming it must refer to the component above my Panel in the
> component hierarchy. As it turns out, it doesn't. Can anyone give me a
> definitive answer on what getParent() refers to? I couldn't figure it out
> from the API docs :(
> 
This question was the result of a typo. As it turns out, getParent() works
just the way I thought and, (ironically), code fragment in my OP worked...
The code in my IDE didn't :)


-- 
View this message in context: http://www.nabble.com/Panel-data-interchange---concept-validation-tp22541467p22553730.html
Sent from the Wicket - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org