arcgissamples\cartography\CopySymbolMain.java—ArcObjects 10.4 Help for Java | ArcGIS for Desktop
Copy and paste a symbol
arcgissamples\cartography\CopySymbolMain.java
/* Copyright 2015 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.4/userestrictions.txt.
* 
*/
package arcgissamples.cartography;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Label;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.UIManager;

import com.esri.arcgis.beans.map.MapBean;
import com.esri.arcgis.carto.IElement;
import com.esri.arcgis.carto.IGraphicsContainer;
import com.esri.arcgis.carto.IMarkerElement;
import com.esri.arcgis.carto.MarkerElement;
import com.esri.arcgis.carto.RectangleElement;
import com.esri.arcgis.controls.IMapControlEvents2Adapter;
import com.esri.arcgis.controls.IMapControlEvents2OnKeyDownEvent;
import com.esri.arcgis.controls.IMapControlEvents2OnMouseDownEvent;
import com.esri.arcgis.display.IDisplayTransformation;
import com.esri.arcgis.display.IMarkerSymbol;
import com.esri.arcgis.display.ISimpleMarkerSymbol;
import com.esri.arcgis.display.RgbColor;
import com.esri.arcgis.display.SimpleFillSymbol;
import com.esri.arcgis.display.SimpleMarkerSymbol;
import com.esri.arcgis.display.esriSimpleFillStyle;
import com.esri.arcgis.display.esriSimpleMarkerStyle;
import com.esri.arcgis.geometry.Envelope;
import com.esri.arcgis.geometry.IPoint;
import com.esri.arcgis.geometry.Point;
import com.esri.arcgis.interop.AutomationException;
import com.esri.arcgis.system.AoInitialize;
import com.esri.arcgis.system.EngineInitializer;
import com.esri.arcgis.system.esriLicenseProductCode;
import com.esri.arcgis.system.esriLicenseStatus;

/**
 * Using this sample, you can copy a symbol from one element to another.
 * This sample works with MarkerElements.
 */
public class CopySymbolMain extends JFrame {

  private static final long serialVersionUID = 1L;
  static AoInitialize aoInit;
  MapBean mapBean = new MapBean();
  IElement holowElement = null;
  IElement selectedElement = null;
  IElement copyElement = null;

  /**
   * Constructor
   */
  public CopySymbolMain() {
    initUI();
  }

