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; christian@4472: christian@4472: import java.util.ArrayList; christian@4472: import java.util.LinkedHashMap; christian@4472: import java.util.List; christian@4472: aheinecke@5606: public class MapPrintPanel extends Canvas { christian@4472: aheinecke@5606: private static final String MAPFISH_MAPTITLE = "mapfish-mapTitle"; aheinecke@5606: private static final String MAPFISH_RANGE = "mapfish-range"; aheinecke@5606: private static final String MAPFISH_SUBTITLE = "mapfish-subtitle"; aheinecke@5606: private static final String MAPFISH_STRETCH = "mapfish-strech"; aheinecke@5606: private static final String MAPFISH_CREATED = "mapfish-created"; aheinecke@5606: private static final String MAPFISH_SOURCE = "mapfish-source"; aheinecke@5606: private static final String MAPFISH_CREATOR = "mapfish-creator"; aheinecke@5606: private static final String MAPFISH_DATEPLACE = "mapfish-dateplace"; 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@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@5606: url.append(props.getName()); aheinecke@5606: url.append("="); aheinecke@5606: url.append(props.getValue()); aheinecke@5606: } aheinecke@5606: } 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(); christian@4474: properties.add(new PropertySetting(MAPFISH_MAPTITLE, this.pageTitle.getValueAsString())); aheinecke@5606: // properties.add(new PropertySetting(MAPFISH_LAYOUT, this.pageFormat.getValueAsString())); aheinecke@5606: properties.add(new PropertySetting(MAPFISH_SUBTITLE, this.pageSubtitle.getValueAsString())); aheinecke@5606: properties.add(new PropertySetting(MAPFISH_RANGE, this.pageRange.getValueAsString())); aheinecke@5606: properties.add(new PropertySetting(MAPFISH_STRETCH, this.pageStrech.getValueAsString())); aheinecke@5606: properties.add(new PropertySetting(MAPFISH_CREATED, this.pageCreated.getValueAsString())); aheinecke@5606: properties.add(new PropertySetting(MAPFISH_SOURCE, this.pageSource.getValueAsString())); aheinecke@5606: properties.add(new PropertySetting(MAPFISH_CREATOR, this.pageCreator.getValueAsString())); aheinecke@5606: properties.add(new PropertySetting(MAPFISH_DATEPLACE, this.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: }