Subversion Repositories eFlore/Applications.coel

Rev

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

package org.tela_botanica.client.composants;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.tela_botanica.client.composants.pagination.ChargeurListe;
import org.tela_botanica.client.composants.pagination.Proxy;
import org.tela_botanica.client.composants.pagination.TransformateurJSONaModelData;
import org.tela_botanica.client.interfaces.Rafraichissable;
import org.tela_botanica.client.composants.GrilleParametrable;

import com.extjs.gxt.ui.client.Style.SortDir;
import com.extjs.gxt.ui.client.data.BasePagingLoadConfig;
import com.extjs.gxt.ui.client.data.LoadEvent;
import com.extjs.gxt.ui.client.data.Loader;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.data.PagingLoadConfig;
import com.extjs.gxt.ui.client.data.PagingLoadResult;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.GridEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.store.Store;
import com.extjs.gxt.ui.client.store.StoreEvent;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.EditorGrid;
import com.extjs.gxt.ui.client.widget.grid.GridSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.RowNumberer;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.toolbar.PagingToolBar;

public class GrillePaginable<D extends ModelData>  extends ContentPanel implements Rafraichissable, GrilleParametrable<D> {

        //+---------------------------------------------------------------------------------------------------------------+
        // Attributs
        
        // Pagination
        private int start = 0;
        private int limit = 50;
        
        // Récupération, stockage et pagination des données
        private ModelType modelType = null;
        private Proxy<D> proxy = null;
        private TransformateurJSONaModelData<PagingLoadResult<D>> reader = null;
        private BasePagingLoadConfig plc = null;
        private ChargeurListe<PagingLoadResult<D>> loader = null;
        private ListStore<D> storeGrille = null;
        
        // Elements graphiques
        EditorGrid<D> grille = null;
        List<ColumnConfig> colonnes = null;
        ColumnModel modeleDeColonnes = null;
        
        //+---------------------------------------------------------------------------------------------------------------+
        // Constructeur
        
        public GrillePaginable(ModelType modeltype, HashMap<String, String> virtualFields, Proxy<D> proxy, List<ColumnConfig> colonnes, ColumnModel modeleDeColonnes) {
                
                // Récupération des paramètres
                this.modelType = modeltype;
                this.proxy = proxy;
                this.colonnes = colonnes;
                this.modeleDeColonnes = modeleDeColonnes;
                
                // Formatage du conteneur;
                this.setLayout(new FitLayout());
                
                // Création des objets pour la récupération  et la pagination des données
                plc = new BasePagingLoadConfig();
                plc.setLimit(limit);
                plc.setOffset(start);
                
                reader = new TransformateurJSONaModelData<PagingLoadResult<D>>(modelType, virtualFields);
                
                creerChargeurDeListe();
                creerGrille();
        }

        //+---------------------------------------------------------------------------------------------------------------+
        // Accesseurs
        
        public EditorGrid<D> getGrille() {
                return grille;
        }
        
        public ListStore<D> getStore() {
                return storeGrille;
        }
        
        public GridSelectionModel<D> getSelectionModel() {
                return grille.getSelectionModel();
        }
        
        public D getSelection() {
                return grille.getSelectionModel().getSelectedItem();
        }
        
        @SuppressWarnings("unchecked")
        public Proxy getProxy() {
                return this.proxy;
        }
        
        
        //+---------------------------------------------------------------------------------------------------------------+
        // Méthodes publiques
        
        public void rafraichir(Object nouvellesDonnees) {
                
        }
        
        //+---------------------------------------------------------------------------------------------------------------+
        // Méthodes privées
        
        private void creerChargeurDeListe() {
                loader = new ChargeurListe<PagingLoadResult<D>>(proxy, reader, this);
        loader.setLimit(plc.getLimit());
        loader.setOffset(plc.getOffset());
                loader.addListener(Loader.BeforeLoad, new Listener<LoadEvent>() {
                        public void handleEvent(LoadEvent be) {                                 

                        }  
                });
                loader.addListener(Loader.Load, new Listener<LoadEvent>() {
                        public void handleEvent(LoadEvent be) {                                 

                        }  
                });
        }
        
        private void creerGrille() {
                final PagingToolBar toolBar = new PagingToolBar(this.limit);  
                toolBar.bind(loader);  
                this.setBottomComponent(toolBar);
                
                storeGrille = new ListStore<D>(loader);
                storeGrille.addListener(Store.Add, new Listener<StoreEvent<D>>() {
                        public void handleEvent(StoreEvent<D> ce) {

                        }
                });
                storeGrille.addListener(Store.Remove, new Listener<StoreEvent<D>>() {
                        public void handleEvent(StoreEvent<D> ce) {

                        }
                });
                storeGrille.addListener(Store.Update, new Listener<StoreEvent<D>>() {
                        public void handleEvent(StoreEvent<D> ce) {

                        }
                });
                
                GridSelectionModel<D> modeleDeSelection = new GridSelectionModel<D>();
                
                grille = new EditorGrid<D>(storeGrille, modeleDeColonnes);
                grille.setHeight("100%");
                grille.setBorders(true);
                grille.setSelectionModel(modeleDeSelection);
                grille.getView().setForceFit(true);
                grille.setStripeRows(true);
                grille.setBorders(true); 
                grille.setStateful(true);  
                grille.addListener(Events.Attach, new Listener<GridEvent<D>>() {  
               public void handleEvent(GridEvent<D> be) {  
                 PagingLoadConfig config = new BasePagingLoadConfig();  
                 config.setOffset(start);  
                 config.setLimit(limit);  
                   
                 Map<String, Object> state = grille.getState();  
                 if (state.containsKey("offset")) {  
                   int offset = (Integer)state.get("offset");  
                   int limit = (Integer)state.get("limit");  
                   config.setOffset(offset);  
                   config.setLimit(limit);  
                 }  
                 if (state.containsKey("sortField")) {  
                   config.setSortField((String)state.get("sortField"));  
                   config.setSortDir(SortDir.valueOf((String)state.get("sortDir")));
                 }  
                 loader.load(config);  
               }  
             });
                
                this.add(grille);
        }
        
        public void reload() {
                loader.load();
        }
}