Subversion Repositories eFlore/Archives.cel-v1

Rev

Rev 26 | Rev 28 | 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)
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
	   */
26 ddelon 110
 
111
 
9 ddelon 112
	  String rematch=match.replaceAll(" ","/");
113
	  rematch=rematch.replaceAll("%","");
2 ddelon 114
 
115
	  if (this.searchUrl!=null && searching==false) {
116
		  searching=true;
9 ddelon 117
	 //     HTTPRequest.asyncGet(URL.encodeComponent(this.searchUrl) + rematch, responseTextHandler );
4 ddelon 118
	      HTTPRequest.asyncGet(this.searchUrl + rematch, responseTextHandler );
2 ddelon 119
 
120
	  }
121
  }
122
 
123
 
124
  public void onKeyDown(Widget arg0, char arg1, int arg2) {
8 ddelon 125
 
126
 
127
	  if(arg1 == KEY_ENTER)
2 ddelon 128
	    {
8 ddelon 129
	      enterKey(arg0, arg1, arg2);
2 ddelon 130
	    }
8 ddelon 131
	    else if(arg1 == KEY_DOWN)
2 ddelon 132
	    {
8 ddelon 133
	      downKey(arg0, arg1, arg2);
2 ddelon 134
	    }
8 ddelon 135
	    else if(arg1 == KEY_UP)
136
	    {
137
	      upKey(arg0, arg1, arg2);
138
	    }
139
	    else if(arg1 == KEY_ESCAPE)
140
	    {
141
	      escapeKey(arg0, arg1, arg2);
142
	    }
143
 
2 ddelon 144
 
8 ddelon 145
  }
2 ddelon 146
  /**
8 ddelon 147
   * Not used at all (probleme avec ie, qui ne comprend pas les touches meta)
2 ddelon 148
   */
8 ddelon 149
  public void onKeyPress(Widget arg0, char arg1, int arg2) {
150
 
2 ddelon 151
 
8 ddelon 152
  }
153
 
154
  // The down key was pressed.
155
  protected void downKey(Widget arg0, char arg1, int arg2) {
156
 
157
	    int selectedIndex = choices.getSelectedIndex();
158
	    selectedIndex++;
159
	    if (selectedIndex >= choices.getItemCount())
160
	    {
161
	      selectedIndex = 0;
162
	    }
163
	    choices.setSelectedIndex(selectedIndex);
164
 }
2 ddelon 165
 
8 ddelon 166
  // The up key was pressed.
167
  protected void upKey(Widget arg0, char arg1, int arg2) {
168
    int selectedIndex = choices.getSelectedIndex();
169
    selectedIndex--;
170
    if(selectedIndex < 0)
171
    {
172
      selectedIndex = choices.getItemCount() - 1;
173
    }
174
    choices.setSelectedIndex(selectedIndex);
175
  }
2 ddelon 176
 
8 ddelon 177
  // The enter key was pressed.
178
  protected void enterKey(Widget arg0, char arg1, int arg2) {
2 ddelon 179
      if(visible)
180
      {
181
        complete();
182
      }
183
      else {
8 ddelon 184
    	 // Validation de l'entree : appel asynchrone
2 ddelon 185
          if (autoCompleteAsyncTextBoxListeners!= null) {
6 ddelon 186
              autoCompleteAsyncTextBoxListeners.fireTextBoxEnter(this,this.getText(),currentValue);
2 ddelon 187
          }
6 ddelon 188
    	  currentValue=null;
2 ddelon 189
    	  this.setText("");
190
      }
8 ddelon 191
 
192
  }
193
 
194
//The escape key was pressed.
195
  protected void escapeKey(Widget arg0, char arg1, int arg2) {
196
    choices.clear();
197
    items.clear();
198
    choicesPopup.hide();
199
    visible = false;
200
 
201
  }
202
 
203
 
204
  // Any other non-special key was pressed.
