Subversion Repositories eFlore/Archives.cel-v1

Rev

Rev 14 | Blame | Last modification | View Log | RSS feed

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

/*
 * DateList.java  : filtrage des releves par date d'observation
 * 
 * 
 * 1: Le programme initialise les filtres communes, lieu-dit et dates)
 */


package org.tela_botanica.client;



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.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.SourcesTableEvents;
import com.google.gwt.user.client.ui.TableListener;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * A tree displaying a set of email folders.
 */
public class DateList extends Composite {
        
        
        // Debut Barre de navigation

        private class NavBar extends Composite implements ClickListener {

                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 HTML status = new HTML();
                

                public NavBar() {
                        
                        
                        HorizontalPanel bar = new HorizontalPanel();
                        
                        bar.setStyleName("navbar");
                        status.setStyleName("status");
                        
                        
                        bar.add(status);
                        bar.setCellHorizontalAlignment(status, HasHorizontalAlignment.ALIGN_RIGHT);
                        bar.setCellVerticalAlignment(status, HasVerticalAlignment.ALIGN_MIDDLE);
                        bar.setCellWidth(status, "100%");

                        bar.add(gotoFirst);
                        bar.add(gotoPrev);
                        bar.add(gotoNext);
                        bar.add(gotoEnd);
                        
                        
                        initWidget(bar);
                        
                }
                

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

        }

        // Fin Barre de navigation


                private static final int VISIBLE_DATE_COUNT = 10;
                private static final String VALUE_UNKNOWN = "Inconnues";

                private Grid header = new Grid(1, 2);
                
                private Grid selector = new Grid(1, 2);

                private FlexTable table = new FlexTable();

                private VerticalPanel outer = new VerticalPanel();
                private VerticalPanel inner = new VerticalPanel();

                private int startIndex = 0;

                private String user;

                private String serviceBaseUrl = null;

                private String location = "all";
                private String date = "all";
                

                private NavBar navBar=null;

                
                private int count = 0;
                
                // Tous selectionne 
                private int  selectedRow = -1;
                
                private Mediator mediator = null;

                
                public DateList(Mediator med) {
                        
                        mediator=med;

                        mediator.registerDateList(this);

                        user=mediator.getUser();
                        serviceBaseUrl = mediator.getServiceBaseUrl();
                        
                        navBar = new NavBar();
                        
                        // Mise en forme du header

                        

                        header.setCellSpacing(0);
                        header.setCellPadding(2);
                        header.setWidth("100%");

                        header.setStyleName("date-ListHeader");

                        
                        
                        header.setWidget(0, 1,navBar);
                        

//                      header.getCellFormatter().setWidth(0, 0, "100%");


                        // Mise en forme de l'entree "Toutes localites"

                        selector.setCellSpacing(0);
                        selector.setCellPadding(0);
                        selector.setWidth("100%");

                        selector.setHTML(0, 0, "Toutes");

                        selector.getCellFormatter().setWidth(0, 0, "100%");


                         // Hook up events.
                        selector.addTableListener(new TableListener () {
                        
                                  public void onCellClicked(SourcesTableEvents sender, int row, int cell) {
                                                styleRow(selectedRow, false);
                                        selector.getRowFormatter().addStyleName(0, "date-SelectedRow");
                                            mediator.onDateSelected("all");
                                            date="all";
                                          }

                    });
                        
                        selector.setStyleName("date-ListElement");

                        
                        // Mise en forme du contenu 

                        table.setCellSpacing(0);
                        table.setBorderWidth(0);
                        table.setCellPadding(2);
                        table.setWidth("100%");

                        table.setStyleName("date-ListElement");

                        
                        outer.add(header);
                        inner.add(selector); // Toutes localit�s
                        inner.add(table);
                        inner.setStyleName("date-List");
                        inner.setWidth("100%");
                        outer.setWidth("100%");
                        outer.add(inner);
                        
                         // Hook up events.
                    table.addTableListener(new TableListener () {
                        
                                  public void onCellClicked(SourcesTableEvents sender, int row, int cell) {
                                          
                                              selectRow(row);
                                              String adate=table.getText(row,cell);
                                              if (adate.compareTo(VALUE_UNKNOWN)!=0) {
                                                  date=adate;
                                                  mediator.onDateSelected(adate);
                                              }
                                              else {
                                                  date="00/00/0000";
                                                  mediator.onDateSelected("00/00/0000");
                                              }
                                            
                                          }

                    });
                    
                        styleRow(selectedRow, false);
                selector.getRowFormatter().addStyleName(0, "date-SelectedRow");
                                
                    
                        initWidget(outer);


  }

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

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

