Subversion Repositories eFlore/Applications.coel

Rev

Rev 1032 | Rev 1082 | 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;
989 jpm 7
import org.tela_botanica.client.composants.ChampSliderPourcentage;
985 jpm 8
import org.tela_botanica.client.interfaces.Rafraichissable;
9
import org.tela_botanica.client.modeles.Information;
10
import org.tela_botanica.client.modeles.MenuApplicationId;
989 jpm 11
import org.tela_botanica.client.modeles.commentaire.Commentaire;
999 jpm 12
import org.tela_botanica.client.modeles.projet.Projet;
13
import org.tela_botanica.client.modeles.projet.ProjetListe;
989 jpm 14
import org.tela_botanica.client.util.Debug;
985 jpm 15
import org.tela_botanica.client.util.UtilArray;
16
import org.tela_botanica.client.util.UtilString;
17
import org.tela_botanica.client.vues.Formulaire;
18
 
999 jpm 19
import com.extjs.gxt.ui.client.event.BaseEvent;
985 jpm 20
import com.extjs.gxt.ui.client.event.Events;
999 jpm 21
import com.extjs.gxt.ui.client.event.Listener;
22
import com.extjs.gxt.ui.client.store.ListStore;
985 jpm 23
import com.extjs.gxt.ui.client.widget.MessageBox;
989 jpm 24
import com.extjs.gxt.ui.client.widget.form.CheckBox;
999 jpm 25
import com.extjs.gxt.ui.client.widget.form.ComboBox;
26
import com.extjs.gxt.ui.client.widget.form.Field;
989 jpm 27
import com.extjs.gxt.ui.client.widget.form.TextArea;
985 jpm 28
import com.extjs.gxt.ui.client.widget.form.TextField;
999 jpm 29
import com.extjs.gxt.ui.client.widget.form.Validator;
30
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;
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
 
999 jpm 39
	private ComboBox<Projet> 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;
985 jpm 47
 
989 jpm 48
	public CommentaireForm(Mediateur mediateurCourrant, String commentaireId) {
49
		initialiserCommentaireForm(mediateurCourrant, commentaireId);
985 jpm 50
	}
51
 
989 jpm 52
	public CommentaireForm(Mediateur mediateurCourrant, String commentaireId, Rafraichissable vueARafraichirApresValidation) {
985 jpm 53
		vueExterneARafraichirApresValidation = vueARafraichirApresValidation;
989 jpm 54
		initialiserCommentaireForm(mediateurCourrant, commentaireId);
985 jpm 55
	}
56
 
989 jpm 57
	private void initialiserCommentaireForm(Mediateur mediateurCourrant, String commentaireId) {
985 jpm 58
		initialiserValidation();
59
 
989 jpm 60
		commentaire = new Commentaire();
61
		commentaire.setId(commentaireId);
985 jpm 62
 
989 jpm 63
		String modeDeCreation = (UtilString.isEmpty(commentaire.getId()) ? Formulaire.MODE_AJOUTER : Formulaire.MODE_MODIFIER);
64
		initialiserFormulaire(mediateurCourrant, modeDeCreation, MenuApplicationId.COMMENTAIRE);
985 jpm 65
 
989 jpm 66
		panneauFormulaire.setLayout(new FormLayout());
985 jpm 67
		String titre = genererTitreFormulaire();
68
		panneauFormulaire.setHeading(titre);
69
 
989 jpm 70
		creerChamps();
71
 
985 jpm 72
		if (modeDeCreation.equals(Formulaire.MODE_MODIFIER)) {
989 jpm 73
			mediateur.selectionnerCommentaire(this, commentaireId);
985 jpm 74
		}
75
	}
76
 
77
	private String genererTitreFormulaire() {
989 jpm 78
		String titre = i18nC.commentaireTitreFormAjout();
985 jpm 79
		if (mode.equals(Formulaire.MODE_MODIFIER)) {
989 jpm 80
			 titre = i18nC.commentaireTitreFormModif()+" - "+i18nC.id()+": "+commentaire.getId();
985 jpm 81
		}
82
		return titre;
83
	}
84
 
