Subversion Repositories eFlore/Archives.cel-v1

Rev

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

Rev Author Line No. Line
10 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;
11 ddelon 20
import com.google.gwt.json.client.JSONNumber;
10 ddelon 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;
11 ddelon 26
import com.google.gwt.user.client.ui.Button;
27
import com.google.gwt.user.client.ui.ClickListener;
10 ddelon 28
import com.google.gwt.user.client.ui.Composite;
11 ddelon 29
import com.google.gwt.user.client.ui.DockPanel;
10 ddelon 30
import com.google.gwt.user.client.ui.FlexTable;
31
import com.google.gwt.user.client.ui.Grid;
11 ddelon 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;
10 ddelon 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;
11 ddelon 39
import com.google.gwt.user.client.ui.Widget;
10 ddelon 40
 
41
/**
42
 * A tree displaying a set of email folders.
43
 */
44
public class LocationList extends Composite {
11 ddelon 45
 
46
 
47
 
48
	// Barre de navigation
10 ddelon 49
 
11 ddelon 50
	private class NavBar extends Composite implements ClickListener {
10 ddelon 51
 
11 ddelon 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_LOCATION_COUNT;
103
				if (startIndex >= count)
104
					startIndex -= VISIBLE_LOCATION_COUNT;
105
			} else {
106
				if (sender == gotoPrev) {
107
					// Move back a page.
108
					startIndex -= VISIBLE_LOCATION_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
 
12 ddelon 128
		private static final int VISIBLE_LOCATION_COUNT = 10;
10 ddelon 129
		private static final String VALUE_UNKNOWN = "Inconnues";
130
 
131
		private Grid header = new Grid(1, 1);
132
 
133
		private Grid selector = new Grid(1, 1);
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
 
11 ddelon 148
 
149
		private NavBar navBar=null;
150
 
151
 
152
		private int count = 65000;
153
 
10 ddelon 154
		// Tous selectionné
155
		private int  selectedRow = -1;
156
 
157
		private Mediator mediator = null;
158
 
159
 
160
		public LocationList(Mediator med) {
161
 
162
			mediator=med;
163
 
164
			mediator.registerLocationList(this);
165
 
166
			user=mediator.getUser();
167
			serviceBaseUrl = mediator.getServiceBaseUrl();
168
 
11 ddelon 169
			navBar = new NavBar();
170
 
10 ddelon 171
			// Mise en forme du header
172
 
173
			header.setCellSpacing(0);
174
			header.setCellPadding(2);
175
			header.setWidth("100%");
176
 
177
			header.setStyleName("location-ListHeader");
178
 
179
 
12 ddelon 180
			header.setHTML(0, 0, "Relev&eacute;s par commune&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");  // yeah !
10 ddelon 181
 
182
			header.getCellFormatter().setWidth(0, 0, "100%");
183
 
184
 
11 ddelon 185
			// Mise en forme de l'entree "Toutes localités"
10 ddelon 186
 
187
			selector.setCellSpacing(0);
188
			selector.setCellPadding(0);
189
			selector.setWidth("100%");
190
 
191
			selector.setHTML(0, 0, "Toutes");
192
 
193
			selector.getCellFormatter().setWidth(0, 0, "100%");
194
 
195
 
196
			 // Hook up events.
197
			selector.addTableListener(new TableListener () {
198
 
199
				  public void onCellClicked(SourcesTableEvents sender, int row, int cell) {
200
					  	styleRow(selectedRow, false);
201
				        selector.getRowFormatter().addStyleName(0, "location-SelectedRow");
202
					    mediator.onLocationSelected("all");
203
					  }
204
 
205
		    });
206
 
207
			selector.setStyleName("location-ListElement");
208
 
209
 
11 ddelon 210
			// Mise en forme du contenu
10 ddelon 211
 
212
			table.setCellSpacing(0);
213
			table.setBorderWidth(0);
214
			table.setCellPadding(2);
215
			table.setWidth("100%");
216
 
217
			table.setStyleName("location-ListElement");
218
 
219
			// Mise en forme barre navigation
11 ddelon 220
 
221
			outer.add(navBar);
222
 
223
			navBar.setWidth("100%");
224
 
10 ddelon 225
			outer.add(header);
11 ddelon 226
			inner.add(selector); // Toutes localités
10 ddelon 227
			inner.add(table);
228
			inner.setStyleName("location-List");
229
			inner.setWidth("100%");
230
			outer.setWidth("100%");
231
			outer.add(inner);
232
 
233
			 // Hook up events.
234
		    table.addTableListener(new TableListener () {
235
 
236
				  public void onCellClicked(SourcesTableEvents sender, int row, int cell) {
237
 
238
					      selectRow(row);
239
					      String loc=table.getText(row,cell);
240
					      if (loc.compareTo(VALUE_UNKNOWN)!=0) {
241
					    	  mediator.onLocationSelected(table.getText(row,cell));
242
					      }
243
					      else {
244
					    	  mediator.onLocationSelected("000null");
245
					      }
246
 
247
					  }
248
 
249
		    });
250
 
251
		  	styleRow(selectedRow, false);
252
	        selector.getRowFormatter().addStyleName(0, "location-SelectedRow");
12 ddelon 253
		   // mediator.onLocationSelected("all");
10 ddelon 254
 
11 ddelon 255
 
12 ddelon 256
			//updateCount();
11 ddelon 257
			// update()
10 ddelon 258
 
259
			initWidget(outer);
260
 
261
 
262
  }
11 ddelon 263
 
264
		/**
265
		 * Recherche nombre d'enregistrement pour l'utilisateur en cours
266
		 *
267
		 *
268
		 */
10 ddelon 269
 
11 ddelon 270
		public void updateCount() {
271
 
272
			setStatusDisabled();
273
 
274
			HTTPRequest.asyncGet(serviceBaseUrl + "/InventoryLocationList/" + user ,
275
					new ResponseTextHandler() {
276
 
277
						public void onCompletion(String str) {
278
							JSONValue jsonValue = JSONParser.parse(str);
279
							JSONNumber jsonNumber;
280
							if ((jsonNumber = jsonValue.isNumber()) != null) {
281
								count = (int) jsonNumber.getValue();
12 ddelon 282
								mediator.onLocationUpdate(location);
11 ddelon 283
								update();
284
							}
285
						}
286
					});
287
 
288
		}
289
 
10 ddelon 290
 
291
		  private void selectRow(int row) {
292
 
293
		    styleRow(selectedRow, false);
294
		    styleRow(row, true);
295
 
296
		    selectedRow = row;
297
 
298
		  }
299
 
300
 
301
		  private void styleRow(int row, boolean selected) {
302
			    if (row != -1) {
303
				   selector.getRowFormatter().removeStyleName(0, "location-SelectedRow");
304
			      if (selected)
305
			        table.getRowFormatter().addStyleName(row, "location-SelectedRow");
306
			      else
307
			    	if (row < table.getRowCount()) {
308
			    		table.getRowFormatter().removeStyleName(row, "location-SelectedRow");
309
			    	}
310
			    }
311
			  }
312
 
313
 
314
	/**
315
	 *
316
	 * Mise a jour de l'affichage, à partir des données d'inventaire deja
317
	 * saisies. La valeur de this.startIndex permet de determiner quelles
318
	 * données seront affichées
319
	 *
320
	 */
321
 
322
		public void update() {
323
 
11 ddelon 324
 
325
			setStatusDisabled();
10 ddelon 326
 
327
			HTTPRequest.asyncGet(serviceBaseUrl + "/InventoryLocationList/" + user + "/"
328
					+ startIndex + "/" + VISIBLE_LOCATION_COUNT,
329
 
330
			new ResponseTextHandler() {
331
 
332
				public void onCompletion(String str) {
333
 
334
					JSONValue jsonValue = JSONParser.parse(str);
335
					JSONArray jsonArray;
336
					JSONArray jsonArrayNested;
11 ddelon 337
 
338
					int row=0;
339
					int i=0;
10 ddelon 340
 
341
					if ((jsonArray = jsonValue.isArray()) != null) {
342
						int arraySize = jsonArray.size();
11 ddelon 343
						for (i = 0; i < arraySize; ++i) {
10 ddelon 344
							if ((jsonArrayNested = jsonArray.get(i).isArray()) != null) {
11 ddelon 345
								if (i>=table.getRowCount()) {
346
									 row = table.insertRow(table.getRowCount());
347
								}
348
								else {
349
									row = i;
350
								}
10 ddelon 351
								// Lieu
352
 
353
								String loc=((JSONString)jsonArrayNested.get(0)).stringValue();
354
 
355
								if (loc.compareTo("000null")!=0) {
356
									table.setText(row, 0,loc);
357
								}
358
								else {
359
									table.setText(row, 0,VALUE_UNKNOWN);
360
								}
361
 
362
								if (loc.compareTo(location)==0) {
363
								  styleRow(row, true);
364
								}
11 ddelon 365
								else {
366
								  styleRow(row, false);
367
								}
10 ddelon 368
 
369
								table.getFlexCellFormatter().setWidth(row, 0, "100%");
370
							}
371
 
372
						}
373
					}
11 ddelon 374
 
375
					if (location.compareTo("all")==0) {
376
							selector.getRowFormatter().addStyleName(0, "location-SelectedRow");
377
					}
10 ddelon 378
 
11 ddelon 379
 
380
					// Suppression fin ancien affichage
381
					if (i<table.getRowCount()-1) {
382
						 for (int j = table.getRowCount() - 1; j > i-1; j--) {
383
							 table.removeRow(j);
384
						 }
385
					}
386
					setStatusEnabled();
387
 
388
 
10 ddelon 389
				}
390
			});
391
 
392
		}
393
 
394
		public void setLocation(String location) {
395
			this.location = location;
396
		}
397
 
398
 
11 ddelon 399
 
400
		/*
401
		 * Positionnement index de parcours (this.startIndex) pour affichage de la
402
		 * dernière page
403
		 *
404
		 * @param
405
		 * @return void
406
		 */
10 ddelon 407
 
11 ddelon 408
		private void gotoEnd() {
409
 
410
			if ((count == 0) || (count % VISIBLE_LOCATION_COUNT) > 0) {
411
				startIndex = count - (count % VISIBLE_LOCATION_COUNT);
412
			} else {
413
				startIndex = count - VISIBLE_LOCATION_COUNT;
414
			}
415
 
416
		}
417
 
418
 
419
		/**
420
		 * Affichage message d'attente et désactivation navigation
421
		 *
422
		 * @param
423
		 * @return void
424
		 */
425
 
426
		private void setStatusDisabled() {
427
 
428
			navBar.gotoFirst.setEnabled(false);
429
			navBar.gotoPrev.setEnabled(false);
430
			navBar.gotoNext.setEnabled(false);
431
			navBar.gotoEnd.setEnabled(false);
432
 
433
			setStatusText("Patientez ...");
434
		}
435
 
436
		/**
437
		 * Affichage numero de page et gestion de la navigation
438
		 *
439
		 */
440
 
441
		private void setStatusEnabled() {
442
 
443
			// Il y a forcemment un disabled avant d'arriver ici
444
 
445
			if (count > 0) {
446
 
447
				if (startIndex >= VISIBLE_LOCATION_COUNT) { // Au dela de la
448
															// premiere page
449
					navBar.gotoPrev.setEnabled(true);
450
					navBar.gotoFirst.setEnabled(true);
451
					if (startIndex < (count - VISIBLE_LOCATION_COUNT)) { // Pas la
452
																		// derniere
453
																		// page
454
						navBar.gotoNext.setEnabled(true);
455
						navBar.gotoEnd.setEnabled(true);
456
						setStatusText((startIndex + 1) + " - "
457
								+ (startIndex + VISIBLE_LOCATION_COUNT) + " sur " + count );
458
					} else { // Derniere page
459
						setStatusText((startIndex + 1) + " - " + count + " sur " + count );
460
					}
461
				} else { // Premiere page
462
					if (count > VISIBLE_LOCATION_COUNT) { // Des pages derrieres
463
						navBar.gotoNext.setEnabled(true);
464
						navBar.gotoEnd.setEnabled(true);
465
						setStatusText((startIndex + 1) + " - "
466
								+ (startIndex + VISIBLE_LOCATION_COUNT) + " sur " + count);
467
					} else {
468
						setStatusText((startIndex + 1) + " - " + count + " sur " + count);
469
					}
470
				}
471
			}
472
 
473
			else { // Pas d'inventaire, pas de navigation
474
				setStatusText("0 - 0 sur 0");
475
			}
476
		}
477
 
478
 
479
 
480
		private void setStatusText(String text) {
481
			navBar.status.setText(text);
482
		}
483
 
10 ddelon 484
}