JAL-2360 ColourSchemes holds configured schemes, AlignFrame colour menu
[jalview.git] / src / jalview / schemes / ResidueColourScheme.java
index e675a27..9cc4bd1 100755 (executable)
@@ -21,6 +21,7 @@
 package jalview.schemes;
 
 import jalview.analysis.Conservation;
+import jalview.datamodel.AlignmentI;
 import jalview.datamodel.AnnotatedCollectionI;
 import jalview.datamodel.ProfileI;
 import jalview.datamodel.ProfilesI;
@@ -28,7 +29,6 @@ import jalview.datamodel.SequenceCollectionI;
 import jalview.datamodel.SequenceI;
 import jalview.util.ColorUtils;
 import jalview.util.Comparison;
-import jalview.util.MessageManager;
 
 import java.awt.Color;
 import java.util.Map;
@@ -36,12 +36,21 @@ import java.util.Map;
 /**
  * Base class for residue-based colour schemes
  */
-public class ResidueColourScheme implements ColourSchemeI
+public abstract class ResidueColourScheme implements ColourSchemeI
 {
+  public static final String NONE = "None";
+
+  /*
+   * lookup up by character value e.g. 'G' to the colors array index
+   * e.g. if symbolIndex['K'] = 11 then colors[11] is the colour for K
+   */
   final int[] symbolIndex;
 
   boolean conservationColouring = false;
 
+  /*
+   * colour for residue characters as indexed by symbolIndex
+   */
   Color[] colors = null;
 
   int threshold = 0;
@@ -101,6 +110,17 @@ public class ResidueColourScheme implements ColourSchemeI
   }
 
   /**
+   * Returns the colour for symbol 'A'. Intended for use in a 'fixed colour'
+   * colour scheme (for example a feature colour).
+   */
+  @Override
+  public Color findColour()
+  {
+    // TODO delete this method in favour of ColorUtils.parseColourString()?
+    return findColour('A');
+  }
+
+  /**
    * Find a colour without an index in a sequence
    */
   @Override
@@ -326,18 +346,69 @@ public class ResidueColourScheme implements ColourSchemeI
   {
   }
 
+  /**
+   * Answers false if the colour scheme is nucleotide or peptide specific, and
+   * the data does not match, else true. Override to modify or extend this test
+   * as required.
+   */
   @Override
-  public ColourSchemeI applyTo(AnnotatedCollectionI sg,
-          Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
+  public boolean isApplicableTo(AnnotatedCollectionI ac)
   {
-    try
+    if (!isPeptideSpecific() && !isNucleotideSpecific())
+    {
+      return true;
+    }
+
+    /*
+     * inspect the data context (alignment dataset) for residue type
+     */
+    boolean nucleotide = false;
+    AnnotatedCollectionI context = ac.getContext();
+    if (context != null)
+    {
+      if (context instanceof AlignmentI)
+      {
+        nucleotide = ((AlignmentI) context).isNucleotide();
+      }
+      else
+      {
+        // not sure what's going on, play safe
+        return true;
+      }
+    }
+    else if (ac instanceof AlignmentI)
     {
-      return getClass().newInstance();
-    } catch (Exception q)
+      nucleotide = ((AlignmentI) ac).isNucleotide();
+    }
+    else
     {
-      throw new Error(MessageManager.formatMessage(
-              "error.implementation_error_cannot_duplicate_colour_scheme",
-              new String[] { getClass().getName() }), q);
+      return true;
     }
+
+    /*
+     * does data type match colour scheme type?
+     */
+    return (nucleotide && isNucleotideSpecific())
+            || (!nucleotide && isPeptideSpecific());
+  }
+
+  /**
+   * Answers true if the colour scheme is normally only for peptide data
+   * 
+   * @return
+   */
+  public boolean isPeptideSpecific()
+  {
+    return false;
+  }
+
+  /**
+   * Answers true if the colour scheme is normally only for nucleotide data
+   * 
+   * @return
+   */
+  public boolean isNucleotideSpecific()
+  {
+    return false;
   }
 }