Subversion Repositories eFlore/Applications.cel

Rev

Rev 1968 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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