Custom Resource
com\esri\adf\sample\resource\MyCustomMapFunctionality.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.adf.sample.resource;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.esri.adf.sample.resource.util.MyCustomUtil;
import com.esri.adf.web.ags.ADFAGSException;
import com.esri.adf.web.data.GISResource;
import com.esri.adf.web.data.MapFunctionality;
import com.esri.adf.web.data.WebContext;
import com.esri.adf.web.data.WebElementConverter;
import com.esri.adf.web.data.WebMap;
import com.esri.adf.web.data.export.ExportFunctionality;
import com.esri.adf.web.data.export.ExportProperties;
import com.esri.adf.web.data.geometry.WebExtent;
import com.esri.adf.web.data.geometry.WebSpatialReference;
import com.esri.adf.web.util.ImageUtil;
import com.esri.adf.web.util.WebUtil;

public class MyCustomMapFunctionality implements MapFunctionality, ExportFunctionality, Serializable {
  private static final long serialVersionUID = 1L;

  private static Logger logger = Logger.getLogger(MyCustomMapFunctionality.class.getName());

  private MyCustomResource resource = null;

  private double mapScale = 0D;

  private WebExtent currentExtent = null;

  private WebExtent fullExtent = null;

  private WebExtent initialExtent = null;

  private boolean disabled = false;

  private double transparency = 1D;

  public void initFunctionality(GISResource resource) {
    logger.log(Level.FINE, "MyCustomMapFunctionality.initFunctionality(" + resource + ")");
    if (resource == null) {
      throw new ADFMyCustomException("Resource is null");
    }
    if (!(resource instanceof MyCustomResource)) {
      throw new ADFMyCustomException("Resource is not a instance of MyCustomResource");
    }
    this.resource = (MyCustomResource) resource;
  }

  public MyCustomResource getResource() {
    return this.resource;
  }

  public void destroyFunctionality() {
  }

  public InputStream exportImage() {
    WebContext mwContext = this.resource.getWebContext();
    WebMap mwMap = mwContext.getWebMap();
    int width = mwMap.getWidth();
    int height = mwMap.getHeight();

    WebSpatialReference mapsr = this.resource.getDefaultSpatialReference();

    if (this.initialExtent == null) {
      this.initialExtent = new WebExtent(mwMap.getInitExtent());
    }

    if (this.fullExtent == null) {
      this.fullExtent = new WebExtent(mwMap.getFullExtent());
    }

    if (mapsr != null && !(mwContext.getSpatialReference().equals(mapsr))) {
      logger.fine("Projecting the current extent");
      this.currentExtent = mwContext.projectExtent(this.currentExtent);
      logger.fine("Projected Extent : " + this.currentExtent);
    }

    logger.fine("Reaspecting the current extent");
    this.currentExtent = WebUtil.reaspect(this.currentExtent, width, height);
    logger.fine("Reaspected Extent : " + this.currentExtent);

    if (mapsr == null && mwContext.getSpatialReference() != null) {
      logger.fine("Setting Spatial Reference of this resource to Context Spatial Reference (" + this.resource + ")");
      this.resource.setDefaultSpatialReference(mwContext.getSpatialReference());
    }

    try {
      WebElementConverter converter = new WebElementConverter(width, height);
      converter.draw(MyCustomUtil.drawGrid(width, height, 200));
      MyCustomUtil.drawExtent(converter, width, height, mwMap.getCurrentExtent());

      if (mwMap.isLastEnabledMapFunctionality(this)) {
        InputStream opaqueImage = new ByteArrayInputStream(ImageUtil.createBlankImage(width, height, 1D, "PNG"));
        InputStream transparentImage = new ByteArrayInputStream(converter.getPNGImage());
        return new ByteArrayInputStream(ImageUtil.mergeImages(new InputStream[] { transparentImage, opaqueImage },
            "PNG"));
      }
      return new ByteArrayInputStream(converter.getPNGImage());
    } catch (Exception e) {
      throw new ADFMyCustomException("Unable to export map image.", e);
    }
  }

  public InputStream export(ExportProperties exportProperties) {
    if (exportProperties == null) {
      return null;
    }
    if (exportProperties.getExtent() == null) {
      return null;
    }
    try {
      WebContext mwContext = this.resource.getWebContext();
      WebMap mwMap = mwContext.getWebMap();
      int width = mwMap.getWidth();
      int height = mwMap.getHeight();

      WebElementConverter converter = new WebElementConverter(width, height);
      converter.draw(MyCustomUtil.drawGrid(width, height, 200));
      MyCustomUtil.drawExtent(converter, width, height, exportProperties.getExtent());

      if (mwMap.isLastEnabledMapFunctionality(this)) {
        InputStream opaqueImage = new ByteArrayInputStream(ImageUtil.createBlankImage(width, height, 1D, "PNG"));
        InputStream transparentImage = new ByteArrayInputStream(converter.getPNGImage());
        return new ByteArrayInputStream(ImageUtil.mergeImages(new InputStream[] { transparentImage, opaqueImage },
            "PNG"));
      }
      return new ByteArrayInputStream(converter.getPNGImage());

    } catch (Exception e) {
      throw new ADFAGSException("Unable to export map image.", e);
    }
  }

  public double getMapScale() {
    return this.mapScale;
  }

  public WebExtent getCurrentExtent() {
    return this.currentExtent;
  }

  public void setCurrentExtent(WebExtent newMapExtent) {
    this.currentExtent = newMapExtent;
  }

  public WebExtent getFullExtent() {
    return this.fullExtent;
  }

  public void setFullExtent(WebExtent fullExtent) {
    this.fullExtent = fullExtent;
  }

  public WebExtent getInitialExtent() {
    return this.initialExtent;
  }

  public void setInitialExtent(WebExtent initialExtent) {
    this.initialExtent = initialExtent;
  }

  public boolean isDisabled() {
    return this.disabled;
  }

  public void setDisabled(boolean disabled) {
    this.disabled = disabled;
  }

  public double getTransparency() {
    return this.transparency;
  }

  public void setTransparency(double transparency) {
    this.transparency = transparency;
  }
}