Mercurial > dive4elements > river
comparison gwt-client/src/main/java/org/dive4elements/river/client/client/ui/map/MapPrintPanel.java @ 5838:5aa05a7a34b7
Rename modules to more fitting names.
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Thu, 25 Apr 2013 15:23:37 +0200 |
parents | flys-client/src/main/java/org/dive4elements/river/client/client/ui/map/MapPrintPanel.java@821a02bbfb4e |
children | 172338b1407f |
comparison
equal
deleted
inserted
replaced
5837:d9901a08d0a6 | 5838:5aa05a7a34b7 |
---|---|
1 package org.dive4elements.river.client.client.ui.map; | |
2 | |
3 import com.google.gwt.core.client.GWT; | |
4 import com.google.gwt.user.client.rpc.AsyncCallback; | |
5 import com.google.gwt.user.client.Window; | |
6 | |
7 import com.smartgwt.client.util.SC; | |
8 import com.smartgwt.client.widgets.Canvas; | |
9 import com.smartgwt.client.widgets.form.DynamicForm; | |
10 import com.smartgwt.client.widgets.form.fields.TextItem; | |
11 import com.smartgwt.client.widgets.form.fields.ButtonItem; | |
12 import com.smartgwt.client.widgets.form.fields.SelectItem; | |
13 import com.smartgwt.client.widgets.form.fields.events.ClickEvent; | |
14 import com.smartgwt.client.widgets.form.fields.events.ClickHandler; | |
15 import com.smartgwt.client.types.Alignment; | |
16 | |
17 import org.gwtopenmaps.openlayers.client.Bounds; | |
18 import org.gwtopenmaps.openlayers.client.Map; | |
19 | |
20 import org.dive4elements.river.client.client.Config; | |
21 import org.dive4elements.river.client.client.FLYSConstants; | |
22 import org.dive4elements.river.client.client.services.CollectionAttributeService; | |
23 import org.dive4elements.river.client.client.services.CollectionAttributeServiceAsync; | |
24 import org.dive4elements.river.client.shared.model.Collection; | |
25 import org.dive4elements.river.client.shared.model.OutputSettings; | |
26 import org.dive4elements.river.client.shared.model.Property; | |
27 import org.dive4elements.river.client.shared.model.PropertySetting; | |
28 import org.dive4elements.river.client.shared.model.Settings; | |
29 import org.dive4elements.river.client.shared.model.Artifact; | |
30 import org.dive4elements.river.client.shared.model.ArtifactDescription; | |
31 import org.dive4elements.river.client.shared.model.Data; | |
32 import org.dive4elements.river.client.shared.model.DataItem; | |
33 import org.dive4elements.river.client.shared.model.DataList; | |
34 | |
35 | |
36 import java.util.ArrayList; | |
37 import java.util.LinkedHashMap; | |
38 import java.util.List; | |
39 import java.util.MissingResourceException; | |
40 | |
41 public class MapPrintPanel extends Canvas { | |
42 | |
43 private static final String MAPFISH_MAPTITLE = "mapfish_mapTitle"; | |
44 private static final String MAPFISH_RANGE = "mapfish_data_range"; | |
45 private static final String MAPFISH_SUBTITLE = "mapfish_data_subtitle"; | |
46 private static final String MAPFISH_STRETCH = "mapfish_data_strech"; | |
47 private static final String MAPFISH_INSTITUTION = "mapfish_data_institution"; | |
48 private static final String MAPFISH_SOURCE = "mapfish_data_source"; | |
49 private static final String MAPFISH_CREATOR = "mapfish_data_creator"; | |
50 private static final String MAPFISH_DATEPLACE = "mapfish_data_dateplace"; | |
51 private static final String MAPFISH_RIVER = "mapfish_data_river"; | |
52 private static final String MAPFISH_LOGO = "mapfish_logo"; | |
53 | |
54 protected FLYSConstants MSG = GWT.create(FLYSConstants.class); | |
55 | |
56 /** CollectionAttribute Update Service. */ | |
57 protected CollectionAttributeServiceAsync updater = | |
58 GWT.create(CollectionAttributeService.class); | |
59 | |
60 | |
61 protected Collection collection; | |
62 protected Settings settings; | |
63 protected TextItem pageTitle = new TextItem(); | |
64 protected TextItem pageRange = new TextItem(); | |
65 protected TextItem pageSubtitle = new TextItem(); | |
66 protected TextItem pageStretch = new TextItem(); | |
67 protected TextItem pageInstitution = new TextItem(); | |
68 protected TextItem pageSource = new TextItem(); | |
69 protected TextItem pageCreator = new TextItem(); | |
70 protected TextItem pageDatePlace = new TextItem(); | |
71 protected SelectItem pageLogo = createPageLogoSelectItem(); | |
72 // protected SelectItem pageFormat = createPageFormatSelectItem(); | |
73 protected MapToolbar mapToolbar; | |
74 protected MapPrintWindow parent; | |
75 | |
76 public MapPrintPanel(Collection collection, MapToolbar mapToolbar, MapPrintWindow parent) { | |
77 this.collection = collection; | |
78 this.mapToolbar = mapToolbar; | |
79 this.parent = parent; | |
80 initLayout(); | |
81 | |
82 this.settings = collection.getSettings("print-settings"); | |
83 if (settings == null) { | |
84 settings = new OutputSettings(); | |
85 GWT.log("settings are empty"); | |
86 } | |
87 else { | |
88 List<Property> properties = settings.getSettings("default"); | |
89 for (Property prop : properties) { | |
90 PropertySetting props = (PropertySetting)prop; | |
91 GWT.log(props.getName() + "=" + props.getValue()); | |
92 if (props.getName().equals(MAPFISH_MAPTITLE)) { | |
93 this.pageTitle.setValue(props.getValue()); | |
94 } | |
95 else if (props.getName().equals(MAPFISH_RANGE)) { | |
96 this.pageRange.setValue(props.getValue()); | |
97 } | |
98 else if (props.getName().equals(MAPFISH_SUBTITLE)) { | |
99 this.pageSubtitle.setValue(props.getValue()); | |
100 } | |
101 else if (props.getName().equals(MAPFISH_STRETCH)) { | |
102 this.pageStretch.setValue(props.getValue()); | |
103 } | |
104 else if (props.getName().equals(MAPFISH_INSTITUTION)) { | |
105 this.pageInstitution.setValue(props.getValue()); | |
106 } | |
107 else if (props.getName().equals(MAPFISH_SOURCE)) { | |
108 this.pageSource.setValue(props.getValue()); | |
109 } | |
110 else if (props.getName().equals(MAPFISH_CREATOR)) { | |
111 this.pageCreator.setValue(props.getValue()); | |
112 } | |
113 else if (props.getName().equals(MAPFISH_DATEPLACE)) { | |
114 this.pageDatePlace.setValue(props.getValue()); | |
115 } else { | |
116 GWT.log("Unknown Print property: " + prop.getName()); | |
117 } | |
118 } | |
119 } | |
120 } | |
121 | |
122 protected void initLayout() { | |
123 // TODO: i18n | |
124 this.pageTitle.setTitle(MSG.mapTitle()); | |
125 this.pageSubtitle.setTitle(MSG.mapSubtitle()); | |
126 this.pageRange.setTitle(MSG.mapRange()); | |
127 this.pageStretch.setTitle(MSG.mapStretch()); | |
128 this.pageInstitution.setTitle(MSG.mapInstitution()); | |
129 this.pageSource.setTitle(MSG.mapSource()); | |
130 this.pageCreator.setTitle(MSG.mapCreator()); | |
131 this.pageDatePlace.setTitle(MSG.mapDate()); | |
132 | |
133 pageTitle.setLength(30); | |
134 pageSubtitle.setLength(30); | |
135 pageRange.setLength(30); | |
136 pageStretch.setLength(30); | |
137 pageInstitution.setLength(30); | |
138 pageSource.setLength(30); | |
139 pageCreator.setLength(30); | |
140 pageDatePlace.setLength(30); | |
141 ButtonItem printButton = createPrintButtonItem(); | |
142 | |
143 printButton.setAlign(Alignment.RIGHT); | |
144 | |
145 DynamicForm df = new DynamicForm(); | |
146 df.setFields( | |
147 // this.pageFormat, | |
148 this.pageTitle, | |
149 this.pageSubtitle, | |
150 this.pageRange, | |
151 this.pageStretch, | |
152 this.pageInstitution, | |
153 this.pageSource, | |
154 this.pageCreator, | |
155 this.pageDatePlace, | |
156 this.pageLogo, | |
157 printButton); | |
158 addChild(df); | |
159 } | |
160 | |
161 protected SelectItem createPageLogoSelectItem() { | |
162 LinkedHashMap values = new LinkedHashMap(); | |
163 // TODO: this should be configurable | |
164 values.put(MSG.bfgLogo(), "BfG Logo"); | |
165 | |
166 SelectItem selItem = new SelectItem(); | |
167 selItem.setTitle(MSG.mapLogo()); | |
168 selItem.setValueMap(values); | |
169 selItem.setDefaultToFirstOption(true); | |
170 | |
171 return selItem; | |
172 } | |
173 | |
174 /* | |
175 * Commented out because we only provide a layout for A4 Landscape atm | |
176 | |
177 protected SelectItem createPageFormatSelectItem() { | |
178 LinkedHashMap values = new LinkedHashMap(); | |
179 // TODO: i18n | |
180 values.put("A4 landscape", "DIN A4 (Querformat)"); | |
181 //values.put("A4 portrait", "DIN A4 (Hochformat)"); | |
182 //values.put("A0 portrait", "DIN A0 (Hochformat)"); | |
183 | |
184 SelectItem selItem = new SelectItem(); | |
185 selItem.setTitle("Seitengröße:"); // TODO: i18n | |
186 selItem.setValueMap(values); | |
187 selItem.setDefaultToFirstOption(true); | |
188 | |
189 return selItem; | |
190 } | |
191 */ | |
192 protected ButtonItem createPrintButtonItem() { | |
193 ButtonItem btn = new ButtonItem(); | |
194 btn.addClickHandler(new ClickHandler() { | |
195 | |
196 public void onClick(ClickEvent event) { | |
197 updateCollection(); | |
198 Window.open(createPrintUrl(), "_blank", ""); | |
199 parent.destroy(); | |
200 } | |
201 }); | |
202 btn.setTitle(MSG.print()); | |
203 return btn; | |
204 } | |
205 | |
206 private String createPrintUrl() { | |
207 MapOutputTab ot = (MapOutputTab)mapToolbar.getOutputTab(); | |
208 Collection collection = ot.getCollection(); | |
209 String uuid = collection.identifier(); | |
210 | |
211 String mapType = collection.getOutputModes().containsKey("floodmap") | |
212 ? "floodmap" | |
213 : "map"; | |
214 | |
215 StringBuilder url = new StringBuilder(); | |
216 url.append(GWT.getModuleBaseURL()); | |
217 url.append("map-print?"); | |
218 | |
219 Map map = mapToolbar.getMap(); | |
220 Bounds bounds = map.getExtent(); | |
221 | |
222 if (bounds != null) { | |
223 try { | |
224 url.append("minx="); | |
225 url.append(bounds.getLowerLeftX()); | |
226 url.append("&"); | |
227 | |
228 url.append("maxx="); | |
229 url.append(bounds.getUpperRightX()); | |
230 url.append("&"); | |
231 | |
232 url.append("miny="); | |
233 url.append(bounds.getLowerLeftY()); | |
234 url.append("&"); | |
235 | |
236 url.append("maxy="); | |
237 url.append(bounds.getUpperRightY()); | |
238 url.append("&"); | |
239 } | |
240 catch (Exception e) { | |
241 // XXX: Ignore it. bounds.getXXX() throw | |
242 // exceptions when bound is invalid. :-/ | |
243 } | |
244 } | |
245 | |
246 url.append("uuid="); | |
247 url.append(uuid); | |
248 url.append("&maptype="); | |
249 url.append(mapType); | |
250 | |
251 appendPrintToUrl(collection, url); | |
252 | |
253 return url.toString(); | |
254 } | |
255 | |
256 private void appendPrintToUrl(Collection collection, StringBuilder url) { | |
257 Settings settings = collection.getSettings("print-settings"); | |
258 if (settings != null) { | |
259 List<Property> properties = settings.getSettings("default"); | |
260 for (Property prop : properties) { | |
261 PropertySetting props = (PropertySetting)prop; | |
262 url.append("&"); | |
263 String localized; | |
264 try { | |
265 localized = MSG.getString(props.getName()); | |
266 } | |
267 catch (MissingResourceException mre) { | |
268 localized = props.getName(); | |
269 } | |
270 url.append(toJavaEncodedString(localized)); | |
271 url.append("="); | |
272 url.append(props.getValue()); | |
273 } | |
274 } | |
275 // O.o | |
276 String river = findRiver(((MapOutputTab)mapToolbar.getOutputTab() | |
277 ).getCollectionView().getArtifact()); | |
278 url.append("&" + toJavaEncodedString(MSG.getString(MAPFISH_RIVER)) + "=" + river); | |
279 } | |
280 | |
281 // Copy of DatacageWindow's findRiver | |
282 protected String findRiver(Artifact artifact) { | |
283 ArtifactDescription adescr = artifact.getArtifactDescription(); | |
284 DataList [] data = adescr.getOldData(); | |
285 | |
286 if (data != null && data.length > 0) { | |
287 for (int i = 0; i < data.length; i++) { | |
288 DataList dl = data[i]; | |
289 if (dl.getState().equals("state.winfo.river")) { | |
290 for (int j = dl.size()-1; j >= 0; --j) { | |
291 Data d = dl.get(j); | |
292 DataItem [] di = d.getItems(); | |
293 if (di != null && di.length == 1) { | |
294 return d.getItems()[0].getStringValue(); | |
295 } | |
296 } | |
297 } | |
298 } | |
299 } | |
300 | |
301 return ""; | |
302 } | |
303 | |
304 public static String toJavaEncodedString(String str) { | |
305 if (str == null) { | |
306 return null; | |
307 } | |
308 StringBuilder sb = new StringBuilder(); | |
309 for (int i = 0, len = str.length(); i < len; i++) { | |
310 int unipoint = Character.codePointAt(str, i); | |
311 if ((unipoint < 32) || (unipoint > 127)) { | |
312 sb.append("\\u"); | |
313 sb.append(Integer.toHexString((unipoint >> 3*4) & 0xf)); | |
314 sb.append(Integer.toHexString((unipoint >> 2*4) & 0xf)); | |
315 sb.append(Integer.toHexString((unipoint >> 1*4) & 0xf)); | |
316 sb.append(Integer.toHexString((unipoint >> 0*4) & 0xf)); | |
317 } else { | |
318 sb.append(str.charAt(i)); | |
319 } | |
320 } | |
321 return sb.toString(); | |
322 } | |
323 | |
324 protected void updateCollection() { | |
325 final Config config = Config.getInstance(); | |
326 final String loc = config.getLocale(); | |
327 | |
328 GWT.log("MapPrintPanel.updateCollection via RPC now"); | |
329 | |
330 List<Property> properties = new ArrayList<Property>(); | |
331 properties.add(new PropertySetting(MAPFISH_MAPTITLE, toJavaEncodedString(pageTitle.getValueAsString()))); | |
332 // properties.add(new PropertySetting(MAPFISH_LAYOUT, toJavaEncodedString(pageFormat.getValueAsString()))); | |
333 properties.add(new PropertySetting(MAPFISH_SUBTITLE, toJavaEncodedString(pageSubtitle.getValueAsString()))); | |
334 properties.add(new PropertySetting(MAPFISH_RANGE, toJavaEncodedString(pageRange.getValueAsString()))); | |
335 properties.add(new PropertySetting(MAPFISH_STRETCH, toJavaEncodedString(pageStretch.getValueAsString()))); | |
336 properties.add(new PropertySetting(MAPFISH_INSTITUTION, toJavaEncodedString(pageInstitution.getValueAsString()))); | |
337 properties.add(new PropertySetting(MAPFISH_SOURCE, toJavaEncodedString(pageSource.getValueAsString()))); | |
338 properties.add(new PropertySetting(MAPFISH_CREATOR, toJavaEncodedString(pageCreator.getValueAsString()))); | |
339 properties.add(new PropertySetting(MAPFISH_DATEPLACE, toJavaEncodedString(pageDatePlace.getValueAsString()))); | |
340 properties.add(new PropertySetting(MAPFISH_LOGO, toJavaEncodedString(pageLogo.getValueAsString()))); | |
341 settings.setSettings("default", properties); | |
342 | |
343 collection.addSettings("print-settings", settings); | |
344 updater.update(collection, loc, new AsyncCallback<Collection>() { | |
345 @Override | |
346 public void onFailure(Throwable caught) { | |
347 GWT.log("Could not update collection attributes."); | |
348 SC.warn(MSG.getString(caught.getMessage())); | |
349 } | |
350 @Override | |
351 public void onSuccess(Collection collection) { | |
352 GWT.log("MapPrint: collection attributes updated"); | |
353 } | |
354 }); | |
355 } | |
356 } |