teichmann@5861: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde teichmann@5861: * Software engineering by Intevation GmbH teichmann@5861: * teichmann@5993: * This file is Free Software under the GNU AGPL (>=v3) teichmann@5861: * and comes with ABSOLUTELY NO WARRANTY! Check out the teichmann@5993: * documentation coming with Dive4Elements River for details. teichmann@5861: */ teichmann@5861: teichmann@5835: package org.dive4elements.river.client.client.ui.map; ingo@4329: ingo@4329: import java.util.List; ingo@4329: d@9619: import org.dive4elements.river.client.client.FLYSConstants; d@9619: import org.dive4elements.river.client.shared.MapUtils; d@9619: import org.dive4elements.river.client.shared.model.AttributedTheme; d@9619: import org.dive4elements.river.client.shared.model.Theme; d@9619: import org.dive4elements.river.client.shared.model.ThemeList; d@9619: tom@7958: import com.google.gwt.core.client.GWT; d@9696: import com.google.gwt.event.dom.client.LoadHandler; d@9644: import com.google.gwt.http.client.URL; d@9619: import com.google.gwt.user.client.ui.Image; d@9619: import com.google.gwt.user.client.ui.Widget; ingo@4329: import com.smartgwt.client.types.ImageStyle; ingo@4329: import com.smartgwt.client.types.VerticalAlignment; ingo@4329: import com.smartgwt.client.widgets.Canvas; ingo@4329: import com.smartgwt.client.widgets.Img; ingo@4329: import com.smartgwt.client.widgets.Label; ingo@4329: import com.smartgwt.client.widgets.Window; ingo@4329: import com.smartgwt.client.widgets.layout.HLayout; ingo@4329: import com.smartgwt.client.widgets.layout.VLayout; ingo@4329: ingo@4329: public class LegendWindow extends Window { ingo@4329: d@9696: protected FLYSConstants MSG = GWT.create(FLYSConstants.class); ingo@4329: d@9696: private ThemeList themeList; ingo@4329: d@9696: private final VLayout legendContainer; d@9694: d@9696: public LegendWindow(final ThemeList themeList) { d@9696: this.themeList = themeList; d@9696: this.legendContainer = new VLayout(); ingo@4329: d@9696: init(); d@9696: } d@9696: d@9696: public void update(final ThemeList themeList) { d@9696: this.themeList = themeList; d@9696: d@9696: final Canvas[] legends = this.legendContainer.getMembers(); d@9696: this.legendContainer.removeMembers(legends); d@9696: d@9696: addLegends(); d@9696: } d@9696: d@9696: private void addLegends() { d@9696: final List themes = this.themeList.getActiveThemes(); d@9696: com.smartgwt.client.util.SC.say("themescount " + themes.size()); d@9696: d@9696: for (int i = 0; i < themes.size(); i++) { d@9696: final Theme theme = themes.get(i); d@9696: if (theme.getActive() == 0) { d@9696: continue; d@9696: } d@9696: try { d@9696: if (theme instanceof AttributedTheme) { d@9697: theme.setActive(0); d@9696: Canvas createLegendGraphicsRow = createLegendGraphicsRow((AttributedTheme) theme); d@9697: // this.legendContainer.addMember(createLegendGraphicsRow); d@9697: theme.setActive(1); d@9697: Canvas createLegendGraphicsRow2 = createLegendGraphicsRow((AttributedTheme) theme); d@9697: this.legendContainer.addMember(createLegendGraphicsRow2); d@9696: } d@9696: } catch (Exception e) { d@9696: com.smartgwt.client.util.SC.say("exception at index " + themes.size()); d@9696: continue; d@9696: } d@9696: } d@9696: } d@9696: d@9696: private Canvas createLegendGraphicsRow(final AttributedTheme at) { d@9696: com.smartgwt.client.util.SC.say("init"); d@9696: final Label label = new Label(at.getDescription()); d@9696: final Widget img = createLegendGraphics(at); d@9696: d@9696: final HLayout row = new HLayout(); d@9696: row.addMember(label); d@9696: row.addMember(img); d@9696: d@9696: String url = "url"; d@9696: d@9696: if (img instanceof Image) { d@9696: Image.prefetch(((Image) img).getUrl()); d@9696: setDimensions(row, ((Image) img).getHeight(), ((Image) img).getWidth()); d@9696: url = " url " + ((Image) img).getUrl(); d@9696: } else if (img instanceof Img) { d@9696: setDimensions(row, ((Img) img).getHeight(), ((Img) img).getWidth()); d@9696: url = " dataPath " + ((Img) img).getDataPath(); d@9696: } else { d@9696: setDimensions(row, 150, 400); // not sure when this is executed d@9696: } d@9696: String labelText = at.getDescription() + ", height: " + row.getHeight() + "width: " + row.getWidth() + " " d@9696: + url; d@9696: // d@9696: // final HLayout row2= new HLayout(); d@9696: // row.addMember(new Label(labelText)); d@9696: d@9696: com.smartgwt.client.util.SC.say(labelText); d@9696: return row; d@9696: } d@9696: d@9696: private void setDimensions(HLayout row, int height, int width) { d@9693: final int minHeight = 30; d@9693: row.setHeight(height < minHeight ? minHeight : height); d@9693: row.setWidth(width); d@9693: } d@9693: d@9696: private Widget createLegendGraphics(final AttributedTheme at) { d@9619: d@9696: final String legend = at.getAttr("legend"); d@9696: // TEST: final Image img = new Image("/images/FLYS_Donau.png"); (funktioniert!), d@9696: // ebenso FLYSResources.getTest(); d@9696: if (legend != null && !"".equals(legend)) { ingo@4329: d@9696: final String imgUrl = URL.encode(GWT.getHostPageBaseURL() + "images/wms_legend/" + legend); ingo@4329: d@9696: final Image image = new Image(imgUrl); d@9696: final int imageWidth = image.getWidth(); d@9696: final int imageHeight = image.getHeight(); d@9696: final double widthPercent = imageWidth / 400.; ingo@4329: d@9696: // removing the limit of 150px height (legend can have an infinite height now, d@9696: // as long as the width is less/eq 400px; if the width is >400, d@9696: // the legend will be scaled to 400px width; the ratio will be kept) d@9696: d@9696: // final double heightPercent = imageHeight / 150.; d@9696: if (widthPercent > 1)// || heightPercent > 1) { d@9696: image.setSize((int) (imageWidth / widthPercent) + "px", (int) (imageHeight / widthPercent) + "px"); d@9696: d@9696: return image; d@9696: } d@9696: final String imgUrl = MapUtils.getLegendGraphicUrl(at.getAttr("url"), at.getAttr("layers")); d@9696: d@9696: final Img img = new Img(imgUrl); d@9696: img.setImageType(ImageStyle.CENTER); d@9696: img.setAutoFit(true); d@9696: d@9696: return img; d@9696: } d@9696: d@9696: private void init() { d@9696: this.legendContainer.setAutoHeight(); d@9696: this.legendContainer.setLayoutAlign(VerticalAlignment.TOP); d@9696: this.legendContainer.setAlign(VerticalAlignment.CENTER); d@9696: d@9696: setTitle(this.MSG.wms_legend()); d@9696: setAutoSize(true); d@9696: setCanDragResize(true); d@9696: setIsModal(false); d@9696: setShowModalMask(false); d@9696: setLayoutAlign(VerticalAlignment.TOP); d@9696: setAlign(VerticalAlignment.TOP); d@9696: d@9696: addItem(this.legendContainer); d@9696: addLegends(); d@9696: d@9696: centerInPage(); d@9696: } ingo@4329: }