Subversion Repositories eFlore/Archives.cel-v1

Rev

Rev 3 | 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
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;
41
 
4 ddelon 42
import java.util.Vector;
2 ddelon 43
 
4 ddelon 44
 
45
/* Le retour de getUser appelle getCount qui appelle update pour veiller à une initialisation correcte
46
 
2 ddelon 47
/**
48
 * A composite that displays a list of emails that can be selected.
49
 */
50
public class TaxonList extends Composite implements AutoCompleteAsyncTextBoxListener  {
51
 
52
 
3 ddelon 53
	  private class NavBar extends Composite implements ClickListener {
54
 
55
	    public final DockPanel bar = new DockPanel();
56
	    public final Button gotoFirst = new Button("<<", this);
57
	    public final Button gotoNext = new Button(">", this);
58
	    public final Button gotoPrev = new Button("<", this);
59
	    public final Button gotoEnd = new Button(">>", this);
60
	    public final HTML status = new HTML();
61
 
62
 
63
	    public NavBar() {
64
	      initWidget(bar);
65
	      bar.setStyleName("navbar");
66
	      status.setStyleName("status");
67
 
68
	      HorizontalPanel buttons = new HorizontalPanel();
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.add(status, DockPanel.CENTER);
76
	      bar.setVerticalAlignment(DockPanel.ALIGN_MIDDLE);
4 ddelon 77
	      bar.setCellHorizontalAlignment(status, HasHorizontalAlignment.ALIGN_RIGHT);
78
	      bar.setCellVerticalAlignment(status, HasVerticalAlignment.ALIGN_MIDDLE);
3 ddelon 79
	      bar.setCellWidth(status, "100%");
80
 
81
	      // Initialize prev & first button to disabled.
82
	      //
83
	    }
84
 
85
 
86
	    public void onClick(Widget sender) {
87
	  	    if (sender == gotoNext) {
88
	  	      // Move forward a page.
89
	  	      startIndex += VISIBLE_TAXON_COUNT;
90
	  	      if (startIndex >= count)
91
	  	          startIndex -= VISIBLE_TAXON_COUNT;
92
	  	    }
93
	  	    else {
94
	  	     if (sender == gotoPrev) {
95
	  	      // Move back a page.
96
	  	      startIndex -= VISIBLE_TAXON_COUNT;
97
	  	      if (startIndex < 0)
98
	  	        startIndex = 0;
99
	  	      }
100
	  	     else {
101
	  	    	 if (sender==gotoEnd) {
102
	  	    	  	if ((count%VISIBLE_TAXON_COUNT)>0) {
103
    		    		startIndex=count-(count%VISIBLE_TAXON_COUNT);
104
    		    	}
105
    		    	else {
106
    		    		startIndex=count-VISIBLE_TAXON_COUNT;
107
    		    	}
108
	  	    	 }
109
	  	    	 else {
110
	  	    		 if (sender== gotoFirst) {
111
	  	    			 startIndex = 0;
112
	  	    		 }
113
	  	    	 }
114
	  	     }
115
	  	   }
116
	  	   update();
117
	    }
118
 
119
	  }
120
 
121
  public void setStatusText(String text) {
122
	    navBar.status.setText(text);
123
  }
124
 
125
  public void clearStatusText() {
126
	    navBar.status.setHTML("&nbsp;");
127
  }
128
 
129
 
130
 
131
  private static final int VISIBLE_TAXON_COUNT = 15;
132
 
4 ddelon 133
  private Grid header = new Grid(1,6);
2 ddelon 134
  private FlexTable table = new FlexTable();
135
  private VerticalPanel panel = new VerticalPanel();
4 ddelon 136
 
3 ddelon 137
  private int startIndex=0;
4 ddelon 138
  private String serviceBaseUrl=getServiceBaseUrl();
3 ddelon 139
  private int count=65000;
140
 
4 ddelon 141
  private Vector complements=null;
142
  private String element=null;
143
  private String complement=null;
144
 
145
  private String user;
146
 
3 ddelon 147
  private NavBar navBar = new NavBar();
148
 
4 ddelon 149
  public TaxonList(Vector comps) {
3 ddelon 150
 
4 ddelon 151
 
152
	getUser();
153
 
154
 
155
	// Information complementaire
156
 
157
	complements=comps;
158
 
2 ddelon 159
    // Setup the header
160
 
161
    header.setCellSpacing(0);
162
    header.setCellPadding(2);
163
    header.setWidth("100%");
164
 
165
    header.setStyleName("taxon-ListHeader");
166
 
4 ddelon 167
    header.setText(0, 0, "");
168
    header.setText(0, 1, "Nom saisi");
169
    header.setText(0, 2, "Nom retenu");
170
    header.setHTML(0, 3, "Code<br>Nomenclatural");
171
    header.setHTML(0, 4, "Code<br>Taxonomique");
172
    header.setText(0, 5, "");
173
 
174
	header.getCellFormatter().setWidth(0,0,"2%");
175
	header.getCellFormatter().setWidth(0,1,"31%");
176
	header.getCellFormatter().setWidth(0,2,"31%");
177
	header.getCellFormatter().setWidth(0,3,"9%");
178
	header.getCellFormatter().setWidth(0,4,"9%");
179
	header.getCellFormatter().setWidth(0,5,"18%");
180
 
181
 
182
 
183
    header.setCellSpacing(0);
184
    header.setCellPadding(2);
185
 
2 ddelon 186
    // Setup the table.
187
 
188
    table.setCellSpacing(0);
4 ddelon 189
    table.setBorderWidth(0);
2 ddelon 190
    table.setCellPadding(2);
191
    table.setWidth("100%");
192
 
3 ddelon 193
 
194
    navBar.setWidth("100%");
195
 
196
 
2 ddelon 197
 
198
    table.setStyleName("taxon-List");
199
 
3 ddelon 200
    panel.add(navBar);
2 ddelon 201
    panel.add(header);
202
    panel.add(table);
203
 
204
    initWidget(panel);
205
 
4 ddelon 206
 
2 ddelon 207
  }
208
 
4 ddelon 209
  public void onValidate(SourcesAutoCompleteAsyncTextBoxEvents sender,int pos, String str) {
2 ddelon 210
 
4 ddelon 211
 
212
// Recherche informations complementaires :
213
// et Ajout ligne
3 ddelon 214
 
4 ddelon 215
	    if (pos>=0) {
216
	    complement=(String) complements.get(pos);
217
	    element=str;
218
 
219
		 HTTPRequest.asyncGet(serviceBaseUrl+"/NameValid/"+complement,
220
				    new ResponseTextHandler(){
3 ddelon 221
 
4 ddelon 222
				    	public void onCompletion(String str) {
223
 
224
							JSONValue jsonValue= JSONParser.parse(str);
225
				  			JSONArray jsonArray;
226
 
227
				  			complements.clear();
228
 
229
				  			if ((jsonArray = jsonValue.isArray()) != null) {
230
				  					  // Nom retenu, Num Nomen nom retenu, Num Taxon, Famille
231
								      addElement(element,complement,((JSONString) jsonArray.get(0)).stringValue(),
232
								    		  ((JSONString) jsonArray.get(1)).stringValue(),((JSONString) jsonArray.get(2)).stringValue(),
233
								    		  ((JSONString) jsonArray.get(3)).stringValue());
234
				  			}
235
				    	}
2 ddelon 236
 
4 ddelon 237
				   	});
238
	    }
239
	    else {
240
 			complements.clear();
241
 
242
			addElement(str," "," "," "," "," ");
243
	    }
244
  }
245
 
246
 
247
  public void addElement(String nom_sel, String num_nom_sel, String nom_ret, String num_nom_ret, String num_taxon, String famille) {
248
 
249
	  int lastOrdre=1;
250
	  if (table.getRowCount()>0) {
251
		  lastOrdre=new Integer(table.getText(table.getRowCount()-1,6)).intValue()+1;
252
	  }
253
 
254
	  count++;
255
	  HTTPRequest.asyncPost(serviceBaseUrl+"/Inventory/", "identifiant="+user+"&nom_sel="+nom_sel+"&num_nom_sel="+num_nom_sel
256
			  +"&nom_ret="+nom_ret+"&num_nom_ret="+num_nom_ret+"&num_taxon="+num_taxon+"&famille="+famille+"&ordre="+lastOrdre,
257
	  new ResponseTextHandler(){
258
 
259
	  	public void onCompletion(String str) {
3 ddelon 260
		    	if ((count%VISIBLE_TAXON_COUNT)>0) {
261
		    		startIndex=count-(count%VISIBLE_TAXON_COUNT);
262
		    	}
263
		    	else {
264
		    		startIndex=count-VISIBLE_TAXON_COUNT;
265
		    	}
4 ddelon 266
		    	if (startIndex < 0) {
267
			  	    startIndex = 0;
268
			  	}
3 ddelon 269
		    	update();
4 ddelon 270
	  	}
271
	  });
272
  }
2 ddelon 273
 
274
 
275
  public void deleteElement() {
276
 
277
	    // Lifo ...
278
      for (int i=table.getRowCount()-1; i>=0;i--){
279
		 if (((CheckBox) table.getWidget(i,0)).isChecked())  {
4 ddelon 280
			String str=table.getText(i,6);
3 ddelon 281
			count--;
4 ddelon 282
			 HTTPRequest.asyncPost(serviceBaseUrl+"/Inventory/"+user+"/"+str, "action=DELETE",
2 ddelon 283
			   new ResponseTextHandler(){
284
 
285
			   	public void onCompletion(String str) {
4 ddelon 286
			    	if ((count%VISIBLE_TAXON_COUNT)==0) {
3 ddelon 287
			    		startIndex=count-VISIBLE_TAXON_COUNT;
288
			    	}
4 ddelon 289
			    	if (startIndex < 0) {
290
				  	    startIndex = 0;
291
				  	}
3 ddelon 292
			    	update();
2 ddelon 293
			   	}
294
			  });
295
 
296
	  	 }
297
	  }
298
  }
4 ddelon 299
 
300
  private void getUser() {
301
		 HTTPRequest.asyncGet(serviceBaseUrl+"/User/",
302
				    new ResponseTextHandler(){
303
 
304
				    	public void onCompletion(String str) {
305
				    		JSONValue jsonValue= JSONParser.parse(str);
306
				    		JSONString jsonString;
307
				    		    if ((jsonString = jsonValue.isString()) != null) {
308
				    		    	user = jsonString.stringValue();
309
				    		    }
310
				    		    getCount();
311
				    	}
312
			 	});
313
 
314
 
315
 
316
		}
317
 
2 ddelon 318
 
4 ddelon 319
  private void getCount() {
320
  // Recherche derniere ligne ... (il y a une autre stratégie pour la calculer si le resultat n'arrive pas assez vite)
321
 
2 ddelon 322
 
4 ddelon 323
	 HTTPRequest.asyncGet(serviceBaseUrl+"/InventoryList/"+user+"/",
324
		    new ResponseTextHandler(){
325
 
326
		    	public void onCompletion(String str) {
327
 
328
//On se positionne sur la dernière page ...
329
 
330
		    		JSONValue jsonValue= JSONParser.parse(str);
331
		    		JSONNumber jsonNumber;
332
		    		    if ((jsonNumber = jsonValue.isNumber()) != null) {
333
		    		    	count=(int) jsonNumber.getValue();
334
		    		    	if ((count%VISIBLE_TAXON_COUNT)>0) {
335
		    		    		startIndex=count-(count%VISIBLE_TAXON_COUNT);
336
		    		    	}
337
		    		    	else {
338
		    		    		startIndex=count-VISIBLE_TAXON_COUNT;
339
		    		    	}
340
		    		    	if (startIndex < 0) {
341
		    			  	    startIndex = 0;
342
		    			  	}
343
 
344
		    		    	update();
345
		    		    }
346
		    	}
347
	 	});
348
 
349
  }
350
 
351
 
2 ddelon 352
  private void update() {
4 ddelon 353
 
354
 
355
	 // TODO : optimisation : ne supprimer que les lignes qui ne seront pas alimentes .
3 ddelon 356
 
357
	 navBar.gotoFirst.setEnabled(false);
358
	 navBar.gotoPrev.setEnabled(false);
359
     navBar.gotoNext.setEnabled(false);
360
     navBar.gotoEnd.setEnabled(false);
361
 
362
     setStatusText("Patientez ...");
363
 
4 ddelon 364
	 HTTPRequest.asyncGet(serviceBaseUrl+"/InventoryList/"+user+"/"+startIndex+"/"+VISIBLE_TAXON_COUNT,
2 ddelon 365
		    new ResponseTextHandler(){
366
 
367
		    	public void onCompletion(String str) {
368
 
369
		    		    JSONValue jsonValue= JSONParser.parse(str);
370
		    		    JSONArray jsonArray;
371
		    		    JSONArray jsonArrayNested;
372
 
373
		    		    // Lifo ...
374
		    	        for (int i=table.getRowCount()-1; i>=0;i--){
375
		                        table.removeRow(i);
376
		                }
377
 
378
 
4 ddelon 379
		    		    int j=0;
2 ddelon 380
		    		    if ((jsonArray = jsonValue.isArray()) != null) {
3 ddelon 381
		    		      int i;
382
		    		      for (i = 0; i < jsonArray.size(); ++i) {
2 ddelon 383
		    		    	  if ((jsonArrayNested = jsonArray.get(i).isArray()) != null) {
384
		    		    		  int row=table.insertRow(table.getRowCount());
4 ddelon 385
		    		    		  // Case a cocher
2 ddelon 386
		    		    		  table.setWidget(row,0,new CheckBox());
4 ddelon 387
		    		    		  // Nom saisi
2 ddelon 388
		    		    		  table.setText(row,1,((JSONString) jsonArrayNested.get(0)).stringValue());
4 ddelon 389
		    		    		  // Nom retenu
390
		    		    		  table.setText(row,2,((JSONString) jsonArrayNested.get(2)).stringValue());
391
		    		    		  // Num nomenclatural
392
		    		    		  table.setText(row,3,((JSONString) jsonArrayNested.get(1)).stringValue());
393
		    		    		  // Num Taxonomique
394
		    		    		  table.setText(row,4,((JSONString) jsonArrayNested.get(4)).stringValue());
395
		    		    		  // Famille
396
		    		    		  table.setText(row,5,((JSONString) jsonArrayNested.get(5)).stringValue());
397
		    		    		  // Numero d'ordre
398
		    		    		  table.setText(row,6,((JSONString) jsonArrayNested.get(6)).stringValue());
399
 
400
		    		    		  table.getCellFormatter().setVisible(row,6,true);
401
 
402
		    		    		  table.getFlexCellFormatter().setWidth(row,0,"2%");
403
		    		    		  table.getFlexCellFormatter().setWidth(row,1,"31%");
404
		    		    		  table.getFlexCellFormatter().setWidth(row,2,"31%");
405
		    		    		  table.getFlexCellFormatter().setWidth(row,3,"9%");
406
		    		    		  table.getFlexCellFormatter().setWidth(row,4,"9%");
407
		    		    		  table.getFlexCellFormatter().setWidth(row,5,"18%");
408
		    		    		  j++;
2 ddelon 409
		    		    	  }
4 ddelon 410
 
2 ddelon 411
		    		      }
4 ddelon 412
		    		      // Calcul nombre d'enregistrement total (c'est un doublon, c'est normal ...)
3 ddelon 413
		    		      if (i<VISIBLE_TAXON_COUNT) {
414
		    		    	  count = startIndex+i;
415
		    		      }
416
 
417
		    		      // Navigation
418
 
419
 
420
		    		      // Premiere page
4 ddelon 421
		    		      if (startIndex<VISIBLE_TAXON_COUNT ) {
3 ddelon 422
		    		    		navBar.gotoPrev.setEnabled(false);
423
		    		    		navBar.gotoFirst.setEnabled(false);
424
		    		    		navBar.gotoNext.setEnabled(true);
425
		    		    		navBar.gotoEnd.setEnabled(true);
4 ddelon 426
		    		    		if (count < VISIBLE_TAXON_COUNT) {
427
		    		    			setStatusText((startIndex + 1) + " - " + count );
428
		    		    		}
429
		    		    		else {
430
				    		      setStatusText((startIndex + 1) + " - " + (startIndex + VISIBLE_TAXON_COUNT));
431
		    		    		}
3 ddelon 432
 
433
		    		      }
434
		    		      // Derniere page
435
		    		      else {
436
		    		    	  if (startIndex>=(count-VISIBLE_TAXON_COUNT)) {
437
		    		    		navBar.gotoPrev.setEnabled(true);
438
		    		    		navBar.gotoFirst.setEnabled(true);
439
		    		    		navBar.gotoNext.setEnabled(false);
440
		    		    		navBar.gotoEnd.setEnabled(false);
441
		    		    		setStatusText((startIndex + 1) + " - " + count);
442
		    		    	  }
4 ddelon 443
		    		    	  // Page normale
3 ddelon 444
		    		    	  else {
445
				    		      navBar.gotoPrev.setEnabled(true);
446
				    		      navBar.gotoFirst.setEnabled(true);
447
				    		      navBar.gotoNext.setEnabled(true);
448
				    		      navBar.gotoEnd.setEnabled(true);
4 ddelon 449
				    		      setStatusText((startIndex + 1) + " - " + (startIndex + VISIBLE_TAXON_COUNT));
3 ddelon 450
		    		    	  }
451
		    		      }
2 ddelon 452
		    		    }
4 ddelon 453
		    		    // Tableau vide (TODO : despaguettiser)
454
		    		    if (j==0) {
455
		    		    	   navBar.gotoPrev.setEnabled(false);
456
				    		   navBar.gotoFirst.setEnabled(false);
457
				    		   navBar.gotoNext.setEnabled(false);
458
				    		   navBar.gotoEnd.setEnabled(false);
459
				    		   setStatusText(0 + " - " + 0);
460
		    		    }
2 ddelon 461
		    	}
462
		    });
463
 
464
  }
3 ddelon 465
 
4 ddelon 466
private String getServiceBaseUrl() {
467
 
468
	  Dictionary theme = Dictionary.getDictionary("Parameters");
469
	  return theme.get("serviceBaseUrl");
470
 
2 ddelon 471
 
4 ddelon 472
	}
473
 
474
 
475
 
2 ddelon 476
}