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; christian@4472: teichmann@5835: import org.dive4elements.river.client.client.Config; teichmann@5835: import org.dive4elements.river.client.client.FLYSConstants; teichmann@5835: import org.dive4elements.river.client.client.services.CollectionAttributeService; teichmann@5835: import org.dive4elements.river.client.client.services.CollectionAttributeServiceAsync; christian@6245: import org.dive4elements.river.client.shared.model.Artifact; christian@6245: import org.dive4elements.river.client.shared.model.ArtifactDescription; teichmann@5835: import org.dive4elements.river.client.shared.model.Collection; christian@6245: import org.dive4elements.river.client.shared.model.Data; christian@6245: import org.dive4elements.river.client.shared.model.DataItem; christian@6245: import org.dive4elements.river.client.shared.model.DataList; teichmann@5835: import org.dive4elements.river.client.shared.model.OutputSettings; teichmann@5835: import org.dive4elements.river.client.shared.model.Property; teichmann@5835: import org.dive4elements.river.client.shared.model.PropertySetting; teichmann@5835: import org.dive4elements.river.client.shared.model.Settings; aheinecke@5675: christian@6245: import com.google.gwt.core.client.GWT; tom@8844: import com.google.gwt.http.client.URL; christian@6245: import com.google.gwt.user.client.Window; christian@6245: import com.google.gwt.user.client.rpc.AsyncCallback; christian@6245: import com.smartgwt.client.types.Alignment; christian@6245: import com.smartgwt.client.util.SC; christian@6245: import com.smartgwt.client.widgets.Canvas; christian@6245: import com.smartgwt.client.widgets.form.DynamicForm; christian@6245: import com.smartgwt.client.widgets.form.fields.ButtonItem; christian@6245: import com.smartgwt.client.widgets.form.fields.SelectItem; christian@6245: import com.smartgwt.client.widgets.form.fields.TextItem; christian@6245: import com.smartgwt.client.widgets.form.fields.events.ClickEvent; christian@6245: import com.smartgwt.client.widgets.form.fields.events.ClickHandler; christian@4472: christian@4472: import java.util.ArrayList; christian@4472: import java.util.LinkedHashMap; christian@4472: import java.util.List; aheinecke@5675: import java.util.MissingResourceException; christian@4472: christian@6245: import org.gwtopenmaps.openlayers.client.Bounds; christian@6245: import org.gwtopenmaps.openlayers.client.Map; christian@6245: aheinecke@5606: public class MapPrintPanel extends Canvas { christian@4472: aheinecke@5675: private static final String MAPFISH_MAPTITLE = "mapfish_mapTitle"; aheinecke@5675: private static final String MAPFISH_RANGE = "mapfish_data_range"; aheinecke@5675: private static final String MAPFISH_SUBTITLE = "mapfish_data_subtitle"; aheinecke@5675: private static final String MAPFISH_STRETCH = "mapfish_data_strech"; tom@8856: private static final String MAPFISH_INSTITUTION = tom@8856: "mapfish_data_institution"; aheinecke@5675: private static final String MAPFISH_SOURCE = "mapfish_data_source"; aheinecke@5675: private static final String MAPFISH_CREATOR = "mapfish_data_creator"; aheinecke@5675: private static final String MAPFISH_DATEPLACE = "mapfish_data_dateplace"; aheinecke@5675: private static final String MAPFISH_RIVER = "mapfish_data_river"; aheinecke@5717: private static final String MAPFISH_LOGO = "mapfish_logo"; christian@4474: christian@4472: protected FLYSConstants MSG = GWT.create(FLYSConstants.class); christian@4472: christian@4472: /** CollectionAttribute Update Service. */ christian@4472: protected CollectionAttributeServiceAsync updater = christian@4472: GWT.create(CollectionAttributeService.class); christian@4472: aheinecke@5606: christian@4472: protected Collection collection; christian@4472: protected Settings settings; christian@4472: protected TextItem pageTitle = new TextItem(); aheinecke@5606: protected TextItem pageRange = new TextItem(); aheinecke@5606: protected TextItem pageSubtitle = new TextItem(); aheinecke@5717: protected TextItem pageStretch = new TextItem(); aheinecke@5717: protected TextItem pageInstitution = new TextItem(); aheinecke@5606: protected TextItem pageSource = new TextItem(); aheinecke@5606: protected TextItem pageCreator = new TextItem(); aheinecke@5606: protected TextItem pageDatePlace = new TextItem(); aheinecke@5717: protected SelectItem pageLogo = createPageLogoSelectItem(); aheinecke@5606: // protected SelectItem pageFormat = createPageFormatSelectItem(); christian@4474: protected MapToolbar mapToolbar; aheinecke@5606: protected MapPrintWindow parent; christian@4472: tom@8856: public MapPrintPanel( tom@8856: Collection collection, tom@8856: MapToolbar mapToolbar, tom@8856: MapPrintWindow parent tom@8856: ) { christian@4472: this.collection = collection; christian@4474: this.mapToolbar = mapToolbar; christian@4474: this.parent = parent; christian@4472: initLayout(); christian@4472: christian@4472: this.settings = collection.getSettings("print-settings"); christian@4472: if (settings == null) { christian@4472: settings = new OutputSettings(); christian@4473: GWT.log("settings are empty"); christian@4472: } christian@4472: else { christian@4472: List properties = settings.getSettings("default"); christian@4472: for (Property prop : properties) { christian@4472: PropertySetting props = (PropertySetting)prop; aheinecke@5606: GWT.log(props.getName() + "=" + props.getValue()); aheinecke@5606: if (props.getName().equals(MAPFISH_MAPTITLE)) { aheinecke@5606: this.pageTitle.setValue(props.getValue()); christian@4472: } aheinecke@5606: else if (props.getName().equals(MAPFISH_RANGE)) { aheinecke@5606: this.pageRange.setValue(props.getValue()); christian@4472: } aheinecke@5606: else if (props.getName().equals(MAPFISH_SUBTITLE)) { aheinecke@5606: this.pageSubtitle.setValue(props.getValue()); aheinecke@5606: } aheinecke@5606: else if (props.getName().equals(MAPFISH_STRETCH)) { aheinecke@5717: this.pageStretch.setValue(props.getValue()); aheinecke@5606: } aheinecke@5717: else if (props.getName().equals(MAPFISH_INSTITUTION)) { aheinecke@5717: this.pageInstitution.setValue(props.getValue()); aheinecke@5606: } aheinecke@5606: else if (props.getName().equals(MAPFISH_SOURCE)) { aheinecke@5606: this.pageSource.setValue(props.getValue()); aheinecke@5606: } aheinecke@5606: else if (props.getName().equals(MAPFISH_CREATOR)) { aheinecke@5606: this.pageCreator.setValue(props.getValue()); aheinecke@5606: } aheinecke@5606: else if (props.getName().equals(MAPFISH_DATEPLACE)) { aheinecke@5606: this.pageDatePlace.setValue(props.getValue()); aheinecke@5606: } else { aheinecke@5606: GWT.log("Unknown Print property: " + prop.getName()); christian@4474: } christian@4472: } christian@4472: } christian@4472: } christian@4472: christian@4472: protected void initLayout() { christian@4474: // TODO: i18n aheinecke@5717: this.pageTitle.setTitle(MSG.mapTitle()); aheinecke@5717: this.pageSubtitle.setTitle(MSG.mapSubtitle()); aheinecke@5717: this.pageRange.setTitle(MSG.mapRange()); aheinecke@5717: this.pageStretch.setTitle(MSG.mapStretch()); aheinecke@5717: this.pageInstitution.setTitle(MSG.mapInstitution()); aheinecke@5717: this.pageSource.setTitle(MSG.mapSource()); aheinecke@5717: this.pageCreator.setTitle(MSG.mapCreator()); aheinecke@5717: this.pageDatePlace.setTitle(MSG.mapDate()); christian@4472: christian@6245: pageTitle.setLength(20); aheinecke@5755: pageSubtitle.setLength(30); aheinecke@5755: pageRange.setLength(30); aheinecke@5755: pageStretch.setLength(30); aheinecke@5755: pageInstitution.setLength(30); aheinecke@5755: pageSource.setLength(30); aheinecke@5755: pageCreator.setLength(30); aheinecke@5755: pageDatePlace.setLength(30); aheinecke@5611: ButtonItem printButton = createPrintButtonItem(); aheinecke@5611: aheinecke@5611: printButton.setAlign(Alignment.RIGHT); aheinecke@5611: christian@4472: DynamicForm df = new DynamicForm(); christian@4472: df.setFields( aheinecke@5606: // this.pageFormat, christian@4472: this.pageTitle, aheinecke@5606: this.pageSubtitle, aheinecke@5606: this.pageRange, aheinecke@5717: this.pageStretch, aheinecke@5717: this.pageInstitution, aheinecke@5606: this.pageSource, aheinecke@5606: this.pageCreator, aheinecke@5606: this.pageDatePlace, aheinecke@5717: this.pageLogo, aheinecke@5611: printButton); christian@4472: addChild(df); christian@4472: } christian@4472: aheinecke@5717: protected SelectItem createPageLogoSelectItem() { aheinecke@5717: LinkedHashMap values = new LinkedHashMap(); aheinecke@5717: // TODO: this should be configurable aheinecke@5717: values.put(MSG.bfgLogo(), "BfG Logo"); aheinecke@5717: aheinecke@5717: SelectItem selItem = new SelectItem(); aheinecke@5717: selItem.setTitle(MSG.mapLogo()); aheinecke@5717: selItem.setValueMap(values); aheinecke@5717: selItem.setDefaultToFirstOption(true); aheinecke@5717: aheinecke@5717: return selItem; aheinecke@5717: } aheinecke@5717: aheinecke@5606: /* aheinecke@5606: * Commented out because we only provide a layout for A4 Landscape atm aheinecke@5606: christian@4472: protected SelectItem createPageFormatSelectItem() { christian@4472: LinkedHashMap values = new LinkedHashMap(); christian@4474: // TODO: i18n christian@4965: values.put("A4 landscape", "DIN A4 (Querformat)"); christian@4965: //values.put("A4 portrait", "DIN A4 (Hochformat)"); christian@4965: //values.put("A0 portrait", "DIN A0 (Hochformat)"); christian@4472: christian@4472: SelectItem selItem = new SelectItem(); christian@4474: selItem.setTitle("Seitengröße:"); // TODO: i18n christian@4472: selItem.setValueMap(values); christian@4472: selItem.setDefaultToFirstOption(true); christian@4472: christian@4472: return selItem; christian@4472: } aheinecke@5606: */ aheinecke@5606: protected ButtonItem createPrintButtonItem() { christian@4472: ButtonItem btn = new ButtonItem(); christian@4472: btn.addClickHandler(new ClickHandler() { christian@4472: christian@6245: @Override christian@4472: public void onClick(ClickEvent event) { aheinecke@5611: updateCollection(); aheinecke@5611: Window.open(createPrintUrl(), "_blank", ""); aheinecke@5611: parent.destroy(); christian@4472: } christian@4472: }); aheinecke@5606: btn.setTitle(MSG.print()); christian@4472: return btn; christian@4472: } christian@4472: aheinecke@5606: private String createPrintUrl() { aheinecke@5606: MapOutputTab ot = (MapOutputTab)mapToolbar.getOutputTab(); aheinecke@5606: Collection collection = ot.getCollection(); aheinecke@5606: String uuid = collection.identifier(); aheinecke@5606: aheinecke@5606: String mapType = collection.getOutputModes().containsKey("floodmap") aheinecke@5606: ? "floodmap" aheinecke@5606: : "map"; aheinecke@5606: aheinecke@5606: StringBuilder url = new StringBuilder(); aheinecke@5606: url.append(GWT.getModuleBaseURL()); aheinecke@5606: url.append("map-print?"); aheinecke@5606: aheinecke@5606: Map map = mapToolbar.getMap(); aheinecke@5606: Bounds bounds = map.getExtent(); aheinecke@5606: aheinecke@5606: if (bounds != null) { aheinecke@5606: try { aheinecke@5606: url.append("minx="); aheinecke@5606: url.append(bounds.getLowerLeftX()); aheinecke@5606: url.append("&"); aheinecke@5606: aheinecke@5606: url.append("maxx="); aheinecke@5606: url.append(bounds.getUpperRightX()); aheinecke@5606: url.append("&"); aheinecke@5606: aheinecke@5606: url.append("miny="); aheinecke@5606: url.append(bounds.getLowerLeftY()); aheinecke@5606: url.append("&"); aheinecke@5606: aheinecke@5606: url.append("maxy="); aheinecke@5606: url.append(bounds.getUpperRightY()); aheinecke@5606: url.append("&"); aheinecke@5606: } aheinecke@5606: catch (Exception e) { aheinecke@5606: // XXX: Ignore it. bounds.getXXX() throw aheinecke@5606: // exceptions when bound is invalid. :-/ aheinecke@5606: } aheinecke@5606: } aheinecke@5606: aheinecke@5606: url.append("uuid="); aheinecke@5606: url.append(uuid); aheinecke@5606: url.append("&maptype="); aheinecke@5606: url.append(mapType); aheinecke@5606: aheinecke@5606: appendPrintToUrl(collection, url); aheinecke@5606: tom@8844: return URL.encode(url.toString()); aheinecke@5606: } aheinecke@5606: aheinecke@5606: private void appendPrintToUrl(Collection collection, StringBuilder url) { aheinecke@5606: Settings settings = collection.getSettings("print-settings"); aheinecke@5606: if (settings != null) { aheinecke@5606: List properties = settings.getSettings("default"); aheinecke@5606: for (Property prop : properties) { aheinecke@5606: PropertySetting props = (PropertySetting)prop; aheinecke@5606: url.append("&"); aheinecke@5675: String localized; aheinecke@5675: try { aheinecke@5675: localized = MSG.getString(props.getName()); aheinecke@5675: } aheinecke@5675: catch (MissingResourceException mre) { aheinecke@5675: localized = props.getName(); aheinecke@5675: } tom@8844: url.append(localized); aheinecke@5606: url.append("="); tom@8844: url.append((String)props.getValue()); aheinecke@5606: } aheinecke@5606: } aheinecke@5675: // O.o aheinecke@5675: String river = findRiver(((MapOutputTab)mapToolbar.getOutputTab() aheinecke@5675: ).getCollectionView().getArtifact()); tom@8844: url.append("&" + MSG.getString(MAPFISH_RIVER) + "=" + river); aheinecke@5675: } aheinecke@5675: aheinecke@6379: // Copy of DatacageWindow's findRiver with added state for map.river aheinecke@5675: protected String findRiver(Artifact artifact) { aheinecke@5675: ArtifactDescription adescr = artifact.getArtifactDescription(); aheinecke@5675: DataList [] data = adescr.getOldData(); aheinecke@5675: aheinecke@5675: if (data != null && data.length > 0) { aheinecke@5675: for (int i = 0; i < data.length; i++) { aheinecke@5675: DataList dl = data[i]; aheinecke@6379: if (dl.getState().equals("state.winfo.river") || aheinecke@6379: dl.getState().equals("state.map.river")) { aheinecke@5675: for (int j = dl.size()-1; j >= 0; --j) { aheinecke@5675: Data d = dl.get(j); aheinecke@5675: DataItem [] di = d.getItems(); aheinecke@5675: if (di != null && di.length == 1) { aheinecke@5675: return d.getItems()[0].getStringValue(); aheinecke@5675: } aheinecke@5675: } aheinecke@5675: } aheinecke@5675: } aheinecke@5675: } aheinecke@5675: aheinecke@5675: return ""; aheinecke@5606: } aheinecke@5606: christian@4472: protected void updateCollection() { christian@4472: final Config config = Config.getInstance(); christian@4472: final String loc = config.getLocale(); christian@4472: aheinecke@5606: GWT.log("MapPrintPanel.updateCollection via RPC now"); christian@4472: christian@4472: List properties = new ArrayList(); tom@8856: properties.add(new PropertySetting( tom@8856: MAPFISH_MAPTITLE, pageTitle.getValueAsString())); tom@8856: properties.add(new PropertySetting( tom@8856: MAPFISH_SUBTITLE, pageSubtitle.getValueAsString())); tom@8856: properties.add(new PropertySetting( tom@8856: MAPFISH_RANGE, pageRange.getValueAsString())); tom@8856: properties.add(new PropertySetting( tom@8856: MAPFISH_STRETCH, pageStretch.getValueAsString())); tom@8856: properties.add(new PropertySetting( tom@8856: MAPFISH_INSTITUTION, pageInstitution.getValueAsString())); tom@8856: properties.add(new PropertySetting( tom@8856: MAPFISH_SOURCE, pageSource.getValueAsString())); tom@8856: properties.add(new PropertySetting( tom@8856: MAPFISH_CREATOR, pageCreator.getValueAsString())); tom@8856: properties.add(new PropertySetting( tom@8856: MAPFISH_DATEPLACE, pageDatePlace.getValueAsString())); tom@8856: properties.add(new PropertySetting( tom@8856: MAPFISH_LOGO, pageLogo.getValueAsString())); christian@4472: settings.setSettings("default", properties); christian@4472: christian@4472: collection.addSettings("print-settings", settings); christian@4472: updater.update(collection, loc, new AsyncCallback() { christian@4472: @Override christian@4472: public void onFailure(Throwable caught) { christian@4472: GWT.log("Could not update collection attributes."); christian@4472: SC.warn(MSG.getString(caught.getMessage())); christian@4472: } christian@4472: @Override christian@4472: public void onSuccess(Collection collection) { aheinecke@5606: GWT.log("MapPrint: collection attributes updated"); christian@4472: } christian@4472: }); christian@4472: } christian@4472: }