/** * */ package org.gcube.portlets.user.performfishanalytics.client.view; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.gcube.portlets.user.performfishanalytics.client.controllers.PerformFishAnalyticsController; import org.gcube.portlets.user.performfishanalytics.client.event.SelectedKPIEvent; import org.gcube.portlets.user.performfishanalytics.client.view.util.ExtendedCheckboxCell; import org.gcube.portlets.user.performfishanalytics.shared.KPI; import org.gcube.portlets.user.performfishanalytics.shared.PopulationType; import com.google.gwt.cell.client.AbstractCell; import com.google.gwt.cell.client.Cell; import com.google.gwt.cell.client.CompositeCell; import com.google.gwt.cell.client.FieldUpdater; import com.google.gwt.cell.client.HasCell; import com.google.gwt.core.shared.GWT; import com.google.gwt.dom.client.Element; import com.google.gwt.safehtml.shared.SafeHtmlBuilder; import com.google.gwt.view.client.ListDataProvider; import com.google.gwt.view.client.MultiSelectionModel; import com.google.gwt.view.client.SelectionChangeEvent; import com.google.gwt.view.client.TreeViewModel; /** * The model that defines the nodes in the tree. * * @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it) * Feb 28, 2019 */ public class CopyOfCustomTreeModel implements TreeViewModel { // private final List kpis = new // ArrayList(); /** * 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 ListDataProvider populationTypeProvider = new ListDataProvider(); private ListDataProvider kpiProvider = new ListDataProvider(); private MultiSelectionModel multiSelectionModel = new MultiSelectionModel(kpiProvider); private List> listKPIAdded = new ArrayList>(); private Map mapCheckBoxCell = new HashMap(); //private List> hasCells; //private ListDataProvider kpiProvider; //private Cell kpiCell; // private ListDataProvider dataProvider = new ListDataProvider(); /** * Instantiates a new custom tree model. */ public CopyOfCustomTreeModel() { //init(); multiSelectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() { public void onSelectionChange(SelectionChangeEvent event) { GWT.log("Selected: " + multiSelectionModel.getSelectedSet()); } }); } /** * Resync. * * @param selectedKPI the selected kpi */ public void resync(List selectedKPI){ for (KPI kpi : selectedKPI) { ExtendedCheckboxCell checkBoxCell = mapCheckBoxCell.get(kpi.getId()); GWT.log("Checkbox is: "+checkBoxCell); if(checkBoxCell!=null){ GWT.log("Resetting checked"); checkBoxCell.setChecked(true); } } } /** * Builds the kpi cell. * * @return the composite cell */ public CompositeCell buildKPICell() { // Construct a composite cell for contacts that includes a checkbox. ArrayList> hasCells = new ArrayList>(); HasCell checkBoxCell = new HasCell() { private ExtendedCheckboxCell cell = new ExtendedCheckboxCell(true, true); public Cell getCell() { return cell; } public FieldUpdater getFieldUpdater() { return new FieldUpdater() { @Override public void update(int index, KPI object, Boolean value) { // TODO Auto-generated method stub GWT.log("Checked: "+object.getName() + " value: "+value); PerformFishAnalyticsController.eventBus.fireEvent(new SelectedKPIEvent(null, object, value)); if(value.booleanValue()) mapCheckBoxCell.put(object.getId(), cell); else mapCheckBoxCell.remove(object.getId()); // cell.set(value); // dataGrid.redraw(); } }; } public Boolean getValue(KPI object) { return multiSelectionModel.isSelected(object); } }; hasCells.add(checkBoxCell); hasCells.add(new HasCell() { private KPICell cell = new KPICell(null); public Cell getCell() { return cell; } public FieldUpdater getFieldUpdater() { return null; } public KPI getValue(KPI object) { return object; } }); CompositeCell kpiCell = new CompositeCell(hasCells) { @Override public void render(Context context, KPI value, SafeHtmlBuilder sb) { if(value.isLeaf()){ sb.appendHtmlConstant(""); super.render(context, value, sb); sb.appendHtmlConstant("
"); }else{ sb.appendHtmlConstant(""); sb.appendHtmlConstant(value.getName()); sb.appendHtmlConstant("
"); } } @Override protected Element getContainerElement(Element parent) { // Return the first TR element in the table. if(parent!=null){ if(parent.getFirstChildElement()!=null){ if(parent.getFirstChildElement().getFirstChildElement()!=null){ return parent.getFirstChildElement().getFirstChildElement().getFirstChildElement(); } } } return null; } @Override protected void render( Context context, KPI value, SafeHtmlBuilder sb, HasCell hasCell) { Cell cell = hasCell.getCell(); sb.appendHtmlConstant(""); cell.render(context, hasCell.getValue(value), sb); sb.appendHtmlConstant(""); } }; return kpiCell; } /** * Sets the new batch type. * * @param populationType the new new batch type */ public void setNewBatchType(PopulationType populationType) { //GWT.log("Displayng KPI for: " + populationType.toString()); if (this.populationTypeProvider != null) { //removing all data from base data provider this.populationTypeProvider.getList().clear(); this.populationTypeProvider.getList().addAll(populationType.getListKPI()); } } /** * Get the {@link NodeInfo} that provides the children of the specified * value. * * @param the generic type * @param value the value * @return the node info */ public NodeInfo getNodeInfo(T value) { GWT.log("Get Node Info fired: " + value); if (value == null) { // LEVEL 0. // We passed null as the root value. Create a fake root KPIs PopulationType rootKPI = new PopulationType("", "KPIs", "", "", null); rootKPI.setListKPI(rootKPI.getListKPI()); ListDataProvider rootProvider = new ListDataProvider(); rootProvider.getList().add(rootKPI); Cell cell = new AbstractCell() { @Override public void render( com.google.gwt.cell.client.Cell.Context context, PopulationType value, SafeHtmlBuilder sb) { GWT.log("Rendering Root: " + value + " KPIs: " + value.getListKPI()); // sb.appendHtmlConstant(" ROOT "); sb.appendEscaped(value.getName()); } }; // Return a node info that pairs the data provider and the cell. return new DefaultNodeInfo(rootProvider, cell); } else if (value instanceof PopulationType) { // LEVEL 1. // We want the children of PopulationType. Getting its list of KPI. // List listOfKpi = ((PopulationType) value).getListKPI(); GWT.log("PopulationType listOfKpi: " + populationTypeProvider.getList()); //return new DefaultNodeInfo(populationTypeProvider, kpiCell, multiSelectionModel, null); CompositeCell kpiCell = buildKPICell(); listKPIAdded.add(kpiCell); return new DefaultNodeInfo(populationTypeProvider, kpiCell); } else if (value instanceof KPI) { // LEVEL 2 - LEAF. // We want the children of the KPI. Return them. List listOfKpi = ((KPI) value).getListKPI(); GWT.log("KPI type listOfKpi: " + listOfKpi.toString()); ListDataProvider localProvider = new ListDataProvider(listOfKpi); kpiProvider.getList().addAll(listOfKpi); // kpiProvider. // multiSelectionModel. CompositeCell kpiCell = buildKPICell(); listKPIAdded.add(kpiCell); return new DefaultNodeInfo(localProvider, kpiCell); } return null; } /** * Check if the specified value represents a leaf node. Leaf nodes cannot be * opened. * * @param value the value * @return true, if is leaf */ public boolean isLeaf(Object value) { if(value==null) return false; // The leaf nodes are the songs, which are Strings. if (value instanceof KPI) { KPI toKPI = (KPI) value; return toKPI.isLeaf(); } return false; } }