Subversion Repositories eFlore/Archives.cel-v1

Rev

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 net.mygwt.ui.client.data.Model;
import net.mygwt.ui.client.viewer.IAsyncContentCallback;
import net.mygwt.ui.client.viewer.IAsyncTreeContentProvider;
import net.mygwt.ui.client.viewer.IElementComparer;
import net.mygwt.ui.client.viewer.ISelectionChangedListener;
import net.mygwt.ui.client.viewer.ModelLabelProvider;
import net.mygwt.ui.client.viewer.SelectionChangedEvent;
import net.mygwt.ui.client.viewer.TreeViewer;
import net.mygwt.ui.client.viewer.Viewer;
import net.mygwt.ui.client.widget.ExpandBar;
import net.mygwt.ui.client.widget.ExpandItem;
import net.mygwt.ui.client.widget.tree.Tree;

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;

public class DateFilterView extends Composite {
        
        
                private static final String VALUE_UNKNOWN = "Inconnus";


                private String user;

                private String serviceBaseUrl = null;

                private String year = "all";
                private String month = "all";
                private String day = "all";
                
                private Mediator mediator = null;

            private YEAR[] years=null;
            private MONTH[] months=null;
            private DAY[] days=null;
                private Model root = null; 
                private MILLENIUM millenium = null; 
                        
                private Tree tree = null;
                private TreeViewer viewer=null;
                

                
                public DateFilterView(Mediator med, InventoryFilterView inventoryFilterView) {
                        
                        mediator=med;

                        mediator.registerDateFilterView(this);
                        user=mediator.getUser();
                        serviceBaseUrl = mediator.getServiceBaseUrl();
                        
                        
                    initTree(); 

                                     
                        ExpandItem dateFilter = new ExpandItem();  
                        dateFilter.setText("Dates");  
                        
                                
                        ExpandBar expandBar=inventoryFilterView.getExpandBar();
                        dateFilter.getContainer().add(tree);
                        expandBar.add(dateFilter);  
                                        


  }


//               Initialisation noeud de base
                
