Rev 2033 | Blame | Compare with Previous | Last modification | View Log | RSS feed
package org.tela_botanica.client.util;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.tela_botanica.client.interfaces.Rafraichissable;
import org.tela_botanica.client.modeles.dao.ListeReferentielChampsEtendusDAO;
import org.tela_botanica.client.modeles.objets.Configuration;
import com.google.gwt.user.client.Timer;
import com.gwtext.client.core.EventCallback;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.core.Ext;
import com.gwtext.client.core.ListenerConfig;
import com.gwtext.client.data.ArrayReader;
import com.gwtext.client.data.FieldDef;
import com.gwtext.client.data.MemoryProxy;
import com.gwtext.client.data.Record;
import com.gwtext.client.data.RecordDef;
import com.gwtext.client.data.Store;
import com.gwtext.client.data.StringFieldDef;
import com.gwtext.client.widgets.Component;
import com.gwtext.client.widgets.event.ComponentListenerAdapter;
import com.gwtext.client.widgets.form.ComboBox;
import com.gwtext.client.widgets.form.Field;
import com.gwtext.client.widgets.form.event.ComboBoxListenerAdapter;
public class ChampSaisieEtendu extends ComboBox implements Rafraichissable {
// TODO: faire un enum
private final int KEY_ALT = 18;
private final int KEY_BACKSPACE = 8;
private final int KEY_CTRL = 17;
private final int KEY_DELETE = 46;
private final int KEY_DOWN = 40;
private final int KEY_END = 35;
private final int KEY_ENTER = 13;
private final int KEY_ESCAPE = 27;
private final int KEY_HOME = 36;
private final int KEY_LEFT = 37;
private final int KEY_PAGEDOWN = 34;
private final int KEY_PAGEUP = 33;
private final int KEY_RIGHT = 39;
private final int KEY_SHIFT = 16;
private final int KEY_TAB = 9;
private final int KEY_UP = 38;
private boolean selectionValeur = false;
private boolean estModifie = false;
private String cle = null;
private String label = null;
private Timer timer = null;
final String resultTplRefPerso = "<div class=\"search-item-ref\">{valeur}</div>";
private List<String> cacheValeurs;
private Record rdSelectionne = null;
private String valeurBrute = "";
public ChampSaisieEtendu(String label, String cle) {
// Accesskey pour debugging
super(label,cle);
this.cle =cle;
this.label = label;
setTpl(resultTplRefPerso);
setMode(ComboBox.REMOTE);
setItemSelector("div.search-item-ref");
setTypeAhead(false);
setLoadingText("Recherche...");
setHideTrigger(true);
setValidateOnBlur(false);
ListenerConfig listenerConfigAutocompletion=new ListenerConfig();
listenerConfigAutocompletion.setDelay(200);
listenerConfigAutocompletion.setStopPropagation(false);
listenerConfigAutocompletion.setStopEvent(false);
initStoreVide();
if(Configuration.saisieChampsEtendusActivee()) {
addKeyPressListener(new EventCallback() {
@Override
public void execute(EventObject e) {
switch(e.getKey()) {
case KEY_ALT:
case KEY_CTRL:
case KEY_END:
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;
case KEY_ENTER:
if(rdSelectionne != null) {
setValue(rdSelectionne.getAsString("valeur"));
selectionValeur=true;
valeurBrute = rdSelectionne.getAsString("valeur");
}
break;
case KEY_DOWN:
if(getValueAsString().isEmpty() && !isExpanded()) {
obtenirListeValeurs("*");
}
break;
default:
if(timer != null) {
timer.cancel();
}
timer = new Timer() {
@Override
public void run() {
obtenirListeValeurs();
}
};
timer.schedule(300);
valeurBrute = getValueAsString();
setValue(valeurBrute);
}
}
},listenerConfigAutocompletion);
}
addListener(new ComboBoxListenerAdapter() {
@Override
public void onSelect(ComboBox comboBox, Record record, int index) {
rdSelectionne = record;
setRawValue(rdSelectionne.getAsString("valeur"));
}
@Override
public void onBlur(Field field) {
if(rdSelectionne != null && !rdSelectionne.getAsString("valeur").equals(valeurBrute)) {
setRawValue(valeurBrute);
}
}
});
this.addListener(new ComponentListenerAdapter() {
@Override
public void onRender(Component component) {
setLargeurChamp();
}
});
}
@Override
public void rafraichir(Object nouvelleDonnees, boolean repandreRaffraichissement) {
@SuppressWarnings("unchecked")
ArrayList<String> valeurs = (ArrayList<String>)nouvelleDonnees;
cacheValeurs = valeurs;
int i = 0;
Object[][] refData = new Object[valeurs.size()][1];
for (Iterator<String> it = valeurs.iterator(); it.hasNext();)
{
refData[i][0]= it.next();
i++;
}
FieldDef defValeur = new StringFieldDef("valeur");
FieldDef[] defTab = {defValeur};
RecordDef rd = new RecordDef(defTab);
final MemoryProxy dataProxy = new MemoryProxy(refData);
final ArrayReader reader = new ArrayReader(rd);
Store store = new Store(dataProxy,reader);
setStore(store);
store.load();
}
private void obtenirListeValeurs() {
String valeurChamp = getRawValue();
obtenirListeValeurs(valeurChamp);
}
private void obtenirListeValeurs(String valeurChamp) {
if(valeurChamp == null) {
valeurChamp = "";
} else {
valeurChamp += "*";
}
ListeReferentielChampsEtendusDAO lrceDao = new ListeReferentielChampsEtendusDAO(null);
lrceDao.obtenirListeValeursChampEtendu(this, cle, valeurChamp);
}
public void setLargeurChamp() {
// Correction un peu moche pour améliorer l'affichage du champ
String idElementEnfant = Ext.get(("x-form-el-"+cle)).getFirstChild().getId();
Ext.get(idElementEnfant).setWidth("90%", false);
Ext.get(cle).setWidth("100%", false);
}
private void initStoreVide() {
Object[][] refData = new Object[0][1];
FieldDef defValeur = new StringFieldDef("valeur");
FieldDef[] defTab = {defValeur};
RecordDef rd = new RecordDef(defTab);
final MemoryProxy dataProxy = new MemoryProxy(refData);
final ArrayReader reader = new ArrayReader(rd);
Store store = new Store(dataProxy,reader);
setStore(store);
store.load();
}
@Override
public void setValue(String valeur) {
valeurBrute = valeur;
super.setValue(valeur);
}
public String getCle() {
return cle;
}
public String getLabel() {
return label;
}
}