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 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);
442 * automatically adjust annotation panel height for new annotation whilst
443 * ensuring the alignment is still visible.
445 public void adjustAnnotationHeight()
447 // TODO: display vertical annotation scrollbar if necessary
448 // this is called after loading new annotation onto alignment
449 if (alignFrame.getSize().height == 0)
451 System.out.println("NEEDS FIXING");
454 validateAnnotationDimensions(true);
455 apvscroll.addNotify();
458 paintAlignment(true);
462 * calculate the annotation dimensions and refresh slider values accordingly.
463 * need to do repaints/notifys afterwards.
465 protected void validateAnnotationDimensions(boolean adjustPanelHeight)
467 boolean modified = false;
468 int height = av.calcPanelHeight();
470 if (hscroll.isVisible())
472 height += (minsize = hscroll.getPreferredSize().height);
474 if (apvscroll.isVisible())
476 minsize += apvscroll.getPreferredSize().height;
478 int mheight = height;
479 Dimension d = sequenceHolderPanel.getSize(), e = idPanel.getSize();
480 int seqandannot = d.height - scalePanelHolder.getSize().height;
482 if (adjustPanelHeight)
484 // NOTE: this logic is different in the application. Need a better algorithm to define behaviour
485 // sets initial preferred height
486 // try and set height according to alignment
487 float sscaling = (float) ((av.getCharHeight() * av.getAlignment().getHeight())/(1.0*mheight));
490 // if the alignment is too big then
491 // default is 0.5 split
492 height = seqandannot / 2;
496 // otherwise just set the panel so that one row of sequence is visible
497 height = -av.getCharHeight() * 1
498 + (int) (seqandannot * (1 - sscaling));
503 // maintain same window layout whilst updating sliders
504 height = annotationPanelHolder.getSize().height;
507 if (seqandannot - height < 5)
509 height = seqandannot;
511 annotationPanel.setSize(new Dimension(d.width, height));
512 alabels.setSize(new Dimension(e.width, height));
513 annotationSpaceFillerHolder.setSize(new Dimension(e.width, height));
514 annotationPanelHolder.setSize(new Dimension(d.width, height));
515 seqPanelHolder.setSize(d.width, seqandannot - height);
517 .setSize(d.width, seqPanel.seqCanvas.getSize().height);
518 int s = apvscroll.getValue();
519 if (s > mheight - height)
523 apvscroll.setValues(s, height, 0, mheight);
524 annotationPanel.setScrollOffset(apvscroll.getValue());
525 alabels.setScrollOffset(apvscroll.getValue());
528 public void setWrapAlignment(boolean wrap)
532 scalePanelHolder.setVisible(!wrap);
534 hscroll.setVisible(!wrap);
535 idwidthAdjuster.setVisible(!wrap);
539 annotationPanelHolder.setVisible(false);
540 annotationSpaceFillerHolder.setVisible(false);
542 else if (av.showAnnotation)
544 annotationPanelHolder.setVisible(true);
545 annotationSpaceFillerHolder.setVisible(true);
548 idSpaceFillerPanel1.setVisible(!wrap);
550 fontChanged(); // This is so that the scalePanel is resized correctly
561 // return value is true if the scroll is valid
562 public boolean scrollUp(boolean up)
566 if (vscroll.getValue() < 1)
570 setScrollValues(hscroll.getValue(), vscroll.getValue() - 1);
574 if (vextent + vscroll.getValue() >= av.getAlignment().getHeight())
578 setScrollValues(hscroll.getValue(), vscroll.getValue() + 1);
585 public boolean scrollRight(boolean right)
589 if (hscroll.getValue() < 1)
593 setScrollValues(hscroll.getValue() - 1, vscroll.getValue());
597 if (hextent + hscroll.getValue() >= av.getAlignment().getWidth())
601 setScrollValues(hscroll.getValue() + 1, vscroll.getValue());
608 public void setScrollValues(int x, int y)
610 int width = av.getAlignment().getWidth();
611 int height = av.getAlignment().getHeight();
613 if (av.hasHiddenColumns())
615 width = av.getColumnSelection().findColumnPosition(width);
623 hextent = seqPanel.seqCanvas.getSize().width / av.charWidth;
624 vextent = seqPanel.seqCanvas.getSize().height / av.charHeight;
631 if (vextent > height)
636 if ((hextent + x) > width)
638 System.err.println("hextent was " + hextent + " and x was " + x);
643 if ((vextent + y) > height)
645 y = height - vextent;
655 System.err.println("x was " + x);
661 int endSeq = y + vextent;
662 if (endSeq > av.getAlignment().getHeight())
664 endSeq = av.getAlignment().getHeight();
667 av.setEndSeq(endSeq);
669 av.setEndRes((x + (seqPanel.seqCanvas.getSize().width / av.charWidth)) - 1);
671 hscroll.setValues(x, hextent, 0, width);
672 vscroll.setValues(y, vextent, 0, height);
674 if (overviewPanel != null)
676 overviewPanel.setBoxPosition();
682 public void adjustmentValueChanged(AdjustmentEvent evt)
684 int oldX = av.getStartRes();
685 int oldY = av.getStartSeq();
687 if (evt == null || evt.getSource() == apvscroll)
689 annotationPanel.setScrollOffset(apvscroll.getValue());
690 alabels.setScrollOffset(apvscroll.getValue());
691 // annotationPanel.image=null;
692 // alabels.image=null;
693 // alabels.repaint();
694 // annotationPanel.repaint();
696 if (evt == null || evt.getSource() == hscroll)
698 int x = hscroll.getValue();
700 av.setEndRes(x + seqPanel.seqCanvas.getSize().width
701 / av.getCharWidth() - 1);
704 if (evt == null || evt.getSource() == vscroll)
706 int offy = vscroll.getValue();
707 if (av.getWrapAlignment())
709 int rowSize = seqPanel.seqCanvas
710 .getWrappedCanvasWidth(seqPanel.seqCanvas.getSize().width);
711 av.setStartRes(vscroll.getValue() * rowSize);
712 av.setEndRes((vscroll.getValue() + 1) * rowSize);
716 av.setStartSeq(offy);
717 av.setEndSeq(offy + seqPanel.seqCanvas.getSize().height
718 / av.getCharHeight());
722 if (overviewPanel != null)
724 overviewPanel.setBoxPosition();
727 int scrollX = av.startRes - oldX;
728 int scrollY = av.startSeq - oldY;
730 if (av.getWrapAlignment() || !fastPaint || av.MAC)
736 // Make sure we're not trying to draw a panel
737 // larger than the visible window
738 if (scrollX > av.endRes - av.startRes)
740 scrollX = av.endRes - av.startRes;
742 else if (scrollX < av.startRes - av.endRes)
744 scrollX = av.startRes - av.endRes;
747 idPanel.idCanvas.fastPaint(scrollY);
748 seqPanel.seqCanvas.fastPaint(scrollX, scrollY);
750 scalePanel.repaint();
751 if (av.getShowAnnotation())
753 annotationPanel.fastPaint(av.getStartRes() - oldX);
760 private void sendViewPosition()
762 StructureSelectionManager.getStructureSelectionManager(av.applet)
763 .sendViewPosition(this, av.startRes, av.endRes, av.startSeq,
767 public void paintAlignment(boolean updateOverview)
773 jalview.structure.StructureSelectionManager
774 .getStructureSelectionManager(av.applet)
775 .sequenceColoursChanged(this);
777 if (overviewPanel != null)
779 overviewPanel.updateOverviewImage();
784 public void update(Graphics g)
789 public void paint(Graphics g)
792 Dimension d = idPanel.idCanvas.getSize();
793 idPanel.idCanvas.setSize(d.width, seqPanel.seqCanvas.getSize().height);
795 if (av.getWrapAlignment())
797 int maxwidth = av.getAlignment().getWidth();
799 if (av.hasHiddenColumns())
801 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
804 int canvasWidth = seqPanel.seqCanvas
805 .getWrappedCanvasWidth(seqPanel.seqCanvas.getSize().width);
809 int max = maxwidth / canvasWidth;
810 vscroll.setMaximum(1 + max);
811 vscroll.setUnitIncrement(1);
812 vscroll.setVisibleAmount(1);
817 setScrollValues(av.getStartRes(), av.getStartSeq());
822 seqPanel.seqCanvas.repaint();
823 scalePanel.repaint();
824 annotationPanel.repaint();
825 idPanel.idCanvas.repaint();
828 protected Panel sequenceHolderPanel = new Panel();
830 protected Scrollbar vscroll = new Scrollbar();
832 protected Scrollbar hscroll = new Scrollbar();
834 protected Panel seqPanelHolder = new Panel();
836 BorderLayout borderLayout1 = new BorderLayout();
838 BorderLayout borderLayout3 = new BorderLayout();
840 protected Panel scalePanelHolder = new Panel();
842 protected Panel idPanelHolder = new Panel();
844 BorderLayout borderLayout5 = new BorderLayout();
846 protected Panel idSpaceFillerPanel1 = new Panel();
848 public Panel annotationSpaceFillerHolder = new Panel();
850 BorderLayout borderLayout6 = new BorderLayout();
852 BorderLayout borderLayout7 = new BorderLayout();
854 Panel hscrollHolder = new Panel();
856 BorderLayout borderLayout10 = new BorderLayout();
858 protected Panel hscrollFillerPanel = new Panel();
860 BorderLayout borderLayout11 = new BorderLayout();
862 BorderLayout borderLayout4 = new BorderLayout();
864 BorderLayout borderLayout2 = new BorderLayout();
866 Panel annotationPanelHolder = new Panel();
868 protected Scrollbar apvscroll = new Scrollbar();
870 BorderLayout borderLayout12 = new BorderLayout();
872 private void jbInit() throws Exception
874 // idPanelHolder.setPreferredSize(new Dimension(70, 10));
875 this.setLayout(borderLayout7);
877 // sequenceHolderPanel.setPreferredSize(new Dimension(150, 150));
878 sequenceHolderPanel.setLayout(borderLayout3);
879 seqPanelHolder.setLayout(borderLayout1);
880 scalePanelHolder.setBackground(Color.white);
882 // scalePanelHolder.setPreferredSize(new Dimension(10, 30));
883 scalePanelHolder.setLayout(borderLayout6);
884 idPanelHolder.setLayout(borderLayout5);
885 idSpaceFillerPanel1.setBackground(Color.white);
887 // idSpaceFillerPanel1.setPreferredSize(new Dimension(10, 30));
888 idSpaceFillerPanel1.setLayout(borderLayout11);
889 annotationSpaceFillerHolder.setBackground(Color.white);
891 // annotationSpaceFillerHolder.setPreferredSize(new Dimension(10, 80));
892 annotationSpaceFillerHolder.setLayout(borderLayout4);
893 hscroll.setOrientation(Scrollbar.HORIZONTAL);
894 hscrollHolder.setLayout(borderLayout10);
895 hscrollFillerPanel.setBackground(Color.white);
896 apvscroll.setOrientation(Scrollbar.VERTICAL);
897 apvscroll.setVisible(true);
898 apvscroll.addAdjustmentListener(this);
900 annotationPanelHolder.setBackground(Color.white);
901 annotationPanelHolder.setLayout(borderLayout12);
902 annotationPanelHolder.add(apvscroll, BorderLayout.EAST);
903 // hscrollFillerPanel.setPreferredSize(new Dimension(70, 10));
904 hscrollHolder.setBackground(Color.white);
906 // annotationScroller.setPreferredSize(new Dimension(10, 80));
907 // this.setPreferredSize(new Dimension(220, 166));
908 seqPanelHolder.setBackground(Color.white);
909 idPanelHolder.setBackground(Color.white);
910 sequenceHolderPanel.add(scalePanelHolder, BorderLayout.NORTH);
911 sequenceHolderPanel.add(seqPanelHolder, BorderLayout.CENTER);
912 seqPanelHolder.add(vscroll, BorderLayout.EAST);
914 // Panel3.add(secondaryPanelHolder, BorderLayout.SOUTH);
915 this.add(idPanelHolder, BorderLayout.WEST);
916 idPanelHolder.add(idSpaceFillerPanel1, BorderLayout.NORTH);
917 idPanelHolder.add(annotationSpaceFillerHolder, BorderLayout.SOUTH);
918 this.add(hscrollHolder, BorderLayout.SOUTH);
919 hscrollHolder.add(hscroll, BorderLayout.CENTER);
920 hscrollHolder.add(hscrollFillerPanel, BorderLayout.WEST);
921 this.add(sequenceHolderPanel, BorderLayout.CENTER);
925 * hides or shows dynamic annotation rows based on groups and av state flags
927 public void updateAnnotation()
929 updateAnnotation(false);
932 public void updateAnnotation(boolean applyGlobalSettings)
934 updateAnnotation(applyGlobalSettings, false);
937 public void updateAnnotation(boolean applyGlobalSettings,
938 boolean preserveNewGroupSettings)
940 av.updateGroupAnnotationSettings(applyGlobalSettings,
941 preserveNewGroupSettings);
942 adjustAnnotationHeight();
946 public AlignmentI getAlignment()
948 return av.getAlignment();
952 public StructureSelectionManager getStructureSelectionManager()
954 return StructureSelectionManager
955 .getStructureSelectionManager(av.applet);
959 public void raiseOOMWarning(String string, OutOfMemoryError error)
962 System.err.println("Out of memory whilst '" + string + "'");
963 error.printStackTrace();