Subversion Repositories eFlore/Applications.del

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2212 arthur 1
package org.tela_botanica.del.client.composants.votes.details;
2
 
3
import java.util.Date;
4
import java.util.Iterator;
5
import java.util.List;
6
import java.util.Map;
7
 
8
import org.tela_botanica.del.client.i18n.I18n;
9
import org.tela_botanica.del.client.modeles.PropositionDetermination;
10
import org.tela_botanica.del.client.modeles.VoteDetermination;
11
import org.tela_botanica.del.client.utils.StringUtils;
12
 
13
import com.google.gwt.core.client.GWT;
14
import com.google.gwt.i18n.client.DateTimeFormat;
15
import com.google.gwt.uibinder.client.UiBinder;
16
import com.google.gwt.uibinder.client.UiField;
17
import com.google.gwt.user.client.ui.Composite;
18
import com.google.gwt.user.client.ui.HTML;
19
import com.google.gwt.user.client.ui.HTMLPanel;
20
import com.google.gwt.user.client.ui.HasWidgets;
21
import com.google.gwt.user.client.ui.Label;
22
import com.google.gwt.user.client.ui.Panel;
23
import com.google.gwt.user.client.ui.Widget;
24
 
25
public class DetailListeVotesDeterminationVue extends Composite implements DetailListeVotesDeterminationPresenteur.Vue {
26
 
27
	private static DetailListeVotesDeterminationVueUIiBinder uiBinder = GWT.create(DetailListeVotesDeterminationVueUIiBinder.class);
28
 
29
	interface DetailListeVotesDeterminationVueUIiBinder extends UiBinder<Widget, DetailListeVotesDeterminationVue> {
30
	};
31
 
32
	@UiField
33
	HTMLPanel titre, auteur, aucuneDonnees, detailVotePour, detailVoteContre;
34
 
35
	@UiField
36
	Label scorePour, scoreContre, date;
37
 
38
	@UiField
39
	Panel panneauChargement;
40
 
41
	public DetailListeVotesDeterminationVue() {
42
		initWidget(uiBinder.createAndBindUi(this));
43
	}
44
 
45
	public String formaterDate(String dateNonFormatee) {
46
		try {
47
			DateTimeFormat parseur = DateTimeFormat.getFormat("yyyy-dd-MM HH:mm:ss");
48
			Date date = parseur.parse(dateNonFormatee);
49
 
50
			DateTimeFormat formateur = DateTimeFormat.getFormat("dd/MM/yyyy");
51
			return formateur.format(date);
52
		} catch (IllegalArgumentException e) {
53
			return "";
54
		}
55
	}
56
 
57
	public String formaterDateQuiEstVraimentUneDateEtPasUnString(Date dateNonFormatee) {
58
		String retour = "";
59
		try {
60
			DateTimeFormat formateur = DateTimeFormat.getFormat("dd/MM/yyyy");
61
			retour = formateur.format(dateNonFormatee);
62
		} catch (IllegalArgumentException e) {
63
		}
64
		return retour;
65
	}
66
 
67
	@Override
68
	public void afficherVotes(PropositionDetermination propositionDetermination) {
69
 
70
		HTML htmlTitre = new HTML(propositionDetermination.getEspece());
71
		titre.add(htmlTitre);
72
 
73
		HTML htmlAuteur = new HTML(I18n.getVocabulary().proposePar()+propositionDetermination.getAuteur());
74
		auteur.add(htmlAuteur);
75
 
76
		Date datePropDet = propositionDetermination.getDate();
77
		String date = formaterDateQuiEstVraimentUneDateEtPasUnString(datePropDet);
78
		this.date.setText(date);
79
 
80
		Map<String, VoteDetermination> listeVotes = propositionDetermination.getVotesDeterminations();
81
 
82
		int votesAnonymesPour = 0;
83
		int votesAnonymesContre = 0;
84
 
85
		float votesPour = 0;
86
		float totalVotes = listeVotes.size();
87
 
88
		for (Iterator<String> iterator = listeVotes.keySet().iterator(); iterator.hasNext();) {
89
			VoteDetermination voteDetermination = listeVotes.get(iterator.next());
90
			if (voteDetermination.getVote() == 1) {
91
				//Votes pour
92
				if (voteDetermination.getAuteur() != null) {
93
					ajouterVote(voteDetermination, detailVotePour);
94
				} else {
95
					votesAnonymesPour++;
96
				}
97
				votesPour++;
98
			} else {
99
				//Votes contre
100
				if (voteDetermination.getAuteur() != null) {
101
					ajouterVote(voteDetermination, detailVoteContre);
102
				} else {
103
					votesAnonymesContre++;
104
				}
105
			}
106
		}
107
 
108
		float moyennePour = (votesPour/totalVotes*100);
109
		scorePour.setText(StringUtils.formaterNombre(moyennePour, 2)+"%");
110
		scoreContre.setText(StringUtils.formaterNombre(100 - moyennePour, 2)+"%");
111
 
112
		if (votesAnonymesPour > 0) {
113
			LigneVotePresenteur presenteurVote= new LigneVotePresenteur(new LigneVoteVue(), votesAnonymesPour);
114
			presenteurVote.go(detailVotePour);
115
		}
116
 
117
		if (votesAnonymesContre > 0) {
118
			LigneVotePresenteur presenteurVote= new LigneVotePresenteur(new LigneVoteVue(), votesAnonymesContre);
119
			presenteurVote.go(detailVoteContre);
120
		}
121
	}
122
 
123
	public void ajouterVote(VoteDetermination vote, HasWidgets panel) {
124
		LigneVotePresenteur presenteurVote = new LigneVotePresenteur(new LigneVoteVue(), vote);
125
		presenteurVote.go(panel);
126
	}
127
 
128
	@Override
129
	public void startChargement() {
130
		panneauChargement.setHeight((this.getOffsetHeight()/2)+"px");
131
		panneauChargement.setWidth((this.getOffsetWidth()/2)+"px");
132
		panneauChargement.setVisible(true);
133
	}
134
 
135
	@Override
136
	public void stopChargement() {
137
		panneauChargement.setVisible(false);
138
	}
139
 
140
	@Override
141
	public void afficherAucuneDonnees() {
142
		aucuneDonnees.setVisible(true);
143
	}
144
}