Rev 30 | Blame | Compare with Previous | 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.http.client.URL;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 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;public class LocationFilterView {private static final String VALUE_UNKNOWN = "Inconnu";private String user;private String serviceBaseUrl = null;private String id_location = "all";private String location = "all";private String lieudit = "all";private Mediator mediator = null;private SALB[] salbs=null;private TALB[] talbs=null;private FALB[] falbs=null;private Model root = null;private RALB ralb = null;private Tree tree = null;private TreeViewer viewer=null;public LocationFilterView(Mediator med,InventoryFilterView inventoryFilterView) {mediator=med;user=mediator.getUser();serviceBaseUrl = mediator.getServiceBaseUrl();mediator.registerLocationFilterView(this);initTree();ExpandItem locationfilter = new ExpandItem();locationfilter.setText("Lieux");ExpandBar expandBar=inventoryFilterView.getExpandBar();locationfilter.getContainer().add(tree);expandBar.add(locationfilter);locationfilter.setExpanded(true);}// Initialisation noeud de basevoid initTree() {// Enregistrement Mise à jour asynchroneIAsyncTreeContentProvider 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 FALB) { // Stationreturn false;}else {if (parent instanceof SALB) { // Departementif (((SALB)parent).toString().compareTo(VALUE_UNKNOWN)==0)return false;elsereturn true;}else {return true;}}}public void getChildren(final Object parent, final IAsyncContentCallback callback) {if (parent instanceof SALB) {// Departement + communeHTTPRequest.asyncGet(serviceBaseUrl + "/InventoryLocationList/" + user + "/"+ ((SALB) 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();talbs=new TALB[arraySize];for (i = 0; i < arraySize; ++i) {if ((jsonArrayNested = jsonArray.get(i).isArray()) != null) {String dep=Util.toCelString(((JSONString)jsonArrayNested.get(0)).toString());if (dep.compareTo("000null")==0) dep=VALUE_UNKNOWN;talbs[i]=new TALB((SALB) parent,dep);}}callback.setElements(talbs);}}});}else if (parent instanceof TALB) {// Commune + stationHTTPRequest.asyncGet(serviceBaseUrl + "/InventoryLocationList/" + user + "/"+ ((TALB) 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();falbs=new FALB[arraySize];for (i = 0; i < arraySize; ++i) {if ((jsonArrayNested = jsonArray.get(i).isArray()) != null) {String com=Util.toCelString(((JSONString)jsonArrayNested.get(0)).toString());if (com.compareTo("000null")==0) com=VALUE_UNKNOWN;falbs[i]=new FALB((TALB) parent, com);}}callback.setElements(falbs);}}});}else if (parent instanceof RALB) {// Rafraichissement RacineHTTPRequest.asyncGet(serviceBaseUrl + "/InventoryLocationList/" + 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();salbs=new SALB[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("000null")==0) all=VALUE_UNKNOWN;salbs[i]=new SALB(all);}}callback.setElements(salbs);Model m=new Model();m.set("name",id_location);viewer.setExpanded(m,true);}}});}}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 TALB) { // Communelocation=m.getAsString("name");if (location.compareTo(VALUE_UNKNOWN)==0) location="000null";id_location=m.getAsString("salb");if (id_location.compareTo(VALUE_UNKNOWN)==0) id_location="000null";lieudit="all";mediator.onLocationSelected(id_location,location,lieudit);}elseif (m instanceof FALB) { // Stationid_location=m.getAsString("salb");if (id_location.compareTo(VALUE_UNKNOWN)==0) id_location="000null";location=m.getAsString("talb");if (location.compareTo(VALUE_UNKNOWN)==0) location="000null";lieudit=m.getAsString("name");if (lieudit.compareTo(VALUE_UNKNOWN)==0) lieudit="000null";mediator.onLocationSelected(id_location,location,lieudit);}elseif (m instanceof SALB) { // Departementid_location=m.getAsString("name");if (id_location.compareTo(VALUE_UNKNOWN)==0) id_location="000null";location="all";lieudit="all";mediator.onLocationSelected(id_location,location,lieudit);}if (m instanceof RALB) { // Paysid_location="all";location="all";lieudit="all";mediator.onLocationSelected(id_location,location,lieudit);}}});// Initialisation premier niveau (doublon obligatoire avec rafraichissement asynchrone)root = new Model();HTTPRequest.asyncGet(serviceBaseUrl + "/InventoryLocationList/" + 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();salbs=new SALB[arraySize];ralb = new RALB("France");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("000null")==0) all=VALUE_UNKNOWN;salbs[i]=new SALB(all);ralb.add(salbs[i]);}}root.add(ralb);viewer.setInput(root);}}});}public void setLocation(String location) {this.location = location;}public void setIDLocation(String id_location) {this.id_location = id_location;}public void setLieudit(String lieudit) {this.lieudit = lieudit;}public void setUser(String user) {this.user = user;}private class RALB extends Model { // Root Administrative level boundaries (pays)public RALB(String name) {set("name", name);set("path", URL.encodeComponent(name));}public String toString() {return getAsString("name");}}private class SALB extends Model { // Second Administrative level boundaries (departerment)public SALB(String name) {set("name", name);set("path", URL.encodeComponent(name));}public String toString() {return getAsString("name");}}private class TALB extends Model { // Tierce Administrative level boundaries (Commune)public TALB(SALB parent, String name) {set("name", name);set("salb", parent.get("name"));set("path", parent.get("path")+"/"+URL.encodeComponent(name));}public String toString() {return getAsString("name");}}private class FALB extends Model { // Forth Administrative level boundaries (Station)public FALB(TALB parent,String name) {set("name", name);set("salb", parent.get("salb"));set("talb", parent.get("name"));set("path", parent.get("path")+"/"+URL.encodeComponent(name));}public String toString() {return getAsString("name");}}public void update() {Model m=new Model();m.set("name",id_location);if (viewer.findItem(m)!=null) {viewer.refresh(m); // Rafraichissement branche departemental}else { // Pas de branche departementale ? : rafraichissement totalModel r=new Model();r.set("name","France");viewer.refresh(r);}}}