Subversion Repositories eFlore/Archives.cel-v1

Rev

Rev 5 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 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
 
4 ddelon 18
import com.google.gwt.i18n.client.Dictionary;
2 ddelon 19
import com.google.gwt.json.client.JSONArray;
3 ddelon 20
import com.google.gwt.json.client.JSONNumber;
2 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;
26
import com.google.gwt.user.client.ui.Composite;
27
import com.google.gwt.user.client.ui.FlexTable;
28
import com.google.gwt.user.client.ui.Grid;
3 ddelon 29
import com.google.gwt.user.client.ui.HTML;
30
import com.google.gwt.user.client.ui.HorizontalPanel;
2 ddelon 31
import com.google.gwt.user.client.ui.VerticalPanel;
3 ddelon 32
import com.google.gwt.user.client.ui.DockPanel;
33
import com.google.gwt.user.client.ui.Button;
2 ddelon 34
import com.google.gwt.user.client.ui.CheckBox;
3 ddelon 35
import com.google.gwt.user.client.ui.Widget;
4 ddelon 36
import com.google.gwt.user.client.ui.ClickListener;
37
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
38
import com.google.gwt.user.client.ui.HasVerticalAlignment;
2 ddelon 39
 
6 ddelon 40
/*
41
 * Le retour de getUser appelle getCount qui appelle update pour veiller à une
42
 * initialisation correcte
43
 *
44
 */
2 ddelon 45
 
