Subversion Repositories eFlore/Applications.coel

Rev

Rev 1246 | Rev 1329 | 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;
1239 cyprien 8
import org.tela_botanica.client.composants.InfoLogger;
985 jpm 9
import org.tela_botanica.client.interfaces.Rafraichissable;
10
import org.tela_botanica.client.modeles.Information;
11
import org.tela_botanica.client.modeles.MenuApplicationId;
989 jpm 12
import org.tela_botanica.client.modeles.commentaire.Commentaire;
999 jpm 13
import org.tela_botanica.client.modeles.projet.Projet;
14
import org.tela_botanica.client.modeles.projet.ProjetListe;
989 jpm 15
import org.tela_botanica.client.util.Debug;
985 jpm 16
import org.tela_botanica.client.util.UtilArray;
17
import org.tela_botanica.client.util.UtilString;
18
import org.tela_botanica.client.vues.Formulaire;
19
 
20
import com.extjs.gxt.ui.client.event.Events;
999 jpm 21
import com.extjs.gxt.ui.client.store.ListStore;
1099 jpm 22
import com.extjs.gxt.ui.client.widget.Info;
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());
1099 jpm 67
		genererTitreFormulaire();
989 jpm 68
		creerChamps();
69
 
985 jpm 70
		if (modeDeCreation.equals(Formulaire.MODE_MODIFIER)) {
989 jpm 71
			mediateur.selectionnerCommentaire(this, commentaireId);
985 jpm 72
		}
73
	}
74
 
1099 jpm 75
	private void genererTitreFormulaire() {
989 jpm 76
		String titre = i18nC.commentaireTitreFormAjout();
985 jpm 77
		if (mode.equals(Formulaire.MODE_MODIFIER)) {
1099 jpm 78
			 titre = i18nC.commentaireTitreFormModif();
79
			 if (commentaire != null) {
80
				 titre += " - "+i18nC.id()+": "+commentaire.getId();
81
			 }
985 jpm 82
		}
1099 jpm 83
		panneauFormulaire.setHeading(titre);
985 jpm 84
	}
85
 
989 jpm 86
	private void creerChamps() {
999 jpm 87
		projetsCombo = new ComboBox<Projet>();
88
		projetsCombo.setTabIndex(tabIndex++);
89
		projetsCombo.setFieldLabel(i18nC.projetChamp());
90
		projetsCombo.setDisplayField("nom");
1077 jpm 91
		projetsCombo.setTriggerAction(TriggerAction.ALL);
92
		projetsCombo.setStore(new ListStore<Projet>());
1082 jpm 93
		projetsCombo.setEmptyText(i18nC.txtListeProjetDefaut());
94
		projetsCombo.setEditable(false);
999 jpm 95
		projetsCombo.setForceSelection(true);
1077 jpm 96
		projetsCombo.setAllowBlank(false);
999 jpm 97
		projetsCombo.setValidator(new Validator() {
1077 jpm 98
			public String validate(Field<?> champ, String valeurAValider) {
999 jpm 99
				String retour = null;
1077 jpm 100
				if (UtilString.isEmpty(valeurAValider)
101
						|| projetsCombo.getStore().findModel("nom", valeurAValider) == null) {
102
					champ.setValue(null);
103
					retour = i18nC.selectionnerValeur();
999 jpm 104
				}
105
				return retour;
106
			}
107
		});
108
		projetsCombo.addStyleName(ComposantClass.OBLIGATOIRE);
1077 jpm 109
		projetsCombo.addListener(Events.Valid, creerEcouteurChampObligatoire());
999 jpm 110
		panneauFormulaire.add(projetsCombo, new FormData(450, 0));
1322 gduche 111
		mediateur.selectionnerProjet(this, null, null);
999 jpm 112
 
985 jpm 113
		titreChp = new TextField<String>();
989 jpm 114
		titreChp.setFieldLabel(i18nC.commentaireTitre());
1077 jpm 115
		titreChp.setAllowBlank(false);
985 jpm 116
		titreChp.addStyleName(ComposantClass.OBLIGATOIRE);
117
		titreChp.addListener(Events.Valid, creerEcouteurChampObligatoire());
1077 jpm 118
		titreChp.addListener(Events.Invalid, creerEcouteurChampObligatoire());
989 jpm 119
		panneauFormulaire.add(titreChp, new FormData(450, 0));
985 jpm 120
 
989 jpm 121
		texteChp = new TextArea();
122
		texteChp.setFieldLabel(i18nC.commentaireTexte());
123
		panneauFormulaire.add(texteChp, new FormData(450, 250));
985 jpm 124
 
989 jpm 125
		ponderationChp = new ChampSliderPourcentage(i18nC.commentairePonderation());
126
		panneauFormulaire.add(ponderationChp, new FormData(450, 0));
127
 
128
		publicChp = new CheckBox();
129
		publicChp.setFieldLabel(i18nC.donneePublic());
130
		panneauFormulaire.add(publicChp, new FormData(50, 0));
985 jpm 131
	}
