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@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: tom@7958: protected FLYSConstants MSG = GWT.create(FLYSConstants.class); tom@7958: ingo@4329: private ThemeList themeList; ingo@4329: d@9619: private final VLayout legendContainer; ingo@4329: d@9619: public LegendWindow(final ThemeList themeList) { ingo@4329: this.themeList = themeList; ingo@4329: this.legendContainer = new VLayout(); ingo@4329: ingo@4329: init(); ingo@4329: } ingo@4329: d@9619: public void update(final ThemeList themeList) { ingo@4329: this.themeList = themeList; ingo@4329: d@9619: final Canvas[] legends = this.legendContainer.getMembers(); d@9619: this.legendContainer.removeMembers(legends); ingo@4329: ingo@4329: addLegends(); ingo@4329: } ingo@4329: ingo@4329: private void addLegends() { d@9619: final List themes = this.themeList.getActiveThemes(); ingo@4329: d@9619: for (final Theme theme : themes) { ingo@4329: if (theme.getActive() == 0) { ingo@4329: continue; ingo@4329: } ingo@4329: ingo@4329: if (theme instanceof AttributedTheme) { d@9619: this.legendContainer.addMember(createLegendGraphicsRow((AttributedTheme) theme)); ingo@4329: } ingo@4329: } ingo@4329: } ingo@4329: d@9619: private Canvas createLegendGraphicsRow(final AttributedTheme at) { d@9619: final Label label = new Label(at.getDescription()); d@9619: final Widget img = createLegendGraphics(at); ingo@4329: d@9619: final HLayout row = new HLayout(); ingo@4329: row.addMember(label); ingo@4329: row.addMember(img); d@9693: d@9693: String url = "url"; d@9693: d@9693: if (img instanceof Image) { d@9693: setDimensions( row, ((Image) img).getHeight(), ((Image) img).getWidth()); d@9693: url = " url " + ((Image) img).getUrl(); d@9693: } d@9693: else if (img instanceof Img) { d@9693: setDimensions( row, ((Img) img).getHeight(), ((Img) img).getWidth()); d@9693: url = " dataPath " + ((Img) img).getDataPath(); d@9693: } d@9693: else { d@9693: setDimensions( row, 150, 400); //not sure when this is executed d@9693: } d@9693: String labelText = at.getDescription() +", height: "+ row.getHeight()+"width: "+ row.getWidth()+" "+url; d@9693: // d@9693: // final HLayout row2= new HLayout(); d@9693: row.addMember(new Label(labelText)); ingo@4329: return row; ingo@4329: } ingo@4329: d@9693: 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@9619: private Widget createLegendGraphics(final AttributedTheme at) { ingo@4329: d@9619: final String legend = at.getAttr("legend"); d@9619: // TEST: final Image img = new Image("/images/FLYS_Donau.png"); (funktioniert!), ebenso FLYSResources.getTest(); d@9619: if (legend != null && !"".equals(legend)) { d@9644: d@9644: final String imgUrl = URL.encode(GWT.getHostPageBaseURL() + "images/wms_legend/" + legend); d@9644: d@9644: final Image image = new Image(imgUrl); d@9619: final int imageWidth = image.getWidth(); d@9619: final int imageHeight = image.getHeight(); d@9619: final double widthPercent = imageWidth / 400.; d@9635: d@9635: // removing the limit of 150px height (legend can have an infinite height now, d@9635: // as long as the width is less/eq 400px; if the width is >400, d@9635: // the legend will be scaled to 400px width; the ratio will be kept) d@9635: d@9635: // final double heightPercent = imageHeight / 150.; d@9635: if (widthPercent > 1)// || heightPercent > 1) { d@9635: image.setSize((int) (imageWidth / widthPercent) + "px", (int) (imageHeight / widthPercent) + "px"); d@9693: d@9619: return image; d@9619: } d@9619: final String imgUrl = MapUtils.getLegendGraphicUrl(at.getAttr("url"), at.getAttr("layers")); d@9619: d@9619: final Img img = new Img(imgUrl); ingo@4329: img.setImageType(ImageStyle.CENTER); ingo@4329: img.setAutoFit(true); ingo@4329: ingo@4329: return img; ingo@4329: } ingo@4329: ingo@4329: private void init() { d@9619: this.legendContainer.setAutoHeight(); d@9619: this.legendContainer.setLayoutAlign(VerticalAlignment.TOP); d@9619: this.legendContainer.setAlign(VerticalAlignment.CENTER); ingo@4329: d@9619: setTitle(this.MSG.wms_legend()); ingo@4329: setAutoSize(true); ingo@4329: setCanDragResize(true); ingo@4329: setIsModal(false); ingo@4329: setShowModalMask(false); ingo@4329: setLayoutAlign(VerticalAlignment.TOP); ingo@4329: setAlign(VerticalAlignment.TOP); ingo@4329: d@9619: addItem(this.legendContainer); ingo@4329: addLegends(); ingo@4329: ingo@4329: centerInPage(); ingo@4329: } ingo@4329: }