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 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 selectionModel = new SingleSelectionModel(); public ScopeTreeModel(ListDataProvider dataProvider) { this.dataProvider=dataProvider; } /** * Get the {@link NodeInfo} that provides the children of the specified * value. */ @Override public 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 cell = new AbstractCell() { @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(dataProvider, cell); } else if (value instanceof ScopeData) { // LEVEL 1. // We want the children of the composer. Return the playlists. ListDataProvider dataProvider = new ListDataProvider( ((ScopeData) value).getChildren()); Cell cell = new AbstractCell() { @Override public void render(Context context, ScopeData value, SafeHtmlBuilder sb) { if (value != null) { sb.appendEscaped(value.getName()); } } }; return new DefaultNodeInfo(dataProvider, cell); } /* else if (value instanceof ScopeData) { // LEVEL 2 - LEAF. // We want the children of the playlist. Return the songs. ListDataProvider dataProvider = new ListDataProvider(((ScopeData) value).geSongs()); // Use the shared selection model. return new DefaultNodeInfo(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 childrens = ((ScopeData) value).getChildren(); if (childrens == null || childrens.isEmpty()) { return true; } else { return false; } } return false; } }