arcgissamples\toolbarbean\SelectFeaturesInMap.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.Dimension; import java.awt.GridLayout; import java.io.IOException; import java.text.DecimalFormat; import javax.swing.BorderFactory; 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.beans.toolbar.ToolbarBean; import com.esri.arcgis.carto.IActiveViewEventsAdapter; import com.esri.arcgis.carto.IActiveViewEventsSpatialReferenceChangedEvent; import com.esri.arcgis.carto.Map; import com.esri.arcgis.controls.ControlsClearSelectionCommand; 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.ControlsSelectFeaturesTool; import com.esri.arcgis.controls.IMapControlEvents2Adapter; import com.esri.arcgis.controls.IMapControlEvents2OnMouseMoveEvent; import com.esri.arcgis.controls.IToolbarControlEventsAdapter; import com.esri.arcgis.controls.IToolbarControlEventsOnMouseMoveEvent; import com.esri.arcgis.controls.IToolbarItem; 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.system.esriUnits; import com.esri.arcgis.systemUI.esriCommandStyles; /** * This sample demonstrates using the ToolBarControl and MapBean in conjunction with default ToolbarBean commands. The * sample demonstrates displaying text in the StatusBar control by setting each Pane's text property. The HitTest method * is used within the IToolbarControlEvents::OnMouseMove event to determine whether the mouse pointer is over an Item. * If the mouse is over an IToolbarItem, the text of the first StatusBar pane is set to the Message of the * IToolbarItem::Command. */ public class SelectFeaturesInMap extends JFrame { JPanel mainPanel = null; JPanel bottomPanel = null; JLabel statusLabel1 = null; JLabel statusLabel2 = null; MapBean mapBean = null; ToolbarBean toolbarBean = null; String mapUnits = ""; public SelectFeaturesInMap() { super("Select Features in Map"); buildFrame(); setSize(600, 500); setVisible(true); initControl(); } public void buildFrame() { bottomPanel = new JPanel(); bottomPanel.setLayout(new GridLayout(1, 2)); statusLabel1 = new JLabel(); statusLabel1.setPreferredSize(new Dimension(300, 20)); statusLabel2 = new JLabel(); statusLabel2.setPreferredSize(new Dimension(190, 20)); bottomPanel.add(statusLabel1); bottomPanel.add(statusLabel2); mainPanel = new JPanel(); mainPanel.setLayout(new BorderLayout()); mapBean = new MapBean(); toolbarBean = new ToolbarBean(); toolbarBean.setSize(490, 20); mainPanel.add(toolbarBean, BorderLayout.NORTH); mainPanel.add(mapBean, BorderLayout.CENTER); mainPanel.add(bottomPanel, BorderLayout.SOUTH); mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); getContentPane().add(mainPanel, BorderLayout.CENTER); } /** * Initilizes control **/ public void initControl() { try { // Set the Buddy toolbarBean.setBuddyControl(mapBean); // Add tool bar items.. toolbarBean.addItem(new ControlsOpenDocCommand(), 0, 0, false, 0, esriCommandStyles.esriCommandStyleIconOnly); // open toolbarBean.addItem(new ControlsMapZoomInTool(), 0, -1, true, 0, esriCommandStyles.esriCommandStyleIconAndText); // ZoomIn toolbarBean.addItem(new ControlsMapZoomOutTool(), 0, -1, false, 0, esriCommandStyles.esriCommandStyleIconAndText); // ZoomOut toolbarBean.addItem(new ControlsMapPanTool(), 0, -1, false, 0, esriCommandStyles.esriCommandStyleIconAndText); // Pan toolbarBean.addItem(new ControlsMapFullExtentCommand(), 0, -1, true, 20, esriCommandStyles.esriCommandStyleIconAndText); // FullExtent toolbarBean.addItem(new ControlsSelectFeaturesTool(), 0, -1, true, 0, esriCommandStyles.esriCommandStyleIconAndText); // SelectFeatures toolbarBean.addItem(new ControlsClearSelectionCommand(), 0, -1, false, 0, esriCommandStyles.esriCommandStyleIconAndText); // ClearSelections // Add mapcontrol lisetener mapBean.addIMapControlEvents2Listener(new MapControlListener()); // Add toobar control listener toolbarBean.addIToolbarControlEventsListener(new ToobarControlListener()); // Add active view listener Map map = (Map) mapBean.getMap(); map.addIActiveViewEventsListener(new ActiveViewEventListener()); } catch (IOException ex) { System.out.println("Exception in initControl : " + ex); ex.printStackTrace(); } } /** * 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 { // Set statusbar text DecimalFormat format = new DecimalFormat(".00"); statusLabel2.setText(format.format(theEvent.getMapX()) + " " + format.format(theEvent.getMapY()) + " " + mapUnits); } catch (Exception ex) { System.out.println("Exception in MapControlListener#onMouseMove : " + ex); ex.printStackTrace(); } } } /** * Description: Class which extends event class IToolbarControlEvents * * @see com.esri.arcgis.beans.toolbar.IToolbarControlEventsAdapter */ class ToobarControlListener extends IToolbarControlEventsAdapter { /** * @see com.esri.arcgis.beans.toolbar.IToolbarControlEventsAdapter#onMouseMove(IToolbarControlEventsOnMouseMoveEvent * theEvent) * @param theEvent */ public void onMouseMove(IToolbarControlEventsOnMouseMoveEvent theEvent) { try { // Determine if the mouse is over an item long index = toolbarBean.hitTest(theEvent.getX(), theEvent.getY(), false); if (index != -1) { // Get a reference to the ToolbarItem interface IToolbarItem toolbarItem = toolbarBean.getItem((int) index); // Set statusbar text statusLabel1.setText(toolbarItem.getCommand().getMessage()); } else statusLabel1.setText(""); } catch (Exception ex) { System.out.println("Exception in ToobarControlListener#onMouseMove : " + ex); ex.printStackTrace(); } } }// End of toolbarcontrollistener class /** * Description: Class which extends event class IActiveViewEventsAdapter * * @see com.esri.arcgis.carto.IActiveViewEvents */ class ActiveViewEventListener extends IActiveViewEventsAdapter { /** * @see com.esri.arcgis.carto.IActiveViewEventsAdapter#spatialReferenceChanged(IActiveViewEventsSpatialReferenceChangedEvent * theEvent) * @param theEvent */ public void spatialReferenceChanged(IActiveViewEventsSpatialReferenceChangedEvent theEvent) { try { // Determine the new map units int units = mapBean.getMapUnits(); if (units == esriUnits.esriCentimeters) mapUnits = "Centimeters"; else if (units == esriUnits.esriDecimalDegrees) mapUnits = "Decimal Degrees"; else if (units == esriUnits.esriDecimeters) mapUnits = "Decimeters"; else if (units == esriUnits.esriFeet) mapUnits = "Feet"; else if (units == esriUnits.esriInches) mapUnits = "Inches"; else if (units == esriUnits.esriKilometers) mapUnits = "Kilometers"; else if (units == esriUnits.esriMeters) mapUnits = "Meters"; else if (units == esriUnits.esriMiles) mapUnits = "Miles"; else if (units == esriUnits.esriMillimeters) mapUnits = "Millimeters"; else if (units == esriUnits.esriNauticalMiles) mapUnits = "NauticalMiles"; else if (units == esriUnits.esriPoints) mapUnits = "Points"; else if (units == esriUnits.esriUnknownUnits) mapUnits = "Unknown"; else if (units == esriUnits.esriYards) mapUnits = "Yards"; } catch (Exception ex) { System.out.println("Exception in ActiveViewEventListener#spatialReferenceChanged : " + ex); ex.printStackTrace(); } } }// End of ActiveViewEventListener 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(); // Create the drawtext object. SelectFeaturesInMap statusBar = new SelectFeaturesInMap(); statusBar.setDefaultCloseOperation(SelectFeaturesInMap.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();} } }