2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ 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
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import jalview.analysis.AnnotationSorter;
24 import jalview.api.AlignViewportI;
25 import jalview.api.AlignmentViewPanel;
26 import jalview.bin.Cache;
27 import jalview.datamodel.AlignmentI;
28 import jalview.datamodel.SearchResultsI;
29 import jalview.datamodel.SequenceFeature;
30 import jalview.datamodel.SequenceGroup;
31 import jalview.datamodel.SequenceI;
32 import jalview.jbgui.GAlignmentPanel;
33 import jalview.math.AlignmentDimension;
34 import jalview.schemes.ResidueProperties;
35 import jalview.structure.StructureSelectionManager;
36 import jalview.util.MessageManager;
37 import jalview.util.Platform;
38 import jalview.viewmodel.ViewportRanges;
40 import java.awt.BorderLayout;
41 import java.awt.Color;
42 import java.awt.Container;
43 import java.awt.Dimension;
45 import java.awt.FontMetrics;
46 import java.awt.Graphics;
47 import java.awt.Insets;
48 import java.awt.event.AdjustmentEvent;
49 import java.awt.event.AdjustmentListener;
50 import java.awt.print.PageFormat;
51 import java.awt.print.Printable;
52 import java.awt.print.PrinterException;
53 import java.beans.PropertyChangeEvent;
54 import java.beans.PropertyChangeListener;
56 import java.io.FileWriter;
57 import java.io.PrintWriter;
58 import java.util.List;
60 import javax.swing.SwingUtilities;
66 * @version $Revision: 1.161 $
68 public class AlignmentPanel extends GAlignmentPanel implements
69 AdjustmentListener, Printable, AlignmentViewPanel
71 public AlignViewport av;
73 ViewportRanges vpRanges;
75 OverviewPanel overviewPanel;
77 private SeqPanel seqPanel;
79 private IdPanel idPanel;
81 private boolean headless;
83 IdwidthAdjuster idwidthAdjuster;
86 public AlignFrame alignFrame;
88 private ScalePanel scalePanel;
90 private AnnotationPanel annotationPanel;
92 private AnnotationLabels alabels;
94 // this value is set false when selection area being dragged
95 boolean fastPaint = true;
97 private int hextent = 0;
99 private int vextent = 0;
102 * Flag set while scrolling to follow complementary cDNA/protein scroll. When
103 * true, suppresses invoking the same method recursively.
105 private boolean dontScrollComplement;
107 private PropertyChangeListener propertyChangeListener;
110 * Creates a new AlignmentPanel object.
115 public AlignmentPanel(AlignFrame af, final AlignViewport av)
119 vpRanges = av.getRanges();
120 setSeqPanel(new SeqPanel(av, this));
121 setIdPanel(new IdPanel(av, this));
123 setScalePanel(new ScalePanel(av, this));
125 idPanelHolder.add(getIdPanel(), BorderLayout.CENTER);
126 idwidthAdjuster = new IdwidthAdjuster(this);
127 idSpaceFillerPanel1.add(idwidthAdjuster, BorderLayout.CENTER);
129 setAnnotationPanel(new AnnotationPanel(this));
130 setAlabels(new AnnotationLabels(this));
132 annotationScroller.setViewportView(getAnnotationPanel());
133 annotationSpaceFillerHolder.add(getAlabels(), BorderLayout.CENTER);
135 scalePanelHolder.add(getScalePanel(), BorderLayout.CENTER);
136 seqPanelHolder.add(getSeqPanel(), BorderLayout.CENTER);
138 setScrollValues(0, 0);
140 hscroll.addAdjustmentListener(this);
141 vscroll.addAdjustmentListener(this);
143 final AlignmentPanel ap = this;
144 propertyChangeListener = new PropertyChangeListener()
147 public void propertyChange(PropertyChangeEvent evt)
149 if (evt.getPropertyName().equals("alignment"))
151 PaintRefresher.Refresh(ap, av.getSequenceSetId(), true, true);
156 av.addPropertyChangeListener(propertyChangeListener);
158 adjustAnnotationHeight();
163 public AlignViewportI getAlignViewport()
168 public void alignmentChanged()
170 av.alignmentChanged(this);
172 alignFrame.updateEditMenuBar();
174 paintAlignment(true);
181 public void fontChanged()
183 // set idCanvas bufferedImage to null
184 // to prevent drawing old image
185 FontMetrics fm = getFontMetrics(av.getFont());
187 scalePanelHolder.setPreferredSize(new Dimension(10, av.getCharHeight()
189 idSpaceFillerPanel1.setPreferredSize(new Dimension(10, av
190 .getCharHeight() + fm.getDescent()));
192 getIdPanel().getIdCanvas().gg = null;
193 getSeqPanel().seqCanvas.img = null;
194 getAnnotationPanel().adjustPanelHeight();
196 Dimension d = calculateIdWidth();
198 d.setSize(d.width + 4, d.height);
199 getIdPanel().getIdCanvas().setPreferredSize(d);
200 hscrollFillerPanel.setPreferredSize(d);
202 if (overviewPanel != null)
204 overviewPanel.setBoxPosition();
206 if (this.alignFrame.getSplitViewContainer() != null)
208 ((SplitFrame) this.alignFrame.getSplitViewContainer()).adjustLayout();
215 * Calculate the width of the alignment labels based on the displayed names
216 * and any bounds on label width set in preferences.
218 * @return Dimension giving the maximum width of the alignment label panel
219 * that should be used.
221 public Dimension calculateIdWidth()
223 // calculate sensible default width when no preference is available
225 if (av.getIdWidth() < 0)
227 int afwidth = (alignFrame != null ? alignFrame.getWidth() : 300);
228 int maxwidth = Math.max(20, Math.min(afwidth - 200, 2 * afwidth / 3));
229 r = calculateIdWidth(maxwidth);
230 av.setIdWidth(r.width);
235 r.width = av.getIdWidth();
242 * Calculate the width of the alignment labels based on the displayed names
243 * and any bounds on label width set in preferences.
246 * -1 or maximum width allowed for IdWidth
247 * @return Dimension giving the maximum width of the alignment label panel
248 * that should be used.
250 public Dimension calculateIdWidth(int maxwidth)
252 Container c = new Container();
254 FontMetrics fm = c.getFontMetrics(new Font(av.font.getName(),
255 Font.ITALIC, av.font.getSize()));
257 AlignmentI al = av.getAlignment();
262 while ((i < al.getHeight()) && (al.getSequenceAt(i) != null))
264 SequenceI s = al.getSequenceAt(i);
266 id = s.getDisplayId(av.getShowJVSuffix());
268 if (fm.stringWidth(id) > idWidth)
270 idWidth = fm.stringWidth(id);
276 // Also check annotation label widths
279 if (al.getAlignmentAnnotation() != null)
281 fm = c.getFontMetrics(getAlabels().getFont());
283 while (i < al.getAlignmentAnnotation().length)
285 String label = al.getAlignmentAnnotation()[i].label;
287 if (fm.stringWidth(label) > idWidth)
289 idWidth = fm.stringWidth(label);
296 return new Dimension(maxwidth < 0 ? idWidth : Math.min(maxwidth,
301 * Highlight the given results on the alignment.
304 public void highlightSearchResults(SearchResultsI results)
306 scrollToPosition(results);
307 getSeqPanel().seqCanvas.highlightSearchResults(results);
311 * Scroll the view to show the position of the highlighted region in results
312 * (if any) and redraw the overview
316 public boolean scrollToPosition(SearchResultsI results)
318 return scrollToPosition(results, 0, true, false);
322 * Scroll the view to show the position of the highlighted region in results
325 * @param searchResults
326 * @param redrawOverview
329 public boolean scrollToPosition(SearchResultsI searchResults,
330 boolean redrawOverview)
332 return scrollToPosition(searchResults, 0, redrawOverview, false);
336 * Scroll the view to show the position of the highlighted region in results
340 * @param verticalOffset
341 * if greater than zero, allows scrolling to a position below the
342 * first displayed sequence
343 * @param redrawOverview
344 * - when set, the overview will be recalculated (takes longer)
346 * if true, try to centre the search results horizontally in the view
347 * @return false if results were not found
349 public boolean scrollToPosition(SearchResultsI results,
350 int verticalOffset, boolean redrawOverview, boolean centre)
352 int startv, endv, starts, ends;
353 // TODO: properly locate search results in view when large numbers of hidden
354 // columns exist before highlighted region
355 // do we need to scroll the panel?
356 // TODO: tons of nullpointerexceptions raised here.
357 if (results != null && results.getSize() > 0 && av != null
358 && av.getAlignment() != null)
360 int seqIndex = av.getAlignment().findIndex(results);
365 SequenceI seq = av.getAlignment().getSequenceAt(seqIndex);
367 int[] r = results.getResults(seq, 0, av.getAlignment().getWidth());
375 // System.err.println(this.av.viewName + " Seq : " + seqIndex
376 // + " Scroll to " + start + "," + end);
379 * To centre results, scroll to positions half the visible width
380 * left/right of the start/end positions
384 int offset = (vpRanges.getEndRes() - vpRanges.getStartRes() + 1) / 2 - 1;
385 start = Math.max(start - offset, 0);
386 end = end + offset - 1;
392 if (end == seq.getEnd())
396 if (av.hasHiddenColumns())
398 start = av.getColumnSelection().findColumnPosition(start);
399 end = av.getColumnSelection().findColumnPosition(end);
402 if (!av.getColumnSelection().isVisible(r[0]))
404 // don't scroll - position isn't visible
411 * allow for offset of target sequence (actually scroll to one above it)
413 seqIndex = Math.max(0, seqIndex - verticalOffset);
415 // System.out.println("start=" + start + ", end=" + end + ", startv="
416 // + av.getStartRes() + ", endv=" + av.getEndRes() + ", starts="
417 // + av.getStartSeq() + ", ends=" + av.getEndSeq());
418 if (!av.getWrapAlignment())
420 if ((startv = vpRanges.getStartRes()) >= start)
423 * Scroll left to make start of search results visible
425 // setScrollValues(start - 1, seqIndex); // plus one residue
426 setScrollValues(start, seqIndex);
428 else if ((endv = vpRanges.getEndRes()) <= end)
431 * Scroll right to make end of search results visible
433 // setScrollValues(startv + 1 + end - endv, seqIndex); // plus one
434 setScrollValues(startv + end - endv, seqIndex);
436 else if ((starts = vpRanges.getStartSeq()) > seqIndex)
439 * Scroll up to make start of search results visible
441 setScrollValues(vpRanges.getStartRes(), seqIndex);
443 else if ((ends = vpRanges.getEndSeq()) <= seqIndex)
446 * Scroll down to make end of search results visible
448 setScrollValues(vpRanges.getStartRes(), starts + seqIndex - ends
452 * Else results are already visible - no need to scroll
457 scrollToWrappedVisible(start);
460 if (redrawOverview && overviewPanel != null)
462 overviewPanel.setBoxPosition();
464 paintAlignment(redrawOverview);
468 void scrollToWrappedVisible(int res)
470 int cwidth = getSeqPanel().seqCanvas
471 .getWrappedCanvasWidth(getSeqPanel().seqCanvas.getWidth());
472 if (res < vpRanges.getStartRes()
473 || res >= (vpRanges.getStartRes() + cwidth))
475 vscroll.setValue((res / cwidth));
476 vpRanges.setStartRes(vscroll.getValue() * cwidth);
484 * @return DOCUMENT ME!
486 public OverviewPanel getOverviewPanel()
488 return overviewPanel;
497 public void setOverviewPanel(OverviewPanel op)
505 * Hide or show annotation panel
508 public void setAnnotationVisible(boolean b)
510 if (!av.getWrapAlignment())
512 annotationSpaceFillerHolder.setVisible(b);
513 annotationScroller.setVisible(b);
519 * automatically adjust annotation panel height for new annotation whilst
520 * ensuring the alignment is still visible.
523 public void adjustAnnotationHeight()
525 // TODO: display vertical annotation scrollbar if necessary
526 // this is called after loading new annotation onto alignment
527 if (alignFrame.getHeight() == 0)
529 System.out.println("NEEDS FIXING");
531 validateAnnotationDimensions(true);
533 paintAlignment(true);
537 * calculate the annotation dimensions and refresh slider values accordingly.
538 * need to do repaints/notifys afterwards.
540 protected void validateAnnotationDimensions(boolean adjustPanelHeight)
542 int annotationHeight = getAnnotationPanel().adjustPanelHeight();
544 if (adjustPanelHeight)
546 int rowHeight = av.getCharHeight();
547 int alignmentHeight = rowHeight * av.getAlignment().getHeight();
550 * Estimate available height in the AlignFrame for alignment +
551 * annotations. Deduct an estimate for title bar, menu bar, scale panel,
552 * hscroll, status bar (as these are not laid out we can't inspect their
553 * actual heights). Insets gives frame borders.
555 int stuff = Platform.isAMac() ? 80 : 100;
556 Insets insets = alignFrame.getInsets();
557 int availableHeight = alignFrame.getHeight() - stuff - insets.top
561 * If not enough vertical space, maximize annotation height while keeping
562 * at least two rows of alignment visible
564 if (annotationHeight + alignmentHeight > availableHeight)
566 annotationHeight = Math.min(annotationHeight, availableHeight - 2
572 // maintain same window layout whilst updating sliders
573 annotationHeight = annotationScroller.getSize().height;
577 annotationScroller.setPreferredSize(new Dimension(annotationScroller
578 .getWidth(), annotationHeight));
580 Dimension e = idPanel.getSize();
581 alabels.setSize(new Dimension(e.width, annotationHeight));
583 annotationSpaceFillerHolder.setPreferredSize(new Dimension(
584 annotationSpaceFillerHolder.getWidth(), annotationHeight));
585 annotationScroller.validate();
586 annotationScroller.addNotify();
590 * update alignment layout for viewport settings
595 public void updateLayout()
598 setAnnotationVisible(av.isShowAnnotation());
599 boolean wrap = av.getWrapAlignment();
600 vpRanges.setStartSeq(0);
601 scalePanelHolder.setVisible(!wrap);
602 hscroll.setVisible(!wrap);
603 idwidthAdjuster.setVisible(!wrap);
607 annotationScroller.setVisible(false);
608 annotationSpaceFillerHolder.setVisible(false);
610 else if (av.isShowAnnotation())
612 annotationScroller.setVisible(true);
613 annotationSpaceFillerHolder.setVisible(true);
616 idSpaceFillerPanel1.setVisible(!wrap);
621 // return value is true if the scroll is valid
622 public boolean scrollUp(boolean up)
626 if (vscroll.getValue() < 1)
632 vscroll.setValue(vscroll.getValue() - 1);
636 if ((vextent + vscroll.getValue()) >= av.getAlignment().getHeight())
642 vscroll.setValue(vscroll.getValue() + 1);
656 * @return DOCUMENT ME!
658 public boolean scrollRight(boolean right)
662 if (hscroll.getValue() < 1)
668 hscroll.setValue(hscroll.getValue() - 1);
672 if ((hextent + hscroll.getValue()) >= av.getAlignment().getWidth())
678 hscroll.setValue(hscroll.getValue() + 1);
687 * Adjust row/column scrollers to show a visible position in the alignment.
690 * visible column to scroll to
692 * visible row to scroll to
695 public void setScrollValues(int x, int y)
697 if (av == null || av.getAlignment() == null)
701 int width = av.getAlignment().getWidth();
702 int height = av.getAlignment().getHeight();
704 if (av.hasHiddenColumns())
706 // reset the width to exclude hidden columns
707 width = av.getColumnSelection().findColumnPosition(width);
710 hextent = getSeqPanel().seqCanvas.getWidth() / av.getCharWidth();
711 vextent = getSeqPanel().seqCanvas.getHeight() / av.getCharHeight();
718 if (vextent > height)
723 if ((hextent + x) > width)
728 if ((vextent + y) > height)
730 y = height - vextent;
743 // update endRes after x has (possibly) been adjusted
744 vpRanges.setEndRes((x + (getSeqPanel().seqCanvas.getWidth() / av
745 .getCharWidth())) - 1);
748 * each scroll adjustment triggers adjustmentValueChanged, which resets the
749 * 'do not scroll complement' flag; ensure it is the same for both
752 boolean flag = isDontScrollComplement();
753 hscroll.setValues(x, hextent, 0, width);
754 setDontScrollComplement(flag);
755 vscroll.setValues(y, vextent, 0, height);
765 public void adjustmentValueChanged(AdjustmentEvent evt)
767 int oldX = vpRanges.getStartRes();
768 int oldY = vpRanges.getStartSeq();
770 if (evt.getSource() == hscroll)
772 int x = hscroll.getValue();
773 vpRanges.setStartRes(x);
774 vpRanges.setEndRes((x + (getSeqPanel().seqCanvas.getWidth() / av
775 .getCharWidth())) - 1);
778 if (evt.getSource() == vscroll)
780 int offy = vscroll.getValue();
782 if (av.getWrapAlignment())
786 int rowSize = getSeqPanel().seqCanvas
787 .getWrappedCanvasWidth(getSeqPanel().seqCanvas.getWidth());
788 vpRanges.setStartRes(offy * rowSize);
789 vpRanges.setEndRes((offy + 1) * rowSize);
793 // This is only called if file loaded is a jar file that
794 // was wrapped when saved and user has wrap alignment true
795 // as preference setting
796 SwingUtilities.invokeLater(new Runnable()
801 setScrollValues(vpRanges.getStartRes(),
802 vpRanges.getStartSeq());
809 vpRanges.setStartSeq(offy);
810 vpRanges.setEndSeq(offy
811 + (getSeqPanel().seqCanvas.getHeight() / av.getCharHeight())
816 if (overviewPanel != null)
818 overviewPanel.setBoxPosition();
821 int scrollX = vpRanges.getStartRes() - oldX;
822 int scrollY = vpRanges.getStartSeq() - oldY;
824 if (av.getWrapAlignment() || !fastPaint)
830 // Make sure we're not trying to draw a panel
831 // larger than the visible window
832 if (scrollX > vpRanges.getEndRes() - vpRanges.getStartRes())
834 scrollX = vpRanges.getEndRes() - vpRanges.getStartRes();
836 else if (scrollX < vpRanges.getStartRes() - vpRanges.getEndRes())
838 scrollX = vpRanges.getStartRes() - vpRanges.getEndRes();
841 if (scrollX != 0 || scrollY != 0)
843 getIdPanel().getIdCanvas().fastPaint(scrollY);
844 getSeqPanel().seqCanvas.fastPaint(scrollX, scrollY);
845 getScalePanel().repaint();
847 if (av.isShowAnnotation() && scrollX != 0)
849 getAnnotationPanel().fastPaint(scrollX);
854 * If there is one, scroll the (Protein/cDNA) complementary alignment to
855 * match, unless we are ourselves doing that.
857 if (isDontScrollComplement())
859 setDontScrollComplement(false);
863 av.scrollComplementaryAlignment();
868 * Repaint the alignment including the annotations and overview panels (if
872 public void paintAlignment(boolean updateOverview)
874 final AnnotationSorter sorter = new AnnotationSorter(getAlignment(),
875 av.isShowAutocalculatedAbove());
876 sorter.sort(getAlignment().getAlignmentAnnotation(),
877 av.getSortAnnotationsBy());
882 // TODO: determine if this paintAlignment changed structure colours
883 av.getStructureSelectionManager().sequenceColoursChanged(this);
885 if (overviewPanel != null)
887 overviewPanel.updateOverviewImage();
899 public void paintComponent(Graphics g)
903 Dimension d = getIdPanel().getIdCanvas().getPreferredSize();
904 idPanelHolder.setPreferredSize(d);
905 hscrollFillerPanel.setPreferredSize(new Dimension(d.width, 12));
909 * set scroll bar positions; first suppress this being 'followed' in any
910 * complementary split pane
912 setDontScrollComplement(true);
914 if (av.getWrapAlignment())
916 int maxwidth = av.getAlignment().getWidth();
918 if (av.hasHiddenColumns())
920 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
923 int canvasWidth = getSeqPanel().seqCanvas
924 .getWrappedCanvasWidth(getSeqPanel().seqCanvas.getWidth());
928 / getSeqPanel().seqCanvas
929 .getWrappedCanvasWidth(getSeqPanel().seqCanvas
931 vscroll.setMaximum(max);
932 vscroll.setUnitIncrement(1);
933 vscroll.setVisibleAmount(1);
938 setScrollValues(vpRanges.getStartRes(), vpRanges.getStartSeq());
952 * @return DOCUMENT ME!
954 * @throws PrinterException
958 public int print(Graphics pg, PageFormat pf, int pi)
959 throws PrinterException
961 pg.translate((int) pf.getImageableX(), (int) pf.getImageableY());
963 int pwidth = (int) pf.getImageableWidth();
964 int pheight = (int) pf.getImageableHeight();
966 if (av.getWrapAlignment())
968 return printWrappedAlignment(pwidth, pheight, pi, pg);
972 return printUnwrapped(pwidth, pheight, pi, pg, pg);
988 * @return DOCUMENT ME!
990 * @throws PrinterException
994 * Draws the alignment image, including sequence ids, sequences, and
995 * annotation labels and annotations if shown, on either one or two Graphics
1002 * the graphics context for sequence ids and annotation labels
1003 * @param alignmentGraphics
1004 * the graphics context for sequences and annotations (may or may not
1005 * be the same context as idGraphics)
1007 * @throws PrinterException
1009 public int printUnwrapped(int pageWidth, int pageHeight, int pi,
1010 Graphics idGraphics, Graphics alignmentGraphics)
1011 throws PrinterException
1013 final int idWidth = getVisibleIdWidth(false);
1016 * Get the horizontal offset to where we draw the sequences.
1017 * This is idWidth if using a single Graphics context, else zero.
1019 final int alignmentGraphicsOffset = idGraphics != alignmentGraphics ? 0 : idWidth;
1021 FontMetrics fm = getFontMetrics(av.getFont());
1022 int charHeight = av.getCharHeight();
1023 int scaleHeight = charHeight + fm.getDescent();
1025 idGraphics.setColor(Color.white);
1026 idGraphics.fillRect(0, 0, pageWidth, pageHeight);
1027 idGraphics.setFont(av.getFont());
1030 * How many sequences and residues can we fit on a printable page?
1032 int totalRes = (pageWidth - idWidth) / av.getCharWidth();
1034 int totalSeq = (pageHeight - scaleHeight) / charHeight - 1;
1036 int alignmentWidth = av.getAlignment().getWidth();
1037 int pagesWide = (alignmentWidth / totalRes) + 1;
1039 final int startRes = (pi % pagesWide) * totalRes;
1040 int endRes = (startRes + totalRes) - 1;
1042 if (endRes > (alignmentWidth - 1))
1044 endRes = alignmentWidth - 1;
1047 final int startSeq = (pi / pagesWide) * totalSeq;
1048 int endSeq = startSeq + totalSeq;
1050 int alignmentHeight = av.getAlignment().getHeight();
1051 if (endSeq > alignmentHeight)
1053 endSeq = alignmentHeight;
1056 int pagesHigh = ((alignmentHeight / totalSeq) + 1)
1059 if (av.isShowAnnotation())
1061 pagesHigh += getAnnotationPanel().adjustPanelHeight() + 3;
1064 pagesHigh /= pageHeight;
1066 if (pi >= (pagesWide * pagesHigh))
1068 return Printable.NO_SUCH_PAGE;
1070 final int alignmentDrawnHeight = (endSeq - startSeq) * charHeight
1074 * draw the Scale at horizontal offset, then reset to top left (0, 0)
1076 alignmentGraphics.translate(alignmentGraphicsOffset, 0);
1077 getScalePanel().drawScale(alignmentGraphics, startRes, endRes,
1078 pageWidth - idWidth, scaleHeight);
1079 alignmentGraphics.translate(-alignmentGraphicsOffset, 0);
1082 * Draw the sequence ids, offset for scale height,
1083 * then reset to top left (0, 0)
1085 idGraphics.translate(0, scaleHeight);
1086 idGraphics.setFont(getIdPanel().getIdCanvas().getIdfont());
1087 Color currentColor = null;
1088 Color currentTextColor = null;
1091 for (int i = startSeq; i < endSeq; i++)
1093 seq = av.getAlignment().getSequenceAt(i);
1094 if ((av.getSelectionGroup() != null)
1095 && av.getSelectionGroup().getSequences(null).contains(seq))
1098 * gray out ids of sequences in selection group (if any)
1100 currentColor = Color.gray;
1101 currentTextColor = Color.black;
1105 currentColor = av.getSequenceColour(seq);
1106 currentTextColor = Color.black;
1109 idGraphics.setColor(currentColor);
1110 idGraphics.fillRect(0, (i - startSeq) * charHeight, idWidth,
1113 idGraphics.setColor(currentTextColor);
1116 String displayId = seq.getDisplayId(av.getShowJVSuffix());
1117 if (av.isRightAlignIds())
1119 fm = idGraphics.getFontMetrics();
1121 - fm.stringWidth(displayId)
1125 idGraphics.drawString(displayId, xPos,
1126 (((i - startSeq) * charHeight) + charHeight)
1127 - (charHeight / 5));
1129 idGraphics.setFont(av.getFont());
1130 idGraphics.translate(0, -scaleHeight);
1133 * draw the sequences, offset for scale height, and id width (if using a
1134 * single graphics context), then reset to (0, scale height)
1136 alignmentGraphics.translate(alignmentGraphicsOffset, scaleHeight);
1137 getSeqPanel().seqCanvas.drawPanel(alignmentGraphics, startRes, endRes,
1138 startSeq, endSeq, 0);
1139 alignmentGraphics.translate(-alignmentGraphicsOffset, 0);
1141 if (av.isShowAnnotation() && (endSeq == alignmentHeight))
1144 * draw annotation labels; drawComponent() translates by
1145 * getScrollOffset(), so compensate for that first;
1146 * then reset to (0, scale height)
1148 int offset = getAlabels().getScrollOffset();
1149 idGraphics.translate(0, -offset);
1150 idGraphics.translate(0, alignmentDrawnHeight);
1151 getAlabels().drawComponent(idGraphics, idWidth);
1152 idGraphics.translate(0, -alignmentDrawnHeight);
1155 * draw the annotations starting at
1156 * (idOffset, alignmentHeight) from (0, scaleHeight)
1158 alignmentGraphics.translate(alignmentGraphicsOffset, alignmentDrawnHeight);
1159 getAnnotationPanel().renderer.drawComponent(getAnnotationPanel(), av,
1160 alignmentGraphics, -1, startRes, endRes + 1);
1163 return Printable.PAGE_EXISTS;
1178 * @return DOCUMENT ME!
1180 * @throws PrinterException
1183 public int printWrappedAlignment(int pwidth, int pheight, int pi,
1184 Graphics pg) throws PrinterException
1186 int annotationHeight = 0;
1187 AnnotationLabels labels = null;
1188 if (av.isShowAnnotation())
1190 annotationHeight = getAnnotationPanel().adjustPanelHeight();
1191 labels = new AnnotationLabels(av);
1194 int hgap = av.getCharHeight();
1195 if (av.getScaleAboveWrapped())
1197 hgap += av.getCharHeight();
1200 int cHeight = av.getAlignment().getHeight() * av.getCharHeight() + hgap
1203 int idWidth = getVisibleIdWidth(false);
1205 int maxwidth = av.getAlignment().getWidth();
1206 if (av.hasHiddenColumns())
1208 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
1211 int resWidth = getSeqPanel().seqCanvas.getWrappedCanvasWidth(pwidth
1214 int totalHeight = cHeight * (maxwidth / resWidth + 1);
1216 pg.setColor(Color.white);
1217 pg.fillRect(0, 0, pwidth, pheight);
1218 pg.setFont(av.getFont());
1222 pg.setColor(Color.black);
1224 pg.translate(0, -pi * pheight);
1226 pg.setClip(0, pi * pheight, pwidth, pheight);
1232 for (int i = 0; i < av.getAlignment().getHeight(); i++)
1234 pg.setFont(getIdPanel().getIdCanvas().getIdfont());
1235 SequenceI s = av.getAlignment().getSequenceAt(i);
1236 String string = s.getDisplayId(av.getShowJVSuffix());
1238 if (av.isRightAlignIds())
1240 FontMetrics fm = pg.getFontMetrics();
1241 xPos = idWidth - fm.stringWidth(string) - 4;
1243 pg.drawString(string, xPos,
1244 ((i * av.getCharHeight()) + ypos + av.getCharHeight())
1245 - (av.getCharHeight() / 5));
1250 ypos + (av.getAlignment().getHeight() * av.getCharHeight()));
1252 pg.setFont(av.getFont());
1253 labels.drawComponent(pg, idWidth);
1257 - (av.getAlignment().getHeight() * av
1262 } while (ypos < totalHeight);
1264 pg.translate(idWidth, 0);
1266 getSeqPanel().seqCanvas.drawWrappedPanel(pg, pwidth - idWidth,
1269 if ((pi * pheight) < totalHeight)
1271 return Printable.PAGE_EXISTS;
1276 return Printable.NO_SUCH_PAGE;
1281 * get current sequence ID panel width, or nominal value if panel were to be
1282 * displayed using default settings
1286 public int getVisibleIdWidth()
1288 return getVisibleIdWidth(true);
1292 * get current sequence ID panel width, or nominal value if panel were to be
1293 * displayed using default settings
1296 * indicate if the Id width for onscreen or offscreen display should
1300 public int getVisibleIdWidth(boolean onscreen)
1302 // see if rendering offscreen - check preferences and calc width accordingly
1303 if (!onscreen && Cache.getDefault("FIGURE_AUTOIDWIDTH", false))
1305 return calculateIdWidth(-1).width + 4;
1307 Integer idwidth = null;
1309 || (idwidth = Cache.getIntegerProperty("FIGURE_FIXEDIDWIDTH")) == null)
1311 int w = getIdPanel().getWidth();
1312 return (w > 0 ? w : calculateIdWidth().width + 4);
1314 return idwidth.intValue() + 4;
1317 void makeAlignmentImage(jalview.util.ImageMaker.TYPE type, File file)
1319 int boarderBottomOffset = 5;
1320 long pSessionId = System.currentTimeMillis();
1321 headless = (System.getProperty("java.awt.headless") != null && System
1322 .getProperty("java.awt.headless").equals("true"));
1323 if (alignFrame != null && !headless)
1327 alignFrame.setProgressBar(MessageManager.formatMessage(
1328 "status.saving_file", new Object[] { type.getLabel() }),
1334 AlignmentDimension aDimension = getAlignmentDimension();
1337 jalview.util.ImageMaker im;
1338 final String imageAction, imageTitle;
1339 if (type == jalview.util.ImageMaker.TYPE.PNG)
1341 imageAction = "Create PNG image from alignment";
1344 else if (type == jalview.util.ImageMaker.TYPE.EPS)
1346 imageAction = "Create EPS file from alignment";
1347 imageTitle = alignFrame.getTitle();
1351 imageAction = "Create SVG file from alignment";
1352 imageTitle = alignFrame.getTitle();
1355 im = new jalview.util.ImageMaker(this, type, imageAction,
1356 aDimension.getWidth(), aDimension.getHeight()
1357 + boarderBottomOffset, file, imageTitle,
1358 alignFrame, pSessionId, headless);
1359 Graphics graphics = im.getGraphics();
1360 if (av.getWrapAlignment())
1362 if (graphics != null)
1364 printWrappedAlignment(aDimension.getWidth(),
1365 aDimension.getHeight() + boarderBottomOffset, 0,
1372 if (graphics != null)
1374 printUnwrapped(aDimension.getWidth(), aDimension.getHeight(),
1375 0, graphics, graphics);
1380 } catch (OutOfMemoryError err)
1383 System.out.println("########################\n" + "OUT OF MEMORY "
1384 + file + "\n" + "########################");
1385 new OOMWarning("Creating Image for " + file, err);
1386 // System.out.println("Create IMAGE: " + err);
1387 } catch (Exception ex)
1389 ex.printStackTrace();
1397 public AlignmentDimension getAlignmentDimension()
1399 int maxwidth = av.getAlignment().getWidth();
1400 if (av.hasHiddenColumns())
1402 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth);
1405 int height = ((av.getAlignment().getHeight() + 1) * av.getCharHeight())
1406 + getScalePanel().getHeight();
1407 int width = getVisibleIdWidth(false) + (maxwidth * av.getCharWidth());
1409 if (av.getWrapAlignment())
1411 height = getWrappedHeight();
1414 // need to obtain default alignment width and then add in any
1415 // additional allowance for id margin
1416 // this duplicates the calculation in getWrappedHeight but adjusts for
1418 width = alignFrame.getWidth() - vscroll.getPreferredSize().width
1419 - alignFrame.getInsets().left
1420 - alignFrame.getInsets().right - getVisibleIdWidth()
1421 + getVisibleIdWidth(false);
1425 width = getSeqPanel().getWidth() + getVisibleIdWidth(false);
1429 else if (av.isShowAnnotation())
1431 height += getAnnotationPanel().adjustPanelHeight() + 3;
1433 return new AlignmentDimension(width, height);
1440 public void makeEPS(File epsFile)
1442 makeAlignmentImage(jalview.util.ImageMaker.TYPE.EPS, epsFile);
1448 public void makePNG(File pngFile)
1450 makeAlignmentImage(jalview.util.ImageMaker.TYPE.PNG, pngFile);
1453 public void makeSVG(File svgFile)
1455 makeAlignmentImage(jalview.util.ImageMaker.TYPE.SVG, svgFile);
1458 public void makePNGImageMap(File imgMapFile, String imageName)
1460 // /////ONLY WORKS WITH NON WRAPPED ALIGNMENTS
1461 // ////////////////////////////////////////////
1462 int idWidth = getVisibleIdWidth(false);
1463 FontMetrics fm = getFontMetrics(av.getFont());
1464 int scaleHeight = av.getCharHeight() + fm.getDescent();
1467 // ////////////////////////////////
1468 if (imgMapFile != null)
1472 int s, sSize = av.getAlignment().getHeight(), res, alwidth = av
1473 .getAlignment().getWidth(), g, gSize, f, fSize, sy;
1474 PrintWriter out = new PrintWriter(new FileWriter(imgMapFile));
1475 out.println(jalview.io.HTMLOutput.getImageMapHTML());
1476 out.println("<img src=\"" + imageName
1477 + "\" border=\"0\" usemap=\"#Map\" >"
1478 + "<map name=\"Map\">");
1480 for (s = 0; s < sSize; s++)
1482 sy = s * av.getCharHeight() + scaleHeight;
1484 SequenceI seq = av.getAlignment().getSequenceAt(s);
1485 SequenceFeature[] features = seq.getSequenceFeatures();
1486 SequenceGroup[] groups = av.getAlignment().findAllGroups(seq);
1487 for (res = 0; res < alwidth; res++)
1489 StringBuilder text = new StringBuilder();
1490 String triplet = null;
1491 if (av.getAlignment().isNucleotide())
1493 triplet = ResidueProperties.nucleotideName.get(seq
1494 .getCharAt(res) + "");
1498 triplet = ResidueProperties.aa2Triplet.get(seq.getCharAt(res)
1502 if (triplet == null)
1507 int alIndex = seq.findPosition(res);
1508 gSize = groups.length;
1509 for (g = 0; g < gSize; g++)
1511 if (text.length() < 1)
1513 text.append("<area shape=\"rect\" coords=\"")
1514 .append((idWidth + res * av.getCharWidth()))
1515 .append(",").append(sy).append(",")
1516 .append((idWidth + (res + 1) * av.getCharWidth()))
1517 .append(",").append((av.getCharHeight() + sy))
1518 .append("\"").append(" onMouseOver=\"toolTip('")
1519 .append(alIndex).append(" ").append(triplet);
1522 if (groups[g].getStartRes() < res
1523 && groups[g].getEndRes() > res)
1525 text.append("<br><em>").append(groups[g].getName())
1530 if (features != null)
1532 if (text.length() < 1)
1534 text.append("<area shape=\"rect\" coords=\"")
1535 .append((idWidth + res * av.getCharWidth()))
1536 .append(",").append(sy).append(",")
1537 .append((idWidth + (res + 1) * av.getCharWidth()))
1538 .append(",").append((av.getCharHeight() + sy))
1539 .append("\"").append(" onMouseOver=\"toolTip('")
1540 .append(alIndex).append(" ").append(triplet);
1542 fSize = features.length;
1543 for (f = 0; f < fSize; f++)
1546 if ((features[f].getBegin() <= seq.findPosition(res))
1547 && (features[f].getEnd() >= seq.findPosition(res)))
1549 if (features[f].isContactFeature())
1551 if (features[f].getBegin() == seq.findPosition(res)
1552 || features[f].getEnd() == seq
1555 text.append("<br>").append(features[f].getType())
1556 .append(" ").append(features[f].getBegin())
1557 .append(":").append(features[f].getEnd());
1562 text.append("<br>");
1563 text.append(features[f].getType());
1564 if (features[f].getDescription() != null
1565 && !features[f].getType().equals(
1566 features[f].getDescription()))
1568 text.append(" ").append(features[f].getDescription());
1571 if (features[f].getValue("status") != null)
1573 text.append(" (").append(features[f].getValue("status"))
1581 if (text.length() > 1)
1583 text.append("')\"; onMouseOut=\"toolTip()\"; href=\"#\">");
1584 out.println(text.toString());
1588 out.println("</map></body></html>");
1591 } catch (Exception ex)
1593 ex.printStackTrace();
1595 } // /////////END OF IMAGE MAP
1599 int getWrappedHeight()
1601 int seqPanelWidth = getSeqPanel().seqCanvas.getWidth();
1603 if (System.getProperty("java.awt.headless") != null
1604 && System.getProperty("java.awt.headless").equals("true"))
1606 seqPanelWidth = alignFrame.getWidth() - getVisibleIdWidth()
1607 - vscroll.getPreferredSize().width
1608 - alignFrame.getInsets().left - alignFrame.getInsets().right;
1611 int chunkWidth = getSeqPanel().seqCanvas
1612 .getWrappedCanvasWidth(seqPanelWidth);
1614 int hgap = av.getCharHeight();
1615 if (av.getScaleAboveWrapped())
1617 hgap += av.getCharHeight();
1620 int annotationHeight = 0;
1621 if (av.isShowAnnotation())
1623 annotationHeight = getAnnotationPanel().adjustPanelHeight();
1626 int cHeight = av.getAlignment().getHeight() * av.getCharHeight() + hgap
1629 int maxwidth = av.getAlignment().getWidth();
1630 if (av.hasHiddenColumns())
1632 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
1635 int height = ((maxwidth / chunkWidth) + 1) * cHeight;
1641 * close the panel - deregisters all listeners and nulls any references to
1644 public void closePanel()
1646 PaintRefresher.RemoveComponent(getSeqPanel().seqCanvas);
1647 PaintRefresher.RemoveComponent(getIdPanel().getIdCanvas());
1648 PaintRefresher.RemoveComponent(this);
1651 * try to ensure references are nulled
1653 if (annotationPanel != null)
1655 annotationPanel.dispose();
1660 av.removePropertyChangeListener(propertyChangeListener);
1661 jalview.structure.StructureSelectionManager ssm = av
1662 .getStructureSelectionManager();
1663 ssm.removeStructureViewerListener(getSeqPanel(), null);
1664 ssm.removeSelectionListener(getSeqPanel());
1665 ssm.removeCommandListener(av);
1666 ssm.removeStructureViewerListener(getSeqPanel(), null);
1667 ssm.removeSelectionListener(getSeqPanel());
1673 if (Cache.log.isDebugEnabled())
1675 Cache.log.warn("Closing alignment panel which is already closed.");
1681 * hides or shows dynamic annotation rows based on groups and av state flags
1683 public void updateAnnotation()
1685 updateAnnotation(false, false);
1688 public void updateAnnotation(boolean applyGlobalSettings)
1690 updateAnnotation(applyGlobalSettings, false);
1693 public void updateAnnotation(boolean applyGlobalSettings,
1694 boolean preserveNewGroupSettings)
1696 av.updateGroupAnnotationSettings(applyGlobalSettings,
1697 preserveNewGroupSettings);
1698 adjustAnnotationHeight();
1702 public AlignmentI getAlignment()
1704 return av == null ? null : av.getAlignment();
1708 public String getViewName()
1714 * Make/Unmake this alignment panel the current input focus
1718 public void setSelected(boolean b)
1722 if (alignFrame.getSplitViewContainer() != null)
1725 * bring enclosing SplitFrame to front first if there is one
1727 ((SplitFrame) alignFrame.getSplitViewContainer()).setSelected(b);
1729 alignFrame.setSelected(b);
1730 } catch (Exception ex)
1736 alignFrame.setDisplayedView(this);
1741 public StructureSelectionManager getStructureSelectionManager()
1743 return av.getStructureSelectionManager();
1747 public void raiseOOMWarning(String string, OutOfMemoryError error)
1749 new OOMWarning(string, error, this);
1753 public jalview.api.FeatureRenderer cloneFeatureRenderer()
1756 return new FeatureRenderer(this);
1760 public jalview.api.FeatureRenderer getFeatureRenderer()
1762 return seqPanel.seqCanvas.getFeatureRenderer();
1765 public void updateFeatureRenderer(
1766 jalview.renderer.seqfeatures.FeatureRenderer fr)
1768 fr.transferSettings(getSeqPanel().seqCanvas.getFeatureRenderer());
1771 public void updateFeatureRendererFrom(jalview.api.FeatureRenderer fr)
1773 if (getSeqPanel().seqCanvas.getFeatureRenderer() != null)
1775 getSeqPanel().seqCanvas.getFeatureRenderer().transferSettings(fr);
1779 public ScalePanel getScalePanel()
1784 public void setScalePanel(ScalePanel scalePanel)
1786 this.scalePanel = scalePanel;
1789 public SeqPanel getSeqPanel()
1794 public void setSeqPanel(SeqPanel seqPanel)
1796 this.seqPanel = seqPanel;
1799 public AnnotationPanel getAnnotationPanel()
1801 return annotationPanel;
1804 public void setAnnotationPanel(AnnotationPanel annotationPanel)
1806 this.annotationPanel = annotationPanel;
1809 public AnnotationLabels getAlabels()
1814 public void setAlabels(AnnotationLabels alabels)
1816 this.alabels = alabels;
1819 public IdPanel getIdPanel()
1824 public void setIdPanel(IdPanel idPanel)
1826 this.idPanel = idPanel;
1830 * Follow a scrolling change in the (cDNA/Protein) complementary alignment.
1831 * The aim is to keep the two alignments 'lined up' on their centre columns.
1834 * holds mapped region(s) of this alignment that we are scrolling
1835 * 'to'; may be modified for sequence offset by this method
1836 * @param verticalOffset
1837 * the number of visible sequences to show above the mapped region
1839 public void scrollToCentre(SearchResultsI sr, int verticalOffset)
1842 * To avoid jumpy vertical scrolling (if some sequences are gapped or not
1843 * mapped), we can make the scroll-to location a sequence above the one
1846 SequenceI mappedTo = sr.getResults().get(0).getSequence();
1847 List<SequenceI> seqs = av.getAlignment().getSequences();
1850 * This is like AlignmentI.findIndex(seq) but here we are matching the
1851 * dataset sequence not the aligned sequence
1853 boolean matched = false;
1854 for (SequenceI seq : seqs)
1856 if (mappedTo == seq.getDatasetSequence())
1864 return; // failsafe, shouldn't happen
1868 * Scroll to position but centring the target residue.
1870 scrollToPosition(sr, verticalOffset, true, true);
1874 * Set a flag to say do not scroll any (cDNA/protein) complement.
1878 protected void setDontScrollComplement(boolean b)
1880 this.dontScrollComplement = b;
1883 protected boolean isDontScrollComplement()
1885 return this.dontScrollComplement;
1891 * @adjustHeight if true, try to recalculate panel height for visible
1894 protected void refresh(boolean adjustHeight)
1896 validateAnnotationDimensions(adjustHeight);
1900 // sort, repaint, update overview
1901 paintAlignment(true);
1905 // lightweight repaint