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.datamodel.*;
27 public class AlignmentPanel extends Panel implements AdjustmentListener
30 public AlignViewport av;
32 OverviewPanel overviewPanel;
38 IdwidthAdjuster idwidthAdjuster;
40 public AlignFrame alignFrame;
42 ScalePanel scalePanel;
44 AnnotationPanel annotationPanel;
46 AnnotationLabels alabels;
48 // this value is set false when selection area being dragged
49 boolean fastPaint = true;
51 public AlignmentPanel(AlignFrame af, final AlignViewport av)
63 seqPanel = new SeqPanel(av, this);
64 idPanel = new IdPanel(av, this);
65 scalePanel = new ScalePanel(av, this);
66 idwidthAdjuster = new IdwidthAdjuster(this);
67 // annotationScroller = new ScrollPane();
68 // annotationScroller.setBackground(Color.white);
69 annotationPanel = new AnnotationPanel(this);
70 annotationPanelHolder.add(annotationPanel, BorderLayout.CENTER);
71 // annotationScroller.setPreferredSize(new Dimension(10,80));
72 // annotationScroller.add(annotationPanel);
73 // annotationScroller.getInsets().set(0,0,0,0);
75 sequenceHolderPanel.add(annotationPanelHolder, BorderLayout.SOUTH);
77 alabels = new AnnotationLabels(this);
79 setAnnotationVisible(av.showAnnotation);
81 idPanelHolder.add(idPanel, BorderLayout.CENTER);
82 idSpaceFillerPanel1.add(idwidthAdjuster, BorderLayout.CENTER);
83 annotationSpaceFillerHolder.add(alabels, BorderLayout.CENTER);
84 scalePanelHolder.add(scalePanel, BorderLayout.CENTER);
85 seqPanelHolder.add(seqPanel, BorderLayout.CENTER);
88 setScrollValues(0, 0);
90 apvscroll.addAdjustmentListener(this);
91 hscroll.addAdjustmentListener(this);
92 vscroll.addAdjustmentListener(this);
94 addComponentListener(new ComponentAdapter()
96 public void componentResized(ComponentEvent evt)
98 setScrollValues(av.getStartRes(), av.getStartSeq());
103 Dimension d = calculateIdWidth();
104 idPanel.idCanvas.setSize(d);
106 hscrollFillerPanel.setSize(d.width, annotationPanel.getSize().height);
108 idPanel.idCanvas.setSize(d.width, seqPanel.seqCanvas.getSize().height);
109 annotationSpaceFillerHolder.setSize(d.width,
110 annotationPanel.getSize().height);
111 alabels.setSize(d.width, annotationPanel.getSize().height);
112 final AlignmentPanel ap = this;
113 av.addPropertyChangeListener(new java.beans.PropertyChangeListener()
115 public void propertyChange(java.beans.PropertyChangeEvent evt)
117 if (evt.getPropertyName().equals("alignment"))
119 PaintRefresher.Refresh(ap, av.getSequenceSetId(), true, true);
127 public SequenceRenderer getSequenceRenderer()
129 return seqPanel.seqCanvas.sr;
132 public FeatureRenderer getFeatureRenderer()
134 return seqPanel.seqCanvas.fr;
137 public void alignmentChanged()
139 av.alignmentChanged(this);
141 if (overviewPanel != null)
143 overviewPanel.updateOverviewImage();
146 alignFrame.updateEditMenuBar();
151 public void fontChanged()
153 // set idCanvas bufferedImage to null
154 // to prevent drawing old image
155 idPanel.idCanvas.image = null;
156 FontMetrics fm = getFontMetrics(av.getFont());
158 scalePanel.setSize(new Dimension(10, av.charHeight + fm.getDescent()));
159 idwidthAdjuster.setSize(new Dimension(10, av.charHeight
161 av.updateSequenceIdColours();
162 annotationPanel.image = null;
163 int ap = annotationPanel.adjustPanelHeight(false);
164 annotationPanel.repaint();
165 Dimension d = calculateIdWidth();
166 d.setSize(d.width + 4, seqPanel.seqCanvas.getSize().height);
167 alabels.setSize(d.width + 4, ap);
168 idPanel.idCanvas.setSize(d);
169 hscrollFillerPanel.setSize(d);
174 if (overviewPanel != null)
176 overviewPanel.updateOverviewImage();
180 public void setIdWidth(int w, int h)
182 idPanel.idCanvas.setSize(w, h);
183 idPanelHolder.setSize(w, idPanelHolder.getSize().height);
184 alabels.setSize(w, alabels.getSize().height);
188 Dimension calculateIdWidth()
190 if (av.nullFrame == null)
192 av.nullFrame = new Frame();
193 av.nullFrame.addNotify();
196 Graphics g = av.nullFrame.getGraphics();
198 FontMetrics fm = g.getFontMetrics(av.font);
199 AlignmentI al = av.getAlignment();
204 while (i < al.getHeight() && al.getSequenceAt(i) != null)
206 SequenceI s = al.getSequenceAt(i);
207 id = s.getDisplayId(av.getShowJVSuffix());
209 if (fm.stringWidth(id) > idWidth)
211 idWidth = fm.stringWidth(id);
216 // Also check annotation label widths
218 if (al.getAlignmentAnnotation() != null)
220 fm = g.getFontMetrics(av.nullFrame.getFont());
221 while (i < al.getAlignmentAnnotation().length)
223 String label = al.getAlignmentAnnotation()[i].label;
224 if (fm.stringWidth(label) > idWidth)
226 idWidth = fm.stringWidth(label);
232 return new Dimension(idWidth, idPanel.idCanvas.getSize().height);
236 * Highlight the given results on the alignment.
239 public void highlightSearchResults(SearchResults results)
241 scrollToPosition(results);
242 seqPanel.seqCanvas.highlightSearchResults(results);
246 * scroll the view to show the position of the highlighted region in results
247 * (if any) and redraw the overview
250 * @return false if results were not found
252 public boolean scrollToPosition(SearchResults results)
254 return scrollToPosition(results, true);
258 * scroll the view to show the position of the highlighted region in results
262 * @param redrawOverview
263 * - when set, the overview will be recalculated (takes longer)
264 * @return false if results were not found
266 public boolean scrollToPosition(SearchResults results,
267 boolean redrawOverview)
269 // do we need to scroll the panel?
270 if (results != null && results.getSize() > 0)
272 int seqIndex = av.alignment.findIndex(results);
277 SequenceI seq = av.alignment.getSequenceAt(seqIndex);
278 int[] r = results.getResults(seq, seq.getStart(), seq.getEnd());
289 if (end == seq.getEnd())
293 if (av.hasHiddenColumns)
295 start = av.getColumnSelection().findColumnPosition(start);
296 end = av.getColumnSelection().findColumnPosition(end);
299 if (!av.colSel.isVisible(r[0]))
301 // don't scroll - position isn't visible
306 if (!av.wrapAlignment)
308 if ((av.getStartRes() > end)
309 || (av.getEndRes() < start)
310 || ((av.getStartSeq() > seqIndex) || (av.getEndSeq() < seqIndex)))
312 if (start > av.alignment.getWidth() - hextent)
314 start = av.alignment.getWidth() - hextent;
320 if (seqIndex > av.alignment.getHeight() - vextent)
322 seqIndex = av.alignment.getHeight() - vextent;
328 setScrollValues(start, seqIndex);
333 scrollToWrappedVisible(start);
336 if (redrawOverview && overviewPanel != null)
338 overviewPanel.setBoxPosition();
340 paintAlignment(redrawOverview);
344 void scrollToWrappedVisible(int res)
346 int cwidth = seqPanel.seqCanvas
347 .getWrappedCanvasWidth(seqPanel.seqCanvas.getSize().width);
348 if (res <= av.getStartRes() || res >= (av.getStartRes() + cwidth))
350 vscroll.setValue(res / cwidth);
351 av.startRes = vscroll.getValue() * cwidth;
355 public OverviewPanel getOverviewPanel()
357 return overviewPanel;
360 public void setOverviewPanel(OverviewPanel op)
365 public void setAnnotationVisible(boolean b)
367 if (!av.wrapAlignment)
369 annotationSpaceFillerHolder.setVisible(b);
370 annotationPanel.setVisible(b);
376 public void setWrapAlignment(boolean wrap)
380 scalePanelHolder.setVisible(!wrap);
382 hscroll.setVisible(!wrap);
383 idwidthAdjuster.setVisible(!wrap);
387 annotationPanel.setVisible(false);
388 annotationSpaceFillerHolder.setVisible(false);
390 else if (av.showAnnotation)
392 annotationPanel.setVisible(true);
393 annotationSpaceFillerHolder.setVisible(true);
396 idSpaceFillerPanel1.setVisible(!wrap);
398 fontChanged(); // This is so that the scalePanel is resized correctly
409 // return value is true if the scroll is valid
410 public boolean scrollUp(boolean up)
414 if (vscroll.getValue() < 1)
418 setScrollValues(hscroll.getValue(), vscroll.getValue() - 1);
422 if (vextent + vscroll.getValue() >= av.getAlignment().getHeight())
426 setScrollValues(hscroll.getValue(), vscroll.getValue() + 1);
433 public boolean scrollRight(boolean right)
437 if (hscroll.getValue() < 1)
441 setScrollValues(hscroll.getValue() - 1, vscroll.getValue());
445 if (hextent + hscroll.getValue() >= av.getAlignment().getWidth())
449 setScrollValues(hscroll.getValue() + 1, vscroll.getValue());
456 public void setScrollValues(int x, int y)
458 int width = av.alignment.getWidth();
459 int height = av.alignment.getHeight();
461 if (av.hasHiddenColumns)
463 width = av.getColumnSelection().findColumnPosition(width);
467 av.setEndRes((x + (seqPanel.seqCanvas.getSize().width / av.charWidth)) - 1);
469 hextent = seqPanel.seqCanvas.getSize().width / av.charWidth;
470 vextent = seqPanel.seqCanvas.getSize().height / av.charHeight;
477 if (vextent > height)
482 if ((hextent + x) > width)
487 if ((vextent + y) > height)
489 y = height - vextent;
504 int endSeq = y + vextent;
505 if (endSeq > av.alignment.getHeight())
507 endSeq = av.alignment.getHeight();
510 av.setEndSeq(endSeq);
511 hscroll.setValues(x, hextent, 0, width);
512 vscroll.setValues(y, vextent, 0, height);
514 if (overviewPanel != null)
516 overviewPanel.setBoxPosition();
521 public void adjustmentValueChanged(AdjustmentEvent evt)
523 int oldX = av.getStartRes();
524 int oldY = av.getStartSeq();
526 if (evt == null || evt.getSource()==apvscroll)
528 annotationPanel.setScrollOffset(apvscroll.getValue());
529 alabels.setScrollOffset(apvscroll.getValue());
530 //annotationPanel.image=null;
531 //alabels.image=null;
533 //annotationPanel.repaint();
535 if (evt == null || evt.getSource() == hscroll)
537 int x = hscroll.getValue();
539 av.setEndRes(x + seqPanel.seqCanvas.getSize().width
540 / av.getCharWidth() - 1);
543 if (evt == null || evt.getSource() == vscroll)
545 int offy = vscroll.getValue();
546 if (av.getWrapAlignment())
548 int rowSize = seqPanel.seqCanvas
549 .getWrappedCanvasWidth(seqPanel.seqCanvas.getSize().width);
550 av.setStartRes(vscroll.getValue() * rowSize);
551 av.setEndRes((vscroll.getValue() + 1) * rowSize);
555 av.setStartSeq(offy);
556 av.setEndSeq(offy + seqPanel.seqCanvas.getSize().height
557 / av.getCharHeight());
561 if (overviewPanel != null)
563 overviewPanel.setBoxPosition();
566 int scrollX = av.startRes - oldX;
567 int scrollY = av.startSeq - oldY;
569 if (av.getWrapAlignment() || !fastPaint || av.MAC)
575 // Make sure we're not trying to draw a panel
576 // larger than the visible window
577 if (scrollX > av.endRes - av.startRes)
579 scrollX = av.endRes - av.startRes;
581 else if (scrollX < av.startRes - av.endRes)
583 scrollX = av.startRes - av.endRes;
586 idPanel.idCanvas.fastPaint(scrollY);
587 seqPanel.seqCanvas.fastPaint(scrollX, scrollY);
589 scalePanel.repaint();
590 if (av.getShowAnnotation())
592 annotationPanel.fastPaint(av.getStartRes() - oldX);
598 public void paintAlignment(boolean updateOverview)
604 jalview.structure.StructureSelectionManager
605 .getStructureSelectionManager().sequenceColoursChanged(this);
607 if (overviewPanel != null)
609 overviewPanel.updateOverviewImage();
614 public void update(Graphics g)
619 public void paint(Graphics g)
622 Dimension d = idPanel.idCanvas.getSize();
623 idPanel.idCanvas.setSize(d.width, seqPanel.seqCanvas.getSize().height);
624 annotationPanelHolder.setSize(seqPanel.getSize().width, annotationPanel.getSize().height);
625 annotationSpaceFillerHolder.setSize(d.width,
626 annotationPanel.getSize().height);
628 alabels.setSize(d.width, annotationPanelHolder.getSize().height);
630 if (av.getWrapAlignment())
632 int maxwidth = av.alignment.getWidth();
634 if (av.hasHiddenColumns)
636 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
639 int canvasWidth = seqPanel.seqCanvas
640 .getWrappedCanvasWidth(seqPanel.seqCanvas.getSize().width);
644 int max = maxwidth / canvasWidth;
645 vscroll.setMaximum(1+max);
646 vscroll.setUnitIncrement(1);
647 vscroll.setVisibleAmount(1);
652 setScrollValues(av.getStartRes(), av.getStartSeq());
657 seqPanel.seqCanvas.repaint();
658 scalePanel.repaint();
659 annotationPanel.repaint();
660 idPanel.idCanvas.repaint();
663 protected Panel sequenceHolderPanel = new Panel();
665 protected Scrollbar vscroll = new Scrollbar();
667 protected Scrollbar hscroll = new Scrollbar();
669 protected Panel seqPanelHolder = new Panel();
671 BorderLayout borderLayout1 = new BorderLayout();
673 BorderLayout borderLayout3 = new BorderLayout();
675 protected Panel scalePanelHolder = new Panel();
677 protected Panel idPanelHolder = new Panel();
679 BorderLayout borderLayout5 = new BorderLayout();
681 protected Panel idSpaceFillerPanel1 = new Panel();
683 public Panel annotationSpaceFillerHolder = new Panel();
685 BorderLayout borderLayout6 = new BorderLayout();
687 BorderLayout borderLayout7 = new BorderLayout();
689 Panel hscrollHolder = new Panel();
691 BorderLayout borderLayout10 = new BorderLayout();
693 protected Panel hscrollFillerPanel = new Panel();
695 BorderLayout borderLayout11 = new BorderLayout();
697 BorderLayout borderLayout4 = new BorderLayout();
699 BorderLayout borderLayout2 = new BorderLayout();
701 Panel annotationPanelHolder = new Panel();
702 protected Scrollbar apvscroll=new Scrollbar();
704 BorderLayout borderLayout12 = new BorderLayout();
706 private void jbInit() throws Exception
708 // idPanelHolder.setPreferredSize(new Dimension(70, 10));
709 this.setLayout(borderLayout7);
711 // sequenceHolderPanel.setPreferredSize(new Dimension(150, 150));
712 sequenceHolderPanel.setLayout(borderLayout3);
713 seqPanelHolder.setLayout(borderLayout1);
714 scalePanelHolder.setBackground(Color.white);
716 // scalePanelHolder.setPreferredSize(new Dimension(10, 30));
717 scalePanelHolder.setLayout(borderLayout6);
718 idPanelHolder.setLayout(borderLayout5);
719 idSpaceFillerPanel1.setBackground(Color.white);
721 // idSpaceFillerPanel1.setPreferredSize(new Dimension(10, 30));
722 idSpaceFillerPanel1.setLayout(borderLayout11);
723 annotationSpaceFillerHolder.setBackground(Color.white);
725 // annotationSpaceFillerHolder.setPreferredSize(new Dimension(10, 80));
726 annotationSpaceFillerHolder.setLayout(borderLayout4);
727 hscroll.setOrientation(Scrollbar.HORIZONTAL);
728 hscrollHolder.setLayout(borderLayout10);
729 hscrollFillerPanel.setBackground(Color.white);
730 apvscroll.setOrientation(Scrollbar.VERTICAL);
731 apvscroll.setVisible(true);
732 apvscroll.addAdjustmentListener(this);
734 annotationPanelHolder.setBackground(Color.white);
735 annotationPanelHolder.setLayout(borderLayout12);
736 annotationPanelHolder.add(apvscroll, BorderLayout.EAST);
737 // hscrollFillerPanel.setPreferredSize(new Dimension(70, 10));
738 hscrollHolder.setBackground(Color.white);
740 // annotationScroller.setPreferredSize(new Dimension(10, 80));
741 // this.setPreferredSize(new Dimension(220, 166));
742 seqPanelHolder.setBackground(Color.white);
743 idPanelHolder.setBackground(Color.white);
744 sequenceHolderPanel.add(scalePanelHolder, BorderLayout.NORTH);
745 sequenceHolderPanel.add(seqPanelHolder, BorderLayout.CENTER);
746 seqPanelHolder.add(vscroll, BorderLayout.EAST);
748 // Panel3.add(secondaryPanelHolder, BorderLayout.SOUTH);
749 this.add(idPanelHolder, BorderLayout.WEST);
750 idPanelHolder.add(idSpaceFillerPanel1, BorderLayout.NORTH);
751 idPanelHolder.add(annotationSpaceFillerHolder, BorderLayout.SOUTH);
752 this.add(hscrollHolder, BorderLayout.SOUTH);
753 hscrollHolder.add(hscroll, BorderLayout.CENTER);
754 hscrollHolder.add(hscrollFillerPanel, BorderLayout.WEST);
755 this.add(sequenceHolderPanel, BorderLayout.CENTER);
759 * hides or shows dynamic annotation rows based on groups and av state flags
761 public void updateAnnotation()
763 updateAnnotation(false);
766 public void updateAnnotation(boolean applyGlobalSettings)
768 // TODO: this should be merged with other annotation update stuff - that
769 // sits on AlignViewport
770 boolean updateCalcs = false;
771 boolean conv = av.isShowGroupConservation();
772 boolean cons = av.isShowGroupConsensus();
773 boolean showprf = av.isShowSequenceLogo();
774 boolean showConsHist = av.isShowConsensusHistogram();
776 boolean sortg = true;
778 // remove old automatic annotation
779 // add any new annotation
781 Vector gr = av.alignment.getGroups(); // OrderedBy(av.alignment.getSequencesArray());
782 // intersect alignment annotation with alignment groups
784 AlignmentAnnotation[] aan = av.alignment.getAlignmentAnnotation();
785 Hashtable oldrfs = new Hashtable();
788 for (int an = 0; an < aan.length; an++)
790 if (aan[an].autoCalculated && aan[an].groupRef != null)
792 oldrfs.put(aan[an].groupRef, aan[an].groupRef);
793 av.alignment.deleteAnnotation(aan[an]);
801 for (int g = 0; g < gr.size(); g++)
804 sg = (SequenceGroup) gr.elementAt(g);
805 if (applyGlobalSettings || !oldrfs.containsKey(sg))
807 // set defaults for this group's conservation/consensus
808 sg.setshowSequenceLogo(showprf);
809 sg.setShowConsensusHistogram(showConsHist);
814 av.alignment.addAnnotation(sg.getConservationRow(), 0);
819 av.alignment.addAnnotation(sg.getConsensus(), 0);
821 // refresh the annotation rows
824 sg.recalcConservation();
829 adjustAnnotationHeight();
832 * automatically adjust annotation panel height for new annotation
833 * whilst ensuring the alignment is still visible.
835 public void adjustAnnotationHeight()
837 // TODO: display vertical annotation scrollbar if necessary
838 // this is called after loading new annotation onto alignment
839 if (alignFrame.getSize().height == 0)
841 System.out.println("NEEDS FIXING");
844 int height = annotationPanel.adjustPanelHeight();
845 if (hscroll.isVisible())
847 height += hscroll.getPreferredSize().height;
850 // sets initial preferred height
851 if (height > alignFrame.getSize().height/ 2)
853 height = alignFrame.getSize().height / 2;
855 apvscroll.setValues(apvscroll.getValue(), height, 0, mheight);
856 annotationPanel.setScrollOffset(apvscroll.getValue());
857 alabels.setScrollOffset(apvscroll.getValue());
858 apvscroll.addNotify();
861 annotationPanelHolder.setSize(new Dimension(annotationPanel
862 .getSize().width, height));
864 annotationSpaceFillerHolder.setSize(new Dimension(
865 annotationSpaceFillerHolder.getSize().width
868 //annotationScroller.validate();// repaint();