Create scene bookmark
arcgissamples\scenebean\CreateSceneBookmark.java
/* Copyright 2010 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.
* 
*/
package arcgissamples.scenebean;

import java.awt.BorderLayout;
import java.io.IOException;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JTextPane;

import com.esri.arcgis.analyst3d.Bookmark3D;
import com.esri.arcgis.analyst3d.IBookmark3D;
import com.esri.arcgis.analyst3d.ISceneBookmarks;
import com.esri.arcgis.beans.scene.SceneBean;
import com.esri.arcgis.beans.toolbar.ToolbarBean;
import com.esri.arcgis.interop.AutomationException;
import com.esri.arcgis.system.AoInitialize;
import com.esri.arcgis.system.EngineInitializer;
import com.esri.arcgis.system.IArray;
import com.esri.arcgis.system.esriLicenseExtensionCode;

/**
 * Description: This sample demonstrates navigating around the SceneControl using the built in scene navigation, rotate
 * gestures and 3D bookmarks.
 */

public class CreateSceneBookmark extends JFrame
{

  private JPanel jContentPane = null;
  private ToolbarBean toolbarbean1 = null;
  private JPanel jPanel = null;
  private JPanel jPanel1 = null;
  private JCheckBox jCheckBox = null;
  private JTextPane jTextPane = null;
  private JLabel jLabel = null;
  private JCheckBox jCheckBox1 = null;
  private JLabel jLabel1 = null;
  private JTextPane jTextPane1 = null;
  private JList jList = null;
  private DefaultListModel listModel1 = new DefaultListModel();
  private JButton jButton = null;
  private JTextField txtBookmarkName = null;
  private SceneBean scenebean1 = null;
  private IArray totBookmarks = null;
  private JTextPane jTextPane2 = null;

  /**
   * This method initializes toolbarbean1
   * 
   * @return com.esri.arcgis.beans.toolbar.ToolbarBean
   */
  private ToolbarBean getToolbarbean1()
  {
    if (toolbarbean1 == null)
    {
      try
      {
        toolbarbean1 = new ToolbarBean();
        toolbarbean1.setItemsString("14|controls/ControlsSceneOpenDocCommand|0|-1|0|0|1");
        toolbarbean1.setBuddyControl(getScenebean1());
      }
      catch (java.lang.Throwable e)
      {
        System.out.println(e);
      }
    }
    return toolbarbean1;
  }

  /**
   * This method initializes jPanel
   * 
   * @return javax.swing.JPanel
   */
  private JPanel getJPanel()
  {
    if (jPanel == null)
    {
      try
      {
        jLabel1 = new JLabel();
        jLabel1.setText("Rotate Gesture");
        jLabel1.setPreferredSize(new java.awt.Dimension(100, 16));
        jLabel = new JLabel();
        jLabel.setText("Navigate Mode");
        jLabel.setPreferredSize(new java.awt.Dimension(100, 16));
        jPanel = new JPanel();
        jPanel.setPreferredSize(new java.awt.Dimension(300, 10));
        jPanel.add(getJCheckBox(), null);
        jPanel.add(jLabel, null);
        jPanel.add(getJTextPane(), null);
        jPanel.add(getJCheckBox1(), null);
        jPanel.add(jLabel1, null);
        jPanel.add(getJTextPane1(), null);
      }
      catch (java.lang.Throwable e)
      {
        System.out.println(e);
      }
    }
    return jPanel;
  }

  /**
   * This method initializes jPanel1
   * 
   * @return javax.swing.JPanel
   */
  private JPanel getJPanel1()
  {
    if (jPanel1 == null)
    {
      try
      {
        jPanel1 = new JPanel();
        jPanel1.setPreferredSize(new java.awt.Dimension(53, 150));
        jPanel1.add(getJTextPane2(), null);
        jPanel1.add(getJList(), null);
        jPanel1.add(getJButton(), null);
        jPanel1.add(getTxtBookmarkName(), null);
      }
      catch (java.lang.Throwable e)
      {
        System.out.println(e);
      }
    }
    return jPanel1;
  }