132
 
133
	public void rafraichir(Object nouvellesDonnees) {
989 jpm 134
		if (nouvellesDonnees instanceof Commentaire) {
985 jpm 135
			// Si on a reçu les details d'une publication
989 jpm 136
			rafraichirCommentaire((Commentaire) nouvellesDonnees);
999 jpm 137
		} else if (nouvellesDonnees instanceof ProjetListe) {
138
			ProjetListe projets = (ProjetListe) nouvellesDonnees;
139
			Formulaire.rafraichirComboBox(projets, projetsCombo);
140
			setValeurComboProjets();
985 jpm 141
		} else if (nouvellesDonnees instanceof Information) {
142
			rafraichirInformation((Information) nouvellesDonnees);
143
		} else {
989 jpm 144
			Debug.log(Mediateur.i18nM.erreurRafraichir(nouvellesDonnees.getClass(), this.getClass()));
985 jpm 145
		}
146
 
147
		if (etreValide()) {
148
			initialiserValidation();
149
			repandreRafraichissement();
1239 cyprien 150
			controlerFermeture();
985 jpm 151
		}
152
	}
153
 
989 jpm 154
	private void rafraichirCommentaire(Commentaire commentaireRecu) {
155
		commentaire = commentaireRecu;
985 jpm 156
		peuplerFormulaire();
1100 jpm 157
		genererTitreFormulaire();
985 jpm 158
	}
159
 
999 jpm 160
	private String getValeurComboProjets() {
161
		String valeur = "";
1077 jpm 162
		if (projetsCombo.getValue() != null && projetsCombo.isValid()) {
999 jpm 163
			valeur = projetsCombo.getValue().getId();
164
		}
165
		return valeur;
166
	}
167
	private void setValeurComboProjets() {
168
		if (projetsCombo.getStore() != null ) {
169
			if (mode.equals(Formulaire.MODE_MODIFIER) && commentaire != null) {
170
				projetsCombo.setValue(projetsCombo.getStore().findModel("id_projet", commentaire.getIdProjet()));
171
			} else if (mode.equals(Formulaire.MODE_AJOUTER)) {
172
				projetsCombo.setValue(projetsCombo.getStore().findModel("id_projet", mediateur.getProjetId()));
173
			}
174
		}
175
	}
176
 
985 jpm 177
	private void rafraichirInformation(Information info) {
1099 jpm 178
		// Gestion des messages d'erreur
179
		if (info.getMessages() != null && !info.getMessages().toString().equals("[]")) {
180
			Debug.log("MESSAGES:\n"+info.getMessages().toString());
181
		}
182
 
183
		// Gestion des actions
989 jpm 184
		String type = info.getType();
185
		if (type.equals("ajout_commentaire") || type.equals("modif_commentaire")) {
186
			commentaireValideOk = true;
985 jpm 187
		}
1099 jpm 188
		if (info.getType().equals("ajout_commentaire")) {
189
			if (vueExterneARafraichirApresValidation != null) {
190
				String noteId = (String) info.getDonnee(0);
191
				commentaire.setId(noteId);
192
			}
193
		}
194
 
195
		// Gestion des messages
196
		if (info.getType().equals("modif_commentaire")) {
1239 cyprien 197
			InfoLogger.display("Modification d'une note", info.toString());
1099 jpm 198
		} else if (info.getType().equals("ajout_commentaire")) {
199
			if (info.getDonnee(0) != null && info.getDonnee(0) instanceof String) {
200
				String noteId = (String) info.getDonnee(0);
1239 cyprien 201
				InfoLogger.display("Ajout d'une note", "La note '"+noteId+"' a bien été ajoutée");
1099 jpm 202
			} else {
1239 cyprien 203
				InfoLogger.display("Ajout d'une note", info.toString());
1099 jpm 204
			}
205
		}
985 jpm 206
	}
207
 
208
	private Boolean etreValide() {
209
		Boolean valide = false;
989 jpm 210
		Debug.log("formulaire"+formulaireValideOk+" - Commentaire :"+commentaireValideOk);
211
		if (formulaireValideOk && commentaireValideOk) {
985 jpm 212
			valide = true;
213
		}
214
		return valide;
215
	}
216
 
217
	private void initialiserValidation() {
218
		formulaireValideOk = false;
989 jpm 219
		commentaireValideOk = false;
985 jpm 220
	}
