Subversion Repositories eFlore/Archives.cel-v1

Rev

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

Rev 13 Rev 25
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.user.client.HTTPRequest;
26
import com.google.gwt.user.client.HTTPRequest;
27
import com.google.gwt.user.client.ResponseTextHandler;
27
import com.google.gwt.user.client.ResponseTextHandler;
28
import com.google.gwt.user.client.ui.KeyboardListener;
28
import com.google.gwt.user.client.ui.KeyboardListener;
29
import com.google.gwt.user.client.ui.ListBox;
29
import com.google.gwt.user.client.ui.ListBox;
30
import com.google.gwt.user.client.ui.PopupPanel;
30
import com.google.gwt.user.client.ui.PopupPanel;
31
import com.google.gwt.user.client.ui.RootPanel;
31
import com.google.gwt.user.client.ui.RootPanel;
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 popupAdded = false;
61
  protected boolean popupAdded = false;
62
  protected boolean visible = false;
62
  protected boolean visible = false;
63
  
63
  
64
  /**
64
  /**
65
   * Value linked to current text
65
   * Value linked to current text
66
   */
66
   */
67
  protected String currentValue = null;
67
  protected String currentValue = null;
68
  
68
  
69
   
69
   
70
   
70
   
71
  /**
71
  /**
72
   * Default Constructor
72
   * Default Constructor
73
   *
73
   *
74
   */
74
   */
75
  public AutoCompleteAsyncTextBox(ResponseTextHandler rsp)
75
  public AutoCompleteAsyncTextBox(ResponseTextHandler rsp)
76
  {
76
  {
77
    super();
77
    super();
78
    responseTextHandler=rsp;
78
    responseTextHandler=rsp;
79
    this.addKeyboardListener(this);
79
    this.addKeyboardListener(this);
80
    choices.sinkEvents(Event.ONCLICK); 
80
    choices.sinkEvents(Event.ONCLICK); 
81
    this.setStyleName("AutoCompleteAsyncTextBox");
81
    this.setStyleName("AutoCompleteAsyncTextBox");
82
       
82
       
83
    choicesPopup.add(choices);
83
    choicesPopup.add(choices);
84
    choicesPopup.addStyleName("AutoCompleteChoices");
84
    choicesPopup.addStyleName("AutoCompleteChoices");
85
       
85
       
86
    choices.setStyleName("list");
86
    choices.setStyleName("list");
87
    
87
    
88
  }
88
  }
89
 
89
 
90
 
90
 
91
  
91
  
92
  public void addAutoCompleteAsyncTextBoxListener(AutoCompleteAsyncTextBoxListener listener) {
92
  public void addAutoCompleteAsyncTextBoxListener(AutoCompleteAsyncTextBoxListener listener) {
93
	    if (autoCompleteAsyncTextBoxListeners == null) {
93
	    if (autoCompleteAsyncTextBoxListeners == null) {
94
	      autoCompleteAsyncTextBoxListeners = new AutoCompleteAsyncTextBoxListenerCollection();
94
	      autoCompleteAsyncTextBoxListeners = new AutoCompleteAsyncTextBoxListenerCollection();
95
	    }
95
	    }
96
	    autoCompleteAsyncTextBoxListeners.addElement(listener);
96
	    autoCompleteAsyncTextBoxListeners.addElement(listener);
97
  }
97
  }
98
 
98
 
99
 
99
 
100
 
100
 
101
  public void setSearchUrl(String url) {
101
  public void setSearchUrl(String url) {
102
	  
102
	  
103
	  this.searchUrl=url;
103
	  this.searchUrl=url;
104
  }
104
  }
105
  
105
  
