Subversion Repositories eFlore/Archives.cel-v1

Rev

Rev 30 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
29 ddelon 1
/*
2
 * Copyright 2006 Google Inc.
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5
 * use this file except in compliance with the License. You may obtain a copy of
6
 * the License at
7
 *
8
 * http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
 * License for the specific language governing permissions and limitations under
14
 * the License.
15
 */
16
package org.tela_botanica.client;
17
 
18
 
30 ddelon 19
import com.google.gwt.http.client.URL;
29 ddelon 20
import com.google.gwt.json.client.JSONArray;
21
import com.google.gwt.json.client.JSONParser;
22
import com.google.gwt.json.client.JSONString;
23
import com.google.gwt.json.client.JSONValue;
24
import com.google.gwt.user.client.HTTPRequest;
25
import com.google.gwt.user.client.ResponseTextHandler;
26
 
27
import net.mygwt.ui.client.data.Model;
28
import net.mygwt.ui.client.viewer.IAsyncContentCallback;
29
import net.mygwt.ui.client.viewer.IAsyncTreeContentProvider;
30
import net.mygwt.ui.client.viewer.IElementComparer;
31
import net.mygwt.ui.client.viewer.ISelectionChangedListener;
32
import net.mygwt.ui.client.viewer.ModelLabelProvider;
33
import net.mygwt.ui.client.viewer.SelectionChangedEvent;
34
import net.mygwt.ui.client.viewer.TreeViewer;
35
import net.mygwt.ui.client.viewer.Viewer;
36
import net.mygwt.ui.client.widget.ExpandBar;
37
import net.mygwt.ui.client.widget.ExpandItem;
38
import net.mygwt.ui.client.widget.tree.Tree;
39
 
40
 
