Subversion Repositories eFlore/Archives.cel-v1

Rev

Rev 9 | Rev 12 | 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
 
23
// TODO : traiter latence (augmenter en fonction rapidité saisie + texte vide)
8 ddelon 24
// TODO : traitement espace apres l'espece (%20)
2 ddelon 25
 
26
import com.google.gwt.user.client.HTTPRequest;
27
import com.google.gwt.user.client.ResponseTextHandler;
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.RootPanel;
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 popupAdded = false;
62
  protected boolean visible = false;
10 ddelon 63
 
64
  /**
65
   * Value linked to current text
66
   */
6 ddelon 67
  protected String currentValue = null;
8 ddelon 68
 
2 ddelon 69
 
4 ddelon 70
 
2 ddelon 71
  /**
72
   * Default Constructor
73
   *
74
   */
4 ddelon 75
  public AutoCompleteAsyncTextBox(ResponseTextHandler rsp)
2 ddelon 76
  {
77
    super();
4 ddelon 78
    responseTextHandler=rsp;
2 ddelon 79
    this.addKeyboardListener(this);
8 ddelon 80
    choices.sinkEvents(Event.ONCLICK);
2 ddelon 81
    this.setStyleName("AutoCompleteAsyncTextBox");
82
 
83
    choicesPopup.add(choices);
84
    choicesPopup.addStyleName("AutoCompleteChoices");
85
 
86
    choices.setStyleName("list");
87
 
88
  }
89
 
90
 
91
 
92
  public void addAutoCompleteAsyncTextBoxListener(AutoCompleteAsyncTextBoxListener listener) {
93
	    if (autoCompleteAsyncTextBoxListeners == null) {
94
	      autoCompleteAsyncTextBoxListeners = new AutoCompleteAsyncTextBoxListenerCollection();
95
	    }
96
	    autoCompleteAsyncTextBoxListeners.addElement(listener);
97
  }
98
 
99
 
100
 
101
  public void setSearchUrl(String url) {
102
 
103
	  this.searchUrl=url;
104
  }
105
 
