Subversion Repositories eFlore/Archives.cel-v1

Rev

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