Subversion Repositories eFlore/Archives.cel-v1

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

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