arcgissamples\symbologybean\ModifyFeatureLayerSymbols.java—ArcObjects 10.4 Help for Java | ArcGIS for Desktop
Modify feature layer symbols
arcgissamples\symbologybean\ModifyFeatureLayerSymbols.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.symbologybean;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import arcgissamples.symbologybean.ui.SymbologyFrame;

import com.esri.arcgis.beans.TOC.TOCBean;
import com.esri.arcgis.beans.pagelayout.PageLayoutBean;
import com.esri.arcgis.beans.toolbar.ToolbarBean;
import com.esri.arcgis.carto.IBasicMap;
import com.esri.arcgis.carto.IFeatureLayer;
import com.esri.arcgis.carto.ILayer;
import com.esri.arcgis.controls.ControlsMapFullExtentCommand;
import com.esri.arcgis.controls.ControlsMapPanTool;
import com.esri.arcgis.controls.ControlsMapZoomInTool;
import com.esri.arcgis.controls.ControlsMapZoomOutTool;
import com.esri.arcgis.controls.ControlsOpenDocCommand;
import com.esri.arcgis.controls.ControlsPageZoomInTool;
import com.esri.arcgis.controls.ControlsPageZoomOutTool;
import com.esri.arcgis.controls.ControlsPageZoomWholePageCommand;
import com.esri.arcgis.controls.ControlsSelectTool;
import com.esri.arcgis.controls.ITOCControlEventsAdapter;
import com.esri.arcgis.controls.ITOCControlEventsOnMouseDownEvent;
import com.esri.arcgis.system.AoInitialize;
import com.esri.arcgis.system.EngineInitializer;
import com.esri.arcgis.system.esriLicenseProductCode;
import com.esri.arcgis.system.esriLicenseStatus;
import com.esri.arcgis.systemUI.esriCommandStyles;

/**
 * This sample demonstrates using the SymbologyControl to change the symbology of the featurelayer present in TOC
 */
public class ModifyFeatureLayerSymbols extends JFrame
{
  private JPanel jMainPanel = null;
  private JPanel jBottomPanel = null;
  private JLabel jLabel1 = null;
  private JLabel jLabel2 = null;
  private PageLayoutBean pageLayoutBean;
  private TOCBean tocBean;
  private ToolbarBean toolbarBean;
  private SymbologyFrame symbolForm = null;

  /*
   * Main program to start the execution.
   */
  public static void main(String s[])
  {
    try
    {
      initVisualbeans();
      initializeArcGISLicenses();
      new ModifyFeatureLayerSymbols();
    }
    catch (Exception e)
    {
      e.printStackTrace();
      System.out.println("Exiting ...");
    }
  }

  /*
   * Initialize engine in visualbeans mode
   */
  public static void initVisualbeans()
  {
    EngineInitializer.initializeVisualBeans();
  }

