Subversion Repositories eFlore/Archives.cel-v1

Rev

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