Subversion Repositories eFlore/Applications.coel

Rev

Rev 1896 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package org.tela_botanica.client.vues.collection;

import java.util.ArrayList;

import org.tela_botanica.client.ComposantClass;
import org.tela_botanica.client.Mediateur;
import org.tela_botanica.client.composants.ChampComboBoxListeValeurs;
import org.tela_botanica.client.composants.InfoLogger;
import org.tela_botanica.client.interfaces.Rafraichissable;
import org.tela_botanica.client.modeles.Information;
import org.tela_botanica.client.modeles.MenuApplicationId;
import org.tela_botanica.client.modeles.Valeur;
import org.tela_botanica.client.modeles.collection.Collection;
import org.tela_botanica.client.synchronisation.Sequenceur;
import org.tela_botanica.client.util.Debug;
import org.tela_botanica.client.util.UtilArray;
import org.tela_botanica.client.util.UtilString;
import org.tela_botanica.client.vues.Formulaire;
import org.tela_botanica.client.vues.structure.StructureForm;

import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.TabItem;
import com.extjs.gxt.ui.client.widget.TabPanel;
import com.extjs.gxt.ui.client.widget.form.FieldSet;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.form.FormPanel.LabelAlign;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.google.gwt.user.client.Window;

public class CollectionForm extends Formulaire implements Rafraichissable {

        protected Collection collection = null;
        protected Collection collectionCollectee = null;

        private ChampComboBoxListeValeurs typesCollectionCombo = null;
        
        private TabPanel onglets = null;
        private CollectionFormGeneral generalOnglet = null;
        private CollectionFormPersonne personneOnglet = null;
        private CollectionFormPublication publicationOnglet = null;
        private CollectionFormDescription descriptionOnglet = null;
        private CollectionFormContenu contenuOnglet = null;
        private CollectionFormInventaire inventaireOnglet = null;
        private CollectionFormCommentaire commentaireOnglet = null;
        private TextField<String> nomChp = null;
        
        private Sequenceur sequenceur = new Sequenceur();
        
        public CollectionForm(Mediateur mediateurCourrant, String collectionId) {
                initialiserCollectionForm(mediateurCourrant, collectionId);
        }
        
        private void initialiserCollectionForm(Mediateur mediateurCourrant, String collectionId) {
                collection = new Collection();
                collection.setId(collectionId);
                
                String modeDeCreation = (UtilString.isEmpty(collection.getId()) ? Formulaire.MODE_AJOUTER : Formulaire.MODE_MODIFIER);
                initialiserFormulaire(mediateurCourrant, modeDeCreation, MenuApplicationId.COLLECTION);
                
                genererTitreFormulaire();
                
                creerOnglets();
                creerFieldsetPrincipal();
                
                if (modeDeCreation.equals(Formulaire.MODE_MODIFIER)) {
                        mediateurCourrant.selectionnerCollection(this, collectionId, null, sequenceur);
                        mediateurCourrant.selectionnerCollectionAPersonne(this, collectionId, null, sequenceur);
                        //mediateurCourrant.selectionnerCollectionAPublication(this, collectionId, sequenceur);
                        //mediateurCourrant.selectionnerCollectionACommentaire(this, collectionId, sequenceur);
                }
        }
        
        private void genererTitreFormulaire() {
                String titre = i18nC.collectionTitreFormAjout();
                if (mode.equals(Formulaire.MODE_MODIFIER)) {
                         titre = i18nC.collectionTitreFormModif();
                         if (collection != null) {
                                 titre += " - "+i18nC.id()+": "+collection.getId();
                         }
                }
                panneauFormulaire.setHeadingHtml(titre);
        }
        
