Subversion Repositories eFlore/Applications.cel

Rev

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