Subversion Repositories eFlore/Archives.cel-v1

Rev

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

Rev 4 Rev 6
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 : traiter Tab (selection)
24
// TODO : traiter Tab (selection)
25
// TODO : revoir traitement keyup, keydown
25
// TODO : revoir traitement keyup, keydown
26
// TODO : reactiver le cache (du design à revoir dans ce cas la) 
26
// TODO : reactiver le cache (du design à revoir dans ce cas la) 
27
 
27
 
28
import com.google.gwt.user.client.HTTPRequest;
28
import com.google.gwt.user.client.HTTPRequest;
29
import com.google.gwt.user.client.ResponseTextHandler;
29
import com.google.gwt.user.client.ResponseTextHandler;
30
import com.google.gwt.user.client.ui.ChangeListener;
30
import com.google.gwt.user.client.ui.ChangeListener;
31
import com.google.gwt.user.client.ui.KeyboardListener;
31
import com.google.gwt.user.client.ui.KeyboardListener;
32
import com.google.gwt.user.client.ui.ListBox;
32
import com.google.gwt.user.client.ui.ListBox;
33
import com.google.gwt.user.client.ui.PopupPanel;
33
import com.google.gwt.user.client.ui.PopupPanel;
34
import com.google.gwt.user.client.ui.RootPanel;
34
import com.google.gwt.user.client.ui.RootPanel;
35
import com.google.gwt.user.client.ui.TextBox;
35
import com.google.gwt.user.client.ui.TextBox;
36
import com.google.gwt.user.client.ui.Widget;
36
import com.google.gwt.user.client.ui.Widget;
37
 
37
 
38
import java.util.Vector;
38
import java.util.Vector;
39
import java.util.HashMap;
39
import java.util.HashMap;
40
 
40
 
41
 
41
 
42
 
42
 
