package org.gcube.portlets.user.accountingdashboard.client.application.mainarea.filter.scopetree; import java.util.ArrayList; import java.util.logging.Logger; import org.gcube.portlets.user.accountingdashboard.client.application.event.RequestReportEvent; import org.gcube.portlets.user.accountingdashboard.client.application.event.RequestReportEvent.RequestReportEventHandler; 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.SelectionChangeEvent; import com.google.gwt.view.client.SingleSelectionModel; import com.google.gwt.view.client.TreeViewModel; /** * * @author Giancarlo Panichi * */ public class ScopeTreeModel implements TreeViewModel { private static Logger logger = Logger.getLogger(""); private ListDataProvider dataProvider; private final SingleSelectionModel selectionModel = new SingleSelectionModel<>(); public ScopeTreeModel(ListDataProvider dataProvider, final RequestReportEventHandler handler) { this.dataProvider = dataProvider; selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() { @Override public void onSelectionChange(SelectionChangeEvent event) { @SuppressWarnings("rawtypes") SingleSelectionModel noSelectionModel = (SingleSelectionModel) event.getSource(); ScopeData scopeDataSelected = (ScopeData) noSelectionModel.getSelectedObject(); logger.fine("Selected: " + scopeDataSelected); RequestReportEvent requestEvent = new RequestReportEvent(scopeDataSelected); handler.onData(requestEvent); } }); } /** * 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, selectionModel, null); } 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, selectionModel, null); } /* * 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; } }