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

305 lines
8.1 KiB
Java
Raw Normal View History

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;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.TreeItem;
2022-07-08 11:44:48 +02:00
/**
* The Class TreeItemPanel.
*
* @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it
*
* Jul 8, 2022
*/
public class TreeItemPanel {
2022-07-04 16:34:15 +02:00
private static final String JSON_ROOT_PATH = "$.";
private Tree tree = new Tree();
2022-07-08 11:44:48 +02:00
private final 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-08 11:44:48 +02:00
/**
* Instantiates a new tree item panel.
*
* @param profileID the profile ID
* @param itemTypeTitle the item type title
* @param gnaCardsModels the gna cards models
* @param appManagerBus the app manager bus
*/
2022-08-08 09:54:49 +02:00
// Create a tree
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
2022-07-08 11:44:48 +02:00
NodeItem rootNode = new NodeItem(null, itemTypeTitle, null, false, false, null);
2022-07-07 12:36:08 +02:00
rootNode.setRoot(true);
root = new TreeItem(rootNode);
2022-07-05 15:51:43 +02:00
treeItemParents.put(JSON_ROOT_PATH, Arrays.asList(root));
2022-07-04 16:34:15 +02:00
for (GeoNaFormCardModel geoNaFormCardModel : gnaCardsModels) {
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
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
2022-07-08 16:54:52 +02:00
GWT.log("TreeItem selected: " + nodeItem);
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-08 11:44:48 +02:00
/**
* Gets the JSON full path from profile.
*
* @param profile the profile
* @return the JSON full path from profile
*/
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-08 11:44:48 +02:00
/**
* Creates the and add child.
*
* @param parent the parent
* @param nodeHTML the node HTML
* @param geoNaFormCardModel the geo na form card model
* @param canBeDuplicated the can be duplicated
* @param canBeDeleted the can be deleted
* @param jsonSectionFullPath the json section full path
* @return the tree item
*/
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-08-08 09:54:49 +02:00
GWT.log("Creating and adding child to parent: " + parent.getText() +", card is"+geoNaFormCardModel);
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;
}
2022-07-08 11:44:48 +02:00
/**
* Adds the child.
*
* @param parent the parent
* @param nextSiblingIndex the index where the item will be inserted
* @param item the item
* @return the tree item
*/
public TreeItem addChild(TreeItem parent, int nextSiblingIndex, TreeItem item) {
2022-07-06 17:12:55 +02:00
GWT.log("Adding child "+item.getText()+" to parent: " + parent.getText());
2022-07-08 11:44:48 +02:00
if(nextSiblingIndex>=0) {
parent.insertItem(nextSiblingIndex, item);
}
else {
parent.addItem(item);
}
2022-07-06 17:12:55 +02:00
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;
}
2022-07-08 11:44:48 +02:00
/**
* Fill item parents.
*
* @param jsonSectionFullPath the json section full path
* @param newChild the new child
*/
2022-07-06 17:12:55 +02:00
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-06 11:06:35 +02:00
2022-07-08 11:44:48 +02:00
/**
* Removes the child.
*
* @param parent the parent
* @param item the item
* @param jsonSectionFullPath the json section full path
*/
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
}
2022-07-08 11:44:48 +02:00
/**
* Count node for full path.
*
* @param jsonSectionFullPath the json section full path
* @return the int
*/
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-08 11:44:48 +02:00
/**
* Gets the tree.
*
* @return the tree
*/
public Tree getTree() {
return tree;
}
2022-07-08 11:44:48 +02:00
/**
* Gets the root.
*
* @return the root
*/
2022-07-06 17:12:55 +02:00
public TreeItem getRoot() {
return root;
}
2022-07-08 11:44:48 +02:00
/**
* Gets the select item.
*
* @return the select item
*/
2022-07-05 15:51:43 +02:00
public TreeItem getSelectItem() {
return tree.getSelectedItem();
}
2022-07-06 11:06:35 +02:00
2022-07-08 11:44:48 +02:00
/**
* Gets the node index.
*
* @param parent the parent
* @param item the item
* @return the node index
*/
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-08 11:44:48 +02:00
/**
* Gets the item type title.
*
* @return the item type title
*/
2022-07-06 17:12:55 +02:00
public String getItemTypeTitle() {
return itemTypeTitle;
}
2022-07-08 11:44:48 +02:00
/**
* Gets the profile ID.
*
* @return the profile ID
*/
2022-07-06 17:12:55 +02:00
public String getProfileID() {
return profileID;
}
2022-07-08 11:44:48 +02:00
/**
* Gets the tree item parents.
*
* @return the tree item parents
*/
2022-07-06 17:12:55 +02:00
public HashMap<String, List<TreeItem>> getTreeItemParents() {
return treeItemParents;
}
2022-07-07 18:21:34 +02:00
2022-07-08 11:44:48 +02:00
/**
* Select item.
*
* @param item the item
*/
2022-07-07 18:21:34 +02:00
public void selectItem(TreeItem item){
item.setSelected(true);
}
2022-07-06 17:12:55 +02:00
}