        private void creerFieldsetPrincipal() {
                FieldSet principalFieldSet = new FieldSet();
                principalFieldSet.setHeadingHtml("Info");
                principalFieldSet.setCollapsible(true);
                principalFieldSet.setLayout(Formulaire.creerFormLayout(150, LabelAlign.LEFT));
                
                nomChp  = new TextField<String>();
                nomChp.setTabIndex(tabIndex++);
                nomChp.setFieldLabel(i18nC.nomCollection());
                nomChp.setAllowBlank(false);
                nomChp.addStyleName(ComposantClass.OBLIGATOIRE);
                nomChp.addListener(Events.Valid, creerEcouteurChampObligatoire());
                nomChp.getMessages().setBlankText(i18nC.champObligatoire());
                principalFieldSet.add(nomChp, new FormData(450, 0));
                
                Listener<BaseEvent> ecouteurTypeCollection = new Listener<BaseEvent>() {
                        public void handleEvent(BaseEvent be) {
                                Valeur valeur = typesCollectionCombo.getValue();
                                
                                // Gestion des onglets en fonction du type de collection
                                mediateur.activerChargement(this, "Chargement des onglets");
                                if (valeur != null && valeur.getId().equals(Valeur.COLLECTION_NCD_HERBIER)) {
                                        activerOngletsHerbier();
                                } else {
                                        activerOngletsDefaut();
                                }
                                mediateur.desactiverChargement(this);
                        }
                };
                
                typesCollectionCombo = new ChampComboBoxListeValeurs(i18nC.typeCollectionNcd(), "typeCollectionNcd", tabIndex++);
                typesCollectionCombo.peupler(Valeur.COLLECTION_NCD_HERBIER);
                typesCollectionCombo.addStyleName(ComposantClass.OBLIGATOIRE);
                typesCollectionCombo.addListener(Events.Change, ecouteurTypeCollection);
                typesCollectionCombo.addListener(Events.Select, ecouteurTypeCollection);
                typesCollectionCombo.addListener(Events.Valid, creerEcouteurChampObligatoire());
                principalFieldSet.add(typesCollectionCombo, new FormData(150, 0));
                typesCollectionCombo.fireEvent(Events.Select);
                
                panneauFormulaire.setTopComponent(principalFieldSet);
        }
        
        private void activerOngletsDefaut() {
                if (onglets.findItem(CollectionFormGeneral.ID, false) == null) {
                        onglets.add(generalOnglet);
                }
                if (onglets.findItem(CollectionFormPersonne.ID, false) == null) {
                        onglets.add(personneOnglet);
                }
                if (onglets.findItem(CollectionFormPublication.ID, false) != null) {
                        onglets.remove(publicationOnglet);
                }
                if (onglets.findItem(CollectionFormDescription.ID, false) != null) {
                        onglets.remove(descriptionOnglet);
                }
                if (onglets.findItem(CollectionFormContenu.ID, false) != null) {
                        onglets.remove(contenuOnglet);
                }
                if (onglets.findItem(CollectionFormInventaire.ID, false) != null) {
                        onglets.remove(inventaireOnglet);
                }
                if (onglets.findItem(CollectionFormCommentaire.ID, false) != null) {
                        onglets.remove(commentaireOnglet);
                }
                panneauFormulaire.layout();
        }

        private void activerOngletsHerbier() {
                if (onglets.findItem(CollectionFormGeneral.ID, false) == null) {
                        onglets.add(generalOnglet);
                }
                if (onglets.findItem(CollectionFormPersonne.ID, false) == null) {
                        onglets.add(personneOnglet);
                }
                if (onglets.findItem(CollectionFormPublication.ID, false) == null) {
                        onglets.add(publicationOnglet);
                }
                if (onglets.findItem(CollectionFormDescription.ID, false) == null) {
                        onglets.add(descriptionOnglet);
                }
                if (onglets.findItem(CollectionFormContenu.ID, false) == null) {
                        onglets.add(contenuOnglet);
                }
                if (onglets.findItem(CollectionFormInventaire.ID, false) == null) {
                        onglets.add(inventaireOnglet);
                }
                if (onglets.findItem(CollectionFormCommentaire.ID, false) == null) {
                        onglets.add(commentaireOnglet);
                }
                panneauFormulaire.layout();
        }
        
