Subversion Repositories eFlore/Applications.cel

Compare Revisions

Ignore whitespace Rev 1546 → Rev 1547

/branches/v1.6-croc/src/org/tela_botanica/client/util/AutoCompletionRefComboBox.java
New file
0,0 → 1,170
package org.tela_botanica.client.util;
 
import java.util.Iterator;
 
import org.tela_botanica.client.interfaces.Rafraichissable;
import org.tela_botanica.client.modeles.objets.ListeReferentielPerso;
import org.tela_botanica.client.modeles.objets.ListeReferentielPerso.TypesReferentiels;
import org.tela_botanica.client.observation.ObservationMediateur;
 
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.form.ComboBox;
import com.gwtext.client.widgets.form.event.ComboBoxListenerAdapter;
 
public abstract class AutoCompletionRefComboBox 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 ObservationMediateur oMediateur = null;
private TypesReferentiels typeRef = null;
private boolean selectionValeur = false;
private boolean estModifie = false;
final String resultTplRefPerso = "<div class=\"search-item-ref\">{element_referentiel}</div>";
public AutoCompletionRefComboBox(String label, String nom, ObservationMediateur oMediateur, TypesReferentiels typeRef) {
// Accesskey pour debugging
super(label,nom);
this.oMediateur = oMediateur;
this.typeRef = typeRef;
 
setTpl(resultTplRefPerso);
setMode(ComboBox.REMOTE);
setItemSelector("div.search-item-ref");
setTypeAhead(true);
setLoadingText("Recherche...");
setHideTrigger(true);
ListenerConfig listenerConfigAutocompletion=new ListenerConfig();
listenerConfigAutocompletion.setDelay(200);
listenerConfigAutocompletion.setStopPropagation(false);
listenerConfigAutocompletion.setStopEvent(false);
addKeyPressListener(new EventCallback() {
 
@Override
public void execute(EventObject e) {
 
switch(e.getKey()) {
case KEY_ALT:
case KEY_CTRL:
case KEY_DOWN:
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 (selectionValeur) {
estModifie= true;
selectionValeur=false;
onModificationValeur();
}
else {
onValidationSaisie();
}
break;
default:
estModifie = true;
onModificationValeur();
obtenirReferentiel();
}
}
},listenerConfigAutocompletion);
// Listener completion
addListener(new ComboBoxListenerAdapter() {
@Override
public void onSelect(ComboBox comboBox, Record record, int index) {
setValue(record.getAsString("element_referentiel"));
selectionValeur=true;
collapse();
}
});
}
 
@Override
public void rafraichir(Object nouvelleDonnees,
boolean repandreRaffraichissement) {
ListeReferentielPerso referentielPerso = (ListeReferentielPerso)nouvelleDonnees;
int i = 0;
Object[][] refData = new Object[referentielPerso.size()][1];
for (Iterator it = referentielPerso.keySet().iterator(); it.hasNext();)
{
String ref= referentielPerso.get(it.next());
refData[i][0]= ref;
i++;
}
FieldDef defStation = new StringFieldDef("element_referentiel");
FieldDef[] defTab = {defStation};
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 obtenirReferentiel() {
String valeurChamp = getValue();
if(valeurChamp == null) {
valeurChamp = "";
}
oMediateur.obtenirListeReferentielPerso(this, typeRef, valeurChamp);
}
public abstract void onValidationSaisie();
public abstract void onModificationValeur();
 
}
/branches/v1.6-croc/src/org/tela_botanica/client/util/Util.java
New file
0,0 → 1,257
package org.tela_botanica.client.util;
 
import java.util.HashMap;
import java.util.Map;
 
import org.tela_botanica.client.modeles.objets.ListeObservation;
import org.tela_botanica.client.modeles.objets.Observation;
 
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONString;
 
