Subversion Repositories eFlore/Archives.cel-v1

Rev

Rev 12 | Rev 25 | 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
  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
 
284
	        if(!popupAdded)
285
	        {
286
	          RootPanel.get().add(choicesPopup);
287
	          popupAdded = true;
288
	        }
289
	        choicesPopup.show();
290
	        visible = true;
291
	        choicesPopup.setPopupPosition(this.getAbsoluteLeft(),
292
	        this.getAbsoluteTop() + this.getOffsetHeight());
8 ddelon 293
	        choicesPopup.setWidth(this.getOffsetWidth() + "px");
2 ddelon 294
	        choices.setWidth(this.getOffsetWidth() + "px");
8 ddelon 295
	    //  }
2 ddelon 296
 
297
	    } else {
298
	      visible = false;
299
	      choicesPopup.hide();
300
	    }
301
	  }
302
 
303
  /**
304
   * A mouseclick in the list of items
305
   */
306
  public void onChange(Widget arg0) {
307
    complete();
308
  }
8 ddelon 309
 
2 ddelon 310
 
311
  public void onClick(Widget arg0) {
312
    complete();
313
  }
314
 
315
  // add selected item to textbox
316
  protected void complete()
317
  {
318
    if(choices.getItemCount() > 0)
319
    {
320
      this.setText(choices.getItemText(choices.getSelectedIndex()));
6 ddelon 321
      currentValue=choices.getValue(choices.getSelectedIndex());
2 ddelon 322
    }
323
 
324
    visible=false;
325
    items.clear();
326
    choices.clear();
327
    choicesPopup.hide();
328
  }
329
 
330
 
6 ddelon 331
  public void addItem(String item, String value) {
332
	  items.add(new String [] {item, value});
2 ddelon 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
  }
10 ddelon 344
 
345
 
346
 
347
public String getValue() {
348
	return currentValue;
349
}
2 ddelon 350
 
351
 
13 ddelon 352
public  void setValue(String value) {
353
	 this.currentValue=value;
354
}
355
 
2 ddelon 356
 
357
 
358
}
359