arcgissamples\mapbean\ShowMapTips.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.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.IOException; import javax.swing.BorderFactory; import javax.swing.Box; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.UIManager; import com.esri.arcgis.beans.map.MapBean; import com.esri.arcgis.carto.FeatureLayer; import com.esri.arcgis.carto.ILayer; import com.esri.arcgis.controls.IMapControlEvents2Adapter; import com.esri.arcgis.controls.IMapControlEvents2OnMouseDownEvent; import com.esri.arcgis.controls.IMapControlEvents2OnMouseMoveEvent; import com.esri.arcgis.geodatabase.IField; import com.esri.arcgis.geodatabase.esriFieldType; import com.esri.arcgis.geometry.IEnvelope; import com.esri.arcgis.interop.NativeLoader; 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 displaying map tips in the MapBean by setting the ToolTipText property to the * value of ILayer::TipText in the OnMouseMove event. The file chooser dialog allows users to search and select map * documents, which are loaded into the MapBean using the LoadMxFile method. The Name of each layer within the MapBean * is asses to a ComboBox at the same index. When a layers Name is selected in the ComboBox the ILayerFields and IField * interfaces are used to loop through the layers fields. The Name of each field is added to a second ComboBox. If the * field Name is the same as the IFeatureLayer::DisplayField it is selected in the ComboBox. The DisplayField is the * field from which ILayer::TipText is obtained and can be changed by selecting a different field Name from the * ComboBox. A CheckBox indicates whether map tips will be displayed for the selected layer using the ILayer::ShowTips * property. If ILayer::ShowTips is true, the MapBean's ToolTipText property is set to the ILayer::TipText value within * the OnMouseMove event. The map tips is displayed at the bottom of the panel below map control The OnMouseDown event * of the MapBean uses the TrackRectangle method to zoom in if the left mouse button is used, and the Pan method if the * right mouse button is used. */ public class ShowMapTips extends JFrame implements ActionListener { JPanel topPanel = null; JPanel mainPanel = null; JPanel bottomPanel = null; JPanel checkBoxAndButtonPanel = null; JPanel labelAndComboBoxPanel = null; JPanel labelsPanel = null; JPanel comboBoxesPanel = null; JCheckBox showMapTipsCheckBox = null; JComboBox dataLayerCombo = null; JComboBox dataFieldCombo = null; JButton addLayerButton = null; JButton zoomFullExtentButton = null; String helpString = "Left mouse button to zoomin, right to pan"; JLabel helpLabel = null; JLabel layersLabel = null; JLabel fieldsLabel = null; JLabel maptipsText = null; MapBean mapBean = null; public ShowMapTips() { super("Map Tips"); buildFrame(); setSize(400, 500); setVisible(true); initControl(); } public void buildFrame() { topPanel = new JPanel(); mainPanel = new JPanel(); bottomPanel = new JPanel(); topPanel.setLayout(new BorderLayout()); mainPanel.setLayout(new BorderLayout()); bottomPanel.setLayout(new BorderLayout(5, 5)); checkBoxAndButtonPanel = new JPanel(); checkBoxAndButtonPanel.setLayout(new BoxLayout(checkBoxAndButtonPanel, BoxLayout.Y_AXIS)); addLayerButton = new JButton("Add Data Layer"); addLayerButton.addActionListener(this); showMapTipsCheckBox = new JCheckBox("Show Map Tips"); showMapTipsCheckBox.addActionListener(this); checkBoxAndButtonPanel.add(addLayerButton); checkBoxAndButtonPanel.add(Box.createHorizontalStrut(5)); checkBoxAndButtonPanel.add(showMapTipsCheckBox); checkBoxAndButtonPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 10, 10)); labelAndComboBoxPanel = new JPanel(new BorderLayout(5, 5)); labelsPanel = new JPanel(new GridLayout(0, 1)); layersLabel = new JLabel("Layers"); fieldsLabel = new JLabel("Fields"); labelsPanel.add(layersLabel); labelsPanel.add(fieldsLabel); comboBoxesPanel = new JPanel(new GridLayout(0, 1)); dataLayerCombo = new JComboBox(); dataLayerCombo.addActionListener(this); dataLayerCombo.setLightWeightPopupEnabled(false); dataFieldCombo = new JComboBox(); dataFieldCombo.addActionListener(this); dataFieldCombo.setLightWeightPopupEnabled(false); comboBoxesPanel.add(dataLayerCombo); comboBoxesPanel.add(dataFieldCombo); labelAndComboBoxPanel.add(labelsPanel, BorderLayout.WEST); labelAndComboBoxPanel.add(comboBoxesPanel, BorderLayout.CENTER); labelAndComboBoxPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 10, 10)); topPanel.add(checkBoxAndButtonPanel, BorderLayout.WEST); topPanel.add(labelAndComboBoxPanel, BorderLayout.CENTER); helpLabel = new JLabel(helpString); zoomFullExtentButton = new JButton("Zoom to Full Extent"); zoomFullExtentButton.addActionListener(this); maptipsText = new JLabel("Map Tip : "); bottomPanel.add(helpLabel, BorderLayout.CENTER); bottomPanel.add(zoomFullExtentButton, BorderLayout.EAST); bottomPanel.add(maptipsText, BorderLayout.SOUTH); // 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 creates and intializes member variables */ public void initControl() { try { showMapTipsCheckBox.setEnabled(false); dataLayerCombo.setEnabled(false); dataFieldCombo.setEnabled(false); mapBean.addIMapControlEvents2Listener(new MapControlListener()); } catch (IOException ex) { System.out.println("Exception in initControl " + ex); ex.printStackTrace(); } } /** * @see java.awt.event.ActionListener#actionPerformed(ActionEvent event) * @param event */ public void actionPerformed(ActionEvent event) { if (event.getSource() == addLayerButton) { try { if (!loadFile()) return; javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { showMapTipsCheckBox.setEnabled(true); dataLayerCombo.setEnabled(true); dataFieldCombo.setEnabled(true); updateLayersDropDown(); } }); } catch (IOException ex) { System.out.println("Exception in addLayerButton#actionPerformed" + ex); ex.printStackTrace(); } } if (event.getSource() == showMapTipsCheckBox) { showLayerTips(); } if (event.getSource() == dataLayerCombo) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { dataLayerComboActionPerformed(); } }); } if (event.getSource() == dataFieldCombo) { try { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { dataFieldComboActionPerformed(); } }); } catch (Exception ex3) { System.err.println("ex caught"); } } if (event.getSource() == zoomFullExtentButton) { try { // Assign map controls extent property to the full extent of all the layers mapBean.setExtent(mapBean.getFullExtent()); } catch (Exception ex) { System.out.println("Exception in zoomFullExtentButton#actionPerformed : " + ex); ex.printStackTrace(); } } } /** * Updates layers drop down */ public void updateLayersDropDown() { dataLayerCombo.removeAllItems(); try { // Add the layer names to combo for (int i = 0; i < mapBean.getLayerCount(); i++) { dataLayerCombo.addItem(mapBean.getLayer(i).getName()); } // Select first layer in control dataLayerCombo.setSelectedIndex(0); } catch (IOException ex) { System.out.println("Exception in updateLayersDropDown : " + ex); ex.printStackTrace(); } } /** * Updates the fields combo in response to the layer selection. */ public void dataLayerComboActionPerformed() { try { // Get Selected Layer int layerIndex = dataLayerCombo.getSelectedIndex(); // Disable field combo if feature layer is not selected and exit FeatureLayer featureLayer = null; try { featureLayer = (FeatureLayer) mapBean.getLayer(layerIndex); } catch (Exception ex) { return; } // Get IFeatureLayer interface dataFieldCombo.removeAllItems(); // Loop through the fields for (int i = 0; i < featureLayer.getFieldCount(); i++) { IField field = featureLayer.getField(i); // If the field is not the shape field if (field.getType() != esriFieldType.esriFieldTypeGeometry) // Add field name to the control dataFieldCombo.addItem(field.getName()); } showLayerTips(); } catch (IOException ex1) { System.out.println("Exception in dataLayerComboActionPerformed: " + ex1); ex1.printStackTrace(); } } /** * sets the display field name of selected feature layer to field name selected in the field combo. */ public void dataFieldComboActionPerformed() { try { // Get the selected index int selectedIndex = dataLayerCombo.getSelectedIndex(); // Get the feature layer FeatureLayer featureLayer = (FeatureLayer) mapBean.getLayer(selectedIndex); // Loop through the fields for (int i = 0; i < featureLayer.getFieldCount(); i++) { // Get IField Interface IField field = featureLayer.getField(i); // If the field name is the name selected in the control if (field.getName().equalsIgnoreCase((String) dataFieldCombo.getSelectedItem())) { // Set the field name as the display field featureLayer.setDisplayField(field.getName()); } } } catch (IOException ex) { System.out.println("Exception in the dataFieldComboActionPerformed : " + ex); ex.printStackTrace(); } } /** * Enables layer tips based the maptips check box value. */ private void showLayerTips() { ILayer layer = null; // Loop through the maps layers try { for (int i = 0; i < mapBean.getLayerCount(); i++) { // Get ILayer interface layer = mapBean.getLayer(i); if (dataLayerCombo.getSelectedIndex() == i) { // If I want to show map tips if (showMapTipsCheckBox.isSelected()) { layer.setShowTips(true); // mapBean.setToolTipText(true); } else layer.setShowTips(false); } else layer.setShowTips(false); } } catch (IOException ex) { System.out.println("Exception in showLayerTips: " + ex); ex.printStackTrace(); } } /** * Method loadFile loads the specified mxd file */ public boolean loadFile() throws IOException { boolean loaded; String devKitHome = null; try { //Get DEVKITHOME Home devKitHome = System.getenv("AGSDEVKITJAVA"); } catch (Error e) { devKitHome = JOptionPane.showInputDialog("Please enter the path to your ArcGIS developer kit directory"); } String fileChosen = devKitHome + File.separator + "java" + File.separator + "samples" + File.separator + "data" + File.separator + "mxds" + File.separator + "usa.mxd"; // check if the selected document can be loaded into MapBean if (mapBean.checkMxFile(fileChosen)) { // load the document in the mapcontrol bean mapBean.loadMxFile(fileChosen, null, null); mapBean.setEnabled(true); loaded = true; } else { JOptionPane.showMessageDialog(null, "The current document does not have permission to be loaded into MapBean."); loaded = false; return loaded; } System.out.println("Document Loaded"); return loaded; } /** * Description: Class which extends map control event class IMapControlEvents2Adapter * * @see com.esri.arcgis.beans.map.IMapControlEvents2Adapter */ class MapControlListener extends IMapControlEvents2Adapter { /** * @see com.esri.arcgis.beans.map.IMapControlEvents2Adapter#onMouseMove(IMapControlEvents2OnMouseMoveEvent * theEvent) * @param theEvent */ public void onMouseMove(IMapControlEvents2OnMouseMoveEvent theEvent) { try { // Exit if not showing tips if (!showMapTipsCheckBox.isSelected()) return; // Get ILayer interface ILayer layer = mapBean.getLayer(dataLayerCombo.getSelectedIndex()); // Set the MapBean tooltiptext to that of the layers maptipsText.setText(" Map Tip : " + layer .getTipText(theEvent.getMapX(), theEvent.getMapY(), mapBean.getExtent().getWidth() / 100)); } catch (Exception ex) { System.out.println("Exception in MapControlListener#onMouseMove : " + ex); ex.printStackTrace(); } } /** * @see com.esri.arcgis.beans.map.IMapControlEvents2Adapter#onMouseDown(IMapControlEvents2OnMouseDownEvent * theEvent) * @param theEvent */ public void onMouseDown(IMapControlEvents2OnMouseDownEvent theEvent) { try { // If left mouse button if (theEvent.getButton() == 1) { // Get IEnvelope interface IEnvelope envelope = null; envelope = mapBean.trackRectangle(); // If user dragged a rectangle if (envelope != null) mapBean.setExtent(envelope); } else if (theEvent.getButton() == 2) mapBean.pan(); } catch (Exception ex) { System.out.println("Exception in MapControlListener#onMouseDown : " + 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()); initializeArcGISLicenses(); ShowMapTips mapTips = new ShowMapTips(); mapTips.setDefaultCloseOperation(ShowMapTips.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(); } } }