Subversion Repositories eFlore/Applications.coel

Rev

Rev 1468 | Rev 1641 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
665 jp_milcent 1
package org.tela_botanica.client.composants;
2
 
3
import java.util.HashMap;
4
 
691 jp_milcent 5
import org.tela_botanica.client.Mediateur;
665 jp_milcent 6
import org.tela_botanica.client.images.Images;
721 gduche 7
import org.tela_botanica.client.modeles.Valeur;
955 jpm 8
import org.tela_botanica.client.modeles.aDonnee;
1468 jpm 9
import org.tela_botanica.client.util.Debug;
665 jp_milcent 10
 
11
import com.extjs.gxt.ui.client.event.ButtonEvent;
1262 cyprien 12
import com.extjs.gxt.ui.client.event.Events;
13
import com.extjs.gxt.ui.client.event.FieldEvent;
14
import com.extjs.gxt.ui.client.event.Listener;
665 jp_milcent 15
import com.extjs.gxt.ui.client.event.SelectionListener;
721 gduche 16
import com.extjs.gxt.ui.client.store.ListStore;
665 jp_milcent 17
import com.extjs.gxt.ui.client.widget.LayoutContainer;
18
import com.extjs.gxt.ui.client.widget.MessageBox;
19
import com.extjs.gxt.ui.client.widget.Text;
20
import com.extjs.gxt.ui.client.widget.button.Button;
721 gduche 21
import com.extjs.gxt.ui.client.widget.form.ComboBox;
857 jpm 22
import com.extjs.gxt.ui.client.widget.form.Field;
665 jp_milcent 23
import com.extjs.gxt.ui.client.widget.form.HiddenField;
24
import com.extjs.gxt.ui.client.widget.form.LabelField;
25
import com.extjs.gxt.ui.client.widget.form.TextField;
1093 gduche 26
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;
665 jp_milcent 27
import com.extjs.gxt.ui.client.widget.layout.ColumnData;
28
import com.extjs.gxt.ui.client.widget.layout.ColumnLayout;
29
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
30
import com.extjs.gxt.ui.client.widget.layout.RowLayout;
31
 
