Subversion Repositories eFlore/Archives.cel-v1

Rev

Go to most recent revision | 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.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.FlexTable;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.SourcesTableEvents;
import com.google.gwt.user.client.ui.TableListener;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * A tree displaying a set of email folders.
 */
public class LocationList extends Composite {


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

                private Grid header = new Grid(1, 1);
                
                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";
                
                // 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();
                        
                
                        // Mise en forme du header

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

                        header.setStyleName("location-ListHeader");

                        
                        header.setHTML(0, 0, "Localités                   ");  // yeah !

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


                        // Mise en forme du selecteur

                        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 de la table.

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

                        table.setStyleName("location-ListElement");

                        // Mise en forme barre navigation

                        outer.add(header);
                        inner.add(selector);
                        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) {
                                                  mediator.onLocationSelected(table.getText(row,cell));
                                              }
                                              else {
                                                  mediator.onLocationSelected("000null");
                                              }
                                            
                                          }

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


  }
                
                
        
                  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() {


                        // TODO : ne pas recreer la table a chaque fois ... : parcouris le retour, le comparer au present
                        // et inserer ou supprimer s'il le faut.

                        // TODO : ou alors prevoir un update pour les ajouts (forcemment à la fin) et un update pour les suppressions ...
                        // Sauf qu'il y a les chgts de pages ... : oui, la un fait un update normal ...
                        
                        // TODO : despaghettiser
                        
                        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;

                                        for (int i = table.getRowCount() - 1; i >= 0; i--) {
                                                        table.removeRow(i);
                                        }
                                        
                                        if (location.compareTo("all")==0) {
                                                styleRow(selectedRow, false);
                                                selector.getRowFormatter().addStyleName(0, "location-SelectedRow");
                                        }

                                                
                                        if ((jsonArray = jsonValue.isArray()) != null) {
                                                int arraySize = jsonArray.size();
                                                for (int i = 0; i < arraySize; ++i) {
                                                        if ((jsonArrayNested = jsonArray.get(i).isArray()) != null) {
                                                                int row = table.insertRow(table.getRowCount());
                                                                // 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);
                                                                }

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

                                                }
                                        }

                                }
                        });

                }

                public void setLocation(String location) {
                        this.location = location;
                }
                
                
                                

}