moved to same place as in jalview.gui
[jalview.git] / src / jalview / appletgui / AlignmentPanel.java
index d8e03f7..e4b9b59 100755 (executable)
@@ -22,9 +22,10 @@ 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,14 +65,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 +79,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);
 
@@ -161,13 +157,14 @@ 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 +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();
   }
@@ -275,11 +273,21 @@ 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 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)
@@ -316,6 +324,7 @@ public class AlignmentPanel extends Panel implements AdjustmentListener
             {
               start = 0;
             }
+
           }
           if (seqIndex > av.alignment.getHeight() - vextent)
           {
@@ -367,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;
@@ -384,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);
     }
 
@@ -468,7 +534,7 @@ public class AlignmentPanel extends Panel implements AdjustmentListener
 
     hextent = seqPanel.seqCanvas.getSize().width / av.charWidth;
     vextent = seqPanel.seqCanvas.getSize().height / av.charHeight;
-
+    
     if (hextent > width)
     {
       hextent = width;
@@ -523,14 +589,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)
     {
@@ -621,12 +687,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 +703,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 +760,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 +770,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,46 +890,11 @@ 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.getHeight() == 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.setPreferredSize(new Dimension(annotationPanel
-            .getWidth(), height));
 
-    annotationSpaceFillerHolder.setPreferredSize(new Dimension(
-            annotationSpaceFillerHolder.getWidth(), height));
-    
-    //annotationScroller.validate();// repaint();
-    validate();
-    addNotify();
-    repaint();
+  @Override
+  public AlignmentI getAlignment()
+  {
+    return av.alignment;
   }
 
 }