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
14 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
 
26 ddelon 19
import com.google.gwt.http.client.URL;
14 ddelon 20
import com.google.gwt.json.client.JSONArray;
21
import com.google.gwt.json.client.JSONNumber;
22
import com.google.gwt.json.client.JSONParser;
23
import com.google.gwt.json.client.JSONString;
24
import com.google.gwt.json.client.JSONValue;
25
import com.google.gwt.user.client.HTTPRequest;
26
import com.google.gwt.user.client.ResponseTextHandler;
27
import com.google.gwt.user.client.ui.Button;
28
import com.google.gwt.user.client.ui.ClickListener;
29
import com.google.gwt.user.client.ui.Composite;
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 StationList extends Composite {
45
 
46
 
26 ddelon 47
	// Debut Barre de navigation
14 ddelon 48
 
49
	private class NavBar extends Composite implements ClickListener {
50
 
51
		public final Button gotoFirst = new Button("<<", this);
52
		public final Button gotoNext = new Button(">", this);
53
		public final Button gotoPrev = new Button("<", this);
54
		public final Button gotoEnd = new Button(">>", this);
55
		public final HTML status = new HTML();
56
 
57
 
58
		public NavBar() {
26 ddelon 59
 
60
 
61
			HorizontalPanel bar = new HorizontalPanel();
62
 
14 ddelon 63
			bar.setStyleName("navbar");
64
			status.setStyleName("status");
65
 
66
 
26 ddelon 67
			bar.add(status);
68
			bar.setCellHorizontalAlignment(status, HasHorizontalAlignment.ALIGN_RIGHT);
69
			bar.setCellVerticalAlignment(status, HasVerticalAlignment.ALIGN_MIDDLE);
70
			bar.setCellWidth(status, "100%");
14 ddelon 71
 
26 ddelon 72
			bar.add(gotoFirst);
73
			bar.add(gotoPrev);
74
			bar.add(gotoNext);
75
			bar.add(gotoEnd);
14 ddelon 76
 
77
 
26 ddelon 78
			initWidget(bar);
14 ddelon 79
 
80
		}
26 ddelon 81
 
14 ddelon 82
 
83
		public void onClick(Widget sender) {
84
			if (sender == gotoNext) {
85
				// Move forward a page.
86
				startIndex += VISIBLE_STATION_COUNT;
87
				if (startIndex >= count)
88
					startIndex -= VISIBLE_STATION_COUNT;
89
			} else {
90
				if (sender == gotoPrev) {
91
					// Move back a page.
92
					startIndex -= VISIBLE_STATION_COUNT;
93
					if (startIndex < 0)
94
						startIndex = 0;
95
				} else {
96
					if (sender == gotoEnd) {
97
						gotoEnd();
98
					} else {
99
						if (sender == gotoFirst) {
100
							startIndex = 0;
101
						}
102
					}
103
				}
104
			}
105
			update();
106
		}
107
 
108
	}
109
 
26 ddelon 110
	// Fin Barre de navigation
111
 
14 ddelon 112
		private static final int VISIBLE_STATION_COUNT = 10;
113
		private static final String VALUE_UNKNOWN = "Inconnues";
114
 
115
		private Grid header = new Grid(1, 2);
116
 
117
		private Grid selector = new Grid(1, 2);
118
 
119
		private FlexTable table = new FlexTable();
120
 
121
		private VerticalPanel outer = new VerticalPanel();
122
		private VerticalPanel inner = new VerticalPanel();
123
 
124
		private int startIndex = 0;
125
 
126
		private String user;
127
 
128
		private String serviceBaseUrl = null;
129
 
130
		private String location = "all";
131
		private String station = "all";
132
 
133
 
134
		private NavBar navBar=null;
135
 
136
 
137
		private int count = 65000;
138
 
139
		// Tous selectionné
140
		private int  selectedRow = -1;
141
 
142
		private Mediator mediator = null;
143
 
144
 
145
		public StationList(Mediator med) {
146
 
147
			mediator=med;
148
 
149
			mediator.registerStationList(this);
150
 
151
			user=mediator.getUser();
152
			serviceBaseUrl = mediator.getServiceBaseUrl();
153
 
154
			navBar = new NavBar();
155
 
156
			// Mise en forme du header
157
 
158
 
159
 
160
			header.setCellSpacing(0);
161
			header.setCellPadding(2);
162
			header.setWidth("100%");
163
 
164
			header.setStyleName("station-ListHeader");
165
			header.setWidget(0, 1,navBar);
166
 
167
			selector.setCellSpacing(0);
168
			selector.setCellPadding(0);
169
			selector.setWidth("100%");
170
 
171
			selector.setHTML(0, 0, "Toutes");
172
 
173
			selector.getCellFormatter().setWidth(0, 0, "100%");
174
 
175
 
176
			 // Hook up events.
177
			selector.addTableListener(new TableListener () {
178
 
179
				  public void onCellClicked(SourcesTableEvents sender, int row, int cell) {
180
					  	styleRow(selectedRow, false);
181
				        selector.getRowFormatter().addStyleName(0, "station-SelectedRow");
182
					    mediator.onStationSelected("all");
183
					    station="all";
184
					  }
185
 
186
		    });
187
 
188
			selector.setStyleName("station-ListElement");
189
 
190
 
191
			// Mise en forme du contenu
192
 
193
			table.setCellSpacing(0);
194
			table.setBorderWidth(0);
195
			table.setCellPadding(2);
196
			table.setWidth("100%");
197
 
198
			table.setStyleName("station-ListElement");
199
 
200
 
201
			outer.add(header);
202
			inner.add(selector); // Toutes station
203
			inner.add(table);
204
			inner.setStyleName("station-List");
205
			inner.setWidth("100%");
206
			outer.setWidth("100%");
207
			outer.add(inner);
208
 
209
			 // Hook up events.
210
		    table.addTableListener(new TableListener () {
211
 
212
				  public void onCellClicked(SourcesTableEvents sender, int row, int cell) {
213
 
214
					      selectRow(row);
215
					      String astation=table.getText(row,cell);
216
 
217
					      if (astation.compareTo(VALUE_UNKNOWN)!=0) {
218
					    	  station=astation;
219
					    	  mediator.onStationSelected(astation);
220
					      }
221
					      else {
222
					    	  station="000null";
223
					    	  mediator.onStationSelected("000null");
224
					      }
225
 
226
 
227
					  }
228
 
229
		    });
230
 
231
		  	styleRow(selectedRow, false);
232
	        selector.getRowFormatter().addStyleName(0, "station-SelectedRow");
233
 
234
			initWidget(outer);
235
 
236
 
237
  }
238
 
239
		/**
240
		 * Recherche nombre d'enregistrement pour l'utilisateur en cours
241
		 *
242
		 *
243
		 */
244
 
245
		public void updateCount() {
246
 
247
			setStatusDisabled();
248
 
26 ddelon 249
			HTTPRequest.asyncGet(serviceBaseUrl + "/InventoryStationList/" + user + "/" + URL.encodeComponent(location) ,
14 ddelon 250
					new ResponseTextHandler() {
251
 
252
						public void onCompletion(String str) {
253
							JSONValue jsonValue = JSONParser.parse(str);
254
							JSONNumber jsonNumber;
255
							if ((jsonNumber = jsonValue.isNumber()) != null) {
256
								count = (int) jsonNumber.getValue();
257
								update();
258
							}
259
						}
260
					});
261
 
262
		}
263
 
264
 
265
		  private void selectRow(int row) {
266
 
267
		    styleRow(selectedRow, false);
268
		    styleRow(row, true);
269
 
270
		    selectedRow = row;
271
 
272
		  }
273
 
274
 
275
		  private void styleRow(int row, boolean selected) {
276
			    if (row != -1) {
277
				   selector.getRowFormatter().removeStyleName(0, "station-SelectedRow");
278
			      if (selected)
279
			        table.getRowFormatter().addStyleName(row, "station-SelectedRow");
280
			      else
281
			    	if (row < table.getRowCount()) {
282
			    		table.getRowFormatter().removeStyleName(row, "station-SelectedRow");
283
			    	}
284
			    }
285
			  }
286
 
287
 
288
	/**
289
	 *
290
	 * Mise a jour de l'affichage, à partir des données d'inventaire deja
291
	 * saisies. La valeur de this.startIndex permet de determiner quelles
292
	 * données seront affichées
293
	 *
294
	 */
295
 
296
		public void update() {
297
 
298
 
299
			setStatusDisabled();
300
 
26 ddelon 301
			HTTPRequest.asyncGet(serviceBaseUrl + "/InventoryStationList/" + user + "/" + URL.encodeComponent(location) + "/"
14 ddelon 302
					+ startIndex + "/" + VISIBLE_STATION_COUNT,
303
 
304
			new ResponseTextHandler() {
305
 
306
				public void onCompletion(String str) {
307
 
308
 
309
 
310
					JSONValue jsonValue = JSONParser.parse(str);
311
					JSONArray jsonArray;
312
					JSONArray jsonArrayNested;
313
 
314
					int row=0;
315
					int i=0;
316
 
317
					if ((jsonArray = jsonValue.isArray()) != null) {
318
						int arraySize = jsonArray.size();
319
						for (i = 0; i < arraySize; ++i) {
320
							if ((jsonArrayNested = jsonArray.get(i).isArray()) != null) {
321
								if (i>=table.getRowCount()) {
322
									 row = table.insertRow(table.getRowCount());
323
								}
324
								else {
325
									row = i;
326
								}
327
 
328
								String astation=((JSONString)jsonArrayNested.get(0)).stringValue();
329
 
330
								if (astation.compareTo("000null")!=0) {
331
									table.setText(row, 0,astation);
332
								}
333
								else {
334
									table.setText(row, 0,VALUE_UNKNOWN);
335
								}
336
 
337
								if (astation.compareTo(station)==0) {
338
								  styleRow(row, true);
339
								}
340
								else {
341
								  styleRow(row, false);
342
								}
343
 
344
								table.getFlexCellFormatter().setWidth(row, 0, "100%");
345
 
346
							}
347
 
348
						}
349
					}
350
 
351
					if (station.compareTo("all")==0) {
352
							selector.getRowFormatter().addStyleName(0, "station-SelectedRow");
353
					}
354
 
355
 
356
					// Suppression fin ancien affichage
357
					if (i<table.getRowCount()) {
358
						 for (int j = table.getRowCount() -1 ; j >= i; j--) {
359
							 table.removeRow(j);
360
						 }
361
					}
362
 
363
					setStatusEnabled();
364
 
365
 
366
				}
367
			});
368
 
369
		}
370
 
371
		public void setLocation(String location) {
372
			this.location = location;
373
		}
374
 
375
 
376
 
377
		/*
378
		 * Positionnement index de parcours (this.startIndex) pour affichage de la
379
		 * dernière page
380
		 *
381
		 * @param
382
		 * @return void
383
		 */
384
 
385
		private void gotoEnd() {
386
 
387
			if ((count == 0) || (count % VISIBLE_STATION_COUNT) > 0) {
388
				startIndex = count - (count % VISIBLE_STATION_COUNT);
389
			} else {
390
				startIndex = count - VISIBLE_STATION_COUNT;
391
			}
392
 
393
		}
394
 
395
 
396
		/**
397
		 * Affichage message d'attente et désactivation navigation
398
		 *
399
		 * @param
400
		 * @return void
401
		 */
402
 
403
		private void setStatusDisabled() {
404
 
405
			navBar.gotoFirst.setEnabled(false);
406
			navBar.gotoPrev.setEnabled(false);
407
			navBar.gotoNext.setEnabled(false);
408
			navBar.gotoEnd.setEnabled(false);
409
 
26 ddelon 410
			navBar.status.setText("Patientez ...");
14 ddelon 411
		}
412
 
413
		/**
414
		 * Affichage numero de page et gestion de la navigation
415
		 *
416
		 */
417
 
418
		private void setStatusEnabled() {
419
 
420
			// Il y a forcemment un disabled avant d'arriver ici
421
 
422
			if (count > 0) {
423
 
424
				if (startIndex >= VISIBLE_STATION_COUNT) { // Au dela de la
425
															// premiere page
426
					navBar.gotoPrev.setEnabled(true);
427
					navBar.gotoFirst.setEnabled(true);
428
					if (startIndex < (count - VISIBLE_STATION_COUNT)) { // Pas la
429
																		// derniere
430
																		// page
431
						navBar.gotoNext.setEnabled(true);
432
						navBar.gotoEnd.setEnabled(true);
26 ddelon 433
						navBar.status.setText((startIndex + 1) + " - "
14 ddelon 434
								+ (startIndex + VISIBLE_STATION_COUNT) + " sur " + count );
435
					} else { // Derniere page
26 ddelon 436
						navBar.status.setText((startIndex + 1) + " - " + count + " sur " + count );
14 ddelon 437
					}
438
				} else { // Premiere page
439
					if (count > VISIBLE_STATION_COUNT) { // Des pages derrieres
440
						navBar.gotoNext.setEnabled(true);
441
						navBar.gotoEnd.setEnabled(true);
26 ddelon 442
						navBar.status.setText((startIndex + 1) + " - "
14 ddelon 443
								+ (startIndex + VISIBLE_STATION_COUNT) + " sur " + count);
444
					} else {
26 ddelon 445
						navBar.status.setText((startIndex + 1) + " - " + count + " sur " + count);
14 ddelon 446
					}
447
				}
448
			}
449
 
450
			else { // Pas d'inventaire, pas de navigation
26 ddelon 451
				navBar.status.setText("0 - 0 sur 0");
14 ddelon 452
			}
453
		}
454
 
455
 
456
 
457
		public void setUser(String user) {
458
			this.user = user;
459
		}
460
 
461
 
462
		public void setStation(String station) {
463
			this.station = station;
464
		}
465
 
466
 
467
}