Subversion Repositories eFlore/Archives.cel-v1

Rev

Rev 12 | 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";
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
 
13 ddelon 180
			header.setWidget(0, 1,navBar);
181
 
10 ddelon 182
 
183
 
11 ddelon 184
			// Mise en forme de l'entree "Toutes localités"
10 ddelon 185
 
186
			selector.setCellSpacing(0);
187
			selector.setCellPadding(0);
188
			selector.setWidth("100%");
189
 
190
			selector.setHTML(0, 0, "Toutes");
191
 
192
			selector.getCellFormatter().setWidth(0, 0, "100%");
193
 
194
 
195
			 // Hook up events.
196
			selector.addTableListener(new TableListener () {
197
 
198
				  public void onCellClicked(SourcesTableEvents sender, int row, int cell) {
199
					  	styleRow(selectedRow, false);
200
				        selector.getRowFormatter().addStyleName(0, "location-SelectedRow");
201
					    mediator.onLocationSelected("all");
202
					  }
203
 
204
		    });
205
 
206
			selector.setStyleName("location-ListElement");
207
 
208
 
11 ddelon 209
			// Mise en forme du contenu
10 ddelon 210
 
211
			table.setCellSpacing(0);
212
			table.setBorderWidth(0);
213
			table.setCellPadding(2);
214
			table.setWidth("100%");
215
 
216
			table.setStyleName("location-ListElement");
217
 
218
			// Mise en forme barre navigation
11 ddelon 219
 
220
			navBar.setWidth("100%");
221
 
10 ddelon 222
			outer.add(header);
11 ddelon 223
			inner.add(selector); // Toutes localités
10 ddelon 224
			inner.add(table);
225
			inner.setStyleName("location-List");
226
			inner.setWidth("100%");
227
			outer.setWidth("100%");
228
			outer.add(inner);
229
 
230
			 // Hook up events.
