Subversion Repositories eFlore/Archives.cel-v1

Rev

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

/**
 David Delon david.delon@clapas.net 2007
 
 */


/*
 * InventoryListView.java  (Composite de Panel)
 *
 * Cas d'utilisation :
 * 
 * Affichage de releve 
 *
 * 1 : Recherche du nombre de releves associe au navigateur ou a l'utilisateur est connecte, en fonction des criteres de selection
 * 2 : Recherche des releves correspondant au critere precedent
 * 3 : Affichage des releves avec positionnement sur le dernier releve
 * 
 * Selection de releve
 * 
 * 1 : L'utilisateur selectionne un releve : lancement de l'affichage detaille pour le releve selectionne
 * 
 * 
 * Pagination :
 * 
 * 1 : Avancement ou recul d'une page
 *
 *
 * 
 *  Suppression d'une liste d'element 
 */


/* Actions declenchees :
 * 
 * onInventoryItemSelected(numero d'ordre de la ligne selectionne) : selection d'une ligne
 * onInventoryItemUnselected(numero d'ordre de la ligne selectionne) : deselection d'une ligne
 * onInventoryUpdated(location) : action suite a la modification, suppression, creation d'un element d'inventaire
 * 
 */


package org.tela_botanica.client;


import net.mygwt.ui.client.Events;
import net.mygwt.ui.client.Style;
import net.mygwt.ui.client.event.BaseEvent;
import net.mygwt.ui.client.event.Listener;
import net.mygwt.ui.client.widget.ContentPanel;
import net.mygwt.ui.client.widget.WidgetContainer;
import net.mygwt.ui.client.widget.layout.BorderLayoutData;
import net.mygwt.ui.client.widget.layout.FillLayout;
import net.mygwt.ui.client.widget.table.Table;
import net.mygwt.ui.client.widget.table.TableColumn;
import net.mygwt.ui.client.widget.table.TableColumnModel;
import net.mygwt.ui.client.widget.table.TableItem;

import com.google.gwt.http.client.URL;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONNumber;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.json.client.JSONValue;
import com.google.gwt.user.client.HTTPRequest;
import com.google.gwt.user.client.ResponseTextHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;


