Subversion Repositories eFlore/Applications.coel

Rev

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

Rev Author Line No. Line
839 jpm 1
package org.tela_botanica.client.composants;
2
 
3
import java.util.Iterator;
954 jpm 4
import java.util.List;
839 jpm 5
 
6
import org.tela_botanica.client.Mediateur;
7
import org.tela_botanica.client.RegistreId;
907 jpm 8
import org.tela_botanica.client.configuration.Configuration;
839 jpm 9
import org.tela_botanica.client.interfaces.Rafraichissable;
10
import org.tela_botanica.client.modeles.Valeur;
11
import org.tela_botanica.client.modeles.ValeurListe;
954 jpm 12
import org.tela_botanica.client.modeles.aDonnee;
13
import org.tela_botanica.client.util.UtilString;
839 jpm 14
 
15
import com.extjs.gxt.ui.client.Registry;
16
import com.extjs.gxt.ui.client.event.BaseEvent;
17
import com.extjs.gxt.ui.client.event.Events;
18
import com.extjs.gxt.ui.client.event.Listener;
19
import com.extjs.gxt.ui.client.widget.LayoutContainer;
20
import com.extjs.gxt.ui.client.widget.form.CheckBox;
21
import com.extjs.gxt.ui.client.widget.form.CheckBoxGroup;
22
import com.extjs.gxt.ui.client.widget.form.Field;
1147 gduche 23
import com.extjs.gxt.ui.client.widget.form.LabelField;
839 jpm 24
import com.extjs.gxt.ui.client.widget.form.TextField;
25
import com.extjs.gxt.ui.client.widget.form.FormPanel.LabelAlign;
1147 gduche 26
import com.extjs.gxt.ui.client.widget.layout.FlowLayout;
839 jpm 27
import com.extjs.gxt.ui.client.widget.layout.FormData;
28
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
29
import com.extjs.gxt.ui.client.widget.tips.ToolTipConfig;
30
import com.google.gwt.core.client.GWT;
31
 
