JAL-2068 add the worker to the currently displayed alignment panel and viewport
[jalview.git] / src / jalview / workers / AlignmentAnnotationFactory.java
1 package jalview.workers;
2
3 import jalview.api.AlignViewportI;
4 import jalview.api.AlignmentViewPanel;
5 import jalview.bin.Jalview;
6 import jalview.datamodel.AlignmentAnnotation;
7 import jalview.datamodel.Annotation;
8 import jalview.gui.AlignFrame;
9
10 import java.awt.Color;
11
12 /**
13  * Factory class with methods which allow clients (including external scripts
14  * such as Groovy) to 'register and forget' an alignment annotation calculator. <br>
15  * Currently supports two flavours of calculator:
16  * <ul>
17  * <li>a simple 'feature counter' which counts any desired score derivable from
18  * residue value and any sequence features at each position of the alignment</li>
19  * <li>a 'general purpose' calculator which computes one or more complete
20  * AlignmentAnnotation objects</li>
21  * </ul>
22  */
23 public class AlignmentAnnotationFactory
24 {
25   /**
26    * Constructs and registers a new alignment annotation worker
27    * 
28    * @param counter
29    *          provider of feature counts per alignment position
30    */
31   public static void newCalculator(FeatureCounterI counter)
32   {
33     // TODO need an interface for AlignFrame by which to access
34     // its AlignViewportI and AlignmentViewPanel
35     AlignmentViewPanel currentAlignFrame = Jalview.getCurrentAlignFrame().alignPanel;
36     if (currentAlignFrame != null)
37     {
38       newCalculator(currentAlignFrame.getAlignViewport(),
39               currentAlignFrame, counter);
40     }
41     else
42     {
43       System.err
44               .println("Can't register calculator as no alignment window has focus");
45     }
46   }
47
48   /**
49    * Constructs and registers a new alignment annotation worker
50    * 
51    * @param viewport
52    * @param panel
53    * @param counter
54    *          provider of feature counts per alignment position
55    */
56   public static void newCalculator(AlignViewportI viewport,
57           AlignmentViewPanel panel, FeatureCounterI counter)
58   {
59     new ColumnCounterWorker(viewport, panel, counter);
60   }
61
62   /**
63    * Constructs and registers a new alignment annotation worker
64    * 
65    * @param calculator
66    *          provider of AlignmentAnnotation for the alignment
67    */
68   public static void newCalculator(AnnotationProviderI calculator)
69   {
70     // TODO need an interface for AlignFrame by which to access
71     // its AlignViewportI and AlignmentViewPanel
72     AlignFrame currentAlignFrame = Jalview.getCurrentAlignFrame() ;
73     if (currentAlignFrame != null)
74     {
75       newCalculator(currentAlignFrame.getViewport(), currentAlignFrame
76               .getAlignPanels().get(0), calculator);
77     }
78     else
79     {
80       System.err
81               .println("Can't register calculator as no alignment window has focus");
82     }
83   }
84
85   /**
86    * Constructs and registers a new alignment annotation worker
87    * 
88    * @param viewport
89    * @param panel
90    * @param calculator
91    *          provider of AlignmentAnnotation for the alignment
92    */
93   public static void newCalculator(AlignViewportI viewport,
94           AlignmentViewPanel panel,
95           AnnotationProviderI calculator)
96   {
97     new AnnotationWorker(viewport, panel, calculator);
98   }
99
100   /**
101    * Factory method to construct an Annotation object
102    * 
103    * @param displayChar
104    * @param desc
105    * @param secondaryStructure
106    * @param val
107    * @param color
108    * @return
109    */
110   public static Annotation newAnnotation(String displayChar, String desc,
111           char secondaryStructure, float val, Color color)
112   {
113     return new Annotation(displayChar, desc, secondaryStructure, val, color);
114   }
115
116   /**
117    * Factory method to construct an AlignmentAnnotation object
118    * 
119    * @param name
120    * @param desc
121    * @param anns
122    * @return
123    */
124   public static AlignmentAnnotation newAlignmentAnnotation(String name,
125           String desc, Annotation[] anns)
126   {
127     return new AlignmentAnnotation(name, desc, anns);
128   }
129 }