Subversion Repositories eFlore/Archives.cel-v1

Rev

Details | 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);
240
					      String loc=table.getText(row,cell);
241
					      date=loc;
242
					      if (loc.compareTo(VALUE_UNKNOWN)!=0) {
243
					    	  mediator.onDateSelected(table.getText(row,cell));
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
 
276
			HTTPRequest.asyncGet(serviceBaseUrl + "/InventoryDateList/" + user + "/" + location,
277
					new ResponseTextHandler() {
278
 
279
						public void onCompletion(String str) {
280
							JSONValue jsonValue = JSONParser.parse(str);
281
							JSONNumber jsonNumber;
282
							if ((jsonNumber = jsonValue.isNumber()) != null) {
283
								count = (int) jsonNumber.getValue();
284
								update();
285
							}
286
						}
287
					});
288
 
289
		}
290
 
291
 
292
		  private void selectRow(int row) {
293
 
294
		    styleRow(selectedRow, false);
295
		    styleRow(row, true);
296
 
297
		    selectedRow = row;
298
 
299
		  }
300
 
301
 
302
		  private void styleRow(int row, boolean selected) {
303
			    if (row != -1) {
304
				   selector.getRowFormatter().removeStyleName(0, "date-SelectedRow");
305
			      if (selected)
306
			        table.getRowFormatter().addStyleName(row, "date-SelectedRow");
307
			      else
308
			    	if (row < table.getRowCount()) {
309
			    		table.getRowFormatter().removeStyleName(row, "date-SelectedRow");
310
			    	}
311
			    }
312
			  }
313
 
314
 
315
	/**
316
	 *
317
	 * Mise a jour de l'affichage, à partir des données d'inventaire deja
318
	 * saisies. La valeur de this.startIndex permet de determiner quelles
319
	 * données seront affichées
320
	 *
321
	 */
322
 
323
		public void update() {
324
 
325
 
326
			setStatusDisabled();
327
 
328
			HTTPRequest.asyncGet(serviceBaseUrl + "/InventoryDateList/" + user + "/" + location + "/"
329
					+ startIndex + "/" + VISIBLE_DATE_COUNT,
330
 
331
			new ResponseTextHandler() {
332
 
333
				public void onCompletion(String str) {
334
 
335
 
336
 
337
					JSONValue jsonValue = JSONParser.parse(str);
338
					JSONArray jsonArray;
339
					JSONArray jsonArrayNested;
340
 
341
					int row=0;
342
					int i=0;
343
 
344
					if ((jsonArray = jsonValue.isArray()) != null) {
345
						int arraySize = jsonArray.size();
346
						for (i = 0; i < arraySize; ++i) {
347
							if ((jsonArrayNested = jsonArray.get(i).isArray()) != null) {
348
								if (i>=table.getRowCount()) {
349
									 row = table.insertRow(table.getRowCount());
350
								}
351
								else {
352
									row = i;
353
								}
354
								// Lieu
355
 
356
								String adate=((JSONString)jsonArrayNested.get(0)).stringValue();
357
 
358
								if (adate.compareTo("0000-00-00 00:00:00")!=0) {
359
									table.setText(row, 0,adate);
360
								}
361
								else {
362
									table.setText(row, 0,VALUE_UNKNOWN);
363
								}
364
 
13 ddelon 365
								if ((adate.compareTo(date)==0) || ( date.compareTo("00/00/0000")==0) && adate.compareTo("0000-00-00 00:00:00")==0 ) {
12 ddelon 366
								  styleRow(row, true);
367
								}
368
								else {
369
								  styleRow(row, false);
370
								}
371
 
372
								table.getFlexCellFormatter().setWidth(row, 0, "100%");
373
							}
374
 
375
						}
376
					}
377
 
378
					if (date.compareTo("all")==0) {
379
							selector.getRowFormatter().addStyleName(0, "date-SelectedRow");
380
					}
381
 
382
 
383
					// Suppression fin ancien affichage
13 ddelon 384
					if (i<table.getRowCount()) {
385
						 for (int j = table.getRowCount() -1 ; j >= i; j--) {
12 ddelon 386
							 table.removeRow(j);
387
						 }
388
					}
13 ddelon 389
 
12 ddelon 390
					setStatusEnabled();
391
 
392
 
393
				}
394
			});
395
 
396
		}
397
 
398
		public void setLocation(String location) {
399
			this.location = location;
400
		}
401
 
402
 
403
 
404
		/*
405
		 * Positionnement index de parcours (this.startIndex) pour affichage de la
406
		 * dernière page
407
		 *
408
		 * @param
409
		 * @return void
410
		 */
411
 
412
		private void gotoEnd() {
413
 
414
			if ((count == 0) || (count % VISIBLE_DATE_COUNT) > 0) {
415
				startIndex = count - (count % VISIBLE_DATE_COUNT);
416
			} else {
417
				startIndex = count - VISIBLE_DATE_COUNT;
418
			}
419
 
420
		}
421
 
422
 
423
		/**
424
		 * Affichage message d'attente et désactivation navigation
425
		 *
426
		 * @param
427
		 * @return void
428
		 */
429
 
430
		private void setStatusDisabled() {
431
 
432
			navBar.gotoFirst.setEnabled(false);
433
			navBar.gotoPrev.setEnabled(false);
434
			navBar.gotoNext.setEnabled(false);
435
			navBar.gotoEnd.setEnabled(false);
436
 
437
			setStatusText("Patientez ...");
438
		}
439
 
440
		/**
441
		 * Affichage numero de page et gestion de la navigation
442
		 *
443
		 */
444
 
445
		private void setStatusEnabled() {
446
 
447
			// Il y a forcemment un disabled avant d'arriver ici
448
 
449
			if (count > 0) {
450
 
451
				if (startIndex >= VISIBLE_DATE_COUNT) { // Au dela de la
452
															// premiere page
453
					navBar.gotoPrev.setEnabled(true);
454
					navBar.gotoFirst.setEnabled(true);
455
					if (startIndex < (count - VISIBLE_DATE_COUNT)) { // Pas la
456
																		// derniere
457
																		// page
458
						navBar.gotoNext.setEnabled(true);
459
						navBar.gotoEnd.setEnabled(true);
460
						setStatusText((startIndex + 1) + " - "
461
								+ (startIndex + VISIBLE_DATE_COUNT) + " sur " + count );
462
					} else { // Derniere page
463
						setStatusText((startIndex + 1) + " - " + count + " sur " + count );
464
					}
465
				} else { // Premiere page
466
					if (count > VISIBLE_DATE_COUNT) { // Des pages derrieres
467
						navBar.gotoNext.setEnabled(true);
468
						navBar.gotoEnd.setEnabled(true);
469
						setStatusText((startIndex + 1) + " - "
470
								+ (startIndex + VISIBLE_DATE_COUNT) + " sur " + count);
471
					} else {
472
						setStatusText((startIndex + 1) + " - " + count + " sur " + count);
473
					}
474
				}
475
			}
476
 
477
			else { // Pas d'inventaire, pas de navigation
478
				setStatusText("0 - 0 sur 0");
479
			}
480
		}
481
 
482
 
483
 
484
		private void setStatusText(String text) {
485
			navBar.status.setText(text);
486
		}
487
 
488
}