106
  private void doFetchURL(String match) {
106
  private void doFetchURL(String match) {
107
	  /*
107
	  /*
108
	   * Here we fetch the URL and call the handler
108
	   * Here we fetch the URL and call the handler
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
      }
189
      }
190
 
190
 
191
  }
191
  }
192
 
192
 
193
//The escape key was pressed.
193
//The escape key was pressed.
194
  protected void escapeKey(Widget arg0, char arg1, int arg2) {
194
  protected void escapeKey(Widget arg0, char arg1, int arg2) {
195
    choices.clear();
195
    choices.clear();
196
    items.clear();
196
    items.clear();
197
    choicesPopup.hide();
197
    choicesPopup.hide();
198
    visible = false;
198
    visible = false;
199
 
199
 
200
  } 
200
  } 
201
 
201
 
202
 
202
 
203
  // Any other non-special key was pressed.
203
  // Any other non-special key was pressed.
204
  protected void otherKey(Widget arg0, char arg1, int arg2) {
204
  protected void otherKey(Widget arg0, char arg1, int arg2) {
205
   
205
   
206
	
206
	
207
    // Lancement appel 
207
    // Lancement appel 
208
    String text = this.getText();
208
    String text = this.getText();
209
    
209
    
210
    	
210
    	
211
	    if(text.length() > 0)
211
	    if(text.length() > 0)
212
	    {
212
	    {
213
	    
213
	    
214
		      items.clear();
214
		      items.clear();
215
		      
215
		      
216
		      if (getFromCache(text)!=null) {
216
		      if (getFromCache(text)!=null) {
217
		    	  items=getFromCache(text);
217
		    	  items=getFromCache(text);
218
		    	  displayList();
218
		    	  displayList();
219
		      }
219
		      }
220
		      else {
220
		      else {
221
		     
221
		     
222
		    	  this.doFetchURL(text);
222
		    	  this.doFetchURL(text);
223
		      }
223
		      }
224
		 }
224
		 }
225
   
225
   
226
	
226
	
227
	    
227
	    
228
    
228
    
229
  } 
229
  } 
230
  
230
  
231
  public void onKeyUp(Widget arg0, char arg1, int arg2) {
231
  public void onKeyUp(Widget arg0, char arg1, int arg2) {
232
	  
232
	  
233
	  switch(arg1) {
233
	  switch(arg1) {
234
      case KEY_ALT:
234
      case KEY_ALT:
235
      case KEY_CTRL:
235
      case KEY_CTRL:
236
      case KEY_DOWN:
236
      case KEY_DOWN:
237
      case KEY_END:
237
      case KEY_END:
238
      case KEY_ENTER:
238
      case KEY_ENTER:
239
      case KEY_ESCAPE:
239
      case KEY_ESCAPE:
240
      case KEY_HOME:
240
      case KEY_HOME:
241
      case KEY_LEFT:
241
      case KEY_LEFT:
242
      case KEY_PAGEDOWN:
242
      case KEY_PAGEDOWN:
243
      case KEY_PAGEUP:
243
      case KEY_PAGEUP:
244
      case KEY_RIGHT:
244
      case KEY_RIGHT:
245
      case KEY_SHIFT:
245
      case KEY_SHIFT:
246
      case KEY_TAB:
246
      case KEY_TAB:
247
      case KEY_UP:
247
      case KEY_UP:
248
        break;
248
        break;
249
      default:
249
      default:
250
        otherKey(arg0, arg1, arg2);
250
        otherKey(arg0, arg1, arg2);
251
        break;
251
        break;
252
    }
252
    }
253
 
253
 
254
  }
254
  }
255
    
255
    
256
  
256
  
257
  // Display assistant
257
  // Display assistant
258
  
258
  
259
    public void displayList() { 
259
    public void displayList() { 
260
    	
260
    	
261
    	searching=false;
261
    	searching=false;
262
	    if(this.items.size() > 0)
262
	    if(this.items.size() > 0)
263
	    {
263
	    {
264
	    	
264
	    	
265
	      addToCache(this.getText(),(Vector) items.clone());
265
	      addToCache(this.getText(),(Vector) items.clone());
266
	      
266
	      
267
	      choices.clear();
267
	      choices.clear();
268
	           
268
	           
269
	      for(int i = 0; i < items.size(); i++)
269
	      for(int i = 0; i < items.size(); i++)
270
	      {
270
	      {
271
	        choices.addItem(((String [])items.get(i))[0],((String [])items.get(i))[1]);
271
	        choices.addItem(((String [])items.get(i))[0],((String [])items.get(i))[1]);
272
	      }
272
	      }
273
	      
273
	      
274
	           
274
	           
275
	      // if there is only one match and it is what is in the
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
276
	      // text field anyways there is no need to show autocompletion
277
	    //  if(items.size() == 1 && (((String []) items.get(0))[0]).compareTo(this.getText()) == 0)
277
	    //  if(items.size() == 1 && (((String []) items.get(0))[0]).compareTo(this.getText()) == 0)
278
	     // {
278
	     // {
279
	       // choicesPopup.hide();
279
	       // choicesPopup.hide();
280
	     // } else {
280
	     // } else {
281
	        choices.setSelectedIndex(0);
281
	        choices.setSelectedIndex(0);
282
	        choices.setVisibleItemCount(items.size());
282
	        choices.setVisibleItemCount(items.size());
283
	               
283
	               
284
	        if(!popupAdded)
284
	        if(!popupAdded)
285
	        {
285
	        {
286
	          RootPanel.get().add(choicesPopup);
286
	          RootPanel.get().add(choicesPopup);
287
	          popupAdded = true;
287
	          popupAdded = true;
288
	        }
288
	        }
289
	        choicesPopup.show();
289
	        choicesPopup.show();
290
	        visible = true;
290
	        visible = true;
291
	        choicesPopup.setPopupPosition(this.getAbsoluteLeft(),
291
	        choicesPopup.setPopupPosition(this.getAbsoluteLeft(),
292
	        this.getAbsoluteTop() + this.getOffsetHeight());
292
	        this.getAbsoluteTop() + this.getOffsetHeight());
293
	        choicesPopup.setWidth(this.getOffsetWidth() + "px");
293
	        choicesPopup.setWidth(this.getOffsetWidth() + "px");
294
	        choices.setWidth(this.getOffsetWidth() + "px");
294
	        choices.setWidth(this.getOffsetWidth() + "px");
295
	    //  }
295
	    //  }
296
	
296
	
297
	    } else {
297
	    } else {
298
	      visible = false;
298
	      visible = false;
299
	      choicesPopup.hide();
299
	      choicesPopup.hide();
300
	    }
300
	    }
301
	  }
301
	  }
302
 
302
 
303
  /**
303
  /**
304
   * A mouseclick in the list of items
304
   * A mouseclick in the list of items
305
   */
305
   */
306
  public void onChange(Widget arg0) {
306
  public void onChange(Widget arg0) {
307
    complete();
307
    complete();
308
  }
308
  }
309
  
309
  
310
 
310
 
311
  public void onClick(Widget arg0) {
311
  public void onClick(Widget arg0) {
312
    complete();
312
    complete();
313
  }
313
  }
314
   
314
   
315
  // add selected item to textbox
315
  // add selected item to textbox
316
  protected void complete()
316
  protected void complete()
317
  {
317
  {
-
 
318
	  
-
 
319
    
318
    if(choices.getItemCount() > 0)
320
    if(choices.getItemCount() > 0)
319
    {
321
    {
320
      this.setText(choices.getItemText(choices.getSelectedIndex()));
322
      this.setText(choices.getItemText(choices.getSelectedIndex()));
321
      currentValue=choices.getValue(choices.getSelectedIndex());
323
      currentValue=choices.getValue(choices.getSelectedIndex());
-
 
324
      if (autoCompleteAsyncTextBoxListeners!= null) {
-
 
325
          autoCompleteAsyncTextBoxListeners.fireTextBoxComplete(responseTextHandler,this.getText(),currentValue);
-
 
326
      }
322
    }
327
    }
323
 
328
 
324
    visible=false;
329
    visible=false;
325
    items.clear();
330
    items.clear();
326
    choices.clear();
331
    choices.clear();
327
    choicesPopup.hide();
332
    choicesPopup.hide();
328
  }
333
  }
329
  
334
  
330
  
335
  
331
  public void addItem(String item, String value) {
336
  public void addItem(String item, String value) {
332
	  items.add(new String [] {item, value});
337
	  items.add(new String [] {item, value});
333
  }
338
  }
334
  
339
  
335
  private void addToCache (String query, Vector result)
340
  private void addToCache (String query, Vector result)
336
  {
341
  {
337
	cache.put(query.toLowerCase(),result);
342
	cache.put(query.toLowerCase(),result);
338
  }
343
  }
339
 
344
 
340
  private Vector getFromCache (String query)
345
  private Vector getFromCache (String query)
341
  {
346
  {
342
	return (Vector) cache.get(query.toLowerCase());
347
	return (Vector) cache.get(query.toLowerCase());
343
  }
348
  }
344
 
349
 
345
 
350
 
346
 
351
 
347
public String getValue() {
352
public String getValue() {
348
	return currentValue;
353
	return currentValue;
349
}
354
}
350
  
355
  
351
 
356
 
352
public  void setValue(String value) {
357
public  void setValue(String value) {
353
	 this.currentValue=value;
358
	 this.currentValue=value;
354
}
359
}
355
  
-
 
356
 
360
  
357
  
361
  
358
}
362
}
359
 
363