arcgissamples\scenario\ToolBar.java—ArcObjects 10.4 Help for Java | ArcGIS for Desktop
Engine Viewer
arcgissamples\scenario\ToolBar.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.
* 
*/
/*
 * ArcGIS Engine Developer Sample
 * Application Name: Toolbar.java
 */

package arcgissamples.scenario;

/**Class that provides toolbar functinality
 * Creates actions and maintains a collection of these actions in a hash table
 * To use this toolbar you must associate a map component using the setMapComponent method
 *
 *
 *
 */
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Enumeration;
import java.util.Hashtable;

import javax.swing.AbstractAction;
import javax.swing.AbstractButton;
import javax.swing.Action;
import javax.swing.Box;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JToolBar;

import arcgissamples.scenario.map.MapComponent;

public class ToolBar extends JToolBar {
  private static final long serialVersionUID = 1L;
  //Collection of actions
  Hashtable<Object, Object> actionsTable = null;
  MapComponent _mapComponent;

  public ToolBar () {
    super();
    actionsTable = new Hashtable<Object, Object>();
  }

  /**Sets the map component
   * @param mapComponent
   */
  public void setMapControl(MapComponent mapComponent) {
    _mapComponent = mapComponent;
  }

  /**Method to add an action. Each action will be added with a action command
   * declared in the MapActions and an icon.
   * @param actionCommand
   * @param icon
   */


  public void addAction(String actionCommand, Icon icon) {
    Action action = createAction(actionCommand, icon);
    if (action != null) {
      JButton button = add(action);
      button.setBorderPainted(false);
      button.addMouseListener(rolloverMouseListener);
      button.setMargin(new java.awt.Insets(2, 2, 2, 2));
      button.setAlignmentX(0.5f);
      button.setToolTipText( Action.NAME);
      add(Box.createHorizontalStrut(5));
      actionsTable.put( (Object) actionCommand, (Object) action);
    }
  }


  private Action createAction(final String actionCommand,
      Icon icon) {

    Action action = new AbstractAction(actionCommand, icon) {
      private static final long serialVersionUID = 1L;

      public void actionPerformed(ActionEvent e) {
        if (actionCommand.equalsIgnoreCase(MapActions.OPEN_ACTION)) {
          _mapComponent.openMxd();
          setFileOpenActions();
        }
        else if (actionCommand.equalsIgnoreCase(MapActions.ZOOM_IN_ACTION))
          _mapComponent.zoomIn();

        else if (actionCommand.equalsIgnoreCase(MapActions.PAN_ACTION))
          _mapComponent.pan();
        else if (actionCommand.equalsIgnoreCase(MapActions.ZOOM_OUT_ACTION))
          _mapComponent.zoomOut();

        else if (actionCommand.equalsIgnoreCase(MapActions.IDENTIFY_ACTION))
          _mapComponent.identify();
      }
    };

    return action;
  }

  /**This method is called when the file is opened and all other actions need to be enabled.
   * */

  public void setFileOpenActions() {
    for (Enumeration e = actionsTable.keys(); e.hasMoreElements(); ) {
      Object o = e.nextElement();
      Action a = (Action) actionsTable.get(o);
      if (!o.toString().equalsIgnoreCase(MapActions.OPEN_ACTION)) {
        a.setEnabled(true);
      }
    }
  }

  /**This method is called to disable all the actions except the file open action
   * Normally called when there is no map is displayed on the screen
   * */

  public void setEmptyMapActions() {
    for (Enumeration e = actionsTable.keys(); e.hasMoreElements(); ) {
      Object o = e.nextElement();
      Action a = (Action) actionsTable.get(o);
      if (!o.toString().equalsIgnoreCase(MapActions.OPEN_ACTION)) {
        a.setEnabled(false);
      }
    }
  }

  //Rollover mouse listener to get the rollover effec
  private static RolloverMouseListener rolloverMouseListener = new RolloverMouseListener();

  /** If this listener is added to a jbutton then it turns it into a rollover button
   *
   */

  private static class RolloverMouseListener extends MouseAdapter {

    public void mouseEntered(MouseEvent ev) {
      AbstractButton button = (AbstractButton) ev.getSource();
      if (button.isEnabled())
        button.setBorderPainted(true);
    }


    public void mouseExited(MouseEvent ev) {
      AbstractButton button = (AbstractButton) ev.getSource();

      if (!button.isSelected())
        button.setBorderPainted(false);
    }

  }

}