| 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 |
|
|
|
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;
|
| 2 |
ddelon |
36 |
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;
|
| 3 |
ddelon |
37 |
import com.google.gwt.user.client.ui.ClickListener;
|
|
|
38 |
import com.google.gwt.user.client.ui.HasAlignment;
|
| 2 |
ddelon |
39 |
|
|
|
40 |
|
|
|
41 |
/**
|
|
|
42 |
* A composite that displays a list of emails that can be selected.
|
|
|
43 |
*/
|
|
|
44 |
public class TaxonList extends Composite implements AutoCompleteAsyncTextBoxListener {
|
|
|
45 |
|
|
|
46 |
|
| 3 |
ddelon |
47 |
private class NavBar extends Composite implements ClickListener {
|
|
|
48 |
|
|
|
49 |
public final DockPanel bar = new DockPanel();
|
|
|
50 |
public final Button gotoFirst = new Button("<<", this);
|
|
|
51 |
public final Button gotoNext = new Button(">", this);
|
|
|
52 |
public final Button gotoPrev = new Button("<", this);
|
|
|
53 |
public final Button gotoEnd = new Button(">>", this);
|
|
|
54 |
public final HTML status = new HTML();
|
|
|
55 |
|
|
|
56 |
|
|
|
57 |
public NavBar() {
|
|
|
58 |
initWidget(bar);
|
|
|
59 |
bar.setStyleName("navbar");
|
|
|
60 |
status.setStyleName("status");
|
|
|
61 |
|
|
|
62 |
HorizontalPanel buttons = new HorizontalPanel();
|
|
|
63 |
buttons.add(gotoFirst);
|
|
|
64 |
buttons.add(gotoPrev);
|
|
|
65 |
buttons.add(gotoNext);
|
|
|
66 |
buttons.add(gotoEnd);
|
|
|
67 |
bar.add(buttons, DockPanel.EAST);
|
|
|
68 |
bar.setCellHorizontalAlignment(buttons, DockPanel.ALIGN_RIGHT);
|
|
|
69 |
bar.add(status, DockPanel.CENTER);
|
|
|
70 |
bar.setVerticalAlignment(DockPanel.ALIGN_MIDDLE);
|
|
|
71 |
bar.setCellHorizontalAlignment(status, HasAlignment.ALIGN_RIGHT);
|
|
|
72 |
bar.setCellVerticalAlignment(status, HasAlignment.ALIGN_MIDDLE);
|
|
|
73 |
bar.setCellWidth(status, "100%");
|
|
|
74 |
|
|
|
75 |
// Initialize prev & first button to disabled.
|
|
|
76 |
//
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
|
|
|
80 |
public void onClick(Widget sender) {
|
|
|
81 |
if (sender == gotoNext) {
|
|
|
82 |
// Move forward a page.
|
|
|
83 |
startIndex += VISIBLE_TAXON_COUNT;
|
|
|
84 |
if (startIndex >= count)
|
|
|
85 |
startIndex -= VISIBLE_TAXON_COUNT;
|
|
|
86 |
}
|
|
|
87 |
else {
|
|
|
88 |
if (sender == gotoPrev) {
|
|
|
89 |
// Move back a page.
|
|
|
90 |
startIndex -= VISIBLE_TAXON_COUNT;
|
|
|
91 |
if (startIndex < 0)
|
|
|
92 |
startIndex = 0;
|
|
|
93 |
}
|
|
|
94 |
else {
|
|
|
95 |
if (sender==gotoEnd) {
|
|
|
96 |
if ((count%VISIBLE_TAXON_COUNT)>0) {
|
|
|
97 |
startIndex=count-(count%VISIBLE_TAXON_COUNT);
|
|
|
98 |
}
|
|
|
99 |
else {
|
|
|
100 |
startIndex=count-VISIBLE_TAXON_COUNT;
|
|
|
101 |
}
|
|
|
102 |
}
|
|
|
103 |
else {
|
|
|
104 |
if (sender== gotoFirst) {
|
|
|
105 |
startIndex = 0;
|
|
|
106 |
}
|
|
|
107 |
}
|
|
|
108 |
}
|
|
|
109 |
}
|
|
|
110 |
update();
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
public void setStatusText(String text) {
|
|
|
116 |
navBar.status.setText(text);
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
public void clearStatusText() {
|
|
|
120 |
navBar.status.setHTML(" ");
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
|
|
|
124 |
|
|
|
125 |
private static final int VISIBLE_TAXON_COUNT = 15;
|
|
|
126 |
|
| 2 |
ddelon |
127 |
private Grid header = new Grid(1,3);
|
|
|
128 |
private FlexTable table = new FlexTable();
|
|
|
129 |
private CellFormatter cellFormater = table.getCellFormatter();
|
|
|
130 |
private VerticalPanel panel = new VerticalPanel();
|
|
|
131 |
|
| 3 |
ddelon |
132 |
private int startIndex=0;
|
|
|
133 |
private int count=65000;
|
|
|
134 |
|
|
|
135 |
private NavBar navBar = new NavBar();
|
|
|
136 |
|
| 2 |
ddelon |
137 |
public TaxonList() {
|
| 3 |
ddelon |
138 |
|
|
|
139 |
|
|
|
140 |
|
| 2 |
ddelon |
141 |
// Setup the header
|
|
|
142 |
|
|
|
143 |
header.setCellSpacing(0);
|
|
|
144 |
header.setCellPadding(2);
|
|
|
145 |
header.setWidth("100%");
|
|
|
146 |
|
|
|
147 |
header.setStyleName("taxon-ListHeader");
|
|
|
148 |
|
|
|
149 |
header.setText(0, 0, "Action");
|
|
|
150 |
header.setText(0, 1, "Nom");
|
|
|
151 |
header.setText(0, 2, "Famille");
|
| 3 |
ddelon |
152 |
|
|
|
153 |
|
| 2 |
ddelon |
154 |
|
|
|
155 |
// Setup the table.
|
|
|
156 |
|
|
|
157 |
table.setCellSpacing(0);
|
|
|
158 |
table.setCellPadding(2);
|
|
|
159 |
table.setWidth("100%");
|
|
|
160 |
|
| 3 |
ddelon |
161 |
|
|
|
162 |
navBar.setWidth("100%");
|
|
|
163 |
|
|
|
164 |
|
| 2 |
ddelon |
165 |
|
|
|
166 |
table.setStyleName("taxon-List");
|
|
|
167 |
|
| 3 |
ddelon |
168 |
panel.add(navBar);
|
| 2 |
ddelon |
169 |
panel.add(header);
|
|
|
170 |
panel.add(table);
|
|
|
171 |
|
|
|
172 |
initWidget(panel);
|
|
|
173 |
|
| 3 |
ddelon |
174 |
// Recherche derniere ligne ... (il y a une autre stratégie pour la calculer si le resultat n'arrive pas assez vite)
|
| 2 |
ddelon |
175 |
|
| 3 |
ddelon |
176 |
|
|
|
177 |
HTTPRequest.asyncGet("http://localhost/david/jrest/InventoryList/dd/",
|
|
|
178 |
new ResponseTextHandler(){
|
|
|
179 |
|
|
|
180 |
public void onCompletion(String str) {
|
|
|
181 |
|
|
|
182 |
// On se positionne sur la dernière page ...
|
|
|
183 |
|
|
|
184 |
JSONValue jsonValue= JSONParser.parse(str);
|
|
|
185 |
JSONNumber jsonNumber;
|
|
|
186 |
if ((jsonNumber = jsonValue.isNumber()) != null) {
|
|
|
187 |
count=(int) jsonNumber.getValue();
|
|
|
188 |
if ((count%VISIBLE_TAXON_COUNT)>0) {
|
|
|
189 |
startIndex=count-(count%VISIBLE_TAXON_COUNT);
|
|
|
190 |
}
|
|
|
191 |
else {
|
|
|
192 |
startIndex=count-VISIBLE_TAXON_COUNT;
|
|
|
193 |
}
|
|
|
194 |
|
|
|
195 |
update();
|
|
|
196 |
}
|
|
|
197 |
}
|
|
|
198 |
});
|
|
|
199 |
|
|
|
200 |
|
| 2 |
ddelon |
201 |
}
|
|
|
202 |
|
|
|
203 |
public void onValidate(SourcesAutoCompleteAsyncTextBoxEvents sender, String str) {
|
|
|
204 |
|
| 3 |
ddelon |
205 |
// Ajout ligne
|
|
|
206 |
|
|
|
207 |
|
|
|
208 |
count++;
|
| 2 |
ddelon |
209 |
|
|
|
210 |
HTTPRequest.asyncPost("http://localhost/david/jrest/Inventory/", "identifiant=dd&nom="+str,
|
|
|
211 |
new ResponseTextHandler(){
|
|
|
212 |
|
|
|
213 |
public void onCompletion(String str) {
|
| 3 |
ddelon |
214 |
if ((count%VISIBLE_TAXON_COUNT)>0) {
|
|
|
215 |
startIndex=count-(count%VISIBLE_TAXON_COUNT);
|
|
|
216 |
}
|
|
|
217 |
else {
|
|
|
218 |
startIndex=count-VISIBLE_TAXON_COUNT;
|
|
|
219 |
}
|
|
|
220 |
update();
|
| 2 |
ddelon |
221 |
}
|
|
|
222 |
});
|
|
|
223 |
|
|
|
224 |
|
|
|
225 |
}
|
|
|
226 |
|
|
|
227 |
|
|
|
228 |
public void deleteElement() {
|
|
|
229 |
|
|
|
230 |
// Lifo ...
|
|
|
231 |
for (int i=table.getRowCount()-1; i>=0;i--){
|
|
|
232 |
if (((CheckBox) table.getWidget(i,0)).isChecked()) {
|
| 3 |
ddelon |
233 |
String str=table.getText(i,2);
|
|
|
234 |
count--;
|
| 2 |
ddelon |
235 |
HTTPRequest.asyncPost("http://localhost/david/jrest/Inventory/dd/"+str, "action=DELETE",
|
|
|
236 |
new ResponseTextHandler(){
|
|
|
237 |
|
|
|
238 |
public void onCompletion(String str) {
|
| 3 |
ddelon |
239 |
if ((count%VISIBLE_TAXON_COUNT)>0) {
|
|
|
240 |
startIndex=count-(count%VISIBLE_TAXON_COUNT);
|
|
|
241 |
}
|
|
|
242 |
else {
|
|
|
243 |
startIndex=count-VISIBLE_TAXON_COUNT;
|
|
|
244 |
}
|
|
|
245 |
update();
|
| 2 |
ddelon |
246 |
}
|
|
|
247 |
});
|
|
|
248 |
|
|
|
249 |
}
|
|
|
250 |
}
|
|
|
251 |
}
|
|
|
252 |
|
|
|
253 |
|
|
|
254 |
private void update() {
|
| 3 |
ddelon |
255 |
|
|
|
256 |
navBar.gotoFirst.setEnabled(false);
|
|
|
257 |
navBar.gotoPrev.setEnabled(false);
|
|
|
258 |
navBar.gotoNext.setEnabled(false);
|
|
|
259 |
navBar.gotoEnd.setEnabled(false);
|
|
|
260 |
|
|
|
261 |
setStatusText("Patientez ...");
|
|
|
262 |
|
|
|
263 |
HTTPRequest.asyncGet("http://localhost/david/jrest/InventoryList/dd/"+startIndex+"/"+VISIBLE_TAXON_COUNT,
|
| 2 |
ddelon |
264 |
new ResponseTextHandler(){
|
|
|
265 |
|
|
|
266 |
public void onCompletion(String str) {
|
|
|
267 |
|
|
|
268 |
JSONValue jsonValue= JSONParser.parse(str);
|
|
|
269 |
JSONArray jsonArray;
|
|
|
270 |
JSONArray jsonArrayNested;
|
|
|
271 |
|
|
|
272 |
// Lifo ...
|
|
|
273 |
for (int i=table.getRowCount()-1; i>=0;i--){
|
|
|
274 |
table.removeRow(i);
|
|
|
275 |
}
|
|
|
276 |
|
|
|
277 |
|
|
|
278 |
if ((jsonArray = jsonValue.isArray()) != null) {
|
| 3 |
ddelon |
279 |
int i;
|
|
|
280 |
for (i = 0; i < jsonArray.size(); ++i) {
|
| 2 |
ddelon |
281 |
if ((jsonArrayNested = jsonArray.get(i).isArray()) != null) {
|
|
|
282 |
int row=table.insertRow(table.getRowCount());
|
|
|
283 |
table.setWidget(row,0,new CheckBox());
|
|
|
284 |
table.setText(row,1,((JSONString) jsonArrayNested.get(0)).stringValue());
|
|
|
285 |
table.setText(row,2,((JSONString) jsonArrayNested.get(1)).stringValue());
|
|
|
286 |
cellFormater.setVisible(row,2,false);
|
|
|
287 |
}
|
|
|
288 |
}
|
| 3 |
ddelon |
289 |
// Calcul nombre d'enregistrement total
|
|
|
290 |
if (i<VISIBLE_TAXON_COUNT) {
|
|
|
291 |
count = startIndex+i;
|
|
|
292 |
}
|
|
|
293 |
|
|
|
294 |
// Navigation
|
|
|
295 |
|
|
|
296 |
setStatusText((startIndex + 1) + " - " + (startIndex + VISIBLE_TAXON_COUNT));
|
|
|
297 |
|
|
|
298 |
// Premiere page
|
|
|
299 |
if (startIndex<VISIBLE_TAXON_COUNT) {
|
|
|
300 |
navBar.gotoPrev.setEnabled(false);
|
|
|
301 |
navBar.gotoFirst.setEnabled(false);
|
|
|
302 |
navBar.gotoNext.setEnabled(true);
|
|
|
303 |
navBar.gotoEnd.setEnabled(true);
|
|
|
304 |
|
|
|
305 |
}
|
|
|
306 |
// Derniere page
|
|
|
307 |
else {
|
|
|
308 |
if (startIndex>=(count-VISIBLE_TAXON_COUNT)) {
|
|
|
309 |
navBar.gotoPrev.setEnabled(true);
|
|
|
310 |
navBar.gotoFirst.setEnabled(true);
|
|
|
311 |
navBar.gotoNext.setEnabled(false);
|
|
|
312 |
navBar.gotoEnd.setEnabled(false);
|
|
|
313 |
setStatusText((startIndex + 1) + " - " + count);
|
|
|
314 |
}
|
|
|
315 |
else {
|
|
|
316 |
navBar.gotoPrev.setEnabled(true);
|
|
|
317 |
navBar.gotoFirst.setEnabled(true);
|
|
|
318 |
navBar.gotoNext.setEnabled(true);
|
|
|
319 |
navBar.gotoEnd.setEnabled(true);
|
|
|
320 |
}
|
|
|
321 |
}
|
| 2 |
ddelon |
322 |
}
|
|
|
323 |
}
|
|
|
324 |
});
|
|
|
325 |
|
|
|
326 |
}
|
| 3 |
ddelon |
327 |
|
|
|
328 |
|
|
|
329 |
|
|
|
330 |
|
| 2 |
ddelon |
331 |
|
|
|
332 |
}
|