arcgissamples\symbologybean\RenderUsingColorRamp.java—ArcObjects 10.4 Help for Java | ArcGIS for Desktop
Render using color ramp
arcgissamples\symbologybean\RenderUsingColorRamp.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.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JSplitPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import arcgissamples.symbologybean.commands.ShowSymbologyCommand;

import com.esri.arcgis.beans.TOC.TOCBean;
import com.esri.arcgis.beans.map.MapBean;
import com.esri.arcgis.beans.toolbar.ToolbarBean;
import com.esri.arcgis.carto.IBasicMap;
import com.esri.arcgis.carto.IGeoFeatureLayer;
import com.esri.arcgis.carto.ILayer;
import com.esri.arcgis.controls.ITOCControlEventsAdapter;
import com.esri.arcgis.controls.ToolbarMenu;
import com.esri.arcgis.geometry.esriGeometryType;
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;
import com.esri.arcgis.systemUI.esriCommandStyles;

public class RenderUsingColorRamp extends JFrame
{
  private static final long serialVersionUID = 1L;

  private AoInitialize ao = null;
  
  private ToolbarBean toolbarBean = null;
  private TOCBean tocBean = null;
  private MapBean mapBean = null;
  JDialog progressDialog = null;

  /**
   * This is the default constructor
   */
  public RenderUsingColorRamp()
  {
    addWindowListener(new WindowAdapter()
    {
      public void windowClosed(WindowEvent e)
      {
        try
        {
          // Release license
          ao.shutdown();

          // Do not make any call to ArcObjects after ShutDownApplication()
          EngineInitializer.releaseAll();

          // Exit app
          System.exit(0);
        }
        catch (AutomationException e1)
        {
          e1.printStackTrace();
        }
        catch (IOException e1)
        {
          e1.printStackTrace();
        }
      }
    });
  }
  
  /**
   * main
   * @param args
   */
  public static void main(String[] args)
  {
    System.out.println("Starting AssignSymbolToGraphicElements - An ArcObjects Java SDK Developer Sample");

    EngineInitializer.initializeVisualBeans();

    final RenderUsingColorRamp app = new RenderUsingColorRamp();
    app.initializeArcGISLicenses();

    SwingUtilities.invokeLater(new Runnable()
    {
      public void run()
      {
        try
        {
          app.buildAppFrame();
          app.setVisible(true);
          Thread documentLoader = new Thread(new Runnable()
          {
            public void run()
            {
              app.getProgressDialog("Loading Map Document").setVisible(true);
              try
              {
                //Get DEVKITHOME Home
                String devKitHome = System.getenv("AGSDEVKITJAVA");
                
                if (devKitHome == null || devKitHome.equalsIgnoreCase(""))
                {
                  JOptionPane.showMessageDialog(app, "ERROR - ArcGIS developer kit install invalid. Exiting application...");
                  System.exit(-1);
                }

                String fileName = devKitHome + "java" + File.separator + "samples" + File.separator
                    + "data" + File.separator + "mxds" + File.separator + "usa.mxd";

                File file = new File(fileName);
                if (!file.exists())
                {
                  JOptionPane.showMessageDialog(app, "ERROR - Unable to find map document "
                      + fileName
                      + ".\nPlease use the Open file command to locate your map document.");
                }
                else
                {
                  app.mapBean.loadMxFile(fileName, null, null);
                }
              }
              catch (java.lang.Throwable e)
              {
                e.printStackTrace();
              }
              app.getProgressDialog("Loading Map Document").setVisible(false);
            }

          }, "Map document loader");
          documentLoader.start();
        }
        catch (Exception e)
        {
          e.printStackTrace();
        }
      }
    });
  }  
  
  /**
   * Builds the application frame with Engine Controls and Swing Components
   */
  private void buildAppFrame() throws Exception
  {
    this.setSize(900, 600);
    this.setTitle("Render Using Color Ramp");
    setContentPane(createAppContentPanel());
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    setVisible(true);
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  }  
  
  /**
   * This method initializes jContentPane
   * 
   * @return javax.swing.JPanel
   */
  private JPanel createAppContentPanel()
  {      
    JPanel contentPanel = new JPanel();
    contentPanel.setLayout(new BorderLayout());
    
    //add toolbar bean to panel
    this.toolbarBean = createToolbarBean();
    contentPanel.add(toolbarBean, java.awt.BorderLayout.NORTH);    
    
    //add a split panel contains toc bean and map bean
    contentPanel.add(createSplitPanel(), java.awt.BorderLayout.CENTER);
    
    //add label
    JLabel label = new JLabel();
    label.setText("Right-click on the States layer to apply a Color Ramp");
    label.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);    
    contentPanel.add(label, java.awt.BorderLayout.SOUTH);
    
    //buddy up the map and toolbar beans
    try
    {
      this.toolbarBean.setBuddyControl(this.mapBean);
    }
    catch (IOException e)
    {
      e.printStackTrace();
    }
    
