2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
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 jalview.api.FeatureRenderer getFeatureRenderer()
149 return seqPanel.seqCanvas.fr;
152 public jalview.api.FeatureRenderer cloneFeatureRenderer()
154 FeatureRenderer nfr = new FeatureRenderer(av);
155 nfr.transferSettings(seqPanel.seqCanvas.fr);
158 public void alignmentChanged()
160 av.alignmentChanged(this);
162 if (overviewPanel != null)
164 overviewPanel.updateOverviewImage();
167 alignFrame.updateEditMenuBar();
172 public void fontChanged()
174 // set idCanvas bufferedImage to null
175 // to prevent drawing old image
176 idPanel.idCanvas.image = null;
177 FontMetrics fm = getFontMetrics(av.getFont());
179 scalePanel.setSize(new Dimension(10, av.charHeight + fm.getDescent()));
180 idwidthAdjuster.setSize(new Dimension(10, av.charHeight
182 av.updateSequenceIdColours();
183 annotationPanel.image = null;
184 int ap = annotationPanel.adjustPanelHeight(false);
185 Dimension d = calculateIdWidth();
186 d.setSize(d.width + 4, seqPanel.seqCanvas.getSize().height);
187 alabels.setSize(d.width + 4, ap);
189 idPanel.idCanvas.setSize(d);
190 hscrollFillerPanel.setSize(d);
192 validateAnnotationDimensions(false);
193 annotationPanel.repaint();
197 if (overviewPanel != null)
199 overviewPanel.updateOverviewImage();
203 public void setIdWidth(int w, int h)
205 idPanel.idCanvas.setSize(w, h);
206 idPanelHolder.setSize(w, idPanelHolder.getSize().height);
207 annotationSpaceFillerHolder.setSize(w,
208 annotationSpaceFillerHolder.getSize().height);
209 alabels.setSize(w, alabels.getSize().height);
213 Dimension calculateIdWidth()
215 if (av.nullFrame == null)
217 av.nullFrame = new Frame();
218 av.nullFrame.addNotify();
221 Graphics g = av.nullFrame.getGraphics();
223 FontMetrics fm = g.getFontMetrics(av.font);
224 AlignmentI al = av.getAlignment();
229 while (i < al.getHeight() && al.getSequenceAt(i) != null)
231 SequenceI s = al.getSequenceAt(i);
232 id = s.getDisplayId(av.getShowJVSuffix());
234 if (fm.stringWidth(id) > idWidth)
236 idWidth = fm.stringWidth(id);
241 // Also check annotation label widths
243 if (al.getAlignmentAnnotation() != null)
245 fm = g.getFontMetrics(av.nullFrame.getFont());
246 while (i < al.getAlignmentAnnotation().length)
248 String label = al.getAlignmentAnnotation()[i].label;
249 if (fm.stringWidth(label) > idWidth)
251 idWidth = fm.stringWidth(label);
257 return new Dimension(idWidth, idPanel.idCanvas.getSize().height);
261 * Highlight the given results on the alignment.
264 public void highlightSearchResults(SearchResults results)
266 scrollToPosition(results);
267 seqPanel.seqCanvas.highlightSearchResults(results);
271 * scroll the view to show the position of the highlighted region in results
272 * (if any) and redraw the overview
275 * @return false if results were not found
277 public boolean scrollToPosition(SearchResults results)
279 return scrollToPosition(results, true);
283 * scroll the view to show the position of the highlighted region in results
287 * @param redrawOverview
288 * - when set, the overview will be recalculated (takes longer)
289 * @return false if results were not found
291 public boolean scrollToPosition(SearchResults results,
292 boolean redrawOverview)
295 // do we need to scroll the panel?
296 if (results != null && results.getSize() > 0)
298 AlignmentI alignment = av.getAlignment();
299 int seqIndex = alignment.findIndex(results);
304 SequenceI seq = alignment.getSequenceAt(seqIndex);
305 int[] r = results.getResults(seq, 0, alignment.getWidth());
311 .println("DEBUG: scroll didn't happen - results not within alignment : "
312 + seq.getStart() + "," + seq.getEnd());
320 * System.out.println("DEBUG: scroll: start=" + r[0] +
321 * " av.getStartRes()=" + av.getStartRes() + " end=" + r[1] +
322 * " seq.end=" + seq.getEnd() + " av.getEndRes()=" + av.getEndRes() +
323 * " hextent=" + hextent);
332 if (end == seq.getEnd())
336 return scrollTo(start, end, seqIndex, false, redrawOverview);
341 public boolean scrollTo(int ostart, int end, int seqIndex,
342 boolean scrollToNearest, boolean redrawOverview)
344 int startv, endv, starts, ends, width;
347 if (av.hasHiddenColumns())
349 start = av.getColumnSelection().findColumnPosition(ostart);
350 end = av.getColumnSelection().findColumnPosition(end);
353 if (!scrollToNearest && !av.getColumnSelection().isVisible(ostart))
355 // don't scroll - position isn't visible
364 if (!av.wrapAlignment)
367 * int spos=av.getStartRes(),sqpos=av.getStartSeq(); if ((startv =
368 * av.getStartRes()) >= start) { spos=start-1; // seqIn //
369 * setScrollValues(start - 1, seqIndex); } else if ((endv =
370 * av.getEndRes()) <= end) { // setScrollValues(spos=startv + 1 + end -
371 * endv, seqIndex); spos=startv + 1 + end - endv; } else if ((starts =
372 * av.getStartSeq()) > seqIndex) { setScrollValues(av.getStartRes(),
373 * seqIndex); } else if ((ends = av.getEndSeq()) <= seqIndex) {
374 * setScrollValues(av.getStartRes(), starts + seqIndex - ends + 1); }
378 if ((av.getStartRes() > end)
379 || (av.getEndRes() < start)
380 || ((av.getStartSeq() > seqIndex) || (av.getEndSeq() < seqIndex)))
382 if (start > av.getAlignment().getWidth() - hextent)
384 start = av.getAlignment().getWidth() - hextent;
391 if (seqIndex > av.getAlignment().getHeight() - vextent)
393 seqIndex = av.getAlignment().getHeight() - vextent;
399 // System.out.println("trying to scroll to: "+start+" "+seqIndex);
400 setScrollValues(start, seqIndex);
405 scrollToWrappedVisible(start);
407 if (redrawOverview && overviewPanel != null)
409 overviewPanel.setBoxPosition();
411 paintAlignment(redrawOverview);
415 void scrollToWrappedVisible(int res)
417 int cwidth = seqPanel.seqCanvas
418 .getWrappedCanvasWidth(seqPanel.seqCanvas.getSize().width);
419 if (res <= av.getStartRes() || res >= (av.getStartRes() + cwidth))
421 vscroll.setValue(res / cwidth);
422 av.startRes = vscroll.getValue() * cwidth;
426 public OverviewPanel getOverviewPanel()
428 return overviewPanel;
431 public void setOverviewPanel(OverviewPanel op)
436 public void setAnnotationVisible(boolean b)
438 if (!av.wrapAlignment)
440 annotationSpaceFillerHolder.setVisible(b);
441 annotationPanelHolder.setVisible(b);
448 * automatically adjust annotation panel height for new annotation whilst
449 * ensuring the alignment is still visible.
451 public void adjustAnnotationHeight()
453 // TODO: display vertical annotation scrollbar if necessary
454 // this is called after loading new annotation onto alignment
455 if (alignFrame.getSize().height == 0)
457 System.out.println("NEEDS FIXING");
460 validateAnnotationDimensions(true);
461 apvscroll.addNotify();
464 paintAlignment(true);
468 * calculate the annotation dimensions and refresh slider values accordingly.
469 * need to do repaints/notifys afterwards.
471 protected void validateAnnotationDimensions(boolean adjustPanelHeight)
473 boolean modified = false;
474 int height = av.calcPanelHeight();
476 if (hscroll.isVisible())
478 height += (minsize = hscroll.getPreferredSize().height);
480 if (apvscroll.isVisible())
482 minsize += apvscroll.getPreferredSize().height;
484 int mheight = height;
485 Dimension d = sequenceHolderPanel.getSize(), e = idPanel.getSize();
486 int seqandannot = d.height - scalePanelHolder.getSize().height;
488 if (adjustPanelHeight)
490 // NOTE: this logic is different in the application. Need a better algorithm to define behaviour
491 // sets initial preferred height
492 // try and set height according to alignment
493 float sscaling = (float) ((av.getCharHeight() * av.getAlignment().getHeight())/(1.0*mheight));
496 // if the alignment is too big then
497 // default is 0.5 split
498 height = seqandannot / 2;
502 // otherwise just set the panel so that one row of sequence is visible
503 height = -av.getCharHeight() * 1
504 + (int) (seqandannot * (1 - sscaling));
509 // maintain same window layout whilst updating sliders
510 height = annotationPanelHolder.getSize().height;
513 if (seqandannot - height < 5)
515 height = seqandannot;
517 annotationPanel.setSize(new Dimension(d.width, height));
518 alabels.setSize(new Dimension(e.width, height));
519 annotationSpaceFillerHolder.setSize(new Dimension(e.width, height));
520 annotationPanelHolder.setSize(new Dimension(d.width, height));
521 seqPanelHolder.setSize(d.width, seqandannot - height);
523 .setSize(d.width, seqPanel.seqCanvas.getSize().height);
524 int s = apvscroll.getValue();
525 if (s > mheight - height)
529 apvscroll.setValues(s, height, 0, mheight);
530 annotationPanel.setScrollOffset(apvscroll.getValue());
531 alabels.setScrollOffset(apvscroll.getValue());
534 public void setWrapAlignment(boolean wrap)
538 scalePanelHolder.setVisible(!wrap);
540 hscroll.setVisible(!wrap);
541 idwidthAdjuster.setVisible(!wrap);
545 annotationPanelHolder.setVisible(false);
546 annotationSpaceFillerHolder.setVisible(false);
548 else if (av.showAnnotation)
550 annotationPanelHolder.setVisible(true);
551 annotationSpaceFillerHolder.setVisible(true);
554 idSpaceFillerPanel1.setVisible(!wrap);
556 fontChanged(); // This is so that the scalePanel is resized correctly
567 // return value is true if the scroll is valid
568 public boolean scrollUp(boolean up)
572 if (vscroll.getValue() < 1)
576 setScrollValues(hscroll.getValue(), vscroll.getValue() - 1);
580 if (vextent + vscroll.getValue() >= av.getAlignment().getHeight())
584 setScrollValues(hscroll.getValue(), vscroll.getValue() + 1);
591 public boolean scrollRight(boolean right)
595 if (hscroll.getValue() < 1)
599 setScrollValues(hscroll.getValue() - 1, vscroll.getValue());
603 if (hextent + hscroll.getValue() >= av.getAlignment().getWidth())
607 setScrollValues(hscroll.getValue() + 1, vscroll.getValue());
614 public void setScrollValues(int x, int y)
616 int width = av.getAlignment().getWidth();
617 int height = av.getAlignment().getHeight();
619 if (av.hasHiddenColumns())
621 width = av.getColumnSelection().findColumnPosition(width);
629 hextent = seqPanel.seqCanvas.getSize().width / av.charWidth;
630 vextent = seqPanel.seqCanvas.getSize().height / av.charHeight;
637 if (vextent > height)
642 if ((hextent + x) > width)
644 System.err.println("hextent was " + hextent + " and x was " + x);
649 if ((vextent + y) > height)
651 y = height - vextent;
661 System.err.println("x was " + x);
667 int endSeq = y + vextent;
668 if (endSeq > av.getAlignment().getHeight())
670 endSeq = av.getAlignment().getHeight();
673 av.setEndSeq(endSeq);
675 av.setEndRes((x + (seqPanel.seqCanvas.getSize().width / av.charWidth)) - 1);
677 hscroll.setValues(x, hextent, 0, width);
678 vscroll.setValues(y, vextent, 0, height);
680 if (overviewPanel != null)
682 overviewPanel.setBoxPosition();
688 public void adjustmentValueChanged(AdjustmentEvent evt)
690 int oldX = av.getStartRes();
691 int oldY = av.getStartSeq();
693 if (evt == null || evt.getSource() == apvscroll)
695 annotationPanel.setScrollOffset(apvscroll.getValue());
696 alabels.setScrollOffset(apvscroll.getValue());
697 // annotationPanel.image=null;
698 // alabels.image=null;
699 // alabels.repaint();
700 // annotationPanel.repaint();
702 if (evt == null || evt.getSource() == hscroll)
704 int x = hscroll.getValue();
706 av.setEndRes(x + seqPanel.seqCanvas.getSize().width
707 / av.getCharWidth() - 1);
710 if (evt == null || evt.getSource() == vscroll)
712 int offy = vscroll.getValue();
713 if (av.getWrapAlignment())
715 int rowSize = seqPanel.seqCanvas
716 .getWrappedCanvasWidth(seqPanel.seqCanvas.getSize().width);
717 av.setStartRes(vscroll.getValue() * rowSize);
718 av.setEndRes((vscroll.getValue() + 1) * rowSize);
722 av.setStartSeq(offy);
723 av.setEndSeq(offy + seqPanel.seqCanvas.getSize().height
724 / av.getCharHeight());
728 if (overviewPanel != null)
730 overviewPanel.setBoxPosition();
733 int scrollX = av.startRes - oldX;
734 int scrollY = av.startSeq - oldY;
736 if (av.getWrapAlignment() || !fastPaint || av.MAC)
742 // Make sure we're not trying to draw a panel
743 // larger than the visible window
744 if (scrollX > av.endRes - av.startRes)
746 scrollX = av.endRes - av.startRes;
748 else if (scrollX < av.startRes - av.endRes)
750 scrollX = av.startRes - av.endRes;
753 idPanel.idCanvas.fastPaint(scrollY);
754 seqPanel.seqCanvas.fastPaint(scrollX, scrollY);
756 scalePanel.repaint();
757 if (av.getShowAnnotation())
759 annotationPanel.fastPaint(av.getStartRes() - oldX);
766 private void sendViewPosition()
768 StructureSelectionManager.getStructureSelectionManager(av.applet)
769 .sendViewPosition(this, av.startRes, av.endRes, av.startSeq,
773 public void paintAlignment(boolean updateOverview)
779 jalview.structure.StructureSelectionManager
780 .getStructureSelectionManager(av.applet)
781 .sequenceColoursChanged(this);
783 if (overviewPanel != null)
785 overviewPanel.updateOverviewImage();
790 public void update(Graphics g)
795 public void paint(Graphics g)
798 Dimension d = idPanel.idCanvas.getSize();
799 idPanel.idCanvas.setSize(d.width, seqPanel.seqCanvas.getSize().height);
801 if (av.getWrapAlignment())
803 int maxwidth = av.getAlignment().getWidth();
805 if (av.hasHiddenColumns())
807 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
810 int canvasWidth = seqPanel.seqCanvas
811 .getWrappedCanvasWidth(seqPanel.seqCanvas.getSize().width);
815 int max = maxwidth / canvasWidth;
816 vscroll.setMaximum(1 + max);
817 vscroll.setUnitIncrement(1);
818 vscroll.setVisibleAmount(1);
823 setScrollValues(av.getStartRes(), av.getStartSeq());
828 seqPanel.seqCanvas.repaint();
829 scalePanel.repaint();
830 annotationPanel.repaint();
831 idPanel.idCanvas.repaint();
834 protected Panel sequenceHolderPanel = new Panel();
836 protected Scrollbar vscroll = new Scrollbar();
838 protected Scrollbar hscroll = new Scrollbar();
840 protected Panel seqPanelHolder = new Panel();
842 BorderLayout borderLayout1 = new BorderLayout();
844 BorderLayout borderLayout3 = new BorderLayout();
846 protected Panel scalePanelHolder = new Panel();
848 protected Panel idPanelHolder = new Panel();
850 BorderLayout borderLayout5 = new BorderLayout();
852 protected Panel idSpaceFillerPanel1 = new Panel();
854 public Panel annotationSpaceFillerHolder = new Panel();
856 BorderLayout borderLayout6 = new BorderLayout();
858 BorderLayout borderLayout7 = new BorderLayout();
860 Panel hscrollHolder = new Panel();
862 BorderLayout borderLayout10 = new BorderLayout();
864 protected Panel hscrollFillerPanel = new Panel();
866 BorderLayout borderLayout11 = new BorderLayout();
868 BorderLayout borderLayout4 = new BorderLayout();
870 BorderLayout borderLayout2 = new BorderLayout();
872 Panel annotationPanelHolder = new Panel();
874 protected Scrollbar apvscroll = new Scrollbar();
876 BorderLayout borderLayout12 = new BorderLayout();
878 private void jbInit() throws Exception
880 // idPanelHolder.setPreferredSize(new Dimension(70, 10));
881 this.setLayout(borderLayout7);
883 // sequenceHolderPanel.setPreferredSize(new Dimension(150, 150));
884 sequenceHolderPanel.setLayout(borderLayout3);
885 seqPanelHolder.setLayout(borderLayout1);
886 scalePanelHolder.setBackground(Color.white);
888 // scalePanelHolder.setPreferredSize(new Dimension(10, 30));
889 scalePanelHolder.setLayout(borderLayout6);
890 idPanelHolder.setLayout(borderLayout5);
891 idSpaceFillerPanel1.setBackground(Color.white);
893 // idSpaceFillerPanel1.setPreferredSize(new Dimension(10, 30));
894 idSpaceFillerPanel1.setLayout(borderLayout11);
895 annotationSpaceFillerHolder.setBackground(Color.white);
897 // annotationSpaceFillerHolder.setPreferredSize(new Dimension(10, 80));
898 annotationSpaceFillerHolder.setLayout(borderLayout4);
899 hscroll.setOrientation(Scrollbar.HORIZONTAL);
900 hscrollHolder.setLayout(borderLayout10);
901 hscrollFillerPanel.setBackground(Color.white);
902 apvscroll.setOrientation(Scrollbar.VERTICAL);
903 apvscroll.setVisible(true);
904 apvscroll.addAdjustmentListener(this);
906 annotationPanelHolder.setBackground(Color.white);
907 annotationPanelHolder.setLayout(borderLayout12);
908 annotationPanelHolder.add(apvscroll, BorderLayout.EAST);
909 // hscrollFillerPanel.setPreferredSize(new Dimension(70, 10));
910 hscrollHolder.setBackground(Color.white);
912 // annotationScroller.setPreferredSize(new Dimension(10, 80));
913 // this.setPreferredSize(new Dimension(220, 166));
914 seqPanelHolder.setBackground(Color.white);
915 idPanelHolder.setBackground(Color.white);
916 sequenceHolderPanel.add(scalePanelHolder, BorderLayout.NORTH);
917 sequenceHolderPanel.add(seqPanelHolder, BorderLayout.CENTER);
918 seqPanelHolder.add(vscroll, BorderLayout.EAST);
920 // Panel3.add(secondaryPanelHolder, BorderLayout.SOUTH);
921 this.add(idPanelHolder, BorderLayout.WEST);
922 idPanelHolder.add(idSpaceFillerPanel1, BorderLayout.NORTH);
923 idPanelHolder.add(annotationSpaceFillerHolder, BorderLayout.SOUTH);
924 this.add(hscrollHolder, BorderLayout.SOUTH);
925 hscrollHolder.add(hscroll, BorderLayout.CENTER);
926 hscrollHolder.add(hscrollFillerPanel, BorderLayout.WEST);
927 this.add(sequenceHolderPanel, BorderLayout.CENTER);
931 * hides or shows dynamic annotation rows based on groups and av state flags
933 public void updateAnnotation()
935 updateAnnotation(false);
938 public void updateAnnotation(boolean applyGlobalSettings)
940 updateAnnotation(applyGlobalSettings, false);
943 public void updateAnnotation(boolean applyGlobalSettings,
944 boolean preserveNewGroupSettings)
946 av.updateGroupAnnotationSettings(applyGlobalSettings,
947 preserveNewGroupSettings);
948 adjustAnnotationHeight();
952 public AlignmentI getAlignment()
954 return av.getAlignment();
958 public StructureSelectionManager getStructureSelectionManager()
960 return StructureSelectionManager
961 .getStructureSelectionManager(av.applet);
965 public void raiseOOMWarning(String string, OutOfMemoryError error)
968 System.err.println("Out of memory whilst '" + string + "'");
969 error.printStackTrace();