Subversion Repositories eFlore/Applications.cel

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
728 aurelien 1
package org.tela_botanica.client.util;
2
 
3
import java.util.Iterator;
4
 
5
import org.tela_botanica.client.interfaces.Rafraichissable;
6
import org.tela_botanica.client.modeles.ListeReferentielPerso;
7
import org.tela_botanica.client.modeles.ListeReferentielPerso.TypesReferentiels;
8
import org.tela_botanica.client.observation.ObservationMediateur;
9
 
10
import com.gwtext.client.core.EventCallback;
11
import com.gwtext.client.core.EventObject;
12
import com.gwtext.client.core.ListenerConfig;
13
import com.gwtext.client.data.ArrayReader;
14
import com.gwtext.client.data.FieldDef;
15
import com.gwtext.client.data.MemoryProxy;
16
import com.gwtext.client.data.Record;
17
import com.gwtext.client.data.RecordDef;
18
import com.gwtext.client.data.Store;
19
import com.gwtext.client.data.StringFieldDef;
20
import com.gwtext.client.widgets.form.ComboBox;
21
import com.gwtext.client.widgets.form.event.ComboBoxListenerAdapter;
22
 
23
public abstract class AutoCompletionRefComboBox extends ComboBox implements Rafraichissable {
24
 
25
	// TODO: faire un enum
26
	private final int KEY_ALT = 18;
27
	private final int KEY_BACKSPACE = 8;
28
	private final int KEY_CTRL = 17;
29
	private final int KEY_DELETE = 46;
30
	private final int KEY_DOWN = 40;
31
	private final int KEY_END = 35;
32
	private final int KEY_ENTER = 13;
33
	private final int KEY_ESCAPE = 27;
34
	private final int KEY_HOME = 36;
35
	private final int KEY_LEFT = 37;
36
	private final int KEY_PAGEDOWN = 34;
37
	private final int KEY_PAGEUP = 33;
38
	private final int KEY_RIGHT = 39;
39
	private final int KEY_SHIFT = 16;
40
	private final int KEY_TAB = 9;
41
	private final int KEY_UP = 38;
42
 
43
	private ObservationMediateur oMediateur = null;
44
	private TypesReferentiels typeRef = null;
45
	private boolean selectionValeur = false;
46
	private boolean estModifie = false;
47
 
48
	final String resultTplRefPerso = "<div class=\"search-item-ref\">{element_referentiel}</div>";
49
 
50
	public AutoCompletionRefComboBox(String label, String nom, ObservationMediateur oMediateur, TypesReferentiels typeRef) {
51
 
52
		// Accesskey pour debugging
53
		super(label,nom);
54
 
55
		this.oMediateur = oMediateur;
56
		this.typeRef = typeRef;
57
 
58
		setTpl(resultTplRefPerso);
59
		setMode(ComboBox.REMOTE);
60
		setItemSelector("div.search-item-ref");
61
		setTypeAhead(true);
62
		setLoadingText("Recherche...");
63
		setHideTrigger(true);
64
 
65
	    ListenerConfig listenerConfigAutocompletion=new ListenerConfig();
66
	    listenerConfigAutocompletion.setDelay(200);
67
	    listenerConfigAutocompletion.setStopPropagation(false);
68
	    listenerConfigAutocompletion.setStopEvent(false);
69
 
70
		addKeyPressListener(new EventCallback() {
71
 
72
			public void execute(EventObject e) {
73
 
74
				switch(e.getKey()) {
75
 
76
				case KEY_ALT:
77
  	    	      case KEY_CTRL:
78
  	    	      case KEY_DOWN:
79
  	    	      case KEY_END:
80
  	    	      case KEY_ESCAPE:
81
  	    	      case KEY_HOME:
82
  	    	      case KEY_LEFT:
83
  	    	      case KEY_PAGEDOWN:
84
  	    	      case KEY_PAGEUP:
85
  	    	      case KEY_RIGHT:
86
  	    	      case KEY_SHIFT:
87
  	    	      case KEY_TAB:
88
  	    	      case KEY_UP:
89
 
90
	    	        break;
91
 
92
  	    	    case KEY_ENTER:
93
  	    	    	if (selectionValeur) {
94
  	    	    		estModifie= true;
95
  	    	    		selectionValeur=false;
96
  	    	    		onModificationValeur();
97
   	    	    	}
98
   	    	    	else {
99
  	    	    		onValidationSaisie();
100
  	    	    	}
101
  	    	    break;
102
 
103
  	    	      default:
104
  	    	    	estModifie = true;
105
  	    	      	obtenirReferentiel();
106
				}
107
			}
108
	    },listenerConfigAutocompletion);
109
 
110
 
111
		// Listener completion
112
		addListener(new ComboBoxListenerAdapter() {
113
             public void onSelect(ComboBox comboBox, Record record, int index) {
114
            	 setValue(record.getAsString("element_referentiel"));
115
            	 selectionValeur=true;
116
                 collapse();
117
             }
118
         });
119
 
120
	}
121
 
122
	public void rafraichir(Object nouvelleDonnees,
123
			boolean repandreRaffraichissement) {
124
 
125
		ListeReferentielPerso referentielPerso = (ListeReferentielPerso)nouvelleDonnees;
126
 
127
		int i = 0;
128
		Object[][] refData = new Object[referentielPerso.size()][1];
129
 
130
		for (Iterator it = referentielPerso.keySet().iterator(); it.hasNext();)
131
		{
132
			String ref= referentielPerso.get(it.next());
133
			refData[i][0]= ref;
134
 
135
			i++;
136
		}
137
 
138
		FieldDef defStation = new StringFieldDef("element_referentiel");
139
		FieldDef[] defTab = {defStation};
140
 
141
		RecordDef rd = new RecordDef(defTab);
142
 
143
		final MemoryProxy dataProxy = new MemoryProxy(refData);
144
		final ArrayReader reader = new ArrayReader(rd);
145
 
146
		Store store=new Store(dataProxy,reader);
147
 
148
		setStore(store);
149
		store.load();
150
	}
151
 
152
	private void obtenirReferentiel() {
153
 
154
		String valeurChamp = getValue();
155
 
156
		if(valeurChamp == null) {
157
			valeurChamp = "";
158
		}
159
 
160
		oMediateur.obtenirListeReferentielPerso(this, typeRef, valeurChamp);
161
	}
162
 
163
	public abstract void onValidationSaisie();
164
	public abstract void onModificationValeur();
165
 
166
}