32
public class ChampMultiValeurs extends LayoutContainer {
727 gduche 33
	//TODO : changer le champRecapitulatif par une hashMap
34
	//TODO : autoriser la modification des champs saisis
35
 
665 jp_milcent 36
	String idChampCache = null;
37
 
38
	String nomLabelChampTxt = "";
39
 
40
	String titreErreur = null;
41
	String valeurVideMsg = null;
42
	String valeurIdentiqueMsg = null;
694 gduche 43
	String valeurNonValideMsg = null;
44
 
693 gduche 45
	String valeurParDefaut = "";
857 jpm 46
	String validationMasque = null;
694 gduche 47
	String exempleValidation = null;
700 gduche 48
	String boutonSupprimerLabel = "";
665 jp_milcent 49
 
50
	LayoutContainer principalLayout = null;
857 jpm 51
	Field<String> champValeurTxt = null;
52
	ComboBox<Valeur> champValeurCombo = null;
665 jp_milcent 53
	HiddenField<String> champRecapitulatif = null;
54
	HashMap<String, LayoutContainer> valeurs = null;
721 gduche 55
	ComboBox<Valeur> types = null;
665 jp_milcent 56
 
57
	int largeurTotale = 420;
58
	int largeurBouton = 20;
59
	int largeurChamp = 0;
861 jpm 60
	int largeurType = 0;
665 jp_milcent 61
 
721 gduche 62
	boolean estMultiType = false;
857 jpm 63
	boolean estComboBox = false;
721 gduche 64
 
665 jp_milcent 65
	public ChampMultiValeurs() {
66
		initialiserChampMultiValeur(null, 0);
67
	}
68
 
69
	public ChampMultiValeurs(String label) {
70
		initialiserChampMultiValeur(label, 0);
71
	}
72
 
73
	public ChampMultiValeurs(String label, int largeurTotale) {
74
		initialiserChampMultiValeur(label, largeurTotale);
75
	}
76
 
972 gduche 77
	public ChampMultiValeurs(boolean estMultiType, String label, int largeurTotale, int largeurType) {
857 jpm 78
		this.estMultiType = estMultiType;
721 gduche 79
		initialiserChampMultiValeur(label, largeurTotale);
80
	}
861 jpm 81
 
82
	public ChampMultiValeurs(boolean estCombobox, boolean estMultiType, String label, int largeurTotale, int largeurType) {
857 jpm 83
		this.estMultiType = estMultiType;
84
		this.estComboBox = estCombobox;
861 jpm 85
		setLargeurType(largeurType);
857 jpm 86
		initialiserChampMultiValeur(label, largeurTotale);
87
	}
88
 
1262 cyprien 89
	public ComboBox<Valeur> getTypes() {
90
		return this.types;
91
	}
92
 
665 jp_milcent 93
	public void setLabel(String label) {
94
		if (label == null) {
95
			label = "";
96
		}
97
		nomLabelChampTxt = label;
98
	}
99
 
861 jpm 100
	public void setLargeurChamp(int largeurMax) {
101
		if (largeurMax != 0 && largeurMax > 20) {
102
			largeurTotale = largeurMax;
665 jp_milcent 103
		}
861 jpm 104
 
665 jp_milcent 105
		largeurChamp = largeurTotale - largeurBouton;
106
	}
107
 
861 jpm 108
	public void setLargeurType(int largeur) {
109
		largeurType = largeur;
110
	}
111
 
694 gduche 112
	public void setValidation (String validation, String exempleValidation)	{
857 jpm 113
		this.validationMasque = validation;
694 gduche 114
		this.exempleValidation = exempleValidation;
115
 
116
		this.valeurNonValideMsg = Mediateur.i18nM.valeurNonValideMsg(exempleValidation);
117
	}
118
 
693 gduche 119
	public void setValeurParDefaut(String valeur)	{
120
		this.valeurParDefaut = valeur;
857 jpm 121
		champValeurTxt.setValue(valeur);
693 gduche 122
	}
123
 
700 gduche 124
	public void setValeurBoutonSupprimer(String valeur)	{
125
		this.boutonSupprimerLabel = valeur;
126
	}
127
 
665 jp_milcent 128
	private void initialiserChampMultiValeur(String label, int largeur) {
129
		setLabel(label);
861 jpm 130
		setLargeurChamp(largeur);
665 jp_milcent 131
 
691 jp_milcent 132
		titreErreur = Mediateur.i18nC.erreurSaisieTitre();
133
		valeurVideMsg = Mediateur.i18nC.demanderValeur();
134
		valeurIdentiqueMsg = Mediateur.i18nC.valeurDejaPresente();
665 jp_milcent 135
 
136
		valeurs = new HashMap<String, LayoutContainer>();
137
		champRecapitulatif = new HiddenField<String>();
138
		champRecapitulatif.setValue("");
139
 
140
		setLayout(new FormLayout());
141
 
142
		creerChampMultiValeurs();
143
	}
144
 
145
	private void creerChampMultiValeurs() {
146
		principalLayout = new LayoutContainer();
147
		principalLayout.setLayout(new RowLayout());
148
 
149
		LabelField label = new LabelField(nomLabelChampTxt + ":");
861 jpm 150
		label.setWidth("95%");
665 jp_milcent 151
		principalLayout.add(label);
152
 
861 jpm 153
		LayoutContainer colonneConteneur = new LayoutContainer();
154
		colonneConteneur.setLayout(new ColumnLayout());
665 jp_milcent 155
 
721 gduche 156
		if (estMultiType == true)	{
157
			types = new ComboBox<Valeur>();
158
			types.setDisplayField("nom");
159
			types.setEmptyText("Choisissez:");
160
			types.setStore(new ListStore<Valeur>());
1093 gduche 161
			types.setTypeAhead(true);
162
			types.setTriggerAction(TriggerAction.ALL);
1262 cyprien 163
 
164
			types.addListener(Events.Select, new Listener<FieldEvent>() {
165
				public void handleEvent(FieldEvent fe) {
166
 
167
				}
168
			});
169
 
861 jpm 170
			colonneConteneur.add(types, new ColumnData(largeurType));
721 gduche 171
		}
172
 
857 jpm 173
		if (estComboBox) {
174
			champValeurCombo = new ComboBox<Valeur>();
175
			champValeurCombo.setDisplayField("nom");
176
			champValeurCombo.setStore(new ListStore<Valeur>());
1262 cyprien 177
			colonneConteneur.add(champValeurCombo, new ColumnData(largeurChamp));
857 jpm 178
		} else {
179
			champValeurTxt = new TextField<String>();
861 jpm 180
			colonneConteneur.add(champValeurTxt, new ColumnData(largeurChamp));
857 jpm 181
		}
665 jp_milcent 182
		Button ajouterBouton = new Button();
183
		ajouterBouton.setIcon(Images.ICONES.ajouter());
857 jpm 184
		ajouterBouton.addSelectionListener(new SelectionListener<ButtonEvent>() {
185
			public void componentSelected(ButtonEvent ce) {
186
				String valeurChamp = "";
187
				if (estComboBox) {
955 jpm 188
					if (champValeurCombo.getValue() != null) {
1468 jpm 189
						valeurChamp = champValeurCombo.getValue().getId();
955 jpm 190
					}
857 jpm 191
				} else {
192
					valeurChamp = champValeurTxt.getValue();
193
				}
194
 
195
				if ((valeurChamp == null) || valeurChamp.trim().equals("") || valeurChamp.trim().equals(valeurParDefaut))	{
196
					MessageBox.alert(titreErreur, valeurVideMsg, null);
197
				} else if (valeurs.get(valeurChamp) != null){
198
					MessageBox.alert(titreErreur, valeurIdentiqueMsg, null);
199
				} else {
200
					if (validationMasque != null && !valeurChamp.matches(validationMasque)) 	{
201
						MessageBox.alert(titreErreur, valeurNonValideMsg, null);
665 jp_milcent 202
					} else {
857 jpm 203
						if (estMultiType)	{
204
							String type = "";
205
							String id = "";
206
							Valeur valeur = types.getValue();
721 gduche 207
 
857 jpm 208
							if (valeur != null)	{
209
								type = valeur.getNom();
210
								id = valeur.getId();
721 gduche 211
							} else {
857 jpm 212
								type = types.getRawValue();
213
								id = type;
721 gduche 214
							}
857 jpm 215
 
216
							if (type.trim().equals(""))	{
1069 gduche 217
								MessageBox.alert(Mediateur.i18nM.titreErreurSaisie(), Mediateur.i18nM.typeChampMulti(), null);
857 jpm 218
							} else {
219
								ajouterValeur(valeurChamp, type, id);
220
							}
221
						} else {
222
							ajouterValeur(valeurChamp);
694 gduche 223
						}
665 jp_milcent 224
					}
225
				}
857 jpm 226
			}
227
		});
861 jpm 228
		colonneConteneur.add(ajouterBouton, new ColumnData(largeurBouton));
665 jp_milcent 229
 
230
		principalLayout.add(champRecapitulatif);
861 jpm 231
		principalLayout.add(colonneConteneur);
665 jp_milcent 232
		add(principalLayout);
233
	}
234
 
721 gduche 235
	public void ajouterValeur(final String texte, final String strValeur, final String id)	{
236
		//Implémenté dans classe inférieure
237
	}
238
 
700 gduche 239
	public void ajouterValeur(final String texte) 	{
1468 jpm 240
 
665 jp_milcent 241
		LayoutContainer colonneLayout = new LayoutContainer();
242
		colonneLayout.setLayout(new ColumnLayout());
243
		valeurs.put(texte, colonneLayout);
244
 
245
		Text champTxt = new Text();
246
		champTxt.setText(texte);
247
		colonneLayout.add(champTxt, new ColumnData(largeurChamp));
248
 
249
		Button supprimerBouton = new Button();
250
		supprimerBouton.setIcon(Images.ICONES.supprimer());
700 gduche 251
		supprimerBouton.setText(boutonSupprimerLabel);
665 jp_milcent 252
		supprimerBouton.setData("valeur", texte);
253
		supprimerBouton.addSelectionListener(new SelectionListener<ButtonEvent>() {
254
			public void componentSelected(ButtonEvent ce) {
255
				String valeur = ce.getComponent().getData("valeur");
256
				LayoutContainer valeurLayout = valeurs.get(valeur);
257
				principalLayout.remove(valeurLayout);
258
 
259
				valeurs.remove(valeur);
1468 jpm 260
 
700 gduche 261
				supprimerValeurDuRecapitulatif(valeur);
665 jp_milcent 262
 
263
				actualiserLayoutGlobal();
264
			}
265
		});
266
		colonneLayout.add(supprimerBouton, new ColumnData(largeurBouton));
267
 
268
		// Ajout du layout de la valeur au layout principal
269
		principalLayout.add(colonneLayout);
270
 
271
		// Ajout de la valeur au champ récapitulatif des valeurs ajoutées
272
		ajouterValeurAuRecapitulatif(texte);
693 gduche 273
		reinitialiserChamp();
665 jp_milcent 274
		actualiserLayoutGlobal();
275
	}
276
 
721 gduche 277
	public void reinitialiserChamp()	{
857 jpm 278
		if (estComboBox) {
279
			champValeurCombo.setValue(champValeurCombo.getStore().findModel("id_valeur", valeurParDefaut));
280
		} else {
281
			champValeurTxt.setValue(valeurParDefaut);
282
		}
283
 
284
		if (estMultiType) {
285
			types.reset();
286
		}
693 gduche 287
	}
288
 
721 gduche 289
	public void actualiserLayoutGlobal() {
665 jp_milcent 290
		layout();
291
	}
292
 
700 gduche 293
	public void ajouterValeurAuRecapitulatif(String texte) {
665 jp_milcent 294
		String texteRecapitulatif = "";
295
		if (champRecapitulatif.getValue() != null)	{
296
			texteRecapitulatif = champRecapitulatif.getValue();
297
		}
955 jpm 298
		texteRecapitulatif += texte + aDonnee.SEPARATEUR_VALEURS;
665 jp_milcent 299
		champRecapitulatif.setValue(texteRecapitulatif);
300
	}
301
 
700 gduche 302
	public void supprimerValeurDuRecapitulatif(String texte) {
1468 jpm 303
 
665 jp_milcent 304
		if (champRecapitulatif.getValue() != null)	{
1468 jpm 305
			String texteValeur = "";
1513 jpm 306
			if (estComboBox) {
307
				// comme c'est le nom de la valeur qui est passée en paramètre,
308
				// on doit récupérer son id
309
				ListStore<Valeur> valeurs = champValeurCombo.getStore();
310
				Valeur objetValeur = valeurs.findModel("nom", texte);
311
 
312
				if (objetValeur != null)	{
313
					texteValeur = objetValeur.getId();
314
				} else {
315
					texteValeur = texte;
316
				}
1468 jpm 317
			} else {
318
				texteValeur = texte;
319
			}
320
 
665 jp_milcent 321
			String texteRecapitulatif = champRecapitulatif.getValue();
1468 jpm 322
			texteRecapitulatif = texteRecapitulatif.replace(texteValeur + aDonnee.SEPARATEUR_VALEURS, "");
665 jp_milcent 323
			champRecapitulatif.setValue(texteRecapitulatif);
324
		}
325
	}
326
 
327
	public String getValeurs() {
328
		String texteRecapitulatif = "";
329
		if (champRecapitulatif.getValue() != null) {
330
			texteRecapitulatif = champRecapitulatif.getValue();
955 jpm 331
			texteRecapitulatif = texteRecapitulatif.replaceAll("(.*)"+aDonnee.SEPARATEUR_VALEURS+"$", "$1");
665 jp_milcent 332
		}
333
		return texteRecapitulatif;
334
	}
669 jp_milcent 335
 
336
	public void peupler(String chaineExistante) {
853 gduche 337
		if (valeurs.size()>0)	{
338
			reinitialiser();
339
		}
691 jp_milcent 340
		if (chaineExistante != null && !chaineExistante.trim().equals("")) {
955 jpm 341
			String[] valeurs = chaineExistante.split(aDonnee.SEPARATEUR_VALEURS);
669 jp_milcent 342
			for (int i = 0; i < valeurs.length; i++) {
343
				ajouterValeur(valeurs[i]);
344
			}
345
		}
346
	}
830 gduche 347
 
348
	public void reinitialiser()	{
349
		//TODO : pourrait etre mieux fait si les valeurs étaient enregistrées dans un
350
		// layout particulier. Il suffirait alors d'enlever les valeurs de ce layout.
351
		principalLayout.removeAll();
352
		initialiserChampMultiValeur(nomLabelChampTxt, largeurTotale);
353
	}
665 jp_milcent 354
}