2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6)
3 * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
18 package jalview.appletgui;
21 import java.awt.event.*;
22 import java.util.Hashtable;
23 import java.util.Vector;
25 import jalview.api.AlignmentViewPanel;
26 import jalview.datamodel.*;
27 import jalview.structure.StructureSelectionManager;
29 public class AlignmentPanel extends Panel implements AdjustmentListener, AlignmentViewPanel
32 public AlignViewport av;
34 OverviewPanel overviewPanel;
40 IdwidthAdjuster idwidthAdjuster;
42 public AlignFrame alignFrame;
44 ScalePanel scalePanel;
46 AnnotationPanel annotationPanel;
48 AnnotationLabels alabels;
50 // this value is set false when selection area being dragged
51 boolean fastPaint = true;
53 public void finalize() {
58 sequenceHolderPanel=null;
60 scalePanelHolder=null;
62 annotationPanelHolder=null;
63 annotationSpaceFillerHolder=null;
65 public AlignmentPanel(AlignFrame af, final AlignViewport av)
77 seqPanel = new SeqPanel(av, this);
78 idPanel = new IdPanel(av, this);
79 scalePanel = new ScalePanel(av, this);
80 idwidthAdjuster = new IdwidthAdjuster(this);
81 annotationPanel = new AnnotationPanel(this);
82 annotationPanelHolder.add(annotationPanel, BorderLayout.CENTER);
84 sequenceHolderPanel.add(annotationPanelHolder, BorderLayout.SOUTH);
86 alabels = new AnnotationLabels(this);
88 setAnnotationVisible(av.showAnnotation);
90 idPanelHolder.add(idPanel, BorderLayout.CENTER);
91 idSpaceFillerPanel1.add(idwidthAdjuster, BorderLayout.CENTER);
92 annotationSpaceFillerHolder.add(alabels, BorderLayout.CENTER);
93 scalePanelHolder.add(scalePanel, BorderLayout.CENTER);
94 seqPanelHolder.add(seqPanel, BorderLayout.CENTER);
97 setScrollValues(0, 0);
99 apvscroll.addAdjustmentListener(this);
100 hscroll.addAdjustmentListener(this);
101 vscroll.addAdjustmentListener(this);
103 addComponentListener(new ComponentAdapter()
105 public void componentResized(ComponentEvent evt)
107 setScrollValues(av.getStartRes(), av.getStartSeq());
108 if (getSize().height>0 && annotationPanelHolder.getSize().height>0) {
109 validateAnnotationDimensions(false);
116 Dimension d = calculateIdWidth();
117 idPanel.idCanvas.setSize(d);
119 hscrollFillerPanel.setSize(d.width, annotationPanel.getSize().height);
121 idPanel.idCanvas.setSize(d.width, seqPanel.seqCanvas.getSize().height);
122 annotationSpaceFillerHolder.setSize(d.width,
123 annotationPanel.getSize().height);
124 alabels.setSize(d.width, annotationPanel.getSize().height);
125 final AlignmentPanel ap = this;
126 av.addPropertyChangeListener(new java.beans.PropertyChangeListener()
128 public void propertyChange(java.beans.PropertyChangeEvent evt)
130 if (evt.getPropertyName().equals("alignment"))
132 PaintRefresher.Refresh(ap, av.getSequenceSetId(), true, true);
140 public SequenceRenderer getSequenceRenderer()
142 return seqPanel.seqCanvas.sr;
145 public FeatureRenderer getFeatureRenderer()
147 return seqPanel.seqCanvas.fr;
150 public void alignmentChanged()
152 av.alignmentChanged(this);
154 if (overviewPanel != null)
156 overviewPanel.updateOverviewImage();
159 alignFrame.updateEditMenuBar();
164 public void fontChanged()
166 // set idCanvas bufferedImage to null
167 // to prevent drawing old image
168 idPanel.idCanvas.image = null;
169 FontMetrics fm = getFontMetrics(av.getFont());
171 scalePanel.setSize(new Dimension(10, av.charHeight + fm.getDescent()));
172 idwidthAdjuster.setSize(new Dimension(10, av.charHeight
174 av.updateSequenceIdColours();
175 annotationPanel.image = null;
176 int ap = annotationPanel.adjustPanelHeight(false);
177 Dimension d = calculateIdWidth();
178 d.setSize(d.width + 4, seqPanel.seqCanvas.getSize().height);
179 alabels.setSize(d.width + 4, ap);
181 idPanel.idCanvas.setSize(d);
182 hscrollFillerPanel.setSize(d);
184 validateAnnotationDimensions(false);
185 annotationPanel.repaint();
189 if (overviewPanel != null)
191 overviewPanel.updateOverviewImage();
195 public void setIdWidth(int w, int h)
197 idPanel.idCanvas.setSize(w, h);
198 idPanelHolder.setSize(w, idPanelHolder.getSize().height);
199 annotationSpaceFillerHolder.setSize(w,annotationSpaceFillerHolder.getSize().height);
200 alabels.setSize(w, alabels.getSize().height);
204 Dimension calculateIdWidth()
206 if (av.nullFrame == null)
208 av.nullFrame = new Frame();
209 av.nullFrame.addNotify();
212 Graphics g = av.nullFrame.getGraphics();
214 FontMetrics fm = g.getFontMetrics(av.font);
215 AlignmentI al = av.getAlignment();
220 while (i < al.getHeight() && al.getSequenceAt(i) != null)
222 SequenceI s = al.getSequenceAt(i);
223 id = s.getDisplayId(av.getShowJVSuffix());
225 if (fm.stringWidth(id) > idWidth)
227 idWidth = fm.stringWidth(id);
232 // Also check annotation label widths
234 if (al.getAlignmentAnnotation() != null)
236 fm = g.getFontMetrics(av.nullFrame.getFont());
237 while (i < al.getAlignmentAnnotation().length)
239 String label = al.getAlignmentAnnotation()[i].label;
240 if (fm.stringWidth(label) > idWidth)
242 idWidth = fm.stringWidth(label);
248 return new Dimension(idWidth, idPanel.idCanvas.getSize().height);
252 * Highlight the given results on the alignment.
255 public void highlightSearchResults(SearchResults results)
257 scrollToPosition(results);
258 seqPanel.seqCanvas.highlightSearchResults(results);
262 * scroll the view to show the position of the highlighted region in results
263 * (if any) and redraw the overview
266 * @return false if results were not found
268 public boolean scrollToPosition(SearchResults results)
270 return scrollToPosition(results, true);
274 * scroll the view to show the position of the highlighted region in results
278 * @param redrawOverview
279 * - when set, the overview will be recalculated (takes longer)
280 * @return false if results were not found
282 public boolean scrollToPosition(SearchResults results,
283 boolean redrawOverview)
285 // do we need to scroll the panel?
286 if (results != null && results.getSize() > 0)
288 int seqIndex = av.alignment.findIndex(results);
293 SequenceI seq = av.alignment.getSequenceAt(seqIndex);
294 int[] r = results.getResults(seq, 0,av.alignment.getWidth());
297 if (av.applet.debug) {// DEBUG
298 System.out.println("DEBUG: scroll didn't happen - results not within alignment : "+seq.getStart()+","+seq.getEnd());
302 if (av.applet.debug) {
304 System.out.println("DEBUG: scroll: start=" + r[0]
305 + " av.getStartRes()=" + av.getStartRes() + " end=" + r[1]
306 + " seq.end=" + seq.getEnd() + " av.getEndRes()="
307 + av.getEndRes() + " hextent=" + hextent);
315 if (end == seq.getEnd())
319 return scrollTo(start, end, seqIndex, false, redrawOverview);
323 public boolean scrollTo(int ostart, int end, int seqIndex, boolean scrollToNearest, boolean redrawOverview)
326 if (av.hasHiddenColumns)
328 start = av.getColumnSelection().findColumnPosition(ostart);
329 end = av.getColumnSelection().findColumnPosition(end);
332 if (!scrollToNearest && !av.colSel.isVisible(ostart))
334 // don't scroll - position isn't visible
339 if (!av.wrapAlignment)
341 if ((av.getStartRes() > end)
342 || (av.getEndRes() < start)
343 || ((av.getStartSeq() > seqIndex) || (av.getEndSeq() < seqIndex)))
345 if (start > av.alignment.getWidth() - hextent)
347 start = av.alignment.getWidth() - hextent;
354 if (seqIndex > av.alignment.getHeight() - vextent)
356 seqIndex = av.alignment.getHeight() - vextent;
362 setScrollValues(start, seqIndex);
367 scrollToWrappedVisible(start);
369 if (redrawOverview && overviewPanel != null)
371 overviewPanel.setBoxPosition();
373 paintAlignment(redrawOverview);
377 void scrollToWrappedVisible(int res)
379 int cwidth = seqPanel.seqCanvas
380 .getWrappedCanvasWidth(seqPanel.seqCanvas.getSize().width);
381 if (res <= av.getStartRes() || res >= (av.getStartRes() + cwidth))
383 vscroll.setValue(res / cwidth);
384 av.startRes = vscroll.getValue() * cwidth;
388 public OverviewPanel getOverviewPanel()
390 return overviewPanel;
393 public void setOverviewPanel(OverviewPanel op)
398 public void setAnnotationVisible(boolean b)
400 if (!av.wrapAlignment)
402 annotationSpaceFillerHolder.setVisible(b);
403 annotationPanelHolder.setVisible(b);
410 * automatically adjust annotation panel height for new annotation whilst
411 * ensuring the alignment is still visible.
413 public void adjustAnnotationHeight()
415 // TODO: display vertical annotation scrollbar if necessary
416 // this is called after loading new annotation onto alignment
417 if (alignFrame.getSize().height == 0)
419 System.out.println("NEEDS FIXING");
422 validateAnnotationDimensions(true);
423 apvscroll.addNotify();
429 * calculate the annotation dimensions and refresh slider values accordingly.
430 * need to do repaints/notifys afterwards.
432 protected void validateAnnotationDimensions(boolean adjustPanelHeight) {
433 boolean modified=false;
434 int height = annotationPanel.calcPanelHeight();
436 if (hscroll.isVisible())
438 height += (minsize=hscroll.getPreferredSize().height);
440 if (apvscroll.isVisible()) {
441 minsize+=apvscroll.getPreferredSize().height;
443 int mheight = height;
444 Dimension d=sequenceHolderPanel.getSize(),e=idPanel.getSize();
445 int seqandannot=d.height-scalePanelHolder.getSize().height;
446 // sets initial preferred height
447 if ((height+40) > seqandannot / 2)
449 height = seqandannot / 2;
451 if (!adjustPanelHeight)
453 // maintain same window layout whilst updating sliders
454 height=annotationPanelHolder.getSize().height;
457 if (seqandannot-height<5)
459 height = seqandannot;
461 annotationPanel.setSize(new Dimension(d.width,height));
462 alabels.setSize(new Dimension(e.width,height));
463 annotationSpaceFillerHolder.setSize(new Dimension(e.width, height));
464 annotationPanelHolder.setSize(new Dimension(d.width, height));
465 seqPanelHolder.setSize(d.width,seqandannot-height);
466 int s=apvscroll.getValue();
467 if (s>mheight-height)
471 apvscroll.setValues(s, height, 0, mheight);
472 annotationPanel.setScrollOffset(apvscroll.getValue());
473 alabels.setScrollOffset(apvscroll.getValue());
476 public void setWrapAlignment(boolean wrap)
480 scalePanelHolder.setVisible(!wrap);
482 hscroll.setVisible(!wrap);
483 idwidthAdjuster.setVisible(!wrap);
487 annotationPanelHolder.setVisible(false);
488 annotationSpaceFillerHolder.setVisible(false);
490 else if (av.showAnnotation)
492 annotationPanelHolder.setVisible(true);
493 annotationSpaceFillerHolder.setVisible(true);
496 idSpaceFillerPanel1.setVisible(!wrap);
498 fontChanged(); // This is so that the scalePanel is resized correctly
509 // return value is true if the scroll is valid
510 public boolean scrollUp(boolean up)
514 if (vscroll.getValue() < 1)
518 setScrollValues(hscroll.getValue(), vscroll.getValue() - 1);
522 if (vextent + vscroll.getValue() >= av.getAlignment().getHeight())
526 setScrollValues(hscroll.getValue(), vscroll.getValue() + 1);
533 public boolean scrollRight(boolean right)
537 if (hscroll.getValue() < 1)
541 setScrollValues(hscroll.getValue() - 1, vscroll.getValue());
545 if (hextent + hscroll.getValue() >= av.getAlignment().getWidth())
549 setScrollValues(hscroll.getValue() + 1, vscroll.getValue());
556 public void setScrollValues(int x, int y)
558 int width = av.alignment.getWidth();
559 int height = av.alignment.getHeight();
561 if (av.hasHiddenColumns)
563 width = av.getColumnSelection().findColumnPosition(width);
567 av.setEndRes((x + (seqPanel.seqCanvas.getSize().width / av.charWidth)) - 1);
569 hextent = seqPanel.seqCanvas.getSize().width / av.charWidth;
570 vextent = seqPanel.seqCanvas.getSize().height / av.charHeight;
577 if (vextent > height)
582 if ((hextent + x) > width)
587 if ((vextent + y) > height)
589 y = height - vextent;
604 int endSeq = y + vextent;
605 if (endSeq > av.alignment.getHeight())
607 endSeq = av.alignment.getHeight();
610 av.setEndSeq(endSeq);
611 hscroll.setValues(x, hextent, 0, width);
612 vscroll.setValues(y, vextent, 0, height);
614 if (overviewPanel != null)
616 overviewPanel.setBoxPosition();
623 public void adjustmentValueChanged(AdjustmentEvent evt)
625 int oldX = av.getStartRes();
626 int oldY = av.getStartSeq();
628 if (evt == null || evt.getSource() == apvscroll)
630 annotationPanel.setScrollOffset(apvscroll.getValue());
631 alabels.setScrollOffset(apvscroll.getValue());
632 // annotationPanel.image=null;
633 // alabels.image=null;
634 // alabels.repaint();
635 // annotationPanel.repaint();
637 if (evt == null || evt.getSource() == hscroll)
639 int x = hscroll.getValue();
641 av.setEndRes(x + seqPanel.seqCanvas.getSize().width
642 / av.getCharWidth() - 1);
645 if (evt == null || evt.getSource() == vscroll)
647 int offy = vscroll.getValue();
648 if (av.getWrapAlignment())
650 int rowSize = seqPanel.seqCanvas
651 .getWrappedCanvasWidth(seqPanel.seqCanvas.getSize().width);
652 av.setStartRes(vscroll.getValue() * rowSize);
653 av.setEndRes((vscroll.getValue() + 1) * rowSize);
657 av.setStartSeq(offy);
658 av.setEndSeq(offy + seqPanel.seqCanvas.getSize().height
659 / av.getCharHeight());
663 if (overviewPanel != null)
665 overviewPanel.setBoxPosition();
668 int scrollX = av.startRes - oldX;
669 int scrollY = av.startSeq - oldY;
671 if (av.getWrapAlignment() || !fastPaint || av.MAC)
677 // Make sure we're not trying to draw a panel
678 // larger than the visible window
679 if (scrollX > av.endRes - av.startRes)
681 scrollX = av.endRes - av.startRes;
683 else if (scrollX < av.startRes - av.endRes)
685 scrollX = av.startRes - av.endRes;
688 idPanel.idCanvas.fastPaint(scrollY);
689 seqPanel.seqCanvas.fastPaint(scrollX, scrollY);
691 scalePanel.repaint();
692 if (av.getShowAnnotation())
694 annotationPanel.fastPaint(av.getStartRes() - oldX);
700 private void sendViewPosition()
702 StructureSelectionManager.getStructureSelectionManager(av.applet).sendViewPosition(this, av.startRes, av.endRes, av.startSeq, av.endSeq);
705 public void paintAlignment(boolean updateOverview)
711 jalview.structure.StructureSelectionManager
712 .getStructureSelectionManager(av.applet).sequenceColoursChanged(this);
714 if (overviewPanel != null)
716 overviewPanel.updateOverviewImage();
721 public void update(Graphics g)
726 public void paint(Graphics g)
729 Dimension d = idPanel.idCanvas.getSize();
730 idPanel.idCanvas.setSize(d.width, seqPanel.seqCanvas.getSize().height);
732 if (av.getWrapAlignment())
734 int maxwidth = av.alignment.getWidth();
736 if (av.hasHiddenColumns)
738 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
741 int canvasWidth = seqPanel.seqCanvas
742 .getWrappedCanvasWidth(seqPanel.seqCanvas.getSize().width);
746 int max = maxwidth / canvasWidth;
747 vscroll.setMaximum(1 + max);
748 vscroll.setUnitIncrement(1);
749 vscroll.setVisibleAmount(1);
754 setScrollValues(av.getStartRes(), av.getStartSeq());
759 seqPanel.seqCanvas.repaint();
760 scalePanel.repaint();
761 annotationPanel.repaint();
762 idPanel.idCanvas.repaint();
765 protected Panel sequenceHolderPanel = new Panel();
767 protected Scrollbar vscroll = new Scrollbar();
769 protected Scrollbar hscroll = new Scrollbar();
771 protected Panel seqPanelHolder = new Panel();
773 BorderLayout borderLayout1 = new BorderLayout();
775 BorderLayout borderLayout3 = new BorderLayout();
777 protected Panel scalePanelHolder = new Panel();
779 protected Panel idPanelHolder = new Panel();
781 BorderLayout borderLayout5 = new BorderLayout();
783 protected Panel idSpaceFillerPanel1 = new Panel();
785 public Panel annotationSpaceFillerHolder = new Panel();
787 BorderLayout borderLayout6 = new BorderLayout();
789 BorderLayout borderLayout7 = new BorderLayout();
791 Panel hscrollHolder = new Panel();
793 BorderLayout borderLayout10 = new BorderLayout();
795 protected Panel hscrollFillerPanel = new Panel();
797 BorderLayout borderLayout11 = new BorderLayout();
799 BorderLayout borderLayout4 = new BorderLayout();
801 BorderLayout borderLayout2 = new BorderLayout();
803 Panel annotationPanelHolder = new Panel();
805 protected Scrollbar apvscroll = new Scrollbar();
807 BorderLayout borderLayout12 = new BorderLayout();
809 private void jbInit() throws Exception
811 // idPanelHolder.setPreferredSize(new Dimension(70, 10));
812 this.setLayout(borderLayout7);
814 //sequenceHolderPanel.setPreferredSize(new Dimension(150, 150));
815 sequenceHolderPanel.setLayout(borderLayout3);
816 seqPanelHolder.setLayout(borderLayout1);
817 scalePanelHolder.setBackground(Color.white);
819 // scalePanelHolder.setPreferredSize(new Dimension(10, 30));
820 scalePanelHolder.setLayout(borderLayout6);
821 idPanelHolder.setLayout(borderLayout5);
822 idSpaceFillerPanel1.setBackground(Color.white);
824 // idSpaceFillerPanel1.setPreferredSize(new Dimension(10, 30));
825 idSpaceFillerPanel1.setLayout(borderLayout11);
826 annotationSpaceFillerHolder.setBackground(Color.white);
828 // annotationSpaceFillerHolder.setPreferredSize(new Dimension(10, 80));
829 annotationSpaceFillerHolder.setLayout(borderLayout4);
830 hscroll.setOrientation(Scrollbar.HORIZONTAL);
831 hscrollHolder.setLayout(borderLayout10);
832 hscrollFillerPanel.setBackground(Color.white);
833 apvscroll.setOrientation(Scrollbar.VERTICAL);
834 apvscroll.setVisible(true);
835 apvscroll.addAdjustmentListener(this);
837 annotationPanelHolder.setBackground(Color.white);
838 annotationPanelHolder.setLayout(borderLayout12);
839 annotationPanelHolder.add(apvscroll, BorderLayout.EAST);
840 // hscrollFillerPanel.setPreferredSize(new Dimension(70, 10));
841 hscrollHolder.setBackground(Color.white);
843 // annotationScroller.setPreferredSize(new Dimension(10, 80));
844 // this.setPreferredSize(new Dimension(220, 166));
845 seqPanelHolder.setBackground(Color.white);
846 idPanelHolder.setBackground(Color.white);
847 sequenceHolderPanel.add(scalePanelHolder, BorderLayout.NORTH);
848 sequenceHolderPanel.add(seqPanelHolder, BorderLayout.CENTER);
849 seqPanelHolder.add(vscroll, BorderLayout.EAST);
851 // Panel3.add(secondaryPanelHolder, BorderLayout.SOUTH);
852 this.add(idPanelHolder, BorderLayout.WEST);
853 idPanelHolder.add(idSpaceFillerPanel1, BorderLayout.NORTH);
854 idPanelHolder.add(annotationSpaceFillerHolder, BorderLayout.SOUTH);
855 this.add(hscrollHolder, BorderLayout.SOUTH);
856 hscrollHolder.add(hscroll, BorderLayout.CENTER);
857 hscrollHolder.add(hscrollFillerPanel, BorderLayout.WEST);
858 this.add(sequenceHolderPanel, BorderLayout.CENTER);
862 * hides or shows dynamic annotation rows based on groups and av state flags
864 public void updateAnnotation()
866 updateAnnotation(false);
869 public void updateAnnotation(boolean applyGlobalSettings)
871 // TODO: this should be merged with other annotation update stuff - that
872 // sits on AlignViewport
873 boolean updateCalcs = false;
874 boolean conv = av.isShowGroupConservation();
875 boolean cons = av.isShowGroupConsensus();
876 boolean showprf = av.isShowSequenceLogo();
877 boolean showConsHist = av.isShowConsensusHistogram();
879 boolean sortg = true;
881 // remove old automatic annotation
882 // add any new annotation
884 Vector gr = av.alignment.getGroups(); // OrderedBy(av.alignment.getSequencesArray());
885 // intersect alignment annotation with alignment groups
887 AlignmentAnnotation[] aan = av.alignment.getAlignmentAnnotation();
888 Hashtable oldrfs = new Hashtable();
891 for (int an = 0; an < aan.length; an++)
893 if (aan[an].autoCalculated && aan[an].groupRef != null)
895 oldrfs.put(aan[an].groupRef, aan[an].groupRef);
896 av.alignment.deleteAnnotation(aan[an]);
904 for (int g = 0; g < gr.size(); g++)
907 sg = (SequenceGroup) gr.elementAt(g);
908 if (applyGlobalSettings || !oldrfs.containsKey(sg))
910 // set defaults for this group's conservation/consensus
911 sg.setshowSequenceLogo(showprf);
912 sg.setShowConsensusHistogram(showConsHist);
917 av.alignment.addAnnotation(sg.getConservationRow(), 0);
922 av.alignment.addAnnotation(sg.getConsensus(), 0);
924 // refresh the annotation rows
927 sg.recalcConservation();
932 adjustAnnotationHeight();
936 public AlignmentI getAlignment()
941 public StructureSelectionManager getStructureSelectionManager()
943 return StructureSelectionManager.getStructureSelectionManager(av.applet);