Subversion Repositories eFlore/Archives.cel-v1

Rev

Rev 7 | Rev 9 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 7 Rev 8
Line 19... Line 19...
19
*/
19
*/
20
package org.tela_botanica.client;
20
package org.tela_botanica.client;
Line 21... Line 21...
21
 
21
 
22
 
22
 
23
// TODO : traiter latence (augmenter en fonction rapidité saisie + texte vide)
-
 
24
// TODO : traiter Tab (selection)
-
 
Line 25... Line 23...
25
// TODO : revoir traitement keyup, keydown
23
// TODO : traiter latence (augmenter en fonction rapidité saisie + texte vide)
26
// TODO : reactiver le cache (du design à revoir dans ce cas la) 
24
// TODO : traitement espace apres l'espece (%20)
27
 
-
 
28
import com.google.gwt.user.client.HTTPRequest;
25
 
29
import com.google.gwt.user.client.ResponseTextHandler;
26
import com.google.gwt.user.client.HTTPRequest;
30
import com.google.gwt.user.client.ui.ChangeListener;
27
import com.google.gwt.user.client.ResponseTextHandler;
31
import com.google.gwt.user.client.ui.KeyboardListener;
28
import com.google.gwt.user.client.ui.KeyboardListener;
32
import com.google.gwt.user.client.ui.ListBox;
29
import com.google.gwt.user.client.ui.ListBox;
33
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;
-
 
32
import com.google.gwt.user.client.ui.TextBox;
Line 34... Line 33...
34
import com.google.gwt.user.client.ui.RootPanel;
33
import com.google.gwt.user.client.ui.Widget;
35
import com.google.gwt.user.client.ui.TextBox;
34
import com.google.gwt.user.client.DOM;
Line 36... Line 35...
36
import com.google.gwt.user.client.ui.Widget;
35
import com.google.gwt.user.client.Event; 
37
 
36
 
Line 38... Line 37...
38
import java.util.Vector;
37
import java.util.Vector;
39
import java.util.HashMap;
38
import java.util.HashMap;
Line 40... Line 39...
40
 
39
 
41
 
40
 
42
 
41
 
