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