  /**
   * Method to start the program execution.
   */
  public static void main(String s[]) {
    try {
      EngineInitializer.initializeVisualBeans();
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
      initializeArcGISLicenses();
      CopySymbolMain application = new CopySymbolMain();
      application.buildElements();
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }
  
  static void initializeArcGISLicenses() {
    try {
      aoInit = new AoInitialize();
      if (aoInit .isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeEngine) 
          == esriLicenseStatus.esriLicenseAvailable)
        aoInit .initialize(esriLicenseProductCode.esriLicenseProductCodeEngine);
      else if (aoInit.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeBasic) 
          == esriLicenseStatus.esriLicenseAvailable)
        aoInit.initialize(esriLicenseProductCode.esriLicenseProductCodeBasic);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  /*
   * Lays out the User Interface, and sets up event listeners
   */
  private void initUI() {
    this.setTitle("Copy Symbol Sample Application");
    this.setSize(new Dimension(600, 500));
    this.getContentPane().setLayout(new BorderLayout());

    Label label = new Label();
    label.setText("Select element and make copy/paste by CTRL+C and CTRL+V");

    this.getContentPane().add(this.mapBean, BorderLayout.CENTER);
    this.getContentPane().add(label, BorderLayout.SOUTH);
    this.setVisible(true);
    // set mouse and keyboard events listener
    try {
      this.mapBean.addIMapControlEvents2Listener(new IMapControlEvents2Adapter() {
        private static final long serialVersionUID = 1L;
        public void onMouseDown(IMapControlEvents2OnMouseDownEvent event) throws IOException, AutomationException {
          supportMouseDown(event);
        }
        public void onKeyDown(IMapControlEvents2OnKeyDownEvent event) throws IOException, AutomationException {
          supportKeyDown(event);
        }
      });
    } catch(IOException e) {
      e.printStackTrace(); // should never happened
    }
    // set windows listener
    this.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        try {
          aoInit.shutdown();
        } catch(IOException ex) {
          // exit anyway
        }
        System.exit(0);
      }
    });
  }

  /**
   * The method builds three symbols.
   */
  private void buildElements() {
    try {
      IGraphicsContainer graphicsContainer = this.mapBean.getActiveView().getGraphicsContainer();
      // add three demo symbols
      Point point1 = new Point();
      point1.setX(0);
      point1.setY(100);
      SimpleMarkerSymbol simpleMarkerSymbol1 = new SimpleMarkerSymbol();
      simpleMarkerSymbol1.setSize(8);
      simpleMarkerSymbol1.setStyle(esriSimpleMarkerStyle.esriSMSCircle);
      RgbColor color1 = new RgbColor();
      color1.setRGB(0x00FF00);
      simpleMarkerSymbol1.setColor(color1);
      MarkerElement markerElement1 = new MarkerElement();
      markerElement1.setSymbol(simpleMarkerSymbol1);
      markerElement1.setGeometry(point1);
      graphicsContainer.addElement(markerElement1, 0);
      //
      Point point2 = new Point();
      point2.setX(0);
      point2.setY(300);
      SimpleMarkerSymbol simpleMarkerSymbol2 = new SimpleMarkerSymbol();
      simpleMarkerSymbol2.setSize(16);
      simpleMarkerSymbol2.setStyle(esriSimpleMarkerStyle.esriSMSDiamond);
      RgbColor color2 = new RgbColor();
      color2.setRGB(0x0000FF);
      simpleMarkerSymbol2.setColor(color2);
      MarkerElement markerElement2 = new MarkerElement();
      markerElement2.setSymbol(simpleMarkerSymbol2);
      markerElement2.setGeometry(point2);
      graphicsContainer.addElement(markerElement2, 0);
      //
      Point point3 = new Point();
      point3.setX(0);
      point3.setY(500);
      SimpleMarkerSymbol simpleMarkerSymbol3 = new SimpleMarkerSymbol();
      simpleMarkerSymbol3.setSize(24);
      simpleMarkerSymbol3.setStyle(esriSimpleMarkerStyle.esriSMSCross);
      RgbColor color3 = new RgbColor();
      color3.setRGB(0xFF0000);
      simpleMarkerSymbol3.setColor(color3);
      MarkerElement markerElement3 = new MarkerElement();
      markerElement3.setSymbol(simpleMarkerSymbol3);
      markerElement3.setGeometry(point3);
      graphicsContainer.addElement(markerElement3, 0);
      // create rectangle symbol for selection
      SimpleFillSymbol simpleFillSymbolHollow = new SimpleFillSymbol();
      simpleFillSymbolHollow.setStyle(esriSimpleFillStyle.esriSFSHollow);
      RectangleElement rectangleElementHollow = new RectangleElement();
      rectangleElementHollow.setSymbol(simpleFillSymbolHollow);
      this.holowElement = rectangleElementHollow;
      //
      this.mapBean.getActiveView().refresh();
    } catch (Exception ex) {
      ex.printStackTrace(); // never happened
    }
  }

  /**
   * The method support mouse operation and draw "rectangle" object.
   */
  protected void supportMouseDown(IMapControlEvents2OnMouseDownEvent event) throws IOException {
    IDisplayTransformation displayTransformation = this.mapBean.getActiveView().getScreenDisplay().getDisplayTransformation();
    IGraphicsContainer graphicsContainer = this.mapBean.getActiveView().getGraphicsContainer();
    graphicsContainer.reset();
    IElement element = graphicsContainer.next();
    // remove rectangle element if exist
    while (element != null) {
      if (element instanceof RectangleElement) {
        graphicsContainer.deleteElement(element);
        break;
      }
      element = graphicsContainer.next();
    }
    // add rectangle element if need
    graphicsContainer.reset();
    element = graphicsContainer.next();
    this.selectedElement = null;
    while (element != null) {
      IPoint point =(IPoint)(element.getGeometry());
      IMarkerElement markerElement = (MarkerElement)(element);
      int hSize = (int) (markerElement.getSymbol().getSize());
      int[] xx = new int[1];
      int[] yy = new int[1];
      displayTransformation.fromMapPoint(point, xx, yy);
      double xc = event.getX();
      double yc = event.getY();
      int x = xx[0];
      int y = yy[0];
      int xmin = x - hSize;
      int ymin = y - hSize;
      int xmax = x + hSize;
      int ymax = y + hSize;
      if ((xc >= xmin && xc <= xmax) && (yc >= ymin && yc <= ymax)) { // compare in screen coords
        Envelope envelope = new Envelope();
        envelope.setLowerLeft(displayTransformation.toMapPoint(xmin, ymax));
        envelope.setLowerRight(displayTransformation.toMapPoint(xmax, ymax));
        envelope.setUpperLeft(displayTransformation.toMapPoint(xmin, ymin));
        envelope.setUpperRight(displayTransformation.toMapPoint(xmax, ymin));
        this.holowElement.setGeometry(envelope);
        graphicsContainer.addElement(this.holowElement, 0);
        this.selectedElement = element;
        break;
      }
      element = graphicsContainer.next();
    }
    this.mapBean.getActiveView().refresh();
  }

  /**
   * The method supports CTRL+C and CTRL+V operations
   */
  protected void supportKeyDown(IMapControlEvents2OnKeyDownEvent event) throws IOException {
    if (event.getKeyCode() == 67 && event.getShift() == 2) {
      this.copyElement = this.selectedElement; // copy ref to the symbol
    }
    if (event.getKeyCode() == 86 && event.getShift() == 2 && this.copyElement != null) {
      MarkerElement markerElement = (MarkerElement) this.copyElement;
      IMarkerSymbol markerSymbol = markerElement.getSymbol();
      ISimpleMarkerSymbol simpleMarkerSymbol = (ISimpleMarkerSymbol)(markerSymbol);
      // clone and paste the symbol
      Point point = new Point();
      point.setX(Math.random() * 800);
      point.setY(Math.random() * 800);
      SimpleMarkerSymbol newSimpleMarkerSymbol = new SimpleMarkerSymbol();
      newSimpleMarkerSymbol.setSize(simpleMarkerSymbol.getSize());
      newSimpleMarkerSymbol.setStyle(simpleMarkerSymbol.getStyle());
      newSimpleMarkerSymbol.setColor(simpleMarkerSymbol.getColor());
      MarkerElement newMarkerElement = new MarkerElement();
      newMarkerElement.setSymbol(newSimpleMarkerSymbol);
      newMarkerElement.setGeometry(point);
      this.mapBean.getActiveView().getGraphicsContainer().addElement(newMarkerElement, 0);
      this.mapBean.getActiveView().refresh();
    }
  }

}