Subversion Repositories eFlore/Applications.coel

Rev

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