120 likes | 213 Views
Arc: Communications between Addins. Dr Andy Evans. Communication between addins. There are various ways of getting hold of other addins, built into the system. However, these are addin specific. Eg. IDockableWindowManager dwm = new IDockableWindowManagerProxy(app); UID uid = new UID();
E N D
Arc: Communications between Addins Dr Andy Evans
Communication between addins There are various ways of getting hold of other addins, built into the system. However, these are addin specific. Eg. IDockableWindowManager dwm = new IDockableWindowManagerProxy(app); UID uid = new UID(); uid.setValue(uk.ac.leeds.geog.geog5790.OurWindow); IDockableWindow tableWin = dwm.getDockableWindow(uid);
More generic method Every addin is held as a static variable within ArcGIS. That is, there is only one copy of it. We could get hold of this, if only we had a method to do so. To understand how we can build such a method, we need to understand Singletons.
Singletons Singletons are both a class and a static variable. Because they are static, there is only ever one copy of them. However, they are not troubled by the problems of containing static code, as they are also perfectly normal classes. How is this amazing trick done?
Simple Singleton class Singleton { static Singleton single = null; static Singleton getInstance() { if (single = null) { single = new Singleton(); } return single; } // other methods. }
Use Note that as getInstance is static, we can call it using the class: Singleton s = Singleton.getInstance(); But it returns to us the class as an object we can use: s.whateverMethodInSingletonWeWant(); But the object is static, so if we call getInstance somewhere else, we get exactly the same object, including any changes we’ve made to it in other code.
Simple Singleton However, we want to make sure no one does this: Singleton s = new Singleton(); Let alone this: s.single = someOtherSingleton; So the constructor (unusually) and the variable are set to private, so no one outside the Singleton class can use them. We must include the empty (or otherwise) constructor, to force it to be private.
Simple Singleton class Singleton { private static Singleton single = null; private Singleton() {} public static Singleton getInstance() { if (single = null) { single = new Singleton(); } return single; } // other methods. } Here the constructor is called from within the class, so it works fine, even though the constructor is private.
Uses Wherever you need one specific version of something, e.g. for storage, that everything else can get at. Wherever you need to communicate between different code running on the JVM. e.g. between Applets running in different webpage frames. Note, however, that which can see it will depend on how the JVM classloader is activated.
AddIns As addins are static variables in Arc, if we build them to be Singletons, we can use the Class’ getInstance() method to get hold of them in other code. Note, however, that as Arc is making the static variable, from the class, we shouldn’t. We don’t need to call the constructor. Arc, however, does need access to it, so it must be public.
AddIn Singleton class AddIn{ private static AddInaddIn = null; public AddIn() { addIn = this; // Grab our static } // variable as Arc makes it. public static AddIngetInstance() { return addIn; } // other methods. } Note the use of “this” to get the object we are currently inside.
Use Again, then, we can: AddIn a = AddIn.getInstance(); a.whateverMethodInSingletonWeWant();