Subversion Repositories eFlore/Applications.coel

Rev

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

package org.tela_botanica.client.vues.commentaire;

import java.util.ArrayList;

import org.tela_botanica.client.ComposantClass;
import org.tela_botanica.client.Mediateur;
import org.tela_botanica.client.composants.ChampComboBoxRechercheTempsReelPaginable;
import org.tela_botanica.client.composants.ChampSliderPourcentage;
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.commentaire.Commentaire;
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 com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.form.CheckBox;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.form.Validator;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.google.gwt.user.client.Window;


public class CommentaireForm extends Formulaire implements Rafraichissable {
        
        private Commentaire commentaire;

        private TextField<String> titreChp;
        private TextArea texteChp;
        private ChampSliderPourcentage ponderationChp;
        private CheckBox publicChp;
        
        private static boolean formulaireValideOk = false;
        private static boolean commentaireValideOk = false;
        
        private Sequenceur sequenceur = new Sequenceur();

        public CommentaireForm(Mediateur mediateurCourrant, String commentaireId) {
                initialiserCommentaireForm(mediateurCourrant, commentaireId);
        }

        public CommentaireForm(Mediateur mediateurCourrant, String commentaireId, Rafraichissable vueARafraichirApresValidation) {
                vueExterneARafraichirApresValidation = vueARafraichirApresValidation;
                initialiserCommentaireForm(mediateurCourrant, commentaireId);
        }
        
        private void initialiserCommentaireForm(Mediateur mediateurCourrant, String commentaireId) {
                initialiserValidation();
                
                commentaire = new Commentaire();
                commentaire.setId(commentaireId);
                
                String modeDeCreation = (UtilString.isEmpty(commentaire.getId()) ? Formulaire.MODE_AJOUTER : Formulaire.MODE_MODIFIER);
                initialiserFormulaire(mediateurCourrant, modeDeCreation, MenuApplicationId.COMMENTAIRE);
                
                panneauFormulaire.setLayout(new FormLayout());
                genererTitreFormulaire();
                creerChamps();

                if (modeDeCreation.equals(Formulaire.MODE_MODIFIER)) {
                        mediateur.selectionnerCommentaire(this, commentaireId, null);
                }
        }
        
        private void genererTitreFormulaire() {
                String titre = i18nC.commentaireTitreFormAjout();
                if (mode.equals(Formulaire.MODE_MODIFIER)) {
                         titre = i18nC.commentaireTitreFormModif();
                         if (commentaire != null) {
                                 titre += " - "+i18nC.id()+": "+commentaire.getId();
                         }
                }
                panneauFormulaire.setHeadingHtml(titre);
        }
        
        private void creerChamps() {
                                
                titreChp = new TextField<String>();
                titreChp.setFieldLabel(i18nC.commentaireTitre());
                titreChp.setAllowBlank(false);
                titreChp.addStyleName(ComposantClass.OBLIGATOIRE);
                titreChp.addListener(Events.Valid, creerEcouteurChampObligatoire());
                titreChp.addListener(Events.Invalid, creerEcouteurChampObligatoire());
                panneauFormulaire.add(titreChp, new FormData(450, 0));
                
                texteChp = new TextArea();
                texteChp.setFieldLabel(i18nC.commentaireTexte());
                panneauFormulaire.add(texteChp, new FormData(450, 250));
                
                ponderationChp = new ChampSliderPourcentage(i18nC.commentairePonderation());
                panneauFormulaire.add(ponderationChp, new FormData(450, 0));
                
                publicChp = new CheckBox();
                publicChp.setFieldLabel(i18nC.donneePublic());
                panneauFormulaire.add(publicChp, new FormData(50, 0));
        }
        
        public void rafraichir(Object nouvellesDonnees) {
                if (nouvellesDonnees instanceof Commentaire) {
                        // Si on a reçu les details d'une publication
                        rafraichirCommentaire((Commentaire) nouvellesDonnees);
                } else if (nouvellesDonnees instanceof Information) {
                        rafraichirInformation((Information) nouvellesDonnees);  
                } else {
                        Debug.log(Mediateur.i18nM.erreurRafraichir(nouvellesDonnees.getClass(), this.getClass()));
                }
                
                if (etreValide()) {
                        initialiserValidation();
                        repandreRafraichissement();
                        controlerFermeture();
                }
        }
        
        private void rafraichirCommentaire(Commentaire commentaireRecu) {
                commentaire = commentaireRecu;
                peuplerFormulaire();
                genererTitreFormulaire();
        }
        
