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

221 lines
6.9 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 TreeItem root;
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;
tree.setAnimationEnabled(true);
NodeItem rootNode = new NodeItem(null, "root", null, false, false, JSON_ROOT_PATH);
rootNode.setRoot(true);
root = new TreeItem(rootNode);
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 = createAndAddChild(listParent.get(0), profile.getSectionTitle(), 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 createAndAddChild(TreeItem parent, String nodeHTML, GeoNaFormCardModel geoNaFormCardModel, boolean canBeDuplicated,
boolean canBeDeleted, String jsonSectionFullPath) {
GWT.log("Creating and adding child to parent: " + parent.getText());
TreeItem newChild = new TreeItem(new NodeItem(parent, nodeHTML, geoNaFormCardModel, canBeDuplicated, canBeDeleted, jsonSectionFullPath));
parent.addItem(newChild);
fillItemParents(jsonSectionFullPath, newChild);
return newChild;
}
public TreeItem addChild(TreeItem parent, TreeItem item) {
GWT.log("Adding child "+item.getText()+" to parent: " + parent.getText());
parent.addItem(item);
NodeItem nodeItem = (NodeItem) item.getWidget();
String jsonSectionFullPath = nodeItem.getJsonSectionFullPath();
fillItemParents(jsonSectionFullPath, item);
return item;
}
private void fillItemParents(String jsonSectionFullPath, TreeItem newChild) {
// 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(newChild);
treeItemParents.put(jsonSectionFullPath, listNodeParents);
}
}
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 getRoot() {
return root;
}
public TreeItem getSelectItem() {
return tree.getSelectedItem();
}
public int getNodeIndex(TreeItem parent, TreeItem item) {
return parent.getChildIndex(item);
}
public String getItemTypeTitle() {
return itemTypeTitle;
}
public String getProfileID() {
return profileID;
}
public HashMap<String, List<TreeItem>> getTreeItemParents() {
return treeItemParents;
}
}