You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
accounting-dashboard/src/main/java/org/gcube/portlets/user/accountingdashboard/client/application/mainarea/filter/scopetree/ScopeTreeModel.java

97 lines
3.1 KiB
Java

package org.gcube.portlets.user.accountingdashboard.client.application.mainarea.filter.scopetree;
import java.util.ArrayList;
import org.gcube.portlets.user.accountingdashboard.shared.data.ScopeData;
import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.cell.client.Cell;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.TreeViewModel;
public class ScopeTreeModel implements TreeViewModel {
private ListDataProvider<ScopeData> dataProvider;
/**
* This selection model is shared across all leaf nodes. A selection model
* can also be shared across all nodes in the tree, or each set of child
* nodes can have its own instance. This gives you flexibility to determine
* how nodes are selected.
*/
//private final SingleSelectionModel<String> selectionModel = new SingleSelectionModel<String>();
public ScopeTreeModel(ListDataProvider<ScopeData> dataProvider) {
this.dataProvider=dataProvider;
}
/**
* Get the {@link NodeInfo} that provides the children of the specified
* value.
*/
@Override
public <T> NodeInfo<?> getNodeInfo(T value) {
if (value == null) {
// LEVEL 0.
// We passed null as the root value. Return the composers.
// Create a data provider that contains the list of composers.
// Create a cell to display a composer.
Cell<ScopeData> cell = new AbstractCell<ScopeData>() {
@Override
public void render(Context context, ScopeData value, SafeHtmlBuilder sb) {
if (value != null) {
sb.appendEscaped(value.getName());
}
}
};
// Return a node info that pairs the data provider and the cell.
return new DefaultNodeInfo<ScopeData>(dataProvider, cell);
} else if (value instanceof ScopeData) {
// LEVEL 1.
// We want the children of the composer. Return the playlists.
ListDataProvider<ScopeData> dataProvider = new ListDataProvider<ScopeData>(
((ScopeData) value).getChildren());
Cell<ScopeData> cell = new AbstractCell<ScopeData>() {
@Override
public void render(Context context, ScopeData value, SafeHtmlBuilder sb) {
if (value != null) {
sb.appendEscaped(value.getName());
}
}
};
return new DefaultNodeInfo<ScopeData>(dataProvider, cell);
}
/*
else if (value instanceof ScopeData) {
// LEVEL 2 - LEAF.
// We want the children of the playlist. Return the songs.
ListDataProvider<String> dataProvider = new ListDataProvider<String>(((ScopeData) value).geSongs());
// Use the shared selection model.
return new DefaultNodeInfo<String>(dataProvider, new TextCell(), selectionModel, null);
}*/
return null;
}
/**
* Check if the specified value represents a leaf node. Leaf nodes cannot be
* opened.
*/
@Override
public boolean isLeaf(Object value) {
// The leaf nodes are the songs, which are Strings.
if (value != null && value instanceof ScopeData) {
ArrayList<ScopeData> childrens = ((ScopeData) value).getChildren();
if (childrens == null || childrens.isEmpty()) {
return true;
} else {
return false;
}
}
return false;
}
}