added feature gradient colours and sortby feature score/density to feature settings...
[jalview.git] / src / jalview / appletgui / FeatureSettings.java
index 4649786..f71159d 100755 (executable)
@@ -23,6 +23,8 @@ import java.util.*;
 import java.awt.*;
 import java.awt.event.*;
 
+import jalview.analysis.AlignmentSorter;
+import jalview.commands.OrderCommand;
 import jalview.datamodel.*;
 
 public class FeatureSettings extends Panel implements ItemListener,
@@ -152,6 +154,78 @@ public class FeatureSettings extends Panel implements ItemListener,
     g.drawString("(Features can be added from searches or", 10, 40);
     g.drawString("from Jalview / GFF features files)", 10, 60);
   }
+  protected void popupSort(final String type, final Hashtable minmax,
+          int x, int y)
+  {
+    java.awt.PopupMenu men = new PopupMenu("Settings for " + type);
+    java.awt.MenuItem scr = new MenuItem("Sort by Score");
+    men.add(scr);
+    final FeatureSettings me = this;
+    scr.addActionListener(new ActionListener()
+    {
+
+      public void actionPerformed(ActionEvent e)
+      {
+        me.sortByScore(new String[]
+        { type });
+      }
+
+    });
+    MenuItem dens = new MenuItem("Sort by Density");
+    dens.addActionListener(new ActionListener()
+    {
+
+      public void actionPerformed(ActionEvent e)
+      {
+        me.sortByDens(new String[]
+        { type });
+      }
+
+    });
+    men.add(dens);
+    if (minmax != null)
+    {
+      final Object typeMinMax = minmax.get(type);
+      final java.awt.CheckboxMenuItem chb = new java.awt.CheckboxMenuItem("Vary Height");
+      // this is broken at the moment
+      chb.setState(minmax.get(type) != null);
+      chb.addActionListener(new ActionListener()
+      {
+
+        public void actionPerformed(ActionEvent e)
+        {
+          chb.setState(chb.getState());
+          if (chb.getState())
+          {
+            minmax.put(type, null);
+          }
+          else
+          {
+            minmax.put(type, typeMinMax);
+          }
+        }
+
+      });
+      men.add(chb);
+      if (typeMinMax != null && ((float[][]) typeMinMax)[0] != null)
+      {
+        // graduated colourschemes for those where minmax exists for the positional features
+        MenuItem mxcol = new MenuItem("Min Max Colour");
+        men.add(mxcol);
+        mxcol.addActionListener(new ActionListener()
+        {
+
+          public void actionPerformed(ActionEvent e)
+          {
+            new FeatureColourChooser(me, type);
+          }
+
+        });
+      }
+    }
+    this.featurePanel.add(men);
+    men.show(this.featurePanel, x, y);
+  }
 
   public void setTableData()
   {
@@ -502,7 +576,10 @@ public class FeatureSettings extends Panel implements ItemListener,
   public void mouseClicked(MouseEvent evt)
   {
     MyCheckbox check = (MyCheckbox) evt.getSource();
-
+    if ((evt.getModifiers() & InputEvent.BUTTON3_MASK)!=0)
+    {
+      this.popupSort(check.getLabel(), fr.minmax, evt.getX(), evt.getY());
+    }
     if (fr.featureLinks != null
             && fr.featureLinks.containsKey(check.getLabel()))
     {
@@ -562,4 +639,81 @@ public class FeatureSettings extends Panel implements ItemListener,
       }
     }
   }
+  protected void sortByDens(String[] typ)
+  {
+    sortBy(typ, "Sort by Density", AlignmentSorter.FEATURE_DENSITY);
+  }
+  private String[] getDisplayedFeatureTypes()
+  {
+    String[] typ = null;
+    if (fr != null)
+    {
+      synchronized (fr.renderOrder)
+      {
+        typ = new String[fr.renderOrder.length];
+        System.arraycopy(fr.renderOrder, 0, typ, 0, typ.length);
+        for (int i = 0; i < typ.length; i++)
+        {
+          if (av.featuresDisplayed.get(typ[i]) == null)
+          {
+            typ[i] = null;
+          }
+        }
+      }
+    }
+    return typ;
+  }
+
+
+  protected void sortBy(String[] typ, String methodText, final String method)
+  {
+    if (typ == null)
+    {
+      typ = getDisplayedFeatureTypes();
+    }
+    String gps[] = null;
+    gps = fr.getGroups(true);
+    if (typ != null)
+    {
+      for (int i = 0; i < typ.length; i++)
+      {
+        System.err.println("Sorting on Types:" + typ[i]);
+      }
+    }
+    if (gps != null)
+    {
+
+      for (int i = 0; i < gps.length; i++)
+      {
+        System.err.println("Sorting on groups:" + gps[i]);
+      }
+    }
+    AlignmentPanel alignPanel = ap;
+    AlignmentI al = alignPanel.av.getAlignment();
+
+    int start, stop;
+    SequenceGroup sg = alignPanel.av.getSelectionGroup();
+    if (sg != null)
+    {
+      start = sg.getStartRes();
+      stop = sg.getEndRes();
+    }
+    else
+    {
+      start = 0;
+      stop = al.getWidth();
+    }
+    SequenceI[] oldOrder = al.getSequencesArray();
+    AlignmentSorter.sortByFeature(typ, gps, start, stop, al, method);
+    this.ap.alignFrame.addHistoryItem(new OrderCommand(methodText, oldOrder, alignPanel.av
+            .getAlignment()));
+    alignPanel.paintAlignment(true);
+
+  }
+
+  protected void sortByScore(String[] typ)
+  {
+    sortBy(typ, "Sort by Feature Score", AlignmentSorter.FEATURE_SCORE);
+  }
+
 }