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
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
 
150
 
151
		private int count = 65000;
152
 
10 ddelon 153
		// Tous selectionné
154
		private int  selectedRow = -1;
155
 
156
		private Mediator mediator = null;
157
 
158
 
159
		public LocationList(Mediator med) {
160
 
161
			mediator=med;
162
 
163
			mediator.registerLocationList(this);
164
 
165
			user=mediator.getUser();
166
			serviceBaseUrl = mediator.getServiceBaseUrl();
167
 
11 ddelon 168
			navBar = new NavBar();
169
 
10 ddelon 170
			// Mise en forme du header
171
 
172
			header.setCellSpacing(0);
173
			header.setCellPadding(2);
174
			header.setWidth("100%");
175
 
176
			header.setStyleName("location-ListHeader");
177
 
178
 
13 ddelon 179
			header.setWidget(0, 1,navBar);
180
 
10 ddelon 181
 
182
 
11 ddelon 183
			// Mise en forme de l'entree "Toutes localités"
10 ddelon 184
 
185
			selector.setCellSpacing(0);
186
			selector.setCellPadding(0);
187
			selector.setWidth("100%");
188
 
189
			selector.setHTML(0, 0, "Toutes");
190
 
191
			selector.getCellFormatter().setWidth(0, 0, "100%");
192
 
193
 
194
			 // Hook up events.
195
			selector.addTableListener(new TableListener () {
196
 
197
				  public void onCellClicked(SourcesTableEvents sender, int row, int cell) {
198
					  	styleRow(selectedRow, false);
199
				        selector.getRowFormatter().addStyleName(0, "location-SelectedRow");
200
					    mediator.onLocationSelected("all");
201
					  }
202
 
203
		    });
204
 
205
			selector.setStyleName("location-ListElement");
206
 
207
 
11 ddelon 208
			// Mise en forme du contenu
10 ddelon 209
 
210
			table.setCellSpacing(0);
211
			table.setBorderWidth(0);
212
			table.setCellPadding(2);
213
			table.setWidth("100%");
214
 
215
			table.setStyleName("location-ListElement");
216
 
217
			// Mise en forme barre navigation
11 ddelon 218
 
219
			navBar.setWidth("100%");
220
 
10 ddelon 221
			outer.add(header);
11 ddelon 222
			inner.add(selector); // Toutes localités
10 ddelon 223
			inner.add(table);
224
			inner.setStyleName("location-List");
225
			inner.setWidth("100%");
226
			outer.setWidth("100%");
227
			outer.add(inner);
228
 
229
			 // Hook up events.
230
		    table.addTableListener(new TableListener () {
231
 
232
				  public void onCellClicked(SourcesTableEvents sender, int row, int cell) {
233
 
234
					      selectRow(row);
235
					      String loc=table.getText(row,cell);
236
					      if (loc.compareTo(VALUE_UNKNOWN)!=0) {
14 ddelon 237
					    	  location=loc;
10 ddelon 238
					    	  mediator.onLocationSelected(table.getText(row,cell));
239
					      }
240
					      else {
14 ddelon 241
					    	  location="000null";
10 ddelon 242
					    	  mediator.onLocationSelected("000null");
243
					      }
244
 
245
					  }
246
 
247
		    });
248
 
249
		  	styleRow(selectedRow, false);
250
	        selector.getRowFormatter().addStyleName(0, "location-SelectedRow");
12 ddelon 251
		   // mediator.onLocationSelected("all");
10 ddelon 252
 
11 ddelon 253
 
12 ddelon 254
			//updateCount();
11 ddelon 255
			// update()
10 ddelon 256
 
257
			initWidget(outer);
258
 
259
 
260
  }
11 ddelon 261
 
262
		/**
263
		 * Recherche nombre d'enregistrement pour l'utilisateur en cours
264
		 *
265
		 *
266
		 */
10 ddelon 267
 
