Subversion Repositories eFlore/Archives.cel-v1

Rev

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

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