import java.awt.event.MouseEvent; import java.io.IOException; import java.net.UnknownHostException; import javax.swing.JOptionPane; import com.esri.arcgis.addins.desktop.Tool; import com.esri.arcgis.arcmapui.IMxDocument; import com.esri.arcgis.carto.IActiveView; import com.esri.arcgis.display.IDisplayTransformation; import com.esri.arcgis.display.IScreenDisplay; import com.esri.arcgis.framework.IApplication; import com.esri.arcgis.geometry.IPoint; import com.esri.arcgis.geometry.ISpatialReference; import com.esri.arcgis.geometry.Point; import com.esri.arcgis.geometry.SpatialReferenceEnvironment; import com.esri.arcgis.interop.AutomationException; public class HelloTool extends Tool { /** * Called when the tool is activated by clicking it. * * @exception java.io.IOException if there are interop problems. * @exception com.esri.arcgis.interop.AutomationException if the component throws an ArcObjects exception. */ @Override public void activate() throws IOException, AutomationException { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent mouseEvent) { // TODO Auto-generated method stub super.mousePressed(mouseEvent); try { Point point1 = new Point(); point1.putCoords((double)mouseEvent.getX(), (double)mouseEvent.getY()); Point point2 = (Point) getMapCoordinatesFromScreenCoordinates(point1,av); String message2 = "x: " + point2.getX() + " y: " + point2.getY(); Point point3 = (Point) getScreenCoordinatesFromMapCoorindates(point1,av); String message3 = "x: " + point3.getX() + " y: " + point3.getY(); JOptionPane.showMessageDialog(null, message2, "Map Coordinates from Screen Coordinates", JOptionPane.INFORMATION_MESSAGE); JOptionPane.showMessageDialog(null, message3, "Screen Coordinates from Map Coordinates", JOptionPane.INFORMATION_MESSAGE); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } private IApplication app; private String appName; private IActiveView av; private int mapUnits = com.esri.arcgis.system.esriUnits.esriFeet; @Override public void init(IApplication app){ //Stuff in here runs right away, even if the map document doesn't have the add-in loaded. this.app = app; try { IMxDocument mxDocument = (IMxDocument)app.getDocument(); appName = app.getDocument().getTitle(); av = mxDocument.getActiveView(); } catch (AutomationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public IPoint getMapCoordinatesFromScreenCoordinates(IPoint screenPoint, IActiveView activeView)throws Exception{ if (screenPoint == null || screenPoint.isEmpty() || activeView == null) return null; int nad83HarnAZCentral = com.esri.arcgis.geometry.esriSRProjCSType.esriSRProjCS_NAD1983HARNSPCS_AZCentral; int mapUnits = com.esri.arcgis.system.esriUnits.esriFeet; SpatialReferenceEnvironment spatRef = new SpatialReferenceEnvironment(); ISpatialReference nad83HarnAZC = spatRef.createProjectedCoordinateSystem(nad83HarnAZCentral); IScreenDisplay screenDisplay = activeView.getScreenDisplay(); IDisplayTransformation displayTransformation = screenDisplay.getDisplayTransformation(); displayTransformation.setSpatialReferenceByRef(nad83HarnAZC); displayTransformation.setUnits(mapUnits); return displayTransformation.toMapPoint((int)screenPoint.getX(), (int) screenPoint.getY()); } public IPoint getScreenCoordinatesFromMapCoorindates(IPoint mapPoint, IActiveView activeView)throws Exception{ if (mapPoint == null || mapPoint.isEmpty() || activeView == null) return null; int nad83HarnAZCentral = com.esri.arcgis.geometry.esriSRProjCSType.esriSRProjCS_NAD1983HARNSPCS_AZCentral; int mapUnits = com.esri.arcgis.system.esriUnits.esriFeet; SpatialReferenceEnvironment spatRef = new SpatialReferenceEnvironment(); ISpatialReference nad83HarnAZC = spatRef.createProjectedCoordinateSystem(nad83HarnAZCentral); IScreenDisplay screenDisplay = activeView.getScreenDisplay(); IDisplayTransformation displayTransformation = screenDisplay.getDisplayTransformation(); displayTransformation.setSpatialReferenceByRef(nad83HarnAZC); displayTransformation.setUnits(mapUnits); int x[] = new int[1]; int y[] = new int[1]; displayTransformation.fromMapPoint(mapPoint, x, y); IPoint returnPoint = new Point(); returnPoint.putCoords(x[0], y[0]); return returnPoint; } }
I think we have to use SwingUtilities.invokelater in this type of situations. An example code is as below :-
public void mousePressed(MouseEvent mouseEvent) {
try {
// your other code
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
JOptionPane.showMessageDialog(null, message2, "Map Coordinates from Screen Coordinates", JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(null, message3, "Screen Coordinates from Map Coordi
} catch (Exception ex) {}
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
-Sankar
Okay, this fixed the problem of ArcMap stalling if the MXD window is clicked while the JOptionPane is visible. But what about making sure that the JOptionPane stays in front of the current ArcMap Session/MXD?
Thank you so much for the help!