arcgissamples\mapbean\UseMapTOCToolbarBeans.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.mapbean; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.File; import javax.swing.JFrame; import javax.swing.UIManager; import com.esri.arcgis.beans.TOC.TOCBean; import com.esri.arcgis.beans.map.MapBean; import com.esri.arcgis.beans.toolbar.ToolbarBean; import com.esri.arcgis.controls.ControlsAddDataCommand; import com.esri.arcgis.controls.ControlsMapFullExtentCommand; import com.esri.arcgis.controls.ControlsMapPanTool; import com.esri.arcgis.controls.ControlsMapZoomInTool; import com.esri.arcgis.controls.ControlsMapZoomOutTool; import com.esri.arcgis.controls.ControlsMapZoomToLastExtentBackCommand; import com.esri.arcgis.controls.ControlsMapZoomToLastExtentForwardCommand; import com.esri.arcgis.controls.ControlsOpenDocCommand; import com.esri.arcgis.system.AoInitialize; import com.esri.arcgis.system.EngineInitializer; import com.esri.arcgis.system.esriLicenseProductCode; import com.esri.arcgis.system.esriLicenseStatus; import com.esri.arcgis.systemUI.esriCommandStyles; /** * Map, TOC, and Toolbar Control application. Uses the MapBean to load mxd files, a TOCBean to display the table of * contents and a ToolbarBean to perform pan/zoom functions on the MapBean. */ public class UseMapTOCToolbarBeans extends JFrame { private static final long serialVersionUID = 1L; public UseMapTOCToolbarBeans() { super("Java Sample: MapBean, TOCBean and ToolbarBean"); } public static void main(String[] args) { System.out.println("Starting MapTocToolbar - An ArcObjects Java SDK Developer Sample"); //Get DEVKITHOME Home String devKitHome = System.getenv("AGSDEVKITJAVA"); if (!(new File(devKitHome).exists())){ System.out.println(devKitHome + " does not exist.\nExiting..."); System.exit(-1); } // Change the following line if you want to load a different mxd file at startup. String mxFile = devKitHome + File.separator + "java" + File.separator + "samples" + File.separator + "data" + File.separator + "mxds" + File.separator + "usa.mxd"; try { EngineInitializer.initializeVisualBeans(); UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); initializeArcGISLicenses(); UseMapTOCToolbarBeans thisApp = new UseMapTOCToolbarBeans(); thisApp.display(mxFile); } catch (Exception ex) { System.out.println("Sample failed: " + ex.getMessage()); ex.printStackTrace(); } } public void display(String mxFile) throws java.io.IOException { MapBean mapBean = new MapBean(); TOCBean tocBean = new TOCBean(); ToolbarBean toolbarBean = new ToolbarBean(); addWindowListener(new WindowAdapter() { // @Override public void windowClosing(WindowEvent e) { try { new AoInitialize().shutdown(); } catch (Exception ex) { // Don't care } e.getWindow().dispose(); } }); mapBean.clearLayers(); mapBean.loadMxFile(mxFile, null, null); // set the control size mapBean.setSize(600, 600); tocBean.setSize(200, 600); toolbarBean.setSize(800, 25); getContentPane().add(mapBean, BorderLayout.CENTER); getContentPane().add(tocBean, BorderLayout.WEST); getContentPane().add(toolbarBean, BorderLayout.NORTH); setSize(new Dimension(800, 700)); setVisible(true); toolbarBean.addItem(new ControlsAddDataCommand(), 0, 0, false, 0, esriCommandStyles.esriCommandStyleIconOnly); //Add data toolbarBean.addItem(new ControlsOpenDocCommand(), 0, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly); // open toolbarBean.addItem(new ControlsMapZoomInTool(), 0, -1, true, 0, esriCommandStyles.esriCommandStyleIconAndText); // ZoomIn toolbarBean.addItem(new ControlsMapZoomOutTool(), 0, -1, false, 0, esriCommandStyles.esriCommandStyleIconAndText); // ZoomOut toolbarBean.addItem(new ControlsMapPanTool(), 0, -1, false, 0, esriCommandStyles.esriCommandStyleIconAndText); // Pan toolbarBean.addItem(new ControlsMapFullExtentCommand(), 0, -1, true, 20, esriCommandStyles.esriCommandStyleTextOnly); // FullExtent toolbarBean.addItem(new ControlsMapZoomToLastExtentBackCommand(), 0, -1, false, 0, esriCommandStyles.esriCommandStyleTextOnly); // ZoomToLastExtentBackCommand toolbarBean.addItem(new ControlsMapZoomToLastExtentForwardCommand(), 0, -1, false, 0, esriCommandStyles.esriCommandStyleTextOnly); // ZoomToLastExtentForwardCommand // Set the TOC and toolbar buddy to the mapBean tocBean.setBuddyControl(mapBean); toolbarBean.setBuddyControl(mapBean); } 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();} } }