sortbytree view setting and action added - setting is not yet stored in the jalview xml
[jalview.git] / src / jalview / gui / AlignFrame.java
index be92acf..33ac298 100755 (executable)
@@ -47,6 +47,7 @@ import jalview.ws.*;
 public class AlignFrame extends GAlignFrame implements DropTargetListener,
         IProgressIndicator
 {
+
   /** DOCUMENT ME!! */
   public static final int DEFAULT_WIDTH = 700;
 
@@ -70,16 +71,39 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
   String fileName = null;
 
   /**
-   * Creates a new AlignFrame object.
+   * Creates a new AlignFrame object with specific width and height.
    * 
    * @param al
-   *                DOCUMENT ME!
+   * @param width
+   * @param height
    */
   public AlignFrame(AlignmentI al, int width, int height)
   {
     this(al, null, width, height);
   }
-
+  /**
+   * Creates a new AlignFrame object with specific width, height and sequenceSetId
+   * @param al
+   * @param width
+   * @param height
+   * @param sequenceSetId
+   */
+  public AlignFrame(AlignmentI al, int width, int height, String sequenceSetId)
+  {
+    this(al, null, width, height, sequenceSetId);
+  }
+  /**
+   * Creates a new AlignFrame object with specific width, height and sequenceSetId
+   * @param al
+   * @param width
+   * @param height
+   * @param sequenceSetId
+   * @param viewId
+   */
+  public AlignFrame(AlignmentI al, int width, int height, String sequenceSetId, String viewId)
+  {
+    this(al, null, width, height, sequenceSetId, viewId);
+  }
   /**
    * new alignment window with hidden columns
    * 
@@ -87,12 +111,42 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
    *                AlignmentI
    * @param hiddenColumns
    *                ColumnSelection or null
+   * @param width Width of alignment frame
+   * @param height height of frame.
    */
   public AlignFrame(AlignmentI al, ColumnSelection hiddenColumns,
           int width, int height)
   {
-    this.setSize(width, height);
-    viewport = new AlignViewport(al, hiddenColumns);
+    this(al, hiddenColumns, width, height, null);
+  }
+  /**
+   * Create alignment frame for al with hiddenColumns, a specific width and height, and specific sequenceId
+   * @param al
+   * @param hiddenColumns
+   * @param width
+   * @param height
+   * @param sequenceSetId (may be null)
+   */
+  public AlignFrame(AlignmentI al, ColumnSelection hiddenColumns,
+          int width, int height, String sequenceSetId)
+  {
+    this(al, hiddenColumns, width, height, sequenceSetId, null);
+  }
+
+  /**
+   * Create alignment frame for al with hiddenColumns, a specific width and height, and specific sequenceId
+   * @param al
+   * @param hiddenColumns
+   * @param width
+   * @param height
+   * @param sequenceSetId (may be null)
+   * @param viewId (may be null)
+   */
+  public AlignFrame(AlignmentI al, ColumnSelection hiddenColumns, int width, int height,
+          String sequenceSetId, String viewId)
+  {
+    setSize(width, height);
+    viewport = new AlignViewport(al, hiddenColumns, sequenceSetId, viewId);
 
     alignPanel = new AlignmentPanel(this, viewport);
 
@@ -120,7 +174,9 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
     addAlignmentPanel(ap, false);
     init();
   }
-
+  /**
+   * initalise the alignframe from the underlying viewport data and the configurations
+   */
   void init()
   {
     if (viewport.conservation == null)
@@ -544,7 +600,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
     annotationPanelMenuItem.setState(av.showAnnotation);
     viewBoxesMenuItem.setSelected(av.showBoxes);
     viewTextMenuItem.setSelected(av.showText);
-
+    showUnconservedMenuItem.setSelected(av.showUnconserved);
     setColourSelected(ColourSchemeProperty.getColourName(av
             .getGlobalColourScheme()));
 
@@ -559,7 +615,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
     updateEditMenuBar();
   }
 
-  Hashtable progressBars;
+  Hashtable progressBars, progressBarHandlers;
 
   /*
    * (non-Javadoc)
@@ -571,6 +627,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
     if (progressBars == null)
     {
       progressBars = new Hashtable();
+      progressBarHandlers = new Hashtable();
     }
 
     JPanel progressPanel;
@@ -585,7 +642,10 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
       {
         statusBar.setText(message);
       }
-
+      if (progressBarHandlers.contains(new Long(id)))
+      {
+          progressBarHandlers.remove(new Long(id));
+      }
       layout.setRows(layout.getRows() - 1);
     }
     else
@@ -607,7 +667,29 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
     setMenusForViewport();
     validate();
   }
+  public void registerHandler(final long id, final IProgressIndicatorHandler handler)
+  {
+    if (progressBarHandlers==null || !progressBars.contains(new Long(id)))
+    {
+      throw new Error("call setProgressBar before registering the progress bar's handler.");
+    }
+    progressBarHandlers.put(new Long(id), handler);
+    final JPanel progressPanel = (JPanel) progressBars.get(new Long(id));
+    if (handler.canCancel())
+    {
+      JButton cancel = new JButton("Cancel");
+      final IProgressIndicator us=this;
+      cancel.addActionListener(new ActionListener() {
 
+        public void actionPerformed(ActionEvent e)
+        {
+          handler.cancelActivity(id);
+          us.setProgressBar("Cancelled "+((JLabel)progressPanel.getComponent(0)).getText(), id);
+        }
+      });
+      progressPanel.add(cancel, BorderLayout.EAST);
+    }
+  }
   /**
    * 
    * @return true if any progress bars are still active
@@ -701,8 +783,8 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
   public void save_actionPerformed(ActionEvent e)
   {
     if (fileName == null
-            || (currentFileFormat == null || jalview.io.AppletFormatAdapter
-                    .isValidFormat(currentFileFormat, true))
+            || (currentFileFormat == null 
+                    || !jalview.io.FormatAdapter.isValidIOFormat(currentFileFormat, true))
             || fileName.startsWith("http"))
     {
       saveAs_actionPerformed(null);
@@ -980,10 +1062,10 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
   }
 
   /**
-   * DOCUMENT ME!
+   * Close the current view or all views in the alignment frame. 
+   * If the frame only contains one view then the alignment will be removed from memory.
    * 
-   * @param e
-   *                DOCUMENT ME!
+   * @param closeAllTabs 
    */
   public void closeMenuItem_actionPerformed(boolean closeAllTabs)
   {
@@ -1001,9 +1083,11 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
           for (int i = 0; i < alignPanels.size(); i++)
           {
             AlignmentPanel ap = (AlignmentPanel) alignPanels.elementAt(i);
-            jalview.structure.StructureSelectionManager
-                    .getStructureSelectionManager()
-                    .removeStructureViewerListener(ap.seqPanel, null);
+            jalview.structure.StructureSelectionManager ssm = 
+              jalview.structure.StructureSelectionManager
+                    .getStructureSelectionManager();
+            ssm.removeStructureViewerListener(ap.seqPanel, null);
+            ssm.removeSelectionListener(ap.seqPanel);
             PaintRefresher.RemoveComponent(ap.seqPanel.seqCanvas);
             PaintRefresher.RemoveComponent(ap.idPanel.idCanvas);
             PaintRefresher.RemoveComponent(ap);
@@ -1012,25 +1096,8 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
         }
         else
         {
-          int index = tabbedPane.getSelectedIndex();
-
-          alignPanels.removeElement(alignPanel);
-          PaintRefresher.RemoveComponent(alignPanel.seqPanel.seqCanvas);
-          PaintRefresher.RemoveComponent(alignPanel.idPanel.idCanvas);
-          PaintRefresher.RemoveComponent(alignPanel);
-          viewport.alignment = null;
-          alignPanel = null;
+          closeView(alignPanel);
           viewport = null;
-
-          tabbedPane.removeTabAt(index);
-          tabbedPane.validate();
-
-          if (index == tabbedPane.getTabCount())
-          {
-            index--;
-          }
-
-          this.tabSelectionChanged(index);
         }
       }
 
@@ -1044,6 +1111,34 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
     }
   }
 
