Subversion Repositories eFlore/Applications.coel

Rev

Rev 860 | Go to most recent revision | Blame | Last modification | View Log | RSS feed

package org.tela_botanica.client.vues;

import org.tela_botanica.client.Mediateur;
import org.tela_botanica.client.composants.ChampComboBoxListeValeurs;
import org.tela_botanica.client.interfaces.Rafraichissable;
import org.tela_botanica.client.modeles.Collection;
import org.tela_botanica.client.modeles.Information;
import org.tela_botanica.client.modeles.MenuApplicationId;
import org.tela_botanica.client.modeles.Valeur;

import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.TabItem;
import com.extjs.gxt.ui.client.widget.TabPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
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.core.client.GWT;

public class CollectionForm extends Formulaire implements Rafraichissable {

        private Collection collection = null;
        private Collection collectionCollectee = null;

        private ChampComboBoxListeValeurs typesCollectionCombo = null;
        
        private TabPanel onglets = null;
        private TabItem generalOnglet = null;
        private TabItem personneOnglet = null;
        private TabItem publicationOnglet = null;
        private TabItem descriptionOnglet = null;
        private TabItem contenuOnglet = null;
        private TabItem inventaireOnglet = null;
        private TextField<String> nomChp = null;
        
        public CollectionForm(Mediateur mediateurCourrant, String modeDeCreation) {
                initialiserFormulaire(mediateurCourrant, modeDeCreation, MenuApplicationId.COLLECTION);
                creerFieldsetPrincipal();
        }
        
