Subversion Repositories eFlore/Applications.cel

Rev

Rev 54 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
12 david 1
/*
2
Auto-Completion Textbox for GWT
3
Copyright (C) 2006 Oliver Albers http://gwt.components.googlepages.com/
4
 
5
This library is free software; you can redistribute it and/or
6
modify it under the terms of the GNU Lesser General Public
7
License as published by the Free Software Foundation; either
8
version 2.1 of the License, or (at your option) any later version.
9
 
10
This library is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
Lesser General Public License for more details.
14
 
15
You should have received a copy of the GNU Lesser General Public
16
License along with this library; if not, write to the Free Software
17
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
 
19
*/
20
 
54 david 21
 
22
// TODO : C
12 david 23
package org.tela_botanica.client.vues;
24
 
25
 
54 david 26
// TODO : Classe A supprimer ( reprendre le mecanisme du cache avant)
27
 
12 david 28
// TODO : traiter latence (augmenter en fonction rapidit� saisie + texte vide)
29
// TODO : traitement espace apres l'espece (%20)
30
// TODO : Utiliser Suggestbox et les Associating Data Transfer Objects (DTOs) with Suggestion Objects
31
 
32
import com.google.gwt.user.client.ui.KeyboardListener;
33
import com.google.gwt.user.client.ui.ListBox;
34
import com.google.gwt.user.client.ui.PopupPanel;
35
import com.google.gwt.user.client.ui.TextBox;
36
import com.google.gwt.user.client.ui.Widget;
37
import com.google.gwt.user.client.DOM;
38
import com.google.gwt.user.client.Event;
39
 
40
import java.util.Vector;
41
import java.util.HashMap;
42
 
43
import org.tela_botanica.client.interfaces.FournisseurListe;
44
import org.tela_botanica.client.interfaces.Rafraichissable;
45
 
46
 
47
 
