2022-07-01 15:39:27 +02:00
|
|
|
package org.gcube.portlets.user.geoportaldataentry.client.ui.tree;
|
|
|
|
|
2022-07-05 15:51:43 +02:00
|
|
|
import java.util.ArrayList;
|
2022-07-04 16:34:15 +02:00
|
|
|
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;
|
2022-07-05 15:51:43 +02:00
|
|
|
import org.gcube.portlets.user.geoportaldataentry.client.events.TreeItemEvent;
|
|
|
|
import org.gcube.portlets.user.geoportaldataentry.client.events.TreeItemEvent.ACTION;
|
2022-07-04 16:34:15 +02:00
|
|
|
import org.gcube.portlets.user.geoportaldataentry.client.ui.card.GeoNaFormCardModel;
|
|
|
|
|
|
|
|
import com.google.gwt.core.client.GWT;
|
2022-07-05 15:51:43 +02:00
|
|
|
import com.google.gwt.event.logical.shared.SelectionEvent;
|
|
|
|
import com.google.gwt.event.logical.shared.SelectionHandler;
|
|
|
|
import com.google.gwt.event.shared.HandlerManager;
|
2022-07-01 15:39:27 +02:00
|
|
|
import com.google.gwt.user.client.ui.Tree;
|
|
|
|
import com.google.gwt.user.client.ui.TreeItem;
|
|
|
|
|
|
|
|
public class TreeItemPanel {
|
|
|
|
|
2022-07-04 16:34:15 +02:00
|
|
|
private static final String JSON_ROOT_PATH = "$.";
|
2022-07-01 15:39:27 +02:00
|
|
|
private Tree tree = new Tree();
|
2022-07-07 12:36:08 +02:00
|
|
|
private TreeItem root;
|
2022-07-05 15:51:43 +02:00
|
|
|
private HandlerManager appManagerBus;
|
|
|
|
private HashMap<String, List<TreeItem>> treeItemParents = new HashMap<String, List<TreeItem>>();
|
2022-07-06 11:06:35 +02:00
|
|
|
private String itemTypeTitle;
|
|
|
|
private String profileID;
|
2022-07-01 15:39:27 +02:00
|
|
|
|
2022-07-04 16:34:15 +02:00
|
|
|
// Create a tree with a few items in it.
|
2022-07-06 11:06:35 +02:00
|
|
|
public TreeItemPanel(String profileID, String itemTypeTitle, Collection<GeoNaFormCardModel> gnaCardsModels,
|
|
|
|
HandlerManager appManagerBus) {
|
2022-07-05 15:51:43 +02:00
|
|
|
this.appManagerBus = appManagerBus;
|
2022-07-06 11:06:35 +02:00
|
|
|
this.itemTypeTitle = itemTypeTitle;
|
|
|
|
this.profileID = profileID;
|
2022-07-06 17:12:55 +02:00
|
|
|
tree.setAnimationEnabled(true);
|
2022-07-07 12:36:08 +02:00
|
|
|
|
|
|
|
NodeItem rootNode = new NodeItem(null, "root", null, false, false, JSON_ROOT_PATH);
|
|
|
|
rootNode.setRoot(true);
|
|
|
|
root = new TreeItem(rootNode);
|
2022-07-06 11:06:35 +02:00
|
|
|
root.setHTML(itemTypeTitle);
|
2022-07-07 12:36:08 +02:00
|
|
|
|
2022-07-04 16:34:15 +02:00
|
|
|
|
|
|
|
// TreeMap<String, List<TreeItem>> mapParentSections = new TreeMap<String,
|
|
|
|
// List<TreeItem>>();
|
2022-07-05 15:51:43 +02:00
|
|
|
|
|
|
|
treeItemParents.put(JSON_ROOT_PATH, Arrays.asList(root));
|
2022-07-04 16:34:15 +02:00
|
|
|
// mapParentSections.put(JSON_ROOT_PATH, Arrays.asList(root));
|
|
|
|
|
2022-07-06 17:12:55 +02:00
|
|
|
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());
|
|
|
|
}
|
2022-07-04 16:34:15 +02:00
|
|
|
|
|
|
|
for (GeoNaFormCardModel geoNaFormCardModel : gnaCardsModels) {
|
2022-07-01 15:39:27 +02:00
|
|
|
|
2022-07-04 16:34:15 +02:00
|
|
|
GcubeProfileDV profile = geoNaFormCardModel.getGcubeProfile();
|
|
|
|
String parentName = profile.getParentName();
|
2022-07-05 15:51:43 +02:00
|
|
|
String jsonSectionFullPath = getJSONFullPathFromProfile(profile);
|
2022-07-06 11:06:35 +02:00
|
|
|
|
2022-07-04 16:34:15 +02:00
|
|
|
if (parentName == null || parentName.isEmpty()) {
|
|
|
|
parentName = JSON_ROOT_PATH;
|
|
|
|
}
|
|
|
|
|
|
|
|
GWT.log("tree getting jsonSectionFullPath: " + jsonSectionFullPath + " in " + treeItemParents.keySet());
|
2022-07-05 15:51:43 +02:00
|
|
|
List<TreeItem> listParent = treeItemParents.get(parentName);
|
2022-07-04 16:34:15 +02:00
|
|
|
GWT.log("tree jsonSection: " + jsonSectionFullPath + " parent " + parentName + " name: "
|
|
|
|
+ profile.getSectionName() + " title: " + profile.getSectionTitle());
|
|
|
|
|
2022-07-06 11:06:35 +02:00
|
|
|
boolean canBeDuplicated = geoNaFormCardModel.getFormCard().isInternalRepeatibleForm();
|
2022-07-07 12:36:08 +02:00
|
|
|
TreeItem child = createAndAddChild(listParent.get(0), profile.getSectionTitle(), geoNaFormCardModel, canBeDuplicated, false,
|
2022-07-06 11:06:35 +02:00
|
|
|
jsonSectionFullPath);
|
|
|
|
|
2022-07-04 16:34:15 +02:00
|
|
|
}
|
2022-07-06 17:12:55 +02:00
|
|
|
|
2022-07-01 15:39:27 +02:00
|
|
|
tree.addItem(root);
|
2022-07-05 15:51:43 +02:00
|
|
|
|
|
|
|
tree.addSelectionHandler(new SelectionHandler<TreeItem>() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onSelection(SelectionEvent<TreeItem> event) {
|
2022-07-06 11:06:35 +02:00
|
|
|
TreeItem treeItem = event.getSelectedItem();
|
|
|
|
|
|
|
|
NodeItem nodeItem = (NodeItem) treeItem.getWidget();
|
2022-07-05 15:51:43 +02:00
|
|
|
|
|
|
|
GWT.log("Selected: " + nodeItem.getGeoNaFormCardModel().getGcubeProfile());
|
2022-07-06 11:06:35 +02:00
|
|
|
appManagerBus.fireEvent(new TreeItemEvent(treeItem, ACTION.SELECTED));
|
2022-07-05 15:51:43 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// expand the element
|
|
|
|
root.setState(true);
|
2022-07-06 17:12:55 +02:00
|
|
|
|
2022-07-01 15:39:27 +02:00
|
|
|
}
|
|
|
|
|
2022-07-05 15:51:43 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-07-07 12:36:08 +02:00
|
|
|
public TreeItem createAndAddChild(TreeItem parent, String nodeHTML, GeoNaFormCardModel geoNaFormCardModel, boolean canBeDuplicated,
|
2022-07-06 11:06:35 +02:00
|
|
|
boolean canBeDeleted, String jsonSectionFullPath) {
|
2022-07-06 17:12:55 +02:00
|
|
|
GWT.log("Creating and adding child to parent: " + parent.getText());
|
2022-07-07 12:36:08 +02:00
|
|
|
TreeItem newChild = new TreeItem(new NodeItem(parent, nodeHTML, geoNaFormCardModel, canBeDuplicated, canBeDeleted, jsonSectionFullPath));
|
2022-07-06 17:12:55 +02:00
|
|
|
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);
|
2022-07-06 11:06:35 +02:00
|
|
|
|
2022-07-06 17:12:55 +02:00
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void fillItemParents(String jsonSectionFullPath, TreeItem newChild) {
|
2022-07-05 15:51:43 +02:00
|
|
|
// if the path section is not the root
|
|
|
|
if (jsonSectionFullPath.compareTo(JSON_ROOT_PATH) != 0) {
|
2022-07-06 11:06:35 +02:00
|
|
|
|
2022-07-05 15:51:43 +02:00
|
|
|
List<TreeItem> listNodeParents = treeItemParents.get(jsonSectionFullPath);
|
2022-07-06 11:06:35 +02:00
|
|
|
|
|
|
|
if (listNodeParents == null) {
|
2022-07-05 15:51:43 +02:00
|
|
|
listNodeParents = new ArrayList<TreeItem>();
|
|
|
|
}
|
2022-07-06 11:06:35 +02:00
|
|
|
|
2022-07-06 17:12:55 +02:00
|
|
|
listNodeParents.add(newChild);
|
2022-07-06 11:06:35 +02:00
|
|
|
|
2022-07-05 15:51:43 +02:00
|
|
|
treeItemParents.put(jsonSectionFullPath, listNodeParents);
|
|
|
|
}
|
2022-07-01 15:39:27 +02:00
|
|
|
}
|
2022-07-06 11:06:35 +02:00
|
|
|
|
|
|
|
public void removeChild(TreeItem parent, TreeItem item, String jsonSectionFullPath) {
|
|
|
|
GWT.log("Removing child to parent: " + parent.getText());
|
2022-07-05 15:51:43 +02:00
|
|
|
List<TreeItem> nodes = treeItemParents.get(jsonSectionFullPath);
|
2022-07-06 11:06:35 +02:00
|
|
|
|
|
|
|
if (nodes != null) {
|
|
|
|
nodes.remove(item);
|
|
|
|
treeItemParents.put(jsonSectionFullPath, nodes);
|
|
|
|
}
|
|
|
|
|
|
|
|
parent.removeItem(item);
|
2022-07-06 17:12:55 +02:00
|
|
|
|
2022-07-06 11:06:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public int countNodeForFullPath(String jsonSectionFullPath) {
|
|
|
|
List<TreeItem> nodes = treeItemParents.get(jsonSectionFullPath);
|
|
|
|
|
|
|
|
if (nodes != null)
|
2022-07-05 15:51:43 +02:00
|
|
|
return nodes.size();
|
2022-07-06 11:06:35 +02:00
|
|
|
|
2022-07-05 15:51:43 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2022-07-01 15:39:27 +02:00
|
|
|
|
|
|
|
public Tree getTree() {
|
|
|
|
return tree;
|
|
|
|
}
|
|
|
|
|
2022-07-06 17:12:55 +02:00
|
|
|
public TreeItem getRoot() {
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
2022-07-05 15:51:43 +02:00
|
|
|
public TreeItem getSelectItem() {
|
|
|
|
return tree.getSelectedItem();
|
|
|
|
}
|
2022-07-06 11:06:35 +02:00
|
|
|
|
|
|
|
public int getNodeIndex(TreeItem parent, TreeItem item) {
|
2022-07-05 15:51:43 +02:00
|
|
|
return parent.getChildIndex(item);
|
|
|
|
}
|
|
|
|
|
2022-07-06 17:12:55 +02:00
|
|
|
public String getItemTypeTitle() {
|
|
|
|
return itemTypeTitle;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getProfileID() {
|
|
|
|
return profileID;
|
|
|
|
}
|
|
|
|
|
|
|
|
public HashMap<String, List<TreeItem>> getTreeItemParents() {
|
|
|
|
return treeItemParents;
|
|
|
|
}
|
|
|
|
|
2022-07-01 15:39:27 +02:00
|
|
|
}
|