Subversion Repositories eFlore/Applications.cel

Rev

Rev 798 | 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;
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;
815 aurelien 105
	    	      	onModificationValeur();
728 aurelien 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
}