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.ArrayList;
4
import java.util.Iterator;
5
 
6
import org.tela_botanica.client.interfaces.Rafraichissable;
7
import org.tela_botanica.client.modeles.dao.ListeReferentielChampsEtendusDAO;
8
 
9
import com.google.gwt.user.client.Timer;
10
import com.gwtext.client.core.EventCallback;
11
import com.gwtext.client.core.EventObject;
12
import com.gwtext.client.core.Ext;
13
import com.gwtext.client.core.ListenerConfig;
14
import com.gwtext.client.data.ArrayReader;
15
import com.gwtext.client.data.FieldDef;
16
import com.gwtext.client.data.MemoryProxy;
17
import com.gwtext.client.data.Record;
18
import com.gwtext.client.data.RecordDef;
19
import com.gwtext.client.data.Store;
20
import com.gwtext.client.data.StringFieldDef;
21
import com.gwtext.client.widgets.Component;
22
import com.gwtext.client.widgets.event.ComponentListenerAdapter;
23
import com.gwtext.client.widgets.form.ComboBox;
24
import com.gwtext.client.widgets.form.event.ComboBoxListenerAdapter;
25
 
26
public class ChampSaisieEtendu extends ComboBox implements Rafraichissable {
27
		// TODO: faire un enum
28
		private final int KEY_ALT = 18;
29
		private final int KEY_BACKSPACE = 8;
30
		private final int KEY_CTRL = 17;
31
		private final int KEY_DELETE = 46;
32
		private final int KEY_DOWN = 40;
33
		private final int KEY_END = 35;
34
		private final int KEY_ENTER = 13;
35
		private final int KEY_ESCAPE = 27;
36
		private final int KEY_HOME = 36;
37
		private final int KEY_LEFT = 37;
38
		private final int KEY_PAGEDOWN = 34;
39
		private final int KEY_PAGEUP = 33;
40
		private final int KEY_RIGHT = 39;
41
		private final int KEY_SHIFT = 16;
42
		private final int KEY_TAB = 9;
43
		private final int KEY_UP = 38;
44
 
45
		private boolean selectionValeur = false;
46
		private boolean estModifie = false;
47
 
48
		private String cle = null;
49
		private String label = null;
50
 
51
		private Timer timer = null;
52
 
53
		final String resultTplRefPerso = "<div class=\"search-item-ref\">{valeur}</div>";
54
 
55
		public ChampSaisieEtendu(String label, String cle) {
56
 
57
			// Accesskey pour debugging
58
			super(label,cle);
59
 
60
			this.cle =cle;
61
			this.label = label;
62
 
63
			setTpl(resultTplRefPerso);
64
			setMode(ComboBox.REMOTE);
65
			setItemSelector("div.search-item-ref");
66
			setTypeAhead(true);
67
			setLoadingText("Recherche...");
68
			setHideTrigger(true);
69
 
70
		    ListenerConfig listenerConfigAutocompletion=new ListenerConfig();
71
		    listenerConfigAutocompletion.setDelay(200);
72
		    listenerConfigAutocompletion.setStopPropagation(false);
73
		    listenerConfigAutocompletion.setStopEvent(false);
74
 
75
			addKeyPressListener(new EventCallback() {
76
 
77
				@Override
78
				public void execute(EventObject e) {
79
 
80
					switch(e.getKey()) {
81
 
82
					  case KEY_ALT:
83
	  	    	      case KEY_CTRL:
84
	  	    	      case KEY_END:
85
	  	    	      case KEY_ESCAPE:
86
	  	    	      case KEY_HOME:
87
	  	    	      case KEY_LEFT:
88
	  	    	      case KEY_PAGEDOWN:
89
	  	    	      case KEY_PAGEUP:
90
	  	    	      case KEY_RIGHT:
91
	  	    	      case KEY_SHIFT:
92
	  	    	      case KEY_TAB:
93
	  	    	      case KEY_UP:
94
 
95
		    	        break;
96
 
97
	  	    	      case KEY_DOWN:
98
	  	    	    	  if(getValueAsString().isEmpty() && !isExpanded()) {
99
	  	    	    		  obtenirListeValeurs("*");
100
	  	    	    	  }
101
	  	    	      break;
102
 
103
	  	    	      default:
104
	  	    	    	if(timer != null) {
105
		    	    		timer.cancel();
106
		    	    	}
107
		    	    	timer = new Timer() {
108
 
109
							@Override
110
							public void run() {
111
			  	    	      	obtenirListeValeurs();
112
							}
113
						};
114
						timer.schedule(300);
115
					}
116
				}
117
		    },listenerConfigAutocompletion);
118
 
119
 
120
			// Listener completion
121
			addListener(new ComboBoxListenerAdapter() {
122
	             @Override
123
				public void onSelect(ComboBox comboBox, Record record, int index) {
124
	            	 setValue(record.getAsString("valeur"));
125
	            	 selectionValeur=true;
126
	                 collapse();
127
	             }
128
	         });
129
 
130
			this.addListener(new ComponentListenerAdapter() {
131
				@Override
132
				public void onRender(Component component) {
133
					setLargeurChamp();
134
				}
135
			});
136
 
137
		}
138
 
139
		@Override
140
		public void rafraichir(Object nouvelleDonnees, boolean repandreRaffraichissement) {
141
 
142
			@SuppressWarnings("unchecked")
143
			ArrayList<String> valeurs = (ArrayList<String>)nouvelleDonnees;
144
 
145
			int i = 0;
146
			Object[][] refData = new Object[valeurs.size()][1];
147
 
148
			for (Iterator<String> it = valeurs.iterator(); it.hasNext();)
149
			{
150
				refData[i][0]= it.next();
151
				i++;
152
			}
153
 
154
			FieldDef defValeur = new StringFieldDef("valeur");
155
			FieldDef[] defTab = {defValeur};
156
 
157
			RecordDef rd = new RecordDef(defTab);
158
 
159
			final MemoryProxy dataProxy = new MemoryProxy(refData);
160
			final ArrayReader reader = new ArrayReader(rd);
161
 
162
			Store store=new Store(dataProxy,reader);
163
 
164
			setStore(store);
165
			store.load();
166
		}
167
 
168
		private void obtenirListeValeurs() {
169
			String valeurChamp = getValue();
170
			obtenirListeValeurs(valeurChamp);
171
		}
172
 
173
		private void obtenirListeValeurs(String valeurChamp) {
174
			if(valeurChamp == null) {
175
				valeurChamp = "";
176
			} else {
177
				valeurChamp += "*";
178
			}
179
			ListeReferentielChampsEtendusDAO lrceDao = new ListeReferentielChampsEtendusDAO(null);
180
			lrceDao.obtenirListeValeursChampEtendu(this, cle, valeurChamp);
181
		}
182
 
183
		public void setLargeurChamp() {
184
			// Correction un peu moche pour améliorer l'affichage du champ
185
			String idElementEnfant = Ext.get(("x-form-el-"+cle)).getFirstChild().getId();
186
			Ext.get(idElementEnfant).setWidth("90%", false);
187
			Ext.get(cle).setWidth("100%", false);
188
		}
189
}