Subversion Repositories eFlore/Archives.cel-v1

Rev

Rev 26 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
27 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.http.client.URL;
20
import com.google.gwt.json.client.JSONArray;
21
import com.google.gwt.json.client.JSONNumber;
22
import com.google.gwt.json.client.JSONParser;
23
import com.google.gwt.json.client.JSONString;
24
import com.google.gwt.json.client.JSONValue;
25
import com.google.gwt.user.client.HTTPRequest;
26
import com.google.gwt.user.client.ResponseTextHandler;
27
import com.google.gwt.user.client.ui.Button;
28
import com.google.gwt.user.client.ui.ClickListener;
29
import com.google.gwt.user.client.ui.Composite;
30
import com.google.gwt.user.client.ui.DockPanel;
31
import com.google.gwt.user.client.ui.FlexTable;
32
import com.google.gwt.user.client.ui.Grid;
33
import com.google.gwt.user.client.ui.HTML;
34
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
35
import com.google.gwt.user.client.ui.HasVerticalAlignment;
36
import com.google.gwt.user.client.ui.HorizontalPanel;
37
import com.google.gwt.user.client.ui.SourcesTableEvents;
38
import com.google.gwt.user.client.ui.TableListener;
39
import com.google.gwt.user.client.ui.VerticalPanel;
40
import com.google.gwt.user.client.ui.Widget;
41
 
42
/**
43
 * A tree displaying a set of email folders.
44
 */