        private void rafraichirInformation(Information info) {
                // Gestion des messages d'erreur
                if (info.getMessages() != null && !info.getMessages().toString().equals("[]")) {
                        Debug.log("MESSAGES:\n"+info.getMessages().toString());
                }
                
                // Gestion des actions
                String type = info.getType();
                if (type.equals("ajout_commentaire") || type.equals("modif_commentaire")) {
                        commentaireValideOk = true;
                }
                if (info.getType().equals("ajout_commentaire")) {
                                String noteId = (String) info.getDonnee(0);
                                commentaire.setId(noteId);
                                this.mode = MODE_MODIFIER;
                }
                
                // Gestion des messages
                if (info.getType().equals("modif_commentaire")) {
                        InfoLogger.display("Modification d'une note", info.toString());
                } else if (info.getType().equals("ajout_commentaire")) {
                        if (info.getDonnee(0) != null && info.getDonnee(0) instanceof String) {
                                String noteId = (String) info.getDonnee(0);
                                InfoLogger.display("Ajout d'une note", "La note '"+noteId+"' a bien été ajoutée");
                        } else {
                                InfoLogger.display("Ajout d'une note", info.toString());
                        }
                }
        }

        private Boolean etreValide() {
                Boolean valide = false;
                if (formulaireValideOk && commentaireValideOk) {
                        valide = true;
                }
                return valide;
        }
        
        private void initialiserValidation() {
                formulaireValideOk = false;
                commentaireValideOk = false;
        }
        
        private void repandreRafraichissement() {
                if (vueExterneARafraichirApresValidation != null) {
                        String type = "commentaire_modifiee";
                        if (mode.equals(Formulaire.MODE_AJOUTER)) {
                                type = "commentaire_ajoutee";
                        }
                        Information info = new Information(type);
                        info.setDonnee(0, commentaire);
                        vueExterneARafraichirApresValidation.rafraichir(info);
                }
        }
        
        public boolean soumettreFormulaire() {
                formulaireValideOk = verifierFormulaire();
                if (formulaireValideOk) {
                        soumettreCommentaire();
                }
                return formulaireValideOk;
        }
        
        private void soumettreCommentaire() {
                Commentaire commentaireCollectee = collecterCommentaire();
                if (commentaireCollectee != null) {
                        if (mode.equals(Formulaire.MODE_AJOUTER)) {
                                mediateur.ajouterCommentaire(this, commentaireCollectee);
                        } else if (mode.equals(Formulaire.MODE_MODIFIER)) {
                                mediateur.modifierCommentaire(this, commentaireCollectee);
                        }
                } else {
                        if(clicBoutonvalidation) {
                                fermerFormulaire();
                        }
                }
        }
        
        public boolean verifierFormulaire() {
                boolean valide = true;
                ArrayList<String> messages = new ArrayList<String>();
                
                String titre = titreChp.getValue();
                if (titre == null || titre.equals("")) {
                        messages.add(i18nC.commentaireMessageTitre());
                }
                
                if (messages.size() != 0) {
                        String[] tableauDeMessages = {};
                        tableauDeMessages = messages.toArray(tableauDeMessages);
                        MessageBox.alert(i18nC.erreurSaisieTitre(), UtilArray.implode(tableauDeMessages, "<br />"), null);
                        valide = false;
                }
                return valide;          
        }
        
        private void peuplerFormulaire() {
                titreChp.setValue(commentaire.getTitre());
                texteChp.setValue(commentaire.getTexte());
                ponderationChp.peupler(commentaire.getPonderation());
                boolean acces = (commentaire.etrePublic() ? true : false);
                publicChp.setValue(acces);
        }
        
        private Commentaire collecterCommentaire() {
                Commentaire commentaireCollectee = (Commentaire) commentaire.cloner(new Commentaire());
                
                String titre = titreChp.getValue();
                commentaireCollectee.setTitre(titre);
                
                String texte = texteChp.getValue();
                commentaireCollectee.setTexte(texte);
                
                String ponderation = ponderationChp.getValeur();
                commentaireCollectee.setPonderation(ponderation);
                
                String acces = (publicChp.getValue() ? "1" : "0");
                commentaireCollectee.setPublic(acces);
                
                Commentaire commentaireARetourner = null;
                if (!commentaireCollectee.comparer(commentaire)) {
                        commentaireARetourner = commentaire = commentaireCollectee;
                }
                return commentaireARetourner;
        }
}