  /**
   * This method initializes jCheckBox
   * 
   * @return javax.swing.JCheckBox
   */
  private JCheckBox getJCheckBox()
  {
    if (jCheckBox == null)
    {
      try
      {
        jCheckBox = new JCheckBox();
        jCheckBox.addItemListener(new java.awt.event.ItemListener()
        {
          public void itemStateChanged(java.awt.event.ItemEvent e)
          {
            jCheckBox_itemStateChanged();
          }
        });
      }
      catch (java.lang.Throwable e)
      {
        System.out.println(e);
      }
    }
    return jCheckBox;
  }

  protected void jCheckBox_itemStateChanged()
  {

    // Enable/disable navigation mode
    if (jCheckBox.isSelected())
      try
      {
        scenebean1.setNavigate(true);
      }
      catch (AutomationException e)
      {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      catch (IOException e)
      {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    else
      try
      {
        scenebean1.setNavigate(false);
      }
      catch (AutomationException e)
      {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      catch (IOException e)
      {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
  }

  /**
   * This method initializes jTextPane
   * 
   * @return javax.swing.JTextPane
   */
  private JTextPane getJTextPane()
  {
    if (jTextPane == null)
    {
      try
      {
        jTextPane = new JTextPane();
        jTextPane.setPreferredSize(new java.awt.Dimension(150, 75));
        jTextPane.setText("Left Mouse to Rotate" + "\n" + "Middle mouse to pan" + "\n" + "Right mouse to zoom");
      }
      catch (java.lang.Throwable e)
      {
        System.out.println(e);
      }
    }
    return jTextPane;
  }

  /**
   * This method initializes jCheckBox1
   * 
   * @return javax.swing.JCheckBox
   */
  private JCheckBox getJCheckBox1()
  {
    if (jCheckBox1 == null)
    {
      try
      {
        jCheckBox1 = new JCheckBox();
        jCheckBox1.addItemListener(new java.awt.event.ItemListener()
        {
          public void itemStateChanged(java.awt.event.ItemEvent e)
          {
            jCheckBox1_itemStateChanged();
          }
        });
      }
      catch (java.lang.Throwable e)
      {
        // TODO: Something
      }
    }
    return jCheckBox1;
  }

  protected void jCheckBox1_itemStateChanged()
  {

    // Enable/disable rotate gesture when checked
    if (jCheckBox.isSelected())
      try
      {
        scenebean1.getSceneGraph().getActiveViewer().setGestureEnabled(true);
      }
      catch (Exception e)
      {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    else
      try
      {
        scenebean1.getSceneGraph().getActiveViewer().setGestureEnabled(false);
      }
      catch (Exception e)
      {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
  }

  /**
   * This method initializes jTextPane1
   * 
   * @return javax.swing.JTextPane
   */
  private JTextPane getJTextPane1()
  {
    if (jTextPane1 == null)
    {
      try
      {
        jTextPane1 = new JTextPane();
        jTextPane1.setPreferredSize(new java.awt.Dimension(150, 125));
        jTextPane1
            .setText("Hold down left mouse button, move mouse left (or right), and keep mouse moving while releasing the left button.  Press escape to stop rotation.");
      }
      catch (java.lang.Throwable e)
      {
        System.out.println(e);
      }
    }
    return jTextPane1;
  }

  /**
   * This method initializes jList
   * 
   * @return javax.swing.JList
   */
  private JList getJList()
  {
    if (jList == null)
    {
      try
      {
        jList = new JList();
        jList.setPreferredSize(new java.awt.Dimension(150, 125));
        jList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jList.setBackground(java.awt.Color.lightGray);
        jList.setFont(new java.awt.Font("Dialog", java.awt.Font.BOLD, 12));
        jList.addMouseListener(new java.awt.event.MouseAdapter()
        {
          public void mouseClicked(java.awt.event.MouseEvent e)
          {
            jList_mouseClicked();
          }
        });

      }
      catch (java.lang.Throwable e)
      {
        System.out.println(e);
      }
    }
    return jList;
  }

  protected void jList_mouseClicked()
  {

    try
    {

      // Get a bookmark corresponding to list and apply it to the
      // SceneViewer
      IBookmark3D bookmark = (IBookmark3D) totBookmarks.getElement(jList.getSelectedIndex());
      // Switch to new bookmark location
      bookmark.apply(scenebean1.getSceneGraph().getActiveViewer(), false, 0);

    }
    catch (AutomationException e)
    {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    catch (IOException e)
    {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    UpdateBookmarks();

  }

  /**
   * This method initializes jButton
   * 
   * @return javax.swing.JButton
   */
  private JButton getJButton()
  {
    if (jButton == null)
    {
      try
      {
        jButton = new JButton();
        jButton.setPreferredSize(new java.awt.Dimension(130, 25));
        jButton.setFont(new java.awt.Font("Dialog", java.awt.Font.BOLD, 10));
        jButton.setVerticalAlignment(javax.swing.SwingConstants.CENTER);
        jButton.setText("Capture Bookmark");
        jButton.addActionListener(new java.awt.event.ActionListener()
        {
          public void actionPerformed(java.awt.event.ActionEvent e)
          {
            jButton_actionPerformed();
          }
        });
      }
      catch (java.lang.Throwable e)
      {
        // TODO: Something
      }
    }
    return jButton;
  }

  protected void jButton_actionPerformed()
  {

    try
    {

      Bookmark3D bookmark3d = new Bookmark3D();
      bookmark3d.setName(txtBookmarkName.getText());
      bookmark3d.capture(scenebean1.getCamera());
      ISceneBookmarks bookmarks = (ISceneBookmarks) scenebean1.getScene();
      bookmarks.addBookmark(bookmark3d);

    }
    catch (AutomationException e)
    {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    catch (IOException e)
    {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    UpdateBookmarks();

  }

  /**
   * This method initializes jTextField
   * 
   * @return javax.swing.JTextField
   */
  private JTextField getTxtBookmarkName()
  {
    if (txtBookmarkName == null)
    {
      try
      {
        txtBookmarkName = new JTextField();
        txtBookmarkName.setPreferredSize(new java.awt.Dimension(150, 25));
      }
      catch (java.lang.Throwable e)
      {
        // TODO: Something
      }
    }
    return txtBookmarkName;
  }

  /**
   * This method initializes scenebean1
   * 
   * @return com.esri.arcgis.beans.scene.SceneBean
   */
  private SceneBean getScenebean1()
  {
    if (scenebean1 == null)
    {
      try
      {
        scenebean1 = new SceneBean();
      }
      catch (java.lang.Throwable e)
      {
        // TODO: Something
      }
    }
    return scenebean1;
  }

  /**
   * This method initializes jTextPane2
   * 
   * @return javax.swing.JTextPane
   */
  private JTextPane getJTextPane2()
  {
    if (jTextPane2 == null)
    {
      try
      {
        jTextPane2 = new JTextPane();
        jTextPane2.setPreferredSize(new java.awt.Dimension(100, 100));
        jTextPane2.setFont(new java.awt.Font("Dialog", java.awt.Font.PLAIN, 12));
        jTextPane2.setText("Enter a name, capture the bookmark and then click on the list to view it.");
      }
      catch (java.lang.Throwable e)
      {
        // TODO: Something
      }
    }
    return jTextPane2;
  }

  /**
   * @param args
   */
  public static void main(String[] args)
  {
    EngineInitializer.initializeVisualBeans();
    CreateSceneBookmark sceneNav = new CreateSceneBookmark();
    sceneNav.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    sceneNav.setVisible(true);

  }

  /**
   * This is the default constructor
   */
  public CreateSceneBookmark()
  {
    super();
    initializeArcGISLicenses();
    initialize();
    UpdateBookmarks();
  }

  /**
   * This method initializes this
   * 
   * @return void
   */
  private void initialize()
  {
    this.setSize(768, 639);
    this.setContentPane(getJContentPane());
    this.setTitle("Scene Navigate and Bookmarks - Open a SXD file to get started");
    this.addWindowListener(new java.awt.event.WindowAdapter()
    {
      public void windowClosing(java.awt.event.WindowEvent e)
      {
        try
        {
          new AoInitialize().checkInExtension(esriLicenseExtensionCode.esriLicenseExtensionCode3DAnalyst);
          new AoInitialize().shutdown();
        }
        catch (Exception ex)
        {
          ex.printStackTrace();
        }
      }
    });
  }

  void initializeArcGISLicenses()
  {
    try
    {
      com.esri.arcgis.system.AoInitialize ao = new com.esri.arcgis.system.AoInitialize();
      if (ao.isProductCodeAvailable(com.esri.arcgis.system.esriLicenseProductCode.esriLicenseProductCodeEngine) == com.esri.arcgis.system.esriLicenseStatus.esriLicenseAvailable)
        ao.initialize(com.esri.arcgis.system.esriLicenseProductCode.esriLicenseProductCodeEngine);
      else if (ao
          .isProductCodeAvailable(com.esri.arcgis.system.esriLicenseProductCode.esriLicenseProductCodeArcView) == com.esri.arcgis.system.esriLicenseStatus.esriLicenseAvailable)
        ao.initialize(com.esri.arcgis.system.esriLicenseProductCode.esriLicenseProductCodeArcView);
      
      ao.checkOutExtension(esriLicenseExtensionCode.esriLicenseExtensionCode3DAnalyst);
    }
    catch (Exception e)
    {
      System.out.println("You do not have the proper license to run this application. ");
      System.exit(0);
    }
  }

  private void UpdateBookmarks()
  {
    // Get bookmarks from Scene
    ISceneBookmarks bookmarks;
    try
    {
      bookmarks = (ISceneBookmarks) scenebean1.getScene();
      totBookmarks = null;
      totBookmarks = bookmarks.getBookmarks();

      // Clear list boxes
      listModel1.clear();
      jList.setModel(listModel1);

      boolean haveBookmarks = false;

      IBookmark3D bookmark3d;
      if (totBookmarks != null)
      {
        // Add the bookmark names to the listbox in the same order as
        // they are in the Scene Document
        for (int i = 0; i <= totBookmarks.getCount() - 1; i++)
        {
          bookmark3d = (IBookmark3D) totBookmarks.getElement(i);
          listModel1.addElement(bookmark3d.getName());
        }
        haveBookmarks = totBookmarks.getCount() != 0;
        jList.setEnabled(true);
      }

      if (haveBookmarks == false)
      {
        // No bookmarks available
        listModel1.addElement("No Bookmarks Available");
        jList.setEnabled(false);
      }

    }
    catch (AutomationException e)
    {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    catch (IOException e)
    {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

  }

  /**
   * This method initializes jContentPane
   * 
   * @return javax.swing.JPanel
   */
  private JPanel getJContentPane()
  {
    if (jContentPane == null)
    {
      jContentPane = new JPanel();
      jContentPane.setLayout(new BorderLayout());
      jContentPane.add(getToolbarbean1(), java.awt.BorderLayout.NORTH);
      jContentPane.add(getJPanel(), java.awt.BorderLayout.EAST);
      jContentPane.add(getJPanel1(), java.awt.BorderLayout.SOUTH);
      jContentPane.add(getScenebean1(), java.awt.BorderLayout.CENTER);
    }
    return jContentPane;
  }

} // @jve:decl-index=0:visual-constraint="10,10"