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());
366 // System.err.println(this.av.viewName + " Seq : " + seqIndex
367 // + " Scroll to " + start + "," + end);
370 * To centre results, scroll to positions half the visible width
371 * left/right of the start/end positions
375 int offset = (av.getEndRes() - av.getStartRes() + 1) / 2 - 1;
376 start = Math.max(start - offset, 0);
377 end = end + offset - 1;
383 if (end == seq.getEnd())
387 if (av.hasHiddenColumns())
389 start = av.getColumnSelection().findColumnPosition(start);
390 end = av.getColumnSelection().findColumnPosition(end);
393 if (!av.getColumnSelection().isVisible(r[0]))
395 // don't scroll - position isn't visible
402 * allow for offset of target sequence (actually scroll to one above it)
404 seqIndex = Math.max(0, seqIndex - verticalOffset);
406 // System.out.println("start=" + start + ", end=" + end + ", startv="
407 // + av.getStartRes() + ", endv=" + av.getEndRes() + ", starts="
408 // + av.getStartSeq() + ", ends=" + av.getEndSeq());
409 if (!av.getWrapAlignment())
411 if ((startv = av.getStartRes()) >= start)
414 * Scroll left to make start of search results visible
416 // setScrollValues(start - 1, seqIndex); // plus one residue
417 setScrollValues(start, seqIndex);
419 else if ((endv = av.getEndRes()) <= end)
422 * Scroll right to make end of search results visible
424 // setScrollValues(startv + 1 + end - endv, seqIndex); // plus one
425 setScrollValues(startv + end - endv, seqIndex);
427 else if ((starts = av.getStartSeq()) > seqIndex)
430 * Scroll up to make start of search results visible
432 setScrollValues(av.getStartRes(), seqIndex);
434 else if ((ends = av.getEndSeq()) <= seqIndex)
437 * Scroll down to make end of search results visible
439 setScrollValues(av.getStartRes(), starts + seqIndex - ends + 1);
442 * Else results are already visible - no need to scroll
447 scrollToWrappedVisible(start);
450 if (redrawOverview && overviewPanel != null)
452 overviewPanel.setBoxPosition();
454 paintAlignment(redrawOverview);
458 void scrollToWrappedVisible(int res)
460 int cwidth = getSeqPanel().seqCanvas
461 .getWrappedCanvasWidth(getSeqPanel().seqCanvas.getWidth());
462 if (res < av.getStartRes() || res >= (av.getStartRes() + cwidth))
464 vscroll.setValue((res / cwidth));
465 av.startRes = vscroll.getValue() * cwidth;
473 * @return DOCUMENT ME!
475 public OverviewPanel getOverviewPanel()
477 return overviewPanel;
486 public void setOverviewPanel(OverviewPanel op)
494 * Hide or show annotation panel
497 public void setAnnotationVisible(boolean b)
499 if (!av.getWrapAlignment())
501 annotationSpaceFillerHolder.setVisible(b);
502 annotationScroller.setVisible(b);
508 * automatically adjust annotation panel height for new annotation whilst
509 * ensuring the alignment is still visible.
511 public void adjustAnnotationHeight()
513 // TODO: display vertical annotation scrollbar if necessary
514 // this is called after loading new annotation onto alignment
515 if (alignFrame.getHeight() == 0)
517 System.out.println("NEEDS FIXING");
519 validateAnnotationDimensions(true);
521 paintAlignment(true);
525 * calculate the annotation dimensions and refresh slider values accordingly.
526 * need to do repaints/notifys afterwards.
528 protected void validateAnnotationDimensions(boolean adjustPanelHeight)
530 int annotationHeight = getAnnotationPanel().adjustPanelHeight();
532 if (adjustPanelHeight)
534 int rowHeight = av.getCharHeight();
535 int alignmentHeight = rowHeight * av.getAlignment().getHeight();
538 * Estimate available height in the AlignFrame for alignment +
539 * annotations. Deduct an estimate for title bar, menu bar, scale panel,
540 * hscroll, status bar (as these are not laid out we can't inspect their
541 * actual heights). Insets gives frame borders.
543 int stuff = Platform.isAMac() ? 80 : 100;
544 Insets insets = alignFrame.getInsets();
545 int availableHeight = alignFrame.getHeight() - stuff - insets.top
549 * If not enough vertical space, maximize annotation height while keeping
550 * at least two rows of alignment visible
552 if (annotationHeight + alignmentHeight > availableHeight)
554 annotationHeight = Math.min(annotationHeight, availableHeight - 2
560 // maintain same window layout whilst updating sliders
561 annotationHeight = annotationScroller.getSize().height;
565 annotationScroller.setPreferredSize(new Dimension(annotationScroller
566 .getWidth(), annotationHeight));
568 annotationSpaceFillerHolder.setPreferredSize(new Dimension(
569 annotationSpaceFillerHolder.getWidth(), annotationHeight));
570 annotationScroller.validate();
571 annotationScroller.addNotify();
575 * update alignment layout for viewport settings
580 public void updateLayout()
583 setAnnotationVisible(av.isShowAnnotation());
584 boolean wrap = av.getWrapAlignment();
586 scalePanelHolder.setVisible(!wrap);
587 hscroll.setVisible(!wrap);
588 idwidthAdjuster.setVisible(!wrap);
592 annotationScroller.setVisible(false);
593 annotationSpaceFillerHolder.setVisible(false);
595 else if (av.isShowAnnotation())
597 annotationScroller.setVisible(true);
598 annotationSpaceFillerHolder.setVisible(true);
601 idSpaceFillerPanel1.setVisible(!wrap);
606 // return value is true if the scroll is valid
607 public boolean scrollUp(boolean up)
611 if (vscroll.getValue() < 1)
617 vscroll.setValue(vscroll.getValue() - 1);
621 if ((vextent + vscroll.getValue()) >= av.getAlignment().getHeight())
627 vscroll.setValue(vscroll.getValue() + 1);
641 * @return DOCUMENT ME!
643 public boolean scrollRight(boolean right)
647 if (hscroll.getValue() < 1)
653 hscroll.setValue(hscroll.getValue() - 1);
657 if ((hextent + hscroll.getValue()) >= av.getAlignment().getWidth())
663 hscroll.setValue(hscroll.getValue() + 1);
672 * Adjust row/column scrollers to show a visible position in the alignment.
675 * visible column to scroll to
677 * visible row to scroll to
680 public void setScrollValues(int x, int y)
682 // System.err.println("Scroll " + this.av.viewName + " to " + x + "," + y);
683 if (av == null || av.getAlignment() == null)
687 int width = av.getAlignment().getWidth();
688 int height = av.getAlignment().getHeight();
690 if (av.hasHiddenColumns())
692 width = av.getColumnSelection().findColumnPosition(width);
695 av.setEndRes((x + (getSeqPanel().seqCanvas.getWidth() / av
696 .getCharWidth())) - 1);
698 hextent = getSeqPanel().seqCanvas.getWidth() / av.getCharWidth();
699 vextent = getSeqPanel().seqCanvas.getHeight() / av.getCharHeight();
706 if (vextent > height)
711 if ((hextent + x) > width)
716 if ((vextent + y) > height)
718 y = height - vextent;
731 hscroll.setValues(x, hextent, 0, width);
732 vscroll.setValues(y, vextent, 0, height);
741 public void adjustmentValueChanged(AdjustmentEvent evt)
743 int oldX = av.getStartRes();
744 int oldY = av.getStartSeq();
746 if (evt.getSource() == hscroll)
748 int x = hscroll.getValue();
750 av.setEndRes((x + (getSeqPanel().seqCanvas.getWidth() / av.getCharWidth())) - 1);
753 if (evt.getSource() == vscroll)
755 int offy = vscroll.getValue();
757 if (av.getWrapAlignment())
761 int rowSize = getSeqPanel().seqCanvas
762 .getWrappedCanvasWidth(getSeqPanel().seqCanvas.getWidth());
763 av.setStartRes(offy * rowSize);
764 av.setEndRes((offy + 1) * rowSize);
768 // This is only called if file loaded is a jar file that
769 // was wrapped when saved and user has wrap alignment true
770 // as preference setting
771 SwingUtilities.invokeLater(new Runnable()
775 setScrollValues(av.getStartRes(), av.getStartSeq());
782 av.setStartSeq(offy);
784 + (getSeqPanel().seqCanvas.getHeight() / av.getCharHeight()));
788 if (overviewPanel != null)
790 overviewPanel.setBoxPosition();
793 int scrollX = av.startRes - oldX;
794 int scrollY = av.startSeq - oldY;
796 if (av.getWrapAlignment() || !fastPaint)
802 // Make sure we're not trying to draw a panel
803 // larger than the visible window
804 if (scrollX > av.endRes - av.startRes)
806 scrollX = av.endRes - av.startRes;
808 else if (scrollX < av.startRes - av.endRes)
810 scrollX = av.startRes - av.endRes;
813 if (scrollX != 0 || scrollY != 0)
815 getIdPanel().getIdCanvas().fastPaint(scrollY);
816 getSeqPanel().seqCanvas.fastPaint(scrollX, scrollY);
817 getScalePanel().repaint();
819 if (av.isShowAnnotation() && scrollX != 0)
821 getAnnotationPanel().fastPaint(scrollX);
826 * If there is one, scroll the (Protein/cDNA) complementary alignment to
827 * match, unless we are ourselves doing that.
829 if (isFollowingComplementScroll())
831 setFollowingComplementScroll(false);
835 av.scrollComplementaryAlignment();
840 * Repaint the alignment including the annotations and overview panels (if
843 public void paintAlignment(boolean updateOverview)
845 final AnnotationSorter sorter = new AnnotationSorter(getAlignment(),
846 av.isShowAutocalculatedAbove());
847 sorter.sort(getAlignment()
848 .getAlignmentAnnotation(),
849 av.getSortAnnotationsBy());
854 // TODO: determine if this paintAlignment changed structure colours
855 av.getStructureSelectionManager().sequenceColoursChanged(this);
857 if (overviewPanel != null)
859 overviewPanel.updateOverviewImage();
870 public void paintComponent(Graphics g)
874 Dimension d = getIdPanel().getIdCanvas().getPreferredSize();
875 idPanelHolder.setPreferredSize(d);
876 hscrollFillerPanel.setPreferredSize(new Dimension(d.width, 12));
879 if (av.getWrapAlignment())
881 int maxwidth = av.getAlignment().getWidth();
883 if (av.hasHiddenColumns())
885 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
888 int canvasWidth = getSeqPanel().seqCanvas
889 .getWrappedCanvasWidth(getSeqPanel().seqCanvas.getWidth());
893 / getSeqPanel().seqCanvas
894 .getWrappedCanvasWidth(getSeqPanel().seqCanvas
896 vscroll.setMaximum(max);
897 vscroll.setUnitIncrement(1);
898 vscroll.setVisibleAmount(1);
903 setScrollValues(av.getStartRes(), av.getStartSeq());
917 * @return DOCUMENT ME!
919 * @throws PrinterException
922 public int print(Graphics pg, PageFormat pf, int pi)
923 throws PrinterException
925 pg.translate((int) pf.getImageableX(), (int) pf.getImageableY());
927 int pwidth = (int) pf.getImageableWidth();
928 int pheight = (int) pf.getImageableHeight();
930 if (av.getWrapAlignment())
932 return printWrappedAlignment(pg, pwidth, pheight, pi);
936 return printUnwrapped(pg, pwidth, pheight, pi);
952 * @return DOCUMENT ME!
954 * @throws PrinterException
957 public int printUnwrapped(Graphics pg, int pwidth, int pheight, int pi)
958 throws PrinterException
960 int idWidth = getVisibleIdWidth(false);
961 FontMetrics fm = getFontMetrics(av.getFont());
962 int scaleHeight = av.getCharHeight() + fm.getDescent();
964 pg.setColor(Color.white);
965 pg.fillRect(0, 0, pwidth, pheight);
966 pg.setFont(av.getFont());
968 // //////////////////////////////////
969 // / How many sequences and residues can we fit on a printable page?
970 int totalRes = (pwidth - idWidth) / av.getCharWidth();
972 int totalSeq = (pheight - scaleHeight) / av.getCharHeight() - 1;
974 int pagesWide = (av.getAlignment().getWidth() / totalRes) + 1;
976 // ///////////////////////////
977 // / Only print these sequences and residues on this page
980 // ///////////////////////////
981 // / Only print these sequences and residues on this page
984 // ///////////////////////////
985 // / Only print these sequences and residues on this page
988 // ///////////////////////////
989 // / Only print these sequences and residues on this page
991 startRes = (pi % pagesWide) * totalRes;
992 endRes = (startRes + totalRes) - 1;
994 if (endRes > (av.getAlignment().getWidth() - 1))
996 endRes = av.getAlignment().getWidth() - 1;
999 startSeq = (pi / pagesWide) * totalSeq;
1000 endSeq = startSeq + totalSeq;
1002 if (endSeq > av.getAlignment().getHeight())
1004 endSeq = av.getAlignment().getHeight();
1007 int pagesHigh = ((av.getAlignment().getHeight() / totalSeq) + 1)
1010 if (av.isShowAnnotation())
1012 pagesHigh += getAnnotationPanel().adjustPanelHeight() + 3;
1015 pagesHigh /= pheight;
1017 if (pi >= (pagesWide * pagesHigh))
1019 return Printable.NO_SUCH_PAGE;
1023 pg.translate(idWidth, 0);
1024 getScalePanel().drawScale(pg, startRes, endRes, pwidth - idWidth,
1026 pg.translate(-idWidth, scaleHeight);
1030 Color currentColor = null;
1031 Color currentTextColor = null;
1033 pg.setFont(getIdPanel().getIdCanvas().getIdfont());
1036 for (int i = startSeq; i < endSeq; i++)
1038 seq = av.getAlignment().getSequenceAt(i);
1039 if ((av.getSelectionGroup() != null)
1040 && av.getSelectionGroup().getSequences(null).contains(seq))
1042 currentColor = Color.gray;
1043 currentTextColor = Color.black;
1047 currentColor = av.getSequenceColour(seq);
1048 currentTextColor = Color.black;
1051 pg.setColor(currentColor);
1052 pg.fillRect(0, (i - startSeq) * av.getCharHeight(), idWidth,
1053 av.getCharHeight());
1055 pg.setColor(currentTextColor);
1058 if (av.isRightAlignIds())
1060 fm = pg.getFontMetrics();
1062 - fm.stringWidth(seq.getDisplayId(av.getShowJVSuffix()))
1067 seq.getDisplayId(av.getShowJVSuffix()),
1069 (((i - startSeq) * av.getCharHeight()) + av.getCharHeight())
1070 - (av.getCharHeight() / 5));
1073 pg.setFont(av.getFont());
1075 // draw main sequence panel
1076 pg.translate(idWidth, 0);
1077 getSeqPanel().seqCanvas.drawPanel(pg, startRes, endRes, startSeq, endSeq, 0);
1079 if (av.isShowAnnotation() && (endSeq == av.getAlignment().getHeight()))
1081 // draw annotation - need to offset for current scroll position
1082 int offset = -getAlabels().getScrollOffset();
1083 pg.translate(0, offset);
1084 pg.translate(-idWidth - 3, (endSeq - startSeq) * av.getCharHeight()
1086 getAlabels().drawComponent(pg, idWidth);
1087 pg.translate(idWidth + 3, 0);
1088 getAnnotationPanel().renderer.drawComponent(getAnnotationPanel(), av,
1089 pg, -1, startRes, endRes + 1);
1090 pg.translate(0, -offset);
1093 return Printable.PAGE_EXISTS;
1108 * @return DOCUMENT ME!
1110 * @throws PrinterException
1113 public int printWrappedAlignment(Graphics pg, int pwidth, int pheight,
1114 int pi) throws PrinterException
1116 int annotationHeight = 0;
1117 AnnotationLabels labels = null;
1118 if (av.isShowAnnotation())
1120 annotationHeight = getAnnotationPanel().adjustPanelHeight();
1121 labels = new AnnotationLabels(av);
1124 int hgap = av.getCharHeight();
1125 if (av.getScaleAboveWrapped())
1127 hgap += av.getCharHeight();
1130 int cHeight = av.getAlignment().getHeight() * av.getCharHeight() + hgap
1133 int idWidth = getVisibleIdWidth(false);
1135 int maxwidth = av.getAlignment().getWidth();
1136 if (av.hasHiddenColumns())
1138 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
1141 int resWidth = getSeqPanel().seqCanvas.getWrappedCanvasWidth(pwidth
1144 int totalHeight = cHeight * (maxwidth / resWidth + 1);
1146 pg.setColor(Color.white);
1147 pg.fillRect(0, 0, pwidth, pheight);
1148 pg.setFont(av.getFont());
1152 pg.setColor(Color.black);
1154 pg.translate(0, -pi * pheight);
1156 pg.setClip(0, pi * pheight, pwidth, pheight);
1162 for (int i = 0; i < av.getAlignment().getHeight(); i++)
1164 pg.setFont(getIdPanel().getIdCanvas().getIdfont());
1165 SequenceI s = av.getAlignment().getSequenceAt(i);
1166 String string = s.getDisplayId(av.getShowJVSuffix());
1168 if (av.isRightAlignIds())
1170 FontMetrics fm = pg.getFontMetrics();
1171 xPos = idWidth - fm.stringWidth(string) - 4;
1173 pg.drawString(string, xPos,
1174 ((i * av.getCharHeight()) + ypos + av.getCharHeight())
1175 - (av.getCharHeight() / 5));
1179 pg.translate(-3, ypos
1180 + (av.getAlignment().getHeight() * av.getCharHeight()));
1182 pg.setFont(av.getFont());
1183 labels.drawComponent(pg, idWidth);
1184 pg.translate(+3, -ypos
1185 - (av.getAlignment().getHeight() * av
1190 } while (ypos < totalHeight);
1192 pg.translate(idWidth, 0);
1194 getSeqPanel().seqCanvas.drawWrappedPanel(pg, pwidth - idWidth, totalHeight,
1197 if ((pi * pheight) < totalHeight)
1199 return Printable.PAGE_EXISTS;
1204 return Printable.NO_SUCH_PAGE;
1209 * get current sequence ID panel width, or nominal value if panel were to be
1210 * displayed using default settings
1214 public int getVisibleIdWidth()
1216 return getVisibleIdWidth(true);
1220 * get current sequence ID panel width, or nominal value if panel were to be
1221 * displayed using default settings
1224 * indicate if the Id width for onscreen or offscreen display should
1228 public int getVisibleIdWidth(boolean onscreen)
1230 // see if rendering offscreen - check preferences and calc width accordingly
1231 if (!onscreen && Cache.getDefault("FIGURE_AUTOIDWIDTH", false))
1233 return calculateIdWidth(-1).width + 4;
1235 Integer idwidth = null;
1237 || (idwidth = Cache.getIntegerProperty("FIGURE_FIXEDIDWIDTH")) == null)
1239 return (getIdPanel().getWidth() > 0 ? getIdPanel().getWidth()
1240 : calculateIdWidth().width + 4);
1242 return idwidth.intValue() + 4;
1245 void makeAlignmentImage(jalview.util.ImageMaker.TYPE type, File file)
1247 long progress = System.currentTimeMillis();
1248 headless = (System.getProperty("java.awt.headless") != null && System
1249 .getProperty("java.awt.headless").equals("true"));
1250 if (alignFrame != null && !headless)
1252 alignFrame.setProgressBar(MessageManager.formatMessage(
1253 "status.saving_file", new Object[]
1254 { type.getLabel() }), progress);
1258 AlignmentDimension aDimension = getAlignmentDimension();
1261 jalview.util.ImageMaker im;
1262 final String imageAction, imageTitle;
1263 if (type == jalview.util.ImageMaker.TYPE.PNG)
1265 imageAction = "Create PNG image from alignment";
1268 else if (type == jalview.util.ImageMaker.TYPE.EPS)
1270 imageAction = "Create EPS file from alignment";
1271 imageTitle = alignFrame.getTitle();
1275 imageAction = "Create SVG file from alignment";
1276 imageTitle = alignFrame.getTitle();
1279 im = new jalview.util.ImageMaker(this, type, imageAction,
1280 aDimension.getWidth(), aDimension.getHeight(), file,
1282 if (av.getWrapAlignment())
1284 if (im.getGraphics() != null)
1286 printWrappedAlignment(im.getGraphics(), aDimension.getWidth(),
1287 aDimension.getHeight(), 0);
1293 if (im.getGraphics() != null)
1295 printUnwrapped(im.getGraphics(), aDimension.getWidth(),
1296 aDimension.getHeight(), 0);
1300 } catch (OutOfMemoryError err)
1303 System.out.println("########################\n" + "OUT OF MEMORY "
1304 + file + "\n" + "########################");
1305 new OOMWarning("Creating Image for " + file, err);
1306 // System.out.println("Create IMAGE: " + err);
1307 } catch (Exception ex)
1309 ex.printStackTrace();
1313 if (alignFrame != null && !headless)
1315 alignFrame.setProgressBar(MessageManager.getString("status.export_complete"), progress);
1320 public AlignmentDimension getAlignmentDimension()
1322 int maxwidth = av.getAlignment().getWidth();
1323 if (av.hasHiddenColumns())
1325 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth);
1328 int height = ((av.getAlignment().getHeight() + 1) * av.getCharHeight())
1329 + getScalePanel().getHeight();
1330 int width = getVisibleIdWidth(false) + (maxwidth * av.getCharWidth());
1332 if (av.getWrapAlignment())
1334 height = getWrappedHeight();
1337 // need to obtain default alignment width and then add in any
1338 // additional allowance for id margin
1339 // this duplicates the calculation in getWrappedHeight but adjusts for
1341 width = alignFrame.getWidth() - vscroll.getPreferredSize().width
1342 - alignFrame.getInsets().left
1343 - alignFrame.getInsets().right - getVisibleIdWidth()
1344 + getVisibleIdWidth(false);
1348 width = getSeqPanel().getWidth() + getVisibleIdWidth(false);
1352 else if (av.isShowAnnotation())
1354 height += getAnnotationPanel().adjustPanelHeight() + 3;
1356 return new AlignmentDimension(width, height);
1363 public void makeEPS(File epsFile)
1365 makeAlignmentImage(jalview.util.ImageMaker.TYPE.EPS, epsFile);
1371 public void makePNG(File pngFile)
1373 makeAlignmentImage(jalview.util.ImageMaker.TYPE.PNG, pngFile);
1376 public void makeSVG(File svgFile)
1378 makeAlignmentImage(jalview.util.ImageMaker.TYPE.SVG, svgFile);
1380 public void makePNGImageMap(File imgMapFile, String imageName)
1382 // /////ONLY WORKS WITH NONE WRAPPED ALIGNMENTS
1383 // ////////////////////////////////////////////
1384 int idWidth = getVisibleIdWidth(false);
1385 FontMetrics fm = getFontMetrics(av.getFont());
1386 int scaleHeight = av.getCharHeight() + fm.getDescent();
1389 // ////////////////////////////////
1390 if (imgMapFile != null)
1394 int s, sSize = av.getAlignment().getHeight(), res, alwidth = av
1395 .getAlignment().getWidth(), g, gSize, f, fSize, sy;
1396 StringBuffer text = new StringBuffer();
1397 PrintWriter out = new PrintWriter(new FileWriter(imgMapFile));
1398 out.println(jalview.io.HTMLOutput.getImageMapHTML());
1399 out.println("<img src=\"" + imageName
1400 + "\" border=\"0\" usemap=\"#Map\" >"
1401 + "<map name=\"Map\">");
1403 for (s = 0; s < sSize; s++)
1405 sy = s * av.getCharHeight() + scaleHeight;
1407 SequenceI seq = av.getAlignment().getSequenceAt(s);
1408 SequenceFeature[] features = seq.getSequenceFeatures();
1409 SequenceGroup[] groups = av.getAlignment().findAllGroups(seq);
1410 for (res = 0; res < alwidth; res++)
1412 text = new StringBuffer();
1413 String triplet = null;
1414 if (av.getAlignment().isNucleotide())
1416 triplet = ResidueProperties.nucleotideName.get(seq
1422 triplet = ResidueProperties.aa2Triplet.get(seq.getCharAt(res)
1426 if (triplet == null)
1431 int alIndex = seq.findPosition(res);
1432 gSize = groups.length;
1433 for (g = 0; g < gSize; g++)
1435 if (text.length() < 1)
1437 text.append("<area shape=\"rect\" coords=\""
1438 + (idWidth + res * av.getCharWidth()) + "," + sy
1439 + "," + (idWidth + (res + 1) * av.getCharWidth())
1441 + (av.getCharHeight() + sy) + "\""
1442 + " onMouseOver=\"toolTip('" + alIndex + " "
1446 if (groups[g].getStartRes() < res
1447 && groups[g].getEndRes() > res)
1449 text.append("<br><em>" + groups[g].getName() + "</em>");
1453 if (features != null)
1455 if (text.length() < 1)
1457 text.append("<area shape=\"rect\" coords=\""
1458 + (idWidth + res * av.getCharWidth()) + "," + sy
1459 + "," + (idWidth + (res + 1) * av.getCharWidth())
1461 + (av.getCharHeight() + sy) + "\""
1462 + " onMouseOver=\"toolTip('" + alIndex + " "
1465 fSize = features.length;
1466 for (f = 0; f < fSize; f++)
1469 if ((features[f].getBegin() <= seq.findPosition(res))
1470 && (features[f].getEnd() >= seq.findPosition(res)))
1472 if (features[f].getType().equals("disulfide bond"))
1474 if (features[f].getBegin() == seq.findPosition(res)
1475 || features[f].getEnd() == seq
1478 text.append("<br>disulfide bond "
1479 + features[f].getBegin() + ":"
1480 + features[f].getEnd());
1485 text.append("<br>");
1486 text.append(features[f].getType());
1487 if (features[f].getDescription() != null
1488 && !features[f].getType().equals(
1489 features[f].getDescription()))
1491 text.append(" " + features[f].getDescription());
1494 if (features[f].getValue("status") != null)
1496 text.append(" (" + features[f].getValue("status")
1504 if (text.length() > 1)
1506 text.append("')\"; onMouseOut=\"toolTip()\"; href=\"#\">");
1507 out.println(text.toString());
1511 out.println("</map></body></html>");
1514 } catch (Exception ex)
1516 ex.printStackTrace();
1518 } // /////////END OF IMAGE MAP
1522 int getWrappedHeight()
1524 int seqPanelWidth = getSeqPanel().seqCanvas.getWidth();
1526 if (System.getProperty("java.awt.headless") != null
1527 && System.getProperty("java.awt.headless").equals("true"))
1529 seqPanelWidth = alignFrame.getWidth() - getVisibleIdWidth()
1530 - vscroll.getPreferredSize().width
1531 - alignFrame.getInsets().left - alignFrame.getInsets().right;
1534 int chunkWidth = getSeqPanel().seqCanvas
1535 .getWrappedCanvasWidth(seqPanelWidth);
1537 int hgap = av.getCharHeight();
1538 if (av.getScaleAboveWrapped())
1540 hgap += av.getCharHeight();
1543 int annotationHeight = 0;
1544 if (av.isShowAnnotation())
1546 annotationHeight = getAnnotationPanel().adjustPanelHeight();
1549 int cHeight = av.getAlignment().getHeight() * av.getCharHeight() + hgap
1552 int maxwidth = av.getAlignment().getWidth();
1553 if (av.hasHiddenColumns())
1555 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
1558 int height = ((maxwidth / chunkWidth) + 1) * cHeight;
1564 * close the panel - deregisters all listeners and nulls any references to
1567 public void closePanel()
1569 PaintRefresher.RemoveComponent(getSeqPanel().seqCanvas);
1570 PaintRefresher.RemoveComponent(getIdPanel().getIdCanvas());
1571 PaintRefresher.RemoveComponent(this);
1574 jalview.structure.StructureSelectionManager ssm = av
1575 .getStructureSelectionManager();
1576 ssm.removeStructureViewerListener(getSeqPanel(), null);
1577 ssm.removeSelectionListener(getSeqPanel());
1578 ssm.removeCommandListener(av);
1579 ssm.removeStructureViewerListener(getSeqPanel(), null);
1580 ssm.removeSelectionListener(getSeqPanel());
1581 av.setAlignment(null);
1586 if (Cache.log.isDebugEnabled())
1588 Cache.log.warn("Closing alignment panel which is already closed.");
1594 * hides or shows dynamic annotation rows based on groups and av state flags
1596 public void updateAnnotation()
1598 updateAnnotation(false, false);
1601 public void updateAnnotation(boolean applyGlobalSettings)
1603 updateAnnotation(applyGlobalSettings, false);
1606 public void updateAnnotation(boolean applyGlobalSettings,
1607 boolean preserveNewGroupSettings)
1609 av.updateGroupAnnotationSettings(applyGlobalSettings,
1610 preserveNewGroupSettings);
1611 adjustAnnotationHeight();
1615 public AlignmentI getAlignment()
1617 return av == null ? null : av.getAlignment();
1622 public String getViewName()
1628 * Make/Unmake this alignment panel the current input focus
1632 public void setSelected(boolean b)
1636 if (alignFrame.getSplitViewContainer() != null)
1639 * bring enclosing SplitFrame to front first if there is one
1641 ((SplitFrame) alignFrame.getSplitViewContainer()).setSelected(b);
1643 alignFrame.setSelected(b);
1644 } catch (Exception ex)
1650 alignFrame.setDisplayedView(this);
1655 public StructureSelectionManager getStructureSelectionManager()
1657 return av.getStructureSelectionManager();
1661 public void raiseOOMWarning(String string, OutOfMemoryError error)
1663 new OOMWarning(string, error, this);
1667 public jalview.api.FeatureRenderer cloneFeatureRenderer()
1670 return new FeatureRenderer(this);
1673 public jalview.api.FeatureRenderer getFeatureRenderer()
1675 return seqPanel.seqCanvas.getFeatureRenderer();
1677 public void updateFeatureRenderer(jalview.renderer.seqfeatures.FeatureRenderer fr)
1679 fr.transferSettings(getSeqPanel().seqCanvas.getFeatureRenderer());
1682 public void updateFeatureRendererFrom(jalview.api.FeatureRenderer fr)
1684 if (getSeqPanel().seqCanvas.getFeatureRenderer() != null)
1686 getSeqPanel().seqCanvas.getFeatureRenderer().transferSettings(fr);
1690 public ScalePanel getScalePanel()
1695 public void setScalePanel(ScalePanel scalePanel)
1697 this.scalePanel = scalePanel;
1700 public SeqPanel getSeqPanel()
1705 public void setSeqPanel(SeqPanel seqPanel)
1707 this.seqPanel = seqPanel;
1710 public AnnotationPanel getAnnotationPanel()
1712 return annotationPanel;
1715 public void setAnnotationPanel(AnnotationPanel annotationPanel)
1717 this.annotationPanel = annotationPanel;
1720 public AnnotationLabels getAlabels()
1725 public void setAlabels(AnnotationLabels alabels)
1727 this.alabels = alabels;
1730 public IdPanel getIdPanel()
1735 public void setIdPanel(IdPanel idPanel)
1737 this.idPanel = idPanel;
1741 * Follow a scrolling change in the (cDNA/Protein) complementary alignment.
1742 * The aim is to keep the two alignments 'lined up' on their centre columns.
1745 * holds mapped region(s) of this alignment that we are scrolling
1746 * 'to'; may be modified for sequence offset by this method
1747 * @param verticalOffset
1748 * the number of visible sequences to show above the mapped region
1750 public void scrollToCentre(SearchResults sr, int verticalOffset)
1753 * To avoid jumpy vertical scrolling (if some sequences are gapped or not
1754 * mapped), we can make the scroll-to location a sequence above the one
1757 SequenceI mappedTo = sr.getResultSequence(0);
1758 List<SequenceI> seqs = av.getAlignment().getSequences();
1761 * This is like AlignmentI.findIndex(seq) but here we are matching the
1762 * dataset sequence not the aligned sequence
1764 boolean matched = false;
1765 for (SequenceI seq : seqs)
1767 if (mappedTo == seq.getDatasetSequence())
1775 return; // failsafe, shouldn't happen
1779 * Scroll to position but centring the target residue.
1781 scrollToPosition(sr, verticalOffset, true, true);
1785 * Set a flag to say we are scrolling to follow a (cDNA/protein) complement.
1789 protected void setFollowingComplementScroll(boolean b)
1791 this.followingComplementScroll = b;
1794 protected boolean isFollowingComplementScroll()
1796 return this.followingComplementScroll;