Graphics Resource
com\esri\adf\sample\graphics\MyGraphicsResource.java
/* Copyright 2012 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.
* 
*/
/* 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 at <your ArcGIS install location>/DeveloperKit10.0/userestrictions.txt.
* 
*/
package com.esri.adf.sample.graphics;

import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.io.Serializable;

import com.esri.adf.web.data.TocFunctionality;
import com.esri.adf.web.data.WebContext;
import com.esri.adf.web.data.WebContextInitialize;
import com.esri.adf.web.data.WebLayerInfo;
import com.esri.adf.web.data.WebMap;
import com.esri.adf.web.data.geometry.WebExtent;
import com.esri.adf.web.data.geometry.WebGeometry;
import com.esri.adf.web.data.geometry.WebMultiPoint;
import com.esri.adf.web.data.geometry.WebPoint;
import com.esri.adf.web.data.geometry.WebPolygon;
import com.esri.adf.web.data.geometry.WebPolyline;
import com.esri.adf.web.data.geometry.WebSpatialReference;
import com.esri.adf.web.data.graphics.GraphicFeature;
import com.esri.adf.web.data.graphics.GraphicsLayer;
import com.esri.adf.web.data.graphics.GraphicsResource;
import com.esri.adf.web.data.query.IdentifyCriteria;
import com.esri.adf.web.data.query.QueryResult;
import com.esri.adf.web.data.query.WebQuery;
import com.esri.adf.web.data.renderer.WebSimpleRenderer;
import com.esri.adf.web.data.results.ResultDefinition;
import com.esri.adf.web.data.symbol.WebPictureMarkerSymbol;
import com.esri.adf.web.data.symbol.WebSimpleLineSymbol;
import com.esri.adf.web.data.symbol.WebSimplePolygonSymbol;
import com.esri.adf.web.faces.event.MapEvent;
import com.esri.internal.adf.web.data.display.ImportImage;

public class MyGraphicsResource implements WebContextInitialize, Serializable {

  private WebContext mWebContext = null;

  private String graphicsLayerName = "Graphics Layer ";

  private short graphicsLayerCount = 0;

  private Map<String, String> actions = null;

  public void init(WebContext context) {
    this.mWebContext = context;
    GraphicsResource gResource = (GraphicsResource) context.getResourceById("graphicsResource");
    gResource.setDefaultSpatialReference(WebSpatialReference.getWebSpatialReference(4326));
    actions = new LinkedHashMap<String, String>();
    actions.put("Zoom", "zoom");
    actions.put("HighLight", "highlight");
    actions.put("Clear Graphic", "clearGraphic");
  }

  public void destroy() {

  }

  public void identifyCriteria(MapEvent event) {
    WebGeometry geometry = event.getWebGeometry().toMapGeometry(event.getWebContext().getWebMap());
    identifyCriteria(geometry);
  }

  public void addGraphicFeature(MapEvent event) {
    WebContext context = event.getWebContext();
    WebMap wMap = context.getWebMap();
    WebGeometry geom = event.getWebGeometry();
    addGraphicFeature(context, geom.toMapGeometry(wMap), true);
  }

  public void identifyCriteria(WebGeometry geometry) {
    try {
      WebQuery webQuery = this.mWebContext.getWebQuery();

      IdentifyCriteria ic = new IdentifyCriteria(geometry);
      List<WebLayerInfo> newInfo = new ArrayList<WebLayerInfo>(1);
      newInfo.add(WebQuery.allLayersOption);

      List<QueryResult> results = webQuery.query(ic, newInfo);
      System.out.println("Result Count : " + results.size());

      ResultDefinition definition = new ResultDefinition();
      definition.setHeader("My Result(s): " + geometry);
      definition.setDisplayNameMethodName("getName");
      definition.setDetailsMethodName("getDetails");
      definition.setActionMethodNames(actions);
      definition.setRemoveMethodName("clearGraphic");
      this.mWebContext.getWebResults().addQueryResults(results, definition);
    } catch (Exception e) {
      e.printStackTrace();
      if (geometry != null && this.mWebContext.getWebMap() != null) {
        geometry = geometry.toMapGeometry(this.mWebContext.getWebMap());
      }
      if (this.mWebContext.getWebResults() != null) {
        this.mWebContext.getWebResults().addResultsWithActionMap(
            "Identify Result(s) " + (geometry == null ? "" : geometry.toString()), null, null, null, null);
      }
    }
  }