989 jpm 85
	private void creerChamps() {
999 jpm 86
		projetsCombo = new ComboBox<Projet>();
87
		projetsCombo.setTabIndex(tabIndex++);
88
		projetsCombo.setFieldLabel(i18nC.projetChamp());
89
		projetsCombo.setDisplayField("nom");
1077 jpm 90
		projetsCombo.setTriggerAction(TriggerAction.ALL);
91
		projetsCombo.setStore(new ListStore<Projet>());
999 jpm 92
		projetsCombo.setForceSelection(true);
1077 jpm 93
		projetsCombo.setAllowBlank(false);
999 jpm 94
		projetsCombo.setValidator(new Validator() {
95
			@Override
1077 jpm 96
			public String validate(Field<?> champ, String valeurAValider) {
999 jpm 97
				String retour = null;
1077 jpm 98
				if (UtilString.isEmpty(valeurAValider)
99
						|| projetsCombo.getStore().findModel("nom", valeurAValider) == null) {
100
					champ.setValue(null);
101
					retour = i18nC.selectionnerValeur();
999 jpm 102
				}
103
				return retour;
104
			}
105
		});
106
		projetsCombo.addStyleName(ComposantClass.OBLIGATOIRE);
1077 jpm 107
		projetsCombo.addListener(Events.Valid, creerEcouteurChampObligatoire());
108
		projetsCombo.addListener(Events.Invalid, creerEcouteurChampObligatoire());
999 jpm 109
		panneauFormulaire.add(projetsCombo, new FormData(450, 0));
110
		mediateur.selectionnerProjet(this, null);
111
 
985 jpm 112
		titreChp = new TextField<String>();
989 jpm 113
		titreChp.setFieldLabel(i18nC.commentaireTitre());
1077 jpm 114
		titreChp.setAllowBlank(false);
985 jpm 115
		titreChp.addStyleName(ComposantClass.OBLIGATOIRE);
116
		titreChp.addListener(Events.Valid, creerEcouteurChampObligatoire());
1077 jpm 117
		titreChp.addListener(Events.Invalid, creerEcouteurChampObligatoire());
989 jpm 118
		panneauFormulaire.add(titreChp, new FormData(450, 0));
985 jpm 119
 
989 jpm 120
		texteChp = new TextArea();
121
		texteChp.setFieldLabel(i18nC.commentaireTexte());
122
		panneauFormulaire.add(texteChp, new FormData(450, 250));
985 jpm 123
 
989 jpm 124
		ponderationChp = new ChampSliderPourcentage(i18nC.commentairePonderation());
125
		panneauFormulaire.add(ponderationChp, new FormData(450, 0));
126
 
127
		publicChp = new CheckBox();
128
		publicChp.setFieldLabel(i18nC.donneePublic());
129
		panneauFormulaire.add(publicChp, new FormData(50, 0));
985 jpm 130
	}
131
 
132
	public void rafraichir(Object nouvellesDonnees) {
989 jpm 133
		if (nouvellesDonnees instanceof Commentaire) {
985 jpm 134
			// Si on a reçu les details d'une publication
989 jpm 135
			rafraichirCommentaire((Commentaire) nouvellesDonnees);
999 jpm 136
		} else if (nouvellesDonnees instanceof ProjetListe) {
137
			ProjetListe projets = (ProjetListe) nouvellesDonnees;
138
			Formulaire.rafraichirComboBox(projets, projetsCombo);
139
			setValeurComboProjets();
985 jpm 140
		} else if (nouvellesDonnees instanceof Information) {
141
			rafraichirInformation((Information) nouvellesDonnees);
142
		} else {
989 jpm 143
			Debug.log(Mediateur.i18nM.erreurRafraichir(nouvellesDonnees.getClass(), this.getClass()));
985 jpm 144
		}
145
 
146
		if (etreValide()) {
147
			initialiserValidation();
148
			repandreRafraichissement();
149
			controlerFermetureApresRafraichissement();
150
		}
151
	}
152
 
989 jpm 153
	private void rafraichirCommentaire(Commentaire commentaireRecu) {
154
		commentaire = commentaireRecu;
985 jpm 155
		peuplerFormulaire();
156
	}
157
 
999 jpm 158
	private String getValeurComboProjets() {
159
		String valeur = "";
1077 jpm 160
		if (projetsCombo.getValue() != null && projetsCombo.isValid()) {
999 jpm 161
			valeur = projetsCombo.getValue().getId();
162
		}
163
		return valeur;
164
	}
165
	private void setValeurComboProjets() {
166
		if (projetsCombo.getStore() != null ) {
167
			if (mode.equals(Formulaire.MODE_MODIFIER) && commentaire != null) {
168
				projetsCombo.setValue(projetsCombo.getStore().findModel("id_projet", commentaire.getIdProjet()));
169
			} else if (mode.equals(Formulaire.MODE_AJOUTER)) {
170
				projetsCombo.setValue(projetsCombo.getStore().findModel("id_projet", mediateur.getProjetId()));
171
			}
172
		}
173
	}
174
 
985 jpm 175
	private void rafraichirInformation(Information info) {
989 jpm 176
		String type = info.getType();
177
		if (type.equals("ajout_commentaire") || type.equals("modif_commentaire")) {
178
			commentaireValideOk = true;
179
			commentaire.setId((String) info.getDonnee(0));
985 jpm 180
		}
181
	}
182
 
183
	private Boolean etreValide() {
184
		Boolean valide = false;
989 jpm 185
		Debug.log("formulaire"+formulaireValideOk+" - Commentaire :"+commentaireValideOk);
186
		if (formulaireValideOk && commentaireValideOk) {
985 jpm 187
			valide = true;
188
		}
189
		return valide;
190
	}
191
 
