Subversion Repositories eFlore/Applications.coel

Rev

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