Subversion Repositories eFlore/Applications.coel

Rev

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

Rev Author Line No. Line
985 jpm 1
package org.tela_botanica.client.vues.commentaire;
2
 
3
import java.util.ArrayList;
4
 
5
import org.tela_botanica.client.ComposantClass;
6
import org.tela_botanica.client.Mediateur;
989 jpm 7
import org.tela_botanica.client.composants.ChampSliderPourcentage;
985 jpm 8
import org.tela_botanica.client.interfaces.Rafraichissable;
9
import org.tela_botanica.client.modeles.Information;
10
import org.tela_botanica.client.modeles.MenuApplicationId;
989 jpm 11
import org.tela_botanica.client.modeles.commentaire.Commentaire;
999 jpm 12
import org.tela_botanica.client.modeles.projet.Projet;
13
import org.tela_botanica.client.modeles.projet.ProjetListe;
989 jpm 14
import org.tela_botanica.client.util.Debug;
985 jpm 15
import org.tela_botanica.client.util.UtilArray;
16
import org.tela_botanica.client.util.UtilString;
17
import org.tela_botanica.client.vues.Formulaire;
18
 
999 jpm 19
import com.extjs.gxt.ui.client.event.BaseEvent;
985 jpm 20
import com.extjs.gxt.ui.client.event.Events;
999 jpm 21
import com.extjs.gxt.ui.client.event.Listener;
22
import com.extjs.gxt.ui.client.store.ListStore;
985 jpm 23
import com.extjs.gxt.ui.client.widget.MessageBox;
989 jpm 24
import com.extjs.gxt.ui.client.widget.form.CheckBox;
999 jpm 25
import com.extjs.gxt.ui.client.widget.form.ComboBox;
26
import com.extjs.gxt.ui.client.widget.form.Field;
989 jpm 27
import com.extjs.gxt.ui.client.widget.form.TextArea;
985 jpm 28
import com.extjs.gxt.ui.client.widget.form.TextField;
999 jpm 29
import com.extjs.gxt.ui.client.widget.form.Validator;
30
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;
985 jpm 31
import com.extjs.gxt.ui.client.widget.layout.FormData;
32
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
33
 
34
 