106
  private void doFetchURL(String match) {
107
	  /*
108
	   * Here we fetch the URL and call the handler
109
	   */
4 ddelon 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
  /**
124
   * Not used at all
125
   */
126
  public void onKeyDown(Widget arg0, char arg1, int arg2) {
8 ddelon 127
 
128
 
129
	  if(arg1 == KEY_ENTER)
2 ddelon 130
	    {
8 ddelon 131
	      enterKey(arg0, arg1, arg2);
2 ddelon 132
	    }
8 ddelon 133
	    else if(arg1 == KEY_DOWN)
2 ddelon 134
	    {
8 ddelon 135
	      downKey(arg0, arg1, arg2);
2 ddelon 136
	    }
8 ddelon 137
	    else if(arg1 == KEY_UP)
138
	    {
139
	      upKey(arg0, arg1, arg2);
140
	    }
141
	    else if(arg1 == KEY_ESCAPE)
142
	    {
143
	      escapeKey(arg0, arg1, arg2);
144
	    }
145
 
2 ddelon 146
 
8 ddelon 147
  }
2 ddelon 148
  /**
8 ddelon 149
   * Not used at all (probleme avec ie, qui ne comprend pas les touches meta)
2 ddelon 150
   */
8 ddelon 151
  public void onKeyPress(Widget arg0, char arg1, int arg2) {
152
 
2 ddelon 153
 
8 ddelon 154
  }
155
 
156
  // The down key was pressed.
157
  protected void downKey(Widget arg0, char arg1, int arg2) {
158
 
159
	    int selectedIndex = choices.getSelectedIndex();
160
	    selectedIndex++;
161
	    if (selectedIndex >= choices.getItemCount())
162
	    {
163
	      selectedIndex = 0;
164
	    }
165
	    choices.setSelectedIndex(selectedIndex);
166
 }
2 ddelon 167
 
8 ddelon 168
  // The up key was pressed.
169
  protected void upKey(Widget arg0, char arg1, int arg2) {
170
    int selectedIndex = choices.getSelectedIndex();
171
    selectedIndex--;
172
    if(selectedIndex < 0)
173
    {
174
      selectedIndex = choices.getItemCount() - 1;
175
    }
176
    choices.setSelectedIndex(selectedIndex);
177
  }
2 ddelon 178
 
8 ddelon 179
  // The enter key was pressed.
180
  protected void enterKey(Widget arg0, char arg1, int arg2) {
2 ddelon 181
      if(visible)
182
      {
183
        complete();
184
      }
185
      else {
8 ddelon 186
    	 // Validation de l'entree : appel asynchrone
2 ddelon 187
          if (autoCompleteAsyncTextBoxListeners!= null) {
6 ddelon 188
              autoCompleteAsyncTextBoxListeners.fireTextBoxEnter(this,this.getText(),currentValue);
2 ddelon 189
          }
6 ddelon 190
    	  currentValue=null;
2 ddelon 191
    	  this.setText("");
192
      }
8 ddelon 193
 
194
  }
195
 
196
//The escape key was pressed.
197
  protected void escapeKey(Widget arg0, char arg1, int arg2) {
198
    choices.clear();
199
    items.clear();
200
    choicesPopup.hide();
201
    visible = false;
202
 
203
  }
204
 
205
 
206
  // Any other non-special key was pressed.
207
  protected void otherKey(Widget arg0, char arg1, int arg2) {
2 ddelon 208
 
8 ddelon 209
 
210
    // Lancement appel
2 ddelon 211
    String text = this.getText();
212
 
8 ddelon 213
 
214
	    if(text.length() > 0)
215
	    {
216
 
217
		      items.clear();
218
 
219
		      if (getFromCache(text)!=null) {
220
		    	  items=getFromCache(text);
221
		    	  displayList();
222
		      }
223
		      else {
224
 
225
		    	  this.doFetchURL(text);
226
		      }
227
		 }
228
 
229
 
230
 
231
 
232
  }
233
 
234
  public void onKeyUp(Widget arg0, char arg1, int arg2) {
235
 
236
	  switch(arg1) {
237
      case KEY_ALT:
238
      case KEY_CTRL:
239
      case KEY_DOWN:
240
      case KEY_END:
241
      case KEY_ENTER:
242
      case KEY_ESCAPE:
243
      case KEY_HOME:
244
      case KEY_LEFT:
245
      case KEY_PAGEDOWN:
246
      case KEY_PAGEUP:
247
      case KEY_RIGHT:
248
      case KEY_SHIFT:
249
      case KEY_TAB:
250
      case KEY_UP:
251
        break;
252
      default:
253
        otherKey(arg0, arg1, arg2);
254
        break;
2 ddelon 255
    }
8 ddelon 256
 
2 ddelon 257
  }
258
 
259
 
260
  // Display assistant
261
 
262
    public void displayList() {
263
 
264
    	searching=false;
265
	    if(this.items.size() > 0)
266
	    {
267
 
7 ddelon 268
	      addToCache(this.getText(),(Vector) items.clone());
2 ddelon 269
 
270
	      choices.clear();
271
 
272
	      for(int i = 0; i < items.size(); i++)
273
	      {
6 ddelon 274
	        choices.addItem(((String [])items.get(i))[0],((String [])items.get(i))[1]);
2 ddelon 275
	      }
276
 
277
 
278
	      // if there is only one match and it is what is in the
279
	      // text field anyways there is no need to show autocompletion
8 ddelon 280
	    //  if(items.size() == 1 && (((String []) items.get(0))[0]).compareTo(this.getText()) == 0)
281
	     // {
282
	       // choicesPopup.hide();
283
	     // } else {
2 ddelon 284
	        choices.setSelectedIndex(0);
285
	        choices.setVisibleItemCount(items.size());
286
 
287
	        if(!popupAdded)
288
	        {
289
	          RootPanel.get().add(choicesPopup);
290
	          popupAdded = true;
291
	        }
292
	        choicesPopup.show();
293
	        visible = true;
294
	        choicesPopup.setPopupPosition(this.getAbsoluteLeft(),
295
	        this.getAbsoluteTop() + this.getOffsetHeight());
8 ddelon 296
	        choicesPopup.setWidth(this.getOffsetWidth() + "px");
2 ddelon 297
	        choices.setWidth(this.getOffsetWidth() + "px");
8 ddelon 298
	    //  }
2 ddelon 299
 
300
	    } else {
301
	      visible = false;
302
	      choicesPopup.hide();
303
	    }
304
	  }
305
 
306
  /**
307
   * A mouseclick in the list of items
308
   */
309
  public void onChange(Widget arg0) {
310
    complete();
311
  }
8 ddelon 312
 
2 ddelon 313
 
314
  public void onClick(Widget arg0) {
315
    complete();
316
  }
317
 
318
  // add selected item to textbox
319
  protected void complete()
320
  {
321
    if(choices.getItemCount() > 0)
322
    {
323
      this.setText(choices.getItemText(choices.getSelectedIndex()));
6 ddelon 324
      currentValue=choices.getValue(choices.getSelectedIndex());
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
 
355
 
356
 
357
}
358