arcgissamples\defensesolutions\CoordinateToolMapControl.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.defensesolutions; import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.KeyEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.IOException; import javax.swing.BorderFactory; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; import com.esri.arcgis.carto.IElement; import com.esri.arcgis.carto.IGraphicsContainer; import com.esri.arcgis.carto.IMarkerElement; import com.esri.arcgis.carto.MarkerElement; import com.esri.arcgis.carto.esriViewDrawPhase; 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.IMapControlEvents2; import com.esri.arcgis.controls.IMapControlEvents2OnAfterDrawEvent; import com.esri.arcgis.controls.IMapControlEvents2OnAfterScreenDrawEvent; import com.esri.arcgis.controls.IMapControlEvents2OnBeforeScreenDrawEvent; import com.esri.arcgis.controls.IMapControlEvents2OnDoubleClickEvent; import com.esri.arcgis.controls.IMapControlEvents2OnExtentUpdatedEvent; import com.esri.arcgis.controls.IMapControlEvents2OnFullExtentUpdatedEvent; import com.esri.arcgis.controls.IMapControlEvents2OnKeyDownEvent; import com.esri.arcgis.controls.IMapControlEvents2OnKeyUpEvent; import com.esri.arcgis.controls.IMapControlEvents2OnMapReplacedEvent; import com.esri.arcgis.controls.IMapControlEvents2OnMouseDownEvent; import com.esri.arcgis.controls.IMapControlEvents2OnMouseMoveEvent; import com.esri.arcgis.controls.IMapControlEvents2OnMouseUpEvent; import com.esri.arcgis.controls.IMapControlEvents2OnOleDropEvent; import com.esri.arcgis.controls.IMapControlEvents2OnSelectionChangedEvent; import com.esri.arcgis.controls.IMapControlEvents2OnViewRefreshedEvent; import com.esri.arcgis.controls.ITOCControlEventsOnMouseUpEvent; import com.esri.arcgis.controls.MapControl; import com.esri.arcgis.controls.TOCControl; import com.esri.arcgis.controls.ToolbarControl; import com.esri.arcgis.defensesolutions.CoordinateTool; import com.esri.arcgis.defensesolutions.ICoordinateTool; import com.esri.arcgis.display.IColor; import com.esri.arcgis.display.IMarkerSymbol; import com.esri.arcgis.display.RgbColor; import com.esri.arcgis.display.SimpleMarkerSymbol; import com.esri.arcgis.geometry.IPoint; import com.esri.arcgis.geometry.Point; import com.esri.arcgis.interop.Variant; import com.esri.arcgis.support.ms.stdole.IUnknown; import com.esri.arcgis.support.ms.stdole.IUnknownProxy; import com.esri.arcgis.system.AoInitialize; import com.esri.arcgis.system.EngineInitializer; import com.esri.arcgis.system.esriLicenseProductCode; import com.esri.arcgis.systemUI.esriCommandStyles; /** * <p>Title: Coordinate Tool with MapControl</p> * <p>Description: This sample shows the user how to use the coordinate tool</p> * @version 1.0 */ public class CoordinateToolMapControl extends JFrame implements ActionListener,IMapControlEvents2 { private static final long serialVersionUID = 1L; private JTextField textX ,textY; private MapControl mapControl; private ToolbarControl toolBarControl; private TOCControl tocControl; private JButton refreshButton; private ICoordinateTool cTool,outputCTool; private JButton computeButton; private String[] dms = new String[1]; private String[] utm = new String[1]; private String[] mgrs = new String[1]; private IPoint[] p1 = new IPoint[10]; private IPoint[] p2 = new IPoint[10]; private JTextField odmsText; private JTextField omgrsText; private JTextField outmText; private IPoint pointOnMap; public CoordinateToolMapControl(String arcgishome) { super("Coordinate tool sample with MapControl"); buidGUI(); setSize(700,800); try { String shapePath = arcgishome + java.io.File.separator+"java"+java.io.File.separator+"samples"+java.io.File.separator+"data"+java.io.File.separator+"world"+java.io.File.separator+"Continent.lyr";//"\\java\\samples\\data\\world\\Continent.lyr"; mapControl.addLayerFromFile(shapePath,0); cTool = new CoordinateTool(); outputCTool = new CoordinateTool(); addWindowListener(new WindowAdapter() { //@Override public void windowClosing(WindowEvent e) { try { new AoInitialize().shutdown(); } catch (Exception ex) { // Don't care } e.getWindow().dispose(); } }); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } catch(IOException io) { JOptionPane.showMessageDialog(this,io); } } private void buidGUI(){ JPanel mainPanel = new JPanel(new BorderLayout()); mapControl = new MapControl(); try { mapControl.addIMapControlEvents2Listener(this); } catch (Exception ex) { } toolBarControl = new ToolbarControl(); tocControl = new TOCControl(); JPanel coToolPanel = new JPanel(); coToolPanel.setLayout(new BoxLayout(coToolPanel,BoxLayout.Y_AXIS)); JPanel buttonPanel = new JPanel(); computeButton = new JButton("Compute Values"); refreshButton = new JButton("Refresh"); computeButton.setMnemonic(KeyEvent.VK_C); refreshButton.setMnemonic(KeyEvent.VK_R); refreshButton.addActionListener(this); computeButton.addActionListener(this); buttonPanel.add(computeButton); buttonPanel.add(refreshButton); JPanel inputPanel = new JPanel(new GridLayout(5,2)); inputPanel.setName("Input"); JPanel outputPanel = new JPanel(new GridLayout(3,2)); outputPanel.setName("Output"); JLabel labelX = new JLabel("Input X:"); textX = new JTextField(); JLabel labelY = new JLabel("Input Y:"); textY = new JTextField(); JLabel idatumLabel = new JLabel("Datum(WGS 1984):"); JTextField datumText = new JTextField("WGS 1984"); datumText.setText("WGS 1984"); datumText.setEditable(false); inputPanel.add(labelX); inputPanel.add(textX); inputPanel.add(labelY); inputPanel.add(textY); inputPanel.add(idatumLabel); inputPanel.add(datumText); inputPanel.setBorder( BorderFactory.createCompoundBorder( BorderFactory.createTitledBorder("Enter Value"), BorderFactory.createEmptyBorder(5,5,5,5))); JLabel dmsLabel = new JLabel("DMS:"); JLabel mgrsLabel = new JLabel("MGRS:"); JLabel utmLabel = new JLabel("UTM:"); odmsText = new JTextField(); omgrsText = new JTextField(); outmText = new JTextField(); outputPanel.add(dmsLabel); outputPanel.add(odmsText); outputPanel.add(mgrsLabel); outputPanel.add(omgrsText); outputPanel.add(utmLabel); outputPanel.add(outmText); outputPanel.setBorder( BorderFactory.createCompoundBorder( BorderFactory.createTitledBorder("Output Value"), BorderFactory.createEmptyBorder(5,5,5,5))); coToolPanel.add(inputPanel,BorderLayout.CENTER); coToolPanel.add(outputPanel,BorderLayout.EAST); coToolPanel.add(buttonPanel,BorderLayout.SOUTH); try { toolBarControl.setBuddyControl(mapControl); tocControl.setBuddyControl(mapControl); toolBarControl.addItem(new ControlsOpenDocCommand(), 0, 0, false, 0, esriCommandStyles.esriCommandStyleIconOnly); //open toolBarControl.addItem(new ControlsMapZoomInTool(), 0, -1, true, 0, esriCommandStyles.esriCommandStyleIconAndText); //ZoomIn toolBarControl.addItem(new ControlsMapZoomOutTool(), 0, -1, false, 0, esriCommandStyles.esriCommandStyleIconAndText); //ZoomOut toolBarControl.addItem(new ControlsMapPanTool(), 0, -1, false, 0, esriCommandStyles.esriCommandStyleIconAndText); //Pan toolBarControl.addItem(new ControlsMapFullExtentCommand(), 0, -1, true, 20, esriCommandStyles.esriCommandStyleTextOnly); } catch (Exception ex) { } mainPanel.add(toolBarControl,BorderLayout.NORTH); mainPanel.add(tocControl,BorderLayout.WEST); mainPanel.add(mapControl,BorderLayout.CENTER); mainPanel.add(coToolPanel,BorderLayout.SOUTH); getContentPane().add(mainPanel); } public void itemStateChanged(ItemEvent e) { if (e.getStateChange() == ItemEvent.SELECTED) { try { useCoordinateTool(outputCTool); } catch (Exception ex) { } } } public void useCoordinateTool( ICoordinateTool myTool) { try { IUnknown pUnk = new IUnknownProxy(pointOnMap); //set vValue with Point Variant vValue = new Variant("", Variant.VT_UNKNOWN , pUnk); String sF = "WGS 1984 (WGS84)"; //set vFrom with String value Variant vFrom = new Variant("", Variant.VT_BSTR , sF); //Get the datum string String datum = "WGS 1984 (WGS84)";;//((String)oDatumCbo.getSelectedItem()).trim(); Variant vTo = new Variant("", Variant.VT_BSTR , datum); //pass to the coordinate tool these values set above and //get the dms utm and mgrs values myTool.convertLocation(vValue, 1, vFrom,vTo, p1,p2,dms,utm, mgrs); odmsText.setText(dms[0]); omgrsText.setText(mgrs[0]); outmText.setText(utm[0]); } catch (Exception ex) { ex.printStackTrace(); } } public void actionPerformed(ActionEvent event){ if(event.getSource() == refreshButton) { try { //remove all graphics mapControl.getActiveView().getGraphicsContainer().deleteAllElements(); mapControl.refresh(esriViewDrawPhase.esriViewBackground, null, null); } catch (Exception ex) { System.out.println("Exception in refresh button performed : " + ex); ex.printStackTrace(); } //set all the text boxes to be empty odmsText.setText(""); omgrsText.setText(""); outmText.setText(""); textX.setText(""); textY.setText(""); } else if(event.getSource()== computeButton){ try { if(textX.getText().length()!=0){ useCoordinateTool(cTool); } else{ JOptionPane.showMessageDialog(this,"Please select a point","Error",JOptionPane.ERROR_MESSAGE); } } catch (Exception ex) { ex.printStackTrace(); } } } public void onAfterDraw(IMapControlEvents2OnAfterDrawEvent theEvent) {} public void onAfterScreenDraw(IMapControlEvents2OnAfterScreenDrawEvent theEvent) {} public void onBeforeScreenDraw(IMapControlEvents2OnBeforeScreenDrawEvent theEvent) {} public void onDoubleClick(IMapControlEvents2OnDoubleClickEvent theEvent) {} public void onExtentUpdated(IMapControlEvents2OnExtentUpdatedEvent theEvent) {} public void onFullExtentUpdated(IMapControlEvents2OnFullExtentUpdatedEvent theEvent) {} public void onKeyDown(IMapControlEvents2OnKeyDownEvent theEvent) {} public void onKeyUp(IMapControlEvents2OnKeyUpEvent theEvent) {} public void onMapReplaced(IMapControlEvents2OnMapReplacedEvent theEvent) {} public void onMouseDown(IMapControlEvents2OnMouseDownEvent theEvent) { //Set text boxes with X and Y values textX.setText(java.lang.String.valueOf(theEvent.getMapX())); textY.setText(java.lang.String.valueOf(theEvent.getMapY())); try { //Create point and set its X and Y values pointOnMap = new Point(); pointOnMap.setX(theEvent.getMapX()); pointOnMap.setY(theEvent.getMapY()); pointOnMap.putCoords(theEvent.getMapX(),theEvent.getMapY()); //Create Marker Symbol and Element to add a point to the map control IMarkerElement markerElement = new MarkerElement(); IMarkerSymbol markerSymbol = new SimpleMarkerSymbol(); IColor color = new RgbColor(); color.setRGB(255); markerSymbol.setColor(color); markerSymbol.setSize(10); markerElement.setSymbol(markerSymbol); IGraphicsContainer gCont = mapControl.getActiveView().getGraphicsContainer(); gCont.deleteAllElements(); IElement element = (IElement)markerElement; element.setGeometry(pointOnMap); gCont.addElement(element,0); //add the point to the map and refresh the map mapControl.refresh(esriViewDrawPhase.esriViewGraphics,null,null); } catch (Exception ex) { ex.printStackTrace(); } } public void onMouseMove(IMapControlEvents2OnMouseMoveEvent theEvent) {} public void onMouseUp(IMapControlEvents2OnMouseUpEvent theEvent) {} public void onOleDrop(IMapControlEvents2OnOleDropEvent theEvent) {} public void onSelectionChanged(IMapControlEvents2OnSelectionChangedEvent theEvent) {} public void onViewRefreshed(IMapControlEvents2OnViewRefreshedEvent theEvent) {} public void onMouseUp(ITOCControlEventsOnMouseUpEvent theEvent) {} public static void main(String[] args){ String arcgisHome = null; arcgisHome = System.getenv( "AGSDEVKITJAVA"); if (arcgisHome == null) { JOptionPane.showMessageDialog( null,"AGSDEVKITJAVA has not been set in your system path variable \n e.g. DEVKITHOME94 = c:/programfiles/arcgis","Error",JOptionPane.ERROR_MESSAGE); System.exit(-1); } try { EngineInitializer.initializeVisualBeans(); AoInitialize aoInit = new AoInitialize(); if (aoInit.isProductCodeAvailable( com.esri.arcgis.system.esriLicenseProductCode.esriLicenseProductCodeEngine) == com.esri.arcgis.system.esriLicenseStatus.esriLicenseAvailable ) aoInit.initialize( esriLicenseProductCode.esriLicenseProductCodeEngine); else { JOptionPane.showInputDialog( "Could not initialize an ArcGIS Engine license. Exiting application."); System.exit( -1); } new CoordinateToolMapControl( arcgisHome); } catch(Exception e){ e.printStackTrace();} } }