Subversion Repositories eFlore/Applications.coel

Rev

Rev 1415 | Rev 1468 | 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().setValidator(new Validator() {
1077 jpm 113
			public String validate(Field<?> champ, String valeurAValider) {
999 jpm 114
				String retour = null;
1077 jpm 115
				if (UtilString.isEmpty(valeurAValider)
1329 cyprien 116
						|| projetsCombo.getStore().findModel("cpr_nom", valeurAValider) == null) {
1077 jpm 117
					champ.setValue(null);
118
					retour = i18nC.selectionnerValeur();
999 jpm 119
				}
120
				return retour;
121
			}
122
		});
1329 cyprien 123
 
999 jpm 124
		panneauFormulaire.add(projetsCombo, new FormData(450, 0));
125
 
985 jpm 126
		titreChp = new TextField<String>();
989 jpm 127
		titreChp.setFieldLabel(i18nC.commentaireTitre());
1077 jpm 128
		titreChp.setAllowBlank(false);
985 jpm 129
		titreChp.addStyleName(ComposantClass.OBLIGATOIRE);
130
		titreChp.addListener(Events.Valid, creerEcouteurChampObligatoire());
1077 jpm 131
		titreChp.addListener(Events.Invalid, creerEcouteurChampObligatoire());
989 jpm 132
		panneauFormulaire.add(titreChp, new FormData(450, 0));
985 jpm 133
 
989 jpm 134
		texteChp = new TextArea();
135
		texteChp.setFieldLabel(i18nC.commentaireTexte());
136
		panneauFormulaire.add(texteChp, new FormData(450, 250));
985 jpm 137
 
989 jpm 138
		ponderationChp = new ChampSliderPourcentage(i18nC.commentairePonderation());
139
		panneauFormulaire.add(ponderationChp, new FormData(450, 0));
140
 
141
		publicChp = new CheckBox();
142
		publicChp.setFieldLabel(i18nC.donneePublic());
143
		panneauFormulaire.add(publicChp, new FormData(50, 0));
985 jpm 144
	}
145
 
146
	public void rafraichir(Object nouvellesDonnees) {
989 jpm 147
		if (nouvellesDonnees instanceof Commentaire) {
985 jpm 148
			// Si on a reçu les details d'une publication
989 jpm 149
			rafraichirCommentaire((Commentaire) nouvellesDonnees);
985 jpm 150
		} else if (nouvellesDonnees instanceof Information) {
151
			rafraichirInformation((Information) nouvellesDonnees);
152
		} else {
989 jpm 153
			Debug.log(Mediateur.i18nM.erreurRafraichir(nouvellesDonnees.getClass(), this.getClass()));
985 jpm 154
		}
155
 
156
		if (etreValide()) {
157
			initialiserValidation();
158
			repandreRafraichissement();
1239 cyprien 159
			controlerFermeture();
985 jpm 160
		}
161
	}
162
 
989 jpm 163
	private void rafraichirCommentaire(Commentaire commentaireRecu) {
164
		commentaire = commentaireRecu;
985 jpm 165
		peuplerFormulaire();
1100 jpm 166
		genererTitreFormulaire();
985 jpm 167
	}
168
 
999 jpm 169
	private String getValeurComboProjets() {
170
		String valeur = "";
1329 cyprien 171
		if (projetsCombo.getCombo().getValue() != null && projetsCombo.getCombo().isValid()) {
172
			Projet projet = new Projet (projetsCombo.getValeur());
173
			valeur = projet.getId();
999 jpm 174
		}
175
		return valeur;
176
	}
177
	private void setValeurComboProjets() {
178
		if (projetsCombo.getStore() != null ) {
179
			if (mode.equals(Formulaire.MODE_MODIFIER) && commentaire != null) {
1329 cyprien 180
				projetsCombo.getCombo().setValue(projetsCombo.getStore().findModel("cpr_id_projet", commentaire.getIdProjet()));
999 jpm 181
			} else if (mode.equals(Formulaire.MODE_AJOUTER)) {
1329 cyprien 182
				projetsCombo.getCombo().setValue(projetsCombo.getStore().findModel("cpr_id_projet", mediateur.getProjetId()));
999 jpm 183
			}
184
		}
185
	}
186
 
985 jpm 187
	private void rafraichirInformation(Information info) {
1099 jpm 188
		// Gestion des messages d'erreur
189
		if (info.getMessages() != null && !info.getMessages().toString().equals("[]")) {
190
			Debug.log("MESSAGES:\n"+info.getMessages().toString());
191
		}
192
 
193
		// Gestion des actions
989 jpm 194
		String type = info.getType();
195
		if (type.equals("ajout_commentaire") || type.equals("modif_commentaire")) {
196
			commentaireValideOk = true;
985 jpm 197
		}
1099 jpm 198
		if (info.getType().equals("ajout_commentaire")) {
199
			if (vueExterneARafraichirApresValidation != null) {
200
				String noteId = (String) info.getDonnee(0);
201
				commentaire.setId(noteId);
202
			}
203
		}
204
 
205
		// Gestion des messages
206
		if (info.getType().equals("modif_commentaire")) {
1239 cyprien 207
			InfoLogger.display("Modification d'une note", info.toString());
1099 jpm 208
		} else if (info.getType().equals("ajout_commentaire")) {
209
			if (info.getDonnee(0) != null && info.getDonnee(0) instanceof String) {
210
				String noteId = (String) info.getDonnee(0);
1239 cyprien 211
				InfoLogger.display("Ajout d'une note", "La note '"+noteId+"' a bien été ajoutée");
1099 jpm 212
			} else {
1239 cyprien 213
				InfoLogger.display("Ajout d'une note", info.toString());
1099 jpm 214
			}
215
		}
985 jpm 216
	}