11 ddelon 268
		public void updateCount() {
269
 
270
			setStatusDisabled();
271
 
14 ddelon 272
//			HTTPRequest.asyncGet(serviceBaseUrl + "/InventoryLocationList/" + user + "/" + location + "/"  ,
273
			HTTPRequest.asyncGet(serviceBaseUrl + "/InventoryLocationList/" + user ,
11 ddelon 274
					new ResponseTextHandler() {
275
 
276
						public void onCompletion(String str) {
277
							JSONValue jsonValue = JSONParser.parse(str);
278
							JSONNumber jsonNumber;
279
							if ((jsonNumber = jsonValue.isNumber()) != null) {
280
								count = (int) jsonNumber.getValue();
281
								update();
282
							}
283
						}
284
					});
285
 
286
		}
287
 
10 ddelon 288
 
289
		  private void selectRow(int row) {
290
 
291
		    styleRow(selectedRow, false);
292
		    styleRow(row, true);
293
 
294
		    selectedRow = row;
295
 
296
		  }
297
 
298
 
299
		  private void styleRow(int row, boolean selected) {
300
			    if (row != -1) {
301
				   selector.getRowFormatter().removeStyleName(0, "location-SelectedRow");
302
			      if (selected)
303
			        table.getRowFormatter().addStyleName(row, "location-SelectedRow");
304
			      else
305
			    	if (row < table.getRowCount()) {
306
			    		table.getRowFormatter().removeStyleName(row, "location-SelectedRow");
307
			    	}
308
			    }
309
			  }
310
 
311
 
312
	/**
313
	 *
314
	 * Mise a jour de l'affichage, à partir des données d'inventaire deja
315
	 * saisies. La valeur de this.startIndex permet de determiner quelles
316
	 * données seront affichées
317
	 *
318
	 */
319
 
320
		public void update() {
321
 
11 ddelon 322
 
323
			setStatusDisabled();
10 ddelon 324
 
14 ddelon 325
//			HTTPRequest.asyncGet(serviceBaseUrl + "/InventoryLocationList/" + user + "/" + location + "/" +
326
			HTTPRequest.asyncGet(serviceBaseUrl + "/InventoryLocationList/" + user + "/" +
10 ddelon 327
					+ startIndex + "/" + VISIBLE_LOCATION_COUNT,
328
 
329
			new ResponseTextHandler() {
330
 
331
				public void onCompletion(String str) {
332
 
333
					JSONValue jsonValue = JSONParser.parse(str);
334
					JSONArray jsonArray;
335
					JSONArray jsonArrayNested;
11 ddelon 336
 
337
					int row=0;
338
					int i=0;
10 ddelon 339
 
340
					if ((jsonArray = jsonValue.isArray()) != null) {
341
						int arraySize = jsonArray.size();
11 ddelon 342
						for (i = 0; i < arraySize; ++i) {
10 ddelon 343
							if ((jsonArrayNested = jsonArray.get(i).isArray()) != null) {
11 ddelon 344
								if (i>=table.getRowCount()) {
345
									 row = table.insertRow(table.getRowCount());
346
								}
347
								else {
348
									row = i;
349
								}
10 ddelon 350
								// Lieu
351
 
352
								String loc=((JSONString)jsonArrayNested.get(0)).stringValue();
353
 
354
								if (loc.compareTo("000null")!=0) {
355
									table.setText(row, 0,loc);
356
								}
357
								else {
358
									table.setText(row, 0,VALUE_UNKNOWN);
359
								}
360
 
361
								if (loc.compareTo(location)==0) {
362
								  styleRow(row, true);
363
								}
11 ddelon 364
								else {
365
								  styleRow(row, false);
366
								}
10 ddelon 367
 
368
								table.getFlexCellFormatter().setWidth(row, 0, "100%");
369
							}
370
 
371
						}
372
					}
11 ddelon 373
 
374
					if (location.compareTo("all")==0) {
375
							selector.getRowFormatter().addStyleName(0, "location-SelectedRow");
376
					}
10 ddelon 377
 
11 ddelon 378
 
379
					// Suppression fin ancien affichage
13 ddelon 380
					if (i<table.getRowCount()) {
381
						 for (int j = table.getRowCount() -1 ; j >= i; j--) {
11 ddelon 382
							 table.removeRow(j);
383
						 }
384
					}
13 ddelon 385
 
11 ddelon 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
		}
14 ddelon 483
 
484
		public void setUser(String user) {
485
			this.user = user;
486
		}
11 ddelon 487
 
10 ddelon 488
}