moved to same place as in jalview.gui
[jalview.git] / src / jalview / appletgui / AlignmentPanel.java
index c2bdaf0..e4b9b59 100755 (executable)
@@ -1,29 +1,31 @@
 /*
- * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
- * Copyright (C) 2008 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
+ * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6)
+ * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
  * 
- * This program 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 2
- * of the License, or (at your option) any later version.
+ * This file is part of Jalview.
  * 
- * This program 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.
+ * 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.
  * 
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
+ * 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/>.
  */
 package jalview.appletgui;
 
 import java.awt.*;
 import java.awt.event.*;
+import java.util.Hashtable;
+import java.util.Vector;
 
+import jalview.api.AlignmentViewPanel;
 import jalview.datamodel.*;
 
-public class AlignmentPanel extends Panel implements AdjustmentListener
+public class AlignmentPanel extends Panel implements AdjustmentListener, AlignmentViewPanel
 {
 
   public AlignViewport av;
@@ -64,8 +66,9 @@ public class AlignmentPanel extends Panel implements AdjustmentListener
     scalePanel = new ScalePanel(av, this);
     idwidthAdjuster = new IdwidthAdjuster(this);
     annotationPanel = new AnnotationPanel(this);
+    annotationPanelHolder.add(annotationPanel, BorderLayout.CENTER);
 
-    sequenceHolderPanel.add(annotationPanel, BorderLayout.SOUTH);
+    sequenceHolderPanel.add(annotationPanelHolder, BorderLayout.SOUTH);
 
     alabels = new AnnotationLabels(this);
 
@@ -80,6 +83,7 @@ public class AlignmentPanel extends Panel implements AdjustmentListener
     fontChanged();
     setScrollValues(0, 0);
 
+    apvscroll.addAdjustmentListener(this);
     hscroll.addAdjustmentListener(this);
     vscroll.addAdjustmentListener(this);
 
@@ -101,7 +105,6 @@ public class AlignmentPanel extends Panel implements AdjustmentListener
     annotationSpaceFillerHolder.setSize(d.width,
             annotationPanel.getSize().height);
     alabels.setSize(d.width, annotationPanel.getSize().height);
-
     final AlignmentPanel ap = this;
     av.addPropertyChangeListener(new java.beans.PropertyChangeListener()
     {
@@ -153,14 +156,15 @@ public class AlignmentPanel extends Panel implements AdjustmentListener
             + fm.getDescent()));
     av.updateSequenceIdColours();
     annotationPanel.image = null;
-    int ap = annotationPanel.adjustPanelHeight();
-    annotationPanel.repaint();
+    int ap = annotationPanel.adjustPanelHeight(false);
     Dimension d = calculateIdWidth();
     d.setSize(d.width + 4, seqPanel.seqCanvas.getSize().height);
     alabels.setSize(d.width + 4, ap);
     idPanel.idCanvas.setSize(d);
     hscrollFillerPanel.setSize(d);
-
+    
+    validateAnnotationDimensions(false);
+    annotationPanel.repaint();
     validate();
     repaint();
 
@@ -174,6 +178,7 @@ public class AlignmentPanel extends Panel implements AdjustmentListener
   {
     idPanel.idCanvas.setSize(w, h);
     idPanelHolder.setSize(w, idPanelHolder.getSize().height);
+    annotationSpaceFillerHolder.setSize(w,annotationSpaceFillerHolder.getSize().height);
     alabels.setSize(w, alabels.getSize().height);
     validate();
   }
@@ -225,18 +230,87 @@ public class AlignmentPanel extends Panel implements AdjustmentListener
     return new Dimension(idWidth, idPanel.idCanvas.getSize().height);
   }
 
+  /**
+   * Highlight the given results on the alignment.
+   * 
+   */
   public void highlightSearchResults(SearchResults results)
   {
+    scrollToPosition(results);
     seqPanel.seqCanvas.highlightSearchResults(results);
+  }
 
