arcgissamples\globe\CreateAndPlayApplication.java—ArcObjects 10.4 Help for Java | ArcGIS for Desktop
Create and play animations in a globe
arcgissamples\globe\CreateAndPlayApplication.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.BorderLayout;
import java.awt.Rectangle;
import java.io.File;

import javax.swing.JDesktopPane;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import com.esri.arcgis.beans.globe.GlobeBean;
import com.esri.arcgis.beans.toolbar.ToolbarBean;
import com.esri.arcgis.controls.ControlsGlobeGlobeToolbar;
import com.esri.arcgis.system.AoInitialize;
import com.esri.arcgis.system.EngineInitializer;
import com.esri.arcgis.system.esriLicenseExtensionCode;
import com.esri.arcgis.system.esriLicenseProductCode;
import com.esri.arcgis.system.esriLicenseStatus;
import com.esri.arcgis.systemUI.esriCommandStyles;

public class CreateAndPlayApplication extends JFrame {

  private static final long serialVersionUID = 1L;

  /**
   * @param args
   */
  public static void main(String[] args) throws Exception {
    //initialize the interop
    initializeInterop();
    
    // initialize to a license level
    initializeArcGISLicenses();
    
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    // start the sample
    final CreateAndPlayApplication thisClass = new CreateAndPlayApplication();
    thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    thisClass.setContentPane(new JDesktopPane());
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        thisClass.initializeUI();
        thisClass.setVisible(true);
        // Enable the progress dialog in anticipation of loading the globe
        // document
        thisClass.getProgressDialog().setVisible(true);
        // Load the globe document in a worker thread
        // Don't engage Swing's event dispatching thread (EDT)
        Thread documentLoader = new Thread(new Runnable() {
          public void run() {
            try {
              String devKitHome = System.getenv("AGSDEVKITJAVA");
              thisClass.getGlobe().load3dFile(devKitHome + "java" + File.separator + 
                                     "samples" + File.separator + 
                                     "data" + File.separator + 
                                     "globe_data" + File.separator +
                                     "Default_Document.3dd");
              
              // Disable the progress dialog when document is loaded
              thisClass.getProgressDialog().setVisible(false);
            } catch (java.lang.Throwable e) {
              e.printStackTrace();
            }
          }
        }, " Globe document loader");
        // Start the worker thread
        documentLoader.start();
      }
    });
  }

  private void initializeUI() {
    this.setContentPane(getJContentPane());
    this.setTitle("Create, Play, Load, and Save Animations");
    this.setBounds(new Rectangle(0, 0, 900, 700));
  }

  private JPanel jContentPane = null;

  private JPanel getJContentPane() {
    if (jContentPane == null) {
      jContentPane = new JPanel();
      jContentPane.setLayout(new BorderLayout());
      jContentPane.add(getGlobe(), BorderLayout.CENTER);
      jContentPane.add(getToolbar(), BorderLayout.NORTH);
      try {
        // Add the animation controls
        AnimationPanel panel = new AnimationPanel(getGlobe());
        jContentPane.add(panel, BorderLayout.WEST);
      } catch (Exception e) {
        e.printStackTrace();
      }

    }
    return jContentPane;
  }

  private GlobeBean globe = null;

  private GlobeBean getGlobe() {
    if (globe == null) {
      globe = new GlobeBean();
    }
    return globe;
  }

  private ToolbarBean toolbar = null;

  private ToolbarBean getToolbar() {
    if (toolbar == null) {
      try {
        toolbar = new ToolbarBean();
        toolbar.addItem(new ControlsGlobeGlobeToolbar(), 0, 0, false, 0,
            esriCommandStyles.esriCommandStyleIconOnly);
        toolbar.setBuddyControl(getGlobe());
      } catch (java.lang.Throwable e) {
        e.printStackTrace();
      }
    }
    return toolbar;
  }

  JDialog progressDialog;

  private JDialog getProgressDialog() {
    if (progressDialog == null) {
      progressDialog = new JDialog();
      progressDialog.setTitle("Loading...");
      JProgressBar bar = new JProgressBar();
      bar.setBorderPainted(true);
      // The progress bar is indeterminate because we don't know how long
      // it will take to load the globe document
      bar.setIndeterminate(true);
      progressDialog.setLocationRelativeTo(this);
      progressDialog.setLocation(this.getWidth() / 2, this.getHeight() / 2);
      progressDialog.add(bar, BorderLayout.PAGE_START);
      progressDialog.pack();
    }
    return progressDialog;
  }

  static void initializeInterop() {
    // Visual beans mode required for multi-threaded applciations like
    // Swing.
    EngineInitializer.initializeVisualBeans();
  }

  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 {
        System.err.println("Could not initialize an Engine or Basic License. Exiting application.");
        System.exit(-1);
      }

      ao.checkOutExtension(esriLicenseExtensionCode.esriLicenseExtensionCode3DAnalyst);
    } catch (Exception e) {e.printStackTrace();}
  }
}