JAL-1421 typed map / lists, explicit imports, method refactoring
[jalview.git] / src / jalview / viewmodel / seqfeatures / FeatureRendererModel.java
index bcf808d..848f565 100644 (file)
@@ -1,6 +1,27 @@
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
+ * 
+ * This file is part of Jalview.
+ * 
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License 
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
+ *  
+ * Jalview is distributed in the hope that it will be useful, but 
+ * WITHOUT ANY WARRANTY; without even the implied warranty 
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
+ * PURPOSE.  See the GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
+ */
 package jalview.viewmodel.seqfeatures;
 
 import jalview.api.AlignViewportI;
+import jalview.api.FeatureColourI;
 import jalview.api.FeaturesDisplayedI;
 import jalview.datamodel.AlignmentI;
 import jalview.datamodel.SequenceFeature;
@@ -14,6 +35,7 @@ import java.beans.PropertyChangeListener;
 import java.beans.PropertyChangeSupport;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.HashMap;
 import java.util.Hashtable;
 import java.util.Iterator;
 import java.util.List;
@@ -36,6 +58,9 @@ public abstract class FeatureRendererModel implements
 
   protected Object currentColour;
 
+  /*
+   * feature types in ordering of rendering, where last means on top
+   */
   protected String[] renderOrder;
 
   protected PropertyChangeSupport changeSupport = new PropertyChangeSupport(
@@ -43,6 +68,13 @@ public abstract class FeatureRendererModel implements
 
   protected AlignmentViewport av;
 
+  /*
+   * map holds per feature type, {{min, max}, {min, max}} feature score
+   * values for positional and non-positional features respectively
+   */
+  private Map<String, float[][]> minmax = new Hashtable<String, float[][]>();
+
+  @Override
   public AlignViewportI getViewport()
   {
     return av;
@@ -119,8 +151,7 @@ public abstract class FeatureRendererModel implements
     }
     if (!fdi.isRegistered(featureType))
     {
-      pushFeatureType(Arrays.asList(new String[]
-      { featureType }));
+      pushFeatureType(Arrays.asList(new String[] { featureType }));
     }
     fdi.setVisible(featureType);
   }
@@ -163,15 +194,13 @@ public abstract class FeatureRendererModel implements
     types.toArray(neworder);
     if (renderOrder != null)
     {
-      System.arraycopy(neworder,0,neworder,renderOrder.length,ts);
+      System.arraycopy(neworder, 0, neworder, renderOrder.length, ts);
       System.arraycopy(renderOrder, 0, neworder, 0, renderOrder.length);
     }
     renderOrder = neworder;
   }
 
-  protected Hashtable minmax = new Hashtable();
-
-  public Hashtable getMinMax()
+  public Map<String, float[][]> getMinMax()
   {
     return minmax;
   }