+  public void closeView(AlignmentPanel alignPanel2)
+  {
+    int index = tabbedPane.getSelectedIndex();
+    int closedindex = tabbedPane.indexOfComponent(alignPanel2);
+    alignPanels.removeElement(alignPanel2);
+    PaintRefresher.RemoveComponent(alignPanel2.seqPanel.seqCanvas);
+    PaintRefresher.RemoveComponent(alignPanel2.idPanel.idCanvas);
+    PaintRefresher.RemoveComponent(alignPanel2);
+    alignPanel2.av.alignment = null;
+    
+    if (viewport == alignPanel2.av)
+    {
+      viewport = null;
+    }
+    alignPanel2 = null;
+    
+    tabbedPane.removeTabAt(closedindex);
+    tabbedPane.validate();
+
+    if (index > closedindex || index == tabbedPane.getTabCount())
+    {
+      // modify currently selected tab index if necessary.
+      index--;
+    }
+
+    this.tabSelectionChanged(index);    
+  }
+
   /**
    * DOCUMENT ME!
    */
@@ -1765,11 +1860,11 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
             viewport.alignment));
 
     viewport.setSelectionGroup(null);
+    viewport.sendSelection();
     viewport.alignment.deleteGroup(sg);
 
     viewport.firePropertyChange("alignment", null, viewport.getAlignment()
             .getSequences());
