teichmann@5835: package org.dive4elements.river.client.client.ui.map; christian@4472: christian@4472: import com.google.gwt.core.client.GWT; christian@4472: import com.google.gwt.user.client.rpc.AsyncCallback; aheinecke@5606: import com.google.gwt.user.client.Window; christian@4472: christian@4472: import com.smartgwt.client.util.SC; christian@4472: import com.smartgwt.client.widgets.Canvas; christian@4472: import com.smartgwt.client.widgets.form.DynamicForm; aheinecke@5611: import com.smartgwt.client.widgets.form.fields.TextItem; christian@4472: import com.smartgwt.client.widgets.form.fields.ButtonItem; aheinecke@5717: import com.smartgwt.client.widgets.form.fields.SelectItem; christian@4472: import com.smartgwt.client.widgets.form.fields.events.ClickEvent; christian@4472: import com.smartgwt.client.widgets.form.fields.events.ClickHandler; aheinecke@5611: import com.smartgwt.client.types.Alignment; christian@4472: aheinecke@5606: import org.gwtopenmaps.openlayers.client.Bounds; aheinecke@5606: import org.gwtopenmaps.openlayers.client.Map; aheinecke@5606: 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; teichmann@5835: import org.dive4elements.river.client.shared.model.Collection; 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; teichmann@5835: import org.dive4elements.river.client.shared.model.Artifact; teichmann@5835: import org.dive4elements.river.client.shared.model.ArtifactDescription; teichmann@5835: import org.dive4elements.river.client.shared.model.Data; teichmann@5835: import org.dive4elements.river.client.shared.model.DataItem; teichmann@5835: import org.dive4elements.river.client.shared.model.DataList; aheinecke@5675: 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: 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"; aheinecke@5717: private static final String MAPFISH_INSTITUTION = "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: aheinecke@5606: public MapPrintPanel(Collection collection, MapToolbar mapToolbar, MapPrintWindow parent) { 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: aheinecke@5755: pageTitle.setLength(30); 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@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: aheinecke@5606: return 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: } aheinecke@5721: url.append(toJavaEncodedString(localized)); aheinecke@5606: url.append("="); aheinecke@5606: url.append(props.getValue()); aheinecke@5606: } aheinecke@5606: } aheinecke@5675: // O.o aheinecke@5675: String river = findRiver(((MapOutputTab)mapToolbar.getOutputTab() aheinecke@5675: ).getCollectionView().getArtifact()); aheinecke@5721: url.append("&" + toJavaEncodedString(MSG.getString(MAPFISH_RIVER)) + "=" + river); aheinecke@5675: } aheinecke@5675: aheinecke@5675: // Copy of DatacageWindow's findRiver 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@5675: if (dl.getState().equals("state.winfo.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: aheinecke@5714: public static String toJavaEncodedString(String str) { aheinecke@5714: if (str == null) { aheinecke@5714: return null; aheinecke@5714: } aheinecke@5714: StringBuilder sb = new StringBuilder(); aheinecke@5714: for (int i = 0, len = str.length(); i < len; i++) { aheinecke@5714: int unipoint = Character.codePointAt(str, i); aheinecke@5714: if ((unipoint < 32) || (unipoint > 127)) { aheinecke@5714: sb.append("\\u"); aheinecke@5714: sb.append(Integer.toHexString((unipoint >> 3*4) & 0xf)); aheinecke@5714: sb.append(Integer.toHexString((unipoint >> 2*4) & 0xf)); aheinecke@5714: sb.append(Integer.toHexString((unipoint >> 1*4) & 0xf)); aheinecke@5714: sb.append(Integer.toHexString((unipoint >> 0*4) & 0xf)); aheinecke@5714: } else { aheinecke@5714: sb.append(str.charAt(i)); aheinecke@5714: } aheinecke@5714: } aheinecke@5714: return sb.toString(); aheinecke@5714: } aheinecke@5714: 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(); aheinecke@5714: properties.add(new PropertySetting(MAPFISH_MAPTITLE, toJavaEncodedString(pageTitle.getValueAsString()))); aheinecke@5714: // properties.add(new PropertySetting(MAPFISH_LAYOUT, toJavaEncodedString(pageFormat.getValueAsString()))); aheinecke@5714: properties.add(new PropertySetting(MAPFISH_SUBTITLE, toJavaEncodedString(pageSubtitle.getValueAsString()))); aheinecke@5714: properties.add(new PropertySetting(MAPFISH_RANGE, toJavaEncodedString(pageRange.getValueAsString()))); aheinecke@5717: properties.add(new PropertySetting(MAPFISH_STRETCH, toJavaEncodedString(pageStretch.getValueAsString()))); aheinecke@5717: properties.add(new PropertySetting(MAPFISH_INSTITUTION, toJavaEncodedString(pageInstitution.getValueAsString()))); aheinecke@5714: properties.add(new PropertySetting(MAPFISH_SOURCE, toJavaEncodedString(pageSource.getValueAsString()))); aheinecke@5714: properties.add(new PropertySetting(MAPFISH_CREATOR, toJavaEncodedString(pageCreator.getValueAsString()))); aheinecke@5714: properties.add(new PropertySetting(MAPFISH_DATEPLACE, toJavaEncodedString(pageDatePlace.getValueAsString()))); aheinecke@5717: properties.add(new PropertySetting(MAPFISH_LOGO, toJavaEncodedString(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: }