@@ -185,9 +214,8 @@ public abstract class FeatureRendererModel implements
    */
   protected final byte[] normaliseScore(SequenceFeature sequenceFeature)
   {
-    float[] mm = ((float[][]) minmax.get(sequenceFeature.type))[0];
-    final byte[] r = new byte[]
-    { 0, (byte) 255 };
+    float[] mm = minmax.get(sequenceFeature.type)[0];
+    final byte[] r = new byte[] { 0, (byte) 255 };
     if (mm != null)
     {
       if (r[0] != 0 || mm[0] < 0.0)
@@ -247,12 +275,6 @@ public abstract class FeatureRendererModel implements
     ArrayList<SequenceFeature> tmp = new ArrayList<SequenceFeature>();
     SequenceFeature[] features = sequence.getSequenceFeatures();
 
-    while (features == null && sequence.getDatasetSequence() != null)
-    {
-      sequence = sequence.getDatasetSequence();
-      features = sequence.getSequenceFeatures();
-    }
-
     if (features != null)
     {
       for (int i = 0; i < features.length; i++)
@@ -323,15 +345,13 @@ public abstract class FeatureRendererModel implements
     }
     if (minmax == null)
     {
-      minmax = new Hashtable();
+      minmax = new Hashtable<String, float[][]>();
     }
     AlignmentI alignment = av.getAlignment();
     for (int i = 0; i < alignment.getHeight(); i++)
     {
       SequenceI asq = alignment.getSequenceAt(i);
-      SequenceI dasq = asq.getDatasetSequence();
-      SequenceFeature[] features = dasq != null ? dasq
-              .getSequenceFeatures() : asq.getSequenceFeatures();
+      SequenceFeature[] features = asq.getSequenceFeatures();
 
       if (features == null)
       {
@@ -377,20 +397,19 @@ public abstract class FeatureRendererModel implements
         {
           allfeatures.add(features[index].getType());
         }
-        if (features[index].score != Float.NaN)
+        if (!Float.isNaN(features[index].score))
         {
           int nonpos = features[index].getBegin() >= 1 ? 0 : 1;
-          float[][] mm = (float[][]) minmax.get(features[index].getType());
+          float[][] mm = minmax.get(features[index].getType());
           if (mm == null)
           {
-            mm = new float[][]
-            { null, null };
+            mm = new float[][] { null, null };
             minmax.put(features[index].getType(), mm);
           }
           if (mm[nonpos] == null)
           {
-            mm[nonpos] = new float[]
-            { features[index].score, features[index].score };
+            mm[nonpos] = new float[] { features[index].score,
+                features[index].score };
 
           }
           else
@@ -441,8 +460,7 @@ public abstract class FeatureRendererModel implements
         {
           if (initOrders)
           {
-            setOrder(oldRender[j], (1 - (1 + (float) j)
-                    / oldRender.length));
+            setOrder(oldRender[j], (1 - (1 + (float) j) / oldRender.length));
           }
           if (allfeatures.contains(oldRender[j]))
           {
@@ -529,6 +547,7 @@ public abstract class FeatureRendererModel implements
    * @param featureType
    * @return java.awt.Color or GraduatedColor
    */
+  @Override
   public Object getFeatureStyle(String featureType)
   {
     Object fc = featureColours.get(featureType);
@@ -609,6 +628,7 @@ public abstract class FeatureRendererModel implements
     return av.getFeaturesDisplayed().isVisible(type);
   }
 
+  @Override
   public void setColour(String featureType, Object col)
   {
     // overwrite
@@ -617,9 +637,18 @@ public abstract class FeatureRendererModel implements
     // Object c = featureColours.get(featureType);
     // if (c == null || c instanceof Color || (c instanceof GraduatedColor &&
     // !((GraduatedColor)c).getMaxColor().equals(_col)))
+    if (col instanceof FeatureColourI)
     {
-      featureColours.put(featureType, col);
+      if (((FeatureColourI) col).isGraduatedColour())
+      {
+        col = new GraduatedColor((FeatureColourI) col);
+      }
+      else
+      {
+        col = ((FeatureColourI) col).getColour();
+      }
     }
+      featureColours.put(featureType, col);
   }
 
   public void setTransparency(float value)
@@ -675,7 +704,7 @@ public abstract class FeatureRendererModel implements
   @Override
   public Map<String, Object> getFeatureColours()
   {
-    return new ConcurrentHashMap<String, Object>(featureColours);
+    return featureColours;
   }
 
   /**
@@ -683,21 +712,32 @@ public abstract class FeatureRendererModel implements
    * 
    * @param data
    *          { String(Type), Colour(Type), Boolean(Displayed) }
+   * @return true if any visible features have been reordered, else false
    */
-  public void setFeaturePriority(Object[][] data)
+  public boolean setFeaturePriority(Object[][] data)
   {
-    setFeaturePriority(data, true);
+    return setFeaturePriority(data, true);
   }
 
   /**
+   * Sets the priority order for features
    * 
    * @param data
    *          { String(Type), Colour(Type), Boolean(Displayed) }
    * @param visibleNew
    *          when true current featureDisplay list will be cleared
+   * @return true if any visible features have been reordered or recoloured,
+   *         else false (i.e. no need to repaint)
    */
-  public void setFeaturePriority(Object[][] data, boolean visibleNew)
+  public boolean setFeaturePriority(Object[][] data, boolean visibleNew)
   {
+    /*
+     * note visible feature ordering and colours before update
+     */
+    List<String> visibleFeatures = getDisplayedFeatureTypes();
+    Map<String, Object> visibleColours = new HashMap<String, Object>(
+            getFeatureColours());
+
     FeaturesDisplayedI av_featuresdisplayed = null;
     if (visibleNew)
     {
@@ -716,10 +756,10 @@ public abstract class FeatureRendererModel implements
     }
     if (data == null)
     {
-      return;
+      return false;
     }
     // The feature table will display high priority
-    // features at the top, but theses are the ones
+    // features at the top, but these are the ones
     // we need to render last, so invert the data
     renderOrder = new String[data.length];
 
@@ -739,6 +779,30 @@ public abstract class FeatureRendererModel implements
       }
     }
 
+    /*
+     * get the new visible ordering and return true if it has changed
+     * order or any colour has changed
+     */
+    List<String> reorderedVisibleFeatures = getDisplayedFeatureTypes();
+    if (!visibleFeatures.equals(reorderedVisibleFeatures))
+    {
+      /*
+       * the list of ordered visible features has changed
+       */
+      return true;
+    }
+
+    /*
+     * return true if any feature colour has changed
+     */
+    for (String feature : visibleFeatures)
+    {
+      if (visibleColours.get(feature) != getFeatureStyle(feature))
+      {
+        return true;
+      }
+    }
+    return false;
   }
 
   /**
@@ -759,7 +823,7 @@ public abstract class FeatureRendererModel implements
     changeSupport.removePropertyChangeListener(listener);
   }
 
-  public Set getAllFeatureColours()
+  public Set<String> getAllFeatureColours()
   {
     return featureColours.keySet();
   }
@@ -778,8 +842,7 @@ public abstract class FeatureRendererModel implements
   {
     if (renderOrder == null)
     {
-      return Arrays.asList(new String[]
-      {});
+      return Arrays.asList(new String[] {});
     }
     return Arrays.asList(renderOrder);
   }
@@ -895,39 +958,39 @@ public abstract class FeatureRendererModel implements
     return av.getFeaturesDisplayed();
   }
 
+  /**
+   * Returns a (possibly empty) list of visible feature types, in render order
+   * (last is on top)
+   */
   @Override
-  public String[] getDisplayedFeatureTypes()
+  public List<String> getDisplayedFeatureTypes()
   {
-    String[] typ = null;
-    typ = getRenderOrder().toArray(new String[0]);
+    List<String> typ = getRenderOrder();
+    List<String> displayed = new ArrayList<String>();
     FeaturesDisplayedI feature_disp = av.getFeaturesDisplayed();
     if (feature_disp != null)
     {
       synchronized (feature_disp)
       {
-        for (int i = 0; i < typ.length; i++)
+        for (String type : typ)
         {
-          if (feature_disp.isVisible(typ[i]))
+          if (feature_disp.isVisible(type))
           {
-            typ[i] = null;
+            displayed.add(type);
           }
         }
       }
     }
-    return typ;
+    return displayed;
   }
 
   @Override
-  public String[] getDisplayedFeatureGroups()
+  public List<String> getDisplayedFeatureGroups()
   {
-    String[] gps = null;
-    ArrayList<String> _gps = new ArrayList<String>();
-    Iterator en = getFeatureGroups().iterator();
-    int g = 0;
+    List<String> _gps = new ArrayList<String>();
     boolean valid = false;
-    while (en.hasNext())
+    for (String gp : getFeatureGroups())
     {
-      String gp = (String) en.next();
       if (checkGroupVisibility(gp, false))
       {
         valid = true;
@@ -939,11 +1002,11 @@ public abstract class FeatureRendererModel implements
       }
       else
       {
-        gps = new String[_gps.size()];
-        _gps.toArray(gps);
+        // gps = new String[_gps.size()];
+        // _gps.toArray(gps);
       }
     }
-    return gps;
+    return _gps;
   }
 
 }