apply version 2.7 copyright
[jalview.git] / src / jalview / appletgui / AlignmentPanel.java
old mode 100755 (executable)
new mode 100644 (file)
index 05ecdcc..08d1f85
@@ -1,6 +1,6 @@
 /*
- * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6)
- * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
+ * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
+ * Copyright (C) 2011 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
  * 
  * This file is part of Jalview.
  * 
@@ -22,9 +22,11 @@ import java.awt.event.*;
 import java.util.Hashtable;
 import java.util.Vector;
 
+import jalview.api.AlignmentViewPanel;
 import jalview.datamodel.*;
+import jalview.structure.StructureSelectionManager;
 
-public class AlignmentPanel extends Panel implements AdjustmentListener
+public class AlignmentPanel extends Panel implements AdjustmentListener, AlignmentViewPanel
 {
 
   public AlignViewport av;
@@ -47,7 +49,19 @@ public class AlignmentPanel extends Panel implements AdjustmentListener
 
   // this value is set false when selection area being dragged
   boolean fastPaint = true;
-
+  
+  public void finalize() {
+    alignFrame=null;
+    av=null;
+    seqPanel=null;
+    seqPanelHolder=null;
+    sequenceHolderPanel=null;
+    scalePanel=null;
+    scalePanelHolder=null;
+    annotationPanel=null;
+    annotationPanelHolder=null;
+    annotationSpaceFillerHolder=null;
+  }
   public AlignmentPanel(AlignFrame af, final AlignViewport av)
   {
     try
@@ -64,14 +78,9 @@ public class AlignmentPanel extends Panel implements AdjustmentListener
     idPanel = new IdPanel(av, this);
     scalePanel = new ScalePanel(av, this);
     idwidthAdjuster = new IdwidthAdjuster(this);
-//    annotationScroller = new ScrollPane();
- //   annotationScroller.setBackground(Color.white);
     annotationPanel = new AnnotationPanel(this);
     annotationPanelHolder.add(annotationPanel, BorderLayout.CENTER);
-    // annotationScroller.setPreferredSize(new Dimension(10,80));
-//    annotationScroller.add(annotationPanel);
-//    annotationScroller.getInsets().set(0,0,0,0);
-    
+
     sequenceHolderPanel.add(annotationPanelHolder, BorderLayout.SOUTH);
 
     alabels = new AnnotationLabels(this);
@@ -83,7 +92,7 @@ public class AlignmentPanel extends Panel implements AdjustmentListener
     annotationSpaceFillerHolder.add(alabels, BorderLayout.CENTER);
     scalePanelHolder.add(scalePanel, BorderLayout.CENTER);
     seqPanelHolder.add(seqPanel, BorderLayout.CENTER);
-    
+
     fontChanged();
     setScrollValues(0, 0);
 
@@ -96,8 +105,12 @@ public class AlignmentPanel extends Panel implements AdjustmentListener
       public void componentResized(ComponentEvent evt)
       {
         setScrollValues(av.getStartRes(), av.getStartSeq());
+        if (getSize().height>0 && annotationPanelHolder.getSize().height>0) {
+          validateAnnotationDimensions(false);
+        }
         repaint();
       }
+      
     });
 
     Dimension d = calculateIdWidth();
@@ -161,13 +174,15 @@ public class AlignmentPanel extends Panel implements AdjustmentListener
     av.updateSequenceIdColours();
     annotationPanel.image = null;
     int ap = annotationPanel.adjustPanelHeight(false);
-    annotationPanel.repaint();
     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();
 
@@ -181,6 +196,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();
   }
@@ -266,6 +282,7 @@ public class AlignmentPanel extends Panel implements AdjustmentListener
   public boolean scrollToPosition(SearchResults results,
           boolean redrawOverview)
   {
+         
     // do we need to scroll the panel?
     if (results != null && results.getSize() > 0)
     {
@@ -275,11 +292,22 @@ public class AlignmentPanel extends Panel implements AdjustmentListener
         return false;
       }
       SequenceI seq = av.alignment.getSequenceAt(seqIndex);
-      int[] r = results.getResults(seq, seq.getStart(), seq.getEnd());
+      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: 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)
@@ -290,21 +318,57 @@ public class AlignmentPanel extends Panel implements AdjustmentListener
       {
         return false;
       }
-      if (av.hasHiddenColumns)
+      return scrollTo(start, end, seqIndex, false, redrawOverview);
+    }
+    return true;
+  }
+  public boolean scrollTo(int ostart, int end, int seqIndex, boolean scrollToNearest, boolean redrawOverview)
+  {
+           int startv, endv, starts, ends, width;
+
+         int start=-1;
+    if (av.hasHiddenColumns)
+    {
+      start = av.getColumnSelection().findColumnPosition(ostart);
+      end = av.getColumnSelection().findColumnPosition(end);
+      if (start == end)
       {
-        start = av.getColumnSelection().findColumnPosition(start);
-        end = av.getColumnSelection().findColumnPosition(end);
-        if (start == end)
+        if (!scrollToNearest && !av.colSel.isVisible(ostart))
         {
-          if (!av.colSel.isVisible(r[0]))
-          {
-            // don't scroll - position isn't visible
-            return false;
-          }
+          // don't scroll - position isn't visible
+          return false;
         }
       }
-      if (!av.wrapAlignment)
-      {
+    }
+    else
+    {
+      start = ostart;
+    }
+    if (!av.wrapAlignment)
+    {
+         /*
+         int spos=av.getStartRes(),sqpos=av.getStartSeq();
+          if ((startv = av.getStartRes()) >= start)
+          {
+                 spos=start-1;
+//               seqIn
+//            setScrollValues(start - 1, seqIndex);
+          }
+          else if ((endv = av.getEndRes()) <= end)
+          {
+//            setScrollValues(spos=startv + 1 + end - endv, seqIndex);
+                 spos=startv + 1 + end - endv;
+          }
+          else if ((starts = av.getStartSeq()) > seqIndex)
+          {
+            setScrollValues(av.getStartRes(), seqIndex);
+          }
+          else if ((ends = av.getEndSeq()) <= seqIndex)
+          {
+            setScrollValues(av.getStartRes(), starts + seqIndex - ends + 1);
+          }
+
+         /* */
         if ((av.getStartRes() > end)
                 || (av.getEndRes() < start)
                 || ((av.getStartSeq() > seqIndex) || (av.getEndSeq() < seqIndex)))