35
public class CommentaireForm extends Formulaire implements Rafraichissable {
36
 
989 jpm 37
	private Commentaire commentaire;
38
 
999 jpm 39
	private ComboBox<Projet> projetsCombo = null;
989 jpm 40
	private TextField<String> titreChp;
41
	private TextArea texteChp;
42
	private ChampSliderPourcentage ponderationChp;
43
	private CheckBox publicChp;
985 jpm 44
 
45
	private static boolean formulaireValideOk = false;
989 jpm 46
	private static boolean commentaireValideOk = false;
985 jpm 47
 
989 jpm 48
	public CommentaireForm(Mediateur mediateurCourrant, String commentaireId) {
49
		initialiserCommentaireForm(mediateurCourrant, commentaireId);
985 jpm 50
	}
51
 
989 jpm 52
	public CommentaireForm(Mediateur mediateurCourrant, String commentaireId, Rafraichissable vueARafraichirApresValidation) {
985 jpm 53
		vueExterneARafraichirApresValidation = vueARafraichirApresValidation;
989 jpm 54
		initialiserCommentaireForm(mediateurCourrant, commentaireId);
985 jpm 55
	}
56
 
989 jpm 57
	private void initialiserCommentaireForm(Mediateur mediateurCourrant, String commentaireId) {
985 jpm 58
		initialiserValidation();
59
 
989 jpm 60
		commentaire = new Commentaire();
61
		commentaire.setId(commentaireId);
985 jpm 62
 
989 jpm 63
		String modeDeCreation = (UtilString.isEmpty(commentaire.getId()) ? Formulaire.MODE_AJOUTER : Formulaire.MODE_MODIFIER);
64
		initialiserFormulaire(mediateurCourrant, modeDeCreation, MenuApplicationId.COMMENTAIRE);
985 jpm 65
 
989 jpm 66
		panneauFormulaire.setLayout(new FormLayout());
985 jpm 67
		String titre = genererTitreFormulaire();
68
		panneauFormulaire.setHeading(titre);
69
 
989 jpm 70
		creerChamps();
71
 
985 jpm 72
		if (modeDeCreation.equals(Formulaire.MODE_MODIFIER)) {
989 jpm 73
			mediateur.selectionnerCommentaire(this, commentaireId);
985 jpm 74
		}
75
	}
76
 
77
	private String genererTitreFormulaire() {
989 jpm 78
		String titre = i18nC.commentaireTitreFormAjout();
985 jpm 79
		if (mode.equals(Formulaire.MODE_MODIFIER)) {
989 jpm 80
			 titre = i18nC.commentaireTitreFormModif()+" - "+i18nC.id()+": "+commentaire.getId();
985 jpm 81
		}
82
		return titre;
83
	}
84
 
989 jpm 85
	private void creerChamps() {
999 jpm 86
		projetsCombo = new ComboBox<Projet>();
87
		projetsCombo.setTabIndex(tabIndex++);
88
		projetsCombo.setFieldLabel(i18nC.projetChamp());
89
		projetsCombo.setDisplayField("nom");
90
		projetsCombo.setForceSelection(true);
91
		projetsCombo.setValidator(new Validator() {
92
			@Override
93
			public String validate(Field<?> field, String value) {
94
				String retour = null;
95
				if (field.getRawValue().equals("")) {
96
					field.setValue(null);
97
				} else if (projetsCombo.getStore().findModel("nom", field.getRawValue()) == null) {
98
					String contenuBrut = field.getRawValue();
99
					field.setValue(null);
100
					field.setRawValue(contenuBrut);
101
					retour = "Veuillez sélectionner une valeur ou laisser le champ vide";
102
				}
103
				return retour;
104
			}
105
		});
106
		projetsCombo.setTriggerAction(TriggerAction.ALL);
107
		projetsCombo.setStore(new ListStore<Projet>());
108
		projetsCombo.addStyleName(ComposantClass.OBLIGATOIRE);
109
		projetsCombo.addListener(Events.Valid, new Listener<BaseEvent>() {
110
			@Override
111
			public void handleEvent(BaseEvent be) {
112
				Projet valeur = projetsCombo.getValue();
113
				// Gestion du style obligatoire
114
				projetsCombo.removeStyleName(ComposantClass.OBLIGATOIRE);
115
				projetsCombo.removeStyleName(ComposantClass.OBLIGATOIRE_OK);
116
				if (valeur != null) {
117
					projetsCombo.addStyleName(ComposantClass.OBLIGATOIRE_OK);
118
				} else {
119
					projetsCombo.addStyleName(ComposantClass.OBLIGATOIRE);
120
				}
121
			}
122
		});
123
		panneauFormulaire.add(projetsCombo, new FormData(450, 0));
124
		mediateur.selectionnerProjet(this, null);
125
 
985 jpm 126
		titreChp = new TextField<String>();
989 jpm 127
		titreChp.setFieldLabel(i18nC.commentaireTitre());
985 jpm 128
		titreChp.addStyleName(ComposantClass.OBLIGATOIRE);
129
		titreChp.addListener(Events.Valid, creerEcouteurChampObligatoire());
989 jpm 130
		panneauFormulaire.add(titreChp, new FormData(450, 0));
985 jpm 131
 
989 jpm 132
		texteChp = new TextArea();
133
		texteChp.setFieldLabel(i18nC.commentaireTexte());
134
		panneauFormulaire.add(texteChp, new FormData(450, 250));
985 jpm 135
 
989 jpm 136
		ponderationChp = new ChampSliderPourcentage(i18nC.commentairePonderation());
137
		panneauFormulaire.add(ponderationChp, new FormData(450, 0));
138
 
139
		publicChp = new CheckBox();
140
		publicChp.setFieldLabel(i18nC.donneePublic());
141
		panneauFormulaire.add(publicChp, new FormData(50, 0));
985 jpm 142
	}
143
 
144
	public void rafraichir(Object nouvellesDonnees) {
989 jpm 145
		if (nouvellesDonnees instanceof Commentaire) {
985 jpm 146
			// Si on a reçu les details d'une publication
989 jpm 147
			rafraichirCommentaire((Commentaire) nouvellesDonnees);
999 jpm 148
		} else if (nouvellesDonnees instanceof ProjetListe) {
149
			ProjetListe projets = (ProjetListe) nouvellesDonnees;
150
			Formulaire.rafraichirComboBox(projets, projetsCombo);
151
			setValeurComboProjets();
985 jpm 152
		} else if (nouvellesDonnees instanceof Information) {
153
			rafraichirInformation((Information) nouvellesDonnees);
154
		} else {
989 jpm 155
			Debug.log(Mediateur.i18nM.erreurRafraichir(nouvellesDonnees.getClass(), this.getClass()));
985 jpm 156
		}
157
 
158
		if (etreValide()) {
159
			initialiserValidation();
160
			repandreRafraichissement();
161
			controlerFermetureApresRafraichissement();
162
		}
163
	}
164
 
989 jpm 165
	private void rafraichirCommentaire(Commentaire commentaireRecu) {
166
		commentaire = commentaireRecu;
985 jpm 167
		peuplerFormulaire();
168
	}
169
 
999 jpm 170
	private String getValeurComboProjets() {
171
		String valeur = "";
172
		if (projetsCombo.getValue() != null) {
173
			valeur = projetsCombo.getValue().getId();
174
		}
175
		return valeur;
176
	}
177
	private void setValeurComboProjets() {
178
		if (projetsCombo.getStore() != null ) {
179
			if (mode.equals(Formulaire.MODE_MODIFIER) && commentaire != null) {
180
				projetsCombo.setValue(projetsCombo.getStore().findModel("id_projet", commentaire.getIdProjet()));
181
			} else if (mode.equals(Formulaire.MODE_AJOUTER)) {
182
				projetsCombo.setValue(projetsCombo.getStore().findModel("id_projet", mediateur.getProjetId()));
183
			}
184
		}
185
	}
186
 
985 jpm 187
	private void rafraichirInformation(Information info) {
989 jpm 188
		String type = info.getType();
189
		if (type.equals("ajout_commentaire") || type.equals("modif_commentaire")) {
190
			commentaireValideOk = true;
191
			commentaire.setId((String) info.getDonnee(0));
985 jpm 192
		}
193
	}
194
 
195
	private Boolean etreValide() {
196
		Boolean valide = false;
989 jpm 197
		Debug.log("formulaire"+formulaireValideOk+" - Commentaire :"+commentaireValideOk);
198
		if (formulaireValideOk && commentaireValideOk) {
985 jpm 199
			valide = true;
200
		}
201
		return valide;
202
	}
203
 
204
	private void initialiserValidation() {
205
		formulaireValideOk = false;
989 jpm 206
		commentaireValideOk = false;
985 jpm 207
	}
208
 
209
	private void repandreRafraichissement() {
210
		if (vueExterneARafraichirApresValidation != null) {
989 jpm 211
			String type = "commentaire_modifiee";
985 jpm 212
			if (mode.equals(Formulaire.MODE_AJOUTER)) {
989 jpm 213
				type = "commentaire_ajoutee";
985 jpm 214
			}
215
			Information info = new Information(type);
989 jpm 216
			info.setDonnee(0, commentaire);
985 jpm 217
			vueExterneARafraichirApresValidation.rafraichir(info);
218
		}
219
	}
220
 
221
	public boolean soumettreFormulaire() {
222
		formulaireValideOk = verifierFormulaire();
989 jpm 223
		Debug.log("Form?"+formulaireValideOk);
985 jpm 224
		if (formulaireValideOk) {
989 jpm 225
			soumettreCommentaire();
985 jpm 226
		}
227
		return formulaireValideOk;
228
	}
229
 
989 jpm 230
	private void soumettreCommentaire() {
231
		Commentaire commentaireCollectee = collecterCommentaire();
232
		if (commentaireCollectee != null) {
233
			Debug.log("Info public collectée ? ok");
985 jpm 234
			if (mode.equals(Formulaire.MODE_AJOUTER)) {
989 jpm 235
				mediateur.ajouterCommentaire(this, commentaireCollectee);
985 jpm 236
			} else if (mode.equals(Formulaire.MODE_MODIFIER)) {
989 jpm 237
				mediateur.modifierCommentaire(this, commentaireCollectee);
985 jpm 238
			}
239
		}
240
	}
241
 
242
	public boolean verifierFormulaire() {
243
		boolean valide = true;
244
		ArrayList<String> messages = new ArrayList<String>();
245
 
246
		String titre = titreChp.getValue();
247
		if (titre == null || titre.equals("")) {
989 jpm 248
			messages.add(i18nC.commentaireMessageTitre());
985 jpm 249
		}
250
 
251
		if (messages.size() != 0) {
252
			String[] tableauDeMessages = {};
253
			tableauDeMessages = messages.toArray(tableauDeMessages);
989 jpm 254
			MessageBox.alert(i18nC.erreurSaisieTitre(), UtilArray.implode(tableauDeMessages, "<br />"), null);
985 jpm 255
			valide = false;
256
		}
257
		return valide;
258
	}
259
 
260
	private void peuplerFormulaire() {
999 jpm 261
		setValeurComboProjets();
989 jpm 262
		titreChp.setValue(commentaire.getTitre());
263
		texteChp.setValue(commentaire.getTexte());
264
		ponderationChp.peupler(commentaire.getPonderation());
265
		boolean acces = (commentaire.etrePublic() ? true : false);
266
		publicChp.setValue(acces);
985 jpm 267
	}
268
 
989 jpm 269
	private Commentaire collecterCommentaire() {
270
		Commentaire commentaireCollectee = (Commentaire) commentaire.cloner(new Commentaire());
985 jpm 271
 
999 jpm 272
		commentaireCollectee.setIdProjet(getValeurComboProjets());
273
 
985 jpm 274
		String titre = titreChp.getValue();
989 jpm 275
		commentaireCollectee.setTitre(titre);
985 jpm 276
 
989 jpm 277
		String texte = texteChp.getValue();
278
		commentaireCollectee.setTexte(texte);
985 jpm 279
 
989 jpm 280
		String ponderation = ponderationChp.getValeur();
281
		commentaireCollectee.setPonderation(ponderation);
985 jpm 282
 
989 jpm 283
		String acces = (publicChp.getValue() ? "1" : "0");
284
		commentaireCollectee.setPublic(acces);
985 jpm 285
 
989 jpm 286
		Commentaire commentaireARetourner = null;
287
		if (!commentaireCollectee.comparer(commentaire)) {
288
			commentaireARetourner = commentaire = commentaireCollectee;
985 jpm 289
		}
989 jpm 290
		return commentaireARetourner;
985 jpm 291
	}
292
 
293
	public void reinitialiserFormulaire() {
294
		if (mode.equals(Formulaire.MODE_MODIFIER)) {
989 jpm 295
			mediateur.afficherFormPublication(commentaire.getId());
985 jpm 296
		} else {
297
			mediateur.afficherFormPublication(null);
298
		}
299
	}
300
}