Subversion Repositories eFlore/Archives.cel-v1

Rev

Rev 28 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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