                                                public void onCompletion(String str) {
                                                        JSONValue jsonValue = JSONParser.parse(str);
                                                        JSONNumber jsonNumber;
                                                        if ((jsonNumber = jsonValue.isNumber()) != null) {
                                                                count = (int) jsonNumber.getValue();
                                                                update();
                                                        }
                                                }
                                        });

                }

        
                  private void selectRow(int row) {
                          
                    styleRow(selectedRow, false);
                    styleRow(row, true);

                    selectedRow = row;
                    
                  }
                  
                  
                  private void styleRow(int row, boolean selected) {
                            if (row != -1) {
                                   selector.getRowFormatter().removeStyleName(0, "date-SelectedRow");
                              if (selected)
                                table.getRowFormatter().addStyleName(row, "date-SelectedRow");
                              else
                                if (row < table.getRowCount()) { 
                                        table.getRowFormatter().removeStyleName(row, "date-SelectedRow");
                                }
                            }
                          }

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

                public void update() {

                        
                        setStatusDisabled();

//                      HTTPRequest.asyncGet(serviceBaseUrl + "/InventoryDateList/" + user + "/" + location + "/" + station + "/"
                        HTTPRequest.asyncGet(serviceBaseUrl + "/InventoryDateList/" + user + "/" + URL.encodeComponent(location) + "/" 
                                        + startIndex + "/" + VISIBLE_DATE_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) {
                                                int arraySize = jsonArray.size();
                                                for (i = 0; i < arraySize; ++i) {
                                                        if ((jsonArrayNested = jsonArray.get(i).isArray()) != null) {
                                                                if (i>=table.getRowCount()) {
                                                                         row = table.insertRow(table.getRowCount());
                                                                }
                                                                else {
                                                                        row = i;
                                                                }
                                                                // Lieu
                                                                
                                                                String adate=((JSONString)jsonArrayNested.get(0)).stringValue();
                                                                
                                                                if (adate.compareTo("0000-00-00 00:00:00")!=0) {
                                                                        table.setText(row, 0,adate); 
                                                                }
                                                                else {
                                                                        table.setText(row, 0,VALUE_UNKNOWN);
                                                                }
                                                                
                                                                if ((adate.compareTo(date)==0) || ( date.compareTo("00/00/0000")==0) && adate.compareTo("0000-00-00 00:00:00")==0 ) {
                                                                  styleRow(row, true);
                                                                }
                                                                else {
                                                                  styleRow(row, false);
                                                                }

                                                                table.getFlexCellFormatter().setWidth(row, 0, "100%");
                                                        }

                                                }
                                        }
                                        
                                        if (date.compareTo("all")==0) {
                                                        selector.getRowFormatter().addStyleName(0, "date-SelectedRow");
                                        }

                                        
                                        // Suppression fin ancien affichage
                                        if (i<table.getRowCount()) {
                                                 for (int j = table.getRowCount() -1 ; j >= i; j--) {
                                                         table.removeRow(j);
                                                 }
                                        }

                                        setStatusEnabled();


                                }
                        });

                }

                public void setLocation(String location) {
                        this.location = location;
                }
                
                
                        
                /*
                 * Positionnement index de parcours (this.startIndex) pour affichage de la
                 * derni�re page
                 * 
                 * @param
                 * @return void
                 */

                private void gotoEnd() {

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

                }
                
                
                /**
                 * Affichage message d'attente et d�sactivation 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_DATE_COUNT) { // Au dela de la
                                                                                                                        // premiere page
                                        navBar.gotoPrev.setEnabled(true);
                                        navBar.gotoFirst.setEnabled(true);
                                        if (startIndex < (count - VISIBLE_DATE_COUNT)) { // Pas la
                                                                                                                                                // derniere
                                                                                                                                                // page
                                                navBar.gotoNext.setEnabled(true);
                                                navBar.gotoEnd.setEnabled(true);
                                                navBar.status.setText((startIndex + 1) + " - "
                                                                + (startIndex + VISIBLE_DATE_COUNT) + " sur " + count );
                                        } else { // Derniere page
                                                navBar.status.setText((startIndex + 1) + " - " + count + " sur " + count );
                                        }
                                } else { // Premiere page
                                        if (count > VISIBLE_DATE_COUNT) { // Des pages derrieres
                                                navBar.gotoNext.setEnabled(true);
                                                navBar.gotoEnd.setEnabled(true);
                                                navBar.status.setText((startIndex + 1) + " - "
                                                                + (startIndex + VISIBLE_DATE_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");
                        }
                }



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

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

}

/* +--Fin du code ---------------------------------------------------------------------------------------+
* $Log$
* 
*/