Subversion Repositories eFlore/Archives.cel-v1

Rev

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

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