2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.1)
3 * Copyright (C) 2014 The Jalview Authors
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/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
19 package jalview.appletgui;
22 import java.awt.event.*;
24 import jalview.api.AlignmentViewPanel;
25 import jalview.datamodel.*;
26 import jalview.structure.StructureSelectionManager;
28 public class AlignmentPanel extends Panel implements AdjustmentListener,
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 seqPanelHolder = null;
59 sequenceHolderPanel = null;
61 scalePanelHolder = null;
62 annotationPanel = null;
63 annotationPanelHolder = null;
64 annotationSpaceFillerHolder = null;
67 public AlignmentPanel(AlignFrame af, final AlignViewport av)
79 seqPanel = new SeqPanel(av, this);
80 idPanel = new IdPanel(av, this);
81 scalePanel = new ScalePanel(av, this);
82 idwidthAdjuster = new IdwidthAdjuster(this);
83 annotationPanel = new AnnotationPanel(this);
84 annotationPanelHolder.add(annotationPanel, BorderLayout.CENTER);
86 sequenceHolderPanel.add(annotationPanelHolder, BorderLayout.SOUTH);
87 alabels = new AnnotationLabels(this);
89 setAnnotationVisible(av.showAnnotation);
91 idPanelHolder.add(idPanel, BorderLayout.CENTER);
92 idSpaceFillerPanel1.add(idwidthAdjuster, BorderLayout.CENTER);
93 annotationSpaceFillerHolder.add(alabels, BorderLayout.CENTER);
94 scalePanelHolder.add(scalePanel, BorderLayout.CENTER);
95 seqPanelHolder.add(seqPanel, BorderLayout.CENTER);
98 setScrollValues(0, 0);
100 apvscroll.addAdjustmentListener(this);
101 hscroll.addAdjustmentListener(this);
102 vscroll.addAdjustmentListener(this);
104 addComponentListener(new ComponentAdapter()
106 public void componentResized(ComponentEvent evt)
108 setScrollValues(av.getStartRes(), av.getStartSeq());
109 if (getSize().height > 0
110 && annotationPanelHolder.getSize().height > 0)
112 validateAnnotationDimensions(false);
119 Dimension d = calculateIdWidth();
120 idPanel.idCanvas.setSize(d);
122 hscrollFillerPanel.setSize(d.width, annotationPanel.getSize().height);
124 idPanel.idCanvas.setSize(d.width, seqPanel.seqCanvas.getSize().height);
125 annotationSpaceFillerHolder.setSize(d.width,
126 annotationPanel.getSize().height);
127 alabels.setSize(d.width, annotationPanel.getSize().height);
128 final AlignmentPanel ap = this;
129 av.addPropertyChangeListener(new java.beans.PropertyChangeListener()
131 public void propertyChange(java.beans.PropertyChangeEvent evt)
133 if (evt.getPropertyName().equals("alignment"))
135 PaintRefresher.Refresh(ap, av.getSequenceSetId(), true, true);
142 public SequenceRenderer getSequenceRenderer()
144 return seqPanel.seqCanvas.sr;
147 public FeatureRenderer getFeatureRenderer()
149 return seqPanel.seqCanvas.fr;
152 public void alignmentChanged()
154 av.alignmentChanged(this);
156 if (overviewPanel != null)
158 overviewPanel.updateOverviewImage();
161 alignFrame.updateEditMenuBar();
166 public void fontChanged()
168 // set idCanvas bufferedImage to null
169 // to prevent drawing old image
170 idPanel.idCanvas.image = null;
171 FontMetrics fm = getFontMetrics(av.getFont());
173 scalePanel.setSize(new Dimension(10, av.charHeight + fm.getDescent()));
174 idwidthAdjuster.setSize(new Dimension(10, av.charHeight
176 av.updateSequenceIdColours();
177 annotationPanel.image = null;
178 int ap = annotationPanel.adjustPanelHeight(false);
179 Dimension d = calculateIdWidth();
180 d.setSize(d.width + 4, seqPanel.seqCanvas.getSize().height);
181 alabels.setSize(d.width + 4, ap);
183 idPanel.idCanvas.setSize(d);
184 hscrollFillerPanel.setSize(d);
186 validateAnnotationDimensions(false);
187 annotationPanel.repaint();
191 if (overviewPanel != null)
193 overviewPanel.updateOverviewImage();
197 public void setIdWidth(int w, int h)
199 idPanel.idCanvas.setSize(w, h);
200 idPanelHolder.setSize(w, idPanelHolder.getSize().height);
201 annotationSpaceFillerHolder.setSize(w,
202 annotationSpaceFillerHolder.getSize().height);
203 alabels.setSize(w, alabels.getSize().height);
207 Dimension calculateIdWidth()
209 if (av.nullFrame == null)
211 av.nullFrame = new Frame();
212 av.nullFrame.addNotify();
215 Graphics g = av.nullFrame.getGraphics();
217 FontMetrics fm = g.getFontMetrics(av.font);
218 AlignmentI al = av.getAlignment();
223 while (i < al.getHeight() && al.getSequenceAt(i) != null)
225 SequenceI s = al.getSequenceAt(i);
226 id = s.getDisplayId(av.getShowJVSuffix());
228 if (fm.stringWidth(id) > idWidth)
230 idWidth = fm.stringWidth(id);
235 // Also check annotation label widths
237 if (al.getAlignmentAnnotation() != null)
239 fm = g.getFontMetrics(av.nullFrame.getFont());
240 while (i < al.getAlignmentAnnotation().length)
242 String label = al.getAlignmentAnnotation()[i].label;
243 if (fm.stringWidth(label) > idWidth)
245 idWidth = fm.stringWidth(label);
251 return new Dimension(idWidth, idPanel.idCanvas.getSize().height);
255 * Highlight the given results on the alignment.
258 public void highlightSearchResults(SearchResults results)
260 scrollToPosition(results);
261 seqPanel.seqCanvas.highlightSearchResults(results);
265 * scroll the view to show the position of the highlighted region in results
266 * (if any) and redraw the overview
269 * @return false if results were not found
271 public boolean scrollToPosition(SearchResults results)
273 return scrollToPosition(results, true);
277 * scroll the view to show the position of the highlighted region in results
281 * @param redrawOverview
282 * - when set, the overview will be recalculated (takes longer)
283 * @return false if results were not found
285 public boolean scrollToPosition(SearchResults results,
286 boolean redrawOverview)
289 // do we need to scroll the panel?
290 if (results != null && results.getSize() > 0)
292 AlignmentI alignment = av.getAlignment();
293 int seqIndex = alignment.findIndex(results);
298 SequenceI seq = alignment.getSequenceAt(seqIndex);
299 int[] r = results.getResults(seq, 0, alignment.getWidth());
305 .println("DEBUG: scroll didn't happen - results not within alignment : "
306 + seq.getStart() + "," + seq.getEnd());
314 * System.out.println("DEBUG: scroll: start=" + r[0] +
315 * " av.getStartRes()=" + av.getStartRes() + " end=" + r[1] +
316 * " seq.end=" + seq.getEnd() + " av.getEndRes()=" + av.getEndRes() +
317 * " hextent=" + hextent);
326 if (end == seq.getEnd())
330 return scrollTo(start, end, seqIndex, false, redrawOverview);
335 public boolean scrollTo(int ostart, int end, int seqIndex,
336 boolean scrollToNearest, boolean redrawOverview)
338 int startv, endv, starts, ends, width;
341 if (av.hasHiddenColumns())
343 start = av.getColumnSelection().findColumnPosition(ostart);
344 end = av.getColumnSelection().findColumnPosition(end);
347 if (!scrollToNearest && !av.getColumnSelection().isVisible(ostart))
349 // don't scroll - position isn't visible
358 if (!av.wrapAlignment)
361 * int spos=av.getStartRes(),sqpos=av.getStartSeq(); if ((startv =
362 * av.getStartRes()) >= start) { spos=start-1; // seqIn //
363 * setScrollValues(start - 1, seqIndex); } else if ((endv =
364 * av.getEndRes()) <= end) { // setScrollValues(spos=startv + 1 + end -
365 * endv, seqIndex); spos=startv + 1 + end - endv; } else if ((starts =
366 * av.getStartSeq()) > seqIndex) { setScrollValues(av.getStartRes(),
367 * seqIndex); } else if ((ends = av.getEndSeq()) <= seqIndex) {
368 * setScrollValues(av.getStartRes(), starts + seqIndex - ends + 1); }
372 if ((av.getStartRes() > end)
373 || (av.getEndRes() < start)
374 || ((av.getStartSeq() > seqIndex) || (av.getEndSeq() < seqIndex)))
376 if (start > av.getAlignment().getWidth() - hextent)
378 start = av.getAlignment().getWidth() - hextent;
385 if (seqIndex > av.getAlignment().getHeight() - vextent)
387 seqIndex = av.getAlignment().getHeight() - vextent;
393 // System.out.println("trying to scroll to: "+start+" "+seqIndex);
394 setScrollValues(start, seqIndex);
399 scrollToWrappedVisible(start);
401 if (redrawOverview && overviewPanel != null)
403 overviewPanel.setBoxPosition();
405 paintAlignment(redrawOverview);
409 void scrollToWrappedVisible(int res)
411 int cwidth = seqPanel.seqCanvas
412 .getWrappedCanvasWidth(seqPanel.seqCanvas.getSize().width);
413 if (res <= av.getStartRes() || res >= (av.getStartRes() + cwidth))
415 vscroll.setValue(res / cwidth);
416 av.startRes = vscroll.getValue() * cwidth;
420 public OverviewPanel getOverviewPanel()
422 return overviewPanel;
425 public void setOverviewPanel(OverviewPanel op)
430 public void setAnnotationVisible(boolean b)
432 if (!av.wrapAlignment)
434 annotationSpaceFillerHolder.setVisible(b);
435 annotationPanelHolder.setVisible(b);
437 annotationSpaceFillerHolder.setVisible(false);
438 annotationPanelHolder.setVisible(false);
445 * automatically adjust annotation panel height for new annotation whilst
446 * ensuring the alignment is still visible.
448 public void adjustAnnotationHeight()
450 // TODO: display vertical annotation scrollbar if necessary
451 // this is called after loading new annotation onto alignment
452 if (alignFrame.getSize().height == 0)
454 System.out.println("NEEDS FIXING");
457 validateAnnotationDimensions(true);
458 apvscroll.addNotify();
461 paintAlignment(true);
465 * calculate the annotation dimensions and refresh slider values accordingly.
466 * need to do repaints/notifys afterwards.
468 protected void validateAnnotationDimensions(boolean adjustPanelHeight)
470 boolean modified = false;
471 int height = av.calcPanelHeight();
473 if (hscroll.isVisible())
475 height += (minsize = hscroll.getPreferredSize().height);
477 if (apvscroll.isVisible())
479 minsize += apvscroll.getPreferredSize().height;
481 int mheight = height;
482 Dimension d = sequenceHolderPanel.getSize(), e = idPanel.getSize();
483 int seqandannot = d.height - scalePanelHolder.getSize().height;
485 if (adjustPanelHeight)
487 // NOTE: this logic is different in the application. Need a better algorithm to define behaviour
488 // sets initial preferred height
489 // try and set height according to alignment
490 float sscaling = (float) ((av.getCharHeight() * av.getAlignment().getHeight())/(1.0*mheight));
493 // if the alignment is too big then
494 // default is 0.5 split
495 height = seqandannot / 2;
499 // otherwise just set the panel so that one row of sequence is visible
500 height = -av.getCharHeight() * 1
501 + (int) (seqandannot * (1 - sscaling));
506 // maintain same window layout whilst updating sliders
507 height = annotationPanelHolder.getSize().height;
510 if (seqandannot - height < 5)
512 height = seqandannot;
514 annotationPanel.setSize(new Dimension(d.width, height));
515 alabels.setSize(new Dimension(e.width, height));
516 annotationSpaceFillerHolder.setSize(new Dimension(e.width, height));
517 annotationPanelHolder.setSize(new Dimension(d.width, height));
518 // seqPanelHolder.setSize(d.width, seqandannot - height);
520 .setSize(d.width, seqPanel.seqCanvas.getSize().height);
521 int s = apvscroll.getValue();
522 if (s > mheight - height)
526 apvscroll.setValues(s, height, 0, mheight);
527 annotationPanel.setScrollOffset(apvscroll.getValue(), false);
528 alabels.setScrollOffset(apvscroll.getValue(), false);
531 public void setWrapAlignment(boolean wrap)
535 scalePanelHolder.setVisible(!wrap);
537 hscroll.setVisible(!wrap);
538 idwidthAdjuster.setVisible(!wrap);
542 annotationPanelHolder.setVisible(false);
543 annotationSpaceFillerHolder.setVisible(false);
545 else if (av.showAnnotation)
547 annotationPanelHolder.setVisible(true);
548 annotationSpaceFillerHolder.setVisible(true);
551 idSpaceFillerPanel1.setVisible(!wrap);
553 fontChanged(); // This is so that the scalePanel is resized correctly
556 sequenceHolderPanel.revalidate();
565 // return value is true if the scroll is valid
566 public boolean scrollUp(boolean up)
570 if (vscroll.getValue() < 1)
574 setScrollValues(hscroll.getValue(), vscroll.getValue() - 1);
578 if (vextent + vscroll.getValue() >= av.getAlignment().getHeight())
582 setScrollValues(hscroll.getValue(), vscroll.getValue() + 1);
589 public boolean scrollRight(boolean right)
593 if (hscroll.getValue() < 1)
597 setScrollValues(hscroll.getValue() - 1, vscroll.getValue());
601 if (hextent + hscroll.getValue() >= av.getAlignment().getWidth())
605 setScrollValues(hscroll.getValue() + 1, vscroll.getValue());
612 public void setScrollValues(int x, int y)
614 int width = av.getAlignment().getWidth();
615 int height = av.getAlignment().getHeight();
617 if (av.hasHiddenColumns())
619 width = av.getColumnSelection().findColumnPosition(width);
627 hextent = seqPanel.seqCanvas.getSize().width / av.charWidth;
628 vextent = seqPanel.seqCanvas.getSize().height / av.charHeight;
635 if (vextent > height)
640 if ((hextent + x) > width)
642 System.err.println("hextent was " + hextent + " and x was " + x);
647 if ((vextent + y) > height)
649 y = height - vextent;
659 System.err.println("x was " + x);
665 int endSeq = y + vextent;
666 if (endSeq > av.getAlignment().getHeight())
668 endSeq = av.getAlignment().getHeight();
671 av.setEndSeq(endSeq);
673 av.setEndRes((x + (seqPanel.seqCanvas.getSize().width / av.charWidth)) - 1);
675 hscroll.setValues(x, hextent, 0, width);
676 vscroll.setValues(y, vextent, 0, height);
678 if (overviewPanel != null)
680 overviewPanel.setBoxPosition();
686 public void adjustmentValueChanged(AdjustmentEvent evt)
688 int oldX = av.getStartRes();
689 int oldY = av.getStartSeq();
691 if (evt == null || evt.getSource() == apvscroll)
693 annotationPanel.setScrollOffset(apvscroll.getValue(), false);
694 alabels.setScrollOffset(apvscroll.getValue(), false);
695 // annotationPanel.image=null;
696 // alabels.image=null;
697 // alabels.repaint();
698 // annotationPanel.repaint();
700 if (evt == null || evt.getSource() == hscroll)
702 int x = hscroll.getValue();
704 av.setEndRes(x + seqPanel.seqCanvas.getSize().width
705 / av.getCharWidth() - 1);
708 if (evt == null || evt.getSource() == vscroll)
710 int offy = vscroll.getValue();
711 if (av.getWrapAlignment())
713 int rowSize = seqPanel.seqCanvas
714 .getWrappedCanvasWidth(seqPanel.seqCanvas.getSize().width);
715 av.setStartRes(vscroll.getValue() * rowSize);
716 av.setEndRes((vscroll.getValue() + 1) * rowSize);
720 av.setStartSeq(offy);
721 av.setEndSeq(offy + seqPanel.seqCanvas.getSize().height
722 / av.getCharHeight());
726 if (overviewPanel != null)
728 overviewPanel.setBoxPosition();
731 int scrollX = av.startRes - oldX;
732 int scrollY = av.startSeq - oldY;
734 if (av.getWrapAlignment() || !fastPaint || av.MAC)
740 // Make sure we're not trying to draw a panel
741 // larger than the visible window
742 if (scrollX > av.endRes - av.startRes)
744 scrollX = av.endRes - av.startRes;
746 else if (scrollX < av.startRes - av.endRes)
748 scrollX = av.startRes - av.endRes;
751 idPanel.idCanvas.fastPaint(scrollY);
752 seqPanel.seqCanvas.fastPaint(scrollX, scrollY);
754 scalePanel.repaint();
755 if (av.getShowAnnotation())
757 annotationPanel.fastPaint(av.getStartRes() - oldX);
764 private void sendViewPosition()
766 StructureSelectionManager.getStructureSelectionManager(av.applet)
767 .sendViewPosition(this, av.startRes, av.endRes, av.startSeq,
771 public void paintAlignment(boolean updateOverview)
777 jalview.structure.StructureSelectionManager
778 .getStructureSelectionManager(av.applet)
779 .sequenceColoursChanged(this);
781 if (overviewPanel != null)
783 overviewPanel.updateOverviewImage();
788 public void update(Graphics g)
793 public void paint(Graphics g)
796 Dimension d = idPanel.idCanvas.getSize();
797 idPanel.idCanvas.setSize(d.width, seqPanel.seqCanvas.getSize().height);
799 if (av.getWrapAlignment())
801 int maxwidth = av.getAlignment().getWidth();
803 if (av.hasHiddenColumns())
805 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
808 int canvasWidth = seqPanel.seqCanvas
809 .getWrappedCanvasWidth(seqPanel.seqCanvas.getSize().width);
813 int max = maxwidth / canvasWidth;
814 vscroll.setMaximum(1 + max);
815 vscroll.setUnitIncrement(1);
816 vscroll.setVisibleAmount(1);
821 setScrollValues(av.getStartRes(), av.getStartSeq());
824 seqPanel.seqCanvas.repaint();
825 idPanel.idCanvas.repaint();
826 if (!av.wrapAlignment)
828 if (av.showAnnotation)
831 annotationPanel.repaint();
833 scalePanel.repaint();
838 protected Panel sequenceHolderPanel = new Panel();
840 protected Scrollbar vscroll = new Scrollbar();
842 protected Scrollbar hscroll = new Scrollbar();
844 protected Panel seqPanelHolder = new Panel();
846 BorderLayout borderLayout1 = new BorderLayout();
848 BorderLayout borderLayout3 = new BorderLayout();
850 protected Panel scalePanelHolder = new Panel();
852 protected Panel idPanelHolder = new Panel();
854 BorderLayout borderLayout5 = new BorderLayout();
856 protected Panel idSpaceFillerPanel1 = new Panel();
858 public Panel annotationSpaceFillerHolder = new Panel();
860 BorderLayout borderLayout6 = new BorderLayout();
862 BorderLayout borderLayout7 = new BorderLayout();
864 Panel hscrollHolder = new Panel();
866 BorderLayout borderLayout10 = new BorderLayout();
868 protected Panel hscrollFillerPanel = new Panel();
870 BorderLayout borderLayout11 = new BorderLayout();
872 BorderLayout borderLayout4 = new BorderLayout();
874 BorderLayout borderLayout2 = new BorderLayout();
876 Panel annotationPanelHolder = new Panel();
878 protected Scrollbar apvscroll = new Scrollbar();
880 BorderLayout borderLayout12 = new BorderLayout();
882 private void jbInit() throws Exception
884 // idPanelHolder.setPreferredSize(new Dimension(70, 10));
885 this.setLayout(borderLayout7);
887 // sequenceHolderPanel.setPreferredSize(new Dimension(150, 150));
888 sequenceHolderPanel.setLayout(borderLayout3);
889 seqPanelHolder.setLayout(borderLayout1);
890 scalePanelHolder.setBackground(Color.white);
892 // scalePanelHolder.setPreferredSize(new Dimension(10, 30));
893 scalePanelHolder.setLayout(borderLayout6);
894 idPanelHolder.setLayout(borderLayout5);
895 idSpaceFillerPanel1.setBackground(Color.white);
897 // idSpaceFillerPanel1.setPreferredSize(new Dimension(10, 30));
898 idSpaceFillerPanel1.setLayout(borderLayout11);
899 annotationSpaceFillerHolder.setBackground(Color.white);
901 // annotationSpaceFillerHolder.setPreferredSize(new Dimension(10, 80));
902 annotationSpaceFillerHolder.setLayout(borderLayout4);
903 hscroll.setOrientation(Scrollbar.HORIZONTAL);
904 hscrollHolder.setLayout(borderLayout10);
905 hscrollFillerPanel.setBackground(Color.white);
906 apvscroll.setOrientation(Scrollbar.VERTICAL);
907 apvscroll.setVisible(true);
908 apvscroll.addAdjustmentListener(this);
910 annotationPanelHolder.setBackground(Color.white);
911 annotationPanelHolder.setLayout(borderLayout12);
912 annotationPanelHolder.add(apvscroll, BorderLayout.EAST);
913 // hscrollFillerPanel.setPreferredSize(new Dimension(70, 10));
914 hscrollHolder.setBackground(Color.white);
916 // annotationScroller.setPreferredSize(new Dimension(10, 80));
917 // this.setPreferredSize(new Dimension(220, 166));
918 seqPanelHolder.setBackground(Color.white);
919 idPanelHolder.setBackground(Color.white);
920 sequenceHolderPanel.add(scalePanelHolder, BorderLayout.NORTH);
921 sequenceHolderPanel.add(seqPanelHolder, BorderLayout.CENTER);
922 seqPanelHolder.add(vscroll, BorderLayout.EAST);
924 // Panel3.add(secondaryPanelHolder, BorderLayout.SOUTH);
925 this.add(idPanelHolder, BorderLayout.WEST);
926 idPanelHolder.add(idSpaceFillerPanel1, BorderLayout.NORTH);
927 idPanelHolder.add(annotationSpaceFillerHolder, BorderLayout.SOUTH);
928 this.add(hscrollHolder, BorderLayout.SOUTH);
929 hscrollHolder.add(hscroll, BorderLayout.CENTER);
930 hscrollHolder.add(hscrollFillerPanel, BorderLayout.WEST);
931 this.add(sequenceHolderPanel, BorderLayout.CENTER);
935 * hides or shows dynamic annotation rows based on groups and av state flags
937 public void updateAnnotation()
939 updateAnnotation(false);
942 public void updateAnnotation(boolean applyGlobalSettings)
944 updateAnnotation(applyGlobalSettings, false);
947 public void updateAnnotation(boolean applyGlobalSettings,
948 boolean preserveNewGroupSettings)
950 av.updateGroupAnnotationSettings(applyGlobalSettings,
951 preserveNewGroupSettings);
952 adjustAnnotationHeight();
956 public AlignmentI getAlignment()
958 return av.getAlignment();
962 public StructureSelectionManager getStructureSelectionManager()
964 return StructureSelectionManager
965 .getStructureSelectionManager(av.applet);
969 public void raiseOOMWarning(String string, OutOfMemoryError error)
972 System.err.println("Out of memory whilst '" + string + "'");
973 error.printStackTrace();