32
public class ChampCaseACocher extends LayoutContainer implements Rafraichissable {
33
 
34
	private Configuration config = null;
35
	private Mediateur mediateur = null;
36
 
37
	private CheckBoxGroup groupeCaseACocher = null;
38
	private boolean avoirChampAutre = false;
39
	private Field<String> champAutre = null;
40
 
41
	private String listeValeursCode = null;
42
 
43
	public ChampCaseACocher(String label, String listeCode, boolean avoirChampAutre) {
44
		config = (Configuration) Registry.get(RegistreId.CONFIG);
45
		mediateur = (Mediateur) Registry.get(RegistreId.MEDIATEUR);
46
 
47
		setLayout(creerFormLayout(650, LabelAlign.TOP));
48
		groupeCaseACocher = new CheckBoxGroup();
49
		setLabel(label);
50
		setChampAutre(avoirChampAutre);
51
 
52
		setListeCode(listeCode);
53
		mediateur.obtenirListeValeurEtRafraichir(this, getListeCode());
54
	}
55
 
56
	public String getListeCode() {
57
		return listeValeursCode;
58
	}
59
	public void setListeCode(String code) {
60
		listeValeursCode = code;
61
	}
62
 
63
	public void setLabel(String label) {
64
		groupeCaseACocher.setFieldLabel(label);
65
	}
66
 
954 jpm 67
	public boolean avoirChampAutre() {
68
		return avoirChampAutre;
69
	}
839 jpm 70
	public void setChampAutre(boolean avoirChamp) {
71
		avoirChampAutre = avoirChamp;
72
		if (avoirChampAutre) {
73
			champAutre = new TextField<String>();
74
		}
75
	}
76
 
954 jpm 77
	public String getValeur() {
78
		String valeur = "";
79
 
80
		List<CheckBox> liste = groupeCaseACocher.getValues();
81
		if (liste != null) {
82
			int taille = liste.size();
83
			for (int i = 0; i < taille; i++) {
84
				CheckBox cac = liste.get(i);
85
				if (cac.isEnabled()) {
86
					valeur += aDonnee.SEPARATEUR_VALEURS+cac.getData("id");
87
				}
88
			}
89
 
90
		}
91
 
92
		if (avoirChampAutre() && !UtilString.isEmpty(champAutre.getValue())) {
955 jpm 93
			valeur += aDonnee.SEPARATEUR_VALEURS+"AUTRE"+aDonnee.SEPARATEUR_TYPE_VALEUR+champAutre.getValue();
954 jpm 94
		}
95
 
96
		if (!valeur.equals("")) {
97
			valeur.replaceFirst("^"+aDonnee.SEPARATEUR_VALEURS, "");
98
		}
99
 
100
		return valeur;
101
	}
102
 
103
	public void peupler(String donnees) {
104
		String[] valeurs = donnees.split(aDonnee.SEPARATEUR_VALEURS);
105
		for (int i = 0; i < valeurs.length; i++) {
955 jpm 106
			if (valeurs[i].startsWith("AUTRE"+aDonnee.SEPARATEUR_TYPE_VALEUR)) {
107
				champAutre.setValue(valeurs[i].replaceFirst("^AUTRE"+aDonnee.SEPARATEUR_TYPE_VALEUR, ""));
954 jpm 108
			} else {
109
				//TODO : check : List<CheckBox> cases = groupeCac.getAll();
110
				List<Field<?>> cases = groupeCaseACocher.getAll();
111
				for (int j = 0; j < cases.size(); j++) {
112
					if (cases.get(j).getId().equals("val-"+valeurs[i])) {
113
						((CheckBox) cases.get(j)).setValue(true);
114
					}
115
				}
116
			}
117
		}
118
	}
119
 
839 jpm 120
	private FormLayout creerFormLayout(Integer labelWidth, LabelAlign labelAlign) {
121
		FormLayout formLayout = new FormLayout();
122
		if (labelWidth != null) {
123
			formLayout.setLabelWidth(labelWidth);
124
		}
125
		if (labelAlign != null) {
126
			formLayout.setLabelAlign(labelAlign);
127
		}
128
		return formLayout;
129
	}
130
 
131
	private void creerChoixMultipleCac(ValeurListe listeValeurs) {
132
		addListener(Events.Hide, new Listener<BaseEvent>() {
133
			public void handleEvent(BaseEvent be) {
134
				groupeCaseACocher.reset();
954 jpm 135
				if (avoirChampAutre()) {
839 jpm 136
					champAutre.setValue("");
137
				}
138
			}
139
		});
140
 
141
		groupeCaseACocher.setAutoWidth(true);
142
		groupeCaseACocher.setStyleAttribute("padding", "3px");
143
		groupeCaseACocher.setData("liste_id", listeValeurs.getId());
144
		for (Iterator<String> it = listeValeurs.keySet().iterator(); it.hasNext();) {
145
			Valeur val = listeValeurs.get(it.next());
146
			String nom =  val.get("nom");
147
			CheckBox cac = new CheckBox();
148
			cac.setId("val-"+val.getId());
149
			cac.setData("id", val.getId());
150
			cac.setBoxLabel(nom);
151
			if (! val.getDescription().equals("NULL")) {
152
				cac.setToolTip(new ToolTipConfig(val.getNom(), val.getDescription()));
153
			}
154
			groupeCaseACocher.add(cac);
155
		}
156
		add(groupeCaseACocher);
157
 
954 jpm 158
		if (avoirChampAutre()) {
839 jpm 159
			// FIXME : éviter le chevauchement du texte des cases à cocher avec le label "Autre" sur les petits écrans
160
			LayoutContainer conteneur = new LayoutContainer();
1147 gduche 161
			conteneur.setLayout(new FlowLayout());
162
			conteneur.add(new LabelField("Autre : "));
839 jpm 163
			champAutre.setId("autre-"+listeValeurs.getId());
164
			champAutre.setLabelStyle("font-weight:normal;");
165
			conteneur.add(champAutre, new FormData(500, 0));
166
 
167
			add(conteneur);
168
		}
169
		layout();
170
	}
171
 
172
	@Override
173
	public void rafraichir(Object nouvellesDonnees) {
174
		if (nouvellesDonnees instanceof ValeurListe) {
175
			ValeurListe listeValeurs = (ValeurListe) nouvellesDonnees;
176
			if (listeValeurs.getId().equals(config.getListeId(getListeCode()))) {
177
				creerChoixMultipleCac(listeValeurs);
178
			}
179
		} else {
180
			GWT.log(Mediateur.i18nM.erreurRafraichir(nouvellesDonnees.getClass(), this.getClass()), null);
181
		}
182
	}
183
 
184
}