Subversion Repositories eFlore/Applications.cel

Rev

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

Rev Author Line No. Line
1940 aurelien 1
package org.tela_botanica.client.util;
2
 
1967 aurelien 3
import java.util.ArrayList;
1940 aurelien 4
import java.util.Date;
5
import java.util.HashMap;
6
import java.util.Iterator;
1967 aurelien 7
import java.util.List;
1940 aurelien 8
import java.util.Map;
9
 
10
import org.tela_botanica.client.interfaces.Rafraichissable;
11
import org.tela_botanica.client.modeles.dao.ListeReferentielChampsEtendusDAO;
1967 aurelien 12
import org.tela_botanica.client.modeles.objets.Configuration;
1940 aurelien 13
 
14
import com.google.gwt.user.client.Timer;
15
import com.google.gwt.user.client.Window;
1945 aurelien 16
import com.google.gwt.user.client.ui.HTML;
1940 aurelien 17
import com.google.gwt.user.client.ui.HorizontalPanel;
18
import com.gwtext.client.core.EventCallback;
19
import com.gwtext.client.core.EventObject;
20
import com.gwtext.client.core.ListenerConfig;
21
import com.gwtext.client.data.ArrayReader;
22
import com.gwtext.client.data.FieldDef;
23
import com.gwtext.client.data.MemoryProxy;
24
import com.gwtext.client.data.Record;
25
import com.gwtext.client.data.RecordDef;
1967 aurelien 26
import com.gwtext.client.data.SimpleStore;
1940 aurelien 27
import com.gwtext.client.data.Store;
28
import com.gwtext.client.data.StringFieldDef;
29
import com.gwtext.client.widgets.Button;
30
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
31
import com.gwtext.client.widgets.form.ComboBox;
1945 aurelien 32
import com.gwtext.client.widgets.form.Field;
1940 aurelien 33
import com.gwtext.client.widgets.form.FormPanel;
34
import com.gwtext.client.widgets.form.event.ComboBoxListenerAdapter;
35
 