        private void creerOnglets() {
                onglets = new TabPanel();
                // NOTE : pour faire apparaître les scrollBar il faut définir la hauteur du panneau d'onglets à 100% (autoHeight ne semble pas fonctionner) 
                onglets.setHeight("100%");
                
                // Onlget formulaire GENERAL
                onglets.add(creerOngletGeneral());
                
                // Onlget formulaire AUTEUR
                onglets.add(creerOngletPersonne());
                
                // Onlget formulaire PUBLICATION
                onglets.add(creerOngletPublication());
                
                // Onlget formulaire DESCRIPTION
                onglets.add(creerOngletDescription());
                
                // Onlget formulaire CONTENU
                onglets.add(creerOngletContenu());
                
                // Onlget formulaire INVENTAIRE
                onglets.add(creerOngletInventaire());
                
                // Onlget formulaire COMMENTAIRE
                onglets.add(creerOngletCommentaire());
                
                // Sélection de l'onglet par défaut
                onglets.setSelection(generalOnglet);
                
                panneauFormulaire.add(onglets);
        }
        
        private TabItem creerOngletGeneral() {
                generalOnglet = new CollectionFormGeneral(this);
                return generalOnglet;
        }
        
        private TabItem creerOngletPersonne() {
                personneOnglet = new CollectionFormPersonne(this);
                return personneOnglet;
        }
        
        private TabItem creerOngletPublication() {
                publicationOnglet = new CollectionFormPublication(this);
                return publicationOnglet;
        }
        
        private TabItem creerOngletDescription() {
                descriptionOnglet = new CollectionFormDescription(this);
                return descriptionOnglet;
        }
        
        private TabItem creerOngletContenu() {
                contenuOnglet = new CollectionFormContenu(this);
                return contenuOnglet;
        }
        
        private TabItem creerOngletInventaire() {
                inventaireOnglet = new CollectionFormInventaire(this);
                return inventaireOnglet;
        }
        
        private TabItem creerOngletCommentaire() {
                commentaireOnglet = new CollectionFormCommentaire(this);
                return commentaireOnglet;
        }
        
        public void rafraichir(Object nouvellesDonnees) {
                if (nouvellesDonnees instanceof Information) {
                        Information info = (Information) nouvellesDonnees;
                        rafraichirInformation(info);
                } else {
                        Debug.log(Mediateur.i18nM.erreurRafraichir(nouvellesDonnees.getClass(), this.getClass()));
                }
                controlerFermeture();
        }

        private void rafraichirInformation(Information info) {
                if (info.getMessages() != null && !info.getMessages().toString().equals("[]")) {
                        Debug.log("MESSAGES:\n"+info.getMessages().toString());
                }
                String infoType = info.getType();
                
                if (infoType.equals("modif_collection")) {
                        InfoLogger.display("Modification d'une collection", info.toString());
                        repandreRafraichissement();
                } else if (infoType.equals("selection_collection")) {
                        InfoLogger.display("Modification d'une collection", info.toString());
                        if (info.getDonnee(0) != null) {
                                collection = (Collection) info.getDonnee(0);
                        }
                        peupler();
                        genererTitreFormulaire();
                } else if (infoType.equals("ajout_collection")) {
                        if (info.getDonnee(0) != null && info.getDonnee(0) instanceof String) {
                                String collectionId = (String) info.getDonnee(0);
                                InfoLogger.display("Ajout d'une collection", "La collection '"+collectionId+"' a bien été ajoutée");
                                collection.setId(collectionId);
                                // Suite à la récupération de l'id de l'institution nouvellement ajoutée nous ajoutons les personnes et les publications liées
                                personneOnglet.rafraichir(info);
                                publicationOnglet.rafraichir(info);
                                commentaireOnglet.rafraichir(info);
                                
                                this.mode = MODE_MODIFIER;
                        } else {
                                InfoLogger.display("Ajout d'une collection", info.toString());
                        }
                } else if (infoType.equals("liste_collection_a_personne")) {
                        personneOnglet.rafraichir(info);
                } else if (infoType.equals("liste_collection_a_publication")) {
                        publicationOnglet.rafraichir(info);
                } else if (infoType.equals("liste_collection_a_commentaire")) {
                        commentaireOnglet.rafraichir(info);
                }
        }
                