41
public class LocationFilterView {
42
 
43
 
44
 
45
		private static final String VALUE_UNKNOWN = "Inconnu";
46
 
47
 
48
		private String user;
49
		private String serviceBaseUrl = null;
50
 
51
		private String id_location = "all";
52
		private String location = "all";
53
		private String lieudit = "all";
54
 
55
 
56
		private Mediator mediator = null;
57
 
58
	    private SALB[] salbs=null;
59
	    private TALB[] talbs=null;
60
	    private FALB[] falbs=null;
61
		private Model root = null;
62
		private RALB ralb = null;
63
		private Tree tree = null;
64
		private TreeViewer viewer=null;
65
 
66
		public LocationFilterView(Mediator med,InventoryFilterView inventoryFilterView) {
67
 
68
 
69
			mediator=med;
70
 
71
 
72
			user=mediator.getUser();
73
			serviceBaseUrl = mediator.getServiceBaseUrl();
74
 
75
			mediator.registerLocationFilterView(this);
76
 
77
 
78
			initTree();
79
 
80
 
81
			ExpandItem locationfilter = new ExpandItem();
82
			locationfilter.setText("Lieux");
83
 
84
 
85
			ExpandBar expandBar=inventoryFilterView.getExpandBar();
86
			locationfilter.getContainer().add(tree);
87
			expandBar.add(locationfilter);
88
 
89
			locationfilter.setExpanded(true);
90
 
91
 
92
 
93
  }
94
 
95
 // Initialisation noeud de base
96
 
97
  void initTree() {
98
 
99
 
100
	  // Enregistrement Mise à jour asynchrone
101
 
102
	   IAsyncTreeContentProvider cp = new IAsyncTreeContentProvider() {
103
 
104
	   public Object[] getElements(Object input) {
105
		      Model m = (Model) input;
106
		       return m.getChildren().toArray();
107
	   }
108
 
109
	   public Object getParent(Object element) {
110
		   return ((Model) element).getParent();
111
	   }
112
 
113
	   public boolean hasChildren(Object parent) {
114
	          if (parent instanceof FALB) {  // Station
115
	        	  return false;
116
		      }
117
	          else  {
118
	        	  if (parent instanceof SALB) {  // Departement
119
	        		  if (((SALB)parent).toString().compareTo(VALUE_UNKNOWN)==0)
120
	        			  return false;
121
	        		  else
122
	        			  return true;
123
	        	  }
124
	        	  else {
125
	        		  return true;
126
	        	  }
127
	          }
128
	    }
129
 
130
	    public void getChildren(final Object parent, final IAsyncContentCallback callback) {
131
 
132
	          if (parent instanceof SALB) {
133
 
134
	        	  // Departement + commune
135
 
136
		           HTTPRequest.asyncGet(serviceBaseUrl + "/InventoryLocationList/" + user + "/"
137
							+ ((SALB) parent).getAsString("path"),
138
 
139
							new ResponseTextHandler() {
140
 
141
								public void onCompletion(String str) {
142
 
143
									JSONValue jsonValue = JSONParser.parse(str);
144
									JSONArray jsonArray;
145
									JSONArray jsonArrayNested;
146
 
147
									int i=0;
148
 
149
									if ((jsonArray = jsonValue.isArray()) != null) {
150
										int arraySize = jsonArray.size();
151
										talbs=new TALB[arraySize];
152
										for (i = 0; i < arraySize; ++i) {
153
											if ((jsonArrayNested = jsonArray.get(i).isArray()) != null) {
154
												String dep=Util.toCelString(((JSONString)jsonArrayNested.get(0)).toString());
155
												if (dep.compareTo("000null")==0) dep=VALUE_UNKNOWN;
156
												talbs[i]=new TALB((SALB) parent,dep);
157
											}
158
										}
159
										callback.setElements(talbs);
160
									}
161
 
162
 
163
								}
164
							});
165
 
166
		         }
167
	          else if (parent instanceof TALB) {
168
 
169
	        	  // Commune + station
170
 
171
		           HTTPRequest.asyncGet(serviceBaseUrl + "/InventoryLocationList/" + user + "/"
172
							+ ((TALB) parent).getAsString("path") ,
173
 
174
							new ResponseTextHandler() {
175
 
176
								public void onCompletion(String str) {
177
 
178
									JSONValue jsonValue = JSONParser.parse(str);
179
									JSONArray jsonArray;
180
									JSONArray jsonArrayNested;
181
 
182
									int i=0;
183
 
184
									if ((jsonArray = jsonValue.isArray()) != null) {
185
										int arraySize = jsonArray.size();
186
										falbs=new FALB[arraySize];
187
										for (i = 0; i < arraySize; ++i) {
188
											if ((jsonArrayNested = jsonArray.get(i).isArray()) != null) {
189
												String com=Util.toCelString(((JSONString)jsonArrayNested.get(0)).toString());
190
												if (com.compareTo("000null")==0) com=VALUE_UNKNOWN;
191
												falbs[i]=new FALB((TALB) parent, com);
192
											}
193
										}
194
										callback.setElements(falbs);
195
 
196
									}
197
 
198
 
199
								}
200
							});
201
 
202
 
203
	          }
204
	          	else if (parent instanceof RALB) {
205
 
206
	          		 // Rafraichissement Racine
207
 
208
			           HTTPRequest.asyncGet(serviceBaseUrl + "/InventoryLocationList/" + user ,
209
 
210
								new ResponseTextHandler() {
211
 
212
									public void onCompletion(String str) {
213
 
214
										JSONValue jsonValue = JSONParser.parse(str);
215
										JSONArray jsonArray;
216
										JSONArray jsonArrayNested;
217
 
218
										int i=0;
219
 
220
										if ((jsonArray = jsonValue.isArray()) != null) {
221
											int arraySize = jsonArray.size();
222
											salbs=new SALB[arraySize];
223
											for (i = 0; i < arraySize; ++i) {
224
												if ((jsonArrayNested = jsonArray.get(i).isArray()) != null) {
225
													String all=Util.toCelString(((JSONString)jsonArrayNested.get(0)).toString());
226
													if (all.compareTo("000null")==0) all=VALUE_UNKNOWN;
227
													salbs[i]=new SALB(all);
228
												}
229
											}
230
 
231
											callback.setElements(salbs);
232
 
233
											Model m=new Model();
234
											m.set("name",id_location);
235
											viewer.setExpanded(m,true);
236
 
237
										}
238
 
239
 
240
									}
241
								});
242
 
243
 
244
 
245
	          	}
246
 
247
	    	}
248
 
249
		    public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
250
 
251
		    }
252
 
253
		 };
254
 
255
		 tree = new Tree();
256
 
257
		 viewer = new TreeViewer(tree);
258
		 viewer.setContentProvider(cp);
259
 
260
		 viewer.setLabelProvider(new ModelLabelProvider());
261
 
262
		 viewer.setComparer(new IElementComparer() {
263
 
264
			 public boolean equals(Object a, Object b) {
265
 
266
				 if ((a==null) || b==null)
267
					 return false;
268
				 if (((Model) a).getAsString("name").compareTo(((Model) b).getAsString("name"))==0) {
269
					 return true;
270
				 }
271
				 else return false;
272
 
273
			 }
274
 
275
		 }
276
		 );
277
 
278
 
279
		   viewer.addSelectionListener(new ISelectionChangedListener() {
280
			        public void selectionChanged(SelectionChangedEvent se) {
281
			          Model m = (Model) se.getSelection().getFirstElement();
282
			       //   Info.show("Selection Changed", "{0} was selected", m.getAsString("path"));
283
 
284
			          if (m instanceof TALB) { // Commune
285
			        	  location=m.getAsString("name");
286
			        	  if (location.compareTo(VALUE_UNKNOWN)==0) location="000null";
287
			        	  id_location=m.getAsString("salb");
288
			        	  if (id_location.compareTo(VALUE_UNKNOWN)==0) id_location="000null";
289
			        	  lieudit="all";
290
			        	  mediator.onLocationSelected(id_location,location,lieudit);
291
			          }
292
			          else
293
				          if (m instanceof FALB) { // Station
294
				        	  id_location=m.getAsString("salb");
295
				        	  if (id_location.compareTo(VALUE_UNKNOWN)==0) id_location="000null";
296
				        	  location=m.getAsString("talb");
297
				        	  if (location.compareTo(VALUE_UNKNOWN)==0) location="000null";
298
				        	  lieudit=m.getAsString("name");
299
				        	  if (lieudit.compareTo(VALUE_UNKNOWN)==0) lieudit="000null";
300
				        	  mediator.onLocationSelected(id_location,location,lieudit);
301
				          }
302
 
303
				          else
304
 
305
				          if (m instanceof SALB) { // Departement
306
				        	  id_location=m.getAsString("name");
307
				        	  if (id_location.compareTo(VALUE_UNKNOWN)==0) id_location="000null";
308
				        	  location="all";
309
				        	  lieudit="all";
310
				        	  mediator.onLocationSelected(id_location,location,lieudit);
311
				          }
312
 
313
			          		if (m instanceof RALB) { // Pays
314
			          				id_location="all";
315
			          				location="all";
316
			          				lieudit="all";
317
			          				mediator.onLocationSelected(id_location,location,lieudit);
318
			          		}
319
 
320
 
321
 
322
			        }
323
		});
324
 
325
		 // Initialisation premier niveau (doublon obligatoire avec rafraichissement asynchrone)
326
 
327
		 root = new Model();
328
		 HTTPRequest.asyncGet(serviceBaseUrl + "/InventoryLocationList/" + user ,
329
 
330
					new ResponseTextHandler() {
331
 
332
						public void onCompletion(String str) {
333
 
334
							JSONValue jsonValue = JSONParser.parse(str);
335
							JSONArray jsonArray;
336
							JSONArray jsonArrayNested;
337
 
338
							int i=0;
339
 
340
							if ((jsonArray = jsonValue.isArray()) != null) {
341
								int arraySize = jsonArray.size();
342
								salbs=new SALB[arraySize];
343
								ralb = new RALB("France");
344
								for (i = 0; i < arraySize; ++i) {
345
									if ((jsonArrayNested = jsonArray.get(i).isArray()) != null) {
346
										String all=Util.toCelString(((JSONString)jsonArrayNested.get(0)).toString());
347
										if (all.compareTo("000null")==0) all=VALUE_UNKNOWN;
348
										salbs[i]=new SALB(all);
349
										ralb.add(salbs[i]);
350
									}
351
								}
352
								root.add(ralb);
353
								viewer.setInput(root);
354
							}
355
 
356
 
357
						}
358
					});
359
 
360
      }
