Engine Viewer
arcgissamples\scenario\EngineViewer.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.scenario;

import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;

import javax.swing.ImageIcon;
import javax.swing.JFrame;

import arcgissamples.scenario.map.MapComponent;
import arcgissamples.scenario.map.MapInfo;
import arcgissamples.scenario.toc.TocControl;

import com.esri.arcgis.system.AoInitialize;
import com.esri.arcgis.system.EngineInitializer;
import com.esri.arcgis.system.esriLicenseProductCode;
import com.esri.arcgis.system.esriLicenseStatus;


public class EngineViewer extends JFrame {
  private static final long serialVersionUID = 1L;
  //Default screen width
  private int defaultScreenWidth = 850;
  //Default screen height
  private int defaultScreenHeight = 630;
  //Splitpane which will contain toc as left component and map as right.
  private javax.swing.JSplitPane splitPane = null;
  //MapComponent
  private MapComponent mapComponent;
  //Toolbar Control
  private ToolBar toolbar;

  public EngineViewer() {
    super("Engine Viewer");
    init();

  }

  public void init() {

    //Create mapComponent instance
    mapComponent = new MapComponent();

    toolbar = new ToolBar();
    toolbar.setMapControl(mapComponent);

    toolbar.addAction(MapActions.OPEN_ACTION,
        new ImageIcon(getClass().
            getResource("/arcgissamples/scenario/icons/Open.gif")));

    toolbar.addAction(MapActions.ZOOM_IN_ACTION,
        new ImageIcon(getClass().
            getResource("/arcgissamples/scenario/icons/zi.gif")));

    toolbar.addAction(MapActions.ZOOM_OUT_ACTION,
        new ImageIcon(getClass().
            getResource("/arcgissamples/scenario/icons/zo.gif")));

    toolbar.addAction(MapActions.PAN_ACTION,
        new ImageIcon(getClass().
            getResource("/arcgissamples/scenario/icons/Pan.gif")));

    toolbar.addAction(MapActions.IDENTIFY_ACTION,
        new ImageIcon(getClass().
            getResource("/arcgissamples/scenario/icons/Identify.gif")));

    toolbar.setEmptyMapActions();

    //Create Toc contriol
    TocControl tocControl = new TocControl(mapComponent);
    mapComponent.setTocControl(tocControl);

    //Create toolbar combobox to display data frame names
    javax.swing.JComboBox cb = new javax.swing.JComboBox();
    cb.setMaximumSize(new Dimension(150, cb.getPreferredSize().height));
    toolbar.add(cb);

    mapComponent.setMapFrameComboBox(cb);

    addWindowListener(
        new WindowAdapter() {
          public void windowClosing(WindowEvent e) {
            if(MapInfo.getmapInfoInstance() != null)
              mapComponent.stopServer();
            System.exit(0);
          }
        });

    //Create splitpane to add TOC on left and MAP as a right component
    splitPane = new javax.swing.JSplitPane(javax.swing.JSplitPane.HORIZONTAL_SPLIT);
    splitPane.setLeftComponent(tocControl);
    splitPane.setDividerLocation(190);
    splitPane.setRightComponent(mapComponent);

    this.getContentPane().add(toolbar, java.awt.BorderLayout.NORTH);
    this.getContentPane().add(splitPane, java.awt.BorderLayout.CENTER);

    EngineViewer.this.setSize(new Dimension(defaultScreenWidth, defaultScreenHeight));
    EngineViewer.this.setVisible(true);

  }

  public static void main(String[] args) {
    try {
      //Initialize ArcGIS Engine for Java API usage.
      EngineInitializer.initializeVisualBeans();

      // Initialize ArcObjects at Appropriate Product Level.
      initializeArcGISLicenses();
      
      //Instantiate simple viewer object.
      EngineViewer engineViewer = new EngineViewer();
      //Default close operation.
      engineViewer.setDefaultCloseOperation(EngineViewer.EXIT_ON_CLOSE);
    }
    catch (Exception ex) {
      System.out.println("Exception in Main class : " + ex);
      ex.printStackTrace();
    }
  }

  static void initializeArcGISLicenses() {
    try {
      AoInitialize ao = new AoInitialize();
      
      if (ao.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeEngine) 
          == esriLicenseStatus.esriLicenseAvailable)
        ao.initialize(esriLicenseProductCode.esriLicenseProductCodeEngine);
      else if (ao.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeArcView) 
          == esriLicenseStatus.esriLicenseAvailable)
        ao.initialize(esriLicenseProductCode.esriLicenseProductCodeArcView);
    } catch (Exception e) {e.printStackTrace();}
  }
  
  
}