Revision: 16864
          http://sourceforge.net/p/gate/code/16864
Author:   markagreenwood
Date:     2013-09-09 12:50:13 +0000 (Mon, 09 Sep 2013)
Log Message:
-----------
added a new method to the ResourceHelper to make it easy to call methods 
defined by helpers from embedded code as well as having easy access through the 
GUI

Modified Paths:
--------------
    gate/trunk/src/gate/gui/ResourceHelper.java

Modified: gate/trunk/src/gate/gui/ResourceHelper.java
===================================================================
--- gate/trunk/src/gate/gui/ResourceHelper.java 2013-09-09 12:07:52 UTC (rev 
16863)
+++ gate/trunk/src/gate/gui/ResourceHelper.java 2013-09-09 12:50:13 UTC (rev 
16864)
@@ -20,6 +20,8 @@
 import gate.event.CreoleEvent;
 import gate.event.CreoleListener;
 
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -27,7 +29,7 @@
 import javax.swing.Action;
 
 public abstract class ResourceHelper extends AbstractResource implements
-                                                             CreoleListener {
+  CreoleListener {
 
   private static final long serialVersionUID = 1657147709821774423L;
 
@@ -54,8 +56,8 @@
    * {@link #buildActions(NameBearerHandle)}.
    * 
    * @param handle
-   *          the {@link gate.gui.NameBearerHandle} instance we are wanting to 
add new menu
-   *          items to
+   *          the {@link gate.gui.NameBearerHandle} instance we are wanting to
+   *          add new menu items to
    * @return a list of {@link javax.swing.Action} instances which will be added
    *         to the right click menu of the specified handle
    */
@@ -72,7 +74,8 @@
 
   /**
    * Build the {@link javax.swing.Action} instances that should be used to
-   * enhance the right-click menu of the specified {@link 
gate.gui.NameBearerHandle}.
+   * enhance the right-click menu of the specified
+   * {@link gate.gui.NameBearerHandle}.
    * 
    * @param handle
    *          the {@link gate.gui.NameBearerHandle} instance we are adding to
@@ -81,6 +84,62 @@
    */
   protected abstract List<Action> buildActions(NameBearerHandle handle);
 
+  /**
+   * Allows for the calling of methods defined within ResourceHelper instances
+   * which aren't part of the core API and so which can only be called via
+   * reflection.
+   * 
+   * @param action
+   *          the name of the method to call (method must take a Resource
+   *          instance as it's first parameter)
+   * @param resource
+   *          the Resource instance that you want to help
+   * @param params
+   *          parameters to the method you are trying to call
+   * @return the return value from the method you are calling, or null if the
+   *         method has a void return type
+   */
+  public Object call(String action, Resource resource, Object... params)
+    throws NoSuchMethodException, IllegalArgumentException,
+    IllegalAccessException, InvocationTargetException {
+
+    // get all the methods defined for this instance of the helper
+    Method[] methods = this.getClass().getMethods();
+
+    for(Method method : methods) {
+      // for each method....
+
+      // if the method name doesn't match then skip onto the next method
+      if(!method.getName().equals(action)) continue;
+
+      // get the types of the methods params
+      Class<?>[] paramTypes = method.getParameterTypes();
+
+      // if the method doesn't have the right number of params then skip to the
+      // next method
+      if(paramTypes.length != params.length + 1) continue;
+
+      //check the param types and skip to the next method if they aren't 
compatible
+      if(!paramTypes[0].isAssignableFrom((resource.getClass()))) continue;
+      for(int i = 0; i < params.length; ++i) {
+        if(!paramTypes[i + 1].isAssignableFrom(params[i].getClass())) continue;
+      }
+
+      //if we got to here then we have found a method we can call so....
+      
+      //copy the params into a single array
+      Object[] parameters = new Object[params.length + 1];
+      parameters[0] = resource;
+      System.arraycopy(params, 0, parameters, 1, params.length);
+      
+      //and finally call the method
+      return method.invoke(this, parameters);
+
+    }
+
+    throw new NoSuchMethodException("we can't find what you are looking for");
+  }
+
   @Override
   public void cleanup() {
     // do the normal cleanup

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.


------------------------------------------------------------------------------
Learn the latest--Visual Studio 2012, SharePoint 2013, SQL 2012, more!
Discover the easy way to master current and previous Microsoft technologies
and advance your career. Get an incredible 1,500+ hours of step-by-step
tutorial videos with LearnDevNow. Subscribe today and save!
http://pubads.g.doubleclick.net/gampad/clk?id=58041391&iu=/4140/ostg.clktrk
_______________________________________________
GATE-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/gate-cvs

Reply via email to