comparison flys-client/src/main/java/de/intevation/flys/client/client/ui/ThemePanel.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
children 67c678903280
comparison
equal deleted inserted replaced
804:374712890b94 805:f43d06d6a4a2
1 package de.intevation.flys.client.client.ui;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import com.google.gwt.core.client.GWT;
7 import com.google.gwt.user.client.rpc.AsyncCallback;
8
9 import com.smartgwt.client.util.SC;
10 import com.smartgwt.client.widgets.Canvas;
11 import com.smartgwt.client.widgets.grid.ListGrid;
12 import com.smartgwt.client.widgets.grid.ListGridRecord;
13 import com.smartgwt.client.widgets.grid.events.EditCompleteEvent;
14 import com.smartgwt.client.widgets.grid.events.EditCompleteHandler;
15
16 import de.intevation.flys.client.shared.model.Collection;
17 import de.intevation.flys.client.shared.model.FacetRecord;
18 import de.intevation.flys.client.shared.model.OutputMode;
19 import de.intevation.flys.client.shared.model.Theme;
20 import de.intevation.flys.client.shared.model.ThemeList;
21
22 import de.intevation.flys.client.client.Config;
23 import de.intevation.flys.client.client.FLYSConstants;
24 import de.intevation.flys.client.client.event.HasOutputParameterChangeHandlers;
25 import de.intevation.flys.client.client.event.OnMoveEvent;
26 import de.intevation.flys.client.client.event.OnMoveHandler;
27 import de.intevation.flys.client.client.event.OutputParameterChangeEvent;
28 import de.intevation.flys.client.client.event.OutputParameterChangeHandler;
29 import de.intevation.flys.client.client.services.CollectionAttributeService;
30 import de.intevation.flys.client.client.services.CollectionAttributeServiceAsync;
31
32
33 public abstract class ThemePanel
34 extends Canvas
35 implements OnMoveHandler, EditCompleteHandler, HasOutputParameterChangeHandlers
36 {
37 protected CollectionAttributeServiceAsync updater =
38 GWT.create(CollectionAttributeService.class);
39
40 private FLYSConstants MSG = GWT.create(FLYSConstants.class);
41
42 protected List<OutputParameterChangeHandler> outHandlers;
43
44 protected Collection collection;
45 protected OutputMode mode;
46
47 protected ThemeNavigationPanel navigation;
48 protected ListGrid list;
49
50
51 public ThemePanel(Collection collection, OutputMode mode) {
52 this.collection = collection;
53 this.mode = mode;
54 this.list = new ListGrid();
55 this.outHandlers = new ArrayList<OutputParameterChangeHandler>();
56 this.navigation = new ThemeNavigationPanel();
57
58 this.navigation.addOnMoveHandler(this);
59 }
60
61
62 public abstract void activateTheme(Theme theme, boolean active);
63
64
65 /**
66 * Replace the current collection with a new one. <b>NOTE: this operation
67 * triggers updateGrid() which modifies the themes in the grid.</b>
68 *
69 * @param collection The new collection object.
70 */
71 protected void setCollection(Collection collection) {
72 this.collection = collection;
73
74 updateGrid();
75 }
76
77
78 /**
79 * Returns the ThemeList of the current collection and output mode.
80 *
81 * @return the current ThemeList.
82 */
83 public ThemeList getThemeList() {
84 return collection.getThemeList(mode.getName());
85 }
86
87
88 /**
89 * Registers a new OutputParameterChangeHandler.
90 *
91 * @param h The new handler.
92 */
93 public void addOutputParameterChangeHandler(OutputParameterChangeHandler h){
94 if (h != null) {
95 outHandlers.add(h);
96 }
97 }
98
99
100 /**
101 * Called when the attribution of an output changed. It informs the
102 * registered handlers about the changes.
103 */
104 protected void fireOutputParameterChanged() {
105 OutputParameterChangeEvent evt = new OutputParameterChangeEvent();
106
107 for (OutputParameterChangeHandler handler: outHandlers) {
108 handler.onOutputParameterChanged(evt);
109 }
110 }
111
112
113 /**
114 * This method is used to clear the current theme grid and add new updated
115 * data.
116 */
117 protected void updateGrid() {
118 GWT.log("ThemePanel.updateGrid");
119
120 clearGrid();
121
122 ThemeList themeList = getThemeList();
123
124 if (themeList == null) {
125 GWT.log("ERROR: No theme list.");
126 return;
127 }
128
129 int count = themeList.getThemeCount();
130
131 for (int i = 1; i <= count; i++) {
132 Theme theme = themeList.getThemeAt(i);
133
134 if (theme == null) {
135 continue;
136 }
137
138 list.addData(new FacetRecord(theme));
139 }
140
141 fireOutputParameterChanged();
142 }
143
144
145 /**
146 * This method triggers the CollectionAttributeService. Based on the current
147 * collectin settings, the attribute of the collection is modified or not.
148 * But in every case, we will get a new collection object - which might be
149 * the same as the current one.
150 */
151 public void updateCollection() {
152 final Config config = Config.getInstance();
153 final String url = config.getServerUrl();
154 final String loc = config.getLocale();
155
156 GWT.log("ThemePanel.updateCollection via RPC now");
157
158 // don't forget to enable the panel after the request has finished!
159 disable();
160
161 updater.update(collection, url, loc, new AsyncCallback<Collection>() {
162 public void onFailure(Throwable caught) {
163 GWT.log("Could not update collection attributes.");
164 SC.warn(MSG.getString(caught.getMessage()));
165
166 enable();
167 }
168
169
170 public void onSuccess(Collection collection) {
171 setCollection(collection);
172
173 enable();
174 }
175 });
176 }
177
178
179
180
181 /**
182 * A method that removes all records from theme grid.
183 */
184 protected void clearGrid() {
185 ListGridRecord[] records = list.getRecords();
186
187 if (records == null || records.length == 0) {
188 return;
189 }
190
191 for (ListGridRecord record: records) {
192 list.removeData(record);
193 }
194 }
195
196
197 /**
198 * This method is called after a cell in the theme grid has been modified.
199 *
200 * @param event The event that stores information about the modified record.
201 */
202 @Override
203 public void onEditComplete(EditCompleteEvent event) {
204 GWT.log("Edited record.");
205
206 int row = event.getRowNum();
207 FacetRecord rec = (FacetRecord) list.getRecord(row);
208
209 activateTheme(rec.getTheme(), rec.getActive());
210
211 updateCollection();
212 }
213
214
215 @Override
216 public void onMove(OnMoveEvent event) {
217 int type = event.getType();
218
219 GWT.log("ThemePanel.onMove: " + type);
220
221 ListGridRecord[] records = list.getSelection();
222
223 if (records == null || records.length == 0) {
224 GWT.log("ThemePanel.onMove: No records selected.");
225 return;
226 }
227
228 switch (type) {
229 case 0: moveRecordsTop(records); break;
230 case 1: moveRecordsUp(records); break;
231 case 2: moveRecordsDown(records); break;
232 case 3: moveRecordsBottom(records); break;
233 }
234
235 updateCollection();
236 }
237
238
239 /**
240 * Moves the selected grid records (themes) to the top of the grid.
241 *
242 * @param records The selected themes in the list. Null not permitted.
243 */
244 protected void moveRecordsTop(ListGridRecord[] records) {
245 ThemeList themeList = getThemeList();
246
247 int idx = 1;
248
249 for (ListGridRecord record: records) {
250 Theme theme = ((FacetRecord) record).getTheme();
251 themeList.setThemePosition(theme, idx++);
252 }
253
254 updateGrid();
255 }
256
257
258 /**
259 * Moves the selected grid records (themes) one step up.
260 *
261 * @param records The selected themes in the list. Null not permitted.
262 */
263 protected void moveRecordsUp(ListGridRecord[] records) {
264 ThemeList themeList = getThemeList();
265
266 int[] newPos = new int[records.length];
267
268 for (int i = 0; i < records.length ; i++) {
269 Theme theme = ((FacetRecord) records[i]).getTheme();
270 newPos[i] = theme.getPosition() - 1;
271 }
272
273 for (int i = 0; i < records.length ; i++) {
274 Theme theme = ((FacetRecord) records[i]).getTheme();
275 themeList.setThemePosition(theme, newPos[i]);
276 }
277
278 updateGrid();
279 }
280
281
282 /**
283 * Moves the selected grid records (themes) one step down.
284 *
285 * @param records The selected themes in the list. Null not permitted.
286 */
287 protected void moveRecordsDown(ListGridRecord[] records) {
288 ThemeList themeList = getThemeList();
289
290 int[] newPos = new int[records.length];
291
292 for (int i = records.length-1; i >= 0; i--) {
293 Theme theme = ((FacetRecord) records[i]).getTheme();
294 newPos[i] = theme.getPosition()+1;
295 }
296
297 for (int i = records.length-1; i >= 0; i--) {
298 Theme theme = ((FacetRecord) records[i]).getTheme();
299 themeList.setThemePosition(theme, newPos[i]);
300 }
301
302 updateGrid();
303 }
304
305
306 /**
307 * Moves the selected grid records (themes) to the bottom of the grid.
308 *
309 * @param records The selected themes in the list. Null not permitted.
310 */
311 protected void moveRecordsBottom(ListGridRecord[] records) {
312 ThemeList themeList = getThemeList();
313
314 int idx = themeList.getThemeCount();
315
316 for (int i = records.length-1; i >= 0; i--) {
317 Theme theme = ((FacetRecord) records[i]).getTheme();
318 themeList.setThemePosition(theme, idx--);
319 }
320
321 updateGrid();
322 }
323 }
324 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org