  public void addGraphicFeature(WebContext context, WebGeometry geom, boolean isRefresh) {
    GraphicsResource gResource = (GraphicsResource) context.getResourceById("graphicsResource");
    WebSimpleRenderer renderer = new WebSimpleRenderer();
    String layerName = graphicsLayerName + (++graphicsLayerCount);
    Map<String, String> attributes = new LinkedHashMap<String, String>();
    attributes.put("Name", layerName);
    attributes.put("ID", new Integer(graphicsLayerCount).toString());
    if (geom instanceof WebPoint) {
      GraphicsLayer gLayer = new GraphicsLayer();
      GraphicFeature gFeature = new GraphicFeature();
      WebPoint point = (WebPoint) geom;
      WebPictureMarkerSymbol markerSymbol = new WebPictureMarkerSymbol();
      try {
        markerSymbol.setURL(new java.net.URL("file:///C:/Program Files/ArcGIS/Desktop10.0/Styles/Pictures/z_flowmeter.bmp"));
      } catch (Exception ex1) {
      }
      renderer.setSymbol(markerSymbol);

      gFeature.setGeometry(point);
      gFeature.setAttributes(attributes);
      gLayer.setName(layerName);
      gLayer.addGraphicFeature(gFeature);
      gLayer.setRenderer(renderer);
      gResource.addGraphicsLayer(gLayer);
    } else if (geom instanceof WebExtent) {
      GraphicsLayer gLayer = new GraphicsLayer();
      GraphicFeature gFeature = new GraphicFeature();
      WebExtent extent = (WebExtent) geom;
      WebMultiPoint points = new WebMultiPoint();
      points.addPoint(new WebPoint(extent.getMinX(), extent.getMaxY()));
      points.addPoint(new WebPoint(extent.getMaxX(), extent.getMaxY()));
      points.addPoint(new WebPoint(extent.getMinX(), extent.getMinY()));
      points.addPoint(new WebPoint(extent.getMaxX(), extent.getMinY()));
      WebPictureMarkerSymbol markerSymbol = new WebPictureMarkerSymbol();
      try {
        markerSymbol.setURL(new java.net.URL("file:///C:/Program Files/ArcGIS/Desktop10.0/Styles/Pictures/z_fitting.bmp"));
      } catch (Exception ex1) {
      }
      renderer.setSymbol(markerSymbol);

      gFeature.setGeometry(points);
      gFeature.setAttributes(attributes);
      gLayer.setName(layerName);
      gLayer.addGraphicFeature(gFeature);
      gLayer.setRenderer(renderer);
      gResource.addGraphicsLayer(gLayer);
    } else if (geom instanceof WebPolyline) {
      GraphicsLayer gLayer = new GraphicsLayer();
      GraphicFeature gFeature = new GraphicFeature();

      WebPolyline polyline = (WebPolyline) geom;
      WebSimpleLineSymbol lineSymbol = new WebSimpleLineSymbol();
      renderer.setSymbol(lineSymbol);

      gFeature.setGeometry(polyline);
      gFeature.setAttributes(attributes);
      gLayer.setName(layerName);
      gLayer.addGraphicFeature(gFeature);
      gLayer.setRenderer(renderer);
      gResource.addGraphicsLayer(gLayer);

    } else if (geom instanceof WebPolygon) {
      GraphicsLayer gLayer = new GraphicsLayer();
      GraphicFeature gFeature = new GraphicFeature();

      WebPolygon polygon = (WebPolygon) geom;
      WebSimplePolygonSymbol polygonSymbol = new WebSimplePolygonSymbol();
      // polygonSymbol.setFillTransparency(0.5);
      BufferedImage bImage = null;
      try {
        bImage = ImportImage.getImage(new java.io.FileInputStream(new java.io.File(
            "C:\\Program Files\\ArcGIS\\Desktop10.0\\Styles\\Pictures\\hacshd29.bmp")));
        polygonSymbol.setFillTexture(bImage);
        polygonSymbol.setFillType(WebSimplePolygonSymbol.TEXTURE);
      } catch (Exception ex) {
      }
      renderer.setSymbol(polygonSymbol);
      gFeature.setGeometry(polygon);
      gFeature.setAttributes(attributes);
      gLayer.setName(layerName);
      gLayer.addGraphicFeature(gFeature);
      gLayer.setRenderer(renderer);
      gResource.addGraphicsLayer(gLayer);
    }

    context.getWebToc().init((TocFunctionality) gResource.getFunctionality(TocFunctionality.FUNCTIONALITY_NAME));
    context.getWebOverview().exportImage();
    context.getWebQuery().init(context);
    if (isRefresh) {
      context.refresh();
    }
  }

  public void addGraphicFeature(WebGeometry geom) {
    this.addGraphicFeature(this.mWebContext, geom, true);
  }
}