ensure annotation height adjuster doesn't disappear off top of screen on
[jalview.git] / src / jalview / appletgui / AlignmentPanel.java
old mode 100755 (executable)
new mode 100644 (file)
index c466b86..60e8cb1
@@ -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
@@ -91,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();
@@ -272,7 +290,7 @@ 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
@@ -282,7 +300,7 @@ public class AlignmentPanel extends Panel implements AdjustmentListener
       }
       if (av.applet.debug) {
         // DEBUG
-        System.out.println("DEBUG: scroll didn't happen: start=" + r[0]
+        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);
@@ -297,17 +315,24 @@ 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 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)
@@ -340,7 +365,6 @@ public class AlignmentPanel extends Panel implements AdjustmentListener
       {
         scrollToWrappedVisible(start);
       }
-    }
     if (redrawOverview && overviewPanel != null)
     {
       overviewPanel.setBoxPosition();
@@ -380,7 +404,74 @@ public class AlignmentPanel extends Panel implements AdjustmentListener
     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();
+    repaint();
+  }
+  /**
+   * 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);
+    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;
@@ -523,6 +614,8 @@ public class AlignmentPanel extends Panel implements AdjustmentListener
     {
       overviewPanel.setBoxPosition();
     }
+    sendViewPosition();
+
 
   }
 
@@ -600,8 +693,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)
   {
@@ -610,7 +708,7 @@ public class AlignmentPanel extends Panel implements AdjustmentListener
     if (updateOverview)
     {
       jalview.structure.StructureSelectionManager
-              .getStructureSelectionManager().sequenceColoursChanged(this);
+              .getStructureSelectionManager(av.applet).sequenceColoursChanged(this);
 
       if (overviewPanel != null)
       {
@@ -833,61 +931,15 @@ public class AlignmentPanel extends Panel implements AdjustmentListener
     adjustAnnotationHeight();
   }
 
-  /**
-   * automatically adjust annotation panel height for new annotation whilst
-   * ensuring the alignment is still visible.
-   */
-  public void adjustAnnotationHeight()
+  @Override
+  public AlignmentI getAlignment()
   {
-    // 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();
+    return av.alignment;
   }
-  /**
-   * 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());
+  @Override
+  public StructureSelectionManager getStructureSelectionManager()
+  {
+    return StructureSelectionManager.getStructureSelectionManager(av.applet);
   }
 
 }