@@ -316,6 +380,7 @@ public class AlignmentPanel extends Panel implements AdjustmentListener
             {
               start = 0;
             }
+
           }
           if (seqIndex > av.alignment.getHeight() - vextent)
           {
@@ -325,14 +390,14 @@ public class AlignmentPanel extends Panel implements AdjustmentListener
               seqIndex = 0;
             }
           }
+          // System.out.println("trying to scroll to: "+start+" "+seqIndex);
           setScrollValues(start, seqIndex);
-        }
+        }/**/
       }
       else
       {
         scrollToWrappedVisible(start);
       }
-    }
     if (redrawOverview && overviewPanel != null)
     {
       overviewPanel.setBoxPosition();
@@ -367,12 +432,80 @@ 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();
+    paintAlignment(true);
+  }
+  /**
+   * calculate the annotation dimensions and refresh slider values accordingly.
+   * need to do repaints/notifys afterwards. 
+   */
+  protected void validateAnnotationDimensions(boolean adjustPanelHeight) {
+    boolean modified=false;
+    int height = annotationPanel.calcPanelHeight();
+    int minsize=0;
+    if (hscroll.isVisible())
+    {
+      height += (minsize=hscroll.getPreferredSize().height);
+    }
+    if (apvscroll.isVisible()) {
+      minsize+=apvscroll.getPreferredSize().height;
+    }
+    int mheight = height;
+    Dimension d=sequenceHolderPanel.getSize(),e=idPanel.getSize(); 
+    int seqandannot=d.height-scalePanelHolder.getSize().height;
+    // sets initial preferred height
+    if ((height+40) > seqandannot / 2)
+    {
+      height = seqandannot / 2;
+    }
+    if (!adjustPanelHeight)
+    {
+      // maintain same window layout whilst updating sliders
+      height=annotationPanelHolder.getSize().height;
+    }
+            
+    if (seqandannot-height<5)
+    {
+      height = seqandannot;
+    }
+    annotationPanel.setSize(new Dimension(d.width,height));
+    alabels.setSize(new Dimension(e.width,height));
+    annotationSpaceFillerHolder.setSize(new Dimension(e.width, height));
+    annotationPanelHolder.setSize(new Dimension(d.width, height));
+    seqPanelHolder.setSize(d.width,seqandannot-height);
+    seqPanel.seqCanvas.setSize(d.width, seqPanel.seqCanvas.getSize().height);
+    int s=apvscroll.getValue();
+    if (s>mheight-height)
+    {
+      s = 0;
+    }
+    apvscroll.setValues(s, height, 0, mheight);
+    annotationPanel.setScrollOffset(apvscroll.getValue());
+    alabels.setScrollOffset(apvscroll.getValue());
+  }
+  
   public void setWrapAlignment(boolean wrap)
   {
     av.startSeq = 0;
@@ -384,12 +517,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);
     }
 
