arcgissamples\mapbean\DragAndDropFiles.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.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.IOException; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.UIManager; import com.esri.arcgis.beans.map.MapBean; import com.esri.arcgis.carto.IEnumLayer; import com.esri.arcgis.carto.ILayer; import com.esri.arcgis.carto.LayerFactoryHelper; import com.esri.arcgis.controls.IMapControlEvents2Adapter; import com.esri.arcgis.controls.IMapControlEvents2OnMouseDownEvent; import com.esri.arcgis.controls.IMapControlEvents2OnMouseMoveEvent; import com.esri.arcgis.controls.IMapControlEvents2OnOleDropEvent; import com.esri.arcgis.controls.esriControlsDragDropEffect; import com.esri.arcgis.controls.esriControlsDropAction; import com.esri.arcgis.controls.esriControlsMousePointer; import com.esri.arcgis.system.AoInitialize; import com.esri.arcgis.system.EngineInitializer; import com.esri.arcgis.system.IEnumName; import com.esri.arcgis.system.IName; import com.esri.arcgis.system.esriLicenseProductCode; import com.esri.arcgis.system.esriLicenseStatus; import com.esri.arcgis.systemUI.DataObjectHelper; /** * Description: This sample demonstrates dropping data dragged from ArcCatalog and Windows Explorer onto the MapBean. To * enable data to be dropped onto the MapBean the OleDropEnabled property is set to true which allows the OnOleDrop * event to trigger whenever data is dragged over the control. * <p/> * When dragged data initially enters the MapBean the OnOleDrop event is triggered with an esriDropEnter action. The * IDataObjectHelper interface is used to determine whether the data is being dragged from ArcCatalog or Windows * Explorer using the CanGetNames and CanGetFiles methods. If the data is from ArcCataolg or Windows Explorer the * DragDropEffect is set to esriDragDropCopy, otherwise it is set to esriDragDropNone. The DragDropEffect is then stored * for later use. When data is being dragged around the MapBean the OnOleDrop event is triggered with an esriDropOver * action. The DragDropEffect is set to the effect stored when the data initially entered the control and the mouse * coordinates are updated on the form. The MapBean OnMouseMove event updates the coordinates on the form when the * OnOleDrop event is not being triggered. When data is dropped onto the MapBean the OnOleDrop event is triggered with * an esriDropped action. The IDataObjectHelper interface is used to obtain an array of file path strings from the data * using the GetFiles method, if the CanGetFiles method returns true, otherwise an IEnumName collection is obtained * using the GetNames method. The CheckMxFile method is used to determine whether each file path in the array is a valid * Mx document. If valid, the LoadMxFile method is used to load the document into the MapBean, otherwise a new IFileName * object is created with its path property set to the file path in the array. The IFileName is used to create a layer * that can be added to the MapBean, in the same way that the IName's obtained by enumerating the IEnumName collection * do. (Note that Mx document and layer files (.lyr) if dropped from ArcCatalog will return true for CanGetFiles as they * are both a type of IFileName). The ILayerFactoryHelper interface is used to create a collection of ILayer's using the * CreateLayersFromName method. Each layer is added to the MapBean using the AddLayer method. */ public class DragAndDropFiles extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; JPanel topPanel = null; JPanel mainPanel = null; JPanel bottomPanel = null; JLabel descriptionLabel = null; JCheckBox enableFileCheckBox = null; JLabel coordinateLabel = null; JLabel layerCountLabel = null; JButton clearLayerButton = null; MapBean mapBean = null; int dragEffect; static AoInitialize aoInit; public DragAndDropFiles() { super("Drop Files Example"); buildFrame(); setSize(550, 400); setVisible(true); initControl(); } /** * This method builds 'this' frame as per the following diagram: * /----------------------------------------------------------\ | | | | | MapBean | | BorderLayout.CENTER | | | | | * | | | | |----------------------------------------------------------| | JPanel bottomPanel - BorderLayout.SOUTH | * | | | JCheckbox JLabel JLabel JButton | | | \----------------------------------------------------------/ */ public void buildFrame() { topPanel = new JPanel(); mainPanel = new JPanel(); bottomPanel = new JPanel(); topPanel.setLayout(new FlowLayout(FlowLayout.LEFT)); mainPanel.setLayout(new BorderLayout()); bottomPanel.setLayout(new java.awt.GridLayout(1, 4, 5, 0)); descriptionLabel = new JLabel("Drag and drop files from ArcCatalog and Windows Explorer "); topPanel.add(descriptionLabel); enableFileCheckBox = new JCheckBox("Enable File Dropping", true); enableFileCheckBox.addActionListener(this); coordinateLabel = new JLabel(); layerCountLabel = new JLabel(); clearLayerButton = new JButton("Clear Layers"); clearLayerButton.addActionListener(this); bottomPanel.add(enableFileCheckBox); bottomPanel.add(coordinateLabel); bottomPanel.add(layerCountLabel); bottomPanel.add(clearLayerButton); // Create map control add it to the center of the main panel. mapBean = new MapBean(); mainPanel.add(topPanel, BorderLayout.NORTH); mainPanel.add(mapBean, BorderLayout.CENTER); mainPanel.add(bottomPanel, BorderLayout.SOUTH); mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); getContentPane().add(mainPanel, BorderLayout.CENTER); } /** * Initilizes control Enables old drop and adds map control listener. */ public void initControl() { try { // No entry effect dragEffect = esriControlsDragDropEffect.esriDragDropNone; mapBean.setOleDropEnabled(true); // Add map control listener mapBean.addIMapControlEvents2Listener(new MapControlListener()); layerCount(); } catch (IOException ex) { System.out.println("Exception in initConrol" + ex); ex.printStackTrace(); } } /** * @see java.awt.event.ActionListener#actionPerformed(ActionEvent event) * @param event */ public void actionPerformed(ActionEvent event) { if (event.getSource() == enableFileCheckBox) { try { if (enableFileCheckBox.isSelected()) { mapBean.setOleDropEnabled(true); } else { mapBean.setOleDropEnabled(false); } } catch (IOException ex) { System.out.println("Exception in enableFileCheckBox#actionPerformed : " + ex); ex.printStackTrace(); } } if (event.getSource() == clearLayerButton) { try { mapBean.clearLayers(); layerCount(); } catch (IOException ex) { System.out.println("Exception in clearLayerButton#actionPerformed" + ex); ex.printStackTrace(); } } } /** * Method to update the layer count. */ public void layerCount() { try { // Update map layer count layerCountLabel.setText("Layer Count: " + mapBean.getLayerCount()); } catch (IOException ex) { System.out.println("Exception in layerCount :" + ex); ex.printStackTrace(); } } /** * Method which creates layer and adds it to the map control. * * @param name */ public void createLayer(IName name) throws IOException { try { // Set the mouse pointer mapBean.setMousePointer(esriControlsMousePointer.esriPointerHourglass); // Create LayerFactoryHelper class LayerFactoryHelper layerFactoryHelper = null; layerFactoryHelper = new LayerFactoryHelper(); // Get the IEnumLayer interface through the LayerFatcoryHelper class IEnumLayer enumLayer = layerFactoryHelper.createLayersFromName(name); enumLayer.reset(); // Get the ILayer interface ILayer layer = enumLayer.next(); // Loop through layers while (layer != null) { // Add the layer to the map mapBean.addLayer(layer, 0); layer = enumLayer.next(); } // LayerCount // Set the mouse pointer mapBean.setMousePointer(esriControlsMousePointer.esriPointerDefault); } catch (IOException ex) { System.out.println("Exception in createlayer: " + ex); ex.printStackTrace(); } finally { mapBean.setMousePointer(esriControlsMousePointer.esriPointerDefault); } } /** * Description: Class which extends map control event class IMapControlEvents2Adapter * * @see com.esri.arcgis.beans.map.IMapControlEvents2Adapter */ class MapControlListener extends IMapControlEvents2Adapter { private static final long serialVersionUID = 1L; /** * @see com.esri.arcgis.beans.map.IMapControlEvents2Adapter#onMouseDown(IMapControlEvents2OnMouseDownEvent * theEvent) * @param theEvent */ public void onMouseMove(IMapControlEvents2OnMouseMoveEvent theEvent) { try { // Update mouse coordinates coordinateLabel.setText(" X: " + theEvent.getX() + ", Y: " + theEvent.getY()); } catch (Exception ex) { System.out.println("Exception in MapControlListener#onMouseMove : " + ex); ex.printStackTrace(); } } /** * @see com.esri.arcgis.beans.map.IMapControlEvents2Adapter#onMouseDown(IMapControlEvents2OnOleDropEvent * theEvent) * @param theEvent */ public void onOleDrop(IMapControlEvents2OnOleDropEvent theEvent) { try { DataObjectHelper dataObjectHelper = null; dataObjectHelper = (DataObjectHelper) theEvent.getDataObjectHelper(); int action = theEvent.getDropAction(); // If enter the MapBean if (action == esriControlsDropAction.esriDropEnter) { // If data from Windows Explorer or ArcCatlaog if (dataObjectHelper.canGetFiles() || dataObjectHelper.canGetNames()) // Copy effect dragEffect = esriControlsDragDropEffect.esriDragDropCopy; } if (action == esriControlsDropAction.esriDropOver) { // Set effect theEvent.setEffect(dragEffect); // Update mouse coordinates coordinateLabel.setText(" X: " + theEvent.getX() + ", Y: " + theEvent.getY()); } if (action == esriControlsDropAction.esriDropped) { // If data from Windows Explorer if (dataObjectHelper.canGetFiles()) { // Get an array of file paths through the IDataObjectHelper Object files[] = (Object[]) dataObjectHelper.getFiles(); // Loop through the array for (int i = 0; i < files.length; i++) { String file = files[i].toString(); // If a valid Mx document load it into the MapBean if (mapBean.checkMxFile(file)) { mapBean.loadMxFile(file, null, null); layerCount(); } else { // Add the layer to the map mapBean.addLayerFromFile(file, 0); } }// End for loop } // If data from ArcCatalog else if (dataObjectHelper.canGetNames()) { IEnumName enumName = null; enumName = dataObjectHelper.getNames(); enumName.reset(); // Get the IName interface IName name = enumName.next(); // Loop through names while (name != null) { // Create map layer createLayer(name); name = enumName.next(); } } } } catch (IOException ex) { System.out.println("Exception in MapControlListener#onOleDrop: " + ex); ex.printStackTrace(); } } }// End of MapControlListener class /** * 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()); aoInit = new AoInitialize(); initializeArcGISLicenses(aoInit); DragAndDropFiles dropFiles = new DragAndDropFiles(); dropFiles.setDefaultCloseOperation(DragAndDropFiles.EXIT_ON_CLOSE); } catch (Exception ex) { ex.printStackTrace(); } } static void initializeArcGISLicenses(AoInitialize aoInit) { try { if (aoInit.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeEngine) == esriLicenseStatus.esriLicenseAvailable) aoInit.initialize(esriLicenseProductCode.esriLicenseProductCodeEngine); else if (aoInit.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeArcView) == esriLicenseStatus.esriLicenseAvailable) aoInit.initialize(esriLicenseProductCode.esriLicenseProductCodeArcView); else if (aoInit.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeArcEditor) == esriLicenseStatus.esriLicenseAvailable) aoInit.initialize(esriLicenseProductCode.esriLicenseProductCodeArcEditor); else if (aoInit.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeArcInfo) == esriLicenseStatus.esriLicenseAvailable) aoInit.initialize(esriLicenseProductCode.esriLicenseProductCodeArcInfo); } catch (Exception e) {e.printStackTrace();} } }