Subversion Repositories eFlore/Applications.del

Rev

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

package org.tela_botanica.del.client.composants.votes.moyennevotes;

import java.util.HashMap;

import org.cobogw.gwt.user.client.ui.Rating;
import org.tela_botanica.del.client.composants.votes.barrerepartition.InfoBulleAnim;
import org.tela_botanica.del.client.i18n.I18n;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.DivElement;
import com.google.gwt.dom.client.SpanElement;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.dom.client.HasMouseMoveHandlers;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FocusPanel;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.Widget;

public class MoyenneVoteVue extends Composite implements MoyenneVotePresenteur.Vue {

        // Annotation can be used to change the name of the associated xml file
        // @UiTemplate("MoyenneVoteVue.ui.xml")
        interface MyUiBinder extends UiBinder<Widget, MoyenneVoteVue> {
        }

        private static MyUiBinder uiBinder = GWT.create(MyUiBinder.class);
        private Rating votes;
        private int valeurOrigine;
        private InfoBulleAnim animerVotePrisEnCompte;
        private InfoBulleAnim animerVoteModifie;
        private InfoBulleAnim animerVoteSupprime;
        
        @UiField
        FocusPanel voter;

        @UiField
        Panel votePrisEnCompte, voteModifie, voteSupprime, zoneFleur, panneauDetailVotes;
        
        @UiField
        Label nbVotes, protocole, noteGenerale/*, nbPoints*/, zoneVoter, lienPanneauFermer;

        @UiField 
        Button boutonAnnuler;

        @UiField
        // pour le panneau de détail des votes
        SpanElement pdvMoyenneArithmetique, pdvMoyennePonderee, pdvMediane, pdvNbPoints,
                                votants5, votants4, votants3, votants2, votants1;
        @UiField
        DivElement barre5, barre4, barre3, barre2, barre1;
        
        public static final double TAILLE_MAX_BARRE = 135; // si "int", peut faire foirer les divisions

        public MoyenneVoteVue() {
                initWidget(uiBinder.createAndBindUi(this));
                votes = new Rating(0, 5);
                votes.setReadOnly(false);
                voter.add(votes);
                masquerBoutonAnnuler();
                masquerPanneauDetailVotes();
                votePrisEnCompte.setVisible(false);
                animerVotePrisEnCompte = new InfoBulleAnim(votePrisEnCompte);
                animerVoteModifie = new InfoBulleAnim(voteModifie);
                animerVoteSupprime = new InfoBulleAnim(voteSupprime);
        }
        
        public HasClickHandlers getBoutonAnnuler() {
                return boutonAnnuler;
        }
        
        public HasText getNbVotes() {
                return nbVotes;
        }
        
        public HasClickHandlers getLienNbVotes() {
                return nbVotes;
        }
        
        /*public HasText getNbPoints() {
                return nbPoints;
        }*/
        
        public HasClickHandlers getVotes() {
                return votes;
        }
        
        public int getValeurVote() {
                return votes.getValue();
        }

        public void afficherBoutonAnnuler() {
                boutonAnnuler.setVisible(true);
        }
        
        public void masquerBoutonAnnuler() {
                boutonAnnuler.setVisible(false);
        }
        
        public void afficherNbVotes() {
                nbVotes.setVisible(true);
        }

        public void masquerNbVotes() {
                nbVotes.setVisible(false);
        }

        /*public void afficherNbPoints() {
                nbPoints.setVisible(true);
        }*/

        /*public void masquerNbPoints() {
                nbPoints.setVisible(false);
        }*/
        
        public void afficherPanneauDetailVotes() {
                panneauDetailVotes.setVisible(true);
        }
        
        public void masquerPanneauDetailVotes() {
                panneauDetailVotes.setVisible(false);
        }

        public Panel getPanneauDetailVotes() {
                return panneauDetailVotes;
        }

        public HasClickHandlers getLienPanneauFermer() {
                return lienPanneauFermer;
        }

        public void reinitialiserVotes() {
                votes.setValue(valeurOrigine);
        }

        public void rafraichir(int voteUtilisateur, int nombreVotes, int nombrePoints, double moyenneArithmetique, double mediane, HashMap<Integer,Integer> occurrencesParNote, double moyennePonderee) {
                valeurOrigine = voteUtilisateur;
                String valeurVote = nombreVotes+" "+I18n.getVocabulary().nbVotes();
                if (nombreVotes > 1) {
                        valeurVote += "s";
                }
                /*String valeurPoints = ", "+Math.max(0, nombrePoints)+" "+I18n.getVocabulary().nbPoints();
                if (nombrePoints > 1) {
                        valeurPoints += "s";
                }*/
                nbVotes.setText(valeurVote);
                //nbPoints.setText(valeurPoints);
                pdvNbPoints.setInnerHTML("" + nombrePoints);
                votes.setValue(voteUtilisateur);
                rafraichirPanneauDetail(moyennePonderee, moyenneArithmetique, mediane, occurrencesParNote, nombreVotes);
        }

        // si la chose est nulle, on retourne 0
        private int nullCestZero(Integer chose) {
                if (chose == null) {
                        return 0;
                } else {
                        return chose;
                }
        }

