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);
386 * automatically adjust annotation panel height for new annotation whilst
387 * ensuring the alignment is still visible.
389 public void adjustAnnotationHeight()
391 // TODO: display vertical annotation scrollbar if necessary
392 // this is called after loading new annotation onto alignment
393 if (alignFrame.getSize().height == 0)
395 System.out.println("NEEDS FIXING");
398 validateAnnotationDimensions(true);
399 apvscroll.addNotify();
406 * calculate the annotation dimensions and refresh slider values accordingly.
407 * need to do repaints/notifys afterwards.
409 protected void validateAnnotationDimensions(boolean adjustPanelHeight) {
410 int height = annotationPanel.calcPanelHeight();
411 if (hscroll.isVisible())
413 height += hscroll.getPreferredSize().height;
415 int mheight = height;
416 // sets initial preferred height
417 if ((height+40) > getSize().height / 2)
419 height = getSize().height / 2;
421 if (!adjustPanelHeight)
423 // maintain same window layout whilst updating sliders
424 height=seqPanelHolder.getSize().height;
426 Dimension d=seqPanelHolder.getSize(),e=idPanel.getSize();
427 annotationPanel.setSize(new Dimension(d.width,height));
428 alabels.setSize(e.width,height);
429 annotationSpaceFillerHolder.setSize(new Dimension(e.width, height));
430 annotationPanelHolder.setSize(new Dimension(d.width, height));
431 seqPanelHolder.setSize(d.width,d.height-height);
432 int s=apvscroll.getValue();
433 if (s>mheight-height)
437 apvscroll.setValues(apvscroll.getValue(), height, 0, mheight);
438 annotationPanel.setScrollOffset(apvscroll.getValue());
439 alabels.setScrollOffset(apvscroll.getValue());
442 public void setWrapAlignment(boolean wrap)
446 scalePanelHolder.setVisible(!wrap);
448 hscroll.setVisible(!wrap);
449 idwidthAdjuster.setVisible(!wrap);
453 annotationPanelHolder.setVisible(false);
454 annotationSpaceFillerHolder.setVisible(false);
456 else if (av.showAnnotation)
458 annotationPanelHolder.setVisible(true);
459 annotationSpaceFillerHolder.setVisible(true);
462 idSpaceFillerPanel1.setVisible(!wrap);
464 fontChanged(); // This is so that the scalePanel is resized correctly
475 // return value is true if the scroll is valid
476 public boolean scrollUp(boolean up)
480 if (vscroll.getValue() < 1)
484 setScrollValues(hscroll.getValue(), vscroll.getValue() - 1);
488 if (vextent + vscroll.getValue() >= av.getAlignment().getHeight())
492 setScrollValues(hscroll.getValue(), vscroll.getValue() + 1);
499 public boolean scrollRight(boolean right)
503 if (hscroll.getValue() < 1)
507 setScrollValues(hscroll.getValue() - 1, vscroll.getValue());
511 if (hextent + hscroll.getValue() >= av.getAlignment().getWidth())
515 setScrollValues(hscroll.getValue() + 1, vscroll.getValue());
522 public void setScrollValues(int x, int y)
524 int width = av.alignment.getWidth();
525 int height = av.alignment.getHeight();
527 if (av.hasHiddenColumns)
529 width = av.getColumnSelection().findColumnPosition(width);
533 av.setEndRes((x + (seqPanel.seqCanvas.getSize().width / av.charWidth)) - 1);
535 hextent = seqPanel.seqCanvas.getSize().width / av.charWidth;
536 vextent = seqPanel.seqCanvas.getSize().height / av.charHeight;
543 if (vextent > height)
548 if ((hextent + x) > width)
553 if ((vextent + y) > height)
555 y = height - vextent;
570 int endSeq = y + vextent;
571 if (endSeq > av.alignment.getHeight())
573 endSeq = av.alignment.getHeight();
576 av.setEndSeq(endSeq);
577 hscroll.setValues(x, hextent, 0, width);
578 vscroll.setValues(y, vextent, 0, height);
580 if (overviewPanel != null)
582 overviewPanel.setBoxPosition();
587 public void adjustmentValueChanged(AdjustmentEvent evt)
589 int oldX = av.getStartRes();
590 int oldY = av.getStartSeq();
592 if (evt == null || evt.getSource() == apvscroll)
594 annotationPanel.setScrollOffset(apvscroll.getValue());
595 alabels.setScrollOffset(apvscroll.getValue());
596 // annotationPanel.image=null;
597 // alabels.image=null;
598 // alabels.repaint();
599 // annotationPanel.repaint();
601 if (evt == null || evt.getSource() == hscroll)
603 int x = hscroll.getValue();
605 av.setEndRes(x + seqPanel.seqCanvas.getSize().width
606 / av.getCharWidth() - 1);
609 if (evt == null || evt.getSource() == vscroll)
611 int offy = vscroll.getValue();
612 if (av.getWrapAlignment())
614 int rowSize = seqPanel.seqCanvas
615 .getWrappedCanvasWidth(seqPanel.seqCanvas.getSize().width);
616 av.setStartRes(vscroll.getValue() * rowSize);
617 av.setEndRes((vscroll.getValue() + 1) * rowSize);
621 av.setStartSeq(offy);
622 av.setEndSeq(offy + seqPanel.seqCanvas.getSize().height
623 / av.getCharHeight());
627 if (overviewPanel != null)
629 overviewPanel.setBoxPosition();
632 int scrollX = av.startRes - oldX;
633 int scrollY = av.startSeq - oldY;
635 if (av.getWrapAlignment() || !fastPaint || av.MAC)
641 // Make sure we're not trying to draw a panel
642 // larger than the visible window
643 if (scrollX > av.endRes - av.startRes)
645 scrollX = av.endRes - av.startRes;
647 else if (scrollX < av.startRes - av.endRes)
649 scrollX = av.startRes - av.endRes;
652 idPanel.idCanvas.fastPaint(scrollY);
653 seqPanel.seqCanvas.fastPaint(scrollX, scrollY);
655 scalePanel.repaint();
656 if (av.getShowAnnotation())
658 annotationPanel.fastPaint(av.getStartRes() - oldX);
664 public void paintAlignment(boolean updateOverview)
670 jalview.structure.StructureSelectionManager
671 .getStructureSelectionManager().sequenceColoursChanged(this);
673 if (overviewPanel != null)
675 overviewPanel.updateOverviewImage();
680 public void update(Graphics g)
685 public void paint(Graphics g)
688 Dimension d = idPanel.idCanvas.getSize();
689 idPanel.idCanvas.setSize(d.width, seqPanel.seqCanvas.getSize().height);
691 if (av.getWrapAlignment())
693 int maxwidth = av.alignment.getWidth();
695 if (av.hasHiddenColumns)
697 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
700 int canvasWidth = seqPanel.seqCanvas
701 .getWrappedCanvasWidth(seqPanel.seqCanvas.getSize().width);
705 int max = maxwidth / canvasWidth;
706 vscroll.setMaximum(1 + max);
707 vscroll.setUnitIncrement(1);
708 vscroll.setVisibleAmount(1);
713 setScrollValues(av.getStartRes(), av.getStartSeq());
718 seqPanel.seqCanvas.repaint();
719 scalePanel.repaint();
720 annotationPanel.repaint();
721 idPanel.idCanvas.repaint();
724 protected Panel sequenceHolderPanel = new Panel();
726 protected Scrollbar vscroll = new Scrollbar();
728 protected Scrollbar hscroll = new Scrollbar();
730 protected Panel seqPanelHolder = new Panel();
732 BorderLayout borderLayout1 = new BorderLayout();
734 BorderLayout borderLayout3 = new BorderLayout();
736 protected Panel scalePanelHolder = new Panel();
738 protected Panel idPanelHolder = new Panel();
740 BorderLayout borderLayout5 = new BorderLayout();
742 protected Panel idSpaceFillerPanel1 = new Panel();
744 public Panel annotationSpaceFillerHolder = new Panel();
746 BorderLayout borderLayout6 = new BorderLayout();
748 BorderLayout borderLayout7 = new BorderLayout();
750 Panel hscrollHolder = new Panel();
752 BorderLayout borderLayout10 = new BorderLayout();
754 protected Panel hscrollFillerPanel = new Panel();
756 BorderLayout borderLayout11 = new BorderLayout();
758 BorderLayout borderLayout4 = new BorderLayout();
760 BorderLayout borderLayout2 = new BorderLayout();
762 Panel annotationPanelHolder = new Panel();
764 protected Scrollbar apvscroll = new Scrollbar();
766 BorderLayout borderLayout12 = new BorderLayout();
768 private void jbInit() throws Exception
770 // idPanelHolder.setPreferredSize(new Dimension(70, 10));
771 this.setLayout(borderLayout7);
773 //sequenceHolderPanel.setPreferredSize(new Dimension(150, 150));
774 sequenceHolderPanel.setLayout(borderLayout3);
775 seqPanelHolder.setLayout(borderLayout1);
776 scalePanelHolder.setBackground(Color.white);
778 // scalePanelHolder.setPreferredSize(new Dimension(10, 30));
779 scalePanelHolder.setLayout(borderLayout6);
780 idPanelHolder.setLayout(borderLayout5);
781 idSpaceFillerPanel1.setBackground(Color.white);
783 // idSpaceFillerPanel1.setPreferredSize(new Dimension(10, 30));
784 idSpaceFillerPanel1.setLayout(borderLayout11);
785 annotationSpaceFillerHolder.setBackground(Color.white);
787 // annotationSpaceFillerHolder.setPreferredSize(new Dimension(10, 80));
788 annotationSpaceFillerHolder.setLayout(borderLayout4);
789 hscroll.setOrientation(Scrollbar.HORIZONTAL);
790 hscrollHolder.setLayout(borderLayout10);
791 hscrollFillerPanel.setBackground(Color.white);
792 apvscroll.setOrientation(Scrollbar.VERTICAL);
793 apvscroll.setVisible(true);
794 apvscroll.addAdjustmentListener(this);
796 annotationPanelHolder.setBackground(Color.white);
797 annotationPanelHolder.setLayout(borderLayout12);
798 annotationPanelHolder.add(apvscroll, BorderLayout.EAST);
799 // hscrollFillerPanel.setPreferredSize(new Dimension(70, 10));
800 hscrollHolder.setBackground(Color.white);
802 // annotationScroller.setPreferredSize(new Dimension(10, 80));
803 // this.setPreferredSize(new Dimension(220, 166));
804 seqPanelHolder.setBackground(Color.white);
805 idPanelHolder.setBackground(Color.white);
806 sequenceHolderPanel.add(scalePanelHolder, BorderLayout.NORTH);
807 sequenceHolderPanel.add(seqPanelHolder, BorderLayout.CENTER);
808 seqPanelHolder.add(vscroll, BorderLayout.EAST);
810 // Panel3.add(secondaryPanelHolder, BorderLayout.SOUTH);
811 this.add(idPanelHolder, BorderLayout.WEST);
812 idPanelHolder.add(idSpaceFillerPanel1, BorderLayout.NORTH);
813 idPanelHolder.add(annotationSpaceFillerHolder, BorderLayout.SOUTH);
814 this.add(hscrollHolder, BorderLayout.SOUTH);
815 hscrollHolder.add(hscroll, BorderLayout.CENTER);
816 hscrollHolder.add(hscrollFillerPanel, BorderLayout.WEST);
817 this.add(sequenceHolderPanel, BorderLayout.CENTER);
821 * hides or shows dynamic annotation rows based on groups and av state flags
823 public void updateAnnotation()
825 updateAnnotation(false);
828 public void updateAnnotation(boolean applyGlobalSettings)
830 // TODO: this should be merged with other annotation update stuff - that
831 // sits on AlignViewport
832 boolean updateCalcs = false;
833 boolean conv = av.isShowGroupConservation();
834 boolean cons = av.isShowGroupConsensus();
835 boolean showprf = av.isShowSequenceLogo();
836 boolean showConsHist = av.isShowConsensusHistogram();
838 boolean sortg = true;
840 // remove old automatic annotation
841 // add any new annotation
843 Vector gr = av.alignment.getGroups(); // OrderedBy(av.alignment.getSequencesArray());
844 // intersect alignment annotation with alignment groups
846 AlignmentAnnotation[] aan = av.alignment.getAlignmentAnnotation();
847 Hashtable oldrfs = new Hashtable();
850 for (int an = 0; an < aan.length; an++)
852 if (aan[an].autoCalculated && aan[an].groupRef != null)
854 oldrfs.put(aan[an].groupRef, aan[an].groupRef);
855 av.alignment.deleteAnnotation(aan[an]);
863 for (int g = 0; g < gr.size(); g++)
866 sg = (SequenceGroup) gr.elementAt(g);
867 if (applyGlobalSettings || !oldrfs.containsKey(sg))
869 // set defaults for this group's conservation/consensus
870 sg.setshowSequenceLogo(showprf);
871 sg.setShowConsensusHistogram(showConsHist);
876 av.alignment.addAnnotation(sg.getConservationRow(), 0);
881 av.alignment.addAnnotation(sg.getConsensus(), 0);
883 // refresh the annotation rows
886 sg.recalcConservation();
891 adjustAnnotationHeight();
895 public AlignmentI getAlignment()