christian@4472: package de.intevation.flys.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; 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: christian@4472: import de.intevation.flys.client.client.Config; christian@4472: import de.intevation.flys.client.client.FLYSConstants; christian@4472: import de.intevation.flys.client.client.services.CollectionAttributeService; christian@4472: import de.intevation.flys.client.client.services.CollectionAttributeServiceAsync; christian@4472: import de.intevation.flys.client.shared.model.Collection; christian@4472: import de.intevation.flys.client.shared.model.OutputSettings; christian@4472: import de.intevation.flys.client.shared.model.Property; christian@4472: import de.intevation.flys.client.shared.model.PropertySetting; christian@4472: import de.intevation.flys.client.shared.model.Settings; aheinecke@5675: import de.intevation.flys.client.shared.model.Artifact; aheinecke@5675: import de.intevation.flys.client.shared.model.ArtifactDescription; aheinecke@5675: import de.intevation.flys.client.shared.model.Data; aheinecke@5675: import de.intevation.flys.client.shared.model.DataItem; aheinecke@5675: import de.intevation.flys.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@5675: private static final String MAPFISH_CREATED = "mapfish_data_created"; 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"; 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@5606: protected TextItem pageStrech = new TextItem(); aheinecke@5606: protected TextItem pageCreated = new TextItem(); aheinecke@5606: protected TextItem pageSource = new TextItem(); aheinecke@5606: protected TextItem pageCreator = new TextItem(); aheinecke@5606: protected TextItem pageDatePlace = new TextItem(); 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@5606: this.pageStrech.setValue(props.getValue()); aheinecke@5606: } aheinecke@5606: else if (props.getName().equals(MAPFISH_CREATED)) { aheinecke@5606: this.pageCreated.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@5606: this.pageTitle.setTitle("Titel"); aheinecke@5606: this.pageSubtitle.setTitle("Untertitel"); aheinecke@5606: this.pageRange.setTitle("Bereich"); aheinecke@5606: this.pageStrech.setTitle("Strecke"); aheinecke@5606: this.pageCreated.setTitle("Aufgestellt"); aheinecke@5606: this.pageSource.setTitle("Datenquelle"); aheinecke@5606: this.pageCreator.setTitle("Ersteller"); aheinecke@5606: this.pageDatePlace.setTitle("Ort, Datum"); christian@4472: aheinecke@5675: pageTitle.setLength(21); aheinecke@5675: pageSubtitle.setLength(21); aheinecke@5675: pageRange.setLength(21); aheinecke@5675: pageStrech.setLength(21); aheinecke@5675: pageCreated.setLength(21); aheinecke@5675: pageSource.setLength(21); aheinecke@5675: pageCreator.setLength(21); aheinecke@5675: pageDatePlace.setLength(21); 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@5606: this.pageStrech, aheinecke@5606: this.pageCreated, aheinecke@5606: this.pageSource, aheinecke@5606: this.pageCreator, aheinecke@5606: this.pageDatePlace, aheinecke@5611: printButton); christian@4472: addChild(df); christian@4472: } christian@4472: 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(); aheinecke@5606: GWT.log("Button created"); christian@4472: btn.addClickHandler(new ClickHandler() { christian@4472: christian@4472: public void onClick(ClickEvent event) { aheinecke@5606: GWT.log("Print Button Click"); 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@5675: url.append(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@5675: url.append("&" + 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@5714: properties.add(new PropertySetting(MAPFISH_STRETCH, toJavaEncodedString(pageStrech.getValueAsString()))); aheinecke@5714: properties.add(new PropertySetting(MAPFISH_CREATED, toJavaEncodedString(pageCreated.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()))); 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: }