48
public class AutoCompleteAsyncTextBox extends TextBox implements KeyboardListener {
49
 
50
 
51
// Fournisseur de donnees
52
  private FournisseurListe fournisseurDeDonnees=null;
53
 
54
 
55
 
56
  private HashMap cache = new HashMap();
57
  private boolean searching = false;
58
  private Rafraichissable rafraichissable=null;
59
 
60
  protected PopupPanel choicesPopup = new PopupPanel(true);
61
  protected ListBox choices = new ListBox() {
62
	  public void onBrowserEvent(Event event) {
63
		  if (Event.ONCLICK == DOM.eventGetType(event)) {
64
			  complete();
65
		  }
66
	  }
67
  };
68
  protected Vector items = new Vector();
69
  protected boolean visible = false;
70
 
71
  /**
72
   * Value linked to current text
73
   */
74
  protected String currentValue = null;
75
 
76
 
77
 
78
  /**
79
   * Default Constructor
80
   *
81
   */
82
  public AutoCompleteAsyncTextBox(Rafraichissable r)
83
  {
84
    super();
85
    rafraichissable=r;
86
    this.addKeyboardListener(this);
87
    choices.sinkEvents(Event.ONCLICK);
88
    this.setStyleName("AutoCompleteAsyncTextBox");
89
 
90
    choicesPopup.add(choices);
91
    choicesPopup.addStyleName("AutoCompleteChoices");
92
 
93
    choices.setStyleName("list");
94
 
95
  }
96
 
97
 
98
 
99
  public void setFournisseurDeDonnees(FournisseurListe fournisseurDeDonnees) {
100
 
101
	  this.fournisseurDeDonnees=fournisseurDeDonnees;
102
  }
103
 
104
  private void doFetchData(String match) {
105
 
106
 
107
	  String rematch=match.replaceAll(" ","/");
108
	  rematch=rematch.replaceAll("%","");
109
 
110
	  if (this.fournisseurDeDonnees!=null && searching==false) {
111
		  searching=true;
112
 
113
		  fournisseurDeDonnees.obtenirListeDonnees(rafraichissable, rematch);
114
 
115
	  }
116
  }
117
 
118
 
119
  public void onKeyDown(Widget arg0, char arg1, int arg2) {
120
 
121
 
122
	  if(arg1 == KEY_ENTER)
123
	    {
124
	      enterKey(arg0, arg1, arg2);
125
	    }
126
	    else if(arg1 == KEY_DOWN)
127
	    {
128
	      downKey(arg0, arg1, arg2);
129
	    }
130
	    else if(arg1 == KEY_UP)
131
	    {
132
	      upKey(arg0, arg1, arg2);
133
	    }
134
	    else if(arg1 == KEY_ESCAPE)
135
	    {
136
	      escapeKey(arg0, arg1, arg2);
137
	    }
138
 
139
 
140
  }
141
  /**
142
   * Not used at all (probleme avec ie, qui ne comprend pas les touches meta)
143
   */
144
  public void onKeyPress(Widget arg0, char arg1, int arg2) {
145
 
146
 
147
  }
148
 
149
  // The down key was pressed.
150
  protected void downKey(Widget arg0, char arg1, int arg2) {
151
 
152
	    int selectedIndex = choices.getSelectedIndex();
153
	    selectedIndex++;
154
	    if (selectedIndex >= choices.getItemCount())
155
	    {
156
	      selectedIndex = 0;
157
	    }
158
	    choices.setSelectedIndex(selectedIndex);
159
 }
160
 
161
  // The up key was pressed.
162
  protected void upKey(Widget arg0, char arg1, int arg2) {
163
    int selectedIndex = choices.getSelectedIndex();
164
    selectedIndex--;
165
    if(selectedIndex < 0)
166
    {
167
      selectedIndex = choices.getItemCount() - 1;
168
    }
169
    choices.setSelectedIndex(selectedIndex);
170
  }
171
 
172
  // The enter key was pressed.
173
  protected void enterKey(Widget arg0, char arg1, int arg2) {
174
      if(visible)
175
      {
176
        complete();
177
      }
178
      else {
179
    	 // Validation de l'entree : appel asynchrone
180
        /*  if (autoCompleteAsyncTextBoxListeners!= null) {
181
              autoCompleteAsyncTextBoxListeners.fireTextBoxEnter(this,this.getText(),currentValue);
182
          }*/
183
    	  currentValue=null;
184
    	  this.setText("");
185
    	  this.setValue(null);
186
      }
187
 
188
  }
189
 
190
//The escape key was pressed.
191
  protected void escapeKey(Widget arg0, char arg1, int arg2) {
192
    choices.clear();
193
    items.clear();
194
    choicesPopup.hide();
195
    visible = false;
196
 
197
  }
198
 
199
 
200
  // Any other non-special key was pressed.
201
  protected void otherKey(Widget arg0, char arg1, int arg2) {
202
 
203
 
204
    // Lancement appel
205
    String text = this.getText();
206
 
207
 
208
	    if(text.length() > 0)
209
	    {
210
 
211
	    	  currentValue=null;
212
 
213
		      items.clear();
214
 
215
		      if (getFromCache(text)!=null) {
216
		    	  items=getFromCache(text);
217
		    	  displayList();
218
		      }
219
		      else {
220
 
221
 
222
 
223
		    	  this.doFetchData(text);
224
		      }
225
		 }
226
 
227
 
228
 
229
 
230
  }
231
 
232
  public void onKeyUp(Widget arg0, char arg1, int arg2) {
233
 
234
	  switch(arg1) {
235
      case KEY_ALT:
236
      case KEY_CTRL:
237
      case KEY_DOWN:
238
      case KEY_END:
239
      case KEY_ENTER:
240
      case KEY_ESCAPE:
241
      case KEY_HOME:
242
      case KEY_LEFT:
243
      case KEY_PAGEDOWN:
244
      case KEY_PAGEUP:
245
      case KEY_RIGHT:
246
      case KEY_SHIFT:
247
      case KEY_TAB:
248
      case KEY_UP:
249
        break;
250
      default:
251
        otherKey(arg0, arg1, arg2);
252
        break;
253
    }
254
 
255
  }
256
 
257
 
258
  // Display assistant
259
 
260
    public void displayList() {
261
 
262
    	searching=false;
263
	    if(this.items.size() > 0)
264
	    {
265
 
266
	      addToCache(this.getText(),(Vector) items.clone());
267
 
268
	      choices.clear();
269
 
270
	      for(int i = 0; i < items.size(); i++)
271
	      {
272
	        choices.addItem(((String [])items.get(i))[0],((String [])items.get(i))[1]);
273
	      }
274
 
275
 
276
	      // if there is only one match and it is what is in the
277
	      // text field anyways there is no need to show autocompletion
278
	    //  if(items.size() == 1 && (((String []) items.get(0))[0]).compareTo(this.getText()) == 0)
279
	     // {
280
	       // choicesPopup.hide();
281
	     // } else {
282
	        choices.setSelectedIndex(0);
283
	        choices.setVisibleItemCount(items.size());
284
 
285
 
286
	        visible = true;
287
	        choicesPopup.setPopupPosition(this.getAbsoluteLeft(), this.getAbsoluteTop() + this.getOffsetHeight());
288
	        choicesPopup.setPopupPosition(this.getAbsoluteLeft(), this.getAbsoluteTop() + this.getOffsetHeight());
289
	        choicesPopup.setWidth(this.getOffsetWidth() + "px");
290
	        choices.setWidth(this.getOffsetWidth() + "px");
291
 
292
	        choicesPopup.show();
293
 
294
	    //  }
295
 
296
	    } else {
297
	      visible = false;
298
	      choicesPopup.hide();
299
	    }
300
	  }
301
 
302
  /**
303
   * A mouseclick in the list of items
304
   */
305
  public void onChange(Widget arg0) {
306
    complete();
307
  }
308
 
309
 
310
  public void onClick(Widget arg0) {
311
    complete();
312
  }
313
 
314
  // add selected item to textbox
315
  protected void complete()
316
  {
317
 
318
 
319
    if(choices.getItemCount() > 0)
320
    {
321
      this.setText(choices.getItemText(choices.getSelectedIndex()));
322
      currentValue=choices.getValue(choices.getSelectedIndex());
323
 /*     if (autoCompleteAsyncTextBoxListeners!= null) {
324
          autoCompleteAsyncTextBoxListeners.fireTextBoxComplete(responseTextHandler,this.getText(),currentValue);
325
      }*/
326
    }
327
 
328
    visible=false;
329
    items.clear();
330
    choices.clear();
331
    choicesPopup.hide();
332
  }
333
 
334
 
335
  public void addItem(String item, String value) {
336
	  items.add(new String [] {item, value});
337
  }
338
 
339
  private void addToCache (String query, Vector result)
340
  {
341
	cache.put(query.toLowerCase(),result);
342
  }
343
 
344
  private Vector getFromCache (String query)
345
  {
346
	return (Vector) cache.get(query.toLowerCase());
347
  }
348
 
349
 
350
 
351
public String getValue() {
352
	return currentValue;
353
}
354
 
355
 
356
public  void setValue(String value) {
357
	 this.currentValue=value;
358
}
359
 
360
 
361
 
362
 
363
}
364