arcgissamples\tocbean\EditTOCLabels.java—ArcObjects 10.4 Help for Java | ArcGIS for Desktop
Edit TOC labels
arcgissamples\tocbean\EditTOCLabels.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.tocbean;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FileDialog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.File;
import java.io.IOException;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.event.ChangeListener;

import com.esri.arcgis.beans.TOC.TOCBean;
import com.esri.arcgis.beans.map.MapBean;
import com.esri.arcgis.beans.pagelayout.PageLayoutBean;
import com.esri.arcgis.carto.IBasicMap;
import com.esri.arcgis.carto.ILayer;
import com.esri.arcgis.controls.ITOCControlEventsAdapter;
import com.esri.arcgis.controls.ITOCControlEventsOnBeginLabelEditEvent;
import com.esri.arcgis.controls.ITOCControlEventsOnEndLabelEditEvent;
import com.esri.arcgis.controls.esriTOCControlEdit;
import com.esri.arcgis.controls.esriTOCControlItem;
import com.esri.arcgis.system.AoInitialize;
import com.esri.arcgis.system.EngineInitializer;
import com.esri.arcgis.system.esriLicenseProductCode;
import com.esri.arcgis.system.esriLicenseStatus;

/**
 * Description:This sample demonstrates editing the TOCBean's labels.
 *
 * The Tab control's click event determines whether the TOCBean's Buddy
 * property is set to either the MapBean or PageLayoutBean. The ActiveView
 * of the Buddy is used by the TOCBean to populate itself.
 *
 * The LabelEdit property determines whether labels can be editied unchecked
 * (automatic) or whether the OnBeginLabelEdit and OnEndLabelEdit events are
 * triggered (manual). If label editing is set to manual the getSelectedItem method is
 * used within the OnBeginLabelEdit event to determine the type of label being
 * editied. In this sample, only layer labels can be edited. The OnEndLabelEdit
 * event ensures there are no empty layer labels.
 */


public class EditTOCLabels extends JFrame implements ActionListener, ChangeListener, WindowListener {
  JPanel leftPanel = null;
  JPanel mainPanel = null;
  JPanel rightPanel = null;
  String helpString = "Click on a label, then click on \n" +
    "it a second time to invoke \n " +
    "label editing. The ESC key on \n " +
    "the keyboard can be used \n " +
    "during the edit to cancel the label edit. \n \n" +
    "In this sample, only layer labels \n" +
    "can be editied and no \n" +
    "empty labels are permitted \n" +
    "when label editing is set to manual.";
  JTextArea helpArea = null;
  JLabel labelEditing = null;
  JRadioButton automaticCheck = null;
  JRadioButton manualCheck = null;
  ButtonGroup buttonGroup = null;
  JTabbedPane tabbedPane = null;

  JButton loadDocumentButton = null;
  PageLayoutBean pageLayoutBean;
  MapBean mapBean;
  TOCBean tocBean;

  public EditTOCLabels() {
    super("Label Editing");
    buildFrame();
    setSize(800, 450);
    setVisible(true);
    initControl();
  }

