diff gwt-client/src/main/java/org/dive4elements/river/client/client/ui/map/LegendWindow.java @ 9619:63bbd5e45839

#21 WMS Legend
author dnt_bjoernsen <d.tironi@bjoernsen.de>
date Thu, 10 Oct 2019 16:08:47 +0200
parents 5e38e2924c07
children a03c54129819
line wrap: on
line diff
--- a/gwt-client/src/main/java/org/dive4elements/river/client/client/ui/map/LegendWindow.java	Thu Oct 10 16:02:31 2019 +0200
+++ b/gwt-client/src/main/java/org/dive4elements/river/client/client/ui/map/LegendWindow.java	Thu Oct 10 16:08:47 2019 +0200
@@ -10,8 +10,15 @@
 
 import java.util.List;
 
+import org.dive4elements.river.client.client.FLYSConstants;
+import org.dive4elements.river.client.shared.MapUtils;
+import org.dive4elements.river.client.shared.model.AttributedTheme;
+import org.dive4elements.river.client.shared.model.Theme;
+import org.dive4elements.river.client.shared.model.ThemeList;
+
 import com.google.gwt.core.client.GWT;
-
+import com.google.gwt.user.client.ui.Image;
+import com.google.gwt.user.client.ui.Widget;
 import com.smartgwt.client.types.ImageStyle;
 import com.smartgwt.client.types.VerticalAlignment;
 import com.smartgwt.client.widgets.Canvas;
@@ -21,59 +28,49 @@
 import com.smartgwt.client.widgets.layout.HLayout;
 import com.smartgwt.client.widgets.layout.VLayout;
 
-import org.dive4elements.river.client.client.FLYSConstants;
-
-import org.dive4elements.river.client.shared.MapUtils;
-import org.dive4elements.river.client.shared.model.AttributedTheme;
-import org.dive4elements.river.client.shared.model.Theme;
-import org.dive4elements.river.client.shared.model.ThemeList;
-
-
 public class LegendWindow extends Window {
 
     protected FLYSConstants MSG = GWT.create(FLYSConstants.class);
 
     private ThemeList themeList;
 
-    private VLayout legendContainer;
+    private final VLayout legendContainer;
 
-
-    public LegendWindow(ThemeList themeList) {
+    public LegendWindow(final ThemeList themeList) {
         this.themeList = themeList;
         this.legendContainer = new VLayout();
 
         init();
     }
 
-    public void update(ThemeList themeList) {
+    public void update(final ThemeList themeList) {
         this.themeList = themeList;
 
-        Canvas[] legends = legendContainer.getMembers();
-        legendContainer.removeMembers(legends);
+        final Canvas[] legends = this.legendContainer.getMembers();
+        this.legendContainer.removeMembers(legends);
 
         addLegends();
     }
 
     private void addLegends() {
-        List<Theme> themes = themeList.getActiveThemes();
+        final List<Theme> themes = this.themeList.getActiveThemes();
 
-        for (Theme theme : themes) {
+        for (final Theme theme : themes) {
             if (theme.getActive() == 0) {
                 continue;
             }
 
             if (theme instanceof AttributedTheme) {
-                legendContainer.addMember(
-                    createLegendGraphicsRow((AttributedTheme) theme));
+                this.legendContainer.addMember(createLegendGraphicsRow((AttributedTheme) theme));
             }
         }
     }
 
-    private Canvas createLegendGraphicsRow(AttributedTheme at) {
-        Label label = new Label(at.getDescription());
-        Img img = createLegendGraphics(at);
+    private Canvas createLegendGraphicsRow(final AttributedTheme at) {
+        final Label label = new Label(at.getDescription());
+        final Widget img = createLegendGraphics(at);
 
-        HLayout row = new HLayout();
+        final HLayout row = new HLayout();
         row.addMember(label);
         row.addMember(img);
 
@@ -83,11 +80,27 @@
         return row;
     }
 
-    private Img createLegendGraphics(AttributedTheme at) {
-        String imgUrl = MapUtils.getLegendGraphicUrl(at.getAttr("url"),
-            at.getAttr("layers"));
+    private Widget createLegendGraphics(final AttributedTheme at) {
 
-        Img img = new Img(imgUrl);
+        final String legend = at.getAttr("legend");
+        // TEST: final Image img = new Image("/images/FLYS_Donau.png"); (funktioniert!), ebenso FLYSResources.getTest();
+        if (legend != null && !"".equals(legend)) {
+            final Image image = new Image("/images/wms_legend/" + legend);
+            final int imageWidth = image.getWidth();
+            final int imageHeight = image.getHeight();
+            final double widthPercent = imageWidth / 400.;
+            final double heightPercent = imageHeight / 150.;
+            if (widthPercent > 1 || heightPercent > 1) {
+                if (widthPercent > heightPercent)
+                    image.setSize((int) (imageWidth / widthPercent) + "px", (int) (imageHeight / widthPercent) + "px");
+                else
+                    image.setSize((int) (imageWidth / heightPercent) + "px", (int) (imageHeight / heightPercent) + "px");
+            }
+            return image;
+        }
+        final String imgUrl = MapUtils.getLegendGraphicUrl(at.getAttr("url"), at.getAttr("layers"));
+
+        final Img img = new Img(imgUrl);
         img.setImageType(ImageStyle.CENTER);
         img.setAutoFit(true);
 
@@ -95,11 +108,11 @@
     }
 
     private void init() {
-        legendContainer.setAutoHeight();
-        legendContainer.setLayoutAlign(VerticalAlignment.TOP);
-        legendContainer.setAlign(VerticalAlignment.CENTER);
+        this.legendContainer.setAutoHeight();
+        this.legendContainer.setLayoutAlign(VerticalAlignment.TOP);
+        this.legendContainer.setAlign(VerticalAlignment.CENTER);
 
-        setTitle(MSG.wms_legend());
+        setTitle(this.MSG.wms_legend());
         setAutoSize(true);
         setCanDragResize(true);
         setIsModal(false);
@@ -107,7 +120,7 @@
         setLayoutAlign(VerticalAlignment.TOP);
         setAlign(VerticalAlignment.TOP);
 
-        addItem(legendContainer);
+        addItem(this.legendContainer);
         addLegends();
 
         centerInPage();

http://dive4elements.wald.intevation.org