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.SearchResults;
29 import jalview.datamodel.SequenceFeature;
30 import jalview.datamodel.SequenceGroup;
31 import jalview.datamodel.SequenceI;
32 import jalview.io.HTMLOutput;
33 import jalview.jbgui.GAlignmentPanel;
34 import jalview.math.AlignmentDimension;
35 import jalview.schemes.ResidueProperties;
36 import jalview.structure.StructureSelectionManager;
37 import jalview.util.ImageMaker;
38 import jalview.util.MessageManager;
39 import jalview.util.Platform;
41 import java.awt.BorderLayout;
42 import java.awt.Color;
43 import java.awt.Container;
44 import java.awt.Dimension;
46 import java.awt.FontMetrics;
47 import java.awt.Graphics;
48 import java.awt.Insets;
49 import java.awt.event.AdjustmentEvent;
50 import java.awt.event.AdjustmentListener;
51 import java.awt.print.PageFormat;
52 import java.awt.print.Printable;
53 import java.awt.print.PrinterException;
54 import java.beans.PropertyChangeEvent;
55 import java.beans.PropertyChangeListener;
57 import java.io.FileWriter;
58 import java.io.PrintWriter;
59 import java.util.List;
61 import javax.swing.SwingUtilities;
67 * @version $Revision: 1.161 $
69 public class AlignmentPanel extends GAlignmentPanel implements
70 AdjustmentListener, Printable, AlignmentViewPanel
72 public AlignViewport av;
74 OverviewPanel overviewPanel;
76 private SeqPanel seqPanel;
78 private IdPanel idPanel;
80 private boolean headless;
81 IdwidthAdjuster idwidthAdjuster;
84 public AlignFrame alignFrame;
86 private ScalePanel scalePanel;
88 private AnnotationPanel annotationPanel;
90 private AnnotationLabels alabels;
92 // this value is set false when selection area being dragged
93 boolean fastPaint = true;
100 * Flag set while scrolling to follow complementary cDNA/protein scroll. When
101 * true, suppresses invoking the same method recursively.
103 private boolean followingComplementScroll;
106 * Creates a new AlignmentPanel object.
111 public AlignmentPanel(AlignFrame af, final AlignViewport av)
115 setSeqPanel(new SeqPanel(av, this));
116 setIdPanel(new IdPanel(av, this));
118 setScalePanel(new ScalePanel(av, this));
120 idPanelHolder.add(getIdPanel(), BorderLayout.CENTER);
121 idwidthAdjuster = new IdwidthAdjuster(this);
122 idSpaceFillerPanel1.add(idwidthAdjuster, BorderLayout.CENTER);
124 setAnnotationPanel(new AnnotationPanel(this));
125 setAlabels(new AnnotationLabels(this));
127 annotationScroller.setViewportView(getAnnotationPanel());
128 annotationSpaceFillerHolder.add(getAlabels(), BorderLayout.CENTER);
130 scalePanelHolder.add(getScalePanel(), BorderLayout.CENTER);
131 seqPanelHolder.add(getSeqPanel(), BorderLayout.CENTER);
133 setScrollValues(0, 0);
135 hscroll.addAdjustmentListener(this);
136 vscroll.addAdjustmentListener(this);
138 final AlignmentPanel ap = this;
139 av.addPropertyChangeListener(new PropertyChangeListener()
141 public void propertyChange(PropertyChangeEvent evt)
143 if (evt.getPropertyName().equals("alignment"))
145 PaintRefresher.Refresh(ap, av.getSequenceSetId(), true, true);
151 adjustAnnotationHeight();
156 public AlignViewportI getAlignViewport()
160 public void alignmentChanged()
162 av.alignmentChanged(this);
164 alignFrame.updateEditMenuBar();
166 paintAlignment(true);
173 public void fontChanged()
175 // set idCanvas bufferedImage to null
176 // to prevent drawing old image
177 FontMetrics fm = getFontMetrics(av.getFont());
179 scalePanelHolder.setPreferredSize(new Dimension(10, av.getCharHeight()
181 idSpaceFillerPanel1.setPreferredSize(new Dimension(10, av
185 getIdPanel().getIdCanvas().gg = null;
186 getSeqPanel().seqCanvas.img = null;
187 getAnnotationPanel().adjustPanelHeight();
189 Dimension d = calculateIdWidth();
191 d.setSize(d.width + 4, d.height);
192 getIdPanel().getIdCanvas().setPreferredSize(d);
193 hscrollFillerPanel.setPreferredSize(d);
195 if (overviewPanel != null)
197 overviewPanel.setBoxPosition();
199 if (this.alignFrame.getSplitViewContainer() != null)
201 ((SplitFrame) this.alignFrame.getSplitViewContainer()).adjustLayout();
208 * Calculate the width of the alignment labels based on the displayed names
209 * and any bounds on label width set in preferences.
211 * @return Dimension giving the maximum width of the alignment label panel
212 * that should be used.
214 public Dimension calculateIdWidth()
216 // calculate sensible default width when no preference is available
218 if (av.getIdWidth() < 0)
220 int afwidth = (alignFrame != null ? alignFrame.getWidth() : 300);
221 int maxwidth = Math.max(20, Math.min(afwidth - 200, 2 * afwidth / 3));
222 r = calculateIdWidth(maxwidth);
223 av.setIdWidth(r.width);
228 r.width = av.getIdWidth();
235 * Calculate the width of the alignment labels based on the displayed names
236 * and any bounds on label width set in preferences.
239 * -1 or maximum width allowed for IdWidth
240 * @return Dimension giving the maximum width of the alignment label panel
241 * that should be used.
243 public Dimension calculateIdWidth(int maxwidth)
245 Container c = new Container();
247 FontMetrics fm = c.getFontMetrics(new Font(av.font.getName(),
248 Font.ITALIC, av.font.getSize()));
250 AlignmentI al = av.getAlignment();
255 while ((i < al.getHeight()) && (al.getSequenceAt(i) != null))
257 SequenceI s = al.getSequenceAt(i);
259 id = s.getDisplayId(av.getShowJVSuffix());
261 if (fm.stringWidth(id) > idWidth)
263 idWidth = fm.stringWidth(id);
269 // Also check annotation label widths
272 if (al.getAlignmentAnnotation() != null)
274 fm = c.getFontMetrics(getAlabels().getFont());
276 while (i < al.getAlignmentAnnotation().length)
278 String label = al.getAlignmentAnnotation()[i].label;
280 if (fm.stringWidth(label) > idWidth)
282 idWidth = fm.stringWidth(label);
289 return new Dimension(maxwidth < 0 ? idWidth : Math.min(maxwidth,
294 * Highlight the given results on the alignment.
297 public void highlightSearchResults(SearchResults results)
299 scrollToPosition(results);
300 getSeqPanel().seqCanvas.highlightSearchResults(results);
304 * Scroll the view to show the position of the highlighted region in results
305 * (if any) and redraw the overview
309 public boolean scrollToPosition(SearchResults results)
311 return scrollToPosition(results, 0, true, false);
315 * Scroll the view to show the position of the highlighted region in results
318 * @param searchResults
319 * @param redrawOverview
322 public boolean scrollToPosition(SearchResults searchResults, boolean redrawOverview)
324 return scrollToPosition(searchResults, 0, redrawOverview, false);
328 * Scroll the view to show the position of the highlighted region in results
332 * @param verticalOffset
333 * if greater than zero, allows scrolling to a position below the
334 * first displayed sequence
335 * @param redrawOverview
336 * - when set, the overview will be recalculated (takes longer)
338 * if true, try to centre the search results horizontally in the view
339 * @return false if results were not found
341 public boolean scrollToPosition(SearchResults results,
343 boolean redrawOverview, boolean centre)
345 int startv, endv, starts, ends;
346 // TODO: properly locate search results in view when large numbers of hidden
347 // columns exist before highlighted region
348 // do we need to scroll the panel?
349 // TODO: tons of nullpointerexceptions raised here.
350 if (results != null && results.getSize() > 0 && av != null
351 && av.getAlignment() != null)
353 int seqIndex = av.getAlignment().findIndex(results);
358 SequenceI seq = av.getAlignment().getSequenceAt(seqIndex);
360 int[] r = results.getResults(seq, 0, av.getAlignment().getWidth());
367 // System.err.println("Seq : "+seqIndex+" Scroll to "+start+","+end); //
371 * To centre results, scroll to positions half the visible width
372 * left/right of the start/end positions
376 int offset = (av.getEndRes() - av.getStartRes() + 1) / 2 - 1;
377 start = Math.max(start - offset, 0);
378 end = Math.min(end + offset, seq.getEnd() - 1);
384 if (end == seq.getEnd())
388 if (av.hasHiddenColumns())
390 start = av.getColumnSelection().findColumnPosition(start);
391 end = av.getColumnSelection().findColumnPosition(end);
394 if (!av.getColumnSelection().isVisible(r[0]))
396 // don't scroll - position isn't visible
403 * allow for offset of target sequence (actually scroll to one above it)
405 seqIndex = Math.max(0, seqIndex - verticalOffset);
407 if (!av.getWrapAlignment())
409 if ((startv = av.getStartRes()) >= start)
412 * Scroll left to make start of search results visible
414 // setScrollValues(start - 1, seqIndex); // plus one residue
415 setScrollValues(start, seqIndex);
417 else if ((endv = av.getEndRes()) <= end)
420 * Scroll right to make end of search results visible
422 // setScrollValues(startv + 1 + end - endv, seqIndex); // plus one
423 setScrollValues(startv + end - endv, seqIndex);
425 else if ((starts = av.getStartSeq()) > seqIndex)
428 * Scroll up to make start of search results visible
430 setScrollValues(av.getStartRes(), seqIndex);
432 else if ((ends = av.getEndSeq()) <= seqIndex)
435 * Scroll down to make end of search results visible
437 setScrollValues(av.getStartRes(), starts + seqIndex - ends + 1);
440 * Else results are already visible - no need to scroll
445 scrollToWrappedVisible(start);
448 if (redrawOverview && overviewPanel != null)
450 overviewPanel.setBoxPosition();
452 paintAlignment(redrawOverview);
456 void scrollToWrappedVisible(int res)
458 int cwidth = getSeqPanel().seqCanvas
459 .getWrappedCanvasWidth(getSeqPanel().seqCanvas.getWidth());
460 if (res < av.getStartRes() || res >= (av.getStartRes() + cwidth))
462 vscroll.setValue((res / cwidth));
463 av.startRes = vscroll.getValue() * cwidth;
471 * @return DOCUMENT ME!
473 public OverviewPanel getOverviewPanel()
475 return overviewPanel;
484 public void setOverviewPanel(OverviewPanel op)
492 * Hide or show annotation panel
495 public void setAnnotationVisible(boolean b)
497 if (!av.getWrapAlignment())
499 annotationSpaceFillerHolder.setVisible(b);
500 annotationScroller.setVisible(b);
506 * automatically adjust annotation panel height for new annotation whilst
507 * ensuring the alignment is still visible.
509 public void adjustAnnotationHeight()
511 // TODO: display vertical annotation scrollbar if necessary
512 // this is called after loading new annotation onto alignment
513 if (alignFrame.getHeight() == 0)
515 System.out.println("NEEDS FIXING");
517 validateAnnotationDimensions(true);
519 paintAlignment(true);
523 * calculate the annotation dimensions and refresh slider values accordingly.
524 * need to do repaints/notifys afterwards.
526 protected void validateAnnotationDimensions(boolean adjustPanelHeight)
528 int annotationHeight = getAnnotationPanel().adjustPanelHeight();
530 if (adjustPanelHeight)
532 int rowHeight = av.getCharHeight();
533 int alignmentHeight = rowHeight * av.getAlignment().getHeight();
536 * Estimate available height in the AlignFrame for alignment +
537 * annotations. Deduct an estimate for title bar, menu bar, scale panel,
538 * hscroll, status bar (as these are not laid out we can't inspect their
539 * actual heights). Insets gives frame borders.
541 int stuff = Platform.isAMac() ? 80 : 100;
542 Insets insets = alignFrame.getInsets();
543 int availableHeight = alignFrame.getHeight() - stuff - insets.top
547 * If not enough vertical space, maximize annotation height while keeping
548 * at least two rows of alignment visible
550 if (annotationHeight + alignmentHeight > availableHeight)
552 annotationHeight = Math.min(annotationHeight, availableHeight - 2
558 // maintain same window layout whilst updating sliders
559 annotationHeight = annotationScroller.getSize().height;
563 annotationScroller.setPreferredSize(new Dimension(annotationScroller
564 .getWidth(), annotationHeight));
566 annotationSpaceFillerHolder.setPreferredSize(new Dimension(
567 annotationSpaceFillerHolder.getWidth(), annotationHeight));
568 annotationScroller.validate();
569 annotationScroller.addNotify();
573 * update alignment layout for viewport settings
578 public void updateLayout()
581 setAnnotationVisible(av.isShowAnnotation());
582 boolean wrap = av.getWrapAlignment();
584 scalePanelHolder.setVisible(!wrap);
585 hscroll.setVisible(!wrap);
586 idwidthAdjuster.setVisible(!wrap);
590 annotationScroller.setVisible(false);
591 annotationSpaceFillerHolder.setVisible(false);
593 else if (av.isShowAnnotation())
595 annotationScroller.setVisible(true);
596 annotationSpaceFillerHolder.setVisible(true);
599 idSpaceFillerPanel1.setVisible(!wrap);
604 // return value is true if the scroll is valid
605 public boolean scrollUp(boolean up)
609 if (vscroll.getValue() < 1)
615 vscroll.setValue(vscroll.getValue() - 1);
619 if ((vextent + vscroll.getValue()) >= av.getAlignment().getHeight())
625 vscroll.setValue(vscroll.getValue() + 1);
639 * @return DOCUMENT ME!
641 public boolean scrollRight(boolean right)
645 if (hscroll.getValue() < 1)
651 hscroll.setValue(hscroll.getValue() - 1);
655 if ((hextent + hscroll.getValue()) >= av.getAlignment().getWidth())
661 hscroll.setValue(hscroll.getValue() + 1);
670 * Adjust row/column scrollers to show a visible position in the alignment.
673 * visible column to scroll to DOCUMENT ME!
675 * visible row to scroll to
678 public void setScrollValues(int x, int y)
680 // System.err.println("Scroll to "+x+","+y);
681 if (av == null || av.getAlignment() == null)
685 int width = av.getAlignment().getWidth();
686 int height = av.getAlignment().getHeight();
688 if (av.hasHiddenColumns())
690 width = av.getColumnSelection().findColumnPosition(width);
693 av.setEndRes((x + (getSeqPanel().seqCanvas.getWidth() / av
694 .getCharWidth())) - 1);
696 hextent = getSeqPanel().seqCanvas.getWidth() / av.getCharWidth();
697 vextent = getSeqPanel().seqCanvas.getHeight() / av.getCharHeight();
704 if (vextent > height)
709 if ((hextent + x) > width)
714 if ((vextent + y) > height)
716 y = height - vextent;
729 hscroll.setValues(x, hextent, 0, width);
730 vscroll.setValues(y, vextent, 0, height);
739 public void adjustmentValueChanged(AdjustmentEvent evt)
741 int oldX = av.getStartRes();
742 int oldY = av.getStartSeq();
744 if (evt.getSource() == hscroll)
746 int x = hscroll.getValue();
748 av.setEndRes((x + (getSeqPanel().seqCanvas.getWidth() / av.getCharWidth())) - 1);
751 if (evt.getSource() == vscroll)
753 int offy = vscroll.getValue();
755 if (av.getWrapAlignment())
759 int rowSize = getSeqPanel().seqCanvas
760 .getWrappedCanvasWidth(getSeqPanel().seqCanvas.getWidth());
761 av.setStartRes(offy * rowSize);
762 av.setEndRes((offy + 1) * rowSize);
766 // This is only called if file loaded is a jar file that
767 // was wrapped when saved and user has wrap alignment true
768 // as preference setting
769 SwingUtilities.invokeLater(new Runnable()
773 setScrollValues(av.getStartRes(), av.getStartSeq());
780 av.setStartSeq(offy);
782 + (getSeqPanel().seqCanvas.getHeight() / av.getCharHeight()));
786 if (overviewPanel != null)
788 overviewPanel.setBoxPosition();
791 int scrollX = av.startRes - oldX;
792 int scrollY = av.startSeq - oldY;
794 if (av.getWrapAlignment() || !fastPaint)
800 // Make sure we're not trying to draw a panel
801 // larger than the visible window
802 if (scrollX > av.endRes - av.startRes)
804 scrollX = av.endRes - av.startRes;
806 else if (scrollX < av.startRes - av.endRes)
808 scrollX = av.startRes - av.endRes;
811 if (scrollX != 0 || scrollY != 0)
813 getIdPanel().getIdCanvas().fastPaint(scrollY);
814 getSeqPanel().seqCanvas.fastPaint(scrollX, scrollY);
815 getScalePanel().repaint();
817 if (av.isShowAnnotation() && scrollX != 0)
819 getAnnotationPanel().fastPaint(scrollX);
824 * If there is one, scroll the (Protein/cDNA) complementary alignment to
825 * match, unless we are ourselves doing that.
827 if (isFollowingComplementScroll())
829 setFollowingComplementScroll(false);
833 av.scrollComplementaryAlignment();
838 * Repaint the alignment including the annotations and overview panels (if
841 public void paintAlignment(boolean updateOverview)
843 final AnnotationSorter sorter = new AnnotationSorter(getAlignment(),
844 av.isShowAutocalculatedAbove());
845 sorter.sort(getAlignment()
846 .getAlignmentAnnotation(),
847 av.getSortAnnotationsBy());
852 av.getStructureSelectionManager().sequenceColoursChanged(this);
854 if (overviewPanel != null)
856 overviewPanel.updateOverviewImage();
867 public void paintComponent(Graphics g)
871 Dimension d = getIdPanel().getIdCanvas().getPreferredSize();
872 idPanelHolder.setPreferredSize(d);
873 hscrollFillerPanel.setPreferredSize(new Dimension(d.width, 12));
876 if (av.getWrapAlignment())
878 int maxwidth = av.getAlignment().getWidth();
880 if (av.hasHiddenColumns())
882 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
885 int canvasWidth = getSeqPanel().seqCanvas
886 .getWrappedCanvasWidth(getSeqPanel().seqCanvas.getWidth());
890 / getSeqPanel().seqCanvas
891 .getWrappedCanvasWidth(getSeqPanel().seqCanvas
893 vscroll.setMaximum(max);
894 vscroll.setUnitIncrement(1);
895 vscroll.setVisibleAmount(1);
900 setScrollValues(av.getStartRes(), av.getStartSeq());
914 * @return DOCUMENT ME!
916 * @throws PrinterException
919 public int print(Graphics pg, PageFormat pf, int pi)
920 throws PrinterException
922 pg.translate((int) pf.getImageableX(), (int) pf.getImageableY());
924 int pwidth = (int) pf.getImageableWidth();
925 int pheight = (int) pf.getImageableHeight();
927 if (av.getWrapAlignment())
929 return printWrappedAlignment(pg, pwidth, pheight, pi);
933 return printUnwrapped(pg, pwidth, pheight, pi);
949 * @return DOCUMENT ME!
951 * @throws PrinterException
954 public int printUnwrapped(Graphics pg, int pwidth, int pheight, int pi)
955 throws PrinterException
957 int idWidth = getVisibleIdWidth(false);
958 FontMetrics fm = getFontMetrics(av.getFont());
959 int scaleHeight = av.getCharHeight() + fm.getDescent();
961 pg.setColor(Color.white);
962 pg.fillRect(0, 0, pwidth, pheight);
963 pg.setFont(av.getFont());
965 // //////////////////////////////////
966 // / How many sequences and residues can we fit on a printable page?
967 int totalRes = (pwidth - idWidth) / av.getCharWidth();
969 int totalSeq = (pheight - scaleHeight) / av.getCharHeight() - 1;
971 int pagesWide = (av.getAlignment().getWidth() / totalRes) + 1;
973 // ///////////////////////////
974 // / Only print these sequences and residues on this page
977 // ///////////////////////////
978 // / Only print these sequences and residues on this page
981 // ///////////////////////////
982 // / Only print these sequences and residues on this page
985 // ///////////////////////////
986 // / Only print these sequences and residues on this page
988 startRes = (pi % pagesWide) * totalRes;
989 endRes = (startRes + totalRes) - 1;
991 if (endRes > (av.getAlignment().getWidth() - 1))
993 endRes = av.getAlignment().getWidth() - 1;
996 startSeq = (pi / pagesWide) * totalSeq;
997 endSeq = startSeq + totalSeq;
999 if (endSeq > av.getAlignment().getHeight())
1001 endSeq = av.getAlignment().getHeight();
1004 int pagesHigh = ((av.getAlignment().getHeight() / totalSeq) + 1)
1007 if (av.isShowAnnotation())
1009 pagesHigh += getAnnotationPanel().adjustPanelHeight() + 3;
1012 pagesHigh /= pheight;
1014 if (pi >= (pagesWide * pagesHigh))
1016 return Printable.NO_SUCH_PAGE;
1020 pg.translate(idWidth, 0);
1021 getScalePanel().drawScale(pg, startRes, endRes, pwidth - idWidth,
1023 pg.translate(-idWidth, scaleHeight);
1027 Color currentColor = null;
1028 Color currentTextColor = null;
1030 pg.setFont(getIdPanel().getIdCanvas().getIdfont());
1033 for (int i = startSeq; i < endSeq; i++)
1035 seq = av.getAlignment().getSequenceAt(i);
1036 if ((av.getSelectionGroup() != null)
1037 && av.getSelectionGroup().getSequences(null).contains(seq))
1039 currentColor = Color.gray;
1040 currentTextColor = Color.black;
1044 currentColor = av.getSequenceColour(seq);
1045 currentTextColor = Color.black;
1048 pg.setColor(currentColor);
1049 pg.fillRect(0, (i - startSeq) * av.getCharHeight(), idWidth,
1050 av.getCharHeight());
1052 pg.setColor(currentTextColor);
1055 if (av.isRightAlignIds())
1057 fm = pg.getFontMetrics();
1059 - fm.stringWidth(seq.getDisplayId(av.getShowJVSuffix()))
1064 seq.getDisplayId(av.getShowJVSuffix()),
1066 (((i - startSeq) * av.getCharHeight()) + av.getCharHeight())
1067 - (av.getCharHeight() / 5));
1070 pg.setFont(av.getFont());
1072 // draw main sequence panel
1073 pg.translate(idWidth, 0);
1074 getSeqPanel().seqCanvas.drawPanel(pg, startRes, endRes, startSeq, endSeq, 0);
1076 if (av.isShowAnnotation() && (endSeq == av.getAlignment().getHeight()))
1078 // draw annotation - need to offset for current scroll position
1079 int offset = -getAlabels().getScrollOffset();
1080 pg.translate(0, offset);
1081 pg.translate(-idWidth - 3, (endSeq - startSeq) * av.getCharHeight()
1083 getAlabels().drawComponent(pg, idWidth);
1084 pg.translate(idWidth + 3, 0);
1085 getAnnotationPanel().renderer.drawComponent(getAnnotationPanel(), av,
1086 pg, -1, startRes, endRes + 1);
1087 pg.translate(0, -offset);
1090 return Printable.PAGE_EXISTS;
1105 * @return DOCUMENT ME!
1107 * @throws PrinterException
1110 public int printWrappedAlignment(Graphics pg, int pwidth, int pheight,
1111 int pi) throws PrinterException
1113 int annotationHeight = 0;
1114 AnnotationLabels labels = null;
1115 if (av.isShowAnnotation())
1117 annotationHeight = getAnnotationPanel().adjustPanelHeight();
1118 labels = new AnnotationLabels(av);
1121 int hgap = av.getCharHeight();
1122 if (av.getScaleAboveWrapped())
1124 hgap += av.getCharHeight();
1127 int cHeight = av.getAlignment().getHeight() * av.getCharHeight() + hgap
1130 int idWidth = getVisibleIdWidth(false);
1132 int maxwidth = av.getAlignment().getWidth();
1133 if (av.hasHiddenColumns())
1135 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
1138 int resWidth = getSeqPanel().seqCanvas.getWrappedCanvasWidth(pwidth
1141 int totalHeight = cHeight * (maxwidth / resWidth + 1);
1143 pg.setColor(Color.white);
1144 pg.fillRect(0, 0, pwidth, pheight);
1145 pg.setFont(av.getFont());
1149 pg.setColor(Color.black);
1151 pg.translate(0, -pi * pheight);
1153 pg.setClip(0, pi * pheight, pwidth, pheight);
1159 for (int i = 0; i < av.getAlignment().getHeight(); i++)
1161 pg.setFont(getIdPanel().getIdCanvas().getIdfont());
1162 SequenceI s = av.getAlignment().getSequenceAt(i);
1163 String string = s.getDisplayId(av.getShowJVSuffix());
1165 if (av.isRightAlignIds())
1167 FontMetrics fm = pg.getFontMetrics();
1168 xPos = idWidth - fm.stringWidth(string) - 4;
1170 pg.drawString(string, xPos,
1171 ((i * av.getCharHeight()) + ypos + av.getCharHeight())
1172 - (av.getCharHeight() / 5));
1176 pg.translate(-3, ypos
1177 + (av.getAlignment().getHeight() * av.getCharHeight()));
1179 pg.setFont(av.getFont());
1180 labels.drawComponent(pg, idWidth);
1181 pg.translate(+3, -ypos
1182 - (av.getAlignment().getHeight() * av
1187 } while (ypos < totalHeight);
1189 pg.translate(idWidth, 0);
1191 getSeqPanel().seqCanvas.drawWrappedPanel(pg, pwidth - idWidth, totalHeight,
1194 if ((pi * pheight) < totalHeight)
1196 return Printable.PAGE_EXISTS;
1201 return Printable.NO_SUCH_PAGE;
1206 * get current sequence ID panel width, or nominal value if panel were to be
1207 * displayed using default settings
1211 public int getVisibleIdWidth()
1213 return getVisibleIdWidth(true);
1217 * get current sequence ID panel width, or nominal value if panel were to be
1218 * displayed using default settings
1221 * indicate if the Id width for onscreen or offscreen display should
1225 public int getVisibleIdWidth(boolean onscreen)
1227 // see if rendering offscreen - check preferences and calc width accordingly
1228 if (!onscreen && Cache.getDefault("FIGURE_AUTOIDWIDTH", false))
1230 return calculateIdWidth(-1).width + 4;
1232 Integer idwidth = null;
1234 || (idwidth = Cache.getIntegerProperty("FIGURE_FIXEDIDWIDTH")) == null)
1236 return (getIdPanel().getWidth() > 0 ? getIdPanel().getWidth()
1237 : calculateIdWidth().width + 4);
1239 return idwidth.intValue() + 4;
1242 void makeAlignmentImage(ImageMaker.TYPE type, File file)
1244 long progress = System.currentTimeMillis();
1245 headless = (System.getProperty("java.awt.headless") != null && System
1246 .getProperty("java.awt.headless").equals("true"));
1247 if (alignFrame != null && !headless)
1249 alignFrame.setProgressBar(MessageManager.formatMessage(
1250 "status.saving_file", new Object[]
1251 { type.getLabel() }), progress);
1255 AlignmentDimension aDimension = getAlignmentDimension();
1259 final String imageAction, imageTitle;
1260 if (type == ImageMaker.TYPE.PNG)
1262 imageAction = "Create PNG image from alignment";
1265 else if (type == ImageMaker.TYPE.EPS)
1267 imageAction = "Create EPS file from alignment";
1268 imageTitle = alignFrame.getTitle();
1272 imageAction = "Create SVG file from alignment";
1273 imageTitle = alignFrame.getTitle();
1276 im = new ImageMaker(this, type, imageAction,
1277 aDimension.getWidth(), aDimension.getHeight(), file,
1279 if (av.getWrapAlignment())
1281 if (im.getGraphics() != null)
1283 printWrappedAlignment(im.getGraphics(), aDimension.getWidth(),
1284 aDimension.getHeight(), 0);
1290 if (im.getGraphics() != null)
1292 printUnwrapped(im.getGraphics(), aDimension.getWidth(),
1293 aDimension.getHeight(), 0);
1297 } catch (OutOfMemoryError err)
1300 System.out.println("########################\n" + "OUT OF MEMORY "
1301 + file + "\n" + "########################");
1302 new OOMWarning("Creating Image for " + file, err);
1303 // System.out.println("Create IMAGE: " + err);
1304 } catch (Exception ex)
1306 ex.printStackTrace();
1310 if (alignFrame != null && !headless)
1312 alignFrame.setProgressBar(MessageManager.getString("status.export_complete"), progress);
1317 public AlignmentDimension getAlignmentDimension()
1319 int maxwidth = av.getAlignment().getWidth();
1320 if (av.hasHiddenColumns())
1322 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth);
1325 int height = ((av.getAlignment().getHeight() + 1) * av.getCharHeight())
1326 + getScalePanel().getHeight();
1327 int width = getVisibleIdWidth(false) + (maxwidth * av.getCharWidth());
1329 if (av.getWrapAlignment())
1331 height = getWrappedHeight();
1334 // need to obtain default alignment width and then add in any
1335 // additional allowance for id margin
1336 // this duplicates the calculation in getWrappedHeight but adjusts for
1338 width = alignFrame.getWidth() - vscroll.getPreferredSize().width
1339 - alignFrame.getInsets().left
1340 - alignFrame.getInsets().right - getVisibleIdWidth()
1341 + getVisibleIdWidth(false);
1345 width = getSeqPanel().getWidth() + getVisibleIdWidth(false);
1349 else if (av.isShowAnnotation())
1351 height += getAnnotationPanel().adjustPanelHeight() + 3;
1353 return new AlignmentDimension(width, height);
1360 public void makeEPS(File epsFile)
1362 makeAlignmentImage(ImageMaker.TYPE.EPS, epsFile);
1368 public void makePNG(File pngFile)
1370 makeAlignmentImage(ImageMaker.TYPE.PNG, pngFile);
1373 public void makeSVG(File svgFile)
1375 makeAlignmentImage(ImageMaker.TYPE.SVG, svgFile);
1377 public void makePNGImageMap(File imgMapFile, String imageName)
1379 // /////ONLY WORKS WITH NONE WRAPPED ALIGNMENTS
1380 // ////////////////////////////////////////////
1381 int idWidth = getVisibleIdWidth(false);
1382 FontMetrics fm = getFontMetrics(av.getFont());
1383 int scaleHeight = av.getCharHeight() + fm.getDescent();
1386 // ////////////////////////////////
1387 if (imgMapFile != null)
1391 int s, sSize = av.getAlignment().getHeight(), res, alwidth = av
1392 .getAlignment().getWidth(), g, gSize, f, fSize, sy;
1393 StringBuffer text = new StringBuffer();
1394 PrintWriter out = new PrintWriter(new FileWriter(imgMapFile));
1395 out.println(HTMLOutput.getImageMapHTML());
1396 out.println("<img src=\"" + imageName
1397 + "\" border=\"0\" usemap=\"#Map\" >"
1398 + "<map name=\"Map\">");
1400 for (s = 0; s < sSize; s++)
1402 sy = s * av.getCharHeight() + scaleHeight;
1404 SequenceI seq = av.getAlignment().getSequenceAt(s);
1405 SequenceFeature[] features = seq.getSequenceFeatures();
1406 SequenceGroup[] groups = av.getAlignment().findAllGroups(seq);
1407 for (res = 0; res < alwidth; res++)
1409 text = new StringBuffer();
1410 String triplet = null;
1411 if (av.getAlignment().isNucleotide())
1413 triplet = ResidueProperties.nucleotideName.get(seq
1419 triplet = ResidueProperties.aa2Triplet.get(seq.getCharAt(res)
1423 if (triplet == null)
1428 int alIndex = seq.findPosition(res);
1429 gSize = groups.length;
1430 for (g = 0; g < gSize; g++)
1432 if (text.length() < 1)
1434 text.append("<area shape=\"rect\" coords=\""
1435 + (idWidth + res * av.getCharWidth()) + "," + sy
1436 + "," + (idWidth + (res + 1) * av.getCharWidth())
1438 + (av.getCharHeight() + sy) + "\""
1439 + " onMouseOver=\"toolTip('" + alIndex + " "
1443 if (groups[g].getStartRes() < res
1444 && groups[g].getEndRes() > res)
1446 text.append("<br><em>" + groups[g].getName() + "</em>");
1450 if (features != null)
1452 if (text.length() < 1)
1454 text.append("<area shape=\"rect\" coords=\""
1455 + (idWidth + res * av.getCharWidth()) + "," + sy
1456 + "," + (idWidth + (res + 1) * av.getCharWidth())
1458 + (av.getCharHeight() + sy) + "\""
1459 + " onMouseOver=\"toolTip('" + alIndex + " "
1462 fSize = features.length;
1463 for (f = 0; f < fSize; f++)
1466 if ((features[f].getBegin() <= seq.findPosition(res))
1467 && (features[f].getEnd() >= seq.findPosition(res)))
1469 if (features[f].getType().equals("disulfide bond"))
1471 if (features[f].getBegin() == seq.findPosition(res)
1472 || features[f].getEnd() == seq
1475 text.append("<br>disulfide bond "
1476 + features[f].getBegin() + ":"
1477 + features[f].getEnd());
1482 text.append("<br>");
1483 text.append(features[f].getType());
1484 if (features[f].getDescription() != null
1485 && !features[f].getType().equals(
1486 features[f].getDescription()))
1488 text.append(" " + features[f].getDescription());
1491 if (features[f].getValue("status") != null)
1493 text.append(" (" + features[f].getValue("status")
1501 if (text.length() > 1)
1503 text.append("')\"; onMouseOut=\"toolTip()\"; href=\"#\">");
1504 out.println(text.toString());
1508 out.println("</map></body></html>");
1511 } catch (Exception ex)
1513 ex.printStackTrace();
1515 } // /////////END OF IMAGE MAP
1519 int getWrappedHeight()
1521 int seqPanelWidth = getSeqPanel().seqCanvas.getWidth();
1523 if (System.getProperty("java.awt.headless") != null
1524 && System.getProperty("java.awt.headless").equals("true"))
1526 seqPanelWidth = alignFrame.getWidth() - getVisibleIdWidth()
1527 - vscroll.getPreferredSize().width
1528 - alignFrame.getInsets().left - alignFrame.getInsets().right;
1531 int chunkWidth = getSeqPanel().seqCanvas
1532 .getWrappedCanvasWidth(seqPanelWidth);
1534 int hgap = av.getCharHeight();
1535 if (av.getScaleAboveWrapped())
1537 hgap += av.getCharHeight();
1540 int annotationHeight = 0;
1541 if (av.isShowAnnotation())
1543 annotationHeight = getAnnotationPanel().adjustPanelHeight();
1546 int cHeight = av.getAlignment().getHeight() * av.getCharHeight() + hgap
1549 int maxwidth = av.getAlignment().getWidth();
1550 if (av.hasHiddenColumns())
1552 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
1555 int height = ((maxwidth / chunkWidth) + 1) * cHeight;
1561 * close the panel - deregisters all listeners and nulls any references to
1564 public void closePanel()
1566 PaintRefresher.RemoveComponent(getSeqPanel().seqCanvas);
1567 PaintRefresher.RemoveComponent(getIdPanel().getIdCanvas());
1568 PaintRefresher.RemoveComponent(this);
1571 StructureSelectionManager ssm = av
1572 .getStructureSelectionManager();
1573 ssm.removeStructureViewerListener(getSeqPanel(), null);
1574 ssm.removeSelectionListener(getSeqPanel());
1575 ssm.removeCommandListener(av);
1576 ssm.removeStructureViewerListener(getSeqPanel(), null);
1577 ssm.removeSelectionListener(getSeqPanel());
1578 av.setAlignment(null);
1583 if (Cache.log.isDebugEnabled())
1585 Cache.log.warn("Closing alignment panel which is already closed.");
1591 * hides or shows dynamic annotation rows based on groups and av state flags
1593 public void updateAnnotation()
1595 updateAnnotation(false, false);
1598 public void updateAnnotation(boolean applyGlobalSettings)
1600 updateAnnotation(applyGlobalSettings, false);
1603 public void updateAnnotation(boolean applyGlobalSettings,
1604 boolean preserveNewGroupSettings)
1606 av.updateGroupAnnotationSettings(applyGlobalSettings,
1607 preserveNewGroupSettings);
1608 adjustAnnotationHeight();
1612 public AlignmentI getAlignment()
1614 return av.getAlignment();
1619 public String getViewName()
1625 * Make/Unmake this alignment panel the current input focus
1629 public void setSelected(boolean b)
1633 if (alignFrame.getSplitViewContainer() != null)
1636 * bring enclosing SplitFrame to front first if there is one
1638 ((SplitFrame) alignFrame.getSplitViewContainer()).setSelected(b);
1640 alignFrame.setSelected(b);
1641 } catch (Exception ex)
1647 alignFrame.setDisplayedView(this);
1652 public StructureSelectionManager getStructureSelectionManager()
1654 return av.getStructureSelectionManager();
1658 public void raiseOOMWarning(String string, OutOfMemoryError error)
1660 new OOMWarning(string, error, this);
1664 public jalview.api.FeatureRenderer cloneFeatureRenderer()
1667 return new FeatureRenderer(this);
1670 public jalview.api.FeatureRenderer getFeatureRenderer()
1672 return seqPanel.seqCanvas.getFeatureRenderer();
1674 public void updateFeatureRenderer(jalview.renderer.seqfeatures.FeatureRenderer fr)
1676 fr.transferSettings(getSeqPanel().seqCanvas.getFeatureRenderer());
1679 public void updateFeatureRendererFrom(jalview.api.FeatureRenderer fr)
1681 if (getSeqPanel().seqCanvas.getFeatureRenderer() != null)
1683 getSeqPanel().seqCanvas.getFeatureRenderer().transferSettings(fr);
1687 public ScalePanel getScalePanel()
1692 public void setScalePanel(ScalePanel scalePanel)
1694 this.scalePanel = scalePanel;
1697 public SeqPanel getSeqPanel()
1702 public void setSeqPanel(SeqPanel seqPanel)
1704 this.seqPanel = seqPanel;
1707 public AnnotationPanel getAnnotationPanel()
1709 return annotationPanel;
1712 public void setAnnotationPanel(AnnotationPanel annotationPanel)
1714 this.annotationPanel = annotationPanel;
1717 public AnnotationLabels getAlabels()
1722 public void setAlabels(AnnotationLabels alabels)
1724 this.alabels = alabels;
1727 public IdPanel getIdPanel()
1732 public void setIdPanel(IdPanel idPanel)
1734 this.idPanel = idPanel;
1738 * Follow a scrolling change in the (cDNA/Protein) complementary alignment.
1739 * The aim is to keep the two alignments 'lined up' on their centre columns.
1742 * holds mapped region(s) of this alignment that we are scrolling
1743 * 'to'; may be modified for sequence offset by this method
1744 * @param verticalOffset
1745 * the number of visible sequences to show above the mapped region
1747 public void scrollToCentre(SearchResults sr, int verticalOffset)
1750 * To avoid jumpy vertical scrolling (if some sequences are gapped or not
1751 * mapped), we can make the scroll-to location a sequence above the one
1754 SequenceI mappedTo = sr.getResultSequence(0);
1755 List<SequenceI> seqs = av.getAlignment().getSequences();
1758 * This is like AlignmentI.findIndex(seq) but here we are matching the
1759 * dataset sequence not the aligned sequence
1761 boolean matched = false;
1762 for (SequenceI seq : seqs)
1764 if (mappedTo == seq.getDatasetSequence())
1772 return; // failsafe, shouldn't happen
1776 * Scroll to position but centring the target residue.
1778 scrollToPosition(sr, verticalOffset, true, true);
1782 * Set a flag to say we are scrolling to follow a (cDNA/protein) complement.
1786 protected void setFollowingComplementScroll(boolean b)
1788 this.followingComplementScroll = b;
1791 protected boolean isFollowingComplementScroll()
1793 return this.followingComplementScroll;