221
 
222
	private void repandreRafraichissement() {
223
		if (vueExterneARafraichirApresValidation != null) {
989 jpm 224
			String type = "commentaire_modifiee";
985 jpm 225
			if (mode.equals(Formulaire.MODE_AJOUTER)) {
989 jpm 226
				type = "commentaire_ajoutee";
985 jpm 227
			}
228
			Information info = new Information(type);
989 jpm 229
			info.setDonnee(0, commentaire);
985 jpm 230
			vueExterneARafraichirApresValidation.rafraichir(info);
231
		}
232
	}
233
 
234
	public boolean soumettreFormulaire() {
235
		formulaireValideOk = verifierFormulaire();
236
		if (formulaireValideOk) {
989 jpm 237
			soumettreCommentaire();
985 jpm 238
		}
239
		return formulaireValideOk;
240
	}
241
 
989 jpm 242
	private void soumettreCommentaire() {
243
		Commentaire commentaireCollectee = collecterCommentaire();
244
		if (commentaireCollectee != null) {
985 jpm 245
			if (mode.equals(Formulaire.MODE_AJOUTER)) {
989 jpm 246
				mediateur.ajouterCommentaire(this, commentaireCollectee);
985 jpm 247
			} else if (mode.equals(Formulaire.MODE_MODIFIER)) {
989 jpm 248
				mediateur.modifierCommentaire(this, commentaireCollectee);
985 jpm 249
			}
250
		}
251
	}
252
 
253
	public boolean verifierFormulaire() {
254
		boolean valide = true;
255
		ArrayList<String> messages = new ArrayList<String>();
256
 
257
		String titre = titreChp.getValue();
258
		if (titre == null || titre.equals("")) {
989 jpm 259
			messages.add(i18nC.commentaireMessageTitre());
985 jpm 260
		}
261
 
1077 jpm 262
		if (UtilString.isEmpty(getValeurComboProjets())) {
263
			String selectionDe = i18nC.articleUn()+" "+i18nC.projetSingulier();
264
			String pour = i18nC.articleLa()+" "+i18nC.commentaireSingulier();
265
			messages.add(i18nM.selectionObligatoire(selectionDe, pour));
266
		}
267
 
985 jpm 268
		if (messages.size() != 0) {
269
			String[] tableauDeMessages = {};
270
			tableauDeMessages = messages.toArray(tableauDeMessages);
989 jpm 271
			MessageBox.alert(i18nC.erreurSaisieTitre(), UtilArray.implode(tableauDeMessages, "<br />"), null);
985 jpm 272
			valide = false;
273
		}
274
		return valide;
275
	}
276
 
277
	private void peuplerFormulaire() {
999 jpm 278
		setValeurComboProjets();
989 jpm 279
		titreChp.setValue(commentaire.getTitre());
280
		texteChp.setValue(commentaire.getTexte());
281
		ponderationChp.peupler(commentaire.getPonderation());
282
		boolean acces = (commentaire.etrePublic() ? true : false);
283
		publicChp.setValue(acces);
985 jpm 284
	}
285
 
989 jpm 286
	private Commentaire collecterCommentaire() {
287
		Commentaire commentaireCollectee = (Commentaire) commentaire.cloner(new Commentaire());
985 jpm 288
 
999 jpm 289
		commentaireCollectee.setIdProjet(getValeurComboProjets());
290
 
985 jpm 291
		String titre = titreChp.getValue();
989 jpm 292
		commentaireCollectee.setTitre(titre);
985 jpm 293
 
989 jpm 294
		String texte = texteChp.getValue();
295
		commentaireCollectee.setTexte(texte);
985 jpm 296
 
989 jpm 297
		String ponderation = ponderationChp.getValeur();
298
		commentaireCollectee.setPonderation(ponderation);
985 jpm 299
 
989 jpm 300
		String acces = (publicChp.getValue() ? "1" : "0");
301
		commentaireCollectee.setPublic(acces);
985 jpm 302
 
989 jpm 303
		Commentaire commentaireARetourner = null;
304
		if (!commentaireCollectee.comparer(commentaire)) {
305
			commentaireARetourner = commentaire = commentaireCollectee;
985 jpm 306
		}
989 jpm 307
		return commentaireARetourner;
985 jpm 308
	}
309
 
310
	public void reinitialiserFormulaire() {
311
		if (mode.equals(Formulaire.MODE_MODIFIER)) {
989 jpm 312
			mediateur.afficherFormPublication(commentaire.getId());
985 jpm 313
		} else {
314
			mediateur.afficherFormPublication(null);
315
		}
316
	}
317
}