-
     if (viewport.getAlignment().getHeight() < 1)
     {
       try
@@ -1813,6 +1908,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
     sg.setEndRes(viewport.alignment.getWidth() - 1);
     viewport.setSelectionGroup(sg);
+    viewport.sendSelection();
     alignPanel.paintAlignment(true);
     PaintRefresher.Refresh(alignPanel, viewport.getSequenceSetId());
   }
@@ -1862,7 +1958,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
     }
 
     alignPanel.paintAlignment(true);
-
+    viewport.sendSelection();
     PaintRefresher.Refresh(alignPanel, viewport.getSequenceSetId());
   }
 
@@ -2053,11 +2149,19 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
   public void padGapsMenuitem_actionPerformed(ActionEvent e)
   {
     viewport.padGaps = padGapsMenuitem.isSelected();
-
     viewport.firePropertyChange("alignment", null, viewport.getAlignment()
             .getSequences());
   }
-
+  
+  //else
+  {
+    // if (justifySeqs>0)
+    {
+      // alignment.justify(justifySeqs!=RIGHT_JUSTIFY);
+    }
+  }
+  //}
+  
   /**
    * DOCUMENT ME!
    * 
@@ -2109,7 +2213,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
     newap.av.viewName = newViewName;
 
-    addAlignmentPanel(newap, false);
+    addAlignmentPanel(newap, true);
 
     if (alignPanels.size() == 2)
     {
@@ -2165,6 +2269,16 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
     viewport.centreColumnLabels = centreColumnLabelsMenuItem.getState();
     alignPanel.paintAlignment(true);
   }
+  /* (non-Javadoc)
+   * @see jalview.jbgui.GAlignFrame#followHighlight_actionPerformed()
+   */
+  protected void followHighlight_actionPerformed()
+  {
+    if (viewport.followHighlight = this.followHighlightMenuItem.getState())
+    {
+      alignPanel.scrollToPosition(alignPanel.seqPanel.seqCanvas.searchResults, false);
+    }
+  }
 
   /**
    * DOCUMENT ME!
@@ -2307,7 +2421,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
   }
 
   /**
-   * DOCUMENT ME!
+   * Set or clear 'Show Sequence Features'
    * 
    * @param evt
    *                DOCUMENT ME!
@@ -2321,6 +2435,27 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
       alignPanel.getOverviewPanel().updateOverviewImage();
     }
   }
+  /**
+   * Set or clear 'Show Sequence Features'
+   * 
+   * @param evt
+   *                DOCUMENT ME!
+   */
+  public void showSeqFeaturesHeight_actionPerformed(ActionEvent evt)
+  {
+    viewport.setShowSequenceFeaturesHeight(showSeqFeaturesHeight.isSelected());
+    if (viewport.getShowSequenceFeaturesHeight())
+    {
+      // ensure we're actually displaying features
+      viewport.setShowSequenceFeatures(true);
+      showSeqFeatures.setSelected(true);
+    }
+    alignPanel.paintAlignment(true);
+    if (alignPanel.getOverviewPanel() != null)
+    {
+      alignPanel.getOverviewPanel().updateOverviewImage();
+    }
+  }
 
   /**
    * DOCUMENT ME!
@@ -2902,6 +3037,19 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
     addHistoryItem(new OrderCommand("ID Sort", oldOrder, viewport.alignment));
     alignPanel.paintAlignment(true);
   }
+  /**
+   * DOCUMENT ME!
+   * 
+   * @param e
+   *                DOCUMENT ME!
+   */
+  public void sortLengthMenuItem_actionPerformed(ActionEvent e)
+  {
+    SequenceI[] oldOrder = viewport.getAlignment().getSequencesArray();
+    AlignmentSorter.sortByLength(viewport.getAlignment());
+    addHistoryItem(new OrderCommand("Length Sort", oldOrder, viewport.alignment));
+    alignPanel.paintAlignment(true);
+  }
 
   /**
    * DOCUMENT ME!
@@ -3562,30 +3710,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
   private void resetWebServiceMenu()
   {
     webService.removeAll();
-    // Temporary hack - DBRef Fetcher always top level ws entry.
-    JMenuItem rfetch = new JMenuItem("Fetch DB References");
-    rfetch
-            .setToolTipText("Retrieve and parse sequence database records for the alignment or the currently selected sequences");
-    webService.add(rfetch);
-    rfetch.addActionListener(new ActionListener()
-    {
-
-      public void actionPerformed(ActionEvent e)
-      {
-        new Thread(new Runnable()
-        {
-
-          public void run()
-          {
-            new jalview.ws.DBRefFetcher(alignPanel.av
-                    .getSequenceSelection(), alignPanel.alignFrame)
-                    .fetchDBRefs(false);
-          }
-        }).start();
-
-      }
-
-    });
+    build_fetchdbmenu(webService);
   }
 
   /*
@@ -3639,7 +3764,8 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
       showProducts.removeAll();
       final boolean dna = viewport.getAlignment().isNucleotide();
       final Alignment ds = dataset;
-      String[] ptypes = CrossRef.findSequenceXrefTypes(dna, selection,
+      String[] ptypes = (selection==null || selection.length==0) ? null
+              : CrossRef.findSequenceXrefTypes(dna, selection,
               dataset);
       // Object[] prods =
       // CrossRef.buildXProductsList(viewport.getAlignment().isNucleotide(),
@@ -3715,12 +3841,12 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
             }
             Alignment al = new Alignment(sprods);
             AlignedCodonFrame[] cf = prods.getCodonFrames();
+            al.setDataset(ds);
             for (int s = 0; cf != null && s < cf.length; s++)
             {
               al.addCodonFrame(cf[s]);
               cf[s] = null;
             }
-            al.setDataset(ds);
             AlignFrame naf = new AlignFrame(al, DEFAULT_WIDTH,
                     DEFAULT_HEIGHT);
             String newtitle = "" + ((fdna) ? "Proteins " : "Nucleotides ")
@@ -4077,6 +4203,144 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
   {
     viewport.setShowNpFeats(showNpFeatsMenuitem.isSelected());
   }
+
+  /**
+   * find the viewport amongst the tabs in this alignment frame and close that tab
+   * @param av
+   */
+  public boolean closeView(AlignViewport av)
+  {
+    if (viewport == av)
+    {
+      this.closeMenuItem_actionPerformed(false);
+      return true;
+    }
+    Component[] comp = tabbedPane.getComponents();
+    for (int i=0;comp!=null && i<comp.length;i++)
+    {
+      if (comp[i] instanceof AlignmentPanel)
+      {
+        if (((AlignmentPanel) comp[i]).av == av)
+        {
+          // close the view.
+          closeView((AlignmentPanel) comp[i]);
+          return true;
+        }
+      }
+    }
+    return false;
+  }
+  protected void build_fetchdbmenu(JMenu webService) {
+    // Temporary hack - DBRef Fetcher always top level ws entry.
+    // TODO We probably want to store a sequence database checklist in preferences and have checkboxes.. rather than individual sources selected here
+    JMenu rfetch = new JMenu("Fetch DB References");
+    rfetch
+            .setToolTipText("Retrieve and parse sequence database records for the alignment or the currently selected sequences");
+    webService.add(rfetch);
+
+    JMenuItem fetchr = new JMenuItem("Standard Databases");
+    fetchr.setToolTipText("Fetch from EMBL/EMBLCDS or Uniprot/PDB and any selected DAS sources");
+    fetchr.addActionListener(new ActionListener()
+    {
+
+      public void actionPerformed(ActionEvent e)
+      {
+        new Thread(new Runnable()
+        {
+
+          public void run()
+          {
+            new jalview.ws.DBRefFetcher(alignPanel.av
+                    .getSequenceSelection(), alignPanel.alignFrame)
+                    .fetchDBRefs(false);
+          }
+        }).start();
+
+      }
+
+    });
+    rfetch.add(fetchr);
+    JMenu dfetch = new JMenu();
+    rfetch.add(dfetch);
+    jalview.ws.SequenceFetcher sf = SequenceFetcher.getSequenceFetcherSingleton(this);
+    String[] otherdb = sf.getOrderedSupportedSources(); 
+    // sf.getDbInstances(jalview.ws.dbsources.DasSequenceSource.class);
+    // jalview.util.QuickSort.sort(otherdb, otherdb);
+    int comp=0,mcomp=15;
+    String mname=null;
+    if (otherdb!=null && otherdb.length>0)
+    {
+      for (int i=0; i<otherdb.length; i++)
+      {
+        String dbname =sf.getSourceProxy(otherdb[i]).getDbName();
+        if (mname == null)
+        {
+          mname = "from '"+dbname+"'";
+        }
+        fetchr = new JMenuItem(otherdb[i]);
+        final String[] dassource = new String[] { otherdb[i] };
+        fetchr.addActionListener(new ActionListener()
+        {
+
+          public void actionPerformed(ActionEvent e)
+          {
+            new Thread(new Runnable()
+            {
+
+              public void run()
+              {
+                new jalview.ws.DBRefFetcher(alignPanel.av
+                        .getSequenceSelection(), alignPanel.alignFrame, dassource)
+                        .fetchDBRefs(false);
+              }
+            }).start();
+          }
+          
+        });
+        fetchr.setToolTipText("Retrieve from "+dbname);
+        dfetch.add(fetchr);
+        if (comp++==mcomp || i==(otherdb.length-1))
+        {
+          dfetch.setText(mname+" to '"+dbname+"'");
+          rfetch.add(dfetch);
+          dfetch = new JMenu();
+          mname = null;
+          comp=0;
+        }
+      }
+    }
+  }
+  /**
+   *  Left justify the whole alignment.
+   */
+  protected void justifyLeftMenuItem_actionPerformed(ActionEvent e)
+  {
+    AlignmentI al = viewport.getAlignment();
+    al.justify(false);
+    viewport.firePropertyChange("alignment", null, al);
+  }
+  /**
+   *  Right justify the whole alignment.
+   */
+  protected void justifyRightMenuItem_actionPerformed(ActionEvent e)
+  {
+    AlignmentI al = viewport.getAlignment();
+    al.justify(true);
+    viewport.firePropertyChange("alignment", null, al);
+  }
+  public void setShowSeqFeatures(boolean b)
+  {
+    showSeqFeatures.setSelected(true);
+    viewport.setShowSequenceFeatures(true);
+  }
+  /* (non-Javadoc)
+   * @see jalview.jbgui.GAlignFrame#showUnconservedMenuItem_actionPerformed(java.awt.event.ActionEvent)
+   */
+  protected void showUnconservedMenuItem_actionPerformed(ActionEvent e)
+  {
+    viewport.setShowUnconserved(showUnconservedMenuItem.getState());
+    alignPanel.paintAlignment(true);
+  }
 }
 
 class PrintThread extends Thread