43
public class AutoCompleteAsyncTextBox extends TextBox
43
public class AutoCompleteAsyncTextBox extends TextBox
44
    implements KeyboardListener, ChangeListener, SourcesAutoCompleteAsyncTextBoxEvents {
44
    implements KeyboardListener, ChangeListener, SourcesAutoCompleteAsyncTextBoxEvents {
45
   
45
   
46
  private String searchUrl = null; 
46
  private String searchUrl = null; 
47
  private AutoCompleteAsyncTextBoxListenerCollection autoCompleteAsyncTextBoxListeners=null;  
47
  private AutoCompleteAsyncTextBoxListenerCollection autoCompleteAsyncTextBoxListeners=null;  
48
    
48
    
49
  private HashMap cache = new HashMap();
49
  private HashMap cache = new HashMap();
50
  private boolean searching = false;
50
  private boolean searching = false;
51
  private ResponseTextHandler responseTextHandler=null;
51
  private ResponseTextHandler responseTextHandler=null;
52
		  
52
		  
53
  protected PopupPanel choicesPopup = new PopupPanel(true);
53
  protected PopupPanel choicesPopup = new PopupPanel(true);
54
  protected ListBox choices = new ListBox();
54
  protected ListBox choices = new ListBox();
55
  protected Vector items = new Vector(); 
55
  protected Vector items = new Vector(); 
56
  protected boolean popupAdded = false;
56
  protected boolean popupAdded = false;
57
  protected boolean visible = false;
57
  protected boolean visible = false;
58
  protected int current = -1;
58
  protected String currentValue = null;
59
   
59
   
60
   
60
   
61
  /**
61
  /**
62
   * Default Constructor
62
   * Default Constructor
63
   *
63
   *
64
   */
64
   */
65
  public AutoCompleteAsyncTextBox(ResponseTextHandler rsp)
65
  public AutoCompleteAsyncTextBox(ResponseTextHandler rsp)
66
  {
66
  {
67
    super();
67
    super();
68
    responseTextHandler=rsp;
68
    responseTextHandler=rsp;
69
    this.addKeyboardListener(this);
69
    this.addKeyboardListener(this);
70
    choices.addChangeListener(this);
70
    choices.addChangeListener(this);
71
    this.setStyleName("AutoCompleteAsyncTextBox");
71
    this.setStyleName("AutoCompleteAsyncTextBox");
72
       
72
       
73
    choicesPopup.add(choices);
73
    choicesPopup.add(choices);
74
    choicesPopup.addStyleName("AutoCompleteChoices");
74
    choicesPopup.addStyleName("AutoCompleteChoices");
75
       
75
       
76
    choices.setStyleName("list");
76
    choices.setStyleName("list");
77
    
77
    
78
  }
78
  }
79
 
79
 
80
 
80
 
81
  
81
  
82
  public void addAutoCompleteAsyncTextBoxListener(AutoCompleteAsyncTextBoxListener listener) {
82
  public void addAutoCompleteAsyncTextBoxListener(AutoCompleteAsyncTextBoxListener listener) {
83
	    if (autoCompleteAsyncTextBoxListeners == null) {
83
	    if (autoCompleteAsyncTextBoxListeners == null) {
84
	      autoCompleteAsyncTextBoxListeners = new AutoCompleteAsyncTextBoxListenerCollection();
84
	      autoCompleteAsyncTextBoxListeners = new AutoCompleteAsyncTextBoxListenerCollection();
85
	    }
85
	    }
86
	    autoCompleteAsyncTextBoxListeners.addElement(listener);
86
	    autoCompleteAsyncTextBoxListeners.addElement(listener);
87
  }
87
  }
88
 
88
 
89
 
89
 
90
 
90
 
91
  public void setSearchUrl(String url) {
91
  public void setSearchUrl(String url) {
92
	  
92
	  
93
	  this.searchUrl=url;
93
	  this.searchUrl=url;
94
  }
94
  }
95
  
95
  
96
  private void doFetchURL(String match) {
96
  private void doFetchURL(String match) {
97
	  /*
97
	  /*
98
	   * Here we fetch the URL and call the handler
98
	   * Here we fetch the URL and call the handler
99
	   */	  
99
	   */	  
100
	 
100
	 
101
	  String rematch=match.replaceFirst(" ","/");
101
	  String rematch=match.replaceFirst(" ","/");
102
	  
102
	  
103
	  if (this.searchUrl!=null && searching==false) {
103
	  if (this.searchUrl!=null && searching==false) {
104
		  searching=true;
104
		  searching=true;
105
	      HTTPRequest.asyncGet(this.searchUrl + rematch, responseTextHandler );
105
	      HTTPRequest.asyncGet(this.searchUrl + rematch, responseTextHandler );
106
 
106
 
107
	  }
107
	  }
108
  }
108
  }
109
 
109
 
110
  
110
  
111
  /**
111
  /**
112
   * Not used at all
112
   * Not used at all
113
   */
113
   */
114
  public void onKeyDown(Widget arg0, char arg1, int arg2) {
114
  public void onKeyDown(Widget arg0, char arg1, int arg2) {
115
  }
115
  }
116
 
116
 
117
  /**
117
  /**
118
   * Not used at all
118
   * Not used at all
119
   */
119
   */
120
  public void onKeyPress(Widget arg0, char arg1, int arg2) {
120
  public void onKeyPress(Widget arg0, char arg1, int arg2) {
121
	 
121
	 
122
	    if(arg1 == KEY_DOWN)
122
	    if(arg1 == KEY_DOWN)
123
	    {
123
	    {
124
	      int selectedIndex = choices.getSelectedIndex();
124
	      int selectedIndex = choices.getSelectedIndex();
125
	      selectedIndex++;
125
	      selectedIndex++;
126
	      if(selectedIndex >= choices.getItemCount())
126
	      if(selectedIndex >= choices.getItemCount())
127
	      {
127
	      {
128
	        selectedIndex = 0;
128
	        selectedIndex = 0;
129
	      }
129
	      }
130
	      choices.setSelectedIndex(selectedIndex);
130
	      choices.setSelectedIndex(selectedIndex);
131
	           
131
	           
132
	      return;
132
	      return;
133
	    }
133
	    }
134
	       
134
	       
135
	    if(arg1 == KEY_UP)
135
	    if(arg1 == KEY_UP)
136
	    {
136
	    {
137
	      int selectedIndex = choices.getSelectedIndex();
137
	      int selectedIndex = choices.getSelectedIndex();
138
	      selectedIndex--;
138
	      selectedIndex--;
139
	      if(selectedIndex < 0)
139
	      if(selectedIndex < 0)
140
	      {
140
	      {
141
	        selectedIndex = choices.getItemCount() - 1 ;
141
	        selectedIndex = choices.getItemCount() - 1 ;
142
	      }
142
	      }
143
	      choices.setSelectedIndex(selectedIndex);
143
	      choices.setSelectedIndex(selectedIndex);
144
	           
144
	           
145
	      return;        
145
	      return;        
146
	    }
146
	    }
147
	  
147
	  
148
	  
148
	  
149
  }
149
  }
150
 
150
 
151
  /**
151
  /**
152
   * A key was released, start autocompletion
152
   * A key was released, start autocompletion
153
   */
153
   */
154
  public void onKeyUp(Widget arg0, char arg1, int arg2) {
154
  public void onKeyUp(Widget arg0, char arg1, int arg2) {
155
 
155
 
156
 
156
 
157
	 if(arg1 == KEY_DOWN)
157
	 if(arg1 == KEY_DOWN)
158
	 {
158
	 {
159
		 return;
159
		 return;
160
 
160
 
161
	 }
161
	 }
162
	 
162
	 
163
 
163
 
164
	 if(arg1 == KEY_UP)
164
	 if(arg1 == KEY_UP)
165
	 {
165
	 {
166
		 return;
166
		 return;
167
 
167
 
168
	 }
168
	 }
169
 
169
 
170
    if(arg1 == KEY_ENTER)
170
    if(arg1 == KEY_ENTER)
171
    {
171
    {
172
      if(visible)
172
      if(visible)
173
      {
173
      {
174
        complete();
174
        complete();
175
      }
175
      }
176
      else {
176
      else {
177
    	 // Validation de l'entree : 
177
    	 // Validation de l'entree : 
178
          if (autoCompleteAsyncTextBoxListeners!= null) {
178
          if (autoCompleteAsyncTextBoxListeners!= null) {
179
              autoCompleteAsyncTextBoxListeners.fireTextBoxEnter(this,current, this.getText());
179
              autoCompleteAsyncTextBoxListeners.fireTextBoxEnter(this,this.getText(),currentValue);
180
          }
180
          }
181
    	  current=-1;
181
    	  currentValue=null;
182
    	  this.setText("");
182
    	  this.setText("");
183
      }
183
      }
184
           
184
           
185
      return;
185
      return;
186
    }
186
    }
187
       
187
       
188
    if(arg1 == KEY_ESCAPE)
188
    if(arg1 == KEY_ESCAPE)
189
    {
189
    {
190
      choices.clear();
190
      choices.clear();
191
      items.clear();
191
      items.clear();
192
      choicesPopup.hide();
192
      choicesPopup.hide();
193
      visible = false;
193
      visible = false;
194
           
194
           
195
      return;
195
      return;
196
    }
196
    }
197
       
197
       
198
   
198
   
199
 // Lancement appel 
199
 // Lancement appel 
200
    String text = this.getText();
200
    String text = this.getText();
201
    
201
    
202
    if(text.length() > 0)
202
    if(text.length() > 0)
203
    {
203
    {
204
      items.clear();
204
      items.clear();
205
      
205
      
206
/*      if (getFromCache(text)!=null) {
206
/*      if (getFromCache(text)!=null) {
207
    	  items=getFromCache(text);
207
    	  items=getFromCache(text);
208
    	  displayList();
208
    	  displayList();
209
      }
209
      }
210
      else {*/
210
      else {*/
211
    	  this.doFetchURL(text);
211
    	  this.doFetchURL(text);
212
      //}
212
      //}
213
    }
213
    }
214
  }
214
  }
215
    
215
    
216
  
216
  
217
  // Display assistant
217
  // Display assistant
218
  
218
  
219
    public void displayList() { 
219
    public void displayList() { 
220
    	
220
    	
221
    	searching=false;
221
    	searching=false;
222
	    if(this.items.size() > 0)
222
	    if(this.items.size() > 0)
223
	    {
223
	    {
224
	    	
224
	    	
225
	      //addToCache(this.getText(),(Vector) items.clone());
225
	      //addToCache(this.getText(),(Vector) items.clone());
226
	      
226
	      
227
	      choices.clear();
227
	      choices.clear();
228
	           
228
	           
229
	      for(int i = 0; i < items.size(); i++)
229
	      for(int i = 0; i < items.size(); i++)
230
	      {
230
	      {
231
	        choices.addItem((String) items.get(i));
231
	        choices.addItem(((String [])items.get(i))[0],((String [])items.get(i))[1]);
232
	      }
232
	      }
233
	      
233
	      
234
	           
234
	           
235
	      // if there is only one match and it is what is in the
235
	      // if there is only one match and it is what is in the
236
	      // text field anyways there is no need to show autocompletion
236
	      // text field anyways there is no need to show autocompletion
237
	      if(items.size() == 1 && ((String) items.get(0)).compareTo(this.getText()) == 0)
237
	      if(items.size() == 1 && (((String []) items.get(0))[0]).compareTo(this.getText()) == 0)
238
	      {
238
	      {
239
	        choicesPopup.hide();
239
	        choicesPopup.hide();
240
	      } else {
240
	      } else {
241
	        choices.setSelectedIndex(0);
241
	        choices.setSelectedIndex(0);
242
	        choices.setVisibleItemCount(items.size());
242
	        choices.setVisibleItemCount(items.size());
243
	               
243
	               
244
	        if(!popupAdded)
244
	        if(!popupAdded)
245
	        {
245
	        {
246
	          RootPanel.get().add(choicesPopup);
246
	          RootPanel.get().add(choicesPopup);
247
	          popupAdded = true;
247
	          popupAdded = true;
248
	        }
248
	        }
249
	        choicesPopup.show();
249
	        choicesPopup.show();
250
	        visible = true;
250
	        visible = true;
251
	        choicesPopup.setPopupPosition(this.getAbsoluteLeft(),
251
	        choicesPopup.setPopupPosition(this.getAbsoluteLeft(),
252
	        this.getAbsoluteTop() + this.getOffsetHeight());
252
	        this.getAbsoluteTop() + this.getOffsetHeight());
253
	        //choicesPopup.setWidth(this.getOffsetWidth() + "px");
253
	        //choicesPopup.setWidth(this.getOffsetWidth() + "px");
254
	        choices.setWidth(this.getOffsetWidth() + "px");
254
	        choices.setWidth(this.getOffsetWidth() + "px");
255
	      }
255
	      }
256
	
256
	
257
	    } else {
257
	    } else {
258
	      visible = false;
258
	      visible = false;
259
	      choicesPopup.hide();
259
	      choicesPopup.hide();
260
	    }
260
	    }
261
	  }
261
	  }
262
 
262
 
263
  /**
263
  /**
264
   * A mouseclick in the list of items
264
   * A mouseclick in the list of items
265
   */
265
   */
266
  public void onChange(Widget arg0) {
266
  public void onChange(Widget arg0) {
267
    complete();
267
    complete();
268
  }
268
  }
269
 
269
 
270
  public void onClick(Widget arg0) {
270
  public void onClick(Widget arg0) {
271
    complete();
271
    complete();
272
  }
272
  }
273
   
273
   
274
  // add selected item to textbox
274
  // add selected item to textbox
275
  protected void complete()
275
  protected void complete()
276
  {
276
  {
277
    if(choices.getItemCount() > 0)
277
    if(choices.getItemCount() > 0)
278
    {
278
    {
279
      this.setText(choices.getItemText(choices.getSelectedIndex()));
279
      this.setText(choices.getItemText(choices.getSelectedIndex()));
280
      current=choices.getSelectedIndex();
280
      currentValue=choices.getValue(choices.getSelectedIndex());
281
    }
281
    }
282
 
282
 
283
    visible=false;
283
    visible=false;
284
    items.clear();
284
    items.clear();
285
    choices.clear();
285
    choices.clear();
286
    choicesPopup.hide();
286
    choicesPopup.hide();
287
  }
287
  }
288
  
288
  
289
  
289
  
290
  public void addItem(String item) {
290
  public void addItem(String item, String value) {
291
	  items.add(item);
291
	  items.add(new String [] {item, value});
292
  }
292
  }
293
  
293
  
294
  private void addToCache (String query, Vector result)
294
  private void addToCache (String query, Vector result)
295
  {
295
  {
296
	cache.put(query.toLowerCase(),result);
296
	cache.put(query.toLowerCase(),result);
297
  }
297
  }
298
 
298
 
299
  private Vector getFromCache (String query)
299
  private Vector getFromCache (String query)
300
  {
300
  {
301
	return (Vector) cache.get(query.toLowerCase());
301
	return (Vector) cache.get(query.toLowerCase());
302
  }
302
  }
303
  
303
  
304
 
304
 
305
 
305
 
306
  
306
  
307
}
307
}
308
 
308