PageRenderTime 24ms CodeModel.GetById 15ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/webportal/src/main/java/org/ala/spatial/analysis/web/AreaPolygon.java

http://alageospatialportal.googlecode.com/
Java | 138 lines | 90 code | 21 blank | 27 comment | 16 complexity | 12f2246fcd9615283717c7f578ae5297 MD5 | raw file
  1package org.ala.spatial.analysis.web;
  2
  3import au.org.emii.portal.composer.MapComposer;
  4import au.org.emii.portal.composer.UtilityComposer;
  5import au.org.emii.portal.menu.MapLayer;
  6import au.org.emii.portal.menu.MapLayerMetadata;
  7import au.org.emii.portal.settings.SettingsSupplementary;
  8import net.sf.json.JSONArray;
  9import net.sf.json.JSONException;
 10import net.sf.json.JSONObject;
 11import org.ala.spatial.util.CommonData;
 12import org.ala.spatial.util.LayersUtil;
 13import org.ala.spatial.util.Util;
 14import org.apache.commons.httpclient.HttpClient;
 15import org.apache.commons.httpclient.methods.PostMethod;
 16import org.zkoss.zk.ui.Page;
 17import org.zkoss.zk.ui.event.Event;
 18import org.zkoss.zul.Button;
 19import org.zkoss.zul.Textbox;
 20
 21/**
 22 *
 23 * @author Adam
 24 */
 25public class AreaPolygon extends AreaToolComposer {
 26
 27    private Textbox displayGeom;
 28    Button btnNext; 
 29    //String layerName;
 30    Textbox txtLayerName;
 31    Button btnClear;
 32
 33    @Override
 34    public void afterCompose() {
 35        super.afterCompose();
 36
 37        txtLayerName.setValue(getMapComposer().getNextAreaLayerName("My Area"));
 38    }
 39
 40    public void onClick$btnNext(Event event) {
 41        //reapply layer name
 42        getMapComposer().getMapLayer(layerName).setDisplayName(txtLayerName.getValue());
 43        getMapComposer().redrawLayersList();
 44        ok = true;
 45        this.detach();
 46    }
 47
 48    public void onClick$btnClear(Event event) {
 49        MapComposer mc = getThisMapComposer();
 50        if(layerName != null && mc.getMapLayer(layerName) != null) {
 51            mc.removeLayer(layerName);
 52        }
 53        String script = mc.getOpenLayersJavascript().addPolygonDrawingTool();
 54        mc.getOpenLayersJavascript().execute(mc.getOpenLayersJavascript().iFrameReferences + script);
 55        displayGeom.setValue("");
 56        btnNext.setDisabled(true);
 57        btnClear.setDisabled(true);
 58    }
 59
 60    public void onClick$btnCancel(Event event) {
 61        MapComposer mc = getThisMapComposer();
 62        if(layerName != null && mc.getMapLayer(layerName) != null) {
 63            mc.removeLayer(layerName);
 64        }
 65        this.detach();
 66    }
 67
 68    /**
 69     *
 70     * @param event
 71     */
 72    public void onSelectionGeom(Event event) {
 73        String selectionGeom = (String) event.getData();
 74
 75        try {
 76
 77            String wkt = "";
 78            if (selectionGeom.contains("NaN NaN")) {
 79                displayGeom.setValue("");
 80                //  lastTool = null;
 81            } else if (selectionGeom.startsWith("LAYER(")) {
 82                //reset stored size
 83                // storedSize = null;
 84                //get WKT from this feature
 85                String v = selectionGeom.replace("LAYER(", "");
 86                //FEATURE(table name if known, class name)
 87                v = v.substring(0, v.length() - 1);
 88                
 89                wkt = Util.wktFromJSON(getMapComposer().getMapLayer(v).getGeoJSON());
 90                displayGeom.setValue(wkt);
 91
 92                //calculate area is not populated
 93//                if (storedSize == null) {
 94//                    storedSize = getAreaOfWKT(wkt);
 95//                }
 96            } else {
 97                wkt = selectionGeom;
 98                displayGeom.setValue(wkt);
 99            }
100
101            //get the current MapComposer instance
102            MapComposer mc = getThisMapComposer();
103
104            //add feature to the map as a new layer
105            if (wkt.length() > 0) {
106                layerName = (mc.getMapLayer(txtLayerName.getValue()) == null)?txtLayerName.getValue():mc.getNextAreaLayerName(txtLayerName.getValue());
107                MapLayer mapLayer = mc.addWKTLayer(wkt, layerName, txtLayerName.getValue());
108                MapLayerMetadata md = mapLayer.getMapLayerMetadata();
109                if(md == null) {
110                    md = new MapLayerMetadata();
111                    mapLayer.setMapLayerMetadata(md);
112                }
113                md.setMoreInfo(LayersUtil.getMetadataForWKT("User drawn polygon", wkt));
114
115                btnNext.setDisabled(false);
116                btnClear.setDisabled(false);
117
118            }
119            //   rgAreaSelection.getSelectedItem().setChecked(false);
120
121        } catch (Exception e) {//FIXME
122        }
123    }
124
125    /**
126     * Gets the main pages controller so we can add a
127     * drawing tool to the map
128     * @return MapComposer = map controller class
129     */
130    private MapComposer getThisMapComposer() {
131
132        MapComposer mapComposer = null;
133        Page page = getPage();
134        mapComposer = (MapComposer) page.getFellow("mapPortalPage");
135
136        return mapComposer;
137    }    
138}