                  void initTree() {
                                
                          
                          // Enregistrement Mise à jour asynchrone
                          
                           IAsyncTreeContentProvider cp = new IAsyncTreeContentProvider() {  
                                
                           public Object[] getElements(Object input) {  
                                      Model m = (Model) input;  
                                       return m.getChildren().toArray();  
                           }  
                                     
                           public Object getParent(Object element) {  
                                   return ((Model) element).getParent();  
                           }  
                     
                           public boolean hasChildren(Object parent) {  
                                   
                                  if (parent instanceof DAY) {  // Jour
                                          return false;  
                                      }
                                  
                                  else  {
                                          if (parent instanceof YEAR) {  // Annee
                                                  if (((YEAR)parent).toString().compareTo(VALUE_UNKNOWN)==0)
                                                          return false; 
                                                  else 
                                                          return true;  
                                          }
                              
                                          else {
                                           return true;  
                                          }
                                  }
                           }
                           
                            public void getChildren(final Object parent, final IAsyncContentCallback callback) {  

                                if (parent instanceof YEAR) {  
                                          
                                          // Annee + mois
                                          
                                           HTTPRequest.asyncGet(serviceBaseUrl + "/InventoryDateList/" + user + "/"  
                                                                        + ((YEAR) parent).getAsString("path"),

                                                                        new ResponseTextHandler() {

                                                                                public void onCompletion(String str) {
                                                                                        
                                                                                        JSONValue jsonValue = JSONParser.parse(str);
                                                                                        JSONArray jsonArray;
                                                                                        JSONArray jsonArrayNested;
                                                                        
                                                                                        int i=0;
                                                                                        
                                                                                        if ((jsonArray = jsonValue.isArray()) != null) {
                                                                                                int arraySize = jsonArray.size();
                                                                                                months=new MONTH[arraySize];
                                                                                                for (i = 0; i < arraySize; ++i) {
                                                                                                        if ((jsonArrayNested = jsonArray.get(i).isArray()) != null) {
                                                                                                                String month=Util.toCelString(((JSONString)jsonArrayNested.get(0)).toString());
                                                                                                                if (month.compareTo("0")==0) month=VALUE_UNKNOWN;
                                                                                                                months[i]=new MONTH((YEAR) parent,month);
                                                                                                        }
                                                                                                }
                                                                                                callback.setElements(months);  
                                                                                        }
                                                                                        

                                                                                }
                                                                        });
                                           
                                         }  
                                  else if (parent instanceof MONTH) {  
                                          
                                          // Mois + jours
                                          
                                           HTTPRequest.asyncGet(serviceBaseUrl + "/InventoryDateList/" + user + "/"  
                                                                        + ((MONTH) parent).getAsString("path") ,
                                                                        
                                                                        new ResponseTextHandler() {

                                                                                public void onCompletion(String str) {
                                                                                        
                                                                                        JSONValue jsonValue = JSONParser.parse(str);
                                                                                        JSONArray jsonArray;
                                                                                        JSONArray jsonArrayNested;
                                                                        
                                                                                        int i=0;
                                                                                        
                                                                                        if ((jsonArray = jsonValue.isArray()) != null) {
                                                                                                int arraySize = jsonArray.size();
                                                                                                days=new DAY[arraySize];
                                                                                                for (i = 0; i < arraySize; ++i) {
                                                                                                        if ((jsonArrayNested = jsonArray.get(i).isArray()) != null) {
                                                                                                                String day=Util.toCelString(((JSONString)jsonArrayNested.get(0)).toString());
                                                                                                                if (day.compareTo("0")==0) day=VALUE_UNKNOWN;
                                                                                                                days[i]=new DAY((MONTH) parent, day);
                                                                                                        }
                                                                                                }
                                                                                                callback.setElements(days);  
                                                                                                
                                                                                        }
                                                                                        

                                                                                }
                                                                        });
                                           

                                  }
                                        else if (parent instanceof MILLENIUM) {  
                                                
                                                 // Rafraichissement Racine 
                                                  
                                                   HTTPRequest.asyncGet(serviceBaseUrl + "/InventoryDateList/" + user ,  
                                                                                
                                                                                new ResponseTextHandler() {

                                                                                        public void onCompletion(String str) {
                                                                                                
                                                                                                JSONValue jsonValue = JSONParser.parse(str);
                                                                                                JSONArray jsonArray;
                                                                                                JSONArray jsonArrayNested;
                                                                                
                                                                                                int i=0;
                                                                                                
                                                                                                if ((jsonArray = jsonValue.isArray()) != null) {
                                                                                                        int arraySize = jsonArray.size();
                                                                                                        years=new YEAR[arraySize];
                                                                                                        for (i = 0; i < arraySize; ++i) {
                                                                                                                if ((jsonArrayNested = jsonArray.get(i).isArray()) != null) {
                                                                                                                        String all=Util.toCelString(((JSONString)jsonArrayNested.get(0)).toString());
                                                                                                                        if (all.compareTo("0")==0) all=VALUE_UNKNOWN;
                                                                                                                        years[i]=new YEAR(all);
                                                                                                                }
                                                                                                        }

                                                                                                        callback.setElements(years);  
                                                                                                        
                                                                                                }
                                                                                                

                                                                                        }
                                                                                });

                                          

                                        }

                            }
                                     
                                     
                                    public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {  
                                     
                                    }  
                                     
                                 };  
                          
                                 tree = new Tree();  
                                
                                 viewer = new TreeViewer(tree);  
                                 viewer.setContentProvider(cp);  
                                 
                                 viewer.setLabelProvider(new ModelLabelProvider());

                                 viewer.setComparer(new IElementComparer() {
                                         
                                         public boolean equals(Object a, Object b) {
                                                
                                                 if ((a==null) || b==null) 
                                                         return false;
                                                 if (((Model) a).getAsString("name").compareTo(((Model) b).getAsString("name"))==0) {
                                                         return true;
                                                 }
                                                 else return false;
                                                 
                                         }
                                                 
                                 }
                                 );
                                 
                                 
                                   viewer.addSelectionListener(new ISelectionChangedListener() {  
                                        public void selectionChanged(SelectionChangedEvent se) {  
                                          Model m = (Model) se.getSelection().getFirstElement();  
                                       //   Info.show("Selection Changed", "{0} was selected", m.getAsString("path"));  
                                         
                                          if (m instanceof MONTH) { // Mois
                                                  month=m.getAsString("name");
                                                  if (month.compareTo(VALUE_UNKNOWN)==0) month="0";
                                                  year=m.getAsString("year");
                                                  if (year.compareTo(VALUE_UNKNOWN)==0) year="0";
                                                  day="all";
                                                  mediator.onDateSelected(year,month,day);
                                          }
                                          else 
                                                  if (m instanceof DAY) { // Jour
                                                          month=m.getAsString("month");
                                                          if (month.compareTo(VALUE_UNKNOWN)==0) month="0";
                                                          year=m.getAsString("year");
                                                          if (year.compareTo(VALUE_UNKNOWN)==0) year="0";
                                                          day=m.getAsString("name");
                                                          if (day.compareTo(VALUE_UNKNOWN)==0) day="0";
                                                          mediator.onDateSelected(year,month,day);

                                                  }
                                          
                                                  else 
                                                         
                                                  if (m instanceof YEAR) { // Annee
                                                          month="all";
                                                          year=m.getAsString("name");
                                                          if (year.compareTo(VALUE_UNKNOWN)==0) year="0";
                                                          day="all";
                                                          mediator.onDateSelected(year,month,day);
                                                  }

                                                        if (m instanceof MILLENIUM) { // Tous
                                                                month="all";
                                                                year="all";
                                                                day="all";
                                                                mediator.onDateSelected(year,month,day);
                                                        }

                                          
                                        }  
                        });  
                                 
                                 
                                 // Initialisation premier niveau (doublon obligatoire avec rafraichissement asynchrone)
                                root = new Model(); 
                                
                                HTTPRequest.asyncGet(serviceBaseUrl + "/InventoryDateList/" + user,
                                                        

                                                        new ResponseTextHandler() {

                                                                public void onCompletion(String str) {
                                                                        
                                                                        JSONValue jsonValue = JSONParser.parse(str);
                                                                        JSONArray jsonArray;
                                                                        JSONArray jsonArrayNested;
                                                        
                                                                        int i=0;
                                                                        
                                                                        if ((jsonArray = jsonValue.isArray()) != null) {
                                                                                int arraySize = jsonArray.size();
                                                                                years=new YEAR[arraySize];
                                                                                millenium = new MILLENIUM("Toutes");
                                                                                for (i = 0; i < arraySize; ++i) {
                                                                                        if ((jsonArrayNested = jsonArray.get(i).isArray()) != null) {
                                                                                                String all=Util.toCelString(((JSONString)jsonArrayNested.get(0)).toString());
                                                                                                if (all.compareTo("0")==0) all=VALUE_UNKNOWN;
                                                                                                years[i]=new YEAR(all);
                                                                                                millenium.add(years[i]);
                                                                                        }
                                                                                }
                                                                                root.add(millenium);
                                                                                viewer.setInput(root);  
                                                                        }
                                                                        

                                                                }
                                                        });
                                 
                      }

                                

                  

