Subversion Repositories eFlore/Applications.cel

Rev

Rev 1291 | Details | Compare with Previous | 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;
989 aurelien 6
import org.tela_botanica.client.modeles.objets.ListeReferentielPerso;
7
import org.tela_botanica.client.modeles.objets.ListeReferentielPerso.TypesReferentiels;
728 aurelien 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
 
1291 aurelien 72
			@Override
728 aurelien 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;
815 aurelien 106
  	    	      	onModificationValeur();
728 aurelien 107
  	    	      	obtenirReferentiel();
108
				}
109
			}
110
	    },listenerConfigAutocompletion);
111
 
112
 
113
		// Listener completion
114
		addListener(new ComboBoxListenerAdapter() {
1291 aurelien 115
             @Override
116
			public void onSelect(ComboBox comboBox, Record record, int index) {
728 aurelien 117
            	 setValue(record.getAsString("element_referentiel"));
118
            	 selectionValeur=true;
119
                 collapse();
120
             }
121
         });
122
 
123
	}
124
 
1291 aurelien 125
	@Override
728 aurelien 126
	public void rafraichir(Object nouvelleDonnees,
127
			boolean repandreRaffraichissement) {
128
 
129
		ListeReferentielPerso referentielPerso = (ListeReferentielPerso)nouvelleDonnees;
130
 
131
		int i = 0;
132
		Object[][] refData = new Object[referentielPerso.size()][1];
133
 
134
		for (Iterator it = referentielPerso.keySet().iterator(); it.hasNext();)
135
		{
136
			String ref= referentielPerso.get(it.next());
137
			refData[i][0]= ref;
138
 
139
			i++;
140
		}
141
 
142
		FieldDef defStation = new StringFieldDef("element_referentiel");
143
		FieldDef[] defTab = {defStation};
144
 
145
		RecordDef rd = new RecordDef(defTab);
146
 
147
		final MemoryProxy dataProxy = new MemoryProxy(refData);
148
		final ArrayReader reader = new ArrayReader(rd);
149
 
150
		Store store=new Store(dataProxy,reader);
151
 
152
		setStore(store);
153
		store.load();
154
	}
155
 
156
	private void obtenirReferentiel() {
157
 
158
		String valeurChamp = getValue();
159
 
160
		if(valeurChamp == null) {
161
			valeurChamp = "";
162
		}
163
 
164
		oMediateur.obtenirListeReferentielPerso(this, typeRef, valeurChamp);
165
	}
166
 
167
	public abstract void onValidationSaisie();
168
	public abstract void onModificationValeur();
169
 
170
}