217
 
218
	private Boolean etreValide() {
219
		Boolean valide = false;
989 jpm 220
		if (formulaireValideOk && commentaireValideOk) {
985 jpm 221
			valide = true;
222
		}
223
		return valide;
224
	}
225
 
226
	private void initialiserValidation() {
227
		formulaireValideOk = false;
989 jpm 228
		commentaireValideOk = false;
985 jpm 229
	}
230
 
231
	private void repandreRafraichissement() {
232
		if (vueExterneARafraichirApresValidation != null) {
989 jpm 233
			String type = "commentaire_modifiee";
985 jpm 234
			if (mode.equals(Formulaire.MODE_AJOUTER)) {
989 jpm 235
				type = "commentaire_ajoutee";
985 jpm 236
			}
237
			Information info = new Information(type);
989 jpm 238
			info.setDonnee(0, commentaire);
985 jpm 239
			vueExterneARafraichirApresValidation.rafraichir(info);
240
		}
241
	}
242
 
243
	public boolean soumettreFormulaire() {
244
		formulaireValideOk = verifierFormulaire();
245
		if (formulaireValideOk) {
989 jpm 246
			soumettreCommentaire();
985 jpm 247
		}
248
		return formulaireValideOk;
249
	}
250
 
989 jpm 251
	private void soumettreCommentaire() {
252
		Commentaire commentaireCollectee = collecterCommentaire();
253
		if (commentaireCollectee != null) {
985 jpm 254
			if (mode.equals(Formulaire.MODE_AJOUTER)) {
989 jpm 255
				mediateur.ajouterCommentaire(this, commentaireCollectee);
985 jpm 256
			} else if (mode.equals(Formulaire.MODE_MODIFIER)) {
989 jpm 257
				mediateur.modifierCommentaire(this, commentaireCollectee);
985 jpm 258
			}
259
		}
260
	}
261
 
262
	public boolean verifierFormulaire() {
263
		boolean valide = true;
264
		ArrayList<String> messages = new ArrayList<String>();
265
 
266
		String titre = titreChp.getValue();
267
		if (titre == null || titre.equals("")) {
989 jpm 268
			messages.add(i18nC.commentaireMessageTitre());
985 jpm 269
		}
270
 
1077 jpm 271
		if (UtilString.isEmpty(getValeurComboProjets())) {
272
			String selectionDe = i18nC.articleUn()+" "+i18nC.projetSingulier();
273
			String pour = i18nC.articleLa()+" "+i18nC.commentaireSingulier();
274
			messages.add(i18nM.selectionObligatoire(selectionDe, pour));
275
		}
276
 
985 jpm 277
		if (messages.size() != 0) {
278
			String[] tableauDeMessages = {};
279
			tableauDeMessages = messages.toArray(tableauDeMessages);
989 jpm 280
			MessageBox.alert(i18nC.erreurSaisieTitre(), UtilArray.implode(tableauDeMessages, "<br />"), null);
985 jpm 281
			valide = false;
282
		}
283
		return valide;
284
	}
285
 
286
	private void peuplerFormulaire() {
999 jpm 287
		setValeurComboProjets();
989 jpm 288
		titreChp.setValue(commentaire.getTitre());
289
		texteChp.setValue(commentaire.getTexte());
290
		ponderationChp.peupler(commentaire.getPonderation());
291
		boolean acces = (commentaire.etrePublic() ? true : false);
292
		publicChp.setValue(acces);
985 jpm 293
	}
294
 
989 jpm 295
	private Commentaire collecterCommentaire() {
296
		Commentaire commentaireCollectee = (Commentaire) commentaire.cloner(new Commentaire());
985 jpm 297
 
999 jpm 298
		commentaireCollectee.setIdProjet(getValeurComboProjets());
299
 
985 jpm 300
		String titre = titreChp.getValue();
989 jpm 301
		commentaireCollectee.setTitre(titre);
985 jpm 302
 
989 jpm 303
		String texte = texteChp.getValue();
304
		commentaireCollectee.setTexte(texte);
985 jpm 305
 
989 jpm 306
		String ponderation = ponderationChp.getValeur();
307
		commentaireCollectee.setPonderation(ponderation);
985 jpm 308
 
989 jpm 309
		String acces = (publicChp.getValue() ? "1" : "0");
310
		commentaireCollectee.setPublic(acces);
985 jpm 311
 
989 jpm 312
		Commentaire commentaireARetourner = null;
313
		if (!commentaireCollectee.comparer(commentaire)) {
314
			commentaireARetourner = commentaire = commentaireCollectee;
985 jpm 315
		}
989 jpm 316
		return commentaireARetourner;
985 jpm 317
	}
318
 
319
	public void reinitialiserFormulaire() {
320
		if (mode.equals(Formulaire.MODE_MODIFIER)) {
989 jpm 321
			mediateur.afficherFormPublication(commentaire.getId());
985 jpm 322
		} else {
323
			mediateur.afficherFormPublication(null);
324
		}
325
	}
326
}