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.*;
28 public class AlignmentPanel extends Panel implements AdjustmentListener, AlignmentViewPanel
31 public AlignViewport av;
33 OverviewPanel overviewPanel;
39 IdwidthAdjuster idwidthAdjuster;
41 public AlignFrame alignFrame;
43 ScalePanel scalePanel;
45 AnnotationPanel annotationPanel;
47 AnnotationLabels alabels;
49 // this value is set false when selection area being dragged
50 boolean fastPaint = true;
52 public AlignmentPanel(AlignFrame af, final AlignViewport av)
64 seqPanel = new SeqPanel(av, this);
65 idPanel = new IdPanel(av, this);
66 scalePanel = new ScalePanel(av, this);
67 idwidthAdjuster = new IdwidthAdjuster(this);
68 annotationPanel = new AnnotationPanel(this);
69 annotationPanelHolder.add(annotationPanel, BorderLayout.CENTER);
71 sequenceHolderPanel.add(annotationPanelHolder, BorderLayout.SOUTH);
73 alabels = new AnnotationLabels(this);
75 setAnnotationVisible(av.showAnnotation);
77 idPanelHolder.add(idPanel, BorderLayout.CENTER);
78 idSpaceFillerPanel1.add(idwidthAdjuster, BorderLayout.CENTER);
79 annotationSpaceFillerHolder.add(alabels, BorderLayout.CENTER);
80 scalePanelHolder.add(scalePanel, BorderLayout.CENTER);
81 seqPanelHolder.add(seqPanel, BorderLayout.CENTER);
84 setScrollValues(0, 0);
86 apvscroll.addAdjustmentListener(this);
87 hscroll.addAdjustmentListener(this);
88 vscroll.addAdjustmentListener(this);
90 addComponentListener(new ComponentAdapter()
92 public void componentResized(ComponentEvent evt)
94 setScrollValues(av.getStartRes(), av.getStartSeq());
99 Dimension d = calculateIdWidth();
100 idPanel.idCanvas.setSize(d);
102 hscrollFillerPanel.setSize(d.width, annotationPanel.getSize().height);
104 idPanel.idCanvas.setSize(d.width, seqPanel.seqCanvas.getSize().height);
105 annotationSpaceFillerHolder.setSize(d.width,
106 annotationPanel.getSize().height);
107 alabels.setSize(d.width, annotationPanel.getSize().height);
108 final AlignmentPanel ap = this;
109 av.addPropertyChangeListener(new java.beans.PropertyChangeListener()
111 public void propertyChange(java.beans.PropertyChangeEvent evt)
113 if (evt.getPropertyName().equals("alignment"))
115 PaintRefresher.Refresh(ap, av.getSequenceSetId(), true, true);
123 public SequenceRenderer getSequenceRenderer()
125 return seqPanel.seqCanvas.sr;
128 public FeatureRenderer getFeatureRenderer()
130 return seqPanel.seqCanvas.fr;
133 public void alignmentChanged()
135 av.alignmentChanged(this);
137 if (overviewPanel != null)
139 overviewPanel.updateOverviewImage();
142 alignFrame.updateEditMenuBar();
147 public void fontChanged()
149 // set idCanvas bufferedImage to null
150 // to prevent drawing old image
151 idPanel.idCanvas.image = null;
152 FontMetrics fm = getFontMetrics(av.getFont());
154 scalePanel.setSize(new Dimension(10, av.charHeight + fm.getDescent()));
155 idwidthAdjuster.setSize(new Dimension(10, av.charHeight
157 av.updateSequenceIdColours();
158 annotationPanel.image = null;
159 int ap = annotationPanel.adjustPanelHeight(false);
160 Dimension d = calculateIdWidth();
161 d.setSize(d.width + 4, seqPanel.seqCanvas.getSize().height);
162 alabels.setSize(d.width + 4, ap);
163 idPanel.idCanvas.setSize(d);
164 hscrollFillerPanel.setSize(d);
166 validateAnnotationDimensions(false);
167 annotationPanel.repaint();
171 if (overviewPanel != null)
173 overviewPanel.updateOverviewImage();
177 public void setIdWidth(int w, int h)
179 idPanel.idCanvas.setSize(w, h);
180 idPanelHolder.setSize(w, idPanelHolder.getSize().height);
181 annotationSpaceFillerHolder.setSize(w,annotationSpaceFillerHolder.getSize().height);
182 alabels.setSize(w, alabels.getSize().height);
186 Dimension calculateIdWidth()
188 if (av.nullFrame == null)
190 av.nullFrame = new Frame();
191 av.nullFrame.addNotify();
194 Graphics g = av.nullFrame.getGraphics();
196 FontMetrics fm = g.getFontMetrics(av.font);
197 AlignmentI al = av.getAlignment();
202 while (i < al.getHeight() && al.getSequenceAt(i) != null)
204 SequenceI s = al.getSequenceAt(i);
205 id = s.getDisplayId(av.getShowJVSuffix());
207 if (fm.stringWidth(id) > idWidth)
209 idWidth = fm.stringWidth(id);
214 // Also check annotation label widths
216 if (al.getAlignmentAnnotation() != null)
218 fm = g.getFontMetrics(av.nullFrame.getFont());
219 while (i < al.getAlignmentAnnotation().length)
221 String label = al.getAlignmentAnnotation()[i].label;
222 if (fm.stringWidth(label) > idWidth)
224 idWidth = fm.stringWidth(label);
230 return new Dimension(idWidth, idPanel.idCanvas.getSize().height);
234 * Highlight the given results on the alignment.
237 public void highlightSearchResults(SearchResults results)
239 scrollToPosition(results);
240 seqPanel.seqCanvas.highlightSearchResults(results);
244 * scroll the view to show the position of the highlighted region in results
245 * (if any) and redraw the overview
248 * @return false if results were not found
250 public boolean scrollToPosition(SearchResults results)
252 return scrollToPosition(results, true);
256 * scroll the view to show the position of the highlighted region in results
260 * @param redrawOverview
261 * - when set, the overview will be recalculated (takes longer)
262 * @return false if results were not found
264 public boolean scrollToPosition(SearchResults results,
265 boolean redrawOverview)
267 // do we need to scroll the panel?
268 if (results != null && results.getSize() > 0)
270 int seqIndex = av.alignment.findIndex(results);
275 SequenceI seq = av.alignment.getSequenceAt(seqIndex);
276 int[] r = results.getResults(seq, 0,av.alignment.getWidth());
279 if (av.applet.debug) {// DEBUG
280 System.out.println("DEBUG: scroll didn't happen - results not within alignment : "+seq.getStart()+","+seq.getEnd());
284 if (av.applet.debug) {
286 System.out.println("DEBUG: scroll didn't happen: start=" + r[0]
287 + " av.getStartRes()=" + av.getStartRes() + " end=" + r[1]
288 + " seq.end=" + seq.getEnd() + " av.getEndRes()="
289 + av.getEndRes() + " hextent=" + hextent);
297 if (end == seq.getEnd())
301 if (av.hasHiddenColumns)
303 start = av.getColumnSelection().findColumnPosition(start);
304 end = av.getColumnSelection().findColumnPosition(end);
307 if (!av.colSel.isVisible(r[0]))
309 // don't scroll - position isn't visible
314 if (!av.wrapAlignment)
316 if ((av.getStartRes() > end)
317 || (av.getEndRes() < start)
318 || ((av.getStartSeq() > seqIndex) || (av.getEndSeq() < seqIndex)))
320 if (start > av.alignment.getWidth() - hextent)
322 start = av.alignment.getWidth() - hextent;
329 if (seqIndex > av.alignment.getHeight() - vextent)
331 seqIndex = av.alignment.getHeight() - vextent;
337 setScrollValues(start, seqIndex);
342 scrollToWrappedVisible(start);
345 if (redrawOverview && overviewPanel != null)
347 overviewPanel.setBoxPosition();
349 paintAlignment(redrawOverview);
353 void scrollToWrappedVisible(int res)
355 int cwidth = seqPanel.seqCanvas
356 .getWrappedCanvasWidth(seqPanel.seqCanvas.getSize().width);
357 if (res <= av.getStartRes() || res >= (av.getStartRes() + cwidth))
359 vscroll.setValue(res / cwidth);
360 av.startRes = vscroll.getValue() * cwidth;
364 public OverviewPanel getOverviewPanel()
366 return overviewPanel;
369 public void setOverviewPanel(OverviewPanel op)
374 public void setAnnotationVisible(boolean b)
376 if (!av.wrapAlignment)
378 annotationSpaceFillerHolder.setVisible(b);
379 annotationPanelHolder.setVisible(b);
385 public void setWrapAlignment(boolean wrap)
389 scalePanelHolder.setVisible(!wrap);
391 hscroll.setVisible(!wrap);
392 idwidthAdjuster.setVisible(!wrap);
396 annotationPanelHolder.setVisible(false);
397 annotationSpaceFillerHolder.setVisible(false);
399 else if (av.showAnnotation)
401 annotationPanelHolder.setVisible(true);
402 annotationSpaceFillerHolder.setVisible(true);
405 idSpaceFillerPanel1.setVisible(!wrap);
407 fontChanged(); // This is so that the scalePanel is resized correctly
418 // return value is true if the scroll is valid
419 public boolean scrollUp(boolean up)
423 if (vscroll.getValue() < 1)
427 setScrollValues(hscroll.getValue(), vscroll.getValue() - 1);
431 if (vextent + vscroll.getValue() >= av.getAlignment().getHeight())
435 setScrollValues(hscroll.getValue(), vscroll.getValue() + 1);
442 public boolean scrollRight(boolean right)
446 if (hscroll.getValue() < 1)
450 setScrollValues(hscroll.getValue() - 1, vscroll.getValue());
454 if (hextent + hscroll.getValue() >= av.getAlignment().getWidth())
458 setScrollValues(hscroll.getValue() + 1, vscroll.getValue());
465 public void setScrollValues(int x, int y)
467 int width = av.alignment.getWidth();
468 int height = av.alignment.getHeight();
470 if (av.hasHiddenColumns)
472 width = av.getColumnSelection().findColumnPosition(width);
476 av.setEndRes((x + (seqPanel.seqCanvas.getSize().width / av.charWidth)) - 1);
478 hextent = seqPanel.seqCanvas.getSize().width / av.charWidth;
479 vextent = seqPanel.seqCanvas.getSize().height / av.charHeight;
486 if (vextent > height)
491 if ((hextent + x) > width)
496 if ((vextent + y) > height)
498 y = height - vextent;
513 int endSeq = y + vextent;
514 if (endSeq > av.alignment.getHeight())
516 endSeq = av.alignment.getHeight();
519 av.setEndSeq(endSeq);
520 hscroll.setValues(x, hextent, 0, width);
521 vscroll.setValues(y, vextent, 0, height);
523 if (overviewPanel != null)
525 overviewPanel.setBoxPosition();
530 public void adjustmentValueChanged(AdjustmentEvent evt)
532 int oldX = av.getStartRes();
533 int oldY = av.getStartSeq();
535 if (evt == null || evt.getSource() == apvscroll)
537 annotationPanel.setScrollOffset(apvscroll.getValue());
538 alabels.setScrollOffset(apvscroll.getValue());
539 // annotationPanel.image=null;
540 // alabels.image=null;
541 // alabels.repaint();
542 // annotationPanel.repaint();
544 if (evt == null || evt.getSource() == hscroll)
546 int x = hscroll.getValue();
548 av.setEndRes(x + seqPanel.seqCanvas.getSize().width
549 / av.getCharWidth() - 1);
552 if (evt == null || evt.getSource() == vscroll)
554 int offy = vscroll.getValue();
555 if (av.getWrapAlignment())
557 int rowSize = seqPanel.seqCanvas
558 .getWrappedCanvasWidth(seqPanel.seqCanvas.getSize().width);
559 av.setStartRes(vscroll.getValue() * rowSize);
560 av.setEndRes((vscroll.getValue() + 1) * rowSize);
564 av.setStartSeq(offy);
565 av.setEndSeq(offy + seqPanel.seqCanvas.getSize().height
566 / av.getCharHeight());
570 if (overviewPanel != null)
572 overviewPanel.setBoxPosition();
575 int scrollX = av.startRes - oldX;
576 int scrollY = av.startSeq - oldY;
578 if (av.getWrapAlignment() || !fastPaint || av.MAC)
584 // Make sure we're not trying to draw a panel
585 // larger than the visible window
586 if (scrollX > av.endRes - av.startRes)
588 scrollX = av.endRes - av.startRes;
590 else if (scrollX < av.startRes - av.endRes)
592 scrollX = av.startRes - av.endRes;
595 idPanel.idCanvas.fastPaint(scrollY);
596 seqPanel.seqCanvas.fastPaint(scrollX, scrollY);
598 scalePanel.repaint();
599 if (av.getShowAnnotation())
601 annotationPanel.fastPaint(av.getStartRes() - oldX);
607 public void paintAlignment(boolean updateOverview)
613 jalview.structure.StructureSelectionManager
614 .getStructureSelectionManager().sequenceColoursChanged(this);
616 if (overviewPanel != null)
618 overviewPanel.updateOverviewImage();
623 public void update(Graphics g)
628 public void paint(Graphics g)
631 Dimension d = idPanel.idCanvas.getSize();
632 idPanel.idCanvas.setSize(d.width, seqPanel.seqCanvas.getSize().height);
634 if (av.getWrapAlignment())
636 int maxwidth = av.alignment.getWidth();
638 if (av.hasHiddenColumns)
640 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
643 int canvasWidth = seqPanel.seqCanvas
644 .getWrappedCanvasWidth(seqPanel.seqCanvas.getSize().width);
648 int max = maxwidth / canvasWidth;
649 vscroll.setMaximum(1 + max);
650 vscroll.setUnitIncrement(1);
651 vscroll.setVisibleAmount(1);
656 setScrollValues(av.getStartRes(), av.getStartSeq());
661 seqPanel.seqCanvas.repaint();
662 scalePanel.repaint();
663 annotationPanel.repaint();
664 idPanel.idCanvas.repaint();
667 protected Panel sequenceHolderPanel = new Panel();
669 protected Scrollbar vscroll = new Scrollbar();
671 protected Scrollbar hscroll = new Scrollbar();
673 protected Panel seqPanelHolder = new Panel();
675 BorderLayout borderLayout1 = new BorderLayout();
677 BorderLayout borderLayout3 = new BorderLayout();
679 protected Panel scalePanelHolder = new Panel();
681 protected Panel idPanelHolder = new Panel();
683 BorderLayout borderLayout5 = new BorderLayout();
685 protected Panel idSpaceFillerPanel1 = new Panel();
687 public Panel annotationSpaceFillerHolder = new Panel();
689 BorderLayout borderLayout6 = new BorderLayout();
691 BorderLayout borderLayout7 = new BorderLayout();
693 Panel hscrollHolder = new Panel();
695 BorderLayout borderLayout10 = new BorderLayout();
697 protected Panel hscrollFillerPanel = new Panel();
699 BorderLayout borderLayout11 = new BorderLayout();
701 BorderLayout borderLayout4 = new BorderLayout();
703 BorderLayout borderLayout2 = new BorderLayout();
705 Panel annotationPanelHolder = new Panel();
707 protected Scrollbar apvscroll = new Scrollbar();
709 BorderLayout borderLayout12 = new BorderLayout();
711 private void jbInit() throws Exception
713 // idPanelHolder.setPreferredSize(new Dimension(70, 10));
714 this.setLayout(borderLayout7);
716 //sequenceHolderPanel.setPreferredSize(new Dimension(150, 150));
717 sequenceHolderPanel.setLayout(borderLayout3);
718 seqPanelHolder.setLayout(borderLayout1);
719 scalePanelHolder.setBackground(Color.white);
721 // scalePanelHolder.setPreferredSize(new Dimension(10, 30));
722 scalePanelHolder.setLayout(borderLayout6);
723 idPanelHolder.setLayout(borderLayout5);
724 idSpaceFillerPanel1.setBackground(Color.white);
726 // idSpaceFillerPanel1.setPreferredSize(new Dimension(10, 30));
727 idSpaceFillerPanel1.setLayout(borderLayout11);
728 annotationSpaceFillerHolder.setBackground(Color.white);
730 // annotationSpaceFillerHolder.setPreferredSize(new Dimension(10, 80));
731 annotationSpaceFillerHolder.setLayout(borderLayout4);
732 hscroll.setOrientation(Scrollbar.HORIZONTAL);
733 hscrollHolder.setLayout(borderLayout10);
734 hscrollFillerPanel.setBackground(Color.white);
735 apvscroll.setOrientation(Scrollbar.VERTICAL);
736 apvscroll.setVisible(true);
737 apvscroll.addAdjustmentListener(this);
739 annotationPanelHolder.setBackground(Color.white);
740 annotationPanelHolder.setLayout(borderLayout12);
741 annotationPanelHolder.add(apvscroll, BorderLayout.EAST);
742 // hscrollFillerPanel.setPreferredSize(new Dimension(70, 10));
743 hscrollHolder.setBackground(Color.white);
745 // annotationScroller.setPreferredSize(new Dimension(10, 80));
746 // this.setPreferredSize(new Dimension(220, 166));
747 seqPanelHolder.setBackground(Color.white);
748 idPanelHolder.setBackground(Color.white);
749 sequenceHolderPanel.add(scalePanelHolder, BorderLayout.NORTH);
750 sequenceHolderPanel.add(seqPanelHolder, BorderLayout.CENTER);
751 seqPanelHolder.add(vscroll, BorderLayout.EAST);
753 // Panel3.add(secondaryPanelHolder, BorderLayout.SOUTH);
754 this.add(idPanelHolder, BorderLayout.WEST);
755 idPanelHolder.add(idSpaceFillerPanel1, BorderLayout.NORTH);
756 idPanelHolder.add(annotationSpaceFillerHolder, BorderLayout.SOUTH);
757 this.add(hscrollHolder, BorderLayout.SOUTH);
758 hscrollHolder.add(hscroll, BorderLayout.CENTER);
759 hscrollHolder.add(hscrollFillerPanel, BorderLayout.WEST);
760 this.add(sequenceHolderPanel, BorderLayout.CENTER);
764 * hides or shows dynamic annotation rows based on groups and av state flags
766 public void updateAnnotation()
768 updateAnnotation(false);
771 public void updateAnnotation(boolean applyGlobalSettings)
773 // TODO: this should be merged with other annotation update stuff - that
774 // sits on AlignViewport
775 boolean updateCalcs = false;
776 boolean conv = av.isShowGroupConservation();
777 boolean cons = av.isShowGroupConsensus();
778 boolean showprf = av.isShowSequenceLogo();
779 boolean showConsHist = av.isShowConsensusHistogram();
781 boolean sortg = true;
783 // remove old automatic annotation
784 // add any new annotation
786 Vector gr = av.alignment.getGroups(); // OrderedBy(av.alignment.getSequencesArray());
787 // intersect alignment annotation with alignment groups
789 AlignmentAnnotation[] aan = av.alignment.getAlignmentAnnotation();
790 Hashtable oldrfs = new Hashtable();
793 for (int an = 0; an < aan.length; an++)
795 if (aan[an].autoCalculated && aan[an].groupRef != null)
797 oldrfs.put(aan[an].groupRef, aan[an].groupRef);
798 av.alignment.deleteAnnotation(aan[an]);
806 for (int g = 0; g < gr.size(); g++)
809 sg = (SequenceGroup) gr.elementAt(g);
810 if (applyGlobalSettings || !oldrfs.containsKey(sg))
812 // set defaults for this group's conservation/consensus
813 sg.setshowSequenceLogo(showprf);
814 sg.setShowConsensusHistogram(showConsHist);
819 av.alignment.addAnnotation(sg.getConservationRow(), 0);
824 av.alignment.addAnnotation(sg.getConsensus(), 0);
826 // refresh the annotation rows
829 sg.recalcConservation();
834 adjustAnnotationHeight();
838 * automatically adjust annotation panel height for new annotation whilst
839 * ensuring the alignment is still visible.
841 public void adjustAnnotationHeight()
843 // TODO: display vertical annotation scrollbar if necessary
844 // this is called after loading new annotation onto alignment
845 if (alignFrame.getSize().height == 0)
847 System.out.println("NEEDS FIXING");
850 validateAnnotationDimensions(true);
851 apvscroll.addNotify();
858 * calculate the annotation dimensions and refresh slider values accordingly.
859 * need to do repaints/notifys afterwards.
861 protected void validateAnnotationDimensions(boolean adjustPanelHeight) {
862 int height = annotationPanel.calcPanelHeight();
863 if (hscroll.isVisible())
865 height += hscroll.getPreferredSize().height;
867 int mheight = height;
868 // sets initial preferred height
869 if ((height+40) > getSize().height / 2)
871 height = getSize().height / 2;
873 if (!adjustPanelHeight)
875 // maintain same window layout whilst updating sliders
876 height=seqPanelHolder.getSize().height;
878 Dimension d=seqPanelHolder.getSize(),e=idPanel.getSize();
879 annotationPanel.setSize(new Dimension(d.width,height));
880 alabels.setSize(e.width,height);
881 annotationSpaceFillerHolder.setSize(new Dimension(e.width, height));
882 annotationPanelHolder.setSize(new Dimension(d.width, height));
883 seqPanelHolder.setSize(d.width,d.height-height);
884 int s=apvscroll.getValue();
885 if (s>mheight-height)
889 apvscroll.setValues(apvscroll.getValue(), height, 0, mheight);
890 annotationPanel.setScrollOffset(apvscroll.getValue());
891 alabels.setScrollOffset(apvscroll.getValue());
895 public AlignmentI getAlignment()