1.2ish awt methods
[jalview.git] / src / jalview / appletgui / AlignmentPanel.java
index 0519f47..5373894 100755 (executable)
@@ -19,6 +19,8 @@ package jalview.appletgui;
 
 import java.awt.*;
 import java.awt.event.*;
+import java.util.Hashtable;
+import java.util.Vector;
 
 import jalview.datamodel.*;
 
@@ -62,9 +64,15 @@ 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);
-
-    sequenceHolderPanel.add(annotationPanel, BorderLayout.SOUTH);
+    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);
 
@@ -75,10 +83,11 @@ 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);
 
+    apvscroll.addAdjustmentListener(this);
     hscroll.addAdjustmentListener(this);
     vscroll.addAdjustmentListener(this);
 
@@ -100,7 +109,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()
     {
@@ -152,7 +160,7 @@ public class AlignmentPanel extends Panel implements AdjustmentListener
             + fm.getDescent()));
     av.updateSequenceIdColours();
     annotationPanel.image = null;
-    int ap = annotationPanel.adjustPanelHeight();
+    int ap = annotationPanel.adjustPanelHeight(false);
     annotationPanel.repaint();
     Dimension d = calculateIdWidth();
     d.setSize(d.width + 4, seqPanel.seqCanvas.getSize().height);
@@ -230,16 +238,33 @@ public class AlignmentPanel extends Panel implements AdjustmentListener
    */
   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 void scrollToPosition(SearchResults results)
+  public boolean scrollToPosition(SearchResults results,
+          boolean redrawOverview)
   {
     // do we need to scroll the panel?
     if (results != null && results.getSize() > 0)
@@ -247,34 +272,34 @@ public class AlignmentPanel extends Panel implements AdjustmentListener
       int seqIndex = av.alignment.findIndex(results);
       if (seqIndex == -1)
       {
-        return;
+        return false;
       }
       SequenceI seq = av.alignment.getSequenceAt(seqIndex);
       int[] r = results.getResults(seq, seq.getStart(), seq.getEnd());
       if (r == null)
       {
-        return;
+        return false;
       }
       int start = r[0];
       int end = r[1];
       if (start < 0)
       {
-        return;
+        return false;
       }
       if (end == seq.getEnd())
       {
-        return;
+        return false;
       }
       if (av.hasHiddenColumns)
       {
         start = av.getColumnSelection().findColumnPosition(start);
         end = av.getColumnSelection().findColumnPosition(end);
-        if (start==end)
+        if (start == end)
         {
           if (!av.colSel.isVisible(r[0]))
           {
             // don't scroll - position isn't visible
-            return;
+            return false;
           }
         }
       }
@@ -308,8 +333,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)
@@ -494,6 +523,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();
@@ -583,10 +621,11 @@ 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, annotationPanel.getSize().height);
+    alabels.setSize(d.width, annotationPanelHolder.getSize().height);
 
     if (av.getWrapAlignment())
     {
@@ -603,7 +642,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);
       }
@@ -659,6 +698,11 @@ 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));
@@ -683,7 +727,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);
 
@@ -705,4 +755,120 @@ 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();
+  }
+  /**
+   * 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.getSize().height/ 2)
+    {
+      height = alignFrame.getSize().height / 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();
+  }
+
 }