@@ -462,13 +595,11 @@ public class AlignmentPanel extends Panel implements AdjustmentListener
     {
       width = av.getColumnSelection().findColumnPosition(width);
     }
-
-    av.setStartRes(x);
-    av.setEndRes((x + (seqPanel.seqCanvas.getSize().width / av.charWidth)) - 1);
+    if (x<0) { x = 0; };
 
     hextent = seqPanel.seqCanvas.getSize().width / av.charWidth;
     vextent = seqPanel.seqCanvas.getSize().height / av.charHeight;
-
+    
     if (hextent > width)
     {
       hextent = width;
@@ -481,6 +612,8 @@ public class AlignmentPanel extends Panel implements AdjustmentListener
 
     if ((hextent + x) > width)
     {
+      System.err.println("hextent was "+hextent+" and x was "+x);
+
       x = width - hextent;
     }
 
@@ -496,6 +629,7 @@ public class AlignmentPanel extends Panel implements AdjustmentListener
 
     if (x < 0)
     {
+      System.err.println("x was "+x);
       x = 0;
     }
 
@@ -508,6 +642,9 @@ public class AlignmentPanel extends Panel implements AdjustmentListener
     }
 
     av.setEndSeq(endSeq);
+    av.setStartRes(x);
+    av.setEndRes((x + (seqPanel.seqCanvas.getSize().width / av.charWidth)) - 1);
+
     hscroll.setValues(x, hextent, 0, width);
     vscroll.setValues(y, vextent, 0, height);
 
@@ -515,6 +652,8 @@ public class AlignmentPanel extends Panel implements AdjustmentListener
     {
       overviewPanel.setBoxPosition();
     }
+    sendViewPosition();
+
 
   }
 
@@ -523,14 +662,14 @@ public class AlignmentPanel extends Panel implements AdjustmentListener
     int oldX = av.getStartRes();
     int oldY = av.getStartSeq();
 
-    if (evt == null || evt.getSource()==apvscroll)
+    if (evt == null || evt.getSource() == apvscroll)
     {
       annotationPanel.setScrollOffset(apvscroll.getValue());
       alabels.setScrollOffset(apvscroll.getValue());
-      //annotationPanel.image=null;
-      //alabels.image=null;
-      //alabels.repaint();
-      //annotationPanel.repaint();
+      // annotationPanel.image=null;
+      // alabels.image=null;
+      // alabels.repaint();
+      // annotationPanel.repaint();
     }
     if (evt == null || evt.getSource() == hscroll)
     {
@@ -592,8 +731,13 @@ public class AlignmentPanel extends Panel implements AdjustmentListener
         annotationPanel.fastPaint(av.getStartRes() - oldX);
       }
     }
