Subversion Repositories eFlore/Applications.coel

Rev

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