arcgissamples\tocbean\DragAndDropLayers.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.tocbean; import java.awt.BorderLayout; import java.io.IOException; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextArea; import com.esri.arcgis.beans.TOC.TOCBean; import com.esri.arcgis.beans.pagelayout.PageLayoutBean; import com.esri.arcgis.beans.toolbar.ToolbarBean; import com.esri.arcgis.carto.IElement; import com.esri.arcgis.carto.Map; import com.esri.arcgis.carto.MapFrame; import com.esri.arcgis.carto.esriViewDrawPhase; import com.esri.arcgis.controls.IPageLayoutControlEventsAdapter; import com.esri.arcgis.controls.IPageLayoutControlEventsOnMouseDownEvent; import com.esri.arcgis.geometry.IEnvelope; import com.esri.arcgis.interop.AutomationException; import com.esri.arcgis.system.AoInitialize; import com.esri.arcgis.system.EngineInitializer; import com.esri.arcgis.system.esriLicenseProductCode; import com.esri.arcgis.system.esriLicenseStatus; /** * Description: This sample demonstrates enabling the drag and drop capabilities * of the TOCBean. It also creates an event listener for the onMouseDown event * to create new map frames in the Page Layout. * * Check the Layer Drag and Drop box to enable the capability. Right-click and * drag on the page layout to create new map frames. */ public class DragAndDropLayers extends JFrame { private JPanel jContentPane = null; private TOCBean tocBean = null; private ToolbarBean toolbarbean1 = null; private PageLayoutBean pageLayoutBean = null; private JPanel jPanel = null; private JCheckBox jCheckBox = null; private JLabel jLabel = null; private JTextArea jTextArea = null; /** * This method initializes tocBean * * @return com.esri.arcgis.beans.TOC.TOCBean */ private TOCBean getTocBean() { if (tocBean == null) { tocBean = new TOCBean(); try { tocBean.setBuddyControl(getPageLayoutBean()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return tocBean; } /** * This method initializes toolbarbean1 * * @return com.esri.arcgis.beans.toolbar.ToolbarBean */ private ToolbarBean getToolbarbean1() { if (toolbarbean1 == null) { toolbarbean1 = new ToolbarBean(); try { toolbarbean1.setBuddyControl(getPageLayoutBean()); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } toolbarbean1 .setItemsString("5|controls/ControlsOpenDocCommand|0|-1|0|0|1;" + "13|controls/ControlsPageZoomInTool|0|-1|0|0|1;" + "13|controls/ControlsPageZoomOutTool|0|-1|0|0|1;" + "13|controls/ControlsPageZoomWholePageCommand|0|-1|0|0|1;" + "13|controls/ControlsPageFocusPreviousMapCommand|0|-1|0|0|1;" + "13|controls/ControlsPageFocusNextMapCommand|0|-1|0|0|1;" + "11|controls/ControlsMapFullExtentCommand|0|-1|0|0|1;" + "11|controls/ControlsMapZoomInTool|0|-1|0|0|1;" + "11|controls/ControlsMapZoomOutTool|0|-1|0|0|1"); } return toolbarbean1; } /** * This method initializes pageLayoutBean * * @return com.esri.arcgis.beans.pagelayout.PageLayoutBean */ private PageLayoutBean getPageLayoutBean() { if (pageLayoutBean == null) { pageLayoutBean = new PageLayoutBean(); try { pageLayoutBean .addIPageLayoutControlEventsListener(new PageLayoutControlListener()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return pageLayoutBean; } /** * This method initializes jPanel * * @return javax.swing.JPanel */ private JPanel getJPanel() { if (jPanel == null) { try { jLabel = new JLabel(); jLabel.setText("Enable Layer Drag Drop"); jPanel = new JPanel(); jPanel.setPreferredSize(new java.awt.Dimension(280, 10)); jPanel.add(getJCheckBox(), null); jPanel.add(jLabel, null); jPanel.add(getJTextArea(), null); } catch (java.lang.Throwable e) { // TODO: Something } } return jPanel; } /** * 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) { // TODO: Something } } return jCheckBox; } protected void jCheckBox_itemStateChanged() { // Enable drag and drop when checkbox is selected if (jCheckBox.isSelected()) try { tocBean.setEnableLayerDragDrop(true); } catch (AutomationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } else try { tocBean.setEnableLayerDragDrop(false); } catch (AutomationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * This method initializes jTextArea * * @return javax.swing.JTextArea */ private JTextArea getJTextArea() { if (jTextArea == null) { try { jTextArea = new JTextArea(); jTextArea .setText("1) Load a map document into the PageLayoutControl" + "\n" + "2) Enable layer drag and drop check box" + "\n" + "3) Re-order the layers in the TOCControl" + "\n" + "4) Right click on the PageLayoutControl and drag a" + "\n" + "rectangle to create a new MapFrame" + "\n" + "5) Copy a layer into the new map by dragging and" + "\n" + " dropping a layer" + "\n" + "6) Move a layer into the new map by dragging and" + "\n" + " dropping a layer while holding the CTRL key down"); jTextArea.setPreferredSize(new java.awt.Dimension(260, 150)); jTextArea.setLineWrap(true); jTextArea.setFont(new java.awt.Font("Dialog", java.awt.Font.PLAIN, 10)); } catch (java.lang.Throwable e) { // TODO: Something } } return jTextArea; } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub EngineInitializer.initializeVisualBeans(); DragAndDropLayers lyrDD = new DragAndDropLayers(); lyrDD.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); lyrDD.setVisible(true); } /** * This is the default constructor */ public DragAndDropLayers() { super(); initializeArcGISLicenses(); initialize(); } /** * This method initializes this * * @return void */ private void initialize() { this.setSize(919, 528); this.setContentPane(getJContentPane()); this.setTitle("JFrame"); this.addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent e) { try { new AoInitialize().shutdown(); } catch (Exception ex) { ex.printStackTrace(); } } }); } /** * This method initializes jContentPane * * @return javax.swing.JPanel */ private JPanel getJContentPane() { if (jContentPane == null) { jContentPane = new JPanel(); jContentPane.setLayout(new BorderLayout()); jContentPane.add(getTocBean(), java.awt.BorderLayout.WEST); jContentPane.add(getToolbarbean1(), java.awt.BorderLayout.NORTH); jContentPane.add(getPageLayoutBean(), java.awt.BorderLayout.CENTER); jContentPane.add(getJPanel(), java.awt.BorderLayout.EAST); } return jContentPane; } 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) { System.out.println("You are not authorized to run this application. Please check your license registration."); System.exit(0); } } /** * Description: Class which extends page layout control event class * IPageLayoutControlEventsAdapter * * @see com.esri.arcgis.beans.pagelayout.IPageLayoutControlEventsAdapter */ class PageLayoutControlListener extends IPageLayoutControlEventsAdapter { /** * @see com.esri.arcgis.beans.pagelayout.IPageLayoutControlEventsAdapter#onMouseDown(IPageLayoutControlEventsOnMouseDownEvent * theEvent) * @param theEvent */ public void onMouseDown( IPageLayoutControlEventsOnMouseDownEvent theEvent) { try { // Continue if it is a right-click if (theEvent.getButton() != 2) return; // Create an envelope by tracking a rectangle IEnvelope envelope = pageLayoutBean.trackRectangle(); // Create a map frame element with a new map MapFrame mapFrame = new MapFrame(); Map map = new Map(); mapFrame.setMapByRef(map); // Add the map frame to the PageLayoutControl with specified // geometry try { pageLayoutBean.addElement((IElement) mapFrame, envelope, null, null, 0); } catch (AutomationException e) { System.err .println("Input geometry cannot be 0. You must drag a rectangle."); } // Refresh the PageLayoutControl pageLayoutBean.refresh(esriViewDrawPhase.esriViewGraphics, null, null); } catch (Exception ex) { System.out .println("Exception in PageLayoutControlListener#onMouseDown : " + ex); ex.printStackTrace(); } } } }