21,13 → 21,10 |
|
|
// TODO : traiter latence (augmenter en fonction rapidité saisie + texte vide) |
// TODO : traiter Tab (selection) |
// TODO : revoir traitement keyup, keydown |
// TODO : reactiver le cache (du design à revoir dans ce cas la) |
// TODO : traitement espace apres l'espece (%20) |
|
import com.google.gwt.user.client.HTTPRequest; |
import com.google.gwt.user.client.ResponseTextHandler; |
import com.google.gwt.user.client.ui.ChangeListener; |
import com.google.gwt.user.client.ui.KeyboardListener; |
import com.google.gwt.user.client.ui.ListBox; |
import com.google.gwt.user.client.ui.PopupPanel; |
34,6 → 31,8 |
import com.google.gwt.user.client.ui.RootPanel; |
import com.google.gwt.user.client.ui.TextBox; |
import com.google.gwt.user.client.ui.Widget; |
import com.google.gwt.user.client.DOM; |
import com.google.gwt.user.client.Event; |
|
import java.util.Vector; |
import java.util.HashMap; |
41,7 → 40,7 |
|
|
public class AutoCompleteAsyncTextBox extends TextBox |
implements KeyboardListener, ChangeListener, SourcesAutoCompleteAsyncTextBoxEvents { |
implements KeyboardListener, SourcesAutoCompleteAsyncTextBoxEvents { |
|
private String searchUrl = null; |
private AutoCompleteAsyncTextBoxListenerCollection autoCompleteAsyncTextBoxListeners=null; |
51,11 → 50,18 |
private ResponseTextHandler responseTextHandler=null; |
|
protected PopupPanel choicesPopup = new PopupPanel(true); |
protected ListBox choices = new ListBox(); |
protected ListBox choices = new ListBox() { |
public void onBrowserEvent(Event event) { |
if (Event.ONCLICK == DOM.eventGetType(event)) { |
complete(); |
} |
} |
}; |
protected Vector items = new Vector(); |
protected boolean popupAdded = false; |
protected boolean visible = false; |
protected String currentValue = null; |
|
|
|
/** |
67,7 → 73,7 |
super(); |
responseTextHandler=rsp; |
this.addKeyboardListener(this); |
choices.addChangeListener(this); |
choices.sinkEvents(Event.ONCLICK); |
this.setStyleName("AutoCompleteAsyncTextBox"); |
|
choicesPopup.add(choices); |
112,69 → 118,66 |
* Not used at all |
*/ |
public void onKeyDown(Widget arg0, char arg1, int arg2) { |
} |
|
/** |
* Not used at all |
*/ |
public void onKeyPress(Widget arg0, char arg1, int arg2) { |
|
if(arg1 == KEY_DOWN) |
|
|
if(arg1 == KEY_ENTER) |
{ |
int selectedIndex = choices.getSelectedIndex(); |
selectedIndex++; |
if(selectedIndex >= choices.getItemCount()) |
{ |
selectedIndex = 0; |
} |
choices.setSelectedIndex(selectedIndex); |
|
return; |
enterKey(arg0, arg1, arg2); |
} |
|
if(arg1 == KEY_UP) |
else if(arg1 == KEY_DOWN) |
{ |
int selectedIndex = choices.getSelectedIndex(); |
selectedIndex--; |
if(selectedIndex < 0) |
{ |
selectedIndex = choices.getItemCount() - 1 ; |
} |
choices.setSelectedIndex(selectedIndex); |
|
return; |
downKey(arg0, arg1, arg2); |
} |
else if(arg1 == KEY_UP) |
{ |
upKey(arg0, arg1, arg2); |
} |
else if(arg1 == KEY_ESCAPE) |
{ |
escapeKey(arg0, arg1, arg2); |
} |
|
|
|
} |
|
} |
/** |
* A key was released, start autocompletion |
* Not used at all (probleme avec ie, qui ne comprend pas les touches meta) |
*/ |
public void onKeyUp(Widget arg0, char arg1, int arg2) { |
public void onKeyPress(Widget arg0, char arg1, int arg2) { |
|
|
} |
|
// The down key was pressed. |
protected void downKey(Widget arg0, char arg1, int arg2) { |
|
int selectedIndex = choices.getSelectedIndex(); |
selectedIndex++; |
if (selectedIndex >= choices.getItemCount()) |
{ |
selectedIndex = 0; |
} |
choices.setSelectedIndex(selectedIndex); |
} |
|
if(arg1 == KEY_DOWN) |
{ |
return; |
// The up key was pressed. |
protected void upKey(Widget arg0, char arg1, int arg2) { |
int selectedIndex = choices.getSelectedIndex(); |
selectedIndex--; |
if(selectedIndex < 0) |
{ |
selectedIndex = choices.getItemCount() - 1; |
} |
choices.setSelectedIndex(selectedIndex); |
} |
|
} |
|
|
if(arg1 == KEY_UP) |
{ |
return; |
|
} |
|
if(arg1 == KEY_ENTER) |
{ |
// The enter key was pressed. |
protected void enterKey(Widget arg0, char arg1, int arg2) { |
if(visible) |
{ |
complete(); |
} |
else { |
// Validation de l'entree : |
// Validation de l'entree : appel asynchrone |
if (autoCompleteAsyncTextBoxListeners!= null) { |
autoCompleteAsyncTextBoxListeners.fireTextBoxEnter(this,this.getText(),currentValue); |
} |
181,37 → 184,70 |
currentValue=null; |
this.setText(""); |
} |
|
return; |
} |
|
if(arg1 == KEY_ESCAPE) |
{ |
choices.clear(); |
items.clear(); |
choicesPopup.hide(); |
visible = false; |
|
return; |
} |
|
|
} |
|
//The escape key was pressed. |
protected void escapeKey(Widget arg0, char arg1, int arg2) { |
choices.clear(); |
items.clear(); |
choicesPopup.hide(); |
visible = false; |
|
} |
|
|
// Any other non-special key was pressed. |
protected void otherKey(Widget arg0, char arg1, int arg2) { |
|
// Lancement appel |
|
// Lancement appel |
String text = this.getText(); |
|
if(text.length() > 0) |
{ |
items.clear(); |
|
if (getFromCache(text)!=null) { |
items=getFromCache(text); |
displayList(); |
} |
else { |
|
this.doFetchURL(text); |
} |
|
if(text.length() > 0) |
{ |
|
items.clear(); |
|
if (getFromCache(text)!=null) { |
items=getFromCache(text); |
displayList(); |
} |
else { |
|
this.doFetchURL(text); |
} |
} |
|
|
|
|
} |
|
public void onKeyUp(Widget arg0, char arg1, int arg2) { |
|
switch(arg1) { |
case KEY_ALT: |
case KEY_CTRL: |
case KEY_DOWN: |
case KEY_END: |
case KEY_ENTER: |
case KEY_ESCAPE: |
case KEY_HOME: |
case KEY_LEFT: |
case KEY_PAGEDOWN: |
case KEY_PAGEUP: |
case KEY_RIGHT: |
case KEY_SHIFT: |
case KEY_TAB: |
case KEY_UP: |
break; |
default: |
otherKey(arg0, arg1, arg2); |
break; |
} |
|
} |
|
|
235,10 → 271,10 |
|
// if there is only one match and it is what is in the |
// text field anyways there is no need to show autocompletion |
if(items.size() == 1 && (((String []) items.get(0))[0]).compareTo(this.getText()) == 0) |
{ |
choicesPopup.hide(); |
} else { |
// if(items.size() == 1 && (((String []) items.get(0))[0]).compareTo(this.getText()) == 0) |
// { |
// choicesPopup.hide(); |
// } else { |
choices.setSelectedIndex(0); |
choices.setVisibleItemCount(items.size()); |
|
251,9 → 287,9 |
visible = true; |
choicesPopup.setPopupPosition(this.getAbsoluteLeft(), |
this.getAbsoluteTop() + this.getOffsetHeight()); |
//choicesPopup.setWidth(this.getOffsetWidth() + "px"); |
choicesPopup.setWidth(this.getOffsetWidth() + "px"); |
choices.setWidth(this.getOffsetWidth() + "px"); |
} |
// } |
|
} else { |
visible = false; |
267,6 → 303,7 |
public void onChange(Widget arg0) { |
complete(); |
} |
|
|
public void onClick(Widget arg0) { |
complete(); |