arcgissamples\toolbarbean\MapAsToolbarBuddy.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.toolbarbean; import java.awt.BorderLayout; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; import java.io.IOException; import javax.swing.BorderFactory; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTabbedPane; import javax.swing.UIManager; import javax.swing.event.ChangeListener; import arcgissamples.toolbarbean.commands.FullExtentCommand; import arcgissamples.toolbarbean.commands.OpenCommand; import arcgissamples.toolbarbean.commands.ZoomInCommand; import arcgissamples.toolbarbean.commands.ZoomOutCommand; import com.esri.arcgis.beans.map.MapBean; import com.esri.arcgis.beans.pagelayout.PageLayoutBean; import com.esri.arcgis.beans.toolbar.ToolbarBean; import com.esri.arcgis.controls.IToolbarBuddy; 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.ITool; import com.esri.arcgis.systemUI.esriCommandStyles; /** * This sample demonstrates setting a user application, supporting the IToolbarBuddy interface as the ToolbarBean Buddy. * The 'buddy' application is a form that implements IToolbarBuddy and contains the MapBean and PageLayoutBean. The * ToolbarBean uses the 'buddy' application to set the CurrentTool. The sample contains four commands that are defined * within the same application. * <p/> * Items are added to the ToolbarBean by passing 'LoadMxFile', 'ZoomIn', 'ZoomOut' and 'FullExtent' commands defined * within the application to the AddItem method. Each item's Command::OnCreate method is passed the ToolbarBean as the * hook. The application is accessed from the hook's Buddy. Each command is written to handle the application and work * with both the MapBean and PageLayoutBean. For example, the 'ZoomIn' command works with the MapBean in MapUnits and * the PageLayoutBean in PageUnits. * <p/> * In order to set the application as a Buddy, the application must implement the IToolbarBuddy interface. The * CurrentTool of both the MapBean and PageLayoutBean is set to the IToolbarBuddy::CurrentTool property. The TabStrip * control's click event determines whether the MapBean or PageLayoutBean is the active control that the ToolbarBean * item commands will work with. */ public class MapAsToolbarBuddy extends JFrame implements IToolbarBuddy, ChangeListener, WindowListener { JPanel leftPanel = null; JPanel mainPanel = null; JTabbedPane tabbedPane = null; PageLayoutBean pageLayoutBean; MapBean mapBean; ToolbarBean toolbarBean; ITool currentTool; public static int ACTIVECONTROL = -1; public final static int MAPCONTROL = 1; public final static int PAGELAYOUTCONTROL = 2; public MapAsToolbarBuddy() { super("Application As Toolbar Buddy"); buildFrame(); setSize(700, 450); setVisible(true); initControl(); } /** * This method builds 'this' frame as per the following diagram: * /------------------------------------------------------------------------------| | BorderLayout.NORTH * ToolBarControl | | |----------------------JTabbedPane------------------------------------------ | | | | | | | | | * | | | | | MapBean | PageLayout | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * | | | | -----------------------------------|-------------------------------------------- */ public void buildFrame() { mainPanel = new JPanel(); mainPanel.setLayout(new BorderLayout(5, 5)); toolbarBean = new ToolbarBean(); toolbarBean.setSize(700, 20); tabbedPane = new JTabbedPane(); JPanel mapPanel = new JPanel(new BorderLayout()); mapBean = new MapBean(); mapPanel.add(mapBean, BorderLayout.CENTER); JPanel pagePanel = new JPanel(new BorderLayout()); pageLayoutBean = new PageLayoutBean(); pagePanel.add(pageLayoutBean, BorderLayout.CENTER); tabbedPane.addTab("PageLayout Control", pagePanel); // Workaround to set the map control as the selected panel. tabbedPane.insertTab("Map Control", null, mapPanel, null, 0); tabbedPane.addChangeListener(this); ACTIVECONTROL = PAGELAYOUTCONTROL; mainPanel.add(toolbarBean, BorderLayout.NORTH); mainPanel.add(tabbedPane, BorderLayout.CENTER); mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); getContentPane().add(mainPanel, BorderLayout.CENTER); } /** * Initializes control */ public void initControl() { try { // Set the Buddy toolbarBean.setBuddyControl(this); // Add tool bar items.. toolbarBean.addItem(new OpenCommand(), 0, 0, false, 0, esriCommandStyles.esriCommandStyleTextOnly); // open toolbarBean.addItem(new ZoomInCommand(), 0, -1, true, 0, esriCommandStyles.esriCommandStyleIconAndText); // ZoomIn toolbarBean.addItem(new ZoomOutCommand(), 0, -1, false, 0, esriCommandStyles.esriCommandStyleIconAndText); // ZoomOut toolbarBean.addItem(new FullExtentCommand(), 0, -1, true, 20, esriCommandStyles.esriCommandStyleTextOnly); // FullExtent } catch (IOException ex) { System.out.println("Exception in initControl : " + ex); ex.printStackTrace(); } } public MapBean getMapBean() { return mapBean; } public PageLayoutBean getPageLayoutBean() { return pageLayoutBean; } /** * The method returns the control that is visible. Used by the tools zoomin, zoomout and fullextent to determine * which control is currently visible */ public int getActiveControlType() { return ACTIVECONTROL; } public ITool getCurrentTool() { return currentTool; } public void setCurrentToolByRef(ITool RHS) { try { // Set the current tool if (RHS == null) return; currentTool = RHS; mapBean.setCurrentToolByRef(currentTool); pageLayoutBean.setCurrentToolByRef(currentTool); } catch (IOException ex) { ex.printStackTrace(); } } /** * @see javax.swing.event.ChangeListener#stateChanged(javax.swing.event.ChangeEvent event) * @param event */ public void stateChanged(javax.swing.event.ChangeEvent event) { if (event.getSource() == tabbedPane) { if (tabbedPane.getSelectedIndex() == 0) { mapBean.setVisible(true); pageLayoutBean.setVisible(false); ACTIVECONTROL = MAPCONTROL; } else { mapBean.setVisible(false); pageLayoutBean.setVisible(true); ACTIVECONTROL = PAGELAYOUTCONTROL; } } } /** * @see java.awt.event.WindowEvent#windowClosing(javax.swing.event.WindowEvent event) * @param event */ public void windowClosing(WindowEvent event) { try { toolbarBean.setBuddyControl(null); } catch (IOException ex) { System.out.println("Exception in WindowListener#windowClosing: " + ex); ex.printStackTrace(); } } public void windowOpened(WindowEvent event) { } public void windowClosed(WindowEvent event) { } public void windowIconified(WindowEvent event) { } public void windowDeiconified(WindowEvent event) { } public void windowActivated(WindowEvent event) { } public void windowDeactivated(WindowEvent event) { } /** * Main program to start the program execution. * * @param s */ public static void main(String s[]) { try { EngineInitializer.initializeVisualBeans(); // Set the system look and feel UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); initializeArcGISLicenses(); MapAsToolbarBuddy applicationAsToolbarBuddy = new MapAsToolbarBuddy(); applicationAsToolbarBuddy.setDefaultCloseOperation(MapAsToolbarBuddy.EXIT_ON_CLOSE); } catch (Exception 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); else if (ao.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeArcEditor) == esriLicenseStatus.esriLicenseAvailable) ao.initialize(esriLicenseProductCode.esriLicenseProductCodeArcEditor); else if (ao.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeArcInfo) == esriLicenseStatus.esriLicenseAvailable) ao.initialize(esriLicenseProductCode.esriLicenseProductCodeArcInfo); } catch (Exception e) {e.printStackTrace();} } }