Subversion Repositories eFlore/Applications.coel

Rev

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