Subversion Repositories eFlore/Archives.cel-v1

Rev

Details | Last modification | View Log | RSS feed

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