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.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;
39 import java.awt.BorderLayout;
40 import java.awt.Color;
41 import java.awt.Container;
42 import java.awt.Dimension;
44 import java.awt.FontMetrics;
45 import java.awt.Graphics;
46 import java.awt.Insets;
47 import java.awt.event.AdjustmentEvent;
48 import java.awt.event.AdjustmentListener;
49 import java.awt.print.PageFormat;
50 import java.awt.print.Printable;
51 import java.awt.print.PrinterException;
52 import java.beans.PropertyChangeEvent;
53 import java.beans.PropertyChangeListener;
55 import java.io.FileWriter;
56 import java.io.PrintWriter;
57 import java.util.List;
59 import javax.swing.SwingUtilities;
65 * @version $Revision: 1.161 $
67 public class AlignmentPanel extends GAlignmentPanel implements
68 AdjustmentListener, Printable, AlignmentViewPanel
70 public AlignViewport av;
72 OverviewPanel overviewPanel;
74 private SeqPanel seqPanel;
76 private IdPanel idPanel;
78 private boolean headless;
79 IdwidthAdjuster idwidthAdjuster;
82 public AlignFrame alignFrame;
84 private ScalePanel scalePanel;
86 private AnnotationPanel annotationPanel;
88 private AnnotationLabels alabels;
90 // this value is set false when selection area being dragged
91 boolean fastPaint = true;
98 * Flag set while scrolling to follow complementary cDNA/protein scroll. When
99 * true, suppresses invoking the same method recursively.
101 private boolean followingComplementScroll;
104 * Creates a new AlignmentPanel object.
109 public AlignmentPanel(AlignFrame af, final AlignViewport av)
113 setSeqPanel(new SeqPanel(av, this));
114 setIdPanel(new IdPanel(av, this));
116 setScalePanel(new ScalePanel(av, this));
118 idPanelHolder.add(getIdPanel(), BorderLayout.CENTER);
119 idwidthAdjuster = new IdwidthAdjuster(this);
120 idSpaceFillerPanel1.add(idwidthAdjuster, BorderLayout.CENTER);
122 setAnnotationPanel(new AnnotationPanel(this));
123 setAlabels(new AnnotationLabels(this));
125 annotationScroller.setViewportView(getAnnotationPanel());
126 annotationSpaceFillerHolder.add(getAlabels(), BorderLayout.CENTER);
128 scalePanelHolder.add(getScalePanel(), BorderLayout.CENTER);
129 seqPanelHolder.add(getSeqPanel(), BorderLayout.CENTER);
131 setScrollValues(0, 0);
133 hscroll.addAdjustmentListener(this);
134 vscroll.addAdjustmentListener(this);
136 final AlignmentPanel ap = this;
137 av.addPropertyChangeListener(new PropertyChangeListener()
139 public void propertyChange(PropertyChangeEvent evt)
141 if (evt.getPropertyName().equals("alignment"))
143 PaintRefresher.Refresh(ap, av.getSequenceSetId(), true, true);
149 adjustAnnotationHeight();
154 public AlignViewportI getAlignViewport()
158 public void alignmentChanged()
160 av.alignmentChanged(this);
162 alignFrame.updateEditMenuBar();
164 paintAlignment(true);
171 public void fontChanged()
173 // set idCanvas bufferedImage to null
174 // to prevent drawing old image
175 FontMetrics fm = getFontMetrics(av.getFont());
177 scalePanelHolder.setPreferredSize(new Dimension(10, av.getCharHeight()
179 idSpaceFillerPanel1.setPreferredSize(new Dimension(10, av
183 getIdPanel().getIdCanvas().gg = null;
184 getSeqPanel().seqCanvas.img = null;
185 getAnnotationPanel().adjustPanelHeight();
187 Dimension d = calculateIdWidth();
189 d.setSize(d.width + 4, d.height);
190 getIdPanel().getIdCanvas().setPreferredSize(d);
191 hscrollFillerPanel.setPreferredSize(d);
193 if (overviewPanel != null)
195 overviewPanel.setBoxPosition();
197 if (this.alignFrame.getSplitViewContainer() != null)
199 ((SplitFrame) this.alignFrame.getSplitViewContainer()).adjustLayout();
206 * Calculate the width of the alignment labels based on the displayed names
207 * and any bounds on label width set in preferences.
209 * @return Dimension giving the maximum width of the alignment label panel
210 * that should be used.
212 public Dimension calculateIdWidth()
214 // calculate sensible default width when no preference is available
216 if (av.getIdWidth() < 0)
218 int afwidth = (alignFrame != null ? alignFrame.getWidth() : 300);
219 int maxwidth = Math.max(20, Math.min(afwidth - 200, 2 * afwidth / 3));
220 r = calculateIdWidth(maxwidth);
221 av.setIdWidth(r.width);
226 r.width = av.getIdWidth();
233 * Calculate the width of the alignment labels based on the displayed names
234 * and any bounds on label width set in preferences.
237 * -1 or maximum width allowed for IdWidth
238 * @return Dimension giving the maximum width of the alignment label panel
239 * that should be used.
241 public Dimension calculateIdWidth(int maxwidth)
243 Container c = new Container();
245 FontMetrics fm = c.getFontMetrics(new Font(av.font.getName(),
246 Font.ITALIC, av.font.getSize()));
248 AlignmentI al = av.getAlignment();
253 while ((i < al.getHeight()) && (al.getSequenceAt(i) != null))
255 SequenceI s = al.getSequenceAt(i);
257 id = s.getDisplayId(av.getShowJVSuffix());
259 if (fm.stringWidth(id) > idWidth)
261 idWidth = fm.stringWidth(id);
267 // Also check annotation label widths
270 if (al.getAlignmentAnnotation() != null)
272 fm = c.getFontMetrics(getAlabels().getFont());
274 while (i < al.getAlignmentAnnotation().length)
276 String label = al.getAlignmentAnnotation()[i].label;
278 if (fm.stringWidth(label) > idWidth)
280 idWidth = fm.stringWidth(label);
287 return new Dimension(maxwidth < 0 ? idWidth : Math.min(maxwidth,
292 * Highlight the given results on the alignment.
295 public void highlightSearchResults(SearchResults results)
297 scrollToPosition(results);
298 getSeqPanel().seqCanvas.highlightSearchResults(results);
302 * Scroll the view to show the position of the highlighted region in results
303 * (if any) and redraw the overview
307 public boolean scrollToPosition(SearchResults results)
309 return scrollToPosition(results, 0, true, false);
313 * Scroll the view to show the position of the highlighted region in results
316 * @param searchResults
317 * @param redrawOverview
320 public boolean scrollToPosition(SearchResults searchResults, boolean redrawOverview)
322 return scrollToPosition(searchResults, 0, redrawOverview, false);
326 * Scroll the view to show the position of the highlighted region in results
330 * @param verticalOffset
331 * if greater than zero, allows scrolling to a position below the
332 * first displayed sequence
333 * @param redrawOverview
334 * - when set, the overview will be recalculated (takes longer)
336 * if true, try to centre the search results horizontally in the view
337 * @return false if results were not found
339 public boolean scrollToPosition(SearchResults results,
341 boolean redrawOverview, boolean centre)
343 int startv, endv, starts, ends;
344 // TODO: properly locate search results in view when large numbers of hidden
345 // columns exist before highlighted region
346 // do we need to scroll the panel?
347 // TODO: tons of nullpointerexceptions raised here.
348 if (results != null && results.getSize() > 0 && av != null
349 && av.getAlignment() != null)
351 int seqIndex = av.getAlignment().findIndex(results);
356 SequenceI seq = av.getAlignment().getSequenceAt(seqIndex);
358 int[] r = results.getResults(seq, 0, av.getAlignment().getWidth());
365 // System.err.println("Seq : "+seqIndex+" Scroll to "+start+","+end); //
369 * To centre results, scroll to positions half the visible width
370 * left/right of the start/end positions
374 int offset = (av.getEndRes() - av.getStartRes() + 1) / 2 - 1;
375 start = Math.max(start - offset, 0);
376 end = Math.min(end + offset, seq.getEnd() - 1);
382 if (end == seq.getEnd())
386 if (av.hasHiddenColumns())
388 start = av.getColumnSelection().findColumnPosition(start);
389 end = av.getColumnSelection().findColumnPosition(end);
392 if (!av.getColumnSelection().isVisible(r[0]))
394 // don't scroll - position isn't visible
401 * allow for offset of target sequence (actually scroll to one above it)
403 seqIndex = Math.max(0, seqIndex - verticalOffset);
405 if (!av.getWrapAlignment())
407 if ((startv = av.getStartRes()) >= start)
410 * Scroll left to make start of search results visible
412 // setScrollValues(start - 1, seqIndex); // plus one residue
413 setScrollValues(start, seqIndex);
415 else if ((endv = av.getEndRes()) <= end)
418 * Scroll right to make end of search results visible
420 // setScrollValues(startv + 1 + end - endv, seqIndex); // plus one
421 setScrollValues(startv + end - endv, seqIndex);
423 else if ((starts = av.getStartSeq()) > seqIndex)
426 * Scroll up to make start of search results visible
428 setScrollValues(av.getStartRes(), seqIndex);
430 else if ((ends = av.getEndSeq()) <= seqIndex)
433 * Scroll down to make end of search results visible
435 setScrollValues(av.getStartRes(), starts + seqIndex - ends + 1);
438 * Else results are already visible - no need to scroll
443 scrollToWrappedVisible(start);
446 if (redrawOverview && overviewPanel != null)
448 overviewPanel.setBoxPosition();
450 paintAlignment(redrawOverview);
454 void scrollToWrappedVisible(int res)
456 int cwidth = getSeqPanel().seqCanvas
457 .getWrappedCanvasWidth(getSeqPanel().seqCanvas.getWidth());
458 if (res < av.getStartRes() || res >= (av.getStartRes() + cwidth))
460 vscroll.setValue((res / cwidth));
461 av.startRes = vscroll.getValue() * cwidth;
469 * @return DOCUMENT ME!
471 public OverviewPanel getOverviewPanel()
473 return overviewPanel;
482 public void setOverviewPanel(OverviewPanel op)
490 * Hide or show annotation panel
493 public void setAnnotationVisible(boolean b)
495 if (!av.getWrapAlignment())
497 annotationSpaceFillerHolder.setVisible(b);
498 annotationScroller.setVisible(b);
504 * automatically adjust annotation panel height for new annotation whilst
505 * ensuring the alignment is still visible.
507 public void adjustAnnotationHeight()
509 // TODO: display vertical annotation scrollbar if necessary
510 // this is called after loading new annotation onto alignment
511 if (alignFrame.getHeight() == 0)
513 System.out.println("NEEDS FIXING");
515 validateAnnotationDimensions(true);
517 paintAlignment(true);
521 * calculate the annotation dimensions and refresh slider values accordingly.
522 * need to do repaints/notifys afterwards.
524 protected void validateAnnotationDimensions(boolean adjustPanelHeight)
526 int annotationHeight = getAnnotationPanel().adjustPanelHeight();
528 if (adjustPanelHeight)
530 int rowHeight = av.getCharHeight();
531 int alignmentHeight = rowHeight * av.getAlignment().getHeight();
534 * Estimate available height in the AlignFrame for alignment +
535 * annotations. Deduct an estimate for title bar, menu bar, scale panel,
536 * hscroll, status bar (as these are not laid out we can't inspect their
537 * actual heights). Insets gives frame borders.
539 int stuff = Platform.isAMac() ? 80 : 100;
540 Insets insets = alignFrame.getInsets();
541 int availableHeight = alignFrame.getHeight() - stuff - insets.top
545 * If not enough vertical space, maximize annotation height while keeping
546 * at least two rows of alignment visible
548 if (annotationHeight + alignmentHeight > availableHeight)
550 annotationHeight = Math.min(annotationHeight, availableHeight - 2
556 // maintain same window layout whilst updating sliders
557 annotationHeight = annotationScroller.getSize().height;
561 annotationScroller.setPreferredSize(new Dimension(annotationScroller
562 .getWidth(), annotationHeight));
564 annotationSpaceFillerHolder.setPreferredSize(new Dimension(
565 annotationSpaceFillerHolder.getWidth(), annotationHeight));
566 annotationScroller.validate();
567 annotationScroller.addNotify();
571 * update alignment layout for viewport settings
576 public void updateLayout()
579 setAnnotationVisible(av.isShowAnnotation());
580 boolean wrap = av.getWrapAlignment();
582 scalePanelHolder.setVisible(!wrap);
583 hscroll.setVisible(!wrap);
584 idwidthAdjuster.setVisible(!wrap);
588 annotationScroller.setVisible(false);
589 annotationSpaceFillerHolder.setVisible(false);
591 else if (av.isShowAnnotation())
593 annotationScroller.setVisible(true);
594 annotationSpaceFillerHolder.setVisible(true);
597 idSpaceFillerPanel1.setVisible(!wrap);
602 // return value is true if the scroll is valid
603 public boolean scrollUp(boolean up)
607 if (vscroll.getValue() < 1)
613 vscroll.setValue(vscroll.getValue() - 1);
617 if ((vextent + vscroll.getValue()) >= av.getAlignment().getHeight())
623 vscroll.setValue(vscroll.getValue() + 1);
637 * @return DOCUMENT ME!
639 public boolean scrollRight(boolean right)
643 if (hscroll.getValue() < 1)
649 hscroll.setValue(hscroll.getValue() - 1);
653 if ((hextent + hscroll.getValue()) >= av.getAlignment().getWidth())
659 hscroll.setValue(hscroll.getValue() + 1);
668 * Adjust row/column scrollers to show a visible position in the alignment.
671 * visible column to scroll to DOCUMENT ME!
673 * visible row to scroll to
676 public void setScrollValues(int x, int y)
678 // System.err.println("Scroll to "+x+","+y);
679 if (av == null || av.getAlignment() == null)
683 int width = av.getAlignment().getWidth();
684 int height = av.getAlignment().getHeight();
686 if (av.hasHiddenColumns())
688 width = av.getColumnSelection().findColumnPosition(width);
691 av.setEndRes((x + (getSeqPanel().seqCanvas.getWidth() / av
692 .getCharWidth())) - 1);
694 hextent = getSeqPanel().seqCanvas.getWidth() / av.getCharWidth();
695 vextent = getSeqPanel().seqCanvas.getHeight() / av.getCharHeight();
702 if (vextent > height)
707 if ((hextent + x) > width)
712 if ((vextent + y) > height)
714 y = height - vextent;
727 hscroll.setValues(x, hextent, 0, width);
728 vscroll.setValues(y, vextent, 0, height);
737 public void adjustmentValueChanged(AdjustmentEvent evt)
739 int oldX = av.getStartRes();
740 int oldY = av.getStartSeq();
742 if (evt.getSource() == hscroll)
744 int x = hscroll.getValue();
746 av.setEndRes((x + (getSeqPanel().seqCanvas.getWidth() / av.getCharWidth())) - 1);
749 if (evt.getSource() == vscroll)
751 int offy = vscroll.getValue();
753 if (av.getWrapAlignment())
757 int rowSize = getSeqPanel().seqCanvas
758 .getWrappedCanvasWidth(getSeqPanel().seqCanvas.getWidth());
759 av.setStartRes(offy * rowSize);
760 av.setEndRes((offy + 1) * rowSize);
764 // This is only called if file loaded is a jar file that
765 // was wrapped when saved and user has wrap alignment true
766 // as preference setting
767 SwingUtilities.invokeLater(new Runnable()
771 setScrollValues(av.getStartRes(), av.getStartSeq());
778 av.setStartSeq(offy);
780 + (getSeqPanel().seqCanvas.getHeight() / av.getCharHeight()));
784 if (overviewPanel != null)
786 overviewPanel.setBoxPosition();
789 int scrollX = av.startRes - oldX;
790 int scrollY = av.startSeq - oldY;
792 if (av.getWrapAlignment() || !fastPaint)
798 // Make sure we're not trying to draw a panel
799 // larger than the visible window
800 if (scrollX > av.endRes - av.startRes)
802 scrollX = av.endRes - av.startRes;
804 else if (scrollX < av.startRes - av.endRes)
806 scrollX = av.startRes - av.endRes;
809 if (scrollX != 0 || scrollY != 0)
811 getIdPanel().getIdCanvas().fastPaint(scrollY);
812 getSeqPanel().seqCanvas.fastPaint(scrollX, scrollY);
813 getScalePanel().repaint();
815 if (av.isShowAnnotation() && scrollX != 0)
817 getAnnotationPanel().fastPaint(scrollX);
822 * If there is one, scroll the (Protein/cDNA) complementary alignment to
823 * match, unless we are ourselves doing that.
825 if (isFollowingComplementScroll())
827 setFollowingComplementScroll(false);
831 av.scrollComplementaryAlignment();
836 * Repaint the alignment including the annotations and overview panels (if
839 public void paintAlignment(boolean updateOverview)
841 final AnnotationSorter sorter = new AnnotationSorter(getAlignment(),
842 av.isShowAutocalculatedAbove());
843 sorter.sort(getAlignment()
844 .getAlignmentAnnotation(),
845 av.getSortAnnotationsBy());
850 av.getStructureSelectionManager().sequenceColoursChanged(this);
852 if (overviewPanel != null)
854 overviewPanel.updateOverviewImage();
865 public void paintComponent(Graphics g)
869 Dimension d = getIdPanel().getIdCanvas().getPreferredSize();
870 idPanelHolder.setPreferredSize(d);
871 hscrollFillerPanel.setPreferredSize(new Dimension(d.width, 12));
874 if (av.getWrapAlignment())
876 int maxwidth = av.getAlignment().getWidth();
878 if (av.hasHiddenColumns())
880 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
883 int canvasWidth = getSeqPanel().seqCanvas
884 .getWrappedCanvasWidth(getSeqPanel().seqCanvas.getWidth());
888 / getSeqPanel().seqCanvas
889 .getWrappedCanvasWidth(getSeqPanel().seqCanvas
891 vscroll.setMaximum(max);
892 vscroll.setUnitIncrement(1);
893 vscroll.setVisibleAmount(1);
898 setScrollValues(av.getStartRes(), av.getStartSeq());
912 * @return DOCUMENT ME!
914 * @throws PrinterException
917 public int print(Graphics pg, PageFormat pf, int pi)
918 throws PrinterException
920 pg.translate((int) pf.getImageableX(), (int) pf.getImageableY());
922 int pwidth = (int) pf.getImageableWidth();
923 int pheight = (int) pf.getImageableHeight();
925 if (av.getWrapAlignment())
927 return printWrappedAlignment(pg, pwidth, pheight, pi);
931 return printUnwrapped(pg, pwidth, pheight, pi);
947 * @return DOCUMENT ME!
949 * @throws PrinterException
952 public int printUnwrapped(Graphics pg, int pwidth, int pheight, int pi)
953 throws PrinterException
955 int idWidth = getVisibleIdWidth(false);
956 FontMetrics fm = getFontMetrics(av.getFont());
957 int scaleHeight = av.getCharHeight() + fm.getDescent();
959 pg.setColor(Color.white);
960 pg.fillRect(0, 0, pwidth, pheight);
961 pg.setFont(av.getFont());
963 // //////////////////////////////////
964 // / How many sequences and residues can we fit on a printable page?
965 int totalRes = (pwidth - idWidth) / av.getCharWidth();
967 int totalSeq = (pheight - scaleHeight) / av.getCharHeight() - 1;
969 int pagesWide = (av.getAlignment().getWidth() / totalRes) + 1;
971 // ///////////////////////////
972 // / Only print these sequences and residues on this page
975 // ///////////////////////////
976 // / Only print these sequences and residues on this page
979 // ///////////////////////////
980 // / Only print these sequences and residues on this page
983 // ///////////////////////////
984 // / Only print these sequences and residues on this page
986 startRes = (pi % pagesWide) * totalRes;
987 endRes = (startRes + totalRes) - 1;
989 if (endRes > (av.getAlignment().getWidth() - 1))
991 endRes = av.getAlignment().getWidth() - 1;
994 startSeq = (pi / pagesWide) * totalSeq;
995 endSeq = startSeq + totalSeq;
997 if (endSeq > av.getAlignment().getHeight())
999 endSeq = av.getAlignment().getHeight();
1002 int pagesHigh = ((av.getAlignment().getHeight() / totalSeq) + 1)
1005 if (av.isShowAnnotation())
1007 pagesHigh += getAnnotationPanel().adjustPanelHeight() + 3;
1010 pagesHigh /= pheight;
1012 if (pi >= (pagesWide * pagesHigh))
1014 return Printable.NO_SUCH_PAGE;
1018 pg.translate(idWidth, 0);
1019 getScalePanel().drawScale(pg, startRes, endRes, pwidth - idWidth,
1021 pg.translate(-idWidth, scaleHeight);
1025 Color currentColor = null;
1026 Color currentTextColor = null;
1028 pg.setFont(getIdPanel().getIdCanvas().getIdfont());
1031 for (int i = startSeq; i < endSeq; i++)
1033 seq = av.getAlignment().getSequenceAt(i);
1034 if ((av.getSelectionGroup() != null)
1035 && av.getSelectionGroup().getSequences(null).contains(seq))
1037 currentColor = Color.gray;
1038 currentTextColor = Color.black;
1042 currentColor = av.getSequenceColour(seq);
1043 currentTextColor = Color.black;
1046 pg.setColor(currentColor);
1047 pg.fillRect(0, (i - startSeq) * av.getCharHeight(), idWidth,
1048 av.getCharHeight());
1050 pg.setColor(currentTextColor);
1053 if (av.isRightAlignIds())
1055 fm = pg.getFontMetrics();
1057 - fm.stringWidth(seq.getDisplayId(av.getShowJVSuffix()))
1062 seq.getDisplayId(av.getShowJVSuffix()),
1064 (((i - startSeq) * av.getCharHeight()) + av.getCharHeight())
1065 - (av.getCharHeight() / 5));
1068 pg.setFont(av.getFont());
1070 // draw main sequence panel
1071 pg.translate(idWidth, 0);
1072 getSeqPanel().seqCanvas.drawPanel(pg, startRes, endRes, startSeq, endSeq, 0);
1074 if (av.isShowAnnotation() && (endSeq == av.getAlignment().getHeight()))
1076 // draw annotation - need to offset for current scroll position
1077 int offset = -getAlabels().getScrollOffset();
1078 pg.translate(0, offset);
1079 pg.translate(-idWidth - 3, (endSeq - startSeq) * av.getCharHeight()
1081 getAlabels().drawComponent(pg, idWidth);
1082 pg.translate(idWidth + 3, 0);
1083 getAnnotationPanel().renderer.drawComponent(getAnnotationPanel(), av,
1084 pg, -1, startRes, endRes + 1);
1085 pg.translate(0, -offset);
1088 return Printable.PAGE_EXISTS;
1103 * @return DOCUMENT ME!
1105 * @throws PrinterException
1108 public int printWrappedAlignment(Graphics pg, int pwidth, int pheight,
1109 int pi) throws PrinterException
1111 int annotationHeight = 0;
1112 AnnotationLabels labels = null;
1113 if (av.isShowAnnotation())
1115 annotationHeight = getAnnotationPanel().adjustPanelHeight();
1116 labels = new AnnotationLabels(av);
1119 int hgap = av.getCharHeight();
1120 if (av.getScaleAboveWrapped())
1122 hgap += av.getCharHeight();
1125 int cHeight = av.getAlignment().getHeight() * av.getCharHeight() + hgap
1128 int idWidth = getVisibleIdWidth(false);
1130 int maxwidth = av.getAlignment().getWidth();
1131 if (av.hasHiddenColumns())
1133 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
1136 int resWidth = getSeqPanel().seqCanvas.getWrappedCanvasWidth(pwidth
1139 int totalHeight = cHeight * (maxwidth / resWidth + 1);
1141 pg.setColor(Color.white);
1142 pg.fillRect(0, 0, pwidth, pheight);
1143 pg.setFont(av.getFont());
1147 pg.setColor(Color.black);
1149 pg.translate(0, -pi * pheight);
1151 pg.setClip(0, pi * pheight, pwidth, pheight);
1157 for (int i = 0; i < av.getAlignment().getHeight(); i++)
1159 pg.setFont(getIdPanel().getIdCanvas().getIdfont());
1160 SequenceI s = av.getAlignment().getSequenceAt(i);
1161 String string = s.getDisplayId(av.getShowJVSuffix());
1163 if (av.isRightAlignIds())
1165 FontMetrics fm = pg.getFontMetrics();
1166 xPos = idWidth - fm.stringWidth(string) - 4;
1168 pg.drawString(string, xPos,
1169 ((i * av.getCharHeight()) + ypos + av.getCharHeight())
1170 - (av.getCharHeight() / 5));
1174 pg.translate(-3, ypos
1175 + (av.getAlignment().getHeight() * av.getCharHeight()));
1177 pg.setFont(av.getFont());
1178 labels.drawComponent(pg, idWidth);
1179 pg.translate(+3, -ypos
1180 - (av.getAlignment().getHeight() * av
1185 } while (ypos < totalHeight);
1187 pg.translate(idWidth, 0);
1189 getSeqPanel().seqCanvas.drawWrappedPanel(pg, pwidth - idWidth, totalHeight,
1192 if ((pi * pheight) < totalHeight)
1194 return Printable.PAGE_EXISTS;
1199 return Printable.NO_SUCH_PAGE;
1204 * get current sequence ID panel width, or nominal value if panel were to be
1205 * displayed using default settings
1209 public int getVisibleIdWidth()
1211 return getVisibleIdWidth(true);
1215 * get current sequence ID panel width, or nominal value if panel were to be
1216 * displayed using default settings
1219 * indicate if the Id width for onscreen or offscreen display should
1223 public int getVisibleIdWidth(boolean onscreen)
1225 // see if rendering offscreen - check preferences and calc width accordingly
1226 if (!onscreen && Cache.getDefault("FIGURE_AUTOIDWIDTH", false))
1228 return calculateIdWidth(-1).width + 4;
1230 Integer idwidth = null;
1232 || (idwidth = Cache.getIntegerProperty("FIGURE_FIXEDIDWIDTH")) == null)
1234 return (getIdPanel().getWidth() > 0 ? getIdPanel().getWidth()
1235 : calculateIdWidth().width + 4);
1237 return idwidth.intValue() + 4;
1240 void makeAlignmentImage(jalview.util.ImageMaker.TYPE type, File file)
1242 long progress = System.currentTimeMillis();
1243 headless = (System.getProperty("java.awt.headless") != null && System
1244 .getProperty("java.awt.headless").equals("true"));
1245 if (alignFrame != null && !headless)
1247 alignFrame.setProgressBar(MessageManager.formatMessage(
1248 "status.saving_file", new Object[]
1249 { type.getLabel() }), progress);
1253 AlignmentDimension aDimension = getAlignmentDimension();
1256 jalview.util.ImageMaker im;
1257 final String imageAction, imageTitle;
1258 if (type == jalview.util.ImageMaker.TYPE.PNG)
1260 imageAction = "Create PNG image from alignment";
1263 else if (type == jalview.util.ImageMaker.TYPE.EPS)
1265 imageAction = "Create EPS file from alignment";
1266 imageTitle = alignFrame.getTitle();
1270 imageAction = "Create SVG file from alignment";
1271 imageTitle = alignFrame.getTitle();
1274 im = new jalview.util.ImageMaker(this, type, imageAction,
1275 aDimension.getWidth(), aDimension.getHeight(), file,
1277 if (av.getWrapAlignment())
1279 if (im.getGraphics() != null)
1281 printWrappedAlignment(im.getGraphics(), aDimension.getWidth(),
1282 aDimension.getHeight(), 0);
1288 if (im.getGraphics() != null)
1290 printUnwrapped(im.getGraphics(), aDimension.getWidth(),
1291 aDimension.getHeight(), 0);
1295 } catch (OutOfMemoryError err)
1298 System.out.println("########################\n" + "OUT OF MEMORY "
1299 + file + "\n" + "########################");
1300 new OOMWarning("Creating Image for " + file, err);
1301 // System.out.println("Create IMAGE: " + err);
1302 } catch (Exception ex)
1304 ex.printStackTrace();
1308 if (alignFrame != null && !headless)
1310 alignFrame.setProgressBar(MessageManager.getString("status.export_complete"), progress);
1315 public AlignmentDimension getAlignmentDimension()
1317 int maxwidth = av.getAlignment().getWidth();
1318 if (av.hasHiddenColumns())
1320 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth);
1323 int height = ((av.getAlignment().getHeight() + 1) * av.getCharHeight())
1324 + getScalePanel().getHeight();
1325 int width = getVisibleIdWidth(false) + (maxwidth * av.getCharWidth());
1327 if (av.getWrapAlignment())
1329 height = getWrappedHeight();
1332 // need to obtain default alignment width and then add in any
1333 // additional allowance for id margin
1334 // this duplicates the calculation in getWrappedHeight but adjusts for
1336 width = alignFrame.getWidth() - vscroll.getPreferredSize().width
1337 - alignFrame.getInsets().left
1338 - alignFrame.getInsets().right - getVisibleIdWidth()
1339 + getVisibleIdWidth(false);
1343 width = getSeqPanel().getWidth() + getVisibleIdWidth(false);
1347 else if (av.isShowAnnotation())
1349 height += getAnnotationPanel().adjustPanelHeight() + 3;
1351 return new AlignmentDimension(width, height);
1358 public void makeEPS(File epsFile)
1360 makeAlignmentImage(jalview.util.ImageMaker.TYPE.EPS, epsFile);
1366 public void makePNG(File pngFile)
1368 makeAlignmentImage(jalview.util.ImageMaker.TYPE.PNG, pngFile);
1371 public void makeSVG(File svgFile)
1373 makeAlignmentImage(jalview.util.ImageMaker.TYPE.SVG, svgFile);
1375 public void makePNGImageMap(File imgMapFile, String imageName)
1377 // /////ONLY WORKS WITH NONE WRAPPED ALIGNMENTS
1378 // ////////////////////////////////////////////
1379 int idWidth = getVisibleIdWidth(false);
1380 FontMetrics fm = getFontMetrics(av.getFont());
1381 int scaleHeight = av.getCharHeight() + fm.getDescent();
1384 // ////////////////////////////////
1385 if (imgMapFile != null)
1389 int s, sSize = av.getAlignment().getHeight(), res, alwidth = av
1390 .getAlignment().getWidth(), g, gSize, f, fSize, sy;
1391 StringBuffer text = new StringBuffer();
1392 PrintWriter out = new PrintWriter(new FileWriter(imgMapFile));
1393 out.println(jalview.io.HTMLOutput.getImageMapHTML());
1394 out.println("<img src=\"" + imageName
1395 + "\" border=\"0\" usemap=\"#Map\" >"
1396 + "<map name=\"Map\">");
1398 for (s = 0; s < sSize; s++)
1400 sy = s * av.getCharHeight() + scaleHeight;
1402 SequenceI seq = av.getAlignment().getSequenceAt(s);
1403 SequenceFeature[] features = seq.getSequenceFeatures();
1404 SequenceGroup[] groups = av.getAlignment().findAllGroups(seq);
1405 for (res = 0; res < alwidth; res++)
1407 text = new StringBuffer();
1408 String triplet = null;
1409 if (av.getAlignment().isNucleotide())
1411 triplet = ResidueProperties.nucleotideName.get(seq
1417 triplet = ResidueProperties.aa2Triplet.get(seq.getCharAt(res)
1421 if (triplet == null)
1426 int alIndex = seq.findPosition(res);
1427 gSize = groups.length;
1428 for (g = 0; g < gSize; g++)
1430 if (text.length() < 1)
1432 text.append("<area shape=\"rect\" coords=\""
1433 + (idWidth + res * av.getCharWidth()) + "," + sy
1434 + "," + (idWidth + (res + 1) * av.getCharWidth())
1436 + (av.getCharHeight() + sy) + "\""
1437 + " onMouseOver=\"toolTip('" + alIndex + " "
1441 if (groups[g].getStartRes() < res
1442 && groups[g].getEndRes() > res)
1444 text.append("<br><em>" + groups[g].getName() + "</em>");
1448 if (features != null)
1450 if (text.length() < 1)
1452 text.append("<area shape=\"rect\" coords=\""
1453 + (idWidth + res * av.getCharWidth()) + "," + sy
1454 + "," + (idWidth + (res + 1) * av.getCharWidth())
1456 + (av.getCharHeight() + sy) + "\""
1457 + " onMouseOver=\"toolTip('" + alIndex + " "
1460 fSize = features.length;
1461 for (f = 0; f < fSize; f++)
1464 if ((features[f].getBegin() <= seq.findPosition(res))
1465 && (features[f].getEnd() >= seq.findPosition(res)))
1467 if (features[f].getType().equals("disulfide bond"))
1469 if (features[f].getBegin() == seq.findPosition(res)
1470 || features[f].getEnd() == seq
1473 text.append("<br>disulfide bond "
1474 + features[f].getBegin() + ":"
1475 + features[f].getEnd());
1480 text.append("<br>");
1481 text.append(features[f].getType());
1482 if (features[f].getDescription() != null
1483 && !features[f].getType().equals(
1484 features[f].getDescription()))
1486 text.append(" " + features[f].getDescription());
1489 if (features[f].getValue("status") != null)
1491 text.append(" (" + features[f].getValue("status")
1499 if (text.length() > 1)
1501 text.append("')\"; onMouseOut=\"toolTip()\"; href=\"#\">");
1502 out.println(text.toString());
1506 out.println("</map></body></html>");
1509 } catch (Exception ex)
1511 ex.printStackTrace();
1513 } // /////////END OF IMAGE MAP
1517 int getWrappedHeight()
1519 int seqPanelWidth = getSeqPanel().seqCanvas.getWidth();
1521 if (System.getProperty("java.awt.headless") != null
1522 && System.getProperty("java.awt.headless").equals("true"))
1524 seqPanelWidth = alignFrame.getWidth() - getVisibleIdWidth()
1525 - vscroll.getPreferredSize().width
1526 - alignFrame.getInsets().left - alignFrame.getInsets().right;
1529 int chunkWidth = getSeqPanel().seqCanvas
1530 .getWrappedCanvasWidth(seqPanelWidth);
1532 int hgap = av.getCharHeight();
1533 if (av.getScaleAboveWrapped())
1535 hgap += av.getCharHeight();
1538 int annotationHeight = 0;
1539 if (av.isShowAnnotation())
1541 annotationHeight = getAnnotationPanel().adjustPanelHeight();
1544 int cHeight = av.getAlignment().getHeight() * av.getCharHeight() + hgap
1547 int maxwidth = av.getAlignment().getWidth();
1548 if (av.hasHiddenColumns())
1550 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
1553 int height = ((maxwidth / chunkWidth) + 1) * cHeight;
1559 * close the panel - deregisters all listeners and nulls any references to
1562 public void closePanel()
1564 PaintRefresher.RemoveComponent(getSeqPanel().seqCanvas);
1565 PaintRefresher.RemoveComponent(getIdPanel().getIdCanvas());
1566 PaintRefresher.RemoveComponent(this);
1569 jalview.structure.StructureSelectionManager ssm = av
1570 .getStructureSelectionManager();
1571 ssm.removeStructureViewerListener(getSeqPanel(), null);
1572 ssm.removeSelectionListener(getSeqPanel());
1573 ssm.removeCommandListener(av);
1574 ssm.removeStructureViewerListener(getSeqPanel(), null);
1575 ssm.removeSelectionListener(getSeqPanel());
1576 av.setAlignment(null);
1581 if (Cache.log.isDebugEnabled())
1583 Cache.log.warn("Closing alignment panel which is already closed.");
1589 * hides or shows dynamic annotation rows based on groups and av state flags
1591 public void updateAnnotation()
1593 updateAnnotation(false, false);
1596 public void updateAnnotation(boolean applyGlobalSettings)
1598 updateAnnotation(applyGlobalSettings, false);
1601 public void updateAnnotation(boolean applyGlobalSettings,
1602 boolean preserveNewGroupSettings)
1604 av.updateGroupAnnotationSettings(applyGlobalSettings,
1605 preserveNewGroupSettings);
1606 adjustAnnotationHeight();
1610 public AlignmentI getAlignment()
1612 return av.getAlignment();
1617 public String getViewName()
1623 * Make/Unmake this alignment panel the current input focus
1627 public void setSelected(boolean b)
1631 if (alignFrame.getSplitViewContainer() != null)
1634 * bring enclosing SplitFrame to front first if there is one
1636 ((SplitFrame) alignFrame.getSplitViewContainer()).setSelected(b);
1638 alignFrame.setSelected(b);
1639 } catch (Exception ex)
1645 alignFrame.setDisplayedView(this);
1650 public StructureSelectionManager getStructureSelectionManager()
1652 return av.getStructureSelectionManager();
1656 public void raiseOOMWarning(String string, OutOfMemoryError error)
1658 new OOMWarning(string, error, this);
1662 public jalview.api.FeatureRenderer cloneFeatureRenderer()
1665 return new FeatureRenderer(this);
1668 public jalview.api.FeatureRenderer getFeatureRenderer()
1670 return seqPanel.seqCanvas.getFeatureRenderer();
1672 public void updateFeatureRenderer(jalview.renderer.seqfeatures.FeatureRenderer fr)
1674 fr.transferSettings(getSeqPanel().seqCanvas.getFeatureRenderer());
1677 public void updateFeatureRendererFrom(jalview.api.FeatureRenderer fr)
1679 if (getSeqPanel().seqCanvas.getFeatureRenderer() != null)
1681 getSeqPanel().seqCanvas.getFeatureRenderer().transferSettings(fr);
1685 public ScalePanel getScalePanel()
1690 public void setScalePanel(ScalePanel scalePanel)
1692 this.scalePanel = scalePanel;
1695 public SeqPanel getSeqPanel()
1700 public void setSeqPanel(SeqPanel seqPanel)
1702 this.seqPanel = seqPanel;
1705 public AnnotationPanel getAnnotationPanel()
1707 return annotationPanel;
1710 public void setAnnotationPanel(AnnotationPanel annotationPanel)
1712 this.annotationPanel = annotationPanel;
1715 public AnnotationLabels getAlabels()
1720 public void setAlabels(AnnotationLabels alabels)
1722 this.alabels = alabels;
1725 public IdPanel getIdPanel()
1730 public void setIdPanel(IdPanel idPanel)
1732 this.idPanel = idPanel;
1736 * Follow a scrolling change in the (cDNA/Protein) complementary alignment.
1737 * The aim is to keep the two alignments 'lined up' on their centre columns.
1740 * holds mapped region(s) of this alignment that we are scrolling
1741 * 'to'; may be modified for sequence offset by this method
1742 * @param verticalOffset
1743 * the number of visible sequences to show above the mapped region
1745 public void scrollToCentre(SearchResults sr, int verticalOffset)
1748 * To avoid jumpy vertical scrolling (if some sequences are gapped or not
1749 * mapped), we can make the scroll-to location a sequence above the one
1752 SequenceI mappedTo = sr.getResultSequence(0);
1753 List<SequenceI> seqs = av.getAlignment().getSequences();
1756 * This is like AlignmentI.findIndex(seq) but here we are matching the
1757 * dataset sequence not the aligned sequence
1759 boolean matched = false;
1760 for (SequenceI seq : seqs)
1762 if (mappedTo == seq.getDatasetSequence())
1770 return; // failsafe, shouldn't happen
1774 * Scroll to position but centring the target residue.
1776 scrollToPosition(sr, verticalOffset, true, true);
1780 * Set a flag to say we are scrolling to follow a (cDNA/protein) complement.
1784 protected void setFollowingComplementScroll(boolean b)
1786 this.followingComplementScroll = b;
1789 protected boolean isFollowingComplementScroll()
1791 return this.followingComplementScroll;