231
		    table.addTableListener(new TableListener () {
232
 
233
				  public void onCellClicked(SourcesTableEvents sender, int row, int cell) {
234
 
235
					      selectRow(row);
236
					      String loc=table.getText(row,cell);
237
					      if (loc.compareTo(VALUE_UNKNOWN)!=0) {
238
					    	  mediator.onLocationSelected(table.getText(row,cell));
239
					      }
240
					      else {
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
 
13 ddelon 271
			HTTPRequest.asyncGet(serviceBaseUrl + "/InventoryLocationList/" + user + "/" + location ,
11 ddelon 272
					new ResponseTextHandler() {
273
 
274
						public void onCompletion(String str) {
275
							JSONValue jsonValue = JSONParser.parse(str);
276
							JSONNumber jsonNumber;
277
							if ((jsonNumber = jsonValue.isNumber()) != null) {
278
								count = (int) jsonNumber.getValue();
12 ddelon 279
								mediator.onLocationUpdate(location);
11 ddelon 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
 
13 ddelon 324
			HTTPRequest.asyncGet(serviceBaseUrl + "/InventoryLocationList/" + user + "/" + location + "/"
10 ddelon 325
					+ startIndex + "/" + VISIBLE_LOCATION_COUNT,
326
 
327
			new ResponseTextHandler() {
328
 
329
				public void onCompletion(String str) {
330
 
331
					JSONValue jsonValue = JSONParser.parse(str);
332
					JSONArray jsonArray;
333
					JSONArray jsonArrayNested;
11 ddelon 334
 
335
					int row=0;
336
					int i=0;
10 ddelon 337
 
338
					if ((jsonArray = jsonValue.isArray()) != null) {
339
						int arraySize = jsonArray.size();
11 ddelon 340
						for (i = 0; i < arraySize; ++i) {
10 ddelon 341
							if ((jsonArrayNested = jsonArray.get(i).isArray()) != null) {
11 ddelon 342
								if (i>=table.getRowCount()) {
343
									 row = table.insertRow(table.getRowCount());
344
								}
345
								else {
346
									row = i;
347
								}
10 ddelon 348
								// Lieu
349
 
350
								String loc=((JSONString)jsonArrayNested.get(0)).stringValue();
351
 
352
								if (loc.compareTo("000null")!=0) {
353
									table.setText(row, 0,loc);
354
								}
355
								else {
356
									table.setText(row, 0,VALUE_UNKNOWN);
357
								}
358
 
359
								if (loc.compareTo(location)==0) {
360
								  styleRow(row, true);
361
								}
11 ddelon 362
								else {
363
								  styleRow(row, false);
364
								}
10 ddelon 365
 
366
								table.getFlexCellFormatter().setWidth(row, 0, "100%");
367
							}
368
 
369
						}
370
					}
11 ddelon 371
 
372
					if (location.compareTo("all")==0) {
373
							selector.getRowFormatter().addStyleName(0, "location-SelectedRow");
374
					}
10 ddelon 375
 
11 ddelon 376
 
377
					// Suppression fin ancien affichage
13 ddelon 378
					if (i<table.getRowCount()) {
379
						 for (int j = table.getRowCount() -1 ; j >= i; j--) {
11 ddelon 380
							 table.removeRow(j);
381
						 }
382
					}
13 ddelon 383
 
11 ddelon 384
					setStatusEnabled();
385
 
386
 
10 ddelon 387
				}
388
			});
389
 
390
		}
391
 
392
		public void setLocation(String location) {
393
			this.location = location;
394
		}
395
 
396
 
11 ddelon 397
 
398
		/*
399
		 * Positionnement index de parcours (this.startIndex) pour affichage de la
400
		 * dernière page
401
		 *
402
		 * @param
403
		 * @return void
404
		 */
10 ddelon 405
 
11 ddelon 406
		private void gotoEnd() {
407
 
408
			if ((count == 0) || (count % VISIBLE_LOCATION_COUNT) > 0) {
409
				startIndex = count - (count % VISIBLE_LOCATION_COUNT);
410
			} else {
411
				startIndex = count - VISIBLE_LOCATION_COUNT;
412
			}
413
 
414
		}
415
 
416
 
417
		/**
418
		 * Affichage message d'attente et désactivation navigation
419
		 *
420
		 * @param
421
		 * @return void
422
		 */
423
 
424
		private void setStatusDisabled() {
425
 
426
			navBar.gotoFirst.setEnabled(false);
427
			navBar.gotoPrev.setEnabled(false);
428
			navBar.gotoNext.setEnabled(false);
429
			navBar.gotoEnd.setEnabled(false);
430
 
431
			setStatusText("Patientez ...");
432
		}
433
 
434
		/**
435
		 * Affichage numero de page et gestion de la navigation
436
		 *
437
		 */
438
 
439
		private void setStatusEnabled() {
440
 
441
			// Il y a forcemment un disabled avant d'arriver ici
442
 
443
			if (count > 0) {
444
 
445
				if (startIndex >= VISIBLE_LOCATION_COUNT) { // Au dela de la
446
															// premiere page
447
					navBar.gotoPrev.setEnabled(true);
448
					navBar.gotoFirst.setEnabled(true);
449
					if (startIndex < (count - VISIBLE_LOCATION_COUNT)) { // Pas la
450
																		// derniere
451
																		// page
452
						navBar.gotoNext.setEnabled(true);
453
						navBar.gotoEnd.setEnabled(true);
454
						setStatusText((startIndex + 1) + " - "
455
								+ (startIndex + VISIBLE_LOCATION_COUNT) + " sur " + count );
456
					} else { // Derniere page
457
						setStatusText((startIndex + 1) + " - " + count + " sur " + count );
458
					}
459
				} else { // Premiere page
460
					if (count > VISIBLE_LOCATION_COUNT) { // Des pages derrieres
461
						navBar.gotoNext.setEnabled(true);
462
						navBar.gotoEnd.setEnabled(true);
463
						setStatusText((startIndex + 1) + " - "
464
								+ (startIndex + VISIBLE_LOCATION_COUNT) + " sur " + count);
465
					} else {
466
						setStatusText((startIndex + 1) + " - " + count + " sur " + count);
467
					}
468
				}
469
			}
470
 
471
			else { // Pas d'inventaire, pas de navigation
472
				setStatusText("0 - 0 sur 0");
473
			}
474
		}
475
 
476
 
477
 
478
		private void setStatusText(String text) {
479
			navBar.status.setText(text);
480
		}
481
 
10 ddelon 482
}