arcgissamples\symbologybean\ui\SymbologyFrame.java—ArcObjects 10.4 Help for Java | ArcGIS for Desktop
Add map surrounds
arcgissamples\symbologybean\ui\SymbologyFrame.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.ui;

/*
 * ArcGIS Engine Developer Sample Application Name: SymbologyFrame
 */

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import com.esri.arcgis.carto.IActiveView;
import com.esri.arcgis.carto.IElement;
import com.esri.arcgis.carto.IMapFrame;
import com.esri.arcgis.carto.IMapSurround;
import com.esri.arcgis.carto.IMapSurroundFrame;
import com.esri.arcgis.carto.MapSurroundFrame;
import com.esri.arcgis.carto.esriViewDrawPhase;
import com.esri.arcgis.controls.ISymbologyControlEventsAdapter;
import com.esri.arcgis.controls.ISymbologyControlEventsOnItemSelectedEvent;
import com.esri.arcgis.controls.SymbologyControl;
import com.esri.arcgis.controls.esriSymbologyStyleClass;
import com.esri.arcgis.display.IStyleGalleryItem;
import com.esri.arcgis.display.IStyleGalleryItemProxy;
import com.esri.arcgis.geometry.Envelope;


@SuppressWarnings("serial")
public class SymbologyFrame extends JFrame implements ActionListener
{
  private SymbologyControl symbologyControl;
  private JButton okButton;
  private JButton cancelButton;
  private IActiveView activeView;
  private Envelope envelope;
  private IStyleGalleryItem styleGalleryItem;
  private String strType = "";

  public SymbologyFrame(IActiveView actView, Envelope env, String type)
  {
    activeView = actView;
    envelope = env;
    strType = type;
    initialize();
    loadStyle();
    addListener();
  }

  /*
   * Initialize the symbology Frame and sets size and Title
   */
  private void initialize()
  {
    this.setSize(487, 400);
    this.setContentPane(createContentPane());
    this.setTitle("Select Symbol");
  }

  /*
   * Loads the style file and sets the style class
   */
  private void loadStyle()
  {
    try
    {
      //Get the ArcGIS Engine runtime, if it is available
      String runtimeHome = System.getenv("AGSENGINEJAVA");
      
      //If the ArcGIS Engine runtime is not available, then we can try ArcGIS Desktop runtime
      if(runtimeHome == null){
        runtimeHome = System.getenv("AGSDESKTOPJAVA");
      }
      
      //If no runtime is available, exit application gracefully
      if(runtimeHome == null){
        if(System.getProperty("os.name").toLowerCase().indexOf("win") > -1){
          System.err.println("You must have ArcGIS Engine Runtime or ArcGIS Desktop " + 
              "installed in order to execute this sample.");
          System.err.println("Install one of the products above, then re-run this sample.");
            System.err.println("Exiting execution of this sample...");
            System.exit(0);
        }else{
          System.err.println("You must have ArcGIS Engine Runtime installed " + 
              "in order to execute this sample.");
          System.err.println("Install the product above, then re-run this sample.");
          System.err.println("Exiting execution of this sample...");
          System.exit(0);  
        }
      }
      
      //Obtain relative path to the ESRI.ServerStyle file
      String esriStylePath = runtimeHome + "styles" + File.separator + "ESRI.ServerStyle";
      
      File styleFile = new File(esriStylePath);

          //Test to make sure style file is presente
      if(!styleFile.exists()){
            System.err.println("The ESRI.ServerStyle was not found in the following location: " +
                                                  styleFile.getParent());
              System.err.println("Verify that ESRI.ServerStyle can be located in the specified folder.");
              System.err.println("If not present, try uninstalling your ArcGIS software and reinstalling it.");
              System.err.println("Exiting execution of this sample...");
              System.exit(0);
          }
    
      //Load the ESRI.ServerStyle file into the SymbologyControl
      symbologyControl.loadStyleFile(esriStylePath);
      
      //Set the style class
      if (strType.equalsIgnoreCase("northarrow")){
        symbologyControl.setStyleClass(esriSymbologyStyleClass.esriStyleClassNorthArrows);
      }else if (strType.equalsIgnoreCase("scalebar")){
        symbologyControl.setStyleClass(esriSymbologyStyleClass.esriStyleClassScaleBars);
      }else if (strType.equalsIgnoreCase("text")){
        symbologyControl.setStyleClass(esriSymbologyStyleClass.esriStyleClassScaleTexts);
      }
    }catch (Exception err){err.printStackTrace();}
  }

