Subversion Repositories eFlore/Archives.cel-v1

Rev

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