arcgissamples\globe\AnimationPanelActionListener.java—ArcObjects 10.4 Help for Java | ArcGIS for Desktop
Create and play animations in a globe
arcgissamples\globe\AnimationPanelActionListener.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.globe;

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

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.filechooser.FileFilter;

import com.esri.arcgis.analyst3d.IBasicScene2;
import com.esri.arcgis.animation.IAGAnimationEnvironment;
import com.esri.arcgis.animation.IAnimationExtension;
import com.esri.arcgis.animation.esriAnimationPlayMode;
import com.esri.arcgis.beans.globe.GlobeBean;
import com.esri.arcgis.globecore.IGlobe;
import com.esri.arcgis.system.esriArcGISVersion;

public class AnimationPanelActionListener implements ActionListener {

  private AnimationPanel animationPanel;

  private GlobeBean globeBean;

  private FileFilter agaFileFilter;

  public AnimationPanelActionListener(AnimationPanel panel, GlobeBean globeBean) {
  this.animationPanel = panel;
  this.globeBean = globeBean;
  // A file filter to show only ArcGIS animation files
  agaFileFilter = new FileFilter() {
    public boolean accept(File pathname) {
    return pathname.isDirectory()
      || pathname.getAbsolutePath().endsWith("aga");
    }

    public String getDescription() {
    return "Animation Files (*.aga)";
    }
  };
  }

  public void actionPerformed(ActionEvent e) {
  // As a rule, we shouldn't engage Swing's EDT for performing
  // arcobjects operations.
  // But the operations in this sample are relatively inexpensive
  // so we're executing them on the EDT itself
  try {
    IGlobe globe = this.globeBean.getGlobe();
    if (e.getSource() == animationPanel.getBtnCaptureView()) {
    // Capture current view as an animation track
    AnimationUtil.captureCurrentView(globe);
    adjustUIForAnimationLoaded();
    } else if (e.getSource() == animationPanel.getBtnClear()) {
    // Clear the animation tracks
    AnimationUtil.clearAnimation(globe);
    // Disable appropriate components when there is no
    // animation
    adjustUIForAnimationEmpty();
    } else if (e.getSource() == animationPanel.getBtnLoad()) {
    String inputFile = animationPanel.getTxtLoad().getText();
    if (inputFile == null || inputFile.trim().equals("")) {
      JOptionPane.showMessageDialog(null,
        "Please specify a file to load the animation from.",
        "Cannot load animation", JOptionPane.ERROR_MESSAGE);
    } else {
      // Load the animation file
      AnimationUtil.loadAnimation(globe, inputFile);
      // Enable appropriate components for interacting with the
      // animation
      adjustUIForAnimationLoaded();
    }
    } else if (e.getSource() == animationPanel.getBtnSave()) {
    String outputFile = animationPanel.getTxtSave().getText();
    if (outputFile == null || outputFile.trim().equals("")) {
      JOptionPane.showMessageDialog(null,
        "Please specify a file to save the animation in.",
        "Cannot save animation", JOptionPane.ERROR_MESSAGE);
    } else {
      // Save the animation to a .aga file
      AnimationUtil.saveAnimation(globe, outputFile,
        esriArcGISVersion.esriArcGISVersion92);
    }

    } else if (e.getSource() == animationPanel.getBtnBrowse_Load()) {
    // File where the animation will be loaded from
    JFileChooser chooser = new JFileChooser(
        new File(System.getenv("AGSDEVKITJAVA") + File.separator + "java" + File.separator + "samples" +
                               File.separator + "data" + File.separator + "globe_data"));
    chooser.setFileFilter(agaFileFilter);
    int returnVal = chooser.showOpenDialog(animationPanel);
    if (returnVal == JFileChooser.APPROVE_OPTION)
      animationPanel.getTxtLoad().setText(
        chooser.getSelectedFile().getAbsolutePath());
    } else if (e.getSource() == animationPanel.getBtnBrowse_Save()) {
    // File where the animation will be saved
    JFileChooser chooser = new JFileChooser();
    chooser.setFileFilter(agaFileFilter);
    int returnVal = chooser.showOpenDialog(animationPanel);
    if (returnVal == JFileChooser.APPROVE_OPTION)
      animationPanel.getTxtSave().setText(
        chooser.getSelectedFile().getAbsolutePath() + ".aga");
    } else if (e.getSource() == animationPanel.getBtnPlay()) {
    // Play the animation based on the current settings
    IBasicScene2 basicScene = (IBasicScene2) globe.getGlobeDisplay()
      .getScene();
    IAnimationExtension animExt = basicScene.getAnimationExtension();
    IAGAnimationEnvironment env = animExt.getAnimationEnvironment();
    env.setAnimationDuration((new Double(animationPanel.getTxtDuration().getText())).doubleValue());

    if (animationPanel.getCheckboxUseIntervals().isSelected()) {
      env.setIsIntervalPlay(true);
      env.putPlayInterval((new Double(animationPanel.getTxtIntervalFrom().getText())).doubleValue(), (new Double(animationPanel.getTxtIntervalTo().getText())).doubleValue());
    } else {
      env.setIsIntervalPlay(false);
    }

    switch (animationPanel.getComboLoopingOption().getSelectedIndex()) {
    case 0:
      env.setPlayMode(esriAnimationPlayMode.esriAnimationPlayOnceForward);
      break;
    case 1:
      env.setPlayMode(esriAnimationPlayMode.esriAnimationPlayLoopForward);
      break;
    case 2:
      env.setPlayMode(esriAnimationPlayMode.esriAnimationPlayOnceReverse);
      break;
    case 3:
      env.setPlayMode(esriAnimationPlayMode.esriAnimationPlayLoopReverse);
      break;
    default:
      env.setPlayMode(esriAnimationPlayMode.esriAnimationPlayOnceForward);
      break;
    }

    AnimationUtil.playAnimation(globe, env);

    } else if (e.getSource() == animationPanel.getBtnStop()) {
    // Stop the animation
    AnimationUtil.stopAnimation(globe);
    } else if (e.getSource() == animationPanel.getBtnPause()) {
    // Pause the animation
    AnimationUtil.pauseAnimation(globe);
    } else {
    System.out.println("UNHANDLED" + e.getActionCommand());
    }
  } catch (Exception ex) {
    ex.printStackTrace();
  }

  }

