Subversion Repositories eFlore/Archives.cel-v1

Rev

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

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