arcgissamples\toolbarbean\CreateNewToolbarMenu.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.Rectangle; import java.awt.event.ActionEvent; import java.io.IOException; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JTextPane; import javax.swing.UIManager; import com.esri.arcgis.beans.map.MapBean; import com.esri.arcgis.beans.toolbar.ToolbarBean; 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.ControlsOpenDocCommand; import com.esri.arcgis.controls.IToolbarItem; import com.esri.arcgis.controls.IToolbarMenu; 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.IItemDef; import com.esri.arcgis.systemUI.IMenuDef; import com.esri.arcgis.systemUI.esriCommandStyles; import com.esri.arcgis.interop.AutomationException; public class CreateNewToolbarMenu extends JFrame { private MapBean mapBean1 = new MapBean(); private ToolbarBean toolbarBean1 = new ToolbarBean(); private JTextPane jTextPane1 = new JTextPane(); private JTextPane jTextPane2 = new JTextPane(); private JTextPane jTextPane3 = new JTextPane(); private JButton btnAddSub = new JButton(); private JButton btnAdd = new JButton(); private JTextPane jTextPane4 = new JTextPane(); private NavigationMenu menu; public CreateNewToolbarMenu() { setTitle("Toolbar Menu"); try { init(); } catch (Exception e) { e.printStackTrace(); } this.addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent wevt) { try { new AoInitialize().shutdown(); } catch (AutomationException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } System.exit(0); } }); } public void setBuddyControlAndTools() { try { // Buddy-up the toolbarcontrol with the mapcontrol toolbarBean1.setBuddyControl(mapBean1); // Add items to the toolbar... toolbarBean1.addItem(new ControlsMapFullExtentCommand(), 0, 0, false, 0, esriCommandStyles.esriCommandStyleIconAndText); toolbarBean1.addItem(new ControlsMapPanTool(), 0, 0, false, 0, esriCommandStyles.esriCommandStyleIconAndText); toolbarBean1.addItem(new ControlsMapZoomOutTool(), 0, 0, false, 0, esriCommandStyles.esriCommandStyleIconAndText); toolbarBean1.addItem(new ControlsMapZoomInTool(), 0, 0, true, 0, esriCommandStyles.esriCommandStyleIconAndText); toolbarBean1.addItem(new ControlsOpenDocCommand(), 0, 0, false, 0, esriCommandStyles.esriCommandStyleIconAndText); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { /* * Critical: Before we do anything, we must initialize the Engine for use of the Controls */ EngineInitializer.initializeVisualBeans(); initializeArcGISLicenses(); CreateNewToolbarMenu toolbarMenu = new CreateNewToolbarMenu(); toolbarMenu.setSize(650, 475); try { toolbarMenu.setBuddyControlAndTools(); } catch (Exception ex) { ex.printStackTrace(); } toolbarMenu.setVisible(true); toolbarMenu.mapBean1.setSize(400, 345); toolbarMenu.toolbarBean1.setSize(512, 25); } 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();} } private void init() throws Exception { // Set up all of the GUI stuff... this.getContentPane().setLayout(null); this.setBackground(new java.awt.Color(236, 233, 216)); this.getContentPane().setBackground(new java.awt.Color(236, 233, 216)); this.setResizable(false); mapBean1.setBounds(new Rectangle(15, 49, 400, 345)); toolbarBean1.setBounds(new Rectangle(16, 13, 512, 25)); jTextPane1.setBackground(new java.awt.Color(236, 233, 216)); jTextPane1.setFont(new java.awt.Font("Dialog", 0, 10)); jTextPane1.setDisabledTextColor(UIManager.getColor("Label.background")); jTextPane1.setEditable(false); jTextPane1.setText("Browse to a map document to load into the MapBean."); jTextPane1.setBounds(new Rectangle(464, 54, 137, 50)); jTextPane2.setBounds(new Rectangle(464, 109, 135, 50)); jTextPane2.setText("Navigate around the data using the commands on the ToolbarBean."); jTextPane2.setBackground(new java.awt.Color(236, 233, 216)); jTextPane2.setFont(new java.awt.Font("Dialog", 0, 10)); jTextPane2.setEditable(false); jTextPane3.setBackground(new java.awt.Color(236, 233, 216)); jTextPane3.setFont(new java.awt.Font("Dialog", 0, 10)); jTextPane3.setEditable(false); jTextPane3.setText("Add the Navigation menu onto the ToolbarBean."); jTextPane3.setBounds(new Rectangle(464, 166, 135, 37)); btnAddSub.setText("Add Sub Menu"); btnAddSub.addActionListener(new ToolbarMenu_btnAddSub_actionAdapter(this)); btnAddSub.setEnabled(false); btnAddSub.setBounds(new Rectangle(471, 335, 106, 36)); btnAddSub.setFont(new java.awt.Font("Dialog", 0, 10)); btnAddSub.setBorder(BorderFactory.createRaisedBevelBorder()); btnAddSub.setBackground(new java.awt.Color(236, 233, 216)); btnAdd.setBackground(new java.awt.Color(236, 233, 216)); btnAdd.setBounds(new Rectangle(470, 223, 106, 36)); btnAdd.setFont(new java.awt.Font("Dialog", 0, 10)); btnAdd.setBorder(BorderFactory.createRaisedBevelBorder()); btnAdd.setSelected(false); btnAdd.setText("Add Menu"); btnAdd.addActionListener(new ToolbarMenu_btnAdd_actionAdapter(this)); jTextPane4.setBounds(new Rectangle(466, 275, 121, 43)); jTextPane4.setText("Add a sub menu to the Navigation menu."); jTextPane4.setBackground(new java.awt.Color(236, 233, 216)); jTextPane4.setFont(new java.awt.Font("Dialog", 0, 10)); jTextPane4.setToolTipText(""); jTextPane4.setEditable(false); this.getContentPane().add(mapBean1, null); this.getContentPane().add(toolbarBean1, null); this.getContentPane().add(jTextPane1, null); this.getContentPane().add(jTextPane2, null); this.getContentPane().add(btnAdd, null); this.getContentPane().add(btnAddSub, null); this.getContentPane().add(jTextPane3, null); this.getContentPane().add(jTextPane4, null); } void btnAdd_actionPerformed(ActionEvent e) { try { /* * Create an instance of our custom IMenuDef for navigation and add it to the toolbar... */ menu = new NavigationMenu(); int pos = toolbarBean1.getCount(); toolbarBean1.addItem(menu, 0, pos, true, 0, esriCommandStyles.esriCommandStyleMenuBar); btnAdd.setEnabled(false); btnAddSub.setEnabled(true); } catch (Exception ex) { ex.printStackTrace(); } } void btnAddSub_actionPerformed(ActionEvent e) { try { /* * Creat an instance of our custom IMenuDef sub menu and add it to the toolbar... */ ToolbarSubMenu pMenuDef = new ToolbarSubMenu(); int count = toolbarBean1.getCount(); IToolbarItem pToolbarItem = toolbarBean1.getItem(5); IToolbarMenu pToolbarMenu = pToolbarItem.getMenu(); pToolbarMenu.addSubMenu(pMenuDef, 2, true); btnAddSub.setEnabled(false); } catch (Exception ex) { ex.printStackTrace(); } } } class ToolbarSubMenu implements IMenuDef { /** * A sub menu for additional map navigation tools. * * @throws java.io.IOException */ // public ToolbarSubMenu() throws java.io.IOException { // // } public String getName() { return "SubMenu"; } public String getCaption() { return "SubMenu"; } public int getItemCount() { return 8; } public void getItemInfo(int pos, IItemDef itemDef) { try { switch (pos) { case 0: itemDef.setID("esriControlCommands.ControlsMapUpCommand"); break; case 1: itemDef.setID("esriControlCommands.ControlsMapDownCommand"); break; case 2: itemDef.setID("esriControlCommands.ControlsMapLeftCommand"); break; case 3: itemDef.setID("esriControlCommands.ControlsMapRightCommand"); break; case 4: itemDef.setID("esriControlCommands.ControlsMapPageUpCommand"); itemDef.setGroup(true); break; case 5: itemDef.setID("esriControlCommands.ControlsMapPageDownCommand"); break; case 6: itemDef.setID("esriControlCommands.ControlsMapPageLeftCommand"); break; case 7: itemDef.setID("esriControlCommands.ControlsMapPageRightCommand"); break; } } catch (Exception e) { e.printStackTrace(); } } } class NavigationMenu implements IMenuDef { /** * A custom menu for additional map navigation tools. This can be added as an item to a toolbar. * * @throws java.io.IOException */ public NavigationMenu() { // ignore } public String getName() { return "Navigation"; } public String getCaption() { return "Navigation"; } public int getItemCount() { return 5; } public void getItemInfo(int pos, IItemDef itemDef) { try { switch (pos) { case 0: itemDef.setID("esriControlCommands.ControlsMapZoomInFixedCommand"); break; case 1: itemDef.setID("esriControlCommands.ControlsMapZoomOutFixedCommand"); break; case 2: itemDef.setID("esriControlCommands.ControlsMapFullExtentCommand"); itemDef.setGroup(true); break; case 3: itemDef.setID("esriControlCommands.ControlsMapZoomToLastExtentBackCommand"); break; case 4: itemDef.setID("esriControlCommands.ControlsMapZoomToLastExtentForwardCommand"); break; } } catch (Exception e) { e.printStackTrace(); } } } class ToolbarMenu_btnAdd_actionAdapter implements java.awt.event.ActionListener { CreateNewToolbarMenu adaptee; ToolbarMenu_btnAdd_actionAdapter(CreateNewToolbarMenu adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { this.adaptee.btnAdd_actionPerformed(e); } } class ToolbarMenu_btnAddSub_actionAdapter implements java.awt.event.ActionListener { CreateNewToolbarMenu adaptee; ToolbarMenu_btnAddSub_actionAdapter(CreateNewToolbarMenu adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { this.adaptee.btnAddSub_actionPerformed(e); } }