Subversion Repositories eFlore/Archives.cel-v1

Rev

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