Select to view content in your preferred language

ArcMap add-ins JOptionPane

2078
11
08-31-2011 10:01 AM
LeoDonahue
Regular Contributor
ArcMap 10.0 SP2
ArcObjects Java SDK SP2
Eclipse Helios 3.6.2
Java sdk 1.6.0_24

Following this example: http://help.arcgis.com/en/sdk/10.0/java_ao_adf/conceptualhelp/engine/0001/00010000041v000000.htm

I'm finding that using JOptionPane.showMessageDialog's do not always execute right away when you click a tool and sometimes the message dialog opens up behind ArcMap.  And don't click on anything else until you've dismissed the dialog or ArcMap hangs.

Anyone using Java add-ins?  If so, what are you using for notifying the client of certain actions?  JOptionPane messages?

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;
    }
}
11 Replies
SankarSinha
New Contributor

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

ArjunDongre
Occasional Contributor

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!

0 Kudos