Subversion Repositories eFlore/Applications.del

Rev

Rev 161 | Rev 200 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
161 gduche 1
package org.tela_botanica.del.client.vues.rechercheobservations;
2
 
3
import java.util.HashMap;
4
import java.util.Iterator;
166 gduche 5
import java.util.List;
161 gduche 6
 
7
import org.tela_botanica.del.client.composants.presenteur.Presenteur;
166 gduche 8
import org.tela_botanica.del.client.modeles.Observation;
9
import org.tela_botanica.del.client.utils.MockDatasource;
161 gduche 10
 
11
import com.google.gwt.event.dom.client.ClickEvent;
12
import com.google.gwt.event.dom.client.ClickHandler;
13
import com.google.gwt.event.dom.client.KeyCodes;
14
import com.google.gwt.event.dom.client.KeyPressEvent;
15
import com.google.gwt.event.dom.client.KeyPressHandler;
16
import com.google.gwt.user.client.ui.HasWidgets;
17
import com.google.gwt.user.client.ui.Panel;
18
import com.google.gwt.user.client.ui.RootPanel;
19
import com.google.gwt.user.client.ui.TextBox;
20
 
21
public class RechercheObservationsPresenteur extends Presenteur {
22
 
166 gduche 23
	private List<Observation> observations;
24
 
161 gduche 25
	public RechercheObservationsPresenteur() {
26
		super(new RechercheObservationsVue());
27
	}
28
 
29
	public void go(HasWidgets composite) {
30
		if (composite == null) {
31
			composite = RootPanel.get();
32
		}
33
		composite.add(this.getVue());
34
		handleEvents();
166 gduche 35
 
36
		// On commence par afficher la totalité des observations
37
		chercherObservations(null);
38
		afficherObservations();
161 gduche 39
	}
40
 
41
	protected void handleEvents() {
42
 
43
		RechercheObservationsVue vue = (RechercheObservationsVue) getVue();
44
 
45
		// Gestion du clic dans la zone de saisie
46
		vue.recherchePrincipale.addClickHandler(new ClickHandler() {
47
 
48
			public void onClick(ClickEvent event) {
49
				TextBox recherchePrincipale = (TextBox) event.getSource();
50
				if (recherchePrincipale.getText().equals("Recherche libre")) {
51
					recherchePrincipale.setText("");
52
				}
53
			}
54
		});
55
 
56
		// Gestion de l'affichage de la recherche Avancée
57
		vue.lienRechercheAvancee.addClickHandler(new ClickHandler() {
58
 
59
			public void onClick(ClickEvent event) {
60
				Panel rechercheAvancee = ((RechercheObservationsVue) getVue()).rechercheAvancee;
61
				rechercheAvancee.setVisible(!rechercheAvancee.isVisible());
62
			}
63
		});
64
 
65
		// Gestion de l'évènement recherche sur le clic ou sur l'appui de la
66
		// touche Entrée
67
		vue.boutonRecherche.addClickHandler(new ClickHandler() {
68
 
69
			public void onClick(ClickEvent event) {
166 gduche 70
				chercherObservations(collecterFormulaire());
71
				afficherObservations();
161 gduche 72
			}
73
		});
74
 
75
		vue.boutonRechercheAvancee.addClickHandler(new ClickHandler() {
76
			public void onClick(ClickEvent event) {
166 gduche 77
				chercherObservations(collecterFormulaire());
78
				afficherObservations();
161 gduche 79
			}
80
		});
81
 
82
		vue.recherchePrincipale.addKeyPressHandler(new KeyPressHandler() {
83
 
84
			public void onKeyPress(KeyPressEvent event) {
85
				if (event.getNativeEvent().getKeyCode() == KeyCodes.KEY_ENTER) {
166 gduche 86
					chercherObservations(collecterFormulaire());
87
					afficherObservations();
161 gduche 88
				}
89
			}
90
		});
91
	}
92
 
166 gduche 93
	// Gestion de la recherche d'observations :
94
	public HashMap<String, String> collecterFormulaire() {
95
		RechercheObservationsVue vue = (RechercheObservationsVue) getVue();
161 gduche 96
 
166 gduche 97
		HashMap<String, String> champsRecherche = new HashMap<String, String>();
98
 
99
		if (!vue.recherchePrincipale.getText().equals("")) {
100
			champsRecherche.put("search", vue.recherchePrincipale.getText());
101
		}
102
		if (!vue.departement.getText().equals("")) {
103
			champsRecherche.put("dept", vue.departement.getText());
104
		}
105
		if (!vue.commune.getText().equals("")) {
106
			champsRecherche.put("com", vue.commune.getText());
107
		}
108
		if (!vue.taxon.getText().equals("")) {
109
			champsRecherche.put("taxon", vue.taxon.getText());
110
		}
111
		if (!vue.famille.getText().equals("")) {
112
			champsRecherche.put("fam", vue.famille.getText());
113
		}
114
		if (!vue.genre.getText().equals("")) {
115
			champsRecherche.put("gen", vue.genre.getText());
116
		}
117
		if (!vue.tag.getText().equals("")) {
118
			champsRecherche.put("tag", vue.tag.getText());
119
		}
120
		if (!vue.motCle.getText().equals("")) {
121
			champsRecherche.put("motCle", vue.motCle.getText());
122
		}
123
		if (!vue.auteur.getText().equals("")) {
124
			champsRecherche.put("auteur", vue.auteur.getText());
125
		}
126
		if (!vue.date.getText().equals("")) {
127
			champsRecherche.put("date", vue.date.getText());
128
		}
129
		return champsRecherche;
130
	}
131
 
132
	public void chercherObservations(HashMap<String, String> champsRecherche) {
133
 
161 gduche 134
		RechercheObservationsVue vue = (RechercheObservationsVue) getVue();
135
		vue.rechercheAvancee.setVisible(false);
166 gduche 136
		this.observations = MockDatasource.getInstance().getObservations(champsRecherche);
137
		vue.recherchePrecedente.setText(getChaineRecherche(champsRecherche));
138
	}
161 gduche 139
 
166 gduche 140
	public void afficherObservations() {
141
		RechercheObservationsVue vue = (RechercheObservationsVue) getVue();
161 gduche 142
 
166 gduche 143
		for (Observation observation : observations) {
144
			ObservationPresenteur presenteur = new ObservationPresenteur(observation);
145
			presenteur.go(vue.zoneObservations);
146
		}
161 gduche 147
	}
148
 
149
	private String getChaineRecherche(HashMap<String, String> valeursRecherche) {
150
		String chaineRecherche = "";
166 gduche 151
		if (valeursRecherche != null) {
152
			Iterator<String> itCles = valeursRecherche.keySet().iterator();
153
			while (itCles.hasNext()) {
154
				String cle = itCles.next();
155
				String valeur = valeursRecherche.get(cle);
156
				if (valeur != "") {
157
					chaineRecherche += cle + ":=" + valeur + " ";
158
				}
161 gduche 159
			}
160
		}
161
		return chaineRecherche;
162
	}
163
}