Subversion Repositories eFlore/Archives.cel-v1

Rev

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

/*
 * Copyright 2006 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.tela_botanica.client;


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.DockPanel;
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 LocationList extends Composite {
        
        
        
        // 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 HTML status = new HTML();
                

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


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

                        bar.setVerticalAlignment(DockPanel.ALIGN_MIDDLE);
                        
                                        
                        
        
                        
                }

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

        }



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

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

                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 NavBar navBar=null;

                
                private int count = 65000;
                
                // Tous selectionné 
                private int  selectedRow = -1;
                
                private Mediator mediator = null;

                
                public LocationList(Mediator med) {
                        
                        mediator=med;

                        mediator.registerLocationList(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("location-ListHeader");

                        
                        header.setWidget(0, 1,navBar);
                        


                        // Mise en forme de l'entree "Toutes localités"

                        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, "location-SelectedRow");
                                            mediator.onLocationSelected("all");
                                          }

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

                        
                        // Mise en forme du contenu 

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

                        table.setStyleName("location-ListElement");

                        // Mise en forme barre navigation
                        
                        navBar.setWidth("100%");
                        
                        outer.add(header);
                        inner.add(selector); // Toutes localités
                        inner.add(table);
                        inner.setStyleName("location-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 loc=table.getText(row,cell);
                                              if (loc.compareTo(VALUE_UNKNOWN)!=0) {
                                                  location=loc;
                                                  mediator.onLocationSelected(table.getText(row,cell));
                                              }
                                              else {
                                                  location="000null";
                                                  mediator.onLocationSelected("000null");
                                              }
                                            
                                          }

                    });
                    
                        styleRow(selectedRow, false);
                selector.getRowFormatter().addStyleName(0, "location-SelectedRow");
                   // mediator.onLocationSelected("all");
                                
                    
                        //updateCount();
                        // update()
                        
                        initWidget(outer);


  }

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

//                      HTTPRequest.asyncGet(serviceBaseUrl + "/InventoryLocationList/" + user + "/" + location + "/"  ,
                        HTTPRequest.asyncGet(serviceBaseUrl + "/InventoryLocationList/" + user ,
                                        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, "location-SelectedRow");
                              if (selected)
                                table.getRowFormatter().addStyleName(row, "location-SelectedRow");
                              else
                                if (row < table.getRowCount()) { 
                                        table.getRowFormatter().removeStyleName(row, "location-SelectedRow");
                                }
                            }
                          }

                  
        /**
         * 
         * Mise a jour de l'affichage, à partir des données d'inventaire deja
         * saisies. La valeur de this.startIndex permet de determiner quelles
         * données seront affichées
         * 
         */     

                public void update() {

                        
                        setStatusDisabled();

//                      HTTPRequest.asyncGet(serviceBaseUrl + "/InventoryLocationList/" + user + "/" + location + "/" +  
                        HTTPRequest.asyncGet(serviceBaseUrl + "/InventoryLocationList/" + user + "/" +  
                                        + startIndex + "/" + VISIBLE_LOCATION_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 loc=((JSONString)jsonArrayNested.get(0)).stringValue();
                                                                
                                                                if (loc.compareTo("000null")!=0) {
                                                                        table.setText(row, 0,loc); 
                                                                }
                                                                else {
                                                                        table.setText(row, 0,VALUE_UNKNOWN);
                                                                }
                                                                
                                                                if (loc.compareTo(location)==0) {
                                                                  styleRow(row, true);
                                                                }
                                                                else {
                                                                  styleRow(row, false);
                                                                }

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

                                                }
                                        }
                                        
                                        if (location.compareTo("all")==0) {
                                                        selector.getRowFormatter().addStyleName(0, "location-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_LOCATION_COUNT) > 0) {
                                startIndex = count - (count % VISIBLE_LOCATION_COUNT);
                        } else {
                                startIndex = count - VISIBLE_LOCATION_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);

                        setStatusText("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_LOCATION_COUNT) { // Au dela de la
                                                                                                                        // premiere page
                                        navBar.gotoPrev.setEnabled(true);
                                        navBar.gotoFirst.setEnabled(true);
                                        if (startIndex < (count - VISIBLE_LOCATION_COUNT)) { // Pas la
                                                                                                                                                // derniere
                                                                                                                                                // page
                                                navBar.gotoNext.setEnabled(true);
                                                navBar.gotoEnd.setEnabled(true);
                                                setStatusText((startIndex + 1) + " - "
                                                                + (startIndex + VISIBLE_LOCATION_COUNT) + " sur " + count );
                                        } else { // Derniere page
                                                setStatusText((startIndex + 1) + " - " + count + " sur " + count );
                                        }
                                } else { // Premiere page
                                        if (count > VISIBLE_LOCATION_COUNT) { // Des pages derrieres
                                                navBar.gotoNext.setEnabled(true);
                                                navBar.gotoEnd.setEnabled(true);
                                                setStatusText((startIndex + 1) + " - "
                                                                + (startIndex + VISIBLE_LOCATION_COUNT) + " sur " + count);
                                        } else {
                                                setStatusText((startIndex + 1) + " - " + count + " sur " + count);
                                        }
                                }
                        }

                        else { // Pas d'inventaire, pas de navigation
                                setStatusText("0 - 0 sur 0");
                        }
                }



                private void setStatusText(String text) {
                        navBar.status.setText(text);
                }
                
                public void setUser(String user) {
                        this.user = user;
                }

}