        private void creerFieldsetPrincipal() {
                FieldSet principalFieldSet = new FieldSet();
                principalFieldSet.setHeading("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.getMessages().setBlankText(i18nC.champObligatoire());
                principalFieldSet.add(nomChp, new FormData(450, 0));
                
                typesCollectionCombo = new ChampComboBoxListeValeurs(i18nC.typeCollectionNcd(), "typeCollectionNcd", tabIndex++);
                typesCollectionCombo.setValeurDefaut(Valeur.COLLECTION_NCD_HERBIER);
                typesCollectionCombo.addListener(Events.Select, new Listener<BaseEvent>() {
                        @Override
                        public void handleEvent(BaseEvent be) {
                                Valeur valeur = typesCollectionCombo.getValue();
                                panneauFormulaire.remove(onglets);
                                mediateur.activerChargement("");
                                if (valeur != null && valeur.getId().equals(Valeur.COLLECTION_NCD_HERBIER)) {
                                        onglets = creerOngletsHerbier();
                                        panneauFormulaire.add(onglets);
                                } else {
                                        onglets = creerOngletsDefaut();
                                        panneauFormulaire.add(onglets);
                                }
                                mediateur.desactiverChargement();
                                panneauFormulaire.layout();
                        }
                });
                principalFieldSet.add(typesCollectionCombo, new FormData(150, 0));
                typesCollectionCombo.fireEvent(Events.Select);
                
                panneauFormulaire.setTopComponent(principalFieldSet);
        }
        
        protected TabPanel creerOngletsDefaut() {
                TabPanel ongletsCollectionDefaut = new TabPanel();
                
                // Onlget formulaire GENERAL
                ongletsCollectionDefaut.add(creerOngletGeneral());
                
                // Onlget formulaire AUTEUR
                ongletsCollectionDefaut.add(creerOngletPersonne());

                return ongletsCollectionDefaut;
        }
        
        protected TabPanel creerOngletsHerbier() {
                TabPanel ongletsCollectionHerbier = 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) 
                ongletsCollectionHerbier.setHeight("100%");
                
                // Onlget formulaire GENERAL
                ongletsCollectionHerbier.add(creerOngletGeneral());
                
                // Onlget formulaire AUTEUR
                ongletsCollectionHerbier.add(creerOngletPersonne());
                
                // Onlget formulaire PUBLICATION
                ongletsCollectionHerbier.add(creerOngletPublication());
                
                // Onlget formulaire DESCRIPTION
                ongletsCollectionHerbier.add(creerOngletDescription());
                
                // Onlget formulaire CONTENU
                ongletsCollectionHerbier.add(creerOngletContenu());
                
                // Onlget formulaire INVENTAIRE
                ongletsCollectionHerbier.add(creerOngletInventaire());
                
                // Sélection de l'onglet par défaut
                ongletsCollectionHerbier.setSelection(generalOnglet);
                
                return ongletsCollectionHerbier;
        }
        
        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;
        }
        
        public void rafraichir(Object nouvellesDonnees) {
                if (nouvellesDonnees instanceof Information) {
                        Information info = (Information) nouvellesDonnees;
                        rafraichirInformation(info);
                } else {
                        GWT.log(Mediateur.i18nM.erreurRafraichir(nouvellesDonnees.getClass(), this.getClass()), null);
                }
                
                controlerFermetureApresRafraichissement();
        }

        private void rafraichirInformation(Information info) {
                if (info.getMessages() != null && !info.getMessages().toString().equals("[]")) {
                        GWT.log("MESSAGES:\n"+info.getMessages().toString(), null);
                }
                
                if (info.getType().equals("modif_structure")) {
                        Info.display("Modification d'une collection", info.toString());
                } else if (info.getType().equals("selection_collection")) {
                        Info.display("Modification d'une collection", info.toString());
                        if (info.getDonnee(0) != null) {
                                collection = (Collection) info.getDonnee(0);
                        }
                        actualiserPeuplementOnglet();
                        actualiserTitrePanneau();
                }
        }
        
        private void actualiserPeuplementOnglet() {
                // TODO : déclencher le peuplement de l'onglet courant
        }

        private void actualiserTitrePanneau() {
                String titre = i18nC.titreModifFormCollection();
                // Composition du titre
                titre += " - ID : "+collection.getId();
                panneauFormulaire.setHeading(titre);            
        }

        @Override
        protected SelectionListener<ButtonEvent> creerEcouteurValidation() {
                SelectionListener<ButtonEvent> ecouteur = new SelectionListener<ButtonEvent>() {
                        @Override
                        public void componentSelected(ButtonEvent ce) {
                                String code = ((Button) ce.getComponent()).getData("code");
                                if (code.equals(FormulaireBarreValidation.CODE_BOUTON_VALIDER)) {
                                        soumettreFormulaire();
                                        clicBoutonvalidation = true;
                                } else if (code.equals(FormulaireBarreValidation.CODE_BOUTON_APPLIQUER)) {
                                        soumettreFormulaire();
                                } else if (code.equals(FormulaireBarreValidation.CODE_BOUTON_ANNULER)) {
                                        mediateur.clicMenu(menuIdCourant);
                                } else if (code.equals(FormulaireBarreValidation.CODE_BOUTON_REINITIALISER)) {
                                        reinitialiserFormulaire();
                                }
                        }
                };
                
                return ecouteur;
        }
        
        protected boolean soumettreFormulaire() {
                // Vérification de la validité des champs du formulaire
                boolean fomulaireValide = verifierFormulaire();
                if (fomulaireValide) {
                        // Collecte des données du formulaire
                        Collection collection = collecterCollection();
                        
                        if (mode.equals(MODE_AJOUTER)) {
                                mediateur.ajouterCollection(this, collection);
                        } else if (mode.equals(MODE_MODIFIER)) {
                                if (collection == null) {
                                        Info.display("Modification d'une collection", "Rien n'a été enregistré car le formulaire n'a pas été modifié.");
                                } else {
                                        mediateur.modifierCollection(this, collection);
                                }
                        }
                }
                return fomulaireValide;
        }
        
        protected void reinitialiserFormulaire() {
                if (mode.equals(StructureForm.MODE_MODIFIER)) {
                        mediateur.afficherFormCollection(collection.getId());
                } else {
                        mediateur.afficherFormCollection(null);
                }
        }
        
        private Collection collecterCollection() {
                collectionCollectee = (Collection) collection.cloner(new Collection());
                
                collecterOngletGeneral();
                
                Collection collectionARetourner = null;
                if (!collectionCollectee.comparer(collection)) {
                        collectionARetourner = collection = collectionCollectee;
                }
                return collectionARetourner;
        }
        
        private void collecterOngletGeneral() {
                if (generalOnglet.getData("acces").equals(true)) {
                        
                }
        }
        
        protected boolean verifierFormulaire() {
                return true;
        }
}