/webportal/src/main/java/org/ala/spatial/gazetteer/AutoComplete.java
Java | 112 lines | 79 code | 23 blank | 10 comment | 11 complexity | d2b3e9d3b8554ebca0d63bea39679001 MD5 | raw file
1package org.ala.spatial.gazetteer; 2 3import org.apache.commons.httpclient.HttpClient; 4import au.org.emii.portal.composer.MapComposer; 5import au.org.emii.portal.util.GeoJSONUtilities; 6import org.zkoss.zul.Combobox; 7import org.zkoss.zk.ui.event.InputEvent; 8import java.util.Iterator; 9import net.sf.json.JSONArray; 10import net.sf.json.JSONObject; 11import org.ala.spatial.util.CommonData; 12import org.apache.commons.httpclient.methods.GetMethod; 13import org.zkoss.zul.Comboitem; 14import org.zkoss.zk.ui.Page; 15 16public class AutoComplete extends Combobox { 17 private String gazServer = null; 18 19 private GeoJSONUtilities geoJSONUtilities = null; 20 21 public AutoComplete() { 22 refresh(""); //init the child comboitems 23 } 24 25 public AutoComplete(String value) { 26 super(value); //it invokes setValue(), which inits the child comboitems 27 } 28 29 30 31 @Override 32 public void setValue(String value) { 33 super.setValue(value); 34 } 35 36 /** 37 * Gets the main pages controller so we can add a 38 * layer to the map 39 * @return MapComposer = map controller class 40 */ 41 private MapComposer getThisMapComposer() { 42 43 MapComposer mapComposer = null; 44 Page page = this.getPage(); 45 mapComposer = (MapComposer) page.getFellow("mapPortalPage"); 46 47 return mapComposer; 48 } 49 50 /** Listens for what a user is entering. 51 * @param evt 52 */ 53 public void onChanging(InputEvent evt) { 54 if (!evt.isChangingBySelectBack()) { 55 refresh(evt.getValue()); 56 } 57 } 58 59 /** Refresh comboitem based on the specified value. 60 */ 61 private void refresh(String val) { 62 String searchString = val.trim().replaceAll("\\s+", "+"); 63 64 searchString = (searchString.equals(""))?"a":searchString; 65 66 try { 67 HttpClient client = new HttpClient(); 68 GetMethod get = new GetMethod(CommonData.layersServer + "/search?limit=40&q=" + searchString); 69 get.addRequestHeader("Accept", "application/json, text/javascript, */*"); 70 int result = client.executeMethod(get); 71 String slist = get.getResponseBodyAsString(); 72 73 JSONArray ja = JSONArray.fromObject(slist); 74 75 if(ja == null) { 76 return; 77 } 78 79 Iterator it = getItems().iterator(); 80 81 for(int i=0;i<ja.size();i++) { 82 JSONObject jo = ja.getJSONObject(i); 83 String itemString = jo.getString("name"); 84 String description = (jo.containsKey("description") ? jo.getString("description"): "") 85 + " (" + jo.getString("fieldname") + ")" ; 86 87 if (it != null && it.hasNext()) { 88 Comboitem ci = (Comboitem) it.next(); 89 ci.setLabel(itemString); 90 ci.setValue(jo); 91 ci.setDescription(description); 92 } else { 93 it = null; 94 Comboitem ci = new Comboitem(); 95 ci.setLabel(itemString); 96 ci.setValue(jo); 97 ci.setDescription(description); 98 ci.setParent(this); 99 } 100 } 101 102 while (it != null && it.hasNext()) { 103 it.next(); 104 it.remove(); 105 } 106 107 } catch (Exception e) { 108 e.printStackTrace(); 109 } 110 } 111 112}