public class InventoryListView 
                 {

        // Debut Barre de navigation

        private class NavBar extends Composite implements ClickListener {

                
                public final DockPanel bar = new DockPanel();
                

                public final Button gotoFirst = new Button("<<", this);
                public final Button gotoNext = new Button(">", this);
                public final Button gotoPrev = new Button("<", this);
                public final Button gotoEnd = new Button(">>", this);
                public final Label status = new Label();
                

                public NavBar() {
                        
                        initWidget(bar);
                        
                        status.setWordWrap(false);

                        HorizontalPanel buttons = new HorizontalPanel();
                        
                        buttons.add(status);
                        buttons.setCellHorizontalAlignment(status, HasHorizontalAlignment.ALIGN_RIGHT);
                        buttons.setCellVerticalAlignment(status, HasVerticalAlignment.ALIGN_MIDDLE);


                        buttons.add(gotoFirst);
                        buttons.add(gotoPrev);
                        buttons.add(gotoNext);
                        buttons.add(gotoEnd);
                        
                        bar.add(buttons, DockPanel.EAST);

                        
                }
                

                public void onClick(Widget sender) {
                        if (sender == gotoNext) {
                                // Move forward a page.
                                startIndex += VISIBLE_TAXON_COUNT;
                                if (startIndex >= count)
                                        startIndex -= VISIBLE_TAXON_COUNT;
                        } else {
                                if (sender == gotoPrev) {
                                        // Move back a page.
                                        startIndex -= VISIBLE_TAXON_COUNT;
                                        if (startIndex < 0)
                                                startIndex = 0;
                                } else {
                                        if (sender == gotoEnd) {
                                                gotoEnd();
                                        } else {
                                                if (sender == gotoFirst) {
                                                        startIndex = 0;
                                                }
                                        }
                                }
                        }
                        update();
                }

        }

        // Fin Barre de navigation

        // Conteneur (header et table sont dans panel)
        private ContentPanel panel =null;
        private Table table = null;
        
        // Services 
        private String serviceBaseUrl = null;
        private String user;
        private Mediator mediator = null;

        // Navigation
        private int startIndex = 0;
        private int count = 0;
        private static final int VISIBLE_TAXON_COUNT = 15;
        private NavBar navBar=null;

        // Filtre par defaut :
        
        private String location = "all";
        private String date = "all";
        private String search = "all";
        private String station = "all";
        private String ordre= null;
        
        


        public InventoryListView(Mediator med) {
                
                
                // Traitement contexte utilisateur et service

                mediator=med;

                user=mediator.getUser();
            serviceBaseUrl = mediator.getServiceBaseUrl();
        
            
            panel= new ContentPanel(Style.HEADER);
            panel.setLayout(new FillLayout());
            
            
            // Barre navigation integree au header 
            
                navBar = new NavBar();
                panel.getHeader().addWidget(navBar);
            
                
                //  Contenu :
                
                
                //  Colonnes :
                
                TableColumn[] columns = new TableColumn[5]; 
                
                // TODO : renderer date, alignement etc
                
                columns[0] = new TableColumn("etat","Transmis", 50);  
                columns[0].setSortable(false);

                columns[1] = new TableColumn("nom","Nom saisi", 200);  
                columns[1].setSortable(false);
                
                columns[2] = new TableColumn("observation","Observation", 650);  
                columns[2].setSortable(false);

                columns[3] = new TableColumn("date","Date", 75);  
                columns[3].setSortable(false);

                columns[4] = new TableColumn("ordre","Ordre", 50);  
                columns[4].setSortable(false);
                
        
                TableColumnModel cm = new TableColumnModel(columns);  
                
                // Table : 
                
                table = new Table(Style.MULTI | Style.HORIZONTAL, cm);  
                table.setBorders(false);  
                
                
                panel.add(table);
                
                WidgetContainer center=mediator.getCenterContainer();
                BorderLayoutData centerData = new BorderLayoutData(Style.CENTER, .75f, 100, 1000);
                center.add(panel,centerData);

                

                table.addListener(Events.RowClick, new Listener() {

                      public void handleEvent(BaseEvent be) {
                        TableItem item=(TableItem) be.item;
                        if (item!=null) {
                                if (ordre==null) {
                                        ordre= (String) item.getValue(4);
                                        mediator.onInventoryItemSelected(ordre);
                                }
                                else {
                                        if (ordre.compareTo((String) item.getValue(4))==0) {
                                                ordre=null;
                                                table.deselect(be.rowIndex);
                                                mediator.onInventoryItemUnselected();
                                        }
                                        else {
                                                ordre= (String) item.getValue(4);
                                                mediator.onInventoryItemSelected(ordre);
                                        }
                                        
                                }
                        }
                      }
                });
                
        
        


        }
        
        
        /**
         * Suppression d'un ensemble d'element de la liste d'inventaire, on garde ici car s'applique a plusieurs elements
         * 
         */

        public void deleteElement() {

                setStatusDisabled();
                TableItem[] selection=table.getSelection();

                StringBuffer ids=new StringBuffer();
                for (int i = 0; i < selection.length; i++) {
                        ids.append((String)(((TableItem) selection[i]).getValue(4)));
                        if (i<(selection.length-1)) ids.append(",");
                } 
                
                if (ids.length()>0) {
                        
                        HTTPRequest.asyncPost(serviceBaseUrl + "/Inventory/" + user
                                                        + "/" + ids.toString(), "action=DELETE",
                                                        
                                                        new ResponseTextHandler() {
                                                                public void onCompletion(String str) {
                                                                                        mediator.onInventoryUpdated(location);
                                                                }
                                                        });
                }
        
                setStatusEnabled();

        }
        
        
        
        
        /**
         * Transmission de releve a Tela, on garde ici car s'applique a plusieurs elements
         */

        
        public void transmitElement() {
        
                setStatusDisabled();
                
                TableItem[] selection=table.getSelection();

                StringBuffer ids=new StringBuffer();
                for (int i = 0; i < selection.length; i++) {
                        ids.append((String)(((TableItem) selection[i]).getValue(4)));
                        if (i<(selection.length-1)) ids.append(",");
                } 
                
                if (ids.length()>0) {
                
                        HTTPRequest.asyncPost(serviceBaseUrl + "/InventoryTransmit/" + user
                                                        + "/" + ids.toString(), "transmission=1",
                                                        
                                                        new ResponseTextHandler() {
                                                                public void onCompletion(String str) {
                                                                                        update(); // Pour affichage logo 
                                                                }
                                                        });
                }
        
                setStatusEnabled();

                
        }
        
        
        /**
         * Recherche nombre d'enregistrement pour l'utilisateur et la localite en cours
         * 
         */
        
        public void updateCount () {
                
                setStatusDisabled();

                
                // Transformation de la date selectionne vers date time stamp
                
                String adate="all";
                if (date.compareTo("all")!=0) {
                        adate=date.substring(6,10)+"-"+date.substring(3,5)+"-"+date.substring(0,2)+" 00:00:00";
                }

                HTTPRequest.asyncGet(serviceBaseUrl + "/InventoryItemList/" + user + "/" + URL.encodeComponent(location) + "/" + adate  + "/" + URL.encodeComponent(search) +  "/" + URL.encodeComponent(station), 
                                new ResponseTextHandler() {

                                        public void onCompletion(String str) {

                                                JSONValue jsonValue = JSONParser.parse(str);
                                                JSONNumber jsonNumber;
                                                if ((jsonNumber = jsonValue.isNumber()) != null) {
                                                        count = (int) jsonNumber.getValue();
                                                //      if (location.compareTo("")==0) location="000null";
                                                        gotoEnd(); // Derniere page
                                                        update();
                                                }
                                        }
                                });

        }
        
        /**
         * Mise a jour de l'affichage, a partir des donnaes d'inventaire deja
         * saisies. La valeur de this.startIndex permet de determiner quelles
         * donnaes seront affichees
         * 
         */

        public void update() {

                
                
//   TODO : optimisation         (ne pas supprimer mais remplacer)
                

                
//      Ligne d'information 

                // Toutes date par defaut
                
                String adate="all";
                if (date.compareTo("all")!=0) {
                        adate=date.substring(6,10)+"-"+date.substring(3,5)+"-"+date.substring(0,2)+" 00:00:00";
                }

                HTTPRequest.asyncGet(serviceBaseUrl + "/InventoryItemList/" + user + "/" + URL.encodeComponent(location) +"/" + adate + "/" + URL.encodeComponent(search) + "/" + URL.encodeComponent(station) + "/"
                                + startIndex + "/" + VISIBLE_TAXON_COUNT,

                new ResponseTextHandler() {

                        public void onCompletion(String str) {

                                JSONValue jsonValue = JSONParser.parse(str);
                                JSONArray jsonArray;
                                JSONArray jsonArrayNested;
                                
                                int row=0;
                                int i=0;

                                if ((jsonArray = jsonValue.isArray()) != null) {
                                        
                                        StringBuffer observationText=null;

                                        int arraySize = jsonArray.size();
                                        
                                        for (i = 0; i < arraySize; ++i) {
                                                if ((jsonArrayNested = jsonArray.get(i).isArray()) != null) {
                                                        

                                                        Object[] values = new Object[5];
                                                        
                                                        observationText=new StringBuffer();
                                                                                                        
                                                        // Statut Observation transmise ?
                                                        
                                                        
                                                        String atransmit=((JSONString) jsonArrayNested .get(11)).stringValue();
                                                        
                                                        if (atransmit.compareTo("1")==0) {
                                                                values[0] = new Image("tela.gif");
                                                        }
                                                        else {
                                                                values[0] = new HTML("&nbsp;");
                                                        }
                                                        
                                                
                                                        // Nom saisi
                                                        
                                                        values[1] = new HTML("<b>"+Util.toCelString(((JSONString) jsonArrayNested .get(0)).toString())+"</b>");
                                                        
                                                        
                                                        
                                                    // Texte decrivant l'observation
                                                        
                                                        // Nom retenu
                                                        String aname=Util.toCelString(((JSONString) jsonArrayNested .get(2)).toString());
                                                        
                                                        if (aname.compareTo("null")==0) {
                                                        }
                                                        else {
                                                                observationText.append(aname+", ");
                                                        }
                                                        
                                                        // Num nomenclatural
                                                        String ann=((JSONString) jsonArrayNested .get(3)).stringValue();
                                                        
                                                        if (ann.compareTo("0")!=0) {
                                                                observationText.append(""+ann+"-");
                                                        }
                                                        else {
                                                                observationText.append("0-");
                                                        }

                                                        
                                                        // Num Taxonomique
                                                        
                                                        String ant=((JSONString) jsonArrayNested .get(4)).stringValue();
                                                        
                                                        if (ant.compareTo("0")!=0) {
                                                                observationText.append(ant+", ");
                                                        }
                                                        else {
                                                                observationText.append("0, ");
                                                        }

                                                        // Famille
                                                        String afamily=Util.toCelString(((JSONString) jsonArrayNested .get(5)).toString());
                                                        
                                                        if (afamily.compareTo("null")==0) {
                                                                //
                                                        }
                                                        else {
                                                                observationText.append(afamily+", ");
                                                        }

                                                        
                                                        String aloc=Util.toCelString(((JSONString) jsonArrayNested .get(6)).toString());
//                                                              Localisation - Lieu
                                                                
                                                                if (aloc.compareTo("000null")==0) {
                                                                        if (observationText.length()==0) {
                                                                                observationText.append("Commune absente");
                                                                        }
                                                                        else {
                                                                                observationText.append("commune absente");
                                                                        }
                                                                }
                                                                else {
                                                                        if (observationText.length()==0) {
                                                                                observationText.append("Commune de "+aloc);
                                                                        }
                                                                        else {
                                                                                observationText.append("commune de "+aloc);
                                                                        }
                                                                                
                                                                }
                                                        
                                                                
                                                                String alieudit=Util.toCelString(((JSONString) jsonArrayNested .get(9)).toString());
                                                        
//                                                              Localisation - Lieu dit
                                                                
                                                                if (alieudit.compareTo("000null")!=0) {
                                                                        observationText.append(", "+alieudit);
                                                                }
                                                        
                                                                String acomment=Util.toCelString(((JSONString) jsonArrayNested .get(10)).toString());
//                                                              Commentaire
                                                                
                                                                if (acomment.compareTo("null")!=0) {
                                                                        observationText.append(", "+acomment);
                                                                }

                                                                
                                                                String adate=((JSONString) jsonArrayNested .get(8)).stringValue();
                                                                
//                                                              Date 
                                                                if (adate.compareTo("0000-00-00 00:00:00")!=0) {
                                                                        values[3]=new HTML("<b>"+adate+"</b>"); 
                                                                }
                                                                else { 
                                                                        values[3] = new HTML("&nbsp;");
                                                                }
                                                                        


                                                        values[2] = observationText;
                                                        
                                                        String aordre=((JSONString) jsonArrayNested.get(7)).stringValue();
                                                
                                                        // Numero d'ordre (cache)
                                                        
                                                        values[4] = aordre;
                                                        
                                                        
                                                
                                                        
                                                if (i>=table.getItemCount()) {
                                                        TableItem item = new TableItem(values);
                                                        table.add(item);
                                                        }
                                                        else {
                                                                TableItem item=table.getItem(i);
                                                                item.setValue(0,values[0]);
                                                                item.setValue(1,values[1]);
                                                                item.setValue(2,values[2]);
                                                                item.setValue(3,values[3]);
                                                                item.setValue(4,values[4]);
                                                        }
                                                
                                                // Spagetti
                                                        if (ordre!=null) {
                                                                if (aordre.compareTo(ordre)==0) {
                                                                        table.select(i);
                                                                }
                                                                else {
                                                                        table.deselect(i);
                                                                }
                                                        }
                                                        
                                                }

                                        }
                                }

                                // Suppression fin ancien affichage
                                if (i<table.getItemCount()) {
                                         for (int j = table.getItemCount() -1 ; j >= i; j--) {
                                                 TableItem item=table.getItem(j);
                                                 table.remove(item);
                                         }
                                }
                                
                                setStatusEnabled();
                                
                                
                        }
                });
                

        }


        /**
         * Affichage message d'attente et desactivation navigation
         * 
         * @param
         * @return void
         */

        private void setStatusDisabled() {

                navBar.gotoFirst.setEnabled(false);
                navBar.gotoPrev.setEnabled(false);
                navBar.gotoNext.setEnabled(false);
                navBar.gotoEnd.setEnabled(false);

                navBar.status.setText("Patientez ...");
        }

        /**
         * Affichage numero de page et gestion de la navigation
         * 
         */

        private void setStatusEnabled() {

                // Il y a forcemment un disabled avant d'arriver ici

                if (count > 0) {

                        if (startIndex >= VISIBLE_TAXON_COUNT) { // Au dela de la
                                                                                                                // premiere page
                                navBar.gotoPrev.setEnabled(true);
                                navBar.gotoFirst.setEnabled(true);
                                if (startIndex < (count - VISIBLE_TAXON_COUNT)) { // Pas la
                                                                                                                                        // derniere
                                                                                                                                        // page
                                        navBar.gotoNext.setEnabled(true);
                                        navBar.gotoEnd.setEnabled(true);
                                        navBar.status.setText((startIndex + 1) + " - "
                                                        + (startIndex + VISIBLE_TAXON_COUNT) + " sur " + count );
                                } else { // Derniere page
                                        navBar.status.setText((startIndex + 1) + " - " + count + " sur " + count );
                                }
                        } else { // Premiere page
                                if (count > VISIBLE_TAXON_COUNT) { // Des pages derrieres
                                        navBar.gotoNext.setEnabled(true);
                                        navBar.gotoEnd.setEnabled(true);
                                        navBar.status.setText((startIndex + 1) + " - "
                                                        + (startIndex + VISIBLE_TAXON_COUNT) + " sur " + count);
                                } else {
                                        navBar.status.setText((startIndex + 1) + " - " + count + " sur " + count);
                                }
                        }
                }

                else { // Pas d'inventaire, pas de navigation
                        navBar.status.setText("0 - 0 sur 0");
                }
        }

        /*
         * Positionnement index de parcours (this.startIndex) pour affichage de la
         * derniere page
         * 
         * @param
         * @return void
         */

        private void gotoEnd() {

                if ((count == 0) || (count % VISIBLE_TAXON_COUNT) > 0) {
                        startIndex = count - (count % VISIBLE_TAXON_COUNT);
                } else {
                        startIndex = count - VISIBLE_TAXON_COUNT;
                }

        }

        /*
         * Recherche en cours
         * 
         */
        
        public void setSearch(String search) {
                this.search = search;
        }

        
        /*
         * Localite en cours 
         * 
         */
        
        public void setLocation(String location) {
                this.location = location;
        }


        
        /*
         * Station en cours 
         * 
         */

        public void setStation(String station) {
                this.station = station;
        }

        
        
        /*
         * Date en cours 
         * 
         */
        

        public void setDate(String date) {
                this.date = date;
        }

        
        /*
         * Utilisateur en cours 
         * 
         */
        

        
        public void setUser(String user) {
                this.user = user;
        }


        public void displayFilter() {
                
                
        
        // Mise a jour boutton export feuille de calcul
        
        mediator.getActionView().getExportButton().setHTML("<a href=\""+mediator.getServiceBaseUrl()+"/InventoryExport/" 
                        +  user + "/"
                        + URL.encodeComponent(location) + "/"
                        + URL.encodeComponent(station)+ "/"
                        + URL.encodeComponent(search) + "/"
                        + date + 
                        "\">"+"Export&nbsp;tableur</a>");

        // Mise a jour ligne de selection 
        
        
        
        String com;
        if (location.compareTo("all")==0) {
                com="Toutes communes";
        }
        else {
                if (location.compareTo("000null")==0) {
                        com="Communes non renseign&eacute;es";
                }
                else {
                com="Commune de "+location;
                }
        }

        
        String dat;
        
        if (date.compareTo("all")==0) {
                dat=", toutes p&eacute;riodes";
        }
        else {
                if (date.compareTo("00/00/0000")==0) {
                        dat=", p&eacute;riodes non renseign&eacute;es";
                }
                else {
                        dat=", le "+ date;
                }
        }

        
        String stat;
        
        if (station.compareTo("all")==0) {
                stat=", toutes stations";
        }
        else {
                if (station.compareTo("000null")==0) {
                        stat=", stations non renseign&eacute;es";
                }
                else {
                        stat=", station "+ station;
                }
        }

        
        panel.getHeader().setText(com +  dat + stat );


        
}


}

/* +--Fin du code ---------------------------------------------------------------------------------------+
* $Log$
* Revision 1.8  2007-12-22 14:48:53  ddelon
* Documentation et refactorisation
*
* Revision 1.7  2007-09-17 19:25:34  ddelon
* Documentation
*
*/