Line 43... Line 42...
43
public class AutoCompleteAsyncTextBox extends TextBox
42
public class AutoCompleteAsyncTextBox extends TextBox
44
    implements KeyboardListener, ChangeListener, SourcesAutoCompleteAsyncTextBoxEvents {
43
    implements KeyboardListener, SourcesAutoCompleteAsyncTextBoxEvents {
-
 
44
   
-
 
45
  private String searchUrl = null; 
-
 
46
  private AutoCompleteAsyncTextBoxListenerCollection autoCompleteAsyncTextBoxListeners=null;  
-
 
47
    
-
 
48
  private HashMap cache = new HashMap();
-
 
49
  private boolean searching = false;
45
   
50
  private ResponseTextHandler responseTextHandler=null;
46
  private String searchUrl = null; 
51
		  
47
  private AutoCompleteAsyncTextBoxListenerCollection autoCompleteAsyncTextBoxListeners=null;  
52
  protected PopupPanel choicesPopup = new PopupPanel(true);
48
    
53
  protected ListBox choices = new ListBox() {
-
 
54
	  public void onBrowserEvent(Event event) {
Line 49... Line 55...
49
  private HashMap cache = new HashMap();
55
		  if (Event.ONCLICK == DOM.eventGetType(event)) {
50
  private boolean searching = false;
56
			  complete();
51
  private ResponseTextHandler responseTextHandler=null;
57
		  }
Line 65... Line 71...
65
  public AutoCompleteAsyncTextBox(ResponseTextHandler rsp)
71
  public AutoCompleteAsyncTextBox(ResponseTextHandler rsp)
66
  {
72
  {
67
    super();
73
    super();
68
    responseTextHandler=rsp;
74
    responseTextHandler=rsp;
69
    this.addKeyboardListener(this);
75
    this.addKeyboardListener(this);
70
    choices.addChangeListener(this);
76
    choices.sinkEvents(Event.ONCLICK); 
71
    this.setStyleName("AutoCompleteAsyncTextBox");
77
    this.setStyleName("AutoCompleteAsyncTextBox");
Line 72... Line 78...
72
       
78
       
73
    choicesPopup.add(choices);
79
    choicesPopup.add(choices);
Line 110... Line 116...
110
  
116
  
111
  /**
117
  /**
112
   * Not used at all
118
   * Not used at all
113
   */
119
   */
114
  public void onKeyDown(Widget arg0, char arg1, int arg2) {
-
 
115
  }
120
  public void onKeyDown(Widget arg0, char arg1, int arg2) {
116
 
-
 
117
  /**
-
 
118
   * Not used at all
-
 
119
   */
-
 
120
  public void onKeyPress(Widget arg0, char arg1, int arg2) {
121
	  
121
	 
122
	  
122
	    if(arg1 == KEY_DOWN)
123
	  if(arg1 == KEY_ENTER)
123
	    {
-
 
124
	      int selectedIndex = choices.getSelectedIndex();
-
 
125
	      selectedIndex++;
-
 
126
	      if(selectedIndex >= choices.getItemCount())
-
 
127
	      {
124
	    {
128
	        selectedIndex = 0;
-
 
129
	      }
-
 
130
	      choices.setSelectedIndex(selectedIndex);
-
 
131
	           
-
 
132
	      return;
125
	      enterKey(arg0, arg1, arg2);
133
	    }
-
 
134
	       
126
	    }
135
	    if(arg1 == KEY_UP)
127
	    else if(arg1 == KEY_DOWN)
136
	    {
-
 
137
	      int selectedIndex = choices.getSelectedIndex();
-
 
138
	      selectedIndex--;
128
	    {
139
	      if(selectedIndex < 0)
-
 
140
	      {
-
 
141
	        selectedIndex = choices.getItemCount() - 1 ;
-
 
142
	      }
-
 
143
	      choices.setSelectedIndex(selectedIndex);
-
 
144
	           
-
 
145
	      return;        
129
	      downKey(arg0, arg1, arg2);
-
 
130
	    }
-
 
131
	    else if(arg1 == KEY_UP)
-
 
132
	    {
-
 
133
	      upKey(arg0, arg1, arg2);
-
 
134
	    }
-
 
135
	    else if(arg1 == KEY_ESCAPE)
-
 
136
	    {
-
 
137
	      escapeKey(arg0, arg1, arg2);
-
 
138
	    }
Line 146... Line -...
146
	    }
-
 
147
	  
139
	  	
148
	  
-
 
149
  }
140
	  
150
 
141
  }	 
151
  /**
142
  /**
152
   * A key was released, start autocompletion
143
   * Not used at all (probleme avec ie, qui ne comprend pas les touches meta)
153
   */
-
 
154
  public void onKeyUp(Widget arg0, char arg1, int arg2) {
-
 
155
 
-
 
156
 
-
 
157
	 if(arg1 == KEY_DOWN)
-
 
158
	 {
-
 
159
		 return;
-
 
160
 
-
 
161
	 }
144
   */
162
	 
-
 
163
 
-
 
164
	 if(arg1 == KEY_UP)
-
 
Line 165... Line 145...
165
	 {
145
  public void onKeyPress(Widget arg0, char arg1, int arg2) {
-
 
146
	
-
 
147
 
-
 
148
  }
-
 
149
  
-
 
150
  // The down key was pressed.
-
 
151
  protected void downKey(Widget arg0, char arg1, int arg2) {
-
 
152
	  
-
 
153
	    int selectedIndex = choices.getSelectedIndex();
-
 
154
	    selectedIndex++;
-
 
155
	    if (selectedIndex >= choices.getItemCount())
-
 
156
	    {
-
 
157
	      selectedIndex = 0;
Line -... Line 158...
-
 
158
	    }
-
 
159
	    choices.setSelectedIndex(selectedIndex);
-
 
160
 }
-
 
161
 
166
		 return;
162
  // The up key was pressed.
167
 
163
  protected void upKey(Widget arg0, char arg1, int arg2) {
-
 
164
    int selectedIndex = choices.getSelectedIndex();
-
 
165
    selectedIndex--;
-
 
166
    if(selectedIndex < 0)
-
 
167
    {
-
 
168
      selectedIndex = choices.getItemCount() - 1;
-
 
169
    }
-
 
170
    choices.setSelectedIndex(selectedIndex);
168
	 }
171
  } 
169
 
172
 
170
    if(arg1 == KEY_ENTER)
173
  // The enter key was pressed.
171
    {
174
  protected void enterKey(Widget arg0, char arg1, int arg2) {
172
      if(visible)
175
      if(visible)
173
      {
176
      {
174
        complete();
177
        complete();
175
      }
178
      }
176
      else {
179
      else {
177
    	 // Validation de l'entree : 
180
    	 // Validation de l'entree : appel asynchrone  
178
          if (autoCompleteAsyncTextBoxListeners!= null) {
181
          if (autoCompleteAsyncTextBoxListeners!= null) {
179
              autoCompleteAsyncTextBoxListeners.fireTextBoxEnter(this,this.getText(),currentValue);
182
              autoCompleteAsyncTextBoxListeners.fireTextBoxEnter(this,this.getText(),currentValue);
180
          }
183
          }
181
    	  currentValue=null;
-
 
182
    	  this.setText("");
184
    	  currentValue=null;
183
      }
185
    	  this.setText("");
184
           
186
      }
185
      return;
-
 
-
 
187
 
186
    }
188
  }
187
       
189
 
188
    if(arg1 == KEY_ESCAPE)
190
//The escape key was pressed.
189
    {
191
  protected void escapeKey(Widget arg0, char arg1, int arg2) {
190
      choices.clear();
192
    choices.clear();
191
      items.clear();
-
 
192
      choicesPopup.hide();
193
    items.clear();
-
 
194
    choicesPopup.hide();
193
      visible = false;
195
    visible = false;
-
 
196
 
-
 
197
  } 
Line -... Line 198...
-
 
198
 
194
           
199
 
195
      return;
200
  // Any other non-special key was pressed.
Line -... Line 201...
-
 
201
  protected void otherKey(Widget arg0, char arg1, int arg2) {
196
    }
202
   
197
       
203
	
-
 
204
    // Lancement appel 
198
   
205
    String text = this.getText();
199
 // Lancement appel 
206
    
200
    String text = this.getText();
207
    	
201
    
208
	    if(text.length() > 0)
202
    if(text.length() > 0)
209
	    {
203
    {
210
	    
204
      items.clear();
211
		      items.clear();
205
      
212
		      
206
      if (getFromCache(text)!=null) {
213
		      if (getFromCache(text)!=null) {
207
    	  items=getFromCache(text);
214
		    	  items=getFromCache(text);
-
 
215
		    	  displayList();
-
 
216
		      }
-
 
217
		      else {
-
 
218
		     
-
 
219
		    	  this.doFetchURL(text);
-
 
220
		      }
-
 
221
		 }
-
 
222
   
-
 
223
	
-
 
224
	    
-
 
225
    
-
 
226
  } 
-
 
227
  
-
 
228
  public void onKeyUp(Widget arg0, char arg1, int arg2) {
-
 
229
	  
-
 
230
	  switch(arg1) {
-
 
231
      case KEY_ALT:
-
 
232
      case KEY_CTRL:
-
 
233
      case KEY_DOWN:
-
 
234
      case KEY_END:
-
 
235
      case KEY_ENTER:
-
 
236
      case KEY_ESCAPE:
-
 
237
      case KEY_HOME:
-
 
238
      case KEY_LEFT:
-
 
239
      case KEY_PAGEDOWN:
-
 
240
      case KEY_PAGEUP:
-
 
241
      case KEY_RIGHT:
-
 
242
      case KEY_SHIFT:
208
    	  displayList();
243
      case KEY_TAB:
-
 
244
      case KEY_UP:
209
      }
245
        break;
Line 210... Line 246...
210
      else {
246
      default:
Line 233... Line 269...
233
	      }
269
	      }
Line 234... Line 270...
234
	      
270
	      
235
	           
271
	           
236
	      // if there is only one match and it is what is in the
272
	      // if there is only one match and it is what is in the
237
	      // text field anyways there is no need to show autocompletion
273
	      // text field anyways there is no need to show autocompletion
238
	      if(items.size() == 1 && (((String []) items.get(0))[0]).compareTo(this.getText()) == 0)
274
	    //  if(items.size() == 1 && (((String []) items.get(0))[0]).compareTo(this.getText()) == 0)
239
	      {
275
	     // {
240
	        choicesPopup.hide();
276
	       // choicesPopup.hide();
241
	      } else {
277
	     // } else {
Line 242... Line 278...
242
	        choices.setSelectedIndex(0);
278
	        choices.setSelectedIndex(0);
243
	        choices.setVisibleItemCount(items.size());
279
	        choices.setVisibleItemCount(items.size());
Line 249... Line 285...
249
	        }
285
	        }
250
	        choicesPopup.show();
286
	        choicesPopup.show();
251
	        visible = true;
287
	        visible = true;
252
	        choicesPopup.setPopupPosition(this.getAbsoluteLeft(),
288
	        choicesPopup.setPopupPosition(this.getAbsoluteLeft(),
253
	        this.getAbsoluteTop() + this.getOffsetHeight());
289
	        this.getAbsoluteTop() + this.getOffsetHeight());
254
	        //choicesPopup.setWidth(this.getOffsetWidth() + "px");
290
	        choicesPopup.setWidth(this.getOffsetWidth() + "px");
255
	        choices.setWidth(this.getOffsetWidth() + "px");
291
	        choices.setWidth(this.getOffsetWidth() + "px");
256
	      }
292
	    //  }
Line 257... Line 293...
257
	
293
	
258
	    } else {
294
	    } else {
259
	      visible = false;
295
	      visible = false;
260
	      choicesPopup.hide();
296
	      choicesPopup.hide();
Line 265... Line 301...
265
   * A mouseclick in the list of items
301
   * A mouseclick in the list of items
266
   */
302
   */
267
  public void onChange(Widget arg0) {
303
  public void onChange(Widget arg0) {
268
    complete();
304
    complete();
269
  }
305
  }
-
 
306
  
Line 270... Line 307...
270
 
307
 
271
  public void onClick(Widget arg0) {
308
  public void onClick(Widget arg0) {
272
    complete();
309
    complete();