  /*
   * Initialize jContentPanel
   */
  private JPanel createContentPane()
  {
    JPanel mainPanel = new JPanel();
    mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
    
    symbologyControl = createSymbologyControl(); 
    mainPanel.add(symbologyControl);

    JPanel buttonPanel = new JPanel();
    buttonPanel.setLayout(new FlowLayout());
    
    okButton = createOKButton();    
    buttonPanel.add(okButton, null);
    
    cancelButton = createCancelButton();
    buttonPanel.add(cancelButton, null);

    mainPanel.add(buttonPanel);

    return mainPanel;
  }

  /*
   * create symbologyControl
   */
  private SymbologyControl createSymbologyControl()
  {
    SymbologyControl symbologyControl = new SymbologyControl();
    symbologyControl.setBounds(new java.awt.Rectangle(4, 3, 302, 265));

    return symbologyControl;
  }

  /*
   * create ok button
   */
  private JButton createOKButton()
  {
    JButton okButton = new JButton();
    okButton.setText("OK");
    okButton.setEnabled(false);
    okButton.addActionListener(this);
    
    return okButton;
  }

  /*
   * create cancel button
   */
  private JButton createCancelButton()
  {
    JButton cancelButton = new JButton();
    cancelButton.setText("Cancel");
    cancelButton.addActionListener(this);

    return cancelButton;
  }

  /*
   * This method is called when button OK/Cancel is clicked
   */
  public void actionPerformed(ActionEvent e)
  {
    Runnable r = null;
    if (e.getSource() == okButton)
    {
      r = new Runnable()
      {
        public void run()
        {
          try
          {
            if (styleGalleryItem == null)
              return;
            // Get the map frame of the focus map
            IMapFrame mapFrame = (IMapFrame) activeView.getGraphicsContainer().findFrame(
                activeView.getFocusMap());
            // Create a map surround frame
            IMapSurroundFrame mapSurroundFrame = new MapSurroundFrame();
            // Set its map frame and map surround
            mapSurroundFrame.setMapFrameByRef(mapFrame);
            mapSurroundFrame.setMapSurroundByRef((IMapSurround) styleGalleryItem.getItem());
            // QI to IElement and set its geometry
            IElement element = (IElement) mapSurroundFrame;
            element.setGeometry(envelope);
            // Add the element to the graphics container
            activeView.getGraphicsContainer().addElement((IElement) mapSurroundFrame, 0);
            // Refresh
            activeView.partialRefresh(esriViewDrawPhase.esriViewGraphics, mapSurroundFrame, null);
            dispose();
          }
          catch (Exception err)
          {
            err.printStackTrace();
          }
        }
      };
    }
    if (e.getSource() == cancelButton)
    {
      styleGalleryItem = null;
      this.dispose();
    }
    Thread t = new Thread(r);
    t.start();
  }

  /*
   * Adds necessary listener to symbologycontrol to handle the ItemSelecetd event
   */
  @SuppressWarnings("serial")
  public void addListener()
  {
    try
    {
      symbologyControl.addISymbologyControlEventsListener(new ISymbologyControlEventsAdapter()
      {
        @SuppressWarnings("deprecation")
        public void onItemSelected(ISymbologyControlEventsOnItemSelectedEvent theEvent)
        {
          SwingUtilities.invokeLater(new Runnable()
          {
            public void run()
            {
              try
              {
                okButton.setEnabled(true);
              }
              catch (Exception e)
              {
                e.printStackTrace();
              }
            }
          });
          try
          {
            styleGalleryItem = new IStyleGalleryItemProxy(theEvent.getStyleGalleryItem());
          }
          catch (Exception e)
          {
            e.printStackTrace();
          }
        }
      });
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }
}