6 ddelon 46
public class TaxonList extends Composite implements
47
		AutoCompleteAsyncTextBoxListener {
4 ddelon 48
 
6 ddelon 49
	// Barre de navigation
5 ddelon 50
 
6 ddelon 51
	private class NavBar extends Composite implements ClickListener {
2 ddelon 52
 
6 ddelon 53
		public final DockPanel bar = new DockPanel();
2 ddelon 54
 
6 ddelon 55
		public final Button gotoFirst = new Button("<<", this);
3 ddelon 56
 
6 ddelon 57
		public final Button gotoNext = new Button(">", this);
3 ddelon 58
 
6 ddelon 59
		public final Button gotoPrev = new Button("<", this);
3 ddelon 60
 
6 ddelon 61
		public final Button gotoEnd = new Button(">>", this);
3 ddelon 62
 
6 ddelon 63
		public final HTML status = new HTML();
3 ddelon 64
 
6 ddelon 65
		public NavBar() {
66
			initWidget(bar);
67
			bar.setStyleName("navbar");
68
			status.setStyleName("status");
3 ddelon 69
 
6 ddelon 70
			HorizontalPanel buttons = new HorizontalPanel();
71
			buttons.add(gotoFirst);
72
			buttons.add(gotoPrev);
73
			buttons.add(gotoNext);
74
			buttons.add(gotoEnd);
75
			bar.add(buttons, DockPanel.EAST);
76
			bar.setCellHorizontalAlignment(buttons, DockPanel.ALIGN_RIGHT);
77
			bar.add(status, DockPanel.CENTER);
78
			bar.setVerticalAlignment(DockPanel.ALIGN_MIDDLE);
79
			bar.setCellHorizontalAlignment(status,
80
					HasHorizontalAlignment.ALIGN_RIGHT);
81
			bar.setCellVerticalAlignment(status,
82
					HasVerticalAlignment.ALIGN_MIDDLE);
83
			bar.setCellWidth(status, "100%");
3 ddelon 84
 
6 ddelon 85
		}
3 ddelon 86
 
6 ddelon 87
		public void onClick(Widget sender) {
88
			if (sender == gotoNext) {
89
				// Move forward a page.
90
				startIndex += VISIBLE_TAXON_COUNT;
91
				if (startIndex >= count)
92
					startIndex -= VISIBLE_TAXON_COUNT;
93
			} else {
94
				if (sender == gotoPrev) {
95
					// Move back a page.
96
					startIndex -= VISIBLE_TAXON_COUNT;
97
					if (startIndex < 0)
98
						startIndex = 0;
99
				} else {
100
					if (sender == gotoEnd) {
101
						gotoEnd();
102
					} else {
103
						if (sender == gotoFirst) {
104
							startIndex = 0;
105
						}
106
					}
107
				}
108
			}
109
			update();
110
		}
3 ddelon 111
 
6 ddelon 112
	}
2 ddelon 113
 
6 ddelon 114
	private void setStatusText(String text) {
115
		navBar.status.setText(text);
116
	}
4 ddelon 117
 
6 ddelon 118
	private static final int VISIBLE_TAXON_COUNT = 15;
2 ddelon 119
 
6 ddelon 120
	private Grid header = new Grid(1, 6);
3 ddelon 121
 
6 ddelon 122
	private FlexTable table = new FlexTable();
2 ddelon 123
 
6 ddelon 124
	private VerticalPanel panel = new VerticalPanel();
5 ddelon 125
 
6 ddelon 126
	private int startIndex = 0;
3 ddelon 127
 
6 ddelon 128
	private String serviceBaseUrl = getServiceBaseUrl();
2 ddelon 129
 
6 ddelon 130
	private int count = 65000;
4 ddelon 131
 
6 ddelon 132
	private String user;
4 ddelon 133
 
6 ddelon 134
	private NavBar navBar = new NavBar();
2 ddelon 135
 
6 ddelon 136
	public TaxonList() {
5 ddelon 137
 
6 ddelon 138
		initAsync();
2 ddelon 139
 
6 ddelon 140
		// Information complementaire : un tableau associe au retour de
141
		// l'assistant de saisie
5 ddelon 142
 
6 ddelon 143
		// Mise en forme du header
2 ddelon 144
 
6 ddelon 145
		header.setCellSpacing(0);
146
		header.setCellPadding(2);
147
		header.setWidth("100%");
2 ddelon 148
 
6 ddelon 149
		header.setStyleName("taxon-ListHeader");
4 ddelon 150
 
6 ddelon 151
		header.setText(0, 0, "");
152
		header.setText(0, 1, "Nom saisi");
153
		header.setText(0, 2, "Nom retenu");
154
		header.setHTML(0, 3, "Code<br>Nomenclatural");
155
		header.setHTML(0, 4, "Code<br>Taxonomique");
156
		header.setText(0, 5, "Famille");
5 ddelon 157
 
6 ddelon 158
		header.getCellFormatter().setWidth(0, 0, "2%");
159
		header.getCellFormatter().setWidth(0, 1, "31%");
160
		header.getCellFormatter().setWidth(0, 2, "31%");
161
		header.getCellFormatter().setWidth(0, 3, "9%");
162
		header.getCellFormatter().setWidth(0, 4, "9%");
163
		header.getCellFormatter().setWidth(0, 5, "18%");
4 ddelon 164
 
6 ddelon 165
		// Mise en forme de la table.
4 ddelon 166
 
6 ddelon 167
		table.setCellSpacing(0);
168
		table.setBorderWidth(0);
169
		table.setCellPadding(2);
170
		table.setWidth("100%");
4 ddelon 171
 
6 ddelon 172
		// Mise en forme barre navigation
4 ddelon 173
 
6 ddelon 174
		navBar.setWidth("100%");
4 ddelon 175
 
6 ddelon 176
		table.setStyleName("taxon-List");
4 ddelon 177
 
6 ddelon 178
		panel.add(navBar);
179
		panel.add(header);
180
		panel.add(table);
4 ddelon 181
 
6 ddelon 182
		initWidget(panel);
4 ddelon 183
 
6 ddelon 184
	}
5 ddelon 185
 
6 ddelon 186
	/**
187
	 * Action lancee par la selection d'un nom dans l'assistant de saisie. Lance
188
	 * la recherche d'informations complémentaires (famille, numero
189
	 * nomenclaturaux etc) et met a jour l'inventaire (addelement())
190
	 *
191
	 * @return void
192
	 */
193
	public void onValidate(SourcesAutoCompleteAsyncTextBoxEvents sender,
194
			 final String str,final String value) {
3 ddelon 195
 
6 ddelon 196
		setStatusDisabled();
2 ddelon 197
 
6 ddelon 198
		// Saisie assistée
2 ddelon 199
 
6 ddelon 200
		if (value !=null) {
2 ddelon 201
 
202
 
6 ddelon 203
			HTTPRequest.asyncGet(serviceBaseUrl + "/NameValid/" + value,
204
					new ResponseTextHandler() {
2 ddelon 205
 
6 ddelon 206
						public void onCompletion(String strcomplete) {
2 ddelon 207
 
6 ddelon 208
							JSONValue jsonValue = JSONParser.parse(strcomplete);
209
							JSONArray jsonArray;
210
 
211
							if ((jsonArray = jsonValue.isArray()) != null) {
212
								// Nom retenu, Num Nomen nom retenu, Num Taxon,
213
								// Famille
214
								addElement(str, value,
215
										((JSONString) jsonArray.get(0))
216
												.stringValue(),
217
										((JSONString) jsonArray.get(1))
218
												.stringValue(),
219
										((JSONString) jsonArray.get(2))
220
												.stringValue(),
221
										((JSONString) jsonArray.get(3))
222
												.stringValue());
223
							}
224
						}
225
 
226
					});
227
		}
228
		// Saisie libre
229
		else {
230
			addElement(str, " ", " ", " ", " ", " ");
231
		}
232
	}
233
 
234
	/**
235
	 * Ajoute un element à l'inventaire
236
	 *
237
	 * @param nom_sel :
238
	 *            nom selectionne
239
	 * @param num_nom_sel :
240
	 *            numero nomenclatural nom selectionne
241
	 * @param nom_ret :
242
	 *            nom retenu
243
	 * @param num_nom_ret :
244
	 *            numero nomenclaturel nom retenu
245
	 * @param num_taxon :
246
	 *            numero taxonomique
247
	 * @param famille :
248
	 *            famille
249
	 */
250
 
251
	public void addElement(String nom_sel, String num_nom_sel, String nom_ret,
252
			String num_nom_ret, String num_taxon, String famille) {
253
 
254
		// Calcul du nouveau numéro d'ordre
255
 
256
		/*
257
		 * int order=1; if (table.getRowCount()>0) { order=new
258
		 * Integer(table.getText(table.getRowCount()-1,6)).intValue()+1; }
259
		 */
260
 
261
		count++;
262
		HTTPRequest.asyncPost(serviceBaseUrl + "/Inventory/", "identifiant="
263
				+ user + "&nom_sel=" + nom_sel + "&num_nom_sel=" + num_nom_sel
264
				+ "&nom_ret=" + nom_ret + "&num_nom_ret=" + num_nom_ret
265
				+ "&num_taxon=" + num_taxon + "&famille=" + famille,
266
 
267
		new ResponseTextHandler() {
268
 
269
			public void onCompletion(String str) {
270
				gotoEnd();
271
				update();
272
			}
273
		});
274
	}
275
 
276
	/**
277
	 * Suppression d'un element lde l'inventaire, a partir de son numero d'ordre
278
	 *
279
	 */
280
 
281
	public void deleteElement() {
282
 
283
		setStatusDisabled();
284
		boolean checked = false;
285
 
286
		// Lifo ...
287
		for (int i = table.getRowCount() - 1; i >= 0; i--) {
288
			if (((CheckBox) table.getWidget(i, 0)).isChecked()) {
289
				checked = true;
290
				String str = table.getText(i, 6);
291
				count--;
292
				HTTPRequest.asyncPost(serviceBaseUrl + "/Inventory/" + user
293
						+ "/" + str, "action=DELETE",
294
						new ResponseTextHandler() {
295
 
296
							public void onCompletion(String str) {
297
								gotoEnd();
298
								update();
299
							}
300
						});
301
 
302
			}
303
		}
304
		if (!checked) {
305
			setStatusEnabled();
306
		}
307
	}
308
 
309
	/**
310
	 *
311
	 * Lancement des initialisations dependantes de réponses asynchrones : le
312
	 * retour d'une demande d'initialisation declanche la demande
313
	 * d'initialisation suivantes user : resultat recherche nom d'utilisateur
314
	 * count : resultat nombre d'enregistrements d'inventaires affichage
315
	 * enregistrements trouvés
316
	 *
317
	 */
318
	private void initAsync() {
319
 
320
		getUser();
321
		// getCount()
322
		// update()
323
 
324
	}
325
 
326
	/**
327
	 * Recherche utilisateur en cours
328
	 *
329
	 */
330
	private void getUser() {
331
 
332
		setStatusDisabled();
333
 
334
		HTTPRequest.asyncGet(serviceBaseUrl + "/User/",
335
				new ResponseTextHandler() {
336
 
337
					public void onCompletion(String str) {
338
						JSONValue jsonValue = JSONParser.parse(str);
339
						JSONString jsonString;
340
						if ((jsonString = jsonValue.isString()) != null) {
341
							user = jsonString.stringValue();
342
						}
343
						getCount();
344
					}
345
				});
346
 
347
	}
348
 
349
	/**
350
	 * Recherche nombre d'enregistrement pour l'utilisateur en cours
351
	 *
352
	 */
353
	private void getCount() {
354
 
355
		HTTPRequest.asyncGet(serviceBaseUrl + "/InventoryList/" + user + "/",
356
				new ResponseTextHandler() {
357
 
358
					public void onCompletion(String str) {
359
 
360
						JSONValue jsonValue = JSONParser.parse(str);
361
						JSONNumber jsonNumber;
362
						if ((jsonNumber = jsonValue.isNumber()) != null) {
363
							count = (int) jsonNumber.getValue();
364
							gotoEnd(); // Derniere page
365
							update();
366
						}
367
					}
368
				});
369
 
370
	}
371
 
372
	/**
373
	 * Mise a jour de l'affichage, à partir des données d'inventaire deja
374
	 * saisies. La valeur de this.startIndex permet de determiner quelles
375
	 * données seront affichées
376
	 *
377
	 */
378
 
379
	private void update() {
380
 
381
		setStatusDisabled();
382
 
383
		// TODO : optimisation : ne supprimer que les lignes qui ne seront pas
384
		// alimentes .
385
 
386
		HTTPRequest.asyncGet(serviceBaseUrl + "/InventoryList/" + user + "/"
387
				+ startIndex + "/" + VISIBLE_TAXON_COUNT,
388
 
389
		new ResponseTextHandler() {
390
 
391
			public void onCompletion(String str) {
392
 
393
				JSONValue jsonValue = JSONParser.parse(str);
394
				JSONArray jsonArray;
395
				JSONArray jsonArrayNested;
396
 
397
				// Lifo ...
398
				for (int i = table.getRowCount() - 1; i >= 0; i--) {
399
					table.removeRow(i);
400
				}
401
 
402
				int j = 0;
403
				if ((jsonArray = jsonValue.isArray()) != null) {
404
					for (int i = 0; i < jsonArray.size(); ++i) {
405
						if ((jsonArrayNested = jsonArray.get(i).isArray()) != null) {
406
							int row = table.insertRow(table.getRowCount());
407
							// Case a cocher
408
							table.setWidget(row, 0, new CheckBox());
409
							// Nom saisi
410
							table.setText(row, 1, ((JSONString) jsonArrayNested
411
									.get(0)).stringValue());
412
							// Nom retenu
413
							table.setText(row, 2, ((JSONString) jsonArrayNested
414
									.get(2)).stringValue());
415
							// Num nomenclatural
416
							table.setText(row, 3, ((JSONString) jsonArrayNested
417
									.get(1)).stringValue());
418
							// Num Taxonomique
419
							table.setText(row, 4, ((JSONString) jsonArrayNested
420
									.get(4)).stringValue());
421
							// Famille
422
							table.setText(row, 5, ((JSONString) jsonArrayNested
423
									.get(5)).stringValue());
424
							// Numero d'ordre
425
							table.setText(row, 6, ((JSONString) jsonArrayNested
426
									.get(6)).stringValue());
427
 
428
							table.getCellFormatter().setVisible(row, 6, false);
429
 
430
							table.getFlexCellFormatter().setWidth(row, 0, "2%");
431
							table.getFlexCellFormatter()
432
									.setWidth(row, 1, "31%");
433
							table.getFlexCellFormatter()
434
									.setWidth(row, 2, "31%");
435
							table.getFlexCellFormatter().setWidth(row, 3, "9%");
436
							table.getFlexCellFormatter().setWidth(row, 4, "9%");
437
							table.getFlexCellFormatter()
438
									.setWidth(row, 5, "18%");
439
							j++;
440
						}
441
 
442
					}
443
				}
444
				setStatusEnabled();
445
 
446
			}
447
		});
448
 
449
	}
450
 
451
	/**
452
	 * Recuperation du prefixe d'appel des services
453
	 */
454
 
5 ddelon 455
	private String getServiceBaseUrl() {
6 ddelon 456
 
457
		Dictionary theme = Dictionary.getDictionary("Parameters");
458
		return theme.get("serviceBaseUrl");
459
 
460
	}
461
 
5 ddelon 462
	/**
463
	 * Affichage message d'attente et désactivation navigation
6 ddelon 464
	 *
465
	 * @param
466
	 * @return void
5 ddelon 467
	 */
6 ddelon 468
 
5 ddelon 469
	private void setStatusDisabled() {
6 ddelon 470
 
5 ddelon 471
		navBar.gotoFirst.setEnabled(false);
472
		navBar.gotoPrev.setEnabled(false);
473
		navBar.gotoNext.setEnabled(false);
474
		navBar.gotoEnd.setEnabled(false);
6 ddelon 475
 
5 ddelon 476
		setStatusText("Patientez ...");
477
	}
4 ddelon 478
 
5 ddelon 479
	/**
480
	 * Affichage numero de page et gestion de la navigation
6 ddelon 481
	 *
5 ddelon 482
	 */
6 ddelon 483
 
5 ddelon 484
	private void setStatusEnabled() {
6 ddelon 485
 
5 ddelon 486
		// Il y a forcemment un disabled avant d'arriver ici
6 ddelon 487
 
488
		if (count > 0) {
489
 
490
			if (startIndex >= VISIBLE_TAXON_COUNT) { // Au dela de la
491
														// premiere page
5 ddelon 492
				navBar.gotoPrev.setEnabled(true);
493
				navBar.gotoFirst.setEnabled(true);
6 ddelon 494
				if (startIndex < (count - VISIBLE_TAXON_COUNT)) { // Pas la
495
																	// derniere
496
																	// page
5 ddelon 497
					navBar.gotoNext.setEnabled(true);
498
					navBar.gotoEnd.setEnabled(true);
6 ddelon 499
					setStatusText((startIndex + 1) + " - "
500
							+ (startIndex + VISIBLE_TAXON_COUNT));
501
				} else { // Derniere page
5 ddelon 502
					setStatusText((startIndex + 1) + " - " + count);
503
				}
6 ddelon 504
			} else { // Premiere page
5 ddelon 505
				if (count > VISIBLE_TAXON_COUNT) { // Des pages derrieres
506
					navBar.gotoNext.setEnabled(true);
507
					navBar.gotoEnd.setEnabled(true);
6 ddelon 508
					setStatusText((startIndex + 1) + " - "
509
							+ (startIndex + VISIBLE_TAXON_COUNT));
510
				} else {
5 ddelon 511
					setStatusText((startIndex + 1) + " - " + count);
512
				}
513
			}
514
		}
6 ddelon 515
 
5 ddelon 516
		else { // Pas d'inventaire, pas de navigation
6 ddelon 517
			setStatusText(0 + " - " + 0);
5 ddelon 518
		}
519
	}
6 ddelon 520
 
5 ddelon 521
	/**
6 ddelon 522
	 * Positionnement index de parcours (this.startIndex) pour affichage de la
523
	 * dernière page
524
	 *
525
	 * @param
526
	 * @return void
5 ddelon 527
	 */
6 ddelon 528
 
5 ddelon 529
	private void gotoEnd() {
6 ddelon 530
 
531
		if ((count == 0) || (count % VISIBLE_TAXON_COUNT) > 0) {
532
			startIndex = count - (count % VISIBLE_TAXON_COUNT);
533
		} else {
534
			startIndex = count - VISIBLE_TAXON_COUNT;
535
		}
536
 
5 ddelon 537
	}
6 ddelon 538
 
2 ddelon 539
}