45
public class DateList extends Composite {
46
 
47
 
48
 
49
	// Barre de navigation
50
 
51
	private class NavBar extends Composite implements ClickListener {
52
 
53
		public final DockPanel bar = new DockPanel();
54
 
55
		public final Button gotoFirst = new Button("<<", this);
56
 
57
		public final Button gotoNext = new Button(">", this);
58
 
59
		public final Button gotoPrev = new Button("<", this);
60
 
61
		public final Button gotoEnd = new Button(">>", this);
62
 
63
		public final HTML status = new HTML();
64
 
65
 
66
		public NavBar() {
67
			initWidget(bar);
68
			bar.setStyleName("navbar");
69
			status.setStyleName("status");
70
			status.setWordWrap(false);
71
 
72
			HorizontalPanel buttons = new HorizontalPanel();
73
 
74
			buttons.add(status);
75
			buttons.setCellHorizontalAlignment(status,
76
					HasHorizontalAlignment.ALIGN_RIGHT);
77
			buttons.setCellVerticalAlignment(status,
78
					HasVerticalAlignment.ALIGN_MIDDLE);
79
			buttons.setCellWidth(status, "100%");
80
 
81
 
82
			buttons.add(gotoFirst);
83
			buttons.add(gotoPrev);
84
			buttons.add(gotoNext);
85
			buttons.add(gotoEnd);
86
			bar.add(buttons, DockPanel.EAST);
87
			bar.setCellHorizontalAlignment(buttons, DockPanel.ALIGN_RIGHT);
88
 
89
 
90
			bar.setCellHorizontalAlignment(buttons, DockPanel.ALIGN_LEFT);
91
 
92
			bar.setVerticalAlignment(DockPanel.ALIGN_MIDDLE);
93
 
94
 
95
 
96
 
97
 
98
		}
99
 
100
		public void onClick(Widget sender) {
101
			if (sender == gotoNext) {
102
				// Move forward a page.
103
				startIndex += VISIBLE_DATE_COUNT;
104
				if (startIndex >= count)
105
					startIndex -= VISIBLE_DATE_COUNT;
106
			} else {
107
				if (sender == gotoPrev) {
108
					// Move back a page.
109
					startIndex -= VISIBLE_DATE_COUNT;
110
					if (startIndex < 0)
111
						startIndex = 0;
112
				} else {
113
					if (sender == gotoEnd) {
114
						gotoEnd();
115
					} else {
116
						if (sender == gotoFirst) {
117
							startIndex = 0;
118
						}
119
					}
120
				}
121
			}
122
			update();
123
		}
124
 
125
	}
126
 
127
 
128
 
129
		private static final int VISIBLE_DATE_COUNT = 10;
130
		private static final String VALUE_UNKNOWN = "Inconnues";
131
 
132
		private Grid header = new Grid(1, 2);
133
 
134
		private Grid selector = new Grid(1, 2);
135
 
136
		private FlexTable table = new FlexTable();
137
 
138
		private VerticalPanel outer = new VerticalPanel();
139
		private VerticalPanel inner = new VerticalPanel();
140
 
141
		private int startIndex = 0;
142
 
143
		private String user;
144
 
145
		private String serviceBaseUrl = null;
146
 
147
		private String location = "all";
148
		private String date = "all";
149
 
150
 
151
		private NavBar navBar=null;
152
 
153
 
154
		private int count = 65000;
155
 
156
		// Tous selectionné
157
		private int  selectedRow = -1;
158
 
159
		private Mediator mediator = null;
160
 
161
 
162
		public DateList(Mediator med) {
163
 
164
			mediator=med;
165
 
166
			mediator.registerDateList(this);
167
 
168
			user=mediator.getUser();
169
			serviceBaseUrl = mediator.getServiceBaseUrl();
170
 
171
			navBar = new NavBar();
172
 
173
			// Mise en forme du header
174
 
175
 
176
 
177
			header.setCellSpacing(0);
178
			header.setCellPadding(2);
179
			header.setWidth("100%");
180
 
181
			header.setStyleName("date-ListHeader");
182
 
183
 
184
 
185
			header.setWidget(0, 1,navBar);
186
 
187
 
188
//			header.getCellFormatter().setWidth(0, 0, "100%");
189
 
190
 
191
			// Mise en forme de l'entree "Toutes localités"
192
 
193
			selector.setCellSpacing(0);
194
			selector.setCellPadding(0);
195
			selector.setWidth("100%");
196
 
197
			selector.setHTML(0, 0, "Toutes");
198
 
199
			selector.getCellFormatter().setWidth(0, 0, "100%");
200
 
201
 
202
			 // Hook up events.
203
			selector.addTableListener(new TableListener () {
204
 
205
				  public void onCellClicked(SourcesTableEvents sender, int row, int cell) {
206
					  	styleRow(selectedRow, false);
207
				        selector.getRowFormatter().addStyleName(0, "date-SelectedRow");
208
					    mediator.onDateSelected("all");
209
					    date="all";
210
					  }
211
 
212
		    });
213
 
214
			selector.setStyleName("date-ListElement");
215
 
216
 
217
			// Mise en forme du contenu
218
 
219
			table.setCellSpacing(0);
220
			table.setBorderWidth(0);
221
			table.setCellPadding(2);
222
			table.setWidth("100%");
223
 
224
			table.setStyleName("date-ListElement");
225
 
226
 
227
			outer.add(header);
228
			inner.add(selector); // Toutes localités
229
			inner.add(table);
230
			inner.setStyleName("date-List");
231
			inner.setWidth("100%");
232
			outer.setWidth("100%");
233
			outer.add(inner);
234
 
235
			 // Hook up events.
236
		    table.addTableListener(new TableListener () {
237
 
238
				  public void onCellClicked(SourcesTableEvents sender, int row, int cell) {
239
 
240
					      selectRow(row);
241
					      String adate=table.getText(row,cell);
242
					      if (adate.compareTo(VALUE_UNKNOWN)!=0) {
243
					    	  date=adate;
244
					    	  mediator.onDateSelected(adate);
245
					      }
246
					      else {
247
					    	  date="00/00/0000";
248
					    	  mediator.onDateSelected("00/00/0000");
249
					      }
250
 
251
					  }
252
 
253
		    });
254
 
255
		  	styleRow(selectedRow, false);
256
	        selector.getRowFormatter().addStyleName(0, "date-SelectedRow");
257
 
258
 
259
			//updateCount();
260
			// update()
261
 
262
			initWidget(outer);
263
 
264
 
265
  }
266
 
267
		/**
268
		 * Recherche nombre d'enregistrement pour l'utilisateur en cours
269
		 *
270
		 *
271
		 */
272
 
273
		public void updateCount() {
274
 
275
			setStatusDisabled();
276
 
277
			HTTPRequest.asyncGet(serviceBaseUrl + "/InventoryDateList/" + user + "/" + URL.encodeComponent(location) ,
278
					new ResponseTextHandler() {
279
 
280
						public void onCompletion(String str) {
281
							JSONValue jsonValue = JSONParser.parse(str);
282
							JSONNumber jsonNumber;
283
							if ((jsonNumber = jsonValue.isNumber()) != null) {
284
								count = (int) jsonNumber.getValue();
285
								update();
286
							}
287
						}
288
					});
289
 
290
		}
291
 
292
 
293
		  private void selectRow(int row) {
294
 
295
		    styleRow(selectedRow, false);
296
		    styleRow(row, true);
297
 
298
		    selectedRow = row;
299
 
300
		  }
301
 
302
 
303
		  private void styleRow(int row, boolean selected) {
304
			    if (row != -1) {
305
				   selector.getRowFormatter().removeStyleName(0, "date-SelectedRow");
306
			      if (selected)
307
			        table.getRowFormatter().addStyleName(row, "date-SelectedRow");
308
			      else
309
			    	if (row < table.getRowCount()) {
310
			    		table.getRowFormatter().removeStyleName(row, "date-SelectedRow");
311
			    	}
312
			    }
313
			  }
314
 
315
 
316
	/**
317
	 *
318
	 * Mise a jour de l'affichage, à partir des données d'inventaire deja
319
	 * saisies. La valeur de this.startIndex permet de determiner quelles
320
	 * données seront affichées
321
	 *
322
	 */
323
 
324
		public void update() {
325
 
326
 
327
			setStatusDisabled();
328
 
329
			HTTPRequest.asyncGet(serviceBaseUrl + "/InventoryDateList/" + user + "/" + URL.encodeComponent(location) + "/"
330
					+ startIndex + "/" + VISIBLE_DATE_COUNT,
331
 
332
			new ResponseTextHandler() {
333
 
334
				public void onCompletion(String str) {
335
 
336
 
337
 
338
					JSONValue jsonValue = JSONParser.parse(str);
339
					JSONArray jsonArray;
340
					JSONArray jsonArrayNested;
341
 
342
					int row=0;
343
					int i=0;
344
 
345
					if ((jsonArray = jsonValue.isArray()) != null) {
346
						int arraySize = jsonArray.size();
347
						for (i = 0; i < arraySize; ++i) {
348
							if ((jsonArrayNested = jsonArray.get(i).isArray()) != null) {
349
								if (i>=table.getRowCount()) {
350
									 row = table.insertRow(table.getRowCount());
351
								}
352
								else {
353
									row = i;
354
								}
355
								// Lieu
356
 
357
								String adate=((JSONString)jsonArrayNested.get(0)).stringValue();
358
 
359
								if (adate.compareTo("0000-00-00 00:00:00")!=0) {
360
									table.setText(row, 0,adate);
361
								}
362
								else {
363
									table.setText(row, 0,VALUE_UNKNOWN);
364
								}
365
 
366
								if ((adate.compareTo(date)==0) || ( date.compareTo("00/00/0000")==0) && adate.compareTo("0000-00-00 00:00:00")==0 ) {
367
								  styleRow(row, true);
368
								}
369
								else {
370
								  styleRow(row, false);
371
								}
372
 
373
								table.getFlexCellFormatter().setWidth(row, 0, "100%");
374
							}
375
 
376
						}
377
					}
378
 
379
					if (date.compareTo("all")==0) {
380
							selector.getRowFormatter().addStyleName(0, "date-SelectedRow");
381
					}
382
 
383
 
384
					// Suppression fin ancien affichage
385
					if (i<table.getRowCount()) {
386
						 for (int j = table.getRowCount() -1 ; j >= i; j--) {
387
							 table.removeRow(j);
388
						 }
389
					}
390
 
391
					setStatusEnabled();
392
 
393
 
394
				}
395
			});
396
 
397
		}
398
 
399
		public void setLocation(String location) {
400
			this.location = location;
401
		}
402
 
403
 
404
 
405
		/*
406
		 * Positionnement index de parcours (this.startIndex) pour affichage de la
407
		 * dernière page
408
		 *
409
		 * @param
410
		 * @return void
411
		 */
412
 
413
		private void gotoEnd() {
414
 
415
			if ((count == 0) || (count % VISIBLE_DATE_COUNT) > 0) {
416
				startIndex = count - (count % VISIBLE_DATE_COUNT);
417
			} else {
418
				startIndex = count - VISIBLE_DATE_COUNT;
419
			}
420
 
421
		}
422
 
423
 
424
		/**
425
		 * Affichage message d'attente et désactivation navigation
426
		 *
427
		 * @param
428
		 * @return void
429
		 */
430
 
431
		private void setStatusDisabled() {
432
 
433
			navBar.gotoFirst.setEnabled(false);
434
			navBar.gotoPrev.setEnabled(false);
435
			navBar.gotoNext.setEnabled(false);
436
			navBar.gotoEnd.setEnabled(false);
437
 
438
			setStatusText("Patientez ...");
439
		}
440
 
441
		/**
442
		 * Affichage numero de page et gestion de la navigation
443
		 *
444
		 */
445
 
446
		private void setStatusEnabled() {
447
 
448
			// Il y a forcemment un disabled avant d'arriver ici
449
 
450
			if (count > 0) {
451
 
452
				if (startIndex >= VISIBLE_DATE_COUNT) { // Au dela de la
453
															// premiere page
454
					navBar.gotoPrev.setEnabled(true);
455
					navBar.gotoFirst.setEnabled(true);
456
					if (startIndex < (count - VISIBLE_DATE_COUNT)) { // Pas la
457
																		// derniere
458
																		// page
459
						navBar.gotoNext.setEnabled(true);
460
						navBar.gotoEnd.setEnabled(true);
461
						setStatusText((startIndex + 1) + " - "
462
								+ (startIndex + VISIBLE_DATE_COUNT) + " sur " + count );
463
					} else { // Derniere page
464
						setStatusText((startIndex + 1) + " - " + count + " sur " + count );
465
					}
466
				} else { // Premiere page
467
					if (count > VISIBLE_DATE_COUNT) { // Des pages derrieres
468
						navBar.gotoNext.setEnabled(true);
469
						navBar.gotoEnd.setEnabled(true);
470
						setStatusText((startIndex + 1) + " - "
471
								+ (startIndex + VISIBLE_DATE_COUNT) + " sur " + count);
472
					} else {
473
						setStatusText((startIndex + 1) + " - " + count + " sur " + count);
474
					}
475
				}
476
			}
477
 
478
			else { // Pas d'inventaire, pas de navigation
479
				setStatusText("0 - 0 sur 0");
480
			}
481
		}
482
 
483
 
484
 
485
		private void setStatusText(String text) {
486
			navBar.status.setText(text);
487
		}
488
 
489
 
490
		public void setUser(String user) {
491
			this.user = user;
492
		}
493
 
494
 
495
 
496
		public void setDate(String date) {
497
			this.date = date;
498
		}
499
 
500
 
501
}