                private class MILLENIUM extends Model {  // Root 

                                  public MILLENIUM(String name) {
                                    set("name", name);
                                    set("path", name);
                                  }

                                  public String toString() {
                                    return getAsString("name");
                                  }

                 }

                        
                        private class YEAR extends Model {  // Annee

                                  public YEAR(String name) {
                                    set("name", name);
                                    set("path", name);
                                  }

                                  public String toString() {
                                            return getAsString("name");
                                  }

                        }

                        
                        private class MONTH extends Model {  // Mois

                                  public MONTH(YEAR parent, String name) {
                                    set("name", name);
                                    set("year", parent.get("name"));
                                    set("path", parent.get("path")+"/"+name);
                                  }

                                  public String toString() {
                                            return getAsString("name");

                                  }

                        }
                        
                                
                        private class DAY extends Model {  // Jour

                                  public DAY(MONTH parent,String name) {
                                    set("name", name);
                                    set("year", parent.get("year"));
                                    set("month", parent.get("name"));
                                    set("path", parent.get("path")+"/"+name);
                                  }

                                  public String toString() {
                                            return getAsString("name");
                                  }

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

                
                public void setYear(String year) {
                        this.year = year;
                }

                public void setMonth(String month) {
                        this.month = month;
                }
        
                public void setDay(String day) {
                        this.day = day;
                }
        
                public void update() {
                
                        
                        Model r=new Model();
                        r.set("name","Toutes");
                        viewer.refresh(r); 
                
                }


}