From 5a5178c45979b74cdbd9b4e6c0010348807e0862 Mon Sep 17 00:00:00 2001 From: "francesco.mangiacrapa" Date: Mon, 16 Jan 2023 11:37:06 +0100 Subject: [PATCH] Added Layer Style and Show Legend facilities --- .settings/org.eclipse.wst.common.component | 40 ++- .../client/ui/GeonaDataViewMainPanel.java | 32 +- .../layercollection/LayerCollectionPanel.java | 287 ++++++++++++++++++ .../LayerCollectionPanel.ui.xml | 23 ++ .../server/Geoportal_JSON_Mapper.java | 4 +- src/main/webapp/GeoportalDataViewer.css | 11 + 6 files changed, 358 insertions(+), 39 deletions(-) create mode 100644 src/main/java/org/gcube/portlets/user/geoportaldataviewer/client/ui/layercollection/LayerCollectionPanel.java create mode 100644 src/main/java/org/gcube/portlets/user/geoportaldataviewer/client/ui/layercollection/LayerCollectionPanel.ui.xml diff --git a/.settings/org.eclipse.wst.common.component b/.settings/org.eclipse.wst.common.component index 31252a3..88e98e6 100644 --- a/.settings/org.eclipse.wst.common.component +++ b/.settings/org.eclipse.wst.common.component @@ -1,5 +1,7 @@ - + + + @@ -41,7 +43,9 @@ - + + + @@ -83,7 +87,9 @@ - + + + @@ -125,7 +131,9 @@ - + + + @@ -167,7 +175,9 @@ - + + + @@ -209,7 +219,9 @@ - + + + @@ -251,7 +263,9 @@ - + + + @@ -293,7 +307,9 @@ - + + + @@ -335,7 +351,9 @@ - + + + @@ -377,7 +395,9 @@ - + + + diff --git a/src/main/java/org/gcube/portlets/user/geoportaldataviewer/client/ui/GeonaDataViewMainPanel.java b/src/main/java/org/gcube/portlets/user/geoportaldataviewer/client/ui/GeonaDataViewMainPanel.java index 0a4c415..9ada671 100644 --- a/src/main/java/org/gcube/portlets/user/geoportaldataviewer/client/ui/GeonaDataViewMainPanel.java +++ b/src/main/java/org/gcube/portlets/user/geoportaldataviewer/client/ui/GeonaDataViewMainPanel.java @@ -15,12 +15,12 @@ import org.gcube.portlets.user.geoportaldataviewer.client.GeoportalDataViewerCon import org.gcube.portlets.user.geoportaldataviewer.client.GeoportalDataViewerConstants.MAP_PROJECTION; import org.gcube.portlets.user.geoportaldataviewer.client.events.ChangeMapLayerEvent; import org.gcube.portlets.user.geoportaldataviewer.client.events.MapExtentToEvent; -import org.gcube.portlets.user.geoportaldataviewer.client.events.collections.CloseCollectionEvent; import org.gcube.portlets.user.geoportaldataviewer.client.events.collections.OpenCollectionEvent; import org.gcube.portlets.user.geoportaldataviewer.client.gis.OpenLayerMap; import org.gcube.portlets.user.geoportaldataviewer.client.resources.GNAIcons; import org.gcube.portlets.user.geoportaldataviewer.client.resources.GNAImages; import org.gcube.portlets.user.geoportaldataviewer.client.ui.cms.search.SearchFacilityUI; +import org.gcube.portlets.user.geoportaldataviewer.client.ui.layercollection.LayerCollectionPanel; import org.gcube.portlets.user.geoportaldataviewer.client.ui.map.ExtentMapUtil; import org.gcube.portlets.user.geoportaldataviewer.client.ui.map.ExtentMapUtil.Location; import org.gcube.portlets.user.geoportaldataviewer.shared.GCubeCollection; @@ -521,44 +521,22 @@ public class GeonaDataViewMainPanel extends Composite { for (GCubeCollection coll : collection) { GWT.log("Found available collection " + coll); - String label = coll.getUcd().getName(); final String collectionID = coll.getUcd().getId(); // collectionID == UCD_Id - CheckBox checkbox = new CheckBox(label); - checkbox.setId("gcubeCollectionSelector_" + collectionID); + LayerCollectionPanel lcp = new LayerCollectionPanel(coll, applicationBus); - checkbox.addValueChangeHandler(new ValueChangeHandler() { - - @Override - public void onValueChange(ValueChangeEvent event) { - GWT.log("Collection selector flag changed to value : " + event.toDebugString()); - -// String collectionID = ((CheckBox) event.getSource()).getId().replace("gcubeCollectionSelector_", -// ""); - - GWT.log("Collection ID is : " + collectionID + ", event value: " + event.getValue()); - if (event.getValue()) { - // OPEN COLLECTION - applicationBus.fireEvent(new OpenCollectionEvent(collectionID)); - } else { - // CLOSE COLLECTION - applicationBus.fireEvent(new CloseCollectionEvent(collectionID)); - } - } - }); - - openCollectionPanel.add(checkbox); + openCollectionPanel.add(lcp); List listCollections = mapCollectionCheckBoxes.get(collectionID); if (listCollections == null) listCollections = new ArrayList(); - listCollections.add(checkbox); + listCollections.add(lcp.getCheckbox()); mapCollectionCheckBoxes.put(collectionID, listCollections); // Opening All Collections if openCollectionID is null if (openCollectionID == null) { - checkbox.setValue(true, true); + lcp.getCheckbox().setValue(true, true); // String collectionID = checkbox.getId().replace("gcubeCollectionSelector_", // ""); GWT.log("Opening the collection: " + collectionID); diff --git a/src/main/java/org/gcube/portlets/user/geoportaldataviewer/client/ui/layercollection/LayerCollectionPanel.java b/src/main/java/org/gcube/portlets/user/geoportaldataviewer/client/ui/layercollection/LayerCollectionPanel.java new file mode 100644 index 0000000..15dcd9d --- /dev/null +++ b/src/main/java/org/gcube/portlets/user/geoportaldataviewer/client/ui/layercollection/LayerCollectionPanel.java @@ -0,0 +1,287 @@ +package org.gcube.portlets.user.geoportaldataviewer.client.ui.layercollection; + +import java.util.HashMap; + +import org.gcube.application.geoportalcommon.shared.geoportal.materialization.IndexLayerDV; +import org.gcube.portlets.user.geoportaldataviewer.client.GeoportalDataViewer; +import org.gcube.portlets.user.geoportaldataviewer.client.GeoportalDataViewerServiceAsync; +import org.gcube.portlets.user.geoportaldataviewer.client.events.collections.CloseCollectionEvent; +import org.gcube.portlets.user.geoportaldataviewer.client.events.collections.OpenCollectionEvent; +import org.gcube.portlets.user.geoportaldataviewer.client.util.URLUtil; +import org.gcube.portlets.user.geoportaldataviewer.shared.GCubeCollection; +import org.gcube.portlets.user.geoportaldataviewer.shared.ViewerConfiguration; +import org.gcube.portlets.user.geoportaldataviewer.shared.gis.wms.GeoInformationForWMSRequest; + +import com.github.gwtbootstrap.client.ui.Button; +import com.github.gwtbootstrap.client.ui.CheckBox; +import com.github.gwtbootstrap.client.ui.ListBox; +import com.github.gwtbootstrap.client.ui.constants.ButtonType; +import com.google.gwt.core.client.GWT; +import com.google.gwt.dom.client.Style.FontWeight; +import com.google.gwt.event.dom.client.ClickEvent; +import com.google.gwt.event.dom.client.ClickHandler; +import com.google.gwt.event.dom.client.ErrorEvent; +import com.google.gwt.event.dom.client.ErrorHandler; +import com.google.gwt.event.dom.client.LoadEvent; +import com.google.gwt.event.dom.client.LoadHandler; +import com.google.gwt.event.logical.shared.ValueChangeEvent; +import com.google.gwt.event.logical.shared.ValueChangeHandler; +import com.google.gwt.event.shared.HandlerManager; +import com.google.gwt.uibinder.client.UiBinder; +import com.google.gwt.uibinder.client.UiField; +import com.google.gwt.user.client.Window; +import com.google.gwt.user.client.rpc.AsyncCallback; +import com.google.gwt.user.client.ui.Composite; +import com.google.gwt.user.client.ui.FlexTable; +import com.google.gwt.user.client.ui.FlowPanel; +import com.google.gwt.user.client.ui.HTML; +import com.google.gwt.user.client.ui.HTMLPanel; +import com.google.gwt.user.client.ui.HorizontalPanel; +import com.google.gwt.user.client.ui.Image; +import com.google.gwt.user.client.ui.Label; +import com.google.gwt.user.client.ui.ScrollPanel; +import com.google.gwt.user.client.ui.Widget; + +/** + * The Class LayerCollectionPanel. + * + * @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it + * + * Jan 16, 2023 + */ +public class LayerCollectionPanel extends Composite { + + public static String COLORSCALERANGE = "COLORSCALERANGE"; + + @UiField + HTMLPanel basePanel; + + @UiField + HTMLPanel stylePanel; + + @UiField + HTMLPanel styleListPanel; + + @UiField + Button buttonLegend; + + @UiField + ScrollPanel legendPanel; + + private CheckBox checkbox; + + private GeoInformationForWMSRequest geoInformation; + + private static LayerCollectionPanelUiBinder uiBinder = GWT.create(LayerCollectionPanelUiBinder.class); + + private ListBox listBoxStyles = new ListBox(); + + /** + * The Interface LayerCollectionPanelUiBinder. + * + * @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it + * + * Jan 16, 2023 + */ + interface LayerCollectionPanelUiBinder extends UiBinder { + } + + /** + * Instantiates a new layer collection panel. + * + * @param coll the coll + * @param applicationBus the application bus + */ + public LayerCollectionPanel(GCubeCollection coll, HandlerManager applicationBus) { + initWidget(uiBinder.createAndBindUi(this)); + + buttonLegend.setType(ButtonType.LINK); + legendPanel.setVisible(false); + + GWT.log("Found available collection " + coll); + String label = coll.getUcd().getName(); + final String collectionID = coll.getUcd().getId(); // collectionID == UCD_Id + + checkbox = new CheckBox(label); + checkbox.setId("gcubeCollectionSelector_" + collectionID); + checkbox.getElement().getStyle().setFontWeight(FontWeight.BOLD); + + checkbox.addValueChangeHandler(new ValueChangeHandler() { + + @Override + public void onValueChange(ValueChangeEvent event) { + GWT.log("Collection selector flag changed to value : " + event.toDebugString()); + +// String collectionID = ((CheckBox) event.getSource()).getId().replace("gcubeCollectionSelector_", +// ""); + + GWT.log("Collection ID is : " + collectionID + ", event value: " + event.getValue()); + if (event.getValue()) { + // OPEN COLLECTION + applicationBus.fireEvent(new OpenCollectionEvent(collectionID)); + } else { + // CLOSE COLLECTION + legendPanel.clear(); + legendPanel.setVisible(false); + applicationBus.fireEvent(new CloseCollectionEvent(collectionID)); + } + } + }); + + basePanel.add(checkbox); + + ViewerConfiguration theConfig = GeoportalDataViewer.getStatus().getViewerConfig(); + GCubeCollection toOpen = theConfig.getAvailableCollections().get(collectionID); + + // Check if indexes is empty + if (toOpen.getIndexes() == null || toOpen.getIndexes().isEmpty()) { + GWT.log("Unexpected empty indexes in collection " + toOpen); + Window.alert("Cannot open collection index layer for " + toOpen.getUcd().getName() + "."); + return; + } + // TODO Get Default Index Layer + // For now we just take the first - only 1 is expected + IndexLayerDV layer = toOpen.getIndexes().get(0); + // Open Index Layer + + HashMap ogcLinks = layer.getLayer().getOgcLinks(); + final String wmsLink = ogcLinks.get("wms"); + final String layerName = URLUtil.getValueOfParameter("layers", wmsLink); + + GeoportalDataViewerServiceAsync.Util.getInstance().parseWmsRequest(wmsLink, layerName, + new AsyncCallback() { + + @Override + public void onFailure(Throwable caught) { + + Window.alert(caught.getMessage()); + } + + @Override + public void onSuccess(GeoInformationForWMSRequest geoInfoWMS) { + geoInformation = geoInfoWMS; + GWT.log("Parsed WMS Request returned: " + geoInfoWMS); + + if (geoInfoWMS.getStyles() != null && geoInfoWMS.getStyles().getGeoStyles() != null) { + if (geoInfoWMS.getStyles().getGeoStyles().size() > 0) { + stylePanel.setVisible(true); + listBoxStyles.clear(); + for (String styleName : geoInfoWMS.getStyles().getGeoStyles()) { + listBoxStyles.addItem(styleName, styleName); + } + + styleListPanel.add(listBoxStyles); + } + } + + buttonLegend.addClickHandler(new ClickHandler() { + + @Override + public void onClick(ClickEvent event) { + GWT.log("is isToggle: " + buttonLegend.isToggle()); + GWT.log("is isToggled: " + buttonLegend.isToggled()); + if (legendPanel.isVisible()) { + legendPanel.clear(); + legendPanel.setVisible(false); + + } else { + legendPanel.setVisible(true); + loadLegend(wmsLink); + } + + } + }); + + } + }); + + } + + /** + * Gets the checkbox. + * + * @return the checkbox + */ + public CheckBox getCheckbox() { + return checkbox; + } + + /** + * Load legend. + * + * @param wmsLink the wms link + */ + private void loadLegend(String wmsLink) { + + legendPanel.clear(); + String theLayerName = geoInformation.getLayerName(); + final FlexTable flexTable = new FlexTable(); + legendPanel.add(flexTable); + + // Case no style found + if (listBoxStyles.getSelectedValue() == null) { + flexTable.setWidget(0, 0, new HTML("No style found")); + return; + } + + FlowPanel flow = new FlowPanel(); + flow.add(new HTMLPanel("Legend for: " + theLayerName)); + final HorizontalPanel hpLegend = new HorizontalPanel(); + + String url = geoInformation.getBaseWmsServiceHost() + "?service=WMS&" + "version=" + + URLUtil.getValueOfParameter("version", wmsLink) + "&" + "request=GetLegendGraphic&" + "layer=" + + theLayerName; + + String styleName = null; + try { + styleName = listBoxStyles.getSelectedValue(); + } catch (Exception e) { + } + + styleName = styleName != null && !styleName.isEmpty() ? styleName : ""; + + if (!geoInformation.isNcWMS()) { + + url += "&format=image/png" + "&STYLE=" + styleName + + "&LEGEND_OPTIONS=forceRule:True;dx:0.2;dy:0.2;mx:0.2;my:0.2;fontStyle:bold;" + + "borderColor:000000;border:true;fontColor:000000;fontSize:14"; + } else { + + int isNcWmsStyle = styleName.indexOf("/"); + if (isNcWmsStyle != -1) { + styleName = styleName.substring(isNcWmsStyle + 1, styleName.length()); + } + url += "&palette=" + styleName; + if (geoInformation.getMapWMSNoStandard() != null) { + for (String key : geoInformation.getMapWMSNoStandard().keySet()) { // ADDING COLORSCALERANGE? + if (key.compareToIgnoreCase(COLORSCALERANGE) == 0) { + url += "&" + key + "=" + geoInformation.getMapWMSNoStandard().get(key); + break; + } + } + } + } + + GWT.log(url); + flexTable.setStyleName("layer-style-panel-table-legend"); + flexTable.setWidget(0, 0, new Label("Legend")); + final Image legendImage = new Image(url); + legendImage.addLoadHandler(new LoadHandler() { + @Override + public void onLoad(LoadEvent event) { + } + }); + + legendImage.addErrorHandler(new ErrorHandler() { + @Override + public void onError(ErrorEvent event) { + GWT.log("ErrorEvent "); + flexTable.setWidget(0, 1, new HTML("Error on loading the style")); + } + }); + + hpLegend.add(legendImage); + flexTable.setWidget(0, 1, hpLegend); + } + +} diff --git a/src/main/java/org/gcube/portlets/user/geoportaldataviewer/client/ui/layercollection/LayerCollectionPanel.ui.xml b/src/main/java/org/gcube/portlets/user/geoportaldataviewer/client/ui/layercollection/LayerCollectionPanel.ui.xml new file mode 100644 index 0000000..afc4a62 --- /dev/null +++ b/src/main/java/org/gcube/portlets/user/geoportaldataviewer/client/ui/layercollection/LayerCollectionPanel.ui.xml @@ -0,0 +1,23 @@ + + + + .important { + font-weight: bold; + } + + + + + + Layer Style + + + + + + + + + \ No newline at end of file diff --git a/src/main/java/org/gcube/portlets/user/geoportaldataviewer/server/Geoportal_JSON_Mapper.java b/src/main/java/org/gcube/portlets/user/geoportaldataviewer/server/Geoportal_JSON_Mapper.java index 715815d..428cd52 100644 --- a/src/main/java/org/gcube/portlets/user/geoportaldataviewer/server/Geoportal_JSON_Mapper.java +++ b/src/main/java/org/gcube/portlets/user/geoportaldataviewer/server/Geoportal_JSON_Mapper.java @@ -554,10 +554,10 @@ public class Geoportal_JSON_Mapper { } //Setting only if one element exists - if(filesetImages.getListPayload().size()>0) { + if(filesetImages.getListPayload()!=null && filesetImages.getListPayload().size()>0) { listImages.add(filesetImages); } - if(filesetFiles.getListPayload().size()>0) { + if(filesetFiles.getListPayload()!=null && filesetFiles.getListPayload().size()>0) { listFiles.add(filesetFiles); } diff --git a/src/main/webapp/GeoportalDataViewer.css b/src/main/webapp/GeoportalDataViewer.css index e0d7769..78893b9 100644 --- a/src/main/webapp/GeoportalDataViewer.css +++ b/src/main/webapp/GeoportalDataViewer.css @@ -769,3 +769,14 @@ RESPONSIVE width: 100px; } } + +/* OPEN COLLECTION UI */ +.layer-style-panel { + margin-left: 25px; + font-size: 13px; + color: gray; +} + +.layer-style-panel-table-legend table td { + padding-left: 5px; +} \ No newline at end of file