com\esri\arcgis\sample\mapzoom\MapZoomPhaseListener.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.mapzoom; import com.esri.adf.web.data.WebContext; 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.AJAXResponseRenderer; import com.esri.adf.web.faces.renderkit.xml.ajax.AJAXUtil; import java.util.Map; import java.util.Vector; 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; public class MapZoomPhaseListener implements PhaseListener { private static Logger logger = Logger.getLogger(com.esri.arcgis.sample.mapzoom.MapZoomPhaseListener.class.getName()); //parameter names expected in request static final String MAP_ZOOM = "mapZoom"; static final String FORM_ID = "formId"; static final String MAP_ID = "mapId"; static final String FACTOR = "factor"; //implementation: This PhaseListener must be executed in the JSF's Restore View phase public PhaseId getPhaseId() { return PhaseId.APPLY_REQUEST_VALUES; } //implementation: No processing public void beforePhase(PhaseEvent phaseEvent) { //do nothing } //implementation: Process functionality based on request parameters public void afterPhase(PhaseEvent phaseEvent) { FacesContext facesContext = phaseEvent.getFacesContext(); ExternalContext externalContext = facesContext.getExternalContext(); Map paramMap = externalContext.getRequestParameterMap(); //verify if request is to be processed by this PhaseListener if (! MAP_ZOOM.equals((String) paramMap.get(MAP_ZOOM))) return; try { //get Form from view UIComponent form = facesContext.getViewRoot().findComponent((String) paramMap.get(FORM_ID)); //get MapControl MapControl mapControl = (MapControl) form.findComponent((String) paramMap.get(MAP_ID)); //get WebMap WebMap webMap = mapControl.getWebMap(); //get WebContext WebContext webContext = webMap.getWebContext(); //set event source as map, since operation is to be performed on the map Vector eventSources = new Vector(); eventSources.add(mapControl.getId()); AJAXResponseRenderer ajaxRenderer = new AJAXResponseRenderer(facesContext, eventSources, form); //get, expand and set map's extent WebExtent webExtent = webMap.getCurrentExtent(); webExtent.expand(Double.parseDouble((String) paramMap.get(FACTOR))); webMap.setCurrentExtent(webExtent); //refresh context to update all controls webContext.refresh(); //render and get AJAX response Document Document doc = ajaxRenderer.renderResponse(facesContext); //initialize response AJAXUtil.writeResponse(facesContext, doc); } catch (Exception e) { logger.log(Level.SEVERE, "Unable to render xml", e); } finally { //notify Faces Lifecycle facesContext.responseComplete(); } } }