문제

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.

도움이 되었습니까?

해결책

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)
        }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top