geoportal-data-entry-app/src/main/java/org/gcube/portlets/user/geoportaldataentry/client/ui/tree/TreeItemPanel.java

183 lines
6.0 KiB
Java

package org.gcube.portlets.user.geoportaldataentry.client.ui.tree;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import org.gcube.application.geoportalcommon.shared.geoportal.config.GcubeProfileDV;
import org.gcube.portlets.user.geoportaldataentry.client.events.TreeItemEvent;
import org.gcube.portlets.user.geoportaldataentry.client.events.TreeItemEvent.ACTION;
import org.gcube.portlets.user.geoportaldataentry.client.ui.card.GeoNaFormCardModel;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.TreeItem;
public class TreeItemPanel {
private static final String JSON_ROOT_PATH = "$.";
private Tree tree = new Tree();
private HandlerManager appManagerBus;
private HashMap<String, List<TreeItem>> treeItemParents = new HashMap<String, List<TreeItem>>();
private String itemTypeTitle;
private String profileID;
// Create a tree with a few items in it.
public TreeItemPanel(String profileID, String itemTypeTitle, Collection<GeoNaFormCardModel> gnaCardsModels,
HandlerManager appManagerBus) {
this.appManagerBus = appManagerBus;
this.itemTypeTitle = itemTypeTitle;
this.profileID = profileID;
TreeItem root = new TreeItem();
root.setHTML(itemTypeTitle);
// TreeMap<String, List<TreeItem>> mapParentSections = new TreeMap<String,
// List<TreeItem>>();
treeItemParents.put(JSON_ROOT_PATH, Arrays.asList(root));
// mapParentSections.put(JSON_ROOT_PATH, Arrays.asList(root));
// for (GeoNaFormCardModel geoNaFormCardModel : gnaCardsModels) {
//
// GcubeProfileDV profile = geoNaFormCardModel.getGcubeProfile();
// String parentName = profile.getParentName();
//
// if (profile.getSectionName().compareTo("relazioneScavo") == 0) {
// profile.setParentName(JSON_ROOT_PATH + "abstractRelazione");
// }
//
// if (profile.getSectionName().compareTo("posizionamentoScavo") == 0) {
// profile.setParentName(JSON_ROOT_PATH + "immaginiRappresentative");
// }
//
// if (profile.getSectionName().compareTo("pianteFineScavo") == 0) {
// profile.setParentName(JSON_ROOT_PATH + "immaginiRappresentative.posizionamentoScavo");
// }
//
// GWT.log(" print tree parentName: " + parentName + " name: " + profile.getSectionName() + " title: "
// + profile.getSectionTitle());
// }
for (GeoNaFormCardModel geoNaFormCardModel : gnaCardsModels) {
GcubeProfileDV profile = geoNaFormCardModel.getGcubeProfile();
String parentName = profile.getParentName();
String jsonSectionFullPath = getJSONFullPathFromProfile(profile);
if (parentName == null || parentName.isEmpty()) {
parentName = JSON_ROOT_PATH;
}
GWT.log("tree getting jsonSectionFullPath: " + jsonSectionFullPath + " in " + treeItemParents.keySet());
List<TreeItem> listParent = treeItemParents.get(parentName);
GWT.log("tree jsonSection: " + jsonSectionFullPath + " parent " + parentName + " name: "
+ profile.getSectionName() + " title: " + profile.getSectionTitle());
boolean canBeDuplicated = geoNaFormCardModel.getFormCard().isInternalRepeatibleForm();
TreeItem child = addChild(listParent.get(0), geoNaFormCardModel, canBeDuplicated, false,
jsonSectionFullPath);
}
tree.addItem(root);
tree.addSelectionHandler(new SelectionHandler<TreeItem>() {
@Override
public void onSelection(SelectionEvent<TreeItem> event) {
TreeItem treeItem = event.getSelectedItem();
NodeItem nodeItem = (NodeItem) treeItem.getWidget();
GWT.log("Selected: " + nodeItem.getGeoNaFormCardModel().getGcubeProfile());
appManagerBus.fireEvent(new TreeItemEvent(treeItem, ACTION.SELECTED));
}
});
// expand the element
root.setState(true);
}
private String getJSONFullPathFromProfile(GcubeProfileDV profile) {
String parentName = profile.getParentName();
GWT.log("finding parentName: " + parentName + " in " + treeItemParents.keySet());
String jsonSectionFullPath = null;
if (parentName == null || parentName.isEmpty()) {
GWT.log("tree parentName is null: " + parentName + " in " + treeItemParents.keySet());
parentName = JSON_ROOT_PATH;
jsonSectionFullPath = profile.getSectionName();
} else {
jsonSectionFullPath = parentName.endsWith(".") ? parentName + profile.getSectionName()
: parentName + "." + profile.getSectionName();
}
return jsonSectionFullPath;
}
public TreeItem addChild(TreeItem parent, GeoNaFormCardModel geoNaFormCardModel, boolean canBeDuplicated,
boolean canBeDeleted, String jsonSectionFullPath) {
GWT.log("Adding child to parent: " + parent.getText());
TreeItem child = new TreeItem(
new NodeItem(parent, geoNaFormCardModel, canBeDuplicated, canBeDeleted, jsonSectionFullPath));
parent.addItem(child);
// if the path section is not the root
if (jsonSectionFullPath.compareTo(JSON_ROOT_PATH) != 0) {
List<TreeItem> listNodeParents = treeItemParents.get(jsonSectionFullPath);
if (listNodeParents == null) {
listNodeParents = new ArrayList<TreeItem>();
}
listNodeParents.add(child);
treeItemParents.put(jsonSectionFullPath, listNodeParents);
}
return child;
}
public void removeChild(TreeItem parent, TreeItem item, String jsonSectionFullPath) {
GWT.log("Removing child to parent: " + parent.getText());
List<TreeItem> nodes = treeItemParents.get(jsonSectionFullPath);
if (nodes != null) {
nodes.remove(item);
treeItemParents.put(jsonSectionFullPath, nodes);
}
parent.removeItem(item);
}
public int countNodeForFullPath(String jsonSectionFullPath) {
List<TreeItem> nodes = treeItemParents.get(jsonSectionFullPath);
if (nodes != null)
return nodes.size();
return 0;
}
public Tree getTree() {
return tree;
}
public TreeItem getSelectItem() {
return tree.getSelectedItem();
}
public int getNodeIndex(TreeItem parent, TreeItem item) {
return parent.getChildIndex(item);
}
}