Subversion Repositories eFlore/Archives.cel-v1

Rev

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