  /*
   * Check the availability and then initialize proper license
   */
  public static void initializeArcGISLicenses()
  {
    try{
      AoInitialize ao = new AoInitialize();
      if (ao.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeEngine) 
          == esriLicenseStatus.esriLicenseAvailable)
        ao.initialize(esriLicenseProductCode.esriLicenseProductCodeEngine);
      else if (ao.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeBasic) 
          == esriLicenseStatus.esriLicenseAvailable)
        ao.initialize(esriLicenseProductCode.esriLicenseProductCodeBasic);
    }catch (Exception e){
      e.printStackTrace();
    }
  }

  public ModifyFeatureLayerSymbols() throws Exception
  {
    buildFrame();
    initControl();
    addListener();
    setSize(900, 600);
    setTitle("LayerRendering");
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    setVisible(true);
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    shutdownArcObjects();
  }

  /*
   * Build the frame with ArcGIS and Swing Components
   */
  public void buildFrame()
  {
    jMainPanel = new JPanel();
    jMainPanel.setLayout(new BorderLayout());
    jBottomPanel = new JPanel();
    jBottomPanel.setLayout(new GridLayout(2, 1));
    jLabel1 = new JLabel("1) Load a map document into the PageLayoutControl. ");
    jLabel2 = new JLabel("2) Right click on a feature layer to change its symbology");
    jBottomPanel.add(jLabel1);
    jBottomPanel.add(Box.createVerticalStrut(10));
    jBottomPanel.add(jLabel2);
    jBottomPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    jMainPanel.add(getToolbatBean(), BorderLayout.NORTH);
    jMainPanel.add(getTocBean(), BorderLayout.WEST);
    jMainPanel.add(getPagelayoutBean(), BorderLayout.CENTER);
    jMainPanel.add(jBottomPanel, BorderLayout.SOUTH);
    jMainPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    getContentPane().add(jMainPanel, BorderLayout.CENTER);
  }

  /*
   * Initialize toolbarBean
   */
  private ToolbarBean getToolbatBean()
  {
    if (toolbarBean == null)
    {
      toolbarBean = new ToolbarBean();
      toolbarBean.setSize(490, 20);
    }
    return toolbarBean;
  }

  /*
   * Initialize tocBean
   */
  private TOCBean getTocBean()
  {
    if (tocBean == null)
    {
      tocBean = new TOCBean();
      tocBean.setSize(new Dimension(200, 100));
    }
    return tocBean;
  }

  /*
   * Initialize pagelayoutBean
   */
  private PageLayoutBean getPagelayoutBean()
  {
    if (pageLayoutBean == null)
    {
      pageLayoutBean = new PageLayoutBean();
    }
    return pageLayoutBean;
  }

  /*
   * Initialize control and add tools and commands to toolbar
   */
  public void initControl() throws Exception
  {
    try
    {
      // Set the Buddy
      toolbarBean.setBuddyControl(pageLayoutBean);
      tocBean.setBuddyControl(pageLayoutBean);
      // Add tool bar items..
      toolbarBean.addItem(new ControlsOpenDocCommand(), 0, 0, false, 0,
          esriCommandStyles.esriCommandStyleIconOnly); // open
      toolbarBean.addItem(new ControlsPageZoomInTool(), 0, -1, true, 0,
          esriCommandStyles.esriCommandStyleIconOnly); // PageZoomIn
      toolbarBean.addItem(new ControlsPageZoomOutTool(), 0, -1, false, 0,
          esriCommandStyles.esriCommandStyleIconOnly); // PageZoomOut
      toolbarBean.addItem(new ControlsPageZoomWholePageCommand(), 0, -1, false, 0,
          esriCommandStyles.esriCommandStyleIconOnly); // PagePan
      toolbarBean.addItem(new ControlsMapZoomInTool(), 0, -1, false, 0,
          esriCommandStyles.esriCommandStyleIconOnly); // MapZoomIn
      toolbarBean.addItem(new ControlsMapZoomOutTool(), 0, -1, false, 0,
          esriCommandStyles.esriCommandStyleIconOnly); // MapZoomout
      toolbarBean.addItem(new ControlsMapPanTool(), 0, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly); // Map
                                                            // Pan
      toolbarBean.addItem(new ControlsMapFullExtentCommand(), 0, -1, false, 0,
          esriCommandStyles.esriCommandStyleIconOnly); // MapFullExtent
      toolbarBean.addItem(new ControlsSelectTool(), 0, -1, true, 0, esriCommandStyles.esriCommandStyleIconOnly); // Select
    }
    catch (Exception e)
    {
      System.out.println("Couldn't add commands or tools to toolbar bean.");
      e.printStackTrace();
    }
  }

  /*
   * Add necessary listener to TOCcontrol to handle the Mousedown event
   */
  @SuppressWarnings("serial")
  public void addListener()
  {
    try
    {
      tocBean.addITOCControlEventsListener(new ITOCControlEventsAdapter()
      {
        public void onMouseDown(final ITOCControlEventsOnMouseDownEvent e)
        {
          SwingUtilities.invokeLater(new Runnable()
          {
            public void run()
            {
              try
              {
                // Exit if not right mouse button
                if (e.getButton() != 2)
                  return;
                IBasicMap map[] =
                { null };
                ILayer layer[] =
                { null };
                Object other[] =
                { null };
                Object index[] =
                { null };
                int item[] =
                { 0 };
                // Determine what kind of item has been clicked on
                tocBean.hitTest(e.getX(), e.getY(), item, map, layer, other, index);
                if (layer == null)
                  return;
                // Cast the item right clicked to IFeatureLayer
                IFeatureLayer featureLayer = (IFeatureLayer) layer[0];
                if (featureLayer == null)
                  return;
                // create the symbology frame and pass pagelaoutbean and the feature layer as parameters
                symbolForm = new SymbologyFrame(pageLayoutBean, featureLayer);
                symbolForm.setVisible(true);
                symbolForm.setAlwaysOnTop(true);
                symbolForm.setSize(487, 299);
              }
              catch (Exception err)
              {
                err.printStackTrace();
              }
            }
          });
        }
      });
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }

  /*
   * Shutdown ArcObjects when the window closes
   */
  public void shutdownArcObjects() throws Exception
  {
    addWindowListener(new WindowAdapter()
    {
      public void windowClosing(WindowEvent e)
      {
        try
        {
          new AoInitialize().shutdown();
          System.exit(0);
        }
        catch (Exception err)
        {
          err.printStackTrace();
          System.exit(1);
        }
      }
    });
  }

}