+  /**
+   * scroll the view to show the position of the highlighted region in results
+   * (if any) and redraw the overview
+   * 
+   * @param results
+   * @return false if results were not found
+   */
+  public boolean scrollToPosition(SearchResults results)
+  {
+    return scrollToPosition(results, true);
+  }
+
+  /**
+   * scroll the view to show the position of the highlighted region in results
+   * (if any)
+   * 
+   * @param results
+   * @param redrawOverview
+   *          - when set, the overview will be recalculated (takes longer)
+   * @return false if results were not found
+   */
+  public boolean scrollToPosition(SearchResults results,
+          boolean redrawOverview)
+  {
     // do we need to scroll the panel?
-    if (results != null)
+    if (results != null && results.getSize() > 0)
     {
-      SequenceI seq = results.getResultSequence(0);
-      int seqIndex = av.alignment.findIndex(seq);
-      int start = seq.findIndex(results.getResultStart(0)) - 1;
-      int end = seq.findIndex(results.getResultEnd(0)) - 1;
-
+      int seqIndex = av.alignment.findIndex(results);
+      if (seqIndex == -1)
+      {
+        return false;
+      }
+      SequenceI seq = av.alignment.getSequenceAt(seqIndex);
+      int[] r = results.getResults(seq, 0,av.alignment.getWidth());
+      if (r == null)
+      {
+        if (av.applet.debug) {// DEBUG
+          System.out.println("DEBUG: scroll didn't happen - results not within alignment : "+seq.getStart()+","+seq.getEnd());
+        }
+        return false;
+      }
+      if (av.applet.debug) {
+        // DEBUG
+        System.out.println("DEBUG: scroll didn't happen: start=" + r[0]
+                + " av.getStartRes()=" + av.getStartRes() + " end=" + r[1]
+                + " seq.end=" + seq.getEnd() + " av.getEndRes()="
+                + av.getEndRes() + " hextent=" + hextent);
+      }
+      int start = r[0];
+      int end = r[1];
+      if (start < 0)
+      {
+        return false;
+      }
+      if (end == seq.getEnd())
+      {
+        return false;
+      }
+      if (av.hasHiddenColumns)
+      {
+        start = av.getColumnSelection().findColumnPosition(start);
+        end = av.getColumnSelection().findColumnPosition(end);
+        if (start == end)
+        {
+          if (!av.colSel.isVisible(r[0]))
+          {
+            // don't scroll - position isn't visible
+            return false;
+          }
+        }
+      }
       if (!av.wrapAlignment)
       {
         if ((av.getStartRes() > end)
@@ -250,6 +324,7 @@ public class AlignmentPanel extends Panel implements AdjustmentListener
             {
               start = 0;
             }
+
           }
           if (seqIndex > av.alignment.getHeight() - vextent)
           {
@@ -267,8 +342,12 @@ public class AlignmentPanel extends Panel implements AdjustmentListener
         scrollToWrappedVisible(start);
       }
     }
-
-    repaint();
+    if (redrawOverview && overviewPanel != null)
+    {
+      overviewPanel.setBoxPosition();
+    }
+    paintAlignment(redrawOverview);
+    return true;
   }
 
   void scrollToWrappedVisible(int res)
@@ -297,12 +376,69 @@ public class AlignmentPanel extends Panel implements AdjustmentListener
     if (!av.wrapAlignment)
     {
       annotationSpaceFillerHolder.setVisible(b);
-      annotationPanel.setVisible(b);
+      annotationPanelHolder.setVisible(b);
     }
     validate();
     repaint();
   }
