Subversion Repositories eFlore/Archives.cel-v1

Rev

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