Subversion Repositories eFlore/Applications.cel

Rev

Rev 783 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package org.tela_botanica.client.util;

import java.util.Iterator;

import org.tela_botanica.client.interfaces.Rafraichissable;
import org.tela_botanica.client.modeles.ListeReferentielPerso;
import org.tela_botanica.client.modeles.ListeReferentielPerso.TypesReferentiels;
import org.tela_botanica.client.observation.ObservationMediateur;

import com.gwtext.client.core.EventCallback;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.core.ListenerConfig;
import com.gwtext.client.data.ArrayReader;
import com.gwtext.client.data.FieldDef;
import com.gwtext.client.data.MemoryProxy;
import com.gwtext.client.data.Record;
import com.gwtext.client.data.RecordDef;
import com.gwtext.client.data.Store;
import com.gwtext.client.data.StringFieldDef;
import com.gwtext.client.widgets.form.ComboBox;
import com.gwtext.client.widgets.form.event.ComboBoxListenerAdapter;

public abstract class AutoCompletionRefComboBox extends ComboBox implements Rafraichissable {
        
        // TODO: faire un enum
        private final int KEY_ALT = 18;
        private final int KEY_BACKSPACE = 8;
        private final int KEY_CTRL = 17;
        private final int KEY_DELETE = 46;
        private final int KEY_DOWN = 40;
        private final int KEY_END = 35;
        private final int KEY_ENTER = 13;
        private final int KEY_ESCAPE = 27;
        private final int KEY_HOME = 36;
        private final int KEY_LEFT = 37;
        private final int KEY_PAGEDOWN = 34;
        private final int KEY_PAGEUP = 33;
        private final int KEY_RIGHT = 39;
        private final int KEY_SHIFT = 16;
        private final int KEY_TAB = 9;
        private final int KEY_UP = 38;
        
        private ObservationMediateur oMediateur = null;
        private TypesReferentiels typeRef = null;
        private boolean selectionValeur = false;
        private boolean estModifie = false;
        
        final String resultTplRefPerso = "<div class=\"search-item-ref\">{element_referentiel}</div>";  
        
        public AutoCompletionRefComboBox(String label, String nom, ObservationMediateur oMediateur, TypesReferentiels typeRef) {
                
                // Accesskey pour debugging
                super(label,nom);  
                
                this.oMediateur = oMediateur;
                this.typeRef = typeRef;

                setTpl(resultTplRefPerso);
                setMode(ComboBox.REMOTE);
                setItemSelector("div.search-item-ref");
                setTypeAhead(true);  
                setLoadingText("Recherche...");  
                setHideTrigger(true);
                
            ListenerConfig listenerConfigAutocompletion=new ListenerConfig();
            listenerConfigAutocompletion.setDelay(200);
            listenerConfigAutocompletion.setStopPropagation(false);
            listenerConfigAutocompletion.setStopEvent(false);
                
                addKeyPressListener(new EventCallback() {

                        public void execute(EventObject e) {

                                switch(e.getKey()) {
                      
                                case KEY_ALT:
                      case KEY_CTRL:
                      case KEY_DOWN:
                      case KEY_END:
                      case KEY_ESCAPE:
                      case KEY_HOME:
                      case KEY_LEFT:
                      case KEY_PAGEDOWN:
                      case KEY_PAGEUP:
                      case KEY_RIGHT:
                      case KEY_SHIFT:
                      case KEY_TAB:
                      case KEY_UP:
                        
                        break;
                                
                    case KEY_ENTER:     
                        if (selectionValeur) {
                                estModifie= true;
                                selectionValeur=false;
                                onModificationValeur();
                        }
                        else {
                                onValidationSaisie();
                        }
                    break;
                       
                      default:
                        estModifie = true;
                        obtenirReferentiel();
                                }
                        }
            },listenerConfigAutocompletion);
                
                
                // Listener completion 
                addListener(new ComboBoxListenerAdapter() {  
             public void onSelect(ComboBox comboBox, Record record, int index) {  
                 setValue(record.getAsString("element_referentiel"));
                 selectionValeur=true;
                 collapse();
             }  
         });
                
        }

        public void rafraichir(Object nouvelleDonnees,
                        boolean repandreRaffraichissement) {
                
                ListeReferentielPerso referentielPerso = (ListeReferentielPerso)nouvelleDonnees;
                
                int i = 0;
                Object[][] refData = new Object[referentielPerso.size()][1];
                
                for (Iterator it = referentielPerso.keySet().iterator(); it.hasNext();) 
                {
                        String ref= referentielPerso.get(it.next());
                        refData[i][0]= ref;
                        
                        i++;
                }
                
                FieldDef defStation = new StringFieldDef("element_referentiel");
                FieldDef[] defTab = {defStation};
                
                RecordDef rd = new RecordDef(defTab);
                
                final MemoryProxy dataProxy = new MemoryProxy(refData);
                final ArrayReader reader = new ArrayReader(rd);         
                                                
                Store store=new Store(dataProxy,reader);
                
                setStore(store);
                store.load();
        }
        
        private void obtenirReferentiel() {
                
                String valeurChamp = getValue();
                
                if(valeurChamp == null) {
                        valeurChamp = "";
                }
                
                oMediateur.obtenirListeReferentielPerso(this, typeRef, valeurChamp);
        }
        
        public abstract void onValidationSaisie();
        public abstract void onModificationValeur();

}