+    sendViewPosition();
 
   }
+  private void sendViewPosition()
+  {
+    StructureSelectionManager.getStructureSelectionManager(av.applet).sendViewPosition(this, av.startRes, av.endRes, av.startSeq, av.endSeq);
+  }
 
   public void paintAlignment(boolean updateOverview)
   {
@@ -602,7 +746,7 @@ public class AlignmentPanel extends Panel implements AdjustmentListener
     if (updateOverview)
     {
       jalview.structure.StructureSelectionManager
-              .getStructureSelectionManager().sequenceColoursChanged(this);
+              .getStructureSelectionManager(av.applet).sequenceColoursChanged(this);
 
       if (overviewPanel != null)
       {
@@ -621,12 +765,7 @@ public class AlignmentPanel extends Panel implements AdjustmentListener
     invalidate();
     Dimension d = idPanel.idCanvas.getSize();
     idPanel.idCanvas.setSize(d.width, seqPanel.seqCanvas.getSize().height);
-    annotationPanelHolder.setSize(seqPanel.getSize().width, annotationPanel.getSize().height);
-    annotationSpaceFillerHolder.setSize(d.width,
-            annotationPanel.getSize().height);
-
-    alabels.setSize(d.width, annotationPanelHolder.getSize().height);
-
+    
     if (av.getWrapAlignment())
     {
       int maxwidth = av.alignment.getWidth();
@@ -642,7 +781,7 @@ public class AlignmentPanel extends Panel implements AdjustmentListener
       if (canvasWidth > 0)
       {
         int max = maxwidth / canvasWidth;
-        vscroll.setMaximum(1+max);
+        vscroll.setMaximum(1 + max);
         vscroll.setUnitIncrement(1);
         vscroll.setVisibleAmount(1);
       }
@@ -699,7 +838,8 @@ public class AlignmentPanel extends Panel implements AdjustmentListener
   BorderLayout borderLayout2 = new BorderLayout();
 
   Panel annotationPanelHolder = new Panel();
-  protected Scrollbar apvscroll=new Scrollbar();
+
+  protected Scrollbar apvscroll = new Scrollbar();
 
   BorderLayout borderLayout12 = new BorderLayout();
 
@@ -708,7 +848,7 @@ public class AlignmentPanel extends Panel implements AdjustmentListener
     // 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);
@@ -828,47 +968,16 @@ public class AlignmentPanel extends Panel implements AdjustmentListener
     oldrfs.clear();
     adjustAnnotationHeight();
   }
-  /**
-   * 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();
-    int height = annotationPanel.adjustPanelHeight();
-    if (hscroll.isVisible())
-    {
-      height += hscroll.getPreferredSize().height;
-    }
-    int mheight=height;
-    // sets initial preferred height
-    if (height > alignFrame.getHeight() / 2)
-    {
-      height = alignFrame.getHeight() / 2;
-    }
-    apvscroll.setValues(apvscroll.getValue(), height, 0, mheight);
-    annotationPanel.setScrollOffset(apvscroll.getValue());
-    alabels.setScrollOffset(apvscroll.getValue());
-    apvscroll.addNotify();
-    hscroll.addNotify();
 
-    annotationPanelHolder.setSize(new Dimension(annotationPanel
-            .getSize().width, height));
-
-    annotationSpaceFillerHolder.setSize(new Dimension(
-            annotationSpaceFillerHolder.getSize().width
-            , height));
-    
-    //annotationScroller.validate();// repaint();
-    validate();
-    addNotify();
-    repaint();
+  @Override
+  public AlignmentI getAlignment()
+  {
+    return av.alignment;
+  }
+  @Override
+  public StructureSelectionManager getStructureSelectionManager()
+  {
+    return StructureSelectionManager.getStructureSelectionManager(av.applet);
   }
 
 }