experimental buttons for sort on score/density and popup menu to sort by score/densit...
authorjprocter <Jim Procter>
Mon, 8 Dec 2008 12:38:32 +0000 (12:38 +0000)
committerjprocter <Jim Procter>
Mon, 8 Dec 2008 12:38:32 +0000 (12:38 +0000)
src/jalview/gui/FeatureSettings.java

index 255340e..0b645f2 100755 (executable)
@@ -30,6 +30,8 @@ import javax.swing.*;
 import javax.swing.event.*;
 import javax.swing.table.*;
 
+import jalview.analysis.AlignmentSorter;
+import jalview.commands.OrderCommand;
 import jalview.datamodel.*;
 import jalview.io.*;
 
@@ -90,6 +92,10 @@ public class FeatureSettings extends JPanel
       public void mousePressed(MouseEvent evt)
       {
         selectedRow = table.rowAtPoint(evt.getPoint());
+        if (javax.swing.SwingUtilities.isRightMouseButton(evt))
+        {
+          popupSort((String)table.getValueAt(selectedRow, 0), evt.getX(), evt.getY());
+        }
       }
     });
 
@@ -162,6 +168,34 @@ public class FeatureSettings extends JPanel
     frame.setLayer(JLayeredPane.PALETTE_LAYER);
   }
 
+  protected void popupSort(final String type, int x, int y)
+  {
+    JPopupMenu men = new JPopupMenu();
+    men.setName("Sort by "+type);
+    JMenuItem scr = new JMenuItem("Score");
+    men.add(scr);
+    final FeatureSettings me=this;
+    scr.addActionListener(new ActionListener() {
+
+      public void actionPerformed(ActionEvent e)
+      {
+        me.sortByScore(new String[] {type});
+      }
+      
+    });
+    JMenuItem dens = new JMenuItem("Density");
+    dens.addActionListener(new ActionListener() {
+
+      public void actionPerformed(ActionEvent e)
+      {
+        me.sortByDens(new String[] { type });
+      }
+      
+    });
+    men.add(dens);
+    men.show(table, x, y);
+  }
+
   /**
    * true when Feature Settings are updating from feature renderer
    */
@@ -680,6 +714,8 @@ public class FeatureSettings extends JPanel
   JButton cancelDAS = new JButton();
 
   JButton optimizeOrder = new JButton();
+  JButton sortByScore = new JButton();
+  JButton sortByDens = new JButton();
 
   JPanel transbuttons = new JPanel(new BorderLayout());
 
@@ -707,7 +743,24 @@ public class FeatureSettings extends JPanel
         orderByAvWidth();
       }
     });
-    cancel.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
+    sortByScore.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
+    sortByScore.setText("Seq sort by Score");
+    sortByScore.addActionListener(new ActionListener()
+    {
+      public void actionPerformed(ActionEvent e)
+      {
+        sortByScore(null);
+      }
+    });
+    sortByDens.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
+    sortByDens.setText("Seq Sort by density");
+    sortByDens.addActionListener(new ActionListener()
+    {
+      public void actionPerformed(ActionEvent e)
+      {
+        sortByDens(null);
+      }
+    });    cancel.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
     cancel.setText("Cancel");
     cancel.addActionListener(new ActionListener()
     {
@@ -793,6 +846,8 @@ public class FeatureSettings extends JPanel
     buttonPanel.add(cancel);
     buttonPanel.add(loadColours);
     buttonPanel.add(saveColours);
+    buttonPanel.add(sortByScore);
+    buttonPanel.add(sortByDens);
     bigPanel.add(scrollPane, java.awt.BorderLayout.CENTER);
     dasSettingsPane.add(dasButtonPanel, java.awt.BorderLayout.SOUTH);
     dasButtonPanel.add(fetchDAS);
@@ -802,6 +857,114 @@ public class FeatureSettings extends JPanel
     settingsPane.add(buttonPanel, java.awt.BorderLayout.SOUTH);
   }
 
+  protected void sortByDens(String[] typ)
+  {
+    sortBy(typ, "Sort by Density", AlignmentSorter.FEATURE_DENSITY);
+  }
+  protected void sortBy(String[] typ, String methodText, final String method) {
+    if (typ==null)
+    {
+      typ = getDisplayedFeatureTypes();
+    }
+    String gps[]=null;
+    gps = getDisplayedFeatureGroups();
+    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 = af.alignPanel;
+    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);
+    af.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);
+  }
+
+  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 (af.viewport.featuresDisplayed.get(typ[i])==null)
+          {
+            typ[i] = null;
+          }
+        }
+      }
+    }
+    return typ;
+  }
+
+  private String[] getDisplayedFeatureGroups()
+  {
+    String[] gps=null;
+    if (fr!=null)
+    {
+      
+      if (fr.featureGroups!=null)
+      {
+        Enumeration en = fr.featureGroups.keys();
+        gps = new String[fr.featureColours.size()];
+        int g=0;
+        boolean valid=false;
+        while (en.hasMoreElements())
+        {
+          String gp = (String) en.nextElement();
+          Boolean on = (Boolean )fr.featureGroups.get(gp);
+          if (on!=null && on.booleanValue())
+          {
+            valid = true;
+            gps[g++] = gp;
+          }
+        }
+        while (g<gps.length)
+        {
+          gps[g++] = null;
+        }
+        if (!valid)
+        {
+          return null;
+        }
+      }
+    }
+    return gps;
+  }
+
   public void fetchDAS_actionPerformed(ActionEvent e)
   {
     fetchDAS.setEnabled(false);