JAL-2228 patch for Java 7 compatibility
[jalview.git] / src / jalview / workers / AlignmentAnnotationFactory.java
index 37f3ca5..4e11c70 100644 (file)
@@ -1,20 +1,23 @@
 package jalview.workers;
 
+import jalview.api.AlignViewportI;
+import jalview.api.AlignmentViewPanel;
+import jalview.bin.Jalview;
 import jalview.datamodel.AlignmentAnnotation;
 import jalview.datamodel.Annotation;
 import jalview.gui.AlignFrame;
-import jalview.gui.Desktop;
 
 import java.awt.Color;
+import java.lang.reflect.Method;
 
 /**
  * Factory class with methods which allow clients (including external scripts
  * such as Groovy) to 'register and forget' an alignment annotation calculator. <br>
  * Currently supports two flavours of calculator:
  * <ul>
- * <li>a 'feature counter' which can count any desired property derivable from
+ * <li>a simple 'feature counter' which counts any desired score derivable from
  * residue value and any sequence features at each position of the alignment</li>
- * <li>a 'general purpose' calculator which computes one more complete
+ * <li>a 'general purpose' calculator which computes one or more complete
  * AlignmentAnnotation objects</li>
  * </ul>
  */
@@ -26,11 +29,48 @@ public class AlignmentAnnotationFactory
    * @param counter
    *          provider of feature counts per alignment position
    */
-  public static void newCalculator(FeatureCounterI counter)
+  public static void newCalculator(ColumnCounterI counter)
   {
-    if (Desktop.getCurrentAlignFrame() != null)
+    // TODO need an interface for AlignFrame by which to access
+    // its AlignViewportI and AlignmentViewPanel
+    AlignmentViewPanel currentAlignFrame = Jalview.getCurrentAlignFrame().alignPanel;
+    if (currentAlignFrame != null)
     {
-      newCalculator(Desktop.getCurrentAlignFrame(), counter);
+      Method newCalcMethod = null;
+      try
+      {
+        for (Method m : AlignmentAnnotationFactory.class.getMethods())
+        {
+          if (m.getName().equals("newCalculator"))
+          {
+            if (m.getParameterTypes().length == 3
+                    && m.getParameterTypes()[2].isInstance(counter))
+            {
+              newCalcMethod = m;
+              break;
+            }
+          }
+        }
+      } catch (Exception q)
+      {
+      }
+      if (newCalcMethod == null)
+      {
+
+        System.err
+                .println("Couldn't find a newCalculator method for ColumnCounterI type "
+                        + counter.getClass().getName());
+      }
+      try
+      {
+        newCalcMethod.invoke(null, currentAlignFrame.getAlignViewport(),
+                currentAlignFrame, counter);
+      } catch (Exception ie)
+      {
+        System.err
+                .println("Exception when reporting newCalculator method for ColumnCounterI type "
+                        + counter.getClass().getName());
+      }
     }
     else
     {
@@ -42,14 +82,30 @@ public class AlignmentAnnotationFactory
   /**
    * Constructs and registers a new alignment annotation worker
    * 
-   * @param af
-   *          the AlignFrame for which the annotation is to be calculated
+   * @param viewport
+   * @param panel
+   * @param counter
+   *          provider of feature counts per alignment position
+   */
+  public static void newCalculator(AlignViewportI viewport,
+          AlignmentViewPanel panel, FeatureCounterI counter)
+  {
+    new ColumnCounterWorker(viewport, panel, counter);
+  }
+
+  /**
+   * Constructs and registers a new alignment annotation worker for a set of
+   * column counters
+   * 
+   * @param viewport
+   * @param panel
    * @param counter
    *          provider of feature counts per alignment position
    */
-  public static void newCalculator(AlignFrame af, FeatureCounterI counter)
+  public static void newCalculator(AlignViewportI viewport,
+          AlignmentViewPanel panel, FeatureSetCounterI counter)
   {
-    new ColumnCounterWorker(af, counter);
+    new ColumnCounterSetWorker(viewport, panel, counter);
   }
 
   /**
@@ -60,9 +116,13 @@ public class AlignmentAnnotationFactory
    */
   public static void newCalculator(AnnotationProviderI calculator)
   {
-    if (Desktop.getCurrentAlignFrame() != null)
+    // TODO need an interface for AlignFrame by which to access
+    // its AlignViewportI and AlignmentViewPanel
+    AlignFrame currentAlignFrame = Jalview.getCurrentAlignFrame();
+    if (currentAlignFrame != null)
     {
-      newCalculator(Desktop.getCurrentAlignFrame(), calculator);
+      newCalculator(currentAlignFrame.getViewport(), currentAlignFrame
+              .getAlignPanels().get(0), calculator);
     }
     else
     {
@@ -74,15 +134,15 @@ public class AlignmentAnnotationFactory
   /**
    * Constructs and registers a new alignment annotation worker
    * 
-   * @param af
-   *          the AlignFrame for which the annotation is to be calculated
+   * @param viewport
+   * @param panel
    * @param calculator
    *          provider of AlignmentAnnotation for the alignment
    */
-  public static void newCalculator(AlignFrame af,
-          AnnotationProviderI calculator)
+  public static void newCalculator(AlignViewportI viewport,
+          AlignmentViewPanel panel, AnnotationProviderI calculator)
   {
-    new AnnotationWorker(af, calculator);
+    new AnnotationWorker(viewport, panel, calculator);
   }
 
   /**