    return contentPanel;
  }  
  
  /**
   * This method initializes jSplitPane
   * 
   * @return javax.swing.JSplitPane
   */
  private JSplitPane createSplitPanel()
  {
    JSplitPane splitPane = new JSplitPane();
    splitPane.setDividerLocation(150);
    splitPane.setDividerSize(2);
    
    //create and add map bean 
    this.mapBean = createMapBean();
    splitPane.setRightComponent(this.mapBean);

    //create and add TOC bean
    this.tocBean = createTOCBean();
/*    try
    {
      this.tocBean.setBuddyControl(this.toolbarBean);
    }
    catch (AutomationException e)
    {
      e.printStackTrace();
    }
    catch (IOException e)
    {
      e.printStackTrace();
    }
*/    splitPane.setLeftComponent(this.tocBean);

    return splitPane;
  }

  /**
   * This method initializes tocBean
   * 
   * @return com.esri.arcgis.beans.TOC.tocBean
   */
  private TOCBean createTOCBean()
  {
    final TOCBean tocBean = new TOCBean();
    
    try
    {
      tocBean.setBuddyControl(this.mapBean);
      final ToolbarMenu toolbarMenu = new ToolbarMenu();
      toolbarMenu.addItem(new ShowSymbologyCommand(tocBean), 0, -1, false, esriCommandStyles.esriCommandStyleTextOnly);
      toolbarMenu.setHook(this.toolbarBean.getObject());
      tocBean.addITOCControlEventsListener(new ITOCControlEventsAdapter()
      {
        private static final long serialVersionUID = 1L;

        public void onMouseDown(com.esri.arcgis.controls.ITOCControlEventsOnMouseDownEvent e)
        {
          IBasicMap map[] =
          { null };
          ILayer layer[] =
          { null };
          Object other[] =
          { null };
          int itemType[] =
          { 0 };
          Object index[] =
          { null };
          // Determine what kind of item has been clicked on
          try
          {
            tocBean.hitTest(e.getX(), e.getY(), itemType, map, layer, other, index);
            ILayer selLayer = layer[0];

            if (selLayer instanceof IGeoFeatureLayer)
            {
              IGeoFeatureLayer geoLayer = (IGeoFeatureLayer) selLayer;
              if (geoLayer.getFeatureClass().getShapeType() == esriGeometryType.esriGeometryPolygon)
              {
                tocBean.selectItem(selLayer, null);
                mapBean.setCustomProperty(selLayer);
                if (e.getButton() == 2)
                {
                  toolbarMenu.popupMenu(e.getX(), e.getY(), tocBean.getHWnd());
                }
              }
            }
          }
          catch (Exception ex)
          {
            ex.printStackTrace();
          }
        }
      });
    }
    catch (Exception e1)
    {
      e1.printStackTrace();
    }


    return tocBean;
  }

  /**
   * This method initializes mapBean
   * 
   * @return com.esri.arcgis.beans.map.MapBean
   */
  private MapBean createMapBean()
  {
    if (mapBean == null)
    {
      mapBean = new MapBean();
      
      final String devKitHome = System.getenv("AGSDEVKITJAVA");
      
      if (devKitHome == null)
      {
        System.err.println("ArcObjects Java SDK not installed, exiting sample.");
        System.exit(-1);
      }
      
      try
      {
        mapBean.addShapeFile(devKitHome + "java" + File.separator + "samples" + File.separator + "data"
                             + File.separator + "usa", "states");
      }
      catch (IOException e)
      {
        e.printStackTrace();
      }
    }
    return mapBean;
  }

  /**
   * This method creates toolbarBean
   * 
   * @return com.esri.arcgis.beans.toolbar.ToolbarBean
   */
  private ToolbarBean createToolbarBean()
  {
    ToolbarBean toolbarBean = new ToolbarBean();
    toolbarBean.setItemsString("10|controls/ControlsMapFullExtentCommand|0|-1|0|0|1;" +
        "10|controls/ControlsMapZoomInFixedCommand|0|-1|0|0|1;" + 
        "10|controls/ControlsMapZoomOutFixedCommand|0|-1|0|0|1;" + 
        "10|controls/ControlsMapZoomInTool|0|-1|0|0|1;" + 
        "10|controls/ControlsMapZoomOutTool|0|-1|0|0|1;" + 
        "10|controls/ControlsMapPanTool|0|-1|0|0|1;" + 
        "10|controls/ControlsMapZoomToLastExtentBackCommand|0|-1|0|0|1;" + 
        "10|controls/ControlsMapZoomToLastExtentForwardCommand|0|-1|0|0|1");
    
    return toolbarBean;
  }
  
  /**
   * Initializes the lowest available ArcGIS License
   */
  private void initializeArcGISLicenses()
  {
    try
    {
      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);
      }else{
        System.err.println("Could not initialize an Engine or Basic License. Exiting application.");
        System.exit(-1);
      }
    }catch (Exception e){e.printStackTrace();}
  }

  /**
   * Creates a Dialog box with a Progress bar in it. @return
   */
  private JDialog getProgressDialog(String title)
  {
    if (progressDialog == null)
    {
      progressDialog = new JDialog();
      progressDialog.setTitle(title);
      
      progressDialog.setLocationRelativeTo(this);
      progressDialog.setLocation(this.getWidth() / 2, this.getHeight() / 2);
      
      JProgressBar bar = new JProgressBar();
      bar.setBorderPainted(true);
      bar.setIndeterminate(true);
      progressDialog.add(bar, BorderLayout.PAGE_START);
      progressDialog.pack();
      progressDialog.setSize(250, 50);
    }

    return progressDialog;
  }  
}