com\esri\arcgis\sample\mapinformation\MapInformationPhaseListener.java
/* Copyright 2010 ESRI * * All rights reserved under the copyright laws of the United States * and applicable international laws, treaties, and conventions. * * You may freely redistribute and use this sample code, with or * without modification, provided you include the original copyright * notice and use restrictions. * * See the use restrictions. * */ package com.esri.arcgis.sample.mapinformation; import com.esri.adf.web.data.WebMap; import com.esri.adf.web.data.geometry.WebExtent; import com.esri.adf.web.faces.component.MapControl; import com.esri.adf.web.faces.renderkit.xml.ajax.AJAXUtil; import com.esri.adf.web.util.XMLUtil; import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; import javax.faces.component.UIComponent; import javax.faces.context.ExternalContext; import javax.faces.context.FacesContext; import javax.faces.event.PhaseEvent; import javax.faces.event.PhaseId; import javax.faces.event.PhaseListener; import org.w3c.dom.Document; import org.w3c.dom.Element; public class MapInformationPhaseListener implements PhaseListener { private static Logger logger = Logger.getLogger(com.esri.arcgis.sample.mapinformation.MapInformationPhaseListener.class.getName()); //This PhaseListener must be executed in the JSF's Restore View phase public PhaseId getPhaseId() { return PhaseId.APPLY_REQUEST_VALUES; } //No processing public void beforePhase(PhaseEvent phaseEvent) { //do nothing } //Process functionality based on request parameters public void afterPhase(PhaseEvent phaseEvent) { FacesContext facesContext = phaseEvent.getFacesContext(); ExternalContext externalContext = facesContext.getExternalContext(); Map params = externalContext.getRequestParameterMap(); //verify if request is to be processed by this PhaseListener if (! "mapinformation".equals(params.get("mapinformation"))) return; try { Document doc = XMLUtil.newDocument(); //get Form from view String formId = (String) params.get("formId"); UIComponent form = facesContext.getViewRoot().findComponent(formId); if (form == null) XMLUtil.createElement(doc, "error", "Unable to find form (" + formId + ") in FacesContext", null); else { //get MapControl String mapId = (String) params.get("mapId"); MapControl mapControl = (MapControl) form.findComponent(mapId); if (mapControl == null) { XMLUtil.createElement(doc, "error", "Unable to find MapControl (" + mapId + ") in form (" + formId + ").", null); } else { WebMap map = mapControl.getWebMap(); WebExtent extent = map.getCurrentExtent(); //populate response document Element responseElement = XMLUtil.createElement(doc, "response", null, null); XMLUtil.createElement("minx", Double.toString(extent.getMinX()), responseElement); XMLUtil.createElement("miny", Double.toString(extent.getMinY()), responseElement); XMLUtil.createElement("maxx", Double.toString(extent.getMaxX()), responseElement); XMLUtil.createElement("maxy", Double.toString(extent.getMaxY()), responseElement); XMLUtil.createElement("map-scale", Double.toString(map.getMapScale()), responseElement); } } AJAXUtil.writeResponse(facesContext, doc); facesContext.responseComplete(); } catch (Exception e) { logger.log(Level.SEVERE, "Error in rendering AJAX response.", e); } } }