-
+  
+  /**
+   * automatically adjust annotation panel height for new annotation whilst
+   * ensuring the alignment is still visible.
+   */
+  public void adjustAnnotationHeight()
+  {
+    // TODO: display vertical annotation scrollbar if necessary
+    // this is called after loading new annotation onto alignment
+    if (alignFrame.getSize().height == 0)
+    {
+      System.out.println("NEEDS FIXING");
+    }
+    fontChanged();
+    validateAnnotationDimensions(true);
+    apvscroll.addNotify();
+    hscroll.addNotify();
+    validate();
+    addNotify();
+    repaint();
+  }
+  /**
+   * calculate the annotation dimensions and refresh slider values accordingly.
+   * need to do repaints/notifys afterwards. 
+   */
+  protected void validateAnnotationDimensions(boolean adjustPanelHeight) {
+    int height = annotationPanel.calcPanelHeight();
+    if (hscroll.isVisible())
+    {
+      height += hscroll.getPreferredSize().height;
+    }
+    int mheight = height;
+    // sets initial preferred height
+    if ((height+40) > getSize().height / 2)
+    {
+      height = getSize().height / 2;
+    }
+    if (!adjustPanelHeight)
+    {
+      // maintain same window layout whilst updating sliders
+      height=seqPanelHolder.getSize().height;
+    }
+    Dimension d=seqPanelHolder.getSize(),e=idPanel.getSize(); 
+    annotationPanel.setSize(new Dimension(d.width,height));
+    alabels.setSize(e.width,height);
+    annotationSpaceFillerHolder.setSize(new Dimension(e.width, height));
+    annotationPanelHolder.setSize(new Dimension(d.width, height));
+    seqPanelHolder.setSize(d.width,d.height-height);
+    int s=apvscroll.getValue();
+    if (s>mheight-height)
+    {
+      s = 0;
+    }
+    apvscroll.setValues(apvscroll.getValue(), height, 0, mheight);
+    annotationPanel.setScrollOffset(apvscroll.getValue());
+    alabels.setScrollOffset(apvscroll.getValue());
+  }
+  
   public void setWrapAlignment(boolean wrap)
   {
     av.startSeq = 0;
@@ -314,12 +450,12 @@ public class AlignmentPanel extends Panel implements AdjustmentListener
 
     if (wrap)
     {
-      annotationPanel.setVisible(false);
+      annotationPanelHolder.setVisible(false);
       annotationSpaceFillerHolder.setVisible(false);
     }
     else if (av.showAnnotation)
     {
-      annotationPanel.setVisible(true);
+      annotationPanelHolder.setVisible(true);
       annotationSpaceFillerHolder.setVisible(true);
     }
 
@@ -394,12 +530,11 @@ public class AlignmentPanel extends Panel implements AdjustmentListener
     }
 
     av.setStartRes(x);
-    av
-            .setEndRes((x + (seqPanel.seqCanvas.getSize().width / av.charWidth)) - 1);
+    av.setEndRes((x + (seqPanel.seqCanvas.getSize().width / av.charWidth)) - 1);
 
     hextent = seqPanel.seqCanvas.getSize().width / av.charWidth;
     vextent = seqPanel.seqCanvas.getSize().height / av.charHeight;