205
  protected void otherKey(Widget arg0, char arg1, int arg2) {
2 ddelon 206
 
8 ddelon 207
 
208
    // Lancement appel
2 ddelon 209
    String text = this.getText();
210
 
8 ddelon 211
 
212
	    if(text.length() > 0)
213
	    {
214
 
215
		      items.clear();
216
 
217
		      if (getFromCache(text)!=null) {
218
		    	  items=getFromCache(text);
219
		    	  displayList();
220
		      }
221
		      else {
222
 
223
		    	  this.doFetchURL(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;
2 ddelon 253
    }
8 ddelon 254
 
2 ddelon 255
  }
256
 
257
 
258
  // Display assistant
259
 
260
    public void displayList() {
261
 
262
    	searching=false;
263
	    if(this.items.size() > 0)
264
	    {
265
 
7 ddelon 266
	      addToCache(this.getText(),(Vector) items.clone());
2 ddelon 267
 
268
	      choices.clear();
269
 
270
	      for(int i = 0; i < items.size(); i++)
271
	      {
6 ddelon 272
	        choices.addItem(((String [])items.get(i))[0],((String [])items.get(i))[1]);
2 ddelon 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
8 ddelon 278
	    //  if(items.size() == 1 && (((String []) items.get(0))[0]).compareTo(this.getText()) == 0)
279
	     // {
280
	       // choicesPopup.hide();
281
	     // } else {
2 ddelon 282
	        choices.setSelectedIndex(0);
283
	        choices.setVisibleItemCount(items.size());
284
 
285
	        if(!popupAdded)
286
	        {
287
	          RootPanel.get().add(choicesPopup);
288
	          popupAdded = true;
289
	        }
290
	        choicesPopup.show();
291
	        visible = true;
292
	        choicesPopup.setPopupPosition(this.getAbsoluteLeft(),
293
	        this.getAbsoluteTop() + this.getOffsetHeight());
8 ddelon 294
	        choicesPopup.setWidth(this.getOffsetWidth() + "px");
2 ddelon 295
	        choices.setWidth(this.getOffsetWidth() + "px");
8 ddelon 296
	    //  }
2 ddelon 297
 
298
	    } else {
299
	      visible = false;
300
	      choicesPopup.hide();
301
	    }
302
	  }
303
 
304
  /**
305
   * A mouseclick in the list of items
306
   */
307
  public void onChange(Widget arg0) {
308
    complete();
309
  }
8 ddelon 310
 
2 ddelon 311
 
312
  public void onClick(Widget arg0) {
313
    complete();
314
  }
315
 
316
  // add selected item to textbox
317
  protected void complete()
318
  {
25 ddelon 319
 
320
 
2 ddelon 321
    if(choices.getItemCount() > 0)
322
    {
323
      this.setText(choices.getItemText(choices.getSelectedIndex()));
6 ddelon 324
      currentValue=choices.getValue(choices.getSelectedIndex());
25 ddelon 325
      if (autoCompleteAsyncTextBoxListeners!= null) {
326
          autoCompleteAsyncTextBoxListeners.fireTextBoxComplete(responseTextHandler,this.getText(),currentValue);
327
      }
2 ddelon 328
    }
329
 
330
    visible=false;
331
    items.clear();
332
    choices.clear();
333
    choicesPopup.hide();
334
  }
335
 
336
 
6 ddelon 337
  public void addItem(String item, String value) {
338
	  items.add(new String [] {item, value});
2 ddelon 339
  }
340
 
341
  private void addToCache (String query, Vector result)
342
  {
343
	cache.put(query.toLowerCase(),result);
344
  }
345
 
346
  private Vector getFromCache (String query)
347
  {
348
	return (Vector) cache.get(query.toLowerCase());
349
  }
10 ddelon 350
 
351
 
352
 
353
public String getValue() {
354
	return currentValue;
355
}
2 ddelon 356
 
357
 
13 ddelon 358
public  void setValue(String value) {
359
	 this.currentValue=value;
360
}
361
 
2 ddelon 362
 
363
}
364