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 |
// commune.setPageSize(10); // Ne fonctionne pas
|
|
|
61 |
setItemSelector("div.search-item-ref");
|
|
|
62 |
setTypeAhead(true);
|
|
|
63 |
setLoadingText("Recherche...");
|
|
|
64 |
setHideTrigger(true);
|
|
|
65 |
|
|
|
66 |
ListenerConfig listenerConfigAutocompletion=new ListenerConfig();
|
|
|
67 |
listenerConfigAutocompletion.setDelay(200);
|
|
|
68 |
listenerConfigAutocompletion.setStopPropagation(false);
|
|
|
69 |
listenerConfigAutocompletion.setStopEvent(false);
|
|
|
70 |
|
|
|
71 |
addKeyPressListener(new EventCallback() {
|
|
|
72 |
|
|
|
73 |
public void execute(EventObject e) {
|
|
|
74 |
|
|
|
75 |
switch(e.getKey()) {
|
|
|
76 |
|
|
|
77 |
case KEY_ALT:
|
|
|
78 |
case KEY_CTRL:
|
|
|
79 |
case KEY_DOWN:
|
|
|
80 |
case KEY_END:
|
|
|
81 |
case KEY_ESCAPE:
|
|
|
82 |
case KEY_HOME:
|
|
|
83 |
case KEY_LEFT:
|
|
|
84 |
case KEY_PAGEDOWN:
|
|
|
85 |
case KEY_PAGEUP:
|
|
|
86 |
case KEY_RIGHT:
|
|
|
87 |
case KEY_SHIFT:
|
|
|
88 |
case KEY_TAB:
|
|
|
89 |
case KEY_UP:
|
|
|
90 |
|
|
|
91 |
break;
|
|
|
92 |
|
|
|
93 |
case KEY_ENTER:
|
|
|
94 |
if (selectionValeur) {
|
|
|
95 |
estModifie= true;
|
|
|
96 |
selectionValeur=false;
|
|
|
97 |
onModificationValeur();
|
|
|
98 |
}
|
|
|
99 |
else {
|
|
|
100 |
onValidationSaisie();
|
|
|
101 |
}
|
|
|
102 |
break;
|
|
|
103 |
|
|
|
104 |
default:
|
|
|
105 |
estModifie = true;
|
|
|
106 |
obtenirReferentiel();
|
|
|
107 |
}
|
|
|
108 |
}
|
|
|
109 |
},listenerConfigAutocompletion);
|
|
|
110 |
|
|
|
111 |
|
|
|
112 |
// Listener completion
|
|
|
113 |
addListener(new ComboBoxListenerAdapter() {
|
|
|
114 |
public void onSelect(ComboBox comboBox, Record record, int index) {
|
|
|
115 |
setValue(record.getAsString("element_referentiel"));
|
|
|
116 |
selectionValeur=true;
|
|
|
117 |
collapse();
|
|
|
118 |
}
|
|
|
119 |
});
|
|
|
120 |
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
public void rafraichir(Object nouvelleDonnees,
|
|
|
124 |
boolean repandreRaffraichissement) {
|
|
|
125 |
|
|
|
126 |
ListeReferentielPerso referentielPerso = (ListeReferentielPerso)nouvelleDonnees;
|
|
|
127 |
|
|
|
128 |
int i = 0;
|
|
|
129 |
Object[][] refData = new Object[referentielPerso.size()][1];
|
|
|
130 |
|
|
|
131 |
for (Iterator it = referentielPerso.keySet().iterator(); it.hasNext();)
|
|
|
132 |
{
|
|
|
133 |
String ref= referentielPerso.get(it.next());
|
|
|
134 |
refData[i][0]= ref;
|
|
|
135 |
|
|
|
136 |
i++;
|
|
|
137 |
}
|
|
|
138 |
|
|
|
139 |
FieldDef defStation = new StringFieldDef("element_referentiel");
|
|
|
140 |
FieldDef[] defTab = {defStation};
|
|
|
141 |
|
|
|
142 |
RecordDef rd = new RecordDef(defTab);
|
|
|
143 |
|
|
|
144 |
final MemoryProxy dataProxy = new MemoryProxy(refData);
|
|
|
145 |
final ArrayReader reader = new ArrayReader(rd);
|
|
|
146 |
|
|
|
147 |
Store store=new Store(dataProxy,reader);
|
|
|
148 |
|
|
|
149 |
setStore(store);
|
|
|
150 |
store.load();
|
|
|
151 |
}
|
|
|
152 |
|
|
|
153 |
private void obtenirReferentiel() {
|
|
|
154 |
|
|
|
155 |
String valeurChamp = getValue();
|
|
|
156 |
|
|
|
157 |
if(valeurChamp == null) {
|
|
|
158 |
valeurChamp = "";
|
|
|
159 |
}
|
|
|
160 |
|
|
|
161 |
oMediateur.obtenirListeReferentielPerso(this, typeRef, valeurChamp);
|
|
|
162 |
}
|
|
|
163 |
|
|
|
164 |
public abstract void onValidationSaisie();
|
|
|
165 |
public abstract void onModificationValeur();
|
|
|
166 |
|
|
|
167 |
}
|