-
+    
     if (hextent > width)
     {
       hextent = width;
@@ -454,6 +589,15 @@ public class AlignmentPanel extends Panel implements AdjustmentListener
     int oldX = av.getStartRes();
     int oldY = av.getStartSeq();
 
+    if (evt == null || evt.getSource() == apvscroll)
+    {
+      annotationPanel.setScrollOffset(apvscroll.getValue());
+      alabels.setScrollOffset(apvscroll.getValue());
+      // annotationPanel.image=null;
+      // alabels.image=null;
+      // alabels.repaint();
+      // annotationPanel.repaint();
+    }
     if (evt == null || evt.getSource() == hscroll)
     {
       int x = hscroll.getValue();
@@ -543,11 +687,7 @@ public class AlignmentPanel extends Panel implements AdjustmentListener
     invalidate();
     Dimension d = idPanel.idCanvas.getSize();
     idPanel.idCanvas.setSize(d.width, seqPanel.seqCanvas.getSize().height);
-    annotationSpaceFillerHolder.setSize(d.width,
-            annotationPanel.getSize().height);
-
-    alabels.setSize(d.width, annotationPanel.getSize().height);
-
+    
     if (av.getWrapAlignment())
     {
       int maxwidth = av.alignment.getWidth();
@@ -563,7 +703,7 @@ public class AlignmentPanel extends Panel implements AdjustmentListener
       if (canvasWidth > 0)
       {
         int max = maxwidth / canvasWidth;
-        vscroll.setMaximum(max);
+        vscroll.setMaximum(1 + max);
         vscroll.setUnitIncrement(1);
         vscroll.setVisibleAmount(1);
       }
@@ -619,12 +759,18 @@ public class AlignmentPanel extends Panel implements AdjustmentListener
 
   BorderLayout borderLayout2 = new BorderLayout();
 
+  Panel annotationPanelHolder = new Panel();
+
+  protected Scrollbar apvscroll = new Scrollbar();
+
+  BorderLayout borderLayout12 = new BorderLayout();
+
   private void jbInit() throws Exception
   {
     // idPanelHolder.setPreferredSize(new Dimension(70, 10));
     this.setLayout(borderLayout7);
 
-    // sequenceHolderPanel.setPreferredSize(new Dimension(150, 150));
+    //sequenceHolderPanel.setPreferredSize(new Dimension(150, 150));
     sequenceHolderPanel.setLayout(borderLayout3);
     seqPanelHolder.setLayout(borderLayout1);
     scalePanelHolder.setBackground(Color.white);
@@ -643,7 +789,13 @@ public class AlignmentPanel extends Panel implements AdjustmentListener
     hscroll.setOrientation(Scrollbar.HORIZONTAL);
     hscrollHolder.setLayout(borderLayout10);
     hscrollFillerPanel.setBackground(Color.white);
+    apvscroll.setOrientation(Scrollbar.VERTICAL);
+    apvscroll.setVisible(true);
+    apvscroll.addAdjustmentListener(this);
 
+    annotationPanelHolder.setBackground(Color.white);
+    annotationPanelHolder.setLayout(borderLayout12);
+    annotationPanelHolder.add(apvscroll, BorderLayout.EAST);
     // hscrollFillerPanel.setPreferredSize(new Dimension(70, 10));
     hscrollHolder.setBackground(Color.white);
 
@@ -665,4 +817,84 @@ public class AlignmentPanel extends Panel implements AdjustmentListener
     this.add(sequenceHolderPanel, BorderLayout.CENTER);
   }
 
+  /**
+   * hides or shows dynamic annotation rows based on groups and av state flags
+   */
+  public void updateAnnotation()
+  {
+    updateAnnotation(false);
+  }
+
+  public void updateAnnotation(boolean applyGlobalSettings)
+  {
+    // TODO: this should be merged with other annotation update stuff - that
+    // sits on AlignViewport
+    boolean updateCalcs = false;
+    boolean conv = av.isShowGroupConservation();
+    boolean cons = av.isShowGroupConsensus();
+    boolean showprf = av.isShowSequenceLogo();
+    boolean showConsHist = av.isShowConsensusHistogram();
+
+    boolean sortg = true;
+
+    // remove old automatic annotation
+    // add any new annotation
+
+    Vector gr = av.alignment.getGroups(); // OrderedBy(av.alignment.getSequencesArray());
+    // intersect alignment annotation with alignment groups
+
+    AlignmentAnnotation[] aan = av.alignment.getAlignmentAnnotation();
+    Hashtable oldrfs = new Hashtable();
+    if (aan != null)
+    {
+      for (int an = 0; an < aan.length; an++)
+      {
+        if (aan[an].autoCalculated && aan[an].groupRef != null)
+        {
+          oldrfs.put(aan[an].groupRef, aan[an].groupRef);
+          av.alignment.deleteAnnotation(aan[an]);
+          aan[an] = null;
+        }
+      }
+    }
+    SequenceGroup sg;
+    if (gr != null)
+    {
+      for (int g = 0; g < gr.size(); g++)
+      {
+        updateCalcs = false;
+        sg = (SequenceGroup) gr.elementAt(g);
+        if (applyGlobalSettings || !oldrfs.containsKey(sg))
+        {
+          // set defaults for this group's conservation/consensus
+          sg.setshowSequenceLogo(showprf);
+          sg.setShowConsensusHistogram(showConsHist);
+        }
+        if (conv)
+        {
+          updateCalcs = true;
+          av.alignment.addAnnotation(sg.getConservationRow(), 0);
+        }
+        if (cons)
+        {
+          updateCalcs = true;
+          av.alignment.addAnnotation(sg.getConsensus(), 0);
+        }
+        // refresh the annotation rows
+        if (updateCalcs)
+        {
+          sg.recalcConservation();
+        }
+      }
+    }
+    oldrfs.clear();
+    adjustAnnotationHeight();
+  }
+
+  @Override
+  public AlignmentI getAlignment()
+  {
+    return av.alignment;
+  }
+
 }