Subversion Repositories eFlore/Applications.cel

Compare Revisions

Ignore whitespace Rev 1939 → Rev 1940

/trunk/src/org/tela_botanica/client/util/FormulaireSaisieChampEtendu.java
New file
0,0 → 1,237
package org.tela_botanica.client.util;
 
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
 
import org.tela_botanica.client.interfaces.Rafraichissable;
import org.tela_botanica.client.modeles.dao.ListeReferentielChampsEtendusDAO;
 
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.gwtext.client.core.EventCallback;
import com.gwtext.client.core.EventObject;
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.Button;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.form.ComboBox;
import com.gwtext.client.widgets.form.FormPanel;
import com.gwtext.client.widgets.form.TextField;
import com.gwtext.client.widgets.form.event.ComboBoxListenerAdapter;
 
@SuppressWarnings("unchecked")
public abstract class FormulaireSaisieChampEtendu extends FormPanel implements Rafraichissable {
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;
final ComboBox nChamp;
String resultTplRefPerso = "<div class=\"search-item-ref\">{label}</div>";
final Button ajouterChampsEtendu;
final Button annulerAjouterChampEtendu;
String idChamp = null;
private Timer timer = null;
private Map<String, String> cacheClesValeur;
public FormulaireSaisieChampEtendu() {
super();
setPaddings(3);
setBodyBorder(false);
HorizontalPanel hp = new HorizontalPanel();
hp.setBorderWidth(0);
nChamp = new ComboBox("Nom du champ", "nom_champ_etendu");
add(nChamp);
ajouterChampsEtendu = new Button("Ajouter");
annulerAjouterChampEtendu = new Button("Annuler");
hp.add(ajouterChampsEtendu);
hp.add(annulerAjouterChampEtendu);
add(hp);
nChamp.setTpl(resultTplRefPerso);
nChamp.setMode(ComboBox.REMOTE);
nChamp.setItemSelector("div.search-item-ref");
nChamp.setTypeAhead(true);
nChamp.setLoadingText("Recherche...");
nChamp.setHideTrigger(true);
nChamp.setWidth("250px");
nChamp.setValue("");
nChamp.focus();
ajouterListeners();
}
 
private void ajouterListeners() {
ListenerConfig listenerConfigAutocompletion=new ListenerConfig();
listenerConfigAutocompletion.setDelay(200);
listenerConfigAutocompletion.setStopPropagation(false);
listenerConfigAutocompletion.setStopEvent(true);
nChamp.addKeyPressListener(new EventCallback() {
 
@Override
public void execute(EventObject e) {
 
switch(e.getKey()) {
case KEY_ALT:
case KEY_CTRL:
case KEY_ENTER:
case KEY_END:
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_DOWN:
if(nChamp.getValueAsString().isEmpty() && !nChamp.isExpanded()) {
obtenirReferentiel("*");
}
break;
case KEY_ESCAPE:
surAnnulation();
break;
default:
if(timer != null) {
timer.cancel();
}
timer = new Timer() {
@Override
public void run() {
obtenirReferentiel();
}
};
timer.schedule(300);
}
}
},listenerConfigAutocompletion);
// Listener completion
nChamp.addListener(new ComboBoxListenerAdapter() {
@Override
public void onSelect(ComboBox comboBox, Record record, int index) {
nChamp.setValue(record.getAsString("label"));
idChamp = record.getAsString("cle");
nChamp.collapse();
}
});
ajouterChampsEtendu.addListener(new ButtonListenerAdapter() {
@Override
public void onClick(Button button, EventObject e) {
validerChampEtendu();
}
});
annulerAjouterChampEtendu.addListener(new ButtonListenerAdapter() {
@Override
public void onClick(Button button, EventObject e) {
surAnnulation();
}
});
}
private void validerChampEtendu() {
String valeurChamp = nChamp.getValueAsString();
if(valeurChamp != null && !valeurChamp.isEmpty()) {
String idNouveauChamp = "";
if(!estUnChampSelectionne(valeurChamp)) {
Date date = new Date();
// affectation d'un id temporaire qui sera remplacé par l'id auto généré à partir
// du label
idNouveauChamp = "tempid_"+date.getTime();
} else {
idNouveauChamp = idChamp;
}
ChampSaisieEtendu retour = new ChampSaisieEtendu(valeurChamp, idNouveauChamp);
retour.setId(idNouveauChamp);
surValidation(retour);
} else {
Window.alert("Le nom du champ étendu ne peut pas être vide");
}
}
private boolean estUnChampSelectionne(String valeur) {
return cacheClesValeur.containsValue(valeur);
}
private void obtenirReferentiel() {
obtenirReferentiel(nChamp.getValueAsString());
}
private void obtenirReferentiel(String valeur) {
ListeReferentielChampsEtendusDAO lrce = new ListeReferentielChampsEtendusDAO(null);
lrce.obtenirListeNomsChampsEtendus(this, valeur+"*");
}
public abstract void surValidation(ChampSaisieEtendu champ);
public abstract void surAnnulation();
public void rafraichir(Object nouvelleDonnees, boolean repandreRaffraichissement) {
int i = 0;
HashMap<String, String> clesLabels = (HashMap<String, String>)nouvelleDonnees;
cacheClesValeur = clesLabels;
Object[][] refData = new Object[clesLabels.keySet().size()][2];
for (Iterator<String> it = clesLabels.keySet().iterator(); it.hasNext();)
{
String cle = it.next();
String label= clesLabels.get(cle);
refData[i][0]= cle;
refData[i][1]= label;
i++;
}
FieldDef defCle = new StringFieldDef("cle");
FieldDef defLabel = new StringFieldDef("label");
FieldDef[] defTab = {defCle, defLabel};
RecordDef rd = new RecordDef(defTab);
final MemoryProxy dataProxy = new MemoryProxy(refData);
final ArrayReader reader = new ArrayReader(rd);
Store store=new Store(dataProxy,reader);
nChamp.setStore(store);
store.load();
}
}