Subversion Repositories eFlore/Archives.cel-v2

Rev

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

package org.tela_botanica.client.vues;


import java.util.Iterator;

import org.tela_botanica.client.image.ImageMediateur;
import org.tela_botanica.client.interfaces.Rafraichissable;
import org.tela_botanica.client.interfaces.VueListable;
import org.tela_botanica.client.modeles.ImageCarnet;
import org.tela_botanica.client.modeles.ListeImageCarnet;


import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.Window;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.core.XTemplate;
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.util.Format;
import com.gwtext.client.widgets.BoxComponent;
import com.gwtext.client.widgets.Component;
import com.gwtext.client.widgets.Container;
import com.gwtext.client.widgets.DataView;
import com.gwtext.client.widgets.Observable;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.DataView.Data;
import com.gwtext.client.widgets.event.ContainerListener;
import com.gwtext.client.widgets.event.ContainerListenerAdapter;
import com.gwtext.client.widgets.event.DataViewListener;
import com.gwtext.client.widgets.event.DataViewListenerAdapter;

/**
 * Galerie d'images miniatures
 * @author aurelien
 *
 */
public class GalerieImageVue extends Panel implements Rafraichissable, VueListable {

        // instance du médiateur
        private ImageMediateur iMediateur = null; 
        private DataView dView = null ;
        private Store st = null ;
        
        public GalerieImageVue(ImageMediateur im)
        {
                super("Galerie");
                iMediateur = im ;
                
                this.addListener(new ContainerListenerAdapter() {


                        public void onHide(Component component) {
                                // TODO Auto-generated method stub
                                
                        }


                        public void onRender(Component component) {
                                // TODO Auto-generated method stub
                                
                        }


                        public void onShow(Component component) {
                                
                                
                                if(dView == null)
                                {
                                        initialiser();
                                }
                        }
                        
                });
                
        }
        
        
        public void ajouterListenersDataView()
        {
                
                // ajout de listeners pour la gestion de la selection
                // dans la galerie
                dView.addListener(new DataViewListenerAdapter() {



                        public void onClick(DataView source, int index, Element node,
                                        EventObject e) {
                                
                                getIMediateur().clicGalerieImage(index, node, e);
                                
                        }


                        public void onContainerClick(DataView source, EventObject e) {
                                //TODO: appeler le mediateur
                                
                        }


                        public void onContextMenu(DataView source, int index, Element node,
                                        EventObject e) {
                                
                                e.stopEvent() ;
                                getIMediateur().montrerContextMenu(e) ;
                                
                        }


                        public void onDblClick(DataView source, int index, Element node,
                                        EventObject e) {
                                
                                //TODO: appeler le mediateur
                                getIMediateur().clicGalerieImage(index, node, e);
                                
                        }


                        public void onSelectionChange(DataView view, Element[] selections) {
                                //TODO: appeler le mediateur
                                
                        }
                        
                });
        }
        
        
        // instantiation paresseuse
        public void initialiser()
        {
                // Preparation de la dataview et du template            
                // le template va créer une div contenant une image
                // pour chacune des photos
                final XTemplate template = new XTemplate(
                                new String[]{
                                                "<tpl for='.'>",
                                                "<div class='thumb-wrap' id='{num_image}'>",
                                                "<div class='thumb'><img src='{url_image_S}' title='{num_image}'></div>",
                                                "<span>{nom}</span></div>", "</tpl>",
                                                "<div class='x-clear'></div>"});
                template.compile();

                // la dataview affichera les images en accord avec le template
                // cree precedemment
                dView = new DataView("div.thumb-wrap") {
                        public void prepareData(Data data) {
                                data.setProperty("shortName", Format.ellipsis(data
                                                .getProperty("num_image"), 15));
                        }
                };
                dView.setTpl(template);
                
                // parametre d'affichage de la dataview
                this.setAutoScroll(true);
                dView.setAutoHeight(true);
                dView.setMultiSelect(true);
                dView.setOverCls("x-view-over");
                dView.setEmptyText("Aucune image à afficher");
                
                
                // creation du store
                FieldDef defNumImage = new StringFieldDef("num_image");
                FieldDef defUrlImage = new StringFieldDef("url_image");
                FieldDef defUrlImageM = new StringFieldDef("url_image_M");
                FieldDef defUrlImageS = new StringFieldDef("url_image_S");
                FieldDef[] defTab = {defNumImage,defUrlImage,defUrlImageM,defUrlImageS};
                RecordDef rd = new RecordDef(defTab) ;
                st = new Store(rd) ;
                dView.setStore(st);
                
                this.getDView().setLoadingText("chargement");
                
                // ajouts de la gestion des evenements pour la dataview
                ajouterListenersDataView();
                
                this.add(dView);        
                
                getIMediateur().obtenirPhotoGalerie(this);
        }
        
        
        
        public void rafraichir(Object nouvelleDonnees, boolean repandreRafraichissement) {
                
                if(nouvelleDonnees instanceof ListeImageCarnet)
                {
                        ListeImageCarnet data = (ListeImageCarnet) nouvelleDonnees ;
                        Object[][] photoData = new Object[data.size()][4];
                        int i = 0 ;
                        
                        for (Iterator it = data.keySet().iterator(); it.hasNext();) 
                        {
                                ImageCarnet im = (ImageCarnet) data.get(it.next());
                                photoData[i][0] = im.getOrdre() ;
                                photoData[i][1] = im.getUrl() ;
                                photoData[i][2] = im.getSUrl() ;
                                photoData[i][3] = im.getMUrl() ;
                                i++ ;
                        }
                        
                        final MemoryProxy dataProxy = new MemoryProxy(photoData);
                        final ArrayReader reader = new ArrayReader(new RecordDef(
                                        new FieldDef[]{new StringFieldDef("num_image"),
                                                        new StringFieldDef("url_image"),
                                                        new StringFieldDef("url_image_S"),
                                                        new StringFieldDef("url_image_M")}));
        
                        final Store photoStore = new Store(dataProxy, reader);
                        photoStore.load();
        
                        st = photoStore;
                        dView.setStore(st);
                        dView.refresh();
                        
                        if(repandreRafraichissement)
                        {
                                getIMediateur().synchroniserZoomListeGalerie(nouvelleDonnees, this) ;
                        }
                }

        }


        public ImageMediateur getIMediateur() {
                return iMediateur;
        }


        public DataView getDView() {
                return dView;
        }


        public Store getSt() {
                return st;
        }
        
        public String[] getIdSelectionnees()
        { 
                Record[] selection = getDView().getSelectedRecords() ;
                int taille = selection.length ;
                String id_selection[] = new String[taille] ;
                
                for (int i = 0; i < selection.length; i++) {
                        
                        id_selection[i] = selection[i].getAsString("num_image") ;
                }
                
                return id_selection ;
        }
}