AJAX Postback
com\esri\arcgis\sample\mapinformation2\MapInformation2Renderer.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.mapinformation2;

import com.esri.adf.web.data.WebMap;
import com.esri.adf.web.data.geometry.WebExtent;
import com.esri.adf.web.faces.component.MapControl;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.w3c.dom.Element;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import com.esri.adf.web.faces.renderkit.xml.ajax.AJAXRenderer;
import com.esri.adf.web.util.XMLUtil;

/**
 * AJAXRenderer to render current extent & scale of map
 */
public class MapInformation2Renderer implements AJAXRenderer {
  private static Logger logger = Logger.getLogger(MapInformation2Renderer.class.getName());
  
  public Class getControlClass() {
    //returns MapControl
    return MapControl.class;
  }
  
  public Object getOriginalState(UIComponent component) {
    //return map's current extent
    MapControl mapControl = (MapControl) component;
    WebMap webMap = mapControl.getWebMap();
    return webMap.getCurrentExtent();
  }
  
  public void renderAjaxResponse(FacesContext facesContext, UIComponent component, Object state, boolean isEventSource, Element parentElement) {
    //get current extent
    MapControl mapControl = (MapControl) component;
    WebMap webMap = mapControl.getWebMap();
    WebExtent currentExtent = webMap.getCurrentExtent();

    //if original state (WebExtent) is same as current extent, do not render xml
    if (currentExtent.equals((WebExtent) state))
      return;

    try {
      //render map-information-2 tag
      Element rootElement = XMLUtil.createElement("map-information-2", null, parentElement);

      //render information about map's extent
      XMLUtil.createElement("minx", String.valueOf(currentExtent.getMinX()), rootElement);
      XMLUtil.createElement("miny", String.valueOf(currentExtent.getMinY()), rootElement);
      XMLUtil.createElement("maxx", String.valueOf(currentExtent.getMaxX()), rootElement);
      XMLUtil.createElement("maxy", String.valueOf(currentExtent.getMaxY()), rootElement);
      XMLUtil.createElement("map-scale", String.valueOf(webMap.getMapScale()), rootElement);
    }
    catch (Exception e) {
      if (logger.isLoggable(Level.WARNING))
        logger.log(Level.WARNING, "Unable to encode xml.", e);
    }
  }
}