arcgissamples\editing\EventsPane.java—ArcObjects 10.4 Help for Java | ArcGIS for Desktop
Events life cycle
arcgissamples\editing\EventsPane.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.editing;

import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Comparator;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextPane;

import com.esri.arcgis.controls.EngineEditor;
import com.esri.arcgis.controls.IEngineEditEvents;

public class EventsPane extends JTabbedPane implements ActionListener {
  private EngineEditor editor;
  public EventsPane() {
    super();
    initializeUI();
    try{
      editor = new EngineEditor();
      //Register the sample's event listener
      final EditEventsListener listener = new EditEventsListener(this);
      editor.addIEngineEditEventsListener(listener);
      //Add checkboxes for each Editing event
      Method[] events =  IEngineEditEvents.class.getMethods();
      //Sort the array based on event names
      Arrays.sort(events, new Comparator<Method>(){
        public int compare(Method o1, Method o2) {
          return o1.getName().compareTo(o2.getName());
        }
      });
      //For each event, add a checkbox to the UI
      for (final Method event : events) {
        final JCheckBox chkBox = new JCheckBox(event.getName());
        getPanelInput().add(chkBox);
        //Update the event listener whenever the user selects or clears the checkbox
        chkBox.addItemListener(new ItemListener(){
          public void itemStateChanged(ItemEvent e) {
            listener.update(event.getName(),chkBox.isSelected());
          }
        });
      }
    }catch(Exception ex){
      ex.printStackTrace();
    }
  }

  //respond to user actions
  public void actionPerformed(ActionEvent e) {
    if(e.getSource()==btnClearOutput){
      //Clear the output
      getTextOutput().setText("");
    }else if(e.getSource()==btnClearAll){
      //Clear the output
      getTextOutput().setText("");
      //Clear all event checkboxes
      for(int i=0;i<getPanelInput().getComponentCount();i++){
        Component comp = getPanelInput().getComponent(i);
        if(comp instanceof JCheckBox){
          ((JCheckBox)comp).setSelected(false);
        }
      }
    }else if(e.getSource()==btnSelectAll){
      //Select all event checkboxes
      for(int i=0;i<getPanelInput().getComponentCount();i++){
        Component comp = getPanelInput().getComponent(i);
        if(comp instanceof JCheckBox){
          ((JCheckBox)comp).setSelected(true);
        }
      }
    }
  }

  private void initializeUI() {
    this.setSize(300, 200);
    this.addTab("Output", null, getScrollOutput(), null);
    this.addTab("Select Events", null, getScrollInput(), null);
  }

  private JScrollPane getScrollOutput() {
    if (scrollOutput == null) {
      scrollOutput = new JScrollPane();
      scrollOutput.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
      scrollOutput.setViewportView(getPanelOutput());
    }
    return scrollOutput;
  }


  private JScrollPane getScrollInput() {
    if (scrollInput == null) {
      scrollInput = new JScrollPane();
      scrollInput.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
      scrollInput.setViewportView(getPanelInput());
    }
    return scrollInput;
  }

  public JTextPane getTextOutput() {
    if (textOutput == null) {
      textOutput = new JTextPane();
      textOutput.setEditable(false);
    }
    return textOutput;
  }


  private JPanel getPanelInput() {
    if (panelInput == null) {
      GridLayout gridLayout = new GridLayout();
      gridLayout.setRows(23);
      gridLayout.setColumns(1);
      panelInput = new JPanel();
      panelInput.setLayout(gridLayout);
      panelInput.add(getPanelInputButtons(), null);
/*      JPanel buttonPanel = new JPanel();
      gridLayout.setRows(1);
      gridLayout.setColumns(2);
      buttonPanel.setLayout(gridLayout);
*/    }
    return panelInput;
  }

  /**
   * This method initializes panelInputButtons
   *
   * @return javax.swing.JPanel
   */
  private JPanel getPanelInputButtons() {
    if (panelInputButtons == null) {
      GridLayout gridLayout1 = new GridLayout();
      gridLayout1.setRows(1);
      gridLayout1.setHgap(2);
      gridLayout1.setColumns(2);
      panelInputButtons = new JPanel();
      panelInputButtons.setLayout(gridLayout1);
      panelInputButtons.add(getBtnSelectAll(), null);
      panelInputButtons.add(getBtnClearAll(), null);
    }
    return panelInputButtons;
  }


  /**
   * This method initializes btnSelectAll
   *
   * @return javax.swing.JButton
   */
  private JButton getBtnSelectAll() {
    if (btnSelectAll == null) {
      btnSelectAll = new JButton();
      btnSelectAll.setText("Select All");
      btnSelectAll.addActionListener(this);
    }
    return btnSelectAll;
  }


  /**
   * This method initializes btnClearAll
   *
   * @return javax.swing.JButton
   */
  private JButton getBtnClearAll() {
    if (btnClearAll == null) {
      btnClearAll = new JButton();
      btnClearAll.setText("Clear All");
      btnClearAll.addActionListener(this);
    }
    return btnClearAll;
  }


  /**
   * This method initializes panelOutput
   *
   * @return javax.swing.JPanel
   */
  private JPanel getPanelOutput() {
    if (panelOutput == null) {
      GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
      gridBagConstraints1.fill = GridBagConstraints.BOTH;
      gridBagConstraints1.gridy = 1;
      gridBagConstraints1.ipadx = 58;
      gridBagConstraints1.ipady = 4;
      gridBagConstraints1.weightx = 1.0;
      gridBagConstraints1.weighty = 1.0;
      gridBagConstraints1.gridx = 0;
      GridBagConstraints gridBagConstraints = new GridBagConstraints();
      gridBagConstraints.gridx = 0;
      gridBagConstraints.gridheight = 1;
      gridBagConstraints.fill = GridBagConstraints.NONE;
      gridBagConstraints.gridy = 0;
      panelOutput = new JPanel();
      panelOutput.setLayout(new GridBagLayout());
      panelOutput.add(getBtnClearOutput(), gridBagConstraints);
      panelOutput.add(getTextOutput(), gridBagConstraints1);
    }
    return panelOutput;
  }


  /**
   * This method initializes btnClear
   *
   * @return javax.swing.JButton
   */
  private JButton getBtnClearOutput() {
    if (btnClearOutput == null) {
      btnClearOutput = new JButton();
      btnClearOutput.setText("Clear Output");
      btnClearOutput.addActionListener(this);
    }
    return btnClearOutput;
  }


  private static final long serialVersionUID = 1L;
  private JScrollPane scrollOutput = null;
  private JScrollPane scrollInput = null;
  private JTextPane textOutput = null;
  private JPanel panelInput = null;
  private JPanel panelInputButtons = null;
  private JButton btnSelectAll = null;
  private JButton btnClearAll = null;
  private JPanel panelOutput = null;
  private JButton btnClearOutput = null;


}