/webportal/src/main/java/org/ala/spatial/gazetteer/AutoComplete.java

http://alageospatialportal.googlecode.com/ · Java · 112 lines · 79 code · 23 blank · 10 comment · 11 complexity · d2b3e9d3b8554ebca0d63bea39679001 MD5 · raw file

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