        private void peupler() {
                if (collection != null) {
                        nomChp.setValue(collection.getNom());
                        typesCollectionCombo.peupler(collection.getTypeNcd());
                        
                        peuplerOnglets();
                }
        }

        private void peuplerOnglets() {
                generalOnglet.peupler();
                descriptionOnglet.peupler();
                contenuOnglet.peupler();
                inventaireOnglet.peupler();
                commentaireOnglet.peupler();
        }

        public boolean soumettreFormulaire() {
                
                // Vérification de la validité des champs du formulaire
                boolean formulaireValide = verifierFormulaire();

                if (formulaireValide) {
                        // Collecte des données du formulaire
                        Collection collectionAEnregistrer = collecterCollection();
                        if (mode.equals(MODE_AJOUTER)) {
                                mediateur.ajouterCollection(this, collectionAEnregistrer);
                        } else if (mode.equals(MODE_MODIFIER)) {
                                if (collectionAEnregistrer == null) {
                                        //InfoLogger.display("Modification d'une collection", "Rien n'a été enregistré car le formulaire n'a pas été modifié.");
                                        this.controlerFermeture();
                                } else {
                                        mediateur.modifierCollection(this, collectionAEnregistrer);
                                }
                        }
                        
                        soumettreOnglets();
                }
                return formulaireValide;
        }
        
        private void soumettreOnglets() {
                personneOnglet.soumettre();
                publicationOnglet.soumettre();
                commentaireOnglet.soumettre();
        }
        
        private Collection collecterCollection() {
                collectionCollectee = (Collection) collection.cloner(new Collection());
                this.collecter();
                collecterOnglets();
                
                Collection collectionARetourner = null;
                if (!collectionCollectee.comparer(collection) ||
                                !collectionCollectee.getBotanique().comparer(collection.getBotanique()) ||
                                publicationOnglet.publicationsSontModifiees) {
                        collectionARetourner = collection = collectionCollectee;
                        publicationOnglet.publicationsSontModifiees = false;
                }
                return collectionARetourner;
        }
        
        private void collecter() {
                collectionCollectee.setNom(nomChp.getValue());
                collectionCollectee.setTypeNcd(typesCollectionCombo.getValue().getId());
        }

        private void collecterOnglets() {
                generalOnglet.collecter();
                personneOnglet.collecter();
                publicationOnglet.collecter();
                descriptionOnglet.collecter();
                contenuOnglet.collecter();
                inventaireOnglet.collecter();
                commentaireOnglet.collecter();
        }

        public boolean verifierFormulaire() {
                ArrayList<String> messages = new ArrayList<String>();
                
                // Vérification des infos sur le nom de la collection
                if (nomChp.getValue() == null 
                                || nomChp.getValue().equals("") 
                                || (mode.equals(MODE_MODIFIER) && collection != null && collection.getNom().equals(""))) {
                        messages.add("Veuillez donner un nom à la collection.");
                }
                
                // Vérification des infos sur le type de collection
                if (typesCollectionCombo.getValue() == null 
                                || typesCollectionCombo.getValue().equals("")) {
                        messages.add("Veuillez sélectionner un type pour la collection.");
                }
                
                messages.addAll(verifierOnglets());
                                
                // Affichage des messages d'alerte
                if (messages.size() != 0) {
                        String[] tableauDesMessages = {};
                        tableauDesMessages = messages.toArray(tableauDesMessages);
                        MessageBox.alert("Erreurs de saisies", UtilArray.implode(tableauDesMessages, "<br />"), null);
                        return false;
                }
                return true;
        }
        
        private ArrayList<String> verifierOnglets() {
                ArrayList<String> messages = new ArrayList<String>();
                messages.addAll(generalOnglet.verifier());
                messages.addAll(personneOnglet.verifier());
                return messages;
        }
        
        private void repandreRafraichissement() {
                if (vueExterneARafraichirApresValidation != null) {
                        String type = "modif_collection";
                        if (mode.equals(Formulaire.MODE_AJOUTER)) {
                                type = "ajout_collection";
                        }
                        Information info = new Information(type);
                        info.setDonnee(0, collection);
                        vueExterneARafraichirApresValidation.rafraichir(info);
                }
        }
}