  private void adjustUIForAnimationLoaded() {
  // Enable the appropriate components when there is atleast
  // one animation track
  SwingUtilities.invokeLater(new Runnable() {
    public void run() {
    animationPanel.getBtnClear().setEnabled(true);
    animationPanel.getBtnPlay().setEnabled(true);
    animationPanel.getBtnPause().setEnabled(true);
    animationPanel.getBtnStop().setEnabled(true);
    animationPanel.getBtnSave().setEnabled(true);
    animationPanel.getTxtDuration().setEnabled(true);
    animationPanel.getTxtIntervalFrom().setEnabled(true);
    animationPanel.getTxtIntervalTo().setEnabled(true);
    animationPanel.getComboLoopingOption().setEnabled(true);
    animationPanel.getCheckboxUseIntervals().setEnabled(true);

    }

  });

  }

  private void adjustUIForAnimationEmpty() {
  // Disable the appropriate components when there is no
  // animation track
  SwingUtilities.invokeLater(new Runnable() {
    public void run() {
    animationPanel.getBtnClear().setEnabled(false);
    animationPanel.getBtnPlay().setEnabled(false);
    animationPanel.getBtnPause().setEnabled(false);
    animationPanel.getBtnStop().setEnabled(false);
    animationPanel.getBtnSave().setEnabled(false);
    animationPanel.getTxtDuration().setEnabled(false);
    animationPanel.getTxtIntervalFrom().setEnabled(false);
    animationPanel.getTxtIntervalTo().setEnabled(false);
    animationPanel.getComboLoopingOption().setEnabled(false);
    animationPanel.getCheckboxUseIntervals().setEnabled(false);
    }

  });

  }
}