361
 
362
 
363
 
364
		public void setLocation(String location) {
365
			this.location = location;
366
		}
367
 
368
 
369
 
370
		public void setIDLocation(String id_location) {
371
			this.id_location = id_location;
372
		}
373
 
374
 
375
 
376
		public void setLieudit(String lieudit) {
377
			this.lieudit = lieudit;
378
		}
379
 
380
 
381
		public void setUser(String user) {
382
			this.user = user;
383
		}
384
 
385
 
386
		private class RALB extends Model {  // Root Administrative level boundaries (pays)
387
 
388
			  public RALB(String name) {
389
			    set("name", name);
30 ddelon 390
			    set("path", URL.encodeComponent(name));
29 ddelon 391
			  }
392
 
393
			  public String toString() {
394
			    return getAsString("name");
395
			  }
396
 
397
		}
398
 
399
 
400
		private class SALB extends Model {  // Second Administrative level boundaries (departerment)
401
 
402
			  public SALB(String name) {
403
			    set("name", name);
30 ddelon 404
			    set("path", URL.encodeComponent(name));
29 ddelon 405
			  }
406
 
407
			  public String toString() {
408
				    return getAsString("name");
409
			  }
410
 
411
		}
412
 
413
 
414
		private class TALB extends Model {  // Tierce Administrative level boundaries (Commune)
415
 
416
			  public TALB(SALB parent, String name) {
417
			    set("name", name);
418
			    set("salb", parent.get("name"));
30 ddelon 419
			    set("path", parent.get("path")+"/"+URL.encodeComponent(name));
29 ddelon 420
			  }
421
 
422
			  public String toString() {
423
				    return getAsString("name");
424
 
425
			  }
426
 
427
		}
428
 
429
 
430
		private class FALB extends Model {  // Forth Administrative level boundaries (Station)
431
 
432
			  public FALB(TALB parent,String name) {
433
			    set("name", name);
434
			    set("salb", parent.get("salb"));
435
			    set("talb", parent.get("name"));
30 ddelon 436
			    set("path", parent.get("path")+"/"+URL.encodeComponent(name));
29 ddelon 437
			  }
438
 
439
			  public String toString() {
440
				    return getAsString("name");
441
			  }
442
 
443
		}
444
 
445
 
446
		public void update() {
447
 
448
 
449
 
450
			Model m=new Model();
451
			m.set("name",id_location);
452
 
453
			if (viewer.findItem(m)!=null) {
454
				viewer.refresh(m); // Rafraichissement branche departemental
455
 
456
			}
457
			else { // Pas de branche departementale ? : rafraichissement total
458
				Model r=new Model();
459
				r.set("name","France");
460
				viewer.refresh(r);
461
			}
462
 
463
 
464
		}
465
 
466
 
467
}