0

I'm trying to use the GWT CellTree to display a heterogeneous, hierarchical data model. I need to be able to a single selection, but be able to select Parent nodes as well as child nodes. For example, if you look at GWT's own example, you'll see that they only provide one selection model for the leave nodes.

I tried to extend their example by providing one selection model for all nodes. However, that seems impossible. So what I ended up with where 3 SelectionModels one for each node type (Composer, PlayList, Song).

What am I missing?

Thanks in advance.

hba
  • 7,406
  • 10
  • 63
  • 105

1 Answers1

1

In the getNodeInfo function of your TreeViewModel you have to pass the selectionModel to each new DefaultNodeInfo instance at each level.

return new DefaultNodeInfo<MyDTO>(dataProvider,new MyDTOCell(),selectionModel,null);

and then in the SelectionChangeEventHandler you have do something like this:

selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
    @Override
    public void onSelectionChange(SelectionChangeEvent event) {
        Object object = selectionModel.getSelectedObject();
        if (object instanceof MyRootDTO)
        {
              // DO SOMETHING with root level selected node
        }
        else if (object instanceof MySecondLevelDTO) {
        // DO SOMETHING WITH 2. level selected node
        }
        // additional levels
});

Update:
In order to get around the typing problem, you can define an abstract base class which is extended by all your DTO's.

public abstract class BaseModel  {

    public static final ProvidesKey<BaseModel> KEY_PROVIDER = new ProvidesKey<BaseModel>() {
      public Object getKey(BaseModel item) {
        return item == null ? null : item.getId();
      }
    };

    public abstract Object getId();
}

In your DTO's you extend the BaseModel and implement the abstract getId() method:

public class MyDTO extends BaseModel {
        @Override
        public Object getId() {
             //return unique ID (i.e. MyDTO_1)
        }
}
Ümit
  • 17,379
  • 7
  • 55
  • 74
  • Thanks for the reply...I guess I'm missing something because, I thought that the selectionModel is strongly typed. So for the root node you'd have to pass in a selectionModel and for second level you have to pass in selectionModel. How would I get around that? Thanks in advance. – hba Sep 28 '11 at 18:02
  • That's right. However as a workaround you could create an abstract Base class (BaseModel) which implements the ProviderKey interface. All your DTO's extend this BaseModel class. (see update on my answer) – Ümit Sep 28 '11 at 23:35
  • I ended up with a different approach. I created a SelectionModelController that houses 3 selectionModels. After a few days of playing around with this widget, I realized that it doesn't need my other needs so I wrote my own. Thanks again. – hba Oct 08 '11 at 23:55