Subversion Repositories eFlore/Applications.cel

Rev

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