Code showing how to communicate between addins
Details
Code showing how to communicate between two addins, using a DockableWindow and a Button.
Original author/s: ESRI
Original location/s: Adapted from C# code at:
Add-in coding patterns [for .Net]
Adapted by: Andy Evans
License: none
Other info: The .Net site above suggests various ways of communicating using the .Net API, but some of them don't appear to be implemented in
Java. For example, the AddIn
class exists, as part of an undocumented and unsourced package com.esri.arcgis.addinframework
,
but it doesn't seem to include the appropriate static method for gaining objects by ID. It's possible you can grab this at a lower level (or higher for some objects - there
are methods for extensions, for examples), but as addIns appear to be set up as static objects inside ArcDesktop, the easiest way to communicate between them is to set up one of them with
the methods to turn it into a singleton. To see the difference, compare the below with the
version without communication.
DockableWindow: Imports and instance variables
import com.esri.arcgis.addins.desktop.DockableWindow; import javax.swing.*; import com.esri.arcgis.framework.IApplication;
DockableWindow: Code
public class OurWindowClass extends DockableWindow { private static OurWindowClass ow = null; private IApplication app = null; private JLabel jLabel = null; public void init(IApplication app) { this.app = app; } /** * Note that unlike a usual singleton the constructor needs to * be public for ArcGIS to work its addIn magic; we therefore need * to make sure the static variable is set to whatever Arc creates. **/ public RiskOutput() { ow = this; } /** * As Arc will already have called the constructor, we just need... **/ public static OurWindowClass getInstance() { return ow; } public void helloWorld() { jLabel.setText("Hello World"); } @Override public Component createUI() { JPanel jPanel = new JPanel(); jLabel = new JLabel(" "); jPanel.add(jLabel); return jPanel; } }
Button: Imports and instance variables
import com.esri.arcgis.framework.IApplication; import com.esri.arcgis.framework.IDockableWindow; import com.esri.arcgis.framework.IDockableWindowManager; import com.esri.arcgis.framework.IDockableWindowManagerProxy;
Button: Code
public class OurButton extends Button { private IApplication app = null; private IDockableWindow docWin = null; @Override public void onClick() throws IOException, AutomationException { if (docWin != null) { // Here we get a copy of our object. As it is static, we // can pretty much guarentee it will be the one copy of it // on the system. OurWindow ow = OurWindow.getInstance(); ow.helloWorld(); // We can (and should) still use the com.esri.arcgis.framework.DockableWindow // for doing the following kinds of jobs, as OurWindow doesn't inherit // com.esri.arcgis.framework.DockableWindow and so doesn't have these methods (see // also below). docWin.show(true); } } public void init (IApplication app) { this.app = app; try{ // We carry on as with non-communicating versions, getting the com.esri.arcgis.framework.DockableWindow - remember // this isn't the object we want, but a wrapper for it. The com.esri.arcgis.framework.DockableWindow we can use // for lots of jobs, but not for running methods inside our specific class. IDockableWindowManager dwm = new IDockableWindowManagerProxy(app); UID uid = new UID(); uid.setValue("uk.ac.leeds.geog.pgiaas.OurWindowClass"); docWin = dwm.getDockableWindow(uid); docWin.show(false); } catch (Exception e){ JOptionPane.showMessageDialog(null, e.getMessage()); } } }