public class Util {
 
public Util() {
}
public static String getValeurJsonOuVide(JSONObject jo, String index) {
return jsonNonNull(jo, index) ? ((JSONString)jo.get(index)).stringValue() : "";
}
public static Map<String, String> getMapValeursOuVide(JSONArray jo, String index) {
Map<String, String> mapValeurs = new HashMap<String, String>();
final int taillemax = jo.size();
JSONObject temp = null;
for (int i = 0; i < taillemax; i++) {
if ((temp = jo.get(i).isObject()) != null) {
mapValeurs.put(temp.get("cle").isString().stringValue(), temp.get("valeur").isString().stringValue());
}
}
return mapValeurs;
}
public static boolean jsonNonNull(JSONObject jo, String index) {
return (jo != null &&
jo.get(index) != null &&
jo.get(index).isNull() == null
);
}
public static String formaterLieu(Observation obs, String modeleLieu) {
String lieuModele = modeleLieu;
String commune = obs.getLocalite();
String lieuDit = obs.getLieudit();
String station = obs.getStation();
String lieuCommuneFormate = "";
String lieuDitFormate = "";
String stationFormatee = "";
if(commune != null && !commune.contains("000null") && !commune.trim().equals("")) {
String idLoc =obs.getIdentifiantLocalite().replaceAll(" ","/");
if(idLoc != null && !idLoc.contains("000null") && !idLoc.trim().equals("")) {
 
idLoc = idLoc.replaceAll("%","");
idLoc = idLoc.replaceAll("\"","");
idLoc = idLoc.replace('\\',' ');
idLoc = idLoc.trim();
if(idLoc.length() > 2) {
idLoc = idLoc.substring(0,2);
}
lieuCommuneFormate += idLoc+" - ";
}
lieuCommuneFormate += commune;
lieuModele = lieuModele.replaceAll("IDLOCCOMMUNE", lieuCommuneFormate);
} else {
lieuModele = lieuModele.replaceAll("IDLOCCOMMUNE,", lieuCommuneFormate);
}
if(lieuDit != null && !lieuDit.contains("000null") && !lieuDit.trim().equals("")) {
lieuDitFormate += lieuDit;
lieuModele = lieuModele.replaceAll("LIEUDIT", lieuDitFormate);
} else {
lieuModele = lieuModele.replaceAll("LIEUDIT,", lieuDitFormate);
}
if(station != null && !station.contains("000null") && !station.trim().equals("")) {
stationFormatee += station;
lieuModele = lieuModele.replaceAll("STATION", stationFormatee);
} else {
lieuModele = lieuModele.replaceAll("STATION", stationFormatee);
}
lieuModele = lieuModele.trim();
lieuModele = lieuModele.replaceAll(",$","");
lieuModele = lieuModele.replaceAll(",^$",", ");
return lieuModele;
}
public static String obtenirDepartementAPartirChaineCommune(String departement, String commune) {
String dep = "";
if(departement == null) {
departement = "";
}
if(departement.equals("000null") || departement.equals("")) {
String[] depCom = commune.split(" ");
if(depCom.length > 1) {
dep = depCom[1].replace('(', ' ');
} else {
dep = "";
}
} else {
dep = departement;
}
dep = dep.replace(')', ' ');
dep = dep.trim();
dep = dep.replace('\\',' ');
dep = dep.trim();
try
{
int nDep = Integer.parseInt(dep);
if(nDep > 0 && nDep < 110) {
departement = dep ;
}
if(departement.length() == 4) {
departement = "0"+departement;
}
departement = departement.substring(0,2);
}
catch(NumberFormatException e)
{
departement = "" ;
}
return departement;
}
public static String supprimerNumDepartementChaineLocalite(String chaineLocaliteComplete) {
return chaineLocaliteComplete.replaceAll(" \\([0-9]*\\)", "");
}
public static String convertirChaineZoneGeoVersDepartement(String chaineZoneGeo) {
return (!chaineZoneGeo.equals("000null") && !chaineZoneGeo.equals("")) ? chaineZoneGeo.replaceAll("INSEE-C:", "").substring(0, 2): chaineZoneGeo;
}
public static String convertirChaineZoneGeoVersCodeInsee(String chaineZoneGeo) {
return (!chaineZoneGeo.equals("000null") && !chaineZoneGeo.equals("")) ? chaineZoneGeo.replaceAll("INSEE-C:", ""): chaineZoneGeo;
}
/***
* Fusionne les éléments d'un tableau en une chaîne
* @param delim : la chaîne de séparation
* @param args : la tableau
* @return la chaîne fusionnée
*/
public static String implode(String delim, String[] args){
StringBuffer sb = new StringBuffer();
int lgArgs = args.length;
for(int i = 0; i < lgArgs; i++){
if (i > 0) {
sb.append(delim);
}
sb.append(args[i]);
}
return sb.toString();
}
public static boolean filtreValide(String[] filtre) {
return (filtre.length == 2 &&
filtre[0] != null &&
!filtre[0].equals("") &&
filtre[1] != null &&
!filtre[1].equals(""));
}
public static String renvoyerMois(int numMois) {
switch (numMois) {
case 1:
return "janvier" ;
case 2:
return "fevrier" ;
case 3:
return "mars" ;
case 4:
return "avril" ;
case 5:
return "mai" ;
case 6:
return "juin" ;
case 7:
return "juillet" ;
case 8:
return "août" ;
case 9:
return "septembre" ;
case 10:
return "octobre" ;
case 11:
return "novembre" ;
case 12:
return "décembre" ;
default:
return "Inconnu" ;
}
}
public static String remplacerSautsDeligneMalEncodes(String chaineAvecSautsDeLignesMalEncodes) {
String chaineAvecSautsDeLignesBienEncodes = chaineAvecSautsDeLignesMalEncodes.replace('\\','%');
chaineAvecSautsDeLignesBienEncodes = chaineAvecSautsDeLignesBienEncodes.replaceAll("%n","%");
chaineAvecSautsDeLignesBienEncodes = chaineAvecSautsDeLignesBienEncodes.replace('%','\n');
return chaineAvecSautsDeLignesBienEncodes;
}
public static boolean verifierDateFormatCel(String dateAVerifier) {
String dateRemplacee = remplacerSeparateursDateFormatCel(dateAVerifier);
String[] tabDate = dateRemplacee.split("/");
boolean retour = false;
if(tabDate.length == 3) {
//TODO: faire un parsing de date qui fonctionne mieux car
// on peut saisir un 31 novembre par exemple
// mais l'api date de java est mal gérée par gwt
try {
int jour = Integer.parseInt(tabDate[0]);
int mois = Integer.parseInt(tabDate[1]);
int annee = Integer.parseInt(tabDate[2]);
if(jour <= 31 && mois <= 12 && tabDate[2].length() == 4) {
retour = true;
}
} catch (Exception e) {
 
}
}
return retour;
}
public static String remplacerSeparateursDateFormatCel(String date) {
String dateRemplacee = date.replaceAll("-", "/");
return dateRemplacee;
}
}