Subversion Repositories eFlore/Archives.cel-v1

Rev

Rev 25 | Rev 27 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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