  /**This method builds 'this' frame as per the following diagram:
   *
   *   /---------------------------------------------------------------------------------------------------|
   *   |      BorderLayout.WEST                   BorderLayout.CENTER                    BorderLayout.East |
   *   | |-----JPanel-----------------------------JTabbedPane-------------------------|                    |
   *   | |                             |                                              |   JRadioButton     |
   *   | |    JButton BorderLayout     |                       |                      |   JRadioButton     |
   *   | |            NORTH            |                       |                      |                    |
   *   | ------------------------------|     MapBean           |   PageLayout         |   JCheckBox        |
   *   | |                             |                       |                      |                    |
   *   | |                             |                       |                      |                    |
   *   | |                             |                       |                      |                    |
   *   | |   TocControl BorderLayout   |                       |                      |                    |
   *   | |        Center               |                       |                      |                    |
   *   | |                             |                       |                      |                    |
   *   | |                             |                       |                      |                    |
   *   | |                             |                       |                      |                    |
   *   | |                             |                       |                      |                    |
   *   | |                             |                       |                      |                    |
   *   | |                             |                                              |                    |
   *   ----------------------------------------------------------------------------------------------------|
   */
  public void buildFrame() {
    leftPanel = new JPanel();
    mainPanel = new JPanel();
    rightPanel = new JPanel();
    leftPanel.setLayout(new BorderLayout(5, 5));
    mainPanel.setLayout(new BorderLayout(5, 5));
    rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.Y_AXIS));

    loadDocumentButton = new JButton("Load Document");
    loadDocumentButton.addActionListener(this);
    tocBean = new TOCBean();
    tocBean.setSize(new Dimension(200, 100));
    leftPanel.add(loadDocumentButton, BorderLayout.NORTH);
    leftPanel.add(tocBean, BorderLayout.CENTER);

    tabbedPane = new JTabbedPane();
    JPanel mapPanel = new JPanel(new BorderLayout());
    mapBean = new MapBean();
    mapPanel.add(mapBean, BorderLayout.CENTER);
    JPanel pagePanel = new JPanel(new BorderLayout());
    pageLayoutBean = new PageLayoutBean();
    pagePanel.add(pageLayoutBean, BorderLayout.CENTER);
    tabbedPane.addTab("PageLayout Control", pagePanel);
    //Workaround to set the map control as the selected panel.
    tabbedPane.insertTab("Map Control", null, mapPanel, null, 0);
    tabbedPane.addChangeListener(this);

    labelEditing = new JLabel("Label Editing");
    automaticCheck = new JRadioButton("Automatic");
    automaticCheck.setSelected(true);
    automaticCheck.addActionListener(this);
    manualCheck = new JRadioButton("Manual");
    manualCheck.addActionListener(this);
    buttonGroup = new ButtonGroup();
    buttonGroup.add(automaticCheck);
    buttonGroup.add(manualCheck);
    helpArea = new JTextArea(helpString);
    rightPanel.add(labelEditing);
    rightPanel.add(automaticCheck);
    rightPanel.add(manualCheck);
    rightPanel.add(Box.createVerticalStrut(2));
    rightPanel.add(helpArea);
    rightPanel.add(Box.createVerticalGlue());

    //Create page layout control add it to the center of the main panel.
    mainPanel.add(leftPanel, BorderLayout.WEST);
    mainPanel.add(tabbedPane, BorderLayout.CENTER);
    mainPanel.add(rightPanel, BorderLayout.EAST);
    mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

    getContentPane().add(mainPanel, BorderLayout.CENTER);

  }

  /**Initializes control
   *
   */
  public void initControl() {
    try {
      tocBean.addITOCControlEventsListener(new TocControlListener());
    }
    catch (IOException ex) {
      System.out.println("Exception in initControl : " + ex);
      ex.printStackTrace();
    }
  }

  /**@see java.awt.event.ActionListener#actionPerformed(ActionEvent event)
   * @param event
   */
  public void actionPerformed(ActionEvent event) {
    if(event.getSource() == loadDocumentButton) {
      try {
        loadFile();
      }
      catch (IOException ex) {
        System.out.println(
            "Exception in loadDocumentButton#actionPerformed: " + ex);
        ex.printStackTrace();
      }
    }

    if(event.getSource() == manualCheck) {
      try {

        tocBean.setLabelEdit(esriTOCControlEdit.esriTOCControlManual);
      }
      catch (IOException ex) {
        System.out.println(
            "Exception in manualCheck#actionPerformed: " + ex);
        ex.printStackTrace();
      }
    }

    if(event.getSource() == automaticCheck) {
      try {

        tocBean.setLabelEdit(esriTOCControlEdit.esriTOCControlAutomatic);
      }
      catch (IOException ex) {
        System.out.println(
            "Exception in automaticCheck#actionPerformed: " + ex);
        ex.printStackTrace();
      }
    }
  }

  /**@see javax.swing.event.ChangeListener#stateChanged(javax.swing.event.ChangeEvent event)
   * @param event
   */
  public void stateChanged(javax.swing.event.ChangeEvent event) {
    if(event.getSource() == tabbedPane) {
      try {
        //Toggle control visiblity and set the buddy
        if (tabbedPane.getSelectedIndex() == 0) {
          mapBean.setVisible(true);
          pageLayoutBean.setVisible(false);
          tocBean.setBuddyControl(mapBean);
        }
        else {
          mapBean.setVisible(false);
          pageLayoutBean.setVisible(true);
          tocBean.setBuddyControl(pageLayoutBean);

        }
      }
      catch (IOException ex) {
        System.out.println(
            "Exception in tabbedPane#stateChanged: " + ex);
        ex.printStackTrace();
      }
    }

  }

  /**@see java.awt.event.WindowEvent#windowClosing(javax.swing.event.WindowEvent event)
   * @param event
   */

  public void windowClosing(WindowEvent event) {
    try {
      tocBean.setBuddyControl(null);
    }
    catch (IOException ex) {
      System.out.println(
          "Exception in WindowListener#windowClosing: " + ex);
      ex.printStackTrace();

    }
  }

  public void windowOpened(WindowEvent event) {}
  public void windowClosed(WindowEvent event) {}
  public void windowIconified(WindowEvent event) {}
  public void windowDeiconified(WindowEvent event) {}
  public void windowActivated(WindowEvent event) {}
  public void windowDeactivated(WindowEvent event) {}

  /**
   * Method loadFile loads the specified mxd file.
   *
   */
  public boolean loadFile() throws IOException {
    boolean loaded = false;
    FileDialog fileDialog = new FileDialog(new JFrame(), "Open MXD|MXT|PMF file", FileDialog.LOAD);
    fileDialog.setVisible(true);
    String path = fileDialog.getDirectory();
    String name = fileDialog.getFile();
    String fileChosen = path + File.separator + name;
    if (mapBean.checkMxFile(fileChosen)) {
      //load the document into both the mapcontrol and pagelayout beans.
      System.out.print("Loading " + name + " ... ");
      mapBean.loadMxFile(fileChosen, null, null);
      pageLayoutBean.loadMxFile(fileChosen, null);
      System.out.println("done");
      loaded = true;
    } else {
      JOptionPane.showMessageDialog(null, "The current document cannot be loaded.");
      loaded = false;
    }
    return loaded;
  }


  /**
   * Description: Class which extends toc control event class ITocControlEvents2Adapter
   * @see com.esri.arcgis.beans.TOC.ITOCControlEventsAdapter
   * */

  class TocControlListener
  extends ITOCControlEventsAdapter {

    /**
     * @see com.esri.arcgis.beans.TOC.onBeginLabelEdit(ITOCControlEventsOnBeginLabelEditEvent theEvent)
     * @param theEvent
     */

    public void onBeginLabelEdit(ITOCControlEventsOnBeginLabelEditEvent theEvent) {

      try {
        IBasicMap map [] = {null};
        ILayer layer [] = {null};
        Object other [] = {null};
        int itemType [] = {0};
        Object pIndex [] = {null };
        //Determine what kind of item has been clicked on
        tocBean.getSelectedItem(itemType, map, layer, other, pIndex);


        //Only layer items can have their labels edited
        if(itemType[0]  != esriTOCControlItem.esriTOCControlItemLayer)
          theEvent.setCanEdit(false);

      } catch(Exception ex) {
        System.out.println("Exception in TocControlListener#onBeginLabelEdit : " +
            ex);
        ex.printStackTrace();

      }


    }

    /** @see com.esri.arcgis.beans.TOC.onEndLabelEdit(ITOCControlEventsOnBeginLabelEditEvent theEvent)
     * @param theEvent
     */

    public void onEndLabelEdit(ITOCControlEventsOnEndLabelEditEvent theEvent)   {

      try {
        //Prevent empty labels
        if(theEvent.getNewLabel().equals(""))
          theEvent.setCanEdit(false);

      } catch(Exception ex) {
        System.out.println("Exception in TocControlListener#onEndLabelEdit : " +
            ex);
        ex.printStackTrace();

      }
    }


  } //End of TocControlListener class



  /**
   * Main program to start the program execution.
   *
   * @param s
   */


  public static void main(String s[]) {
    try {
      EngineInitializer.initializeVisualBeans();
      //Set the system look and feel
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

      initializeArcGISLicenses();

      EditTOCLabels labelEdit = new EditTOCLabels();
      labelEdit.setDefaultCloseOperation(EditTOCLabels.EXIT_ON_CLOSE);
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  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);
      else if (ao.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeStandard) 
          == esriLicenseStatus.esriLicenseAvailable)
        ao.initialize(esriLicenseProductCode.esriLicenseProductCodeStandard);
      else if (ao.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeAdvanced) 
          == esriLicenseStatus.esriLicenseAvailable)
        ao.initialize(esriLicenseProductCode.esriLicenseProductCodeAdvanced);
    } catch (Exception e) {e.printStackTrace();}
  }
}