comparison flys-client/src/main/java/de/intevation/flys/client/client/ui/chart/ChartThemePanel.java @ 805:f43d06d6a4a2

Refactored code of theme panel and added a MapThemePanel. flys-client/trunk@2366 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 20 Jul 2011 07:52:19 +0000
parents 374712890b94
children 67c678903280
comparison
equal deleted inserted replaced
804:374712890b94 805:f43d06d6a4a2
1 package de.intevation.flys.client.client.ui.chart; 1 package de.intevation.flys.client.client.ui.chart;
2 2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import com.google.gwt.core.client.GWT; 3 import com.google.gwt.core.client.GWT;
7 import com.google.gwt.user.client.rpc.AsyncCallback;
8 4
9 import com.smartgwt.client.types.ListGridFieldType; 5 import com.smartgwt.client.types.ListGridFieldType;
10 import com.smartgwt.client.util.SC;
11 import com.smartgwt.client.widgets.Canvas;
12 import com.smartgwt.client.widgets.grid.events.EditCompleteEvent;
13 import com.smartgwt.client.widgets.grid.events.EditCompleteHandler;
14 import com.smartgwt.client.widgets.grid.ListGrid;
15 import com.smartgwt.client.widgets.grid.ListGridField; 6 import com.smartgwt.client.widgets.grid.ListGridField;
16 import com.smartgwt.client.widgets.grid.ListGridRecord;
17 import com.smartgwt.client.widgets.layout.VLayout; 7 import com.smartgwt.client.widgets.layout.VLayout;
18 8
19 import de.intevation.flys.client.shared.model.Collection; 9 import de.intevation.flys.client.shared.model.Collection;
20 import de.intevation.flys.client.shared.model.FacetRecord; 10 import de.intevation.flys.client.shared.model.Theme;
21 import de.intevation.flys.client.shared.model.OutputMode; 11 import de.intevation.flys.client.shared.model.OutputMode;
22 import de.intevation.flys.client.shared.model.Theme;
23 import de.intevation.flys.client.shared.model.ThemeList;
24 12
25 import de.intevation.flys.client.client.Config;
26 import de.intevation.flys.client.client.FLYSConstants; 13 import de.intevation.flys.client.client.FLYSConstants;
27 import de.intevation.flys.client.client.event.HasOutputParameterChangeHandlers; 14 import de.intevation.flys.client.client.ui.ThemePanel;
28 import de.intevation.flys.client.client.event.OnMoveEvent;
29 import de.intevation.flys.client.client.event.OnMoveHandler;
30 import de.intevation.flys.client.client.event.OutputParameterChangeEvent;
31 import de.intevation.flys.client.client.event.OutputParameterChangeHandler;
32 import de.intevation.flys.client.client.services.CollectionAttributeService;
33 import de.intevation.flys.client.client.services.CollectionAttributeServiceAsync;
34 15
35 16
36 /** 17 /**
37 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> 18 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
38 */ 19 */
39 public class ChartThemePanel 20 public class ChartThemePanel extends ThemePanel {
40 extends Canvas 21
41 implements EditCompleteHandler, OnMoveHandler,
42 HasOutputParameterChangeHandlers
43 {
44 /** The interface that provides i18n messages. */ 22 /** The interface that provides i18n messages. */
45 private FLYSConstants MSG = GWT.create(FLYSConstants.class); 23 private FLYSConstants MSG = GWT.create(FLYSConstants.class);
46
47
48 /** The service that is used to modify collection attributes.*/
49 protected CollectionAttributeServiceAsync updater =
50 GWT.create(CollectionAttributeService.class);
51 24
52 25
53 public static final String GRID_FIELD_ACTIVE = "active"; 26 public static final String GRID_FIELD_ACTIVE = "active";
54 public static final String GRID_FIELD_NAME = "name"; 27 public static final String GRID_FIELD_NAME = "name";
55 28
56 29
57 protected Collection collection;
58
59 protected OutputMode mode;
60
61 protected List<OutputParameterChangeHandler> outHandlers;
62
63 protected ListGrid list;
64
65 protected ChartOutputTab chartOut;
66
67 protected ThemeNavigationPanel navigation;
68
69
70
71 public ChartThemePanel(Collection collection, OutputMode mode) { 30 public ChartThemePanel(Collection collection, OutputMode mode) {
72 this.collection = collection; 31 super(collection, mode);
73 this.mode = mode;
74 this.outHandlers = new ArrayList<OutputParameterChangeHandler>();
75 this.chartOut = chartOut;
76 this.list = new ListGrid();
77 this.navigation = new ThemeNavigationPanel();
78 this.navigation.addOnMoveHandler(this);
79 32
80 initGrid(); 33 initGrid();
81 initLayout(); 34 initLayout();
82 35
83 updateGrid(); 36 updateGrid();
126 79
127 list.setFields(active, name); 80 list.setFields(active, name);
128 } 81 }
129 82
130 83
131 /** 84 @Override
132 * Replace the current collection with a new one. <b>NOTE: this operation 85 public void activateTheme(Theme theme, boolean active) {
133 * triggers updateGrid() which modifies the themes in the grid.</b> 86 theme.setActive(active ? 1 : 0);
134 *
135 * @param collection The new collection object.
136 */
137 protected void setCollection(Collection collection) {
138 this.collection = collection;
139
140 updateGrid();
141 }
142
143
144 /**
145 * Registers a new OutputParameterChangeHandler.
146 *
147 * @param h The new handler.
148 */
149 public void addOutputParameterChangeHandler(OutputParameterChangeHandler h){
150 if (h != null) {
151 outHandlers.add(h);
152 }
153 }
154
155
156 /**
157 * Returns the ThemeList of the current collection and output mode.
158 *
159 * @return the current ThemeList.
160 */
161 public ThemeList getThemeList() {
162 return collection.getThemeList(mode.getName());
163 }
164
165
166 /**
167 * A method that removes all records from theme grid.
168 */
169 protected void clearGrid() {
170 ListGridRecord[] records = list.getRecords();
171
172 if (records == null || records.length == 0) {
173 return;
174 }
175
176 for (ListGridRecord record: records) {
177 list.removeData(record);
178 }
179 }
180
181
182 /**
183 * This method is used to clear the current theme grid and add new updated
184 * data.
185 */
186 protected void updateGrid() {
187 GWT.log("ChartThemePanel.updateGrid");
188
189 clearGrid();
190
191 ThemeList themeList = getThemeList();
192
193 if (themeList == null) {
194 GWT.log("ERROR: No theme list.");
195 return;
196 }
197
198 int count = themeList.getThemeCount();
199
200 for (int i = 1; i <= count; i++) {
201 Theme theme = themeList.getThemeAt(i);
202
203 if (theme == null) {
204 continue;
205 }
206
207 list.addData(new FacetRecord(theme));
208 }
209
210 fireOutputParameterChanged();
211 }
212
213
214 /**
215 * Called when the attribution of a chart changed. It informs the registered
216 * handlers about the changes.
217 */
218 protected void fireOutputParameterChanged() {
219 OutputParameterChangeEvent evt = new OutputParameterChangeEvent();
220
221 for (OutputParameterChangeHandler handler: outHandlers) {
222 handler.onOutputParameterChanged(evt);
223 }
224 }
225
226
227 /**
228 * This method is called after a cell in the theme grid has been modified.
229 *
230 * @param event The event that stores information about the modified record.
231 */
232 public void onEditComplete(EditCompleteEvent event) {
233 GWT.log("Edited record.");
234
235 int row = event.getRowNum();
236 FacetRecord rec = (FacetRecord) list.getRecord(row);
237
238 Theme theme = rec.getTheme();
239 theme.setActive(rec.getActive() ? 1 : 0);
240
241 updateCollection();
242 }
243
244
245 /**
246 * This method triggers the CollectionAttributeService. Based on the current
247 * collectin settings, the attribute of the collection is modified or not.
248 * But in every case, we will get a new collection object - which might be
249 * the same as the current one.
250 */
251 public void updateCollection() {
252 final Config config = Config.getInstance();
253 final String url = config.getServerUrl();
254 final String loc = config.getLocale();
255
256 GWT.log("ChartThemePanel.updateCollection via RPC now");
257
258 // don't forget to enable the panel after the request has finished!
259 disable();
260
261 updater.update(collection, url, loc, new AsyncCallback<Collection>() {
262 public void onFailure(Throwable caught) {
263 GWT.log("Could not update collection attributes.");
264 SC.warn(MSG.getString(caught.getMessage()));
265
266 enable();
267 }
268
269
270 public void onSuccess(Collection collection) {
271 setCollection(collection);
272
273 enable();
274 }
275 });
276 }
277
278
279 public void onMove(OnMoveEvent event) {
280 int type = event.getType();
281
282 GWT.log("ChartThemePanel.onMove: " + type);
283
284 ListGridRecord[] records = list.getSelection();
285
286 if (records == null || records.length == 0) {
287 GWT.log("ChartThemePanel.onMove: No records selected.");
288 return;
289 }
290
291 switch (type) {
292 case 0: moveRecordsTop(records); break;
293 case 1: moveRecordsUp(records); break;
294 case 2: moveRecordsDown(records); break;
295 case 3: moveRecordsBottom(records); break;
296 }
297
298 updateCollection();
299 }
300
301
302 /**
303 * Moves the selected grid records (themes) to the top of the grid.
304 *
305 * @param records The selected themes in the list. Null not permitted.
306 */
307 protected void moveRecordsTop(ListGridRecord[] records) {
308 ThemeList themeList = getThemeList();
309
310 int idx = 1;
311
312 for (ListGridRecord record: records) {
313 Theme theme = ((FacetRecord) record).getTheme();
314 themeList.setThemePosition(theme, idx++);
315 }
316
317 updateGrid();
318 }
319
320
321 /**
322 * Moves the selected grid records (themes) one step up.
323 *
324 * @param records The selected themes in the list. Null not permitted.
325 */
326 protected void moveRecordsUp(ListGridRecord[] records) {
327 ThemeList themeList = getThemeList();
328
329 int[] newPos = new int[records.length];
330
331 for (int i = 0; i < records.length ; i++) {
332 Theme theme = ((FacetRecord) records[i]).getTheme();
333 newPos[i] = theme.getPosition() - 1;
334 }
335
336 for (int i = 0; i < records.length ; i++) {
337 Theme theme = ((FacetRecord) records[i]).getTheme();
338 themeList.setThemePosition(theme, newPos[i]);
339 }
340
341 updateGrid();
342 }
343
344
345 /**
346 * Moves the selected grid records (themes) one step down.
347 *
348 * @param records The selected themes in the list. Null not permitted.
349 */
350 protected void moveRecordsDown(ListGridRecord[] records) {
351 ThemeList themeList = getThemeList();
352
353 int[] newPos = new int[records.length];
354
355 for (int i = records.length-1; i >= 0; i--) {
356 Theme theme = ((FacetRecord) records[i]).getTheme();
357 newPos[i] = theme.getPosition()+1;
358 }
359
360 for (int i = records.length-1; i >= 0; i--) {
361 Theme theme = ((FacetRecord) records[i]).getTheme();
362 themeList.setThemePosition(theme, newPos[i]);
363 }
364
365 updateGrid();
366 }
367
368
369 /**
370 * Moves the selected grid records (themes) to the bottom of the grid.
371 *
372 * @param records The selected themes in the list. Null not permitted.
373 */
374 protected void moveRecordsBottom(ListGridRecord[] records) {
375 ThemeList themeList = getThemeList();
376
377 int idx = themeList.getThemeCount();
378
379 for (int i = records.length-1; i >= 0; i--) {
380 Theme theme = ((FacetRecord) records[i]).getTheme();
381 themeList.setThemePosition(theme, idx--);
382 }
383
384 updateGrid();
385 } 87 }
386 } 88 }
387 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : 89 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org