Subversion Repositories eFlore/Applications.cel

Rev

Details | 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;
13
import com.google.gwt.user.client.ui.HorizontalPanel;
14
import com.gwtext.client.core.EventCallback;
15
import com.gwtext.client.core.EventObject;
16
import com.gwtext.client.core.ListenerConfig;
17
import com.gwtext.client.data.ArrayReader;
18
import com.gwtext.client.data.FieldDef;
19
import com.gwtext.client.data.MemoryProxy;
20
import com.gwtext.client.data.Record;
21
import com.gwtext.client.data.RecordDef;
22
import com.gwtext.client.data.Store;
23
import com.gwtext.client.data.StringFieldDef;
24
import com.gwtext.client.widgets.Button;
25
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
26
import com.gwtext.client.widgets.form.ComboBox;
27
import com.gwtext.client.widgets.form.FormPanel;
28
import com.gwtext.client.widgets.form.TextField;
29
import com.gwtext.client.widgets.form.event.ComboBoxListenerAdapter;
30
 
31
@SuppressWarnings("unchecked")
32
public abstract class FormulaireSaisieChampEtendu extends FormPanel implements Rafraichissable {
33
 
34
	private final int KEY_ALT = 18;
35
	private final int KEY_BACKSPACE = 8;
36
	private final int KEY_CTRL = 17;
37
	private final int KEY_DELETE = 46;
38
	private final int KEY_DOWN = 40;
39
	private final int KEY_END = 35;
40
	private final int KEY_ENTER = 13;
41
	private final int KEY_ESCAPE = 27;
42
	private final int KEY_HOME = 36;
43
	private final int KEY_LEFT = 37;
44
	private final int KEY_PAGEDOWN = 34;
45
	private final int KEY_PAGEUP = 33;
46
	private final int KEY_RIGHT = 39;
47
	private final int KEY_SHIFT = 16;
48
	private final int KEY_TAB = 9;
49
	private final int KEY_UP = 38;
50
 
51
	final ComboBox nChamp;
52
	String resultTplRefPerso = "<div class=\"search-item-ref\">{label}</div>";
53
	final Button ajouterChampsEtendu;
54
	final Button annulerAjouterChampEtendu;
55
	String idChamp = null;
56
 
57
	private Timer timer = null;
58
 
59
	private Map<String, String> cacheClesValeur;
60
 
61
	public FormulaireSaisieChampEtendu() {
62
		super();
63
		setPaddings(3);
64
		setBodyBorder(false);
65
 
66
		HorizontalPanel hp = new HorizontalPanel();
67
		hp.setBorderWidth(0);
68
 
69
		nChamp = new ComboBox("Nom du champ", "nom_champ_etendu");
70
		add(nChamp);
71
 
72
		ajouterChampsEtendu = new Button("Ajouter");
73
		annulerAjouterChampEtendu = new Button("Annuler");
74
 
75
		hp.add(ajouterChampsEtendu);
76
		hp.add(annulerAjouterChampEtendu);
77
		add(hp);
78
 
79
		nChamp.setTpl(resultTplRefPerso);
80
		nChamp.setMode(ComboBox.REMOTE);
81
		nChamp.setItemSelector("div.search-item-ref");
82
		nChamp.setTypeAhead(true);
83
		nChamp.setLoadingText("Recherche...");
84
		nChamp.setHideTrigger(true);
85
		nChamp.setWidth("250px");
86
 
87
		nChamp.setValue("");
88
		nChamp.focus();
89
 
90
		ajouterListeners();
91
	}
92
 
93
	private void ajouterListeners() {
94
		ListenerConfig listenerConfigAutocompletion=new ListenerConfig();
95
	    listenerConfigAutocompletion.setDelay(200);
96
	    listenerConfigAutocompletion.setStopPropagation(false);
97
	    listenerConfigAutocompletion.setStopEvent(true);
98
 
99
	    nChamp.addKeyPressListener(new EventCallback() {
100
 
101
			@Override
102
			public void execute(EventObject e) {
103
 
104
				switch(e.getKey()) {
105
 
106
					case KEY_ALT:
107
		    	      case KEY_CTRL:
108
		    	      case KEY_ENTER:
109
		    	      case KEY_END:
110
		    	      case KEY_HOME:
111
		    	      case KEY_LEFT:
112
		    	      case KEY_PAGEDOWN:
113
		    	      case KEY_PAGEUP:
114
		    	      case KEY_RIGHT:
115
		    	      case KEY_SHIFT:
116
		    	      case KEY_TAB:
117
		    	      case KEY_UP:
118
	    	        break;
119
 
120
		    	    case KEY_DOWN:
121
  	    	    	  if(nChamp.getValueAsString().isEmpty() && !nChamp.isExpanded()) {
122
  	    	    		obtenirReferentiel("*");
123
  	    	    	  }
124
  	    	    	break;
125
 
126
		    	    case KEY_ESCAPE:
127
		    	    	surAnnulation();
128
		    	    break;
129
 
130
		    	    default:
131
		    	    	if(timer != null) {
132
		    	    		timer.cancel();
133
		    	    	}
134
		    	    	timer = new Timer() {
135
 
136
							@Override
137
							public void run() {
138
			  	    	      	obtenirReferentiel();
139
							}
140
						};
141
						timer.schedule(300);
142
				}
143
			}
144
	    },listenerConfigAutocompletion);
145
 
146
		// Listener completion
147
	    nChamp.addListener(new ComboBoxListenerAdapter() {
148
	         @Override
149
			public void onSelect(ComboBox comboBox, Record record, int index) {
150
	        	 nChamp.setValue(record.getAsString("label"));
151
	        	 idChamp = record.getAsString("cle");
152
	        	 nChamp.collapse();
153
	         }
154
	     });
155
 
156
	    ajouterChampsEtendu.addListener(new ButtonListenerAdapter() {
157
			@Override
158
			public void onClick(Button button, EventObject e) {
159
				validerChampEtendu();
160
			}
161
		});
162
 
163
	    annulerAjouterChampEtendu.addListener(new ButtonListenerAdapter() {
164
			@Override
165
			public void onClick(Button button, EventObject e) {
166
				surAnnulation();
167
			}
168
		});
169
	}
170
 
171
	private void validerChampEtendu() {
172
		String valeurChamp = nChamp.getValueAsString();
173
		if(valeurChamp != null && !valeurChamp.isEmpty()) {
174
			String idNouveauChamp = "";
175
			if(!estUnChampSelectionne(valeurChamp)) {
176
				Date date = new Date();
177
				// affectation d'un id temporaire qui sera remplacé par l'id auto généré à partir
178
				// du label
179
				idNouveauChamp = "tempid_"+date.getTime();
180
			} else {
181
				idNouveauChamp = idChamp;
182
			}
183
			ChampSaisieEtendu retour = new ChampSaisieEtendu(valeurChamp, idNouveauChamp);
184
			retour.setId(idNouveauChamp);
185
			surValidation(retour);
186
		} else {
187
			Window.alert("Le nom du champ étendu ne peut pas être vide");
188
		}
189
	}
190
 
191
	private boolean estUnChampSelectionne(String valeur) {
192
		return cacheClesValeur.containsValue(valeur);
193
	}
194
 
195
	private void obtenirReferentiel() {
196
		obtenirReferentiel(nChamp.getValueAsString());
197
	}
198
 
199
	private void obtenirReferentiel(String valeur) {
200
		ListeReferentielChampsEtendusDAO lrce = new ListeReferentielChampsEtendusDAO(null);
201
		lrce.obtenirListeNomsChampsEtendus(this, valeur+"*");
202
	}
203
 
204
	public abstract void surValidation(ChampSaisieEtendu champ);
205
	public abstract void surAnnulation();
206
 
207
	public void rafraichir(Object nouvelleDonnees, boolean repandreRaffraichissement) {
208
		int i = 0;
209
 
210
		HashMap<String, String> clesLabels = (HashMap<String, String>)nouvelleDonnees;
211
		cacheClesValeur = clesLabels;
212
		Object[][] refData = new Object[clesLabels.keySet().size()][2];
213
 
214
		for (Iterator<String> it = clesLabels.keySet().iterator(); it.hasNext();)
215
		{
216
			String cle = it.next();
217
			String label= clesLabels.get(cle);
218
			refData[i][0]= cle;
219
			refData[i][1]= label;
220
			i++;
221
		}
222
 
223
		FieldDef defCle = new StringFieldDef("cle");
224
		FieldDef defLabel = new StringFieldDef("label");
225
		FieldDef[] defTab = {defCle, defLabel};
226
 
227
		RecordDef rd = new RecordDef(defTab);
228
 
229
		final MemoryProxy dataProxy = new MemoryProxy(refData);
230
		final ArrayReader reader = new ArrayReader(rd);
231
 
232
		Store store=new Store(dataProxy,reader);
233
 
234
		nChamp.setStore(store);
235
		store.load();
236
	}
237
}