36
@SuppressWarnings("unchecked")
37
public abstract class FormulaireSaisieChampEtendu extends FormPanel implements Rafraichissable {
38
 
39
	private final int KEY_ALT = 18;
40
	private final int KEY_BACKSPACE = 8;
41
	private final int KEY_CTRL = 17;
42
	private final int KEY_DELETE = 46;
43
	private final int KEY_DOWN = 40;
44
	private final int KEY_END = 35;
45
	private final int KEY_ENTER = 13;
46
	private final int KEY_ESCAPE = 27;
47
	private final int KEY_HOME = 36;
48
	private final int KEY_LEFT = 37;
49
	private final int KEY_PAGEDOWN = 34;
50
	private final int KEY_PAGEUP = 33;
51
	private final int KEY_RIGHT = 39;
52
	private final int KEY_SHIFT = 16;
53
	private final int KEY_TAB = 9;
54
	private final int KEY_UP = 38;
55
 
56
	final ComboBox nChamp;
1968 aurelien 57
	final String resultTplRefPerso = "<div class=\"search-item-ref\">{label}</div>";
1945 aurelien 58
	Button ajouterChampsEtenduEtFermer;
59
	Button ajouterChampsEtendu;
60
	Button annulerAjouterChampEtendu;
1940 aurelien 61
	String idChamp = null;
62
 
1967 aurelien 63
	final ComboBox groupesChampsEtendus;
64
 
1940 aurelien 65
	private Timer timer = null;
66
 
67
	private Map<String, String> cacheClesValeur;
1945 aurelien 68
	private Record rdSelectionne = null;
1967 aurelien 69
    private Record groupeSelectionne = null;
1945 aurelien 70
	private String valeurBrute = "";
1940 aurelien 71
 
72
	public FormulaireSaisieChampEtendu() {
73
		super();
74
		setPaddings(3);
75
		setBodyBorder(false);
76
 
1967 aurelien 77
		HTML indicationSaisieGroupe = new HTML("Vous pouvez ajouter plusieurs champs à la fois correspondant à un groupe "+
78
				"par exemple les champs du projet sauvage de ma rue ou florilèges, ou bien "+
79
				"les champs phytosociolgiques ");
80
		indicationSaisieGroupe.addStyleName("aideCreerChampEtendu");
81
 
82
		groupesChampsEtendus = new ComboBox();
83
		groupesChampsEtendus.setLabel("Ajouter un groupe de champs");
84
		groupesChampsEtendus.setStore(getGroupesChampsEtendusStore());
85
		groupesChampsEtendus.setDisplayField("label") ;
86
		groupesChampsEtendus.setValueField("valeur");
87
		groupesChampsEtendus.setEditable(false);
88
		groupesChampsEtendus.setHideTrigger(false);
89
		groupesChampsEtendus.setForceSelection(true);
90
		groupesChampsEtendus.setCls("champ-separation");
91
		groupesChampsEtendus.setValue("");
92
 
93
		if(groupesChampsEtendusSontDefinis()) {
94
			add(indicationSaisieGroupe);
95
			add(groupesChampsEtendus);
96
		}
97
 
1945 aurelien 98
		HTML indicationSaisie = new HTML("Saisissez un nom de champ ou choisisez un nom proposé par l'autocomplétion "+
99
				"Si vous choissisez un nom de champ existant vous pourrez profiter d'une autocomplétion "+
100
				"sur les valeur déjà saisies pour ce champ par les autres utilisateurs");
101
		indicationSaisie.addStyleName("aideCreerChampEtendu");
102
		add(indicationSaisie);
103
 
1940 aurelien 104
		HorizontalPanel hp = new HorizontalPanel();
105
		hp.setBorderWidth(0);
1967 aurelien 106
 
1940 aurelien 107
		nChamp = new ComboBox("Nom du champ", "nom_champ_etendu");
108
		add(nChamp);
109
 
1967 aurelien 110
		ajouterChampsEtenduEtFermer = new Button("Ajouter et fermer");
111
		ajouterChampsEtenduEtFermer.setTooltip("Ajouter un champ étendu ou un groupe de champs et fermer la fenêtre");
1940 aurelien 112
		ajouterChampsEtendu = new Button("Ajouter");
1967 aurelien 113
		ajouterChampsEtendu.setTooltip("Ajouter un champ étendu ou un groupe de champ");
1940 aurelien 114
		annulerAjouterChampEtendu = new Button("Annuler");
1945 aurelien 115
		annulerAjouterChampEtendu.setTooltip("Fermer la fenêtre sans ajouter de champ");
1940 aurelien 116
 
1945 aurelien 117
		hp.add(ajouterChampsEtenduEtFermer);
1940 aurelien 118
		hp.add(ajouterChampsEtendu);
119
		hp.add(annulerAjouterChampEtendu);
120
		add(hp);
1971 aurelien 121
 
122
		nChamp.setTpl(resultTplRefPerso);
1940 aurelien 123
		nChamp.setMode(ComboBox.REMOTE);
1968 aurelien 124
		nChamp.setItemSelector("div.search-item-ref");
1945 aurelien 125
		nChamp.setTypeAhead(false);
1940 aurelien 126
		nChamp.setLoadingText("Recherche...");
127
		nChamp.setHideTrigger(true);
1971 aurelien 128
		nChamp.setValidateOnBlur(false);
1967 aurelien 129
		nChamp.setWidth("250px");
1940 aurelien 130
		nChamp.focus();
131
 
132
		ajouterListeners();
133
	}
134
 
135
	private void ajouterListeners() {
136
		ListenerConfig listenerConfigAutocompletion=new ListenerConfig();
137
	    listenerConfigAutocompletion.setDelay(200);
138
	    listenerConfigAutocompletion.setStopPropagation(false);
139
	    listenerConfigAutocompletion.setStopEvent(true);
140
 
141
	    nChamp.addKeyPressListener(new EventCallback() {
142
 
143
			@Override
144
			public void execute(EventObject e) {
145
 
146
				switch(e.getKey()) {
147
 
148
					case KEY_ALT:
149
		    	      case KEY_CTRL:
150
		    	      case KEY_END:
151
		    	      case KEY_HOME:
152
		    	      case KEY_LEFT:
153
		    	      case KEY_PAGEDOWN:
154
		    	      case KEY_PAGEUP:
155
		    	      case KEY_RIGHT:
156
		    	      case KEY_SHIFT:
157
		    	      case KEY_TAB:
158
		    	      case KEY_UP:
159
	    	        break;
160
 
1945 aurelien 161
		    	  case KEY_ENTER:
162
  	    	    	  if(rdSelectionne != null) {
163
  	    	    		  nChamp.setValue(rdSelectionne.getAsString("label"));
164
  	    	    		  idChamp = rdSelectionne.getAsString("cle");
165
  	    	    		  valeurBrute = rdSelectionne.getAsString("label");
166
  	    	    	  }
167
	              break;
168
 
1940 aurelien 169
		    	    case KEY_DOWN:
170
  	    	    	  if(nChamp.getValueAsString().isEmpty() && !nChamp.isExpanded()) {
1945 aurelien 171
  	    	    		obtenirListeValeurs("*");
1940 aurelien 172
  	    	    	  }
173
  	    	    	break;
174
 
175
		    	    case KEY_ESCAPE:
176
		    	    	surAnnulation();
177
		    	    break;
178
 
179
		    	    default:
180
		    	    	if(timer != null) {
181
		    	    		timer.cancel();
182
		    	    	}
183
		    	    	timer = new Timer() {
184
 
185
							@Override
186
							public void run() {
1945 aurelien 187
			  	    	      	obtenirListeValeurs();
1940 aurelien 188
							}
189
						};
190
						timer.schedule(300);
1945 aurelien 191
						valeurBrute = nChamp.getValueAsString();
192
						nChamp.setValue(valeurBrute);
1940 aurelien 193
				}
194
			}
195
	    },listenerConfigAutocompletion);
196
 
197
		// Listener completion
198
	    nChamp.addListener(new ComboBoxListenerAdapter() {
1945 aurelien 199
            @Override
1940 aurelien 200
			public void onSelect(ComboBox comboBox, Record record, int index) {
1945 aurelien 201
            	rdSelectionne = record;
202
            }
203
 
204
            @Override
205
			public void onBlur(Field field) {
206
            	nChamp.setRawValue(valeurBrute);
207
            	nChamp.collapse();
208
            }
1940 aurelien 209
	     });
210
 
1967 aurelien 211
	    groupesChampsEtendus.addListener(new ComboBoxListenerAdapter() {
212
			@Override
213
			public void onSelect(ComboBox comboBox, Record record, int index) {
214
            	groupeSelectionne = record;
215
            }
216
	     });
217
 
1945 aurelien 218
	    ajouterChampsEtenduEtFermer.addListener(new ButtonListenerAdapter() {
219
			@Override
220
			public void onClick(Button button, EventObject e) {
1967 aurelien 221
				if(doitAjouterGroupe()) {
222
					surValidationMultiple(renvoyerListeChampsEtendus(groupeSelectionne.getAsString("valeur")));
1945 aurelien 223
				} else {
1967 aurelien 224
					if(champEtenduEstValide()) {
225
						surValidation(validerEtRenvoyerChampEtendu());
226
					} else {
227
						Window.alert("Le nom du champ étendu ne peut pas être vide");
228
					}
229
				}
1945 aurelien 230
			}
231
		});
232
 
1940 aurelien 233
	    ajouterChampsEtendu.addListener(new ButtonListenerAdapter() {
234
			@Override
235
			public void onClick(Button button, EventObject e) {
1945 aurelien 236
				if(champEtenduEstValide()) {
237
					surAjout(validerEtRenvoyerChampEtendu());
238
					raz();
239
				} else {
240
					Window.alert("Le nom du champ étendu ne peut pas être vide");
241
				}
1940 aurelien 242
			}
243
		});
244
 
245
	    annulerAjouterChampEtendu.addListener(new ButtonListenerAdapter() {
246
			@Override
247
			public void onClick(Button button, EventObject e) {
248
				surAnnulation();
249
			}
250
		});
251
	}
252
 
1945 aurelien 253
	private void raz() {
254
		timer = null;
255
		idChamp = null;
256
		nChamp.clearValue();
257
		nChamp.focus();
258
	}
259
 
260
	private ChampSaisieEtendu validerEtRenvoyerChampEtendu() {
1940 aurelien 261
		String valeurChamp = nChamp.getValueAsString();
1945 aurelien 262
		String idNouveauChamp = "";
263
		if(!estUnChampSelectionne(valeurChamp)) {
264
			Date date = new Date();
265
			// affectation d'un id temporaire qui sera remplacé par l'id auto généré à partir
266
			// du label
267
			idNouveauChamp = "tempid_"+date.getTime();
1940 aurelien 268
		} else {
1945 aurelien 269
			idNouveauChamp = idChamp;
1940 aurelien 270
		}
1945 aurelien 271
		ChampSaisieEtendu retour = new ChampSaisieEtendu(valeurChamp, idNouveauChamp);
272
		retour.setId(idNouveauChamp);
273
		return retour;
1940 aurelien 274
	}
275
 
1967 aurelien 276
	private List<ChampSaisieEtendu> renvoyerListeChampsEtendus(String groupe) {
277
		List<ChampSaisieEtendu> champsDuGroupe = new ArrayList<ChampSaisieEtendu>();
278
		HashMap<String, String> listeChampsEtendus = Configuration.getGroupesChampsEtendus().get(groupe);
279
		for (Iterator<String> iterator = listeChampsEtendus.keySet().iterator(); iterator.hasNext();) {
280
			String cle = iterator.next();
281
			String label = listeChampsEtendus.get(cle);
282
			ChampSaisieEtendu champ = new ChampSaisieEtendu(label, cle);
283
			champsDuGroupe.add(champ);
284
		}
285
 
286
		return champsDuGroupe;
287
	}
288
 
289
	private boolean doitAjouterGroupe() {
290
		return groupeSelectionne != null && !groupeSelectionne.getAsString("valeur").equals("");
291
	}
292
 
1945 aurelien 293
	private boolean champEtenduEstValide() {
294
		String valeurChamp = nChamp.getValueAsString();
295
		return (valeurChamp != null && !valeurChamp.isEmpty());
296
	}
297
 
1940 aurelien 298
	private boolean estUnChampSelectionne(String valeur) {
299
		return cacheClesValeur.containsValue(valeur);
300
	}
301
 
1945 aurelien 302
	private void obtenirListeValeurs() {
303
		obtenirListeValeurs(nChamp.getValueAsString());
1940 aurelien 304
	}
305
 
1945 aurelien 306
	private void obtenirListeValeurs(String valeur) {
1940 aurelien 307
		ListeReferentielChampsEtendusDAO lrce = new ListeReferentielChampsEtendusDAO(null);
308
		lrce.obtenirListeNomsChampsEtendus(this, valeur+"*");
309
	}
310
 
1945 aurelien 311
	public abstract void surAjout(ChampSaisieEtendu champ);
1967 aurelien 312
	public abstract void surAjoutMultiple(List<ChampSaisieEtendu> champs);
1940 aurelien 313
	public abstract void surValidation(ChampSaisieEtendu champ);
1967 aurelien 314
	public abstract void surValidationMultiple(List<ChampSaisieEtendu> champs);
1940 aurelien 315
	public abstract void surAnnulation();
316
 
317
	public void rafraichir(Object nouvelleDonnees, boolean repandreRaffraichissement) {
318
		int i = 0;
319
 
320
		HashMap<String, String> clesLabels = (HashMap<String, String>)nouvelleDonnees;
321
		cacheClesValeur = clesLabels;
322
		Object[][] refData = new Object[clesLabels.keySet().size()][2];
323
 
324
		for (Iterator<String> it = clesLabels.keySet().iterator(); it.hasNext();)
325
		{
326
			String cle = it.next();
327
			String label= clesLabels.get(cle);
328
			refData[i][0]= cle;
329
			refData[i][1]= label;
330
			i++;
331
		}
332
 
333
		FieldDef defCle = new StringFieldDef("cle");
334
		FieldDef defLabel = new StringFieldDef("label");
335
		FieldDef[] defTab = {defCle, defLabel};
336
 
337
		RecordDef rd = new RecordDef(defTab);
338
 
339
		final MemoryProxy dataProxy = new MemoryProxy(refData);
340
		final ArrayReader reader = new ArrayReader(rd);
341
 
342
		Store store=new Store(dataProxy,reader);
343
 
344
		nChamp.setStore(store);
345
		store.load();
346
	}
1967 aurelien 347
 
348
	private boolean groupesChampsEtendusSontDefinis() {
349
		return Configuration.getGroupesChampsEtendus().size() > 0;
350
	}
351
 
352
	private Store getGroupesChampsEtendusStore() {
353
		int i = 1;
354
 
355
		Map<String, HashMap<String, String>> listeGroupesChampsEtendus = Configuration.getGroupesChampsEtendus();
356
		Object[][] valeurs = new Object[listeGroupesChampsEtendus.size()+1][2];
357
 
358
		valeurs[0][0] = "...";
359
		valeurs[0][1] = "";
360
		if(listeGroupesChampsEtendus.size() > 0) {
361
			for (Iterator<String> it = listeGroupesChampsEtendus.keySet().iterator(); it.hasNext();)
362
			{
363
				String cle = it.next();
364
				valeurs[i][0]= cle;
365
				valeurs[i][1]= cle;
366
				i++;
367
			}
368
		}
369
		SimpleStore store = new SimpleStore(new String[] {"label","valeur"},valeurs);
370
		store.load();
371
		return store;
372
	}
1940 aurelien 373
}