Subversion Repositories eFlore/Archives.cel-v1

Rev

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