Subversion Repositories eFlore/Applications.coel

Rev

Rev 819 | Rev 928 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package org.tela_botanica.client.vues;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import org.tela_botanica.client.ComposantClass;
import org.tela_botanica.client.Mediateur;
import org.tela_botanica.client.RegistreId;
import org.tela_botanica.client.configuration.Configuration;
import org.tela_botanica.client.i18n.Constantes;
import org.tela_botanica.client.interfaces.Rafraichissable;
import org.tela_botanica.client.modeles.Projet;
import org.tela_botanica.client.modeles.ProjetListe;
import org.tela_botanica.client.modeles.Valeur;
import org.tela_botanica.client.modeles.ValeurListe;
import org.tela_botanica.client.util.UtilString;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.util.Format;
import com.extjs.gxt.ui.client.util.Params;
import com.extjs.gxt.ui.client.widget.HtmlContainer;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.TabItem;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.google.gwt.core.client.GWT;

public abstract class DetailVue extends LayoutContainer implements Rafraichissable {

        protected Mediateur mediateur = null;
        protected Constantes i18nC = null;

        protected HashMap<String, Valeur> ontologie = null;
        protected boolean ontologieChargementOk = false;
        private HashMap<Integer, String> ontologiesEnAttenteDeReception = null;
        
        protected ProjetListe projets = null;
        protected boolean projetsChargementOk = false;
        
        protected String sautLigneTpl = null;
        
        public DetailVue(Mediateur mediateurCourant) {
                mediateur = mediateurCourant;
                i18nC = Mediateur.i18nC;
                
                initialiserSautLigneTpl();
                
                ontologie = new HashMap<String, Valeur>();
                ontologieChargementOk = false;
                ontologiesEnAttenteDeReception = new HashMap<Integer, String>();
                chargerProjets();
                
                setLayout(new FitLayout());
                setBorders(false);
                setScrollMode(Scroll.AUTO);
        }
        
        private void initialiserSautLigneTpl() {
                sautLigneTpl = "<br />\n";
        }
        
        private void chargerProjets() {
                mediateur.selectionnerProjets(this);
        }
                
        protected String construireTxtProjet(String idProjet) {
                String chaineARetourner = idProjet;
                
                if (projets != null) {
                        Projet projet = projets.get(idProjet);
                        if (projet != null) {
                                String nomDuProjet = projet.getNom();
                                if  (!nomDuProjet.equals("")) {
                                        chaineARetourner = nomDuProjet;
                                }
                        }
                }
                
                return chaineARetourner;
        }
                
        protected String construireTxtTruck(String chaineAAnalyser) {
                ArrayList<String> termes = new ArrayList<String>();
                
                if ((chaineAAnalyser != null) && (!chaineAAnalyser.trim().equals("")))  {
                        String[] valeurs = chaineAAnalyser.split(";;");
                        int nbreValeurs = valeurs.length;
                        if (nbreValeurs > 0)    {
                                for (int i = 0; i < nbreValeurs; i++)   {
                                        String valeur = valeurs[i];
                                        String valeurFormatee = formaterValeurTruck(valeur);
                                        termes.add(valeurFormatee);
                                }
                        }
                }
                
                String chaineARetourner = formaterTableauDeTxt(termes);
                return chaineARetourner;
        }
        
        private String formaterValeurTruck(String valeur) {
                String chaineARetourner = "";
                
                if (valeur.matches("^[^#]+##[^$]+$"))   {
                        String[] cleValeur = valeur.split("##");
                        chaineARetourner = cleValeur[1]+" "+formaterParenthese(cleValeur[0]);
                } else if (!valeur.equals(""))  {
                        chaineARetourner = valeur;
                } else {
                        GWT.log("Valeur truck posant problèlme :"+valeur, null);
                }
                
                return chaineARetourner;
        }
        
        protected String formaterParenthese(String chaineAAfficher) {
                if (!chaineAAfficher.equals("")) {
                        chaineAAfficher = "("+chaineAAfficher+")";
                }
                return chaineAAfficher;
        }
        
        protected String formaterTableauDeTxt(ArrayList<String> tableauDeTxt) {
                String chaineAAfficher = "";
                int tailleDuTableau = tableauDeTxt.size();
                if (tailleDuTableau > 0) {
                        int indexAvtDernier = tailleDuTableau - 1;
                        for (int i = 0; i < tailleDuTableau; i++)       {
                                String mot = tableauDeTxt.get(i);
                                if (i != indexAvtDernier) {
                                        chaineAAfficher += mot+", ";
                                } else {
                                        chaineAAfficher += nettoyerPointFinal(mot)+".";
                                }
                        }
                }
                return UtilString.ucFirst(chaineAAfficher);
        }
        
        protected String nettoyerPointFinal(String mot) {
                mot = mot.replaceAll("[.]$", "");
                return mot;
        }
        