        // try {
        //   codeConcisEtEfficace();
        // } catch (ShitLanguageException e) {
        //   codeCracra();
        // }
        private void rafraichirPanneauDetail(double moyennePonderee, double moyenneArithmetique, double mediane, HashMap<Integer,Integer> occurrencesParNote, int nombreVotes) {
                // stats
                NumberFormat df = NumberFormat.getFormat("0.###");
                this.pdvMoyennePonderee.setInnerHTML("" + df.format(moyennePonderee));
                this.pdvMoyenneArithmetique.setInnerHTML("" + df.format(moyenneArithmetique));
                this.pdvMediane.setInnerHTML("" + df.format(mediane));
                // détail des votes
                double  tailleBarre1 = 0,
                                tailleBarre2 = 0,
                                tailleBarre3 = 0,
                                tailleBarre4 = 0,
                                tailleBarre5 = 0;
                // on peut pas mettre des attributs dans une variable ? Quelle idée aussi de faire
                // du Web avec un langage statique... du coup codre cracra, ça vous fera les pieds
                if (nombreVotes > 0) {
                        if (occurrencesParNote.get(1) != null) {
                                tailleBarre1 = (int) (occurrencesParNote.get(1) * TAILLE_MAX_BARRE / nombreVotes);
                        }
                        if (occurrencesParNote.get(2) != null) {
                                tailleBarre2 = (int) (occurrencesParNote.get(2) * TAILLE_MAX_BARRE / nombreVotes);
                        }
                        if (occurrencesParNote.get(3) != null) {
                                tailleBarre3 = (int) (occurrencesParNote.get(3) * TAILLE_MAX_BARRE / nombreVotes);
                        }
                        if (occurrencesParNote.get(4) != null) {
                                tailleBarre4 = (int) (occurrencesParNote.get(4) * TAILLE_MAX_BARRE / nombreVotes);
                        }
                        if (occurrencesParNote.get(5) != null) {
                                tailleBarre5 = (int) (occurrencesParNote.get(5) * TAILLE_MAX_BARRE / nombreVotes);
                        }
                }
                this.barre1.setAttribute("style", "width: " + tailleBarre1 + "px;");
                this.barre2.setAttribute("style", "width: " + tailleBarre2 + "px;");
                this.barre3.setAttribute("style", "width: " + tailleBarre3 + "px;");
                this.barre4.setAttribute("style", "width: " + tailleBarre4 + "px;");
                this.barre5.setAttribute("style", "width: " + tailleBarre5 + "px;");
                this.votants1.setInnerHTML("" + nullCestZero(occurrencesParNote.get(1)));
                this.votants2.setInnerHTML("" + nullCestZero(occurrencesParNote.get(2)));
                this.votants3.setInnerHTML("" + nullCestZero(occurrencesParNote.get(3)));
                this.votants4.setInnerHTML("" + nullCestZero(occurrencesParNote.get(4)));
                this.votants5.setInnerHTML("" + nullCestZero(occurrencesParNote.get(5)));
        }

        @Override
        public void ajouterAuParent(HasWidgets composite) {
                composite.add(this);
        }
        
        @Override
        public void afficherVotePrisEnCompte() {
                votePrisEnCompte.setStyleName("votePrisEnCompteOui");
                animerVotePrisEnCompte.run(2000);
        }
        
        public HasWidgets getZoneFleur() {
                return zoneFleur;
        }
        
        public HasText getZoneProtocole() {
                return protocole;
        }
        
        public void setNoteGenerale(int note) {
                noteGenerale.setText(" : " + note);
        }
        
        @Override
        public void setNoteGeneraleToolTip(double valeurVoteTotalPrecise) {
                double valeurArrondie = (double)Math.round(valeurVoteTotalPrecise * 1000) / 1000;
                zoneFleur.setTitle(I18n.getVocabulary().votesMoyennePrecise()+valeurArrondie);
        }
        
        public void afficherVoteModifie() {
                voteModifie.setStyleName("votePrisEnCompteOui");
                animerVoteModifie.run(2000);
        }
        
        @Override
        public void afficherVoteSupprime() {
                voteSupprime.setStyleName("voteSupprime");
                animerVoteSupprime.run(2000);
        }
        
        public HasMouseMoveHandlers getZoneVoter() {
                return zoneVoter;
        }
        
        public void masquerZoneVoter() {
                zoneVoter.setVisible(false);
        }
        
        @Override
        public void desactiverInteractionVote() {
                votes.setReadOnly(true);
                boutonAnnuler.setEnabled(false);
                boutonAnnuler.addStyleName("elementMasque");
                votes.addStyleName("elementMasque");
        }
        
        @Override
        public void activerInteractionVote() {
                votes.setReadOnly(false);
                boutonAnnuler.setEnabled(true);
                boutonAnnuler.removeStyleName("elementMasque");
                votes.removeStyleName("elementMasque");
        }

        @Override
        public void afficherMessageVoteIdentifie(boolean peutVoter) {
                String message = I18n.getVocabulary().votez();
                if(!peutVoter) {
                        message = I18n.getVocabulary().identifiezVousPourVoter();
                }
                zoneVoter.getElement().setInnerHTML("<span>"+message+" !</span>");      
        }
}