192
	private void initialiserValidation() {
193
		formulaireValideOk = false;
989 jpm 194
		commentaireValideOk = false;
985 jpm 195
	}
196
 
197
	private void repandreRafraichissement() {
198
		if (vueExterneARafraichirApresValidation != null) {
989 jpm 199
			String type = "commentaire_modifiee";
985 jpm 200
			if (mode.equals(Formulaire.MODE_AJOUTER)) {
989 jpm 201
				type = "commentaire_ajoutee";
985 jpm 202
			}
203
			Information info = new Information(type);
989 jpm 204
			info.setDonnee(0, commentaire);
985 jpm 205
			vueExterneARafraichirApresValidation.rafraichir(info);
206
		}
207
	}
208
 
209
	public boolean soumettreFormulaire() {
210
		formulaireValideOk = verifierFormulaire();
989 jpm 211
		Debug.log("Form?"+formulaireValideOk);
985 jpm 212
		if (formulaireValideOk) {
989 jpm 213
			soumettreCommentaire();
985 jpm 214
		}
215
		return formulaireValideOk;
216
	}
217
 
989 jpm 218
	private void soumettreCommentaire() {
219
		Commentaire commentaireCollectee = collecterCommentaire();
220
		if (commentaireCollectee != null) {
221
			Debug.log("Info public collectée ? ok");
985 jpm 222
			if (mode.equals(Formulaire.MODE_AJOUTER)) {
989 jpm 223
				mediateur.ajouterCommentaire(this, commentaireCollectee);
985 jpm 224
			} else if (mode.equals(Formulaire.MODE_MODIFIER)) {
989 jpm 225
				mediateur.modifierCommentaire(this, commentaireCollectee);
985 jpm 226
			}
227
		}
228
	}
229
 
230
	public boolean verifierFormulaire() {
231
		boolean valide = true;
232
		ArrayList<String> messages = new ArrayList<String>();
233
 
234
		String titre = titreChp.getValue();
235
		if (titre == null || titre.equals("")) {
989 jpm 236
			messages.add(i18nC.commentaireMessageTitre());
985 jpm 237
		}
238
 
1077 jpm 239
		if (UtilString.isEmpty(getValeurComboProjets())) {
240
			String selectionDe = i18nC.articleUn()+" "+i18nC.projetSingulier();
241
			String pour = i18nC.articleLa()+" "+i18nC.commentaireSingulier();
242
			messages.add(i18nM.selectionObligatoire(selectionDe, pour));
243
			Debug.log("Projet combo vide");
244
		}
245
		Debug.log("Projet combo non vide : "+getValeurComboProjets());
246
 
985 jpm 247
		if (messages.size() != 0) {
248
			String[] tableauDeMessages = {};
249
			tableauDeMessages = messages.toArray(tableauDeMessages);
989 jpm 250
			MessageBox.alert(i18nC.erreurSaisieTitre(), UtilArray.implode(tableauDeMessages, "<br />"), null);
985 jpm 251
			valide = false;
252
		}
253
		return valide;
254
	}
255
 
256
	private void peuplerFormulaire() {
999 jpm 257
		setValeurComboProjets();
989 jpm 258
		titreChp.setValue(commentaire.getTitre());
259
		texteChp.setValue(commentaire.getTexte());
260
		ponderationChp.peupler(commentaire.getPonderation());
261
		boolean acces = (commentaire.etrePublic() ? true : false);
262
		publicChp.setValue(acces);
985 jpm 263
	}
264
 
989 jpm 265
	private Commentaire collecterCommentaire() {
266
		Commentaire commentaireCollectee = (Commentaire) commentaire.cloner(new Commentaire());
985 jpm 267
 
999 jpm 268
		commentaireCollectee.setIdProjet(getValeurComboProjets());
269
 
985 jpm 270
		String titre = titreChp.getValue();
989 jpm 271
		commentaireCollectee.setTitre(titre);
985 jpm 272
 
989 jpm 273
		String texte = texteChp.getValue();
274
		commentaireCollectee.setTexte(texte);
985 jpm 275
 
989 jpm 276
		String ponderation = ponderationChp.getValeur();
277
		commentaireCollectee.setPonderation(ponderation);
985 jpm 278
 
989 jpm 279
		String acces = (publicChp.getValue() ? "1" : "0");
280
		commentaireCollectee.setPublic(acces);
985 jpm 281
 
989 jpm 282
		Commentaire commentaireARetourner = null;
283
		if (!commentaireCollectee.comparer(commentaire)) {
284
			commentaireARetourner = commentaire = commentaireCollectee;
985 jpm 285
		}
989 jpm 286
		return commentaireARetourner;
985 jpm 287
	}
288
 
289
	public void reinitialiserFormulaire() {
290
		if (mode.equals(Formulaire.MODE_MODIFIER)) {
989 jpm 291
			mediateur.afficherFormPublication(commentaire.getId());
985 jpm 292
		} else {
293
			mediateur.afficherFormPublication(null);
294
		}
295
	}
296
}