        protected String formaterContenu(String template, Params parametres) {
                String cHtml = Format.substitute(template, parametres);
                
                Params cssParams = new Params();
                cssParams.set("css_corps", ComposantClass.DETAIL_CORPS_CONTENU);
                cssParams.set("css_label", ComposantClass.LABEL);
                cssParams.set("css_indentation", ComposantClass.INDENTATION);
                cssParams.set("css_fieldset", ComposantClass.FIELDSET);
                cssParams.set("css_clear", ComposantClass.CLEAR);
                cHtml = Format.substitute(cHtml, cssParams);
                
                return cHtml;           
        }
        
        protected void afficherOnglet(String template, Params parametres, TabItem onglet) {
                String cHtml = formaterContenu(template, parametres);
                
                HtmlContainer corpsConteneurDuHtml = new HtmlContainer(cHtml);
                onglet.removeAll();
                onglet.add(corpsConteneurDuHtml);               
        }
        
        protected String formaterAutre(String chaineAAfficher) {
                if (!chaineAAfficher.equals("")) {
                        chaineAAfficher = " ["+i18nC.autres()+" : "+chaineAAfficher+"]";
                }
                return chaineAAfficher;
        }
        
        protected String formaterOuiNon(String chaineAFormater) {
                String txtARetourner = "";
                if (chaineAFormater.equals("0")) {
                        txtARetourner = i18nC.non();
                } else if (chaineAFormater.equals("1")) {
                        txtARetourner = i18nC.oui();
                }
                return txtARetourner;
        }
        
        protected String formaterSautDeLigne(String chaineAFormater) {
                String txtARetourner = chaineAFormater.replaceAll("\n", sautLigneTpl);
                return txtARetourner;
        }
        
        protected void lancerChargementListesValeurs(String[] listesCodes) {
                Configuration configuration = (Configuration) Registry.get(RegistreId.CONFIG);
                for (int i = 0; i < listesCodes.length ; i++) {
                        String code = listesCodes[i];
                        ontologiesEnAttenteDeReception.put(configuration.getListeId(code), code);
                        mediateur.obtenirListeValeurEtRafraichir(this, code);
                }
        }
        
        protected void receptionerListeValeurs(ValeurListe listeValeursReceptionnee) {
                mettreAJourOntologieEnAttenteDeReception(listeValeursReceptionnee);
                ajouterListeValeursAOntologie(listeValeursReceptionnee);
        }
        
        protected void mettreAJourOntologieEnAttenteDeReception(ValeurListe listeValeursReceptionnee) {
                ontologiesEnAttenteDeReception.remove(listeValeursReceptionnee.getId());
                if (ontologiesEnAttenteDeReception.size() == 0) {
                        ontologieChargementOk = true;
                }
        }
        
        protected void ajouterListeValeursAOntologie(ValeurListe listeValeursReceptionnee) {
                Iterator<String> it = listeValeursReceptionnee.keySet().iterator();
                while (it.hasNext()) {
                        String cle = it.next();
                        Valeur valeur = listeValeursReceptionnee.get(cle);
                        if (valeur != null) {
                                ontologie.put(cle, valeur);
                        }
                }
        }
        
        protected String construireTxtListeOntologie(String chaineAAnalyser) {
                ArrayList<String> termes = new ArrayList<String>();
                ArrayList<String> autres = new ArrayList<String>();
                if ((chaineAAnalyser != null) && (!chaineAAnalyser.trim().equals("")))  {
                        String[] valeurs = chaineAAnalyser.split(";;");
                        int nbreValeurs = valeurs.length;
                        if (nbreValeurs > 0)    {
                                for (int i = 0; i < nbreValeurs; i++)   {
                                        String id = valeurs[i];
                                        if (id.matches("^[0-9]+[\\#]{2}.+$"))   {                                               
                                                //Chaine truk typé : type##valeur;
                                                if (id.contains("AUTRE##"))     {
                                                        String txt = id.replaceFirst("^AUTRE##", "");
                                                        if (!txt.equals("")) {
                                                                autres.add(txt);
                                                        }
                                                } else {
                                                        String type = id.substring(0, id.indexOf("##"));
                                                        Valeur valeur = ontologie.get(type);
                                                        String txt = id.replaceFirst("^" + type + "##", valeur.getNom() + ": ");
                                                        termes.add(txt);
                                                }
                                        } else if (id.matches("^[0-9]+$"))      {
                                                if (ontologie != null) {
                                                        Valeur valeur = ontologie.get(id);
                                                        if (valeur != null) {
                                                                String termeOntologie = valeur.getNom();
                                                                termes.add(termeOntologie);
                                                        }
                                                }
                                        }
                                }
                        }
                }
                
                String chaineTermes = formaterTableauDeTxt(termes);
                String chaineAutres = formaterTableauDeTxt(autres);
                String chaineARetourner = chaineTermes+formaterAutre(chaineAutres);
                
                return chaineARetourner;
        }
}