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;
80 IdwidthAdjuster idwidthAdjuster;
83 public AlignFrame alignFrame;
85 private ScalePanel scalePanel;
87 private AnnotationPanel annotationPanel;
89 private AnnotationLabels alabels;
91 // this value is set false when selection area being dragged
92 boolean fastPaint = true;
99 * Flag set while scrolling to follow complementary cDNA/protein scroll. When
100 * true, suppresses invoking the same method recursively.
102 private boolean followingComplementScroll;
105 * Creates a new AlignmentPanel object.
110 public AlignmentPanel(AlignFrame af, final AlignViewport av)
114 setSeqPanel(new SeqPanel(av, this));
115 setIdPanel(new IdPanel(av, this));
117 setScalePanel(new ScalePanel(av, this));
119 idPanelHolder.add(getIdPanel(), BorderLayout.CENTER);
120 idwidthAdjuster = new IdwidthAdjuster(this);
121 idSpaceFillerPanel1.add(idwidthAdjuster, BorderLayout.CENTER);
123 setAnnotationPanel(new AnnotationPanel(this));
124 setAlabels(new AnnotationLabels(this));
126 annotationScroller.setViewportView(getAnnotationPanel());
127 annotationSpaceFillerHolder.add(getAlabels(), BorderLayout.CENTER);
129 scalePanelHolder.add(getScalePanel(), BorderLayout.CENTER);
130 seqPanelHolder.add(getSeqPanel(), BorderLayout.CENTER);
132 setScrollValues(0, 0);
134 hscroll.addAdjustmentListener(this);
135 vscroll.addAdjustmentListener(this);
137 final AlignmentPanel ap = this;
138 av.addPropertyChangeListener(new PropertyChangeListener()
140 public void propertyChange(PropertyChangeEvent evt)
142 if (evt.getPropertyName().equals("alignment"))
144 PaintRefresher.Refresh(ap, av.getSequenceSetId(), true, true);
150 adjustAnnotationHeight();
155 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
182 .getCharHeight() + fm.getDescent()));
184 getIdPanel().getIdCanvas().gg = null;
185 getSeqPanel().seqCanvas.img = null;
186 getAnnotationPanel().adjustPanelHeight();
188 Dimension d = calculateIdWidth();
190 d.setSize(d.width + 4, d.height);
191 getIdPanel().getIdCanvas().setPreferredSize(d);
192 hscrollFillerPanel.setPreferredSize(d);
194 if (overviewPanel != null)
196 overviewPanel.setBoxPosition();
198 if (this.alignFrame.getSplitViewContainer() != null)
200 ((SplitFrame) this.alignFrame.getSplitViewContainer()).adjustLayout();
207 * Calculate the width of the alignment labels based on the displayed names
208 * and any bounds on label width set in preferences.
210 * @return Dimension giving the maximum width of the alignment label panel
211 * that should be used.
213 public Dimension calculateIdWidth()
215 // calculate sensible default width when no preference is available
217 if (av.getIdWidth() < 0)
219 int afwidth = (alignFrame != null ? alignFrame.getWidth() : 300);
220 int maxwidth = Math.max(20, Math.min(afwidth - 200, 2 * afwidth / 3));
221 r = calculateIdWidth(maxwidth);
222 av.setIdWidth(r.width);
227 r.width = av.getIdWidth();
234 * Calculate the width of the alignment labels based on the displayed names
235 * and any bounds on label width set in preferences.
238 * -1 or maximum width allowed for IdWidth
239 * @return Dimension giving the maximum width of the alignment label panel
240 * that should be used.
242 public Dimension calculateIdWidth(int maxwidth)
244 Container c = new Container();
246 FontMetrics fm = c.getFontMetrics(new Font(av.font.getName(),
247 Font.ITALIC, av.font.getSize()));
249 AlignmentI al = av.getAlignment();
254 while ((i < al.getHeight()) && (al.getSequenceAt(i) != null))
256 SequenceI s = al.getSequenceAt(i);
258 id = s.getDisplayId(av.getShowJVSuffix());
260 if (fm.stringWidth(id) > idWidth)
262 idWidth = fm.stringWidth(id);
268 // Also check annotation label widths
271 if (al.getAlignmentAnnotation() != null)
273 fm = c.getFontMetrics(getAlabels().getFont());
275 while (i < al.getAlignmentAnnotation().length)
277 String label = al.getAlignmentAnnotation()[i].label;
279 if (fm.stringWidth(label) > idWidth)
281 idWidth = fm.stringWidth(label);
288 return new Dimension(maxwidth < 0 ? idWidth : Math.min(maxwidth,
293 * Highlight the given results on the alignment.
296 public void highlightSearchResults(SearchResults results)
298 scrollToPosition(results);
299 getSeqPanel().seqCanvas.highlightSearchResults(results);
303 * Scroll the view to show the position of the highlighted region in results
304 * (if any) and redraw the overview
308 public boolean scrollToPosition(SearchResults results)
310 return scrollToPosition(results, 0, true, false);
314 * Scroll the view to show the position of the highlighted region in results
317 * @param searchResults
318 * @param redrawOverview
321 public boolean scrollToPosition(SearchResults searchResults,
322 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,
342 int verticalOffset, boolean redrawOverview, boolean centre)
344 int startv, endv, starts, ends;
345 // TODO: properly locate search results in view when large numbers of hidden
346 // columns exist before highlighted region
347 // do we need to scroll the panel?
348 // TODO: tons of nullpointerexceptions raised here.
349 if (results != null && results.getSize() > 0 && av != null
350 && av.getAlignment() != null)
352 int seqIndex = av.getAlignment().findIndex(results);
357 SequenceI seq = av.getAlignment().getSequenceAt(seqIndex);
359 int[] r = results.getResults(seq, 0, av.getAlignment().getWidth());
367 // System.err.println(this.av.viewName + " Seq : " + seqIndex
368 // + " 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 = end + offset - 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 // System.out.println("start=" + start + ", end=" + end + ", startv="
408 // + av.getStartRes() + ", endv=" + av.getEndRes() + ", starts="
409 // + av.getStartSeq() + ", ends=" + av.getEndSeq());
410 if (!av.getWrapAlignment())
412 if ((startv = av.getStartRes()) >= start)
415 * Scroll left to make start of search results visible
417 // setScrollValues(start - 1, seqIndex); // plus one residue
418 setScrollValues(start, seqIndex);
420 else if ((endv = av.getEndRes()) <= end)
423 * Scroll right to make end of search results visible
425 // setScrollValues(startv + 1 + end - endv, seqIndex); // plus one
426 setScrollValues(startv + end - endv, seqIndex);
428 else if ((starts = av.getStartSeq()) > seqIndex)
431 * Scroll up to make start of search results visible
433 setScrollValues(av.getStartRes(), seqIndex);
435 else if ((ends = av.getEndSeq()) <= seqIndex)
438 * Scroll down to make end of search results visible
440 setScrollValues(av.getStartRes(), starts + seqIndex - ends + 1);
443 * Else results are already visible - no need to scroll
448 scrollToWrappedVisible(start);
451 if (redrawOverview && overviewPanel != null)
453 overviewPanel.setBoxPosition();
455 paintAlignment(redrawOverview);
459 void scrollToWrappedVisible(int res)
461 int cwidth = getSeqPanel().seqCanvas
462 .getWrappedCanvasWidth(getSeqPanel().seqCanvas.getWidth());
463 if (res < av.getStartRes() || res >= (av.getStartRes() + cwidth))
465 vscroll.setValue((res / cwidth));
466 av.startRes = vscroll.getValue() * cwidth;
474 * @return DOCUMENT ME!
476 public OverviewPanel getOverviewPanel()
478 return overviewPanel;
487 public void setOverviewPanel(OverviewPanel op)
495 * Hide or show annotation panel
498 public void setAnnotationVisible(boolean b)
500 if (!av.getWrapAlignment())
502 annotationSpaceFillerHolder.setVisible(b);
503 annotationScroller.setVisible(b);
509 * automatically adjust annotation panel height for new annotation whilst
510 * ensuring the alignment is still visible.
512 public void adjustAnnotationHeight()
514 // TODO: display vertical annotation scrollbar if necessary
515 // this is called after loading new annotation onto alignment
516 if (alignFrame.getHeight() == 0)
518 System.out.println("NEEDS FIXING");
520 validateAnnotationDimensions(true);
522 paintAlignment(true);
526 * calculate the annotation dimensions and refresh slider values accordingly.
527 * need to do repaints/notifys afterwards.
529 protected void validateAnnotationDimensions(boolean adjustPanelHeight)
531 int annotationHeight = getAnnotationPanel().adjustPanelHeight();
533 if (adjustPanelHeight)
535 int rowHeight = av.getCharHeight();
536 int alignmentHeight = rowHeight * av.getAlignment().getHeight();
539 * Estimate available height in the AlignFrame for alignment +
540 * annotations. Deduct an estimate for title bar, menu bar, scale panel,
541 * hscroll, status bar (as these are not laid out we can't inspect their
542 * actual heights). Insets gives frame borders.
544 int stuff = Platform.isAMac() ? 80 : 100;
545 Insets insets = alignFrame.getInsets();
546 int availableHeight = alignFrame.getHeight() - stuff - insets.top
550 * If not enough vertical space, maximize annotation height while keeping
551 * at least two rows of alignment visible
553 if (annotationHeight + alignmentHeight > availableHeight)
555 annotationHeight = Math.min(annotationHeight, availableHeight - 2
561 // maintain same window layout whilst updating sliders
562 annotationHeight = annotationScroller.getSize().height;
566 annotationScroller.setPreferredSize(new Dimension(annotationScroller
567 .getWidth(), annotationHeight));
569 annotationSpaceFillerHolder.setPreferredSize(new Dimension(
570 annotationSpaceFillerHolder.getWidth(), annotationHeight));
571 annotationScroller.validate();
572 annotationScroller.addNotify();
576 * update alignment layout for viewport settings
581 public void updateLayout()
584 setAnnotationVisible(av.isShowAnnotation());
585 boolean wrap = av.getWrapAlignment();
587 scalePanelHolder.setVisible(!wrap);
588 hscroll.setVisible(!wrap);
589 idwidthAdjuster.setVisible(!wrap);
593 annotationScroller.setVisible(false);
594 annotationSpaceFillerHolder.setVisible(false);
596 else if (av.isShowAnnotation())
598 annotationScroller.setVisible(true);
599 annotationSpaceFillerHolder.setVisible(true);
602 idSpaceFillerPanel1.setVisible(!wrap);
607 // return value is true if the scroll is valid
608 public boolean scrollUp(boolean up)
612 if (vscroll.getValue() < 1)
618 vscroll.setValue(vscroll.getValue() - 1);
622 if ((vextent + vscroll.getValue()) >= av.getAlignment().getHeight())
628 vscroll.setValue(vscroll.getValue() + 1);
642 * @return DOCUMENT ME!
644 public boolean scrollRight(boolean right)
648 if (hscroll.getValue() < 1)
654 hscroll.setValue(hscroll.getValue() - 1);
658 if ((hextent + hscroll.getValue()) >= av.getAlignment().getWidth())
664 hscroll.setValue(hscroll.getValue() + 1);
673 * Adjust row/column scrollers to show a visible position in the alignment.
676 * visible column to scroll to
678 * visible row to scroll to
681 public void setScrollValues(int x, int y)
683 // System.err.println("Scroll " + this.av.viewName + " to " + x + "," + y);
684 if (av == null || av.getAlignment() == null)
688 int width = av.getAlignment().getWidth();
689 int height = av.getAlignment().getHeight();
691 if (av.hasHiddenColumns())
693 width = av.getColumnSelection().findColumnPosition(width);
696 av.setEndRes((x + (getSeqPanel().seqCanvas.getWidth() / av
697 .getCharWidth())) - 1);
699 hextent = getSeqPanel().seqCanvas.getWidth() / av.getCharWidth();
700 vextent = getSeqPanel().seqCanvas.getHeight() / av.getCharHeight();
707 if (vextent > height)
712 if ((hextent + x) > width)
717 if ((vextent + y) > height)
719 y = height - vextent;
732 hscroll.setValues(x, hextent, 0, width);
733 vscroll.setValues(y, vextent, 0, height);
742 public void adjustmentValueChanged(AdjustmentEvent evt)
744 int oldX = av.getStartRes();
745 int oldY = av.getStartSeq();
747 if (evt.getSource() == hscroll)
749 int x = hscroll.getValue();
751 av.setEndRes((x + (getSeqPanel().seqCanvas.getWidth() / av
752 .getCharWidth())) - 1);
755 if (evt.getSource() == vscroll)
757 int offy = vscroll.getValue();
759 if (av.getWrapAlignment())
763 int rowSize = getSeqPanel().seqCanvas
764 .getWrappedCanvasWidth(getSeqPanel().seqCanvas.getWidth());
765 av.setStartRes(offy * rowSize);
766 av.setEndRes((offy + 1) * rowSize);
770 // This is only called if file loaded is a jar file that
771 // was wrapped when saved and user has wrap alignment true
772 // as preference setting
773 SwingUtilities.invokeLater(new Runnable()
777 setScrollValues(av.getStartRes(), av.getStartSeq());
784 av.setStartSeq(offy);
786 + (getSeqPanel().seqCanvas.getHeight() / av.getCharHeight()));
790 if (overviewPanel != null)
792 overviewPanel.setBoxPosition();
795 int scrollX = av.startRes - oldX;
796 int scrollY = av.startSeq - oldY;
798 if (av.getWrapAlignment() || !fastPaint)
804 // Make sure we're not trying to draw a panel
805 // larger than the visible window
806 if (scrollX > av.endRes - av.startRes)
808 scrollX = av.endRes - av.startRes;
810 else if (scrollX < av.startRes - av.endRes)
812 scrollX = av.startRes - av.endRes;
815 if (scrollX != 0 || scrollY != 0)
817 getIdPanel().getIdCanvas().fastPaint(scrollY);
818 getSeqPanel().seqCanvas.fastPaint(scrollX, scrollY);
819 getScalePanel().repaint();
821 if (av.isShowAnnotation() && scrollX != 0)
823 getAnnotationPanel().fastPaint(scrollX);
828 * If there is one, scroll the (Protein/cDNA) complementary alignment to
829 * match, unless we are ourselves doing that.
831 if (isFollowingComplementScroll())
833 setFollowingComplementScroll(false);
837 av.scrollComplementaryAlignment();
842 * Repaint the alignment including the annotations and overview panels (if
845 public void paintAlignment(boolean updateOverview)
847 final AnnotationSorter sorter = new AnnotationSorter(getAlignment(),
848 av.isShowAutocalculatedAbove());
849 sorter.sort(getAlignment().getAlignmentAnnotation(),
850 av.getSortAnnotationsBy());
855 // TODO: determine if this paintAlignment changed structure colours
856 av.getStructureSelectionManager().sequenceColoursChanged(this);
858 if (overviewPanel != null)
860 overviewPanel.updateOverviewImage();
871 public void paintComponent(Graphics g)
875 Dimension d = getIdPanel().getIdCanvas().getPreferredSize();
876 idPanelHolder.setPreferredSize(d);
877 hscrollFillerPanel.setPreferredSize(new Dimension(d.width, 12));
880 if (av.getWrapAlignment())
882 int maxwidth = av.getAlignment().getWidth();
884 if (av.hasHiddenColumns())
886 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
889 int canvasWidth = getSeqPanel().seqCanvas
890 .getWrappedCanvasWidth(getSeqPanel().seqCanvas.getWidth());
894 / getSeqPanel().seqCanvas
895 .getWrappedCanvasWidth(getSeqPanel().seqCanvas
897 vscroll.setMaximum(max);
898 vscroll.setUnitIncrement(1);
899 vscroll.setVisibleAmount(1);
904 setScrollValues(av.getStartRes(), av.getStartSeq());
918 * @return DOCUMENT ME!
920 * @throws PrinterException
923 public int print(Graphics pg, PageFormat pf, int pi)
924 throws PrinterException
926 pg.translate((int) pf.getImageableX(), (int) pf.getImageableY());
928 int pwidth = (int) pf.getImageableWidth();
929 int pheight = (int) pf.getImageableHeight();
931 if (av.getWrapAlignment())
933 return printWrappedAlignment(pg, pwidth, pheight, pi);
937 return printUnwrapped(pg, pwidth, pheight, pi);
953 * @return DOCUMENT ME!
955 * @throws PrinterException
958 public int printUnwrapped(Graphics pg, int pwidth, int pheight, int pi)
959 throws PrinterException
961 int idWidth = getVisibleIdWidth(false);
962 FontMetrics fm = getFontMetrics(av.getFont());
963 int scaleHeight = av.getCharHeight() + fm.getDescent();
965 pg.setColor(Color.white);
966 pg.fillRect(0, 0, pwidth, pheight);
967 pg.setFont(av.getFont());
969 // //////////////////////////////////
970 // / How many sequences and residues can we fit on a printable page?
971 int totalRes = (pwidth - idWidth) / av.getCharWidth();
973 int totalSeq = (pheight - scaleHeight) / av.getCharHeight() - 1;
975 int pagesWide = (av.getAlignment().getWidth() / totalRes) + 1;
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
989 // ///////////////////////////
990 // / Only print these sequences and residues on this page
992 startRes = (pi % pagesWide) * totalRes;
993 endRes = (startRes + totalRes) - 1;
995 if (endRes > (av.getAlignment().getWidth() - 1))
997 endRes = av.getAlignment().getWidth() - 1;
1000 startSeq = (pi / pagesWide) * totalSeq;
1001 endSeq = startSeq + totalSeq;
1003 if (endSeq > av.getAlignment().getHeight())
1005 endSeq = av.getAlignment().getHeight();
1008 int pagesHigh = ((av.getAlignment().getHeight() / totalSeq) + 1)
1011 if (av.isShowAnnotation())
1013 pagesHigh += getAnnotationPanel().adjustPanelHeight() + 3;
1016 pagesHigh /= pheight;
1018 if (pi >= (pagesWide * pagesHigh))
1020 return Printable.NO_SUCH_PAGE;
1024 pg.translate(idWidth, 0);
1025 getScalePanel().drawScale(pg, startRes, endRes, pwidth - idWidth,
1027 pg.translate(-idWidth, scaleHeight);
1031 Color currentColor = null;
1032 Color currentTextColor = null;
1034 pg.setFont(getIdPanel().getIdCanvas().getIdfont());
1037 for (int i = startSeq; i < endSeq; i++)
1039 seq = av.getAlignment().getSequenceAt(i);
1040 if ((av.getSelectionGroup() != null)
1041 && av.getSelectionGroup().getSequences(null).contains(seq))
1043 currentColor = Color.gray;
1044 currentTextColor = Color.black;
1048 currentColor = av.getSequenceColour(seq);
1049 currentTextColor = Color.black;
1052 pg.setColor(currentColor);
1053 pg.fillRect(0, (i - startSeq) * av.getCharHeight(), idWidth,
1054 av.getCharHeight());
1056 pg.setColor(currentTextColor);
1059 if (av.isRightAlignIds())
1061 fm = pg.getFontMetrics();
1063 - fm.stringWidth(seq.getDisplayId(av.getShowJVSuffix()))
1067 pg.drawString(seq.getDisplayId(av.getShowJVSuffix()), xPos,
1068 (((i - startSeq) * av.getCharHeight()) + av.getCharHeight())
1069 - (av.getCharHeight() / 5));
1072 pg.setFont(av.getFont());
1074 // draw main sequence panel
1075 pg.translate(idWidth, 0);
1076 getSeqPanel().seqCanvas.drawPanel(pg, startRes, endRes, startSeq,
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));
1180 ypos + (av.getAlignment().getHeight() * av.getCharHeight()));
1182 pg.setFont(av.getFont());
1183 labels.drawComponent(pg, idWidth);
1187 - (av.getAlignment().getHeight() * av
1192 } while (ypos < totalHeight);
1194 pg.translate(idWidth, 0);
1196 getSeqPanel().seqCanvas.drawWrappedPanel(pg, pwidth - idWidth,
1199 if ((pi * pheight) < totalHeight)
1201 return Printable.PAGE_EXISTS;
1206 return Printable.NO_SUCH_PAGE;
1211 * get current sequence ID panel width, or nominal value if panel were to be
1212 * displayed using default settings
1216 public int getVisibleIdWidth()
1218 return getVisibleIdWidth(true);
1222 * get current sequence ID panel width, or nominal value if panel were to be
1223 * displayed using default settings
1226 * indicate if the Id width for onscreen or offscreen display should
1230 public int getVisibleIdWidth(boolean onscreen)
1232 // see if rendering offscreen - check preferences and calc width accordingly
1233 if (!onscreen && Cache.getDefault("FIGURE_AUTOIDWIDTH", false))
1235 return calculateIdWidth(-1).width + 4;
1237 Integer idwidth = null;
1239 || (idwidth = Cache.getIntegerProperty("FIGURE_FIXEDIDWIDTH")) == null)
1241 return (getIdPanel().getWidth() > 0 ? getIdPanel().getWidth()
1242 : calculateIdWidth().width + 4);
1244 return idwidth.intValue() + 4;
1247 void makeAlignmentImage(jalview.util.ImageMaker.TYPE type, File file)
1249 long progress = System.currentTimeMillis();
1250 headless = (System.getProperty("java.awt.headless") != null && System
1251 .getProperty("java.awt.headless").equals("true"));
1252 if (alignFrame != null && !headless)
1254 alignFrame.setProgressBar(MessageManager.formatMessage(
1255 "status.saving_file", new Object[] { type.getLabel() }),
1260 AlignmentDimension aDimension = getAlignmentDimension();
1263 jalview.util.ImageMaker im;
1264 final String imageAction, imageTitle;
1265 if (type == jalview.util.ImageMaker.TYPE.PNG)
1267 imageAction = "Create PNG image from alignment";
1270 else if (type == jalview.util.ImageMaker.TYPE.EPS)
1272 imageAction = "Create EPS file from alignment";
1273 imageTitle = alignFrame.getTitle();
1277 imageAction = "Create SVG file from alignment";
1278 imageTitle = alignFrame.getTitle();
1281 im = new jalview.util.ImageMaker(this, type, imageAction,
1282 aDimension.getWidth(), aDimension.getHeight(), file,
1284 if (av.getWrapAlignment())
1286 if (im.getGraphics() != null)
1288 printWrappedAlignment(im.getGraphics(), aDimension.getWidth(),
1289 aDimension.getHeight(), 0);
1295 if (im.getGraphics() != null)
1297 printUnwrapped(im.getGraphics(), aDimension.getWidth(),
1298 aDimension.getHeight(), 0);
1302 } catch (OutOfMemoryError err)
1305 System.out.println("########################\n" + "OUT OF MEMORY "
1306 + file + "\n" + "########################");
1307 new OOMWarning("Creating Image for " + file, err);
1308 // System.out.println("Create IMAGE: " + err);
1309 } catch (Exception ex)
1311 ex.printStackTrace();
1315 if (alignFrame != null && !headless)
1317 alignFrame.setProgressBar(
1318 MessageManager.getString("status.export_complete"),
1324 public AlignmentDimension getAlignmentDimension()
1326 int maxwidth = av.getAlignment().getWidth();
1327 if (av.hasHiddenColumns())
1329 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth);
1332 int height = ((av.getAlignment().getHeight() + 1) * av.getCharHeight())
1333 + getScalePanel().getHeight();
1334 int width = getVisibleIdWidth(false) + (maxwidth * av.getCharWidth());
1336 if (av.getWrapAlignment())
1338 height = getWrappedHeight();
1341 // need to obtain default alignment width and then add in any
1342 // additional allowance for id margin
1343 // this duplicates the calculation in getWrappedHeight but adjusts for
1345 width = alignFrame.getWidth() - vscroll.getPreferredSize().width
1346 - alignFrame.getInsets().left
1347 - alignFrame.getInsets().right - getVisibleIdWidth()
1348 + getVisibleIdWidth(false);
1352 width = getSeqPanel().getWidth() + getVisibleIdWidth(false);
1356 else if (av.isShowAnnotation())
1358 height += getAnnotationPanel().adjustPanelHeight() + 3;
1360 return new AlignmentDimension(width, height);
1367 public void makeEPS(File epsFile)
1369 makeAlignmentImage(jalview.util.ImageMaker.TYPE.EPS, epsFile);
1375 public void makePNG(File pngFile)
1377 makeAlignmentImage(jalview.util.ImageMaker.TYPE.PNG, pngFile);
1380 public void makeSVG(File svgFile)
1382 makeAlignmentImage(jalview.util.ImageMaker.TYPE.SVG, svgFile);
1385 public void makePNGImageMap(File imgMapFile, String imageName)
1387 // /////ONLY WORKS WITH NONE WRAPPED ALIGNMENTS
1388 // ////////////////////////////////////////////
1389 int idWidth = getVisibleIdWidth(false);
1390 FontMetrics fm = getFontMetrics(av.getFont());
1391 int scaleHeight = av.getCharHeight() + fm.getDescent();
1394 // ////////////////////////////////
1395 if (imgMapFile != null)
1399 int s, sSize = av.getAlignment().getHeight(), res, alwidth = av
1400 .getAlignment().getWidth(), g, gSize, f, fSize, sy;
1401 StringBuffer text = new StringBuffer();
1402 PrintWriter out = new PrintWriter(new FileWriter(imgMapFile));
1403 out.println(jalview.io.HTMLOutput.getImageMapHTML());
1404 out.println("<img src=\"" + imageName
1405 + "\" border=\"0\" usemap=\"#Map\" >"
1406 + "<map name=\"Map\">");
1408 for (s = 0; s < sSize; s++)
1410 sy = s * av.getCharHeight() + scaleHeight;
1412 SequenceI seq = av.getAlignment().getSequenceAt(s);
1413 SequenceFeature[] features = seq.getSequenceFeatures();
1414 SequenceGroup[] groups = av.getAlignment().findAllGroups(seq);
1415 for (res = 0; res < alwidth; res++)
1417 text = new StringBuffer();
1418 String triplet = null;
1419 if (av.getAlignment().isNucleotide())
1421 triplet = ResidueProperties.nucleotideName.get(seq
1422 .getCharAt(res) + "");
1426 triplet = ResidueProperties.aa2Triplet.get(seq.getCharAt(res)
1430 if (triplet == null)
1435 int alIndex = seq.findPosition(res);
1436 gSize = groups.length;
1437 for (g = 0; g < gSize; g++)
1439 if (text.length() < 1)
1441 text.append("<area shape=\"rect\" coords=\""
1442 + (idWidth + res * av.getCharWidth()) + "," + sy
1443 + "," + (idWidth + (res + 1) * av.getCharWidth())
1444 + "," + (av.getCharHeight() + sy) + "\""
1445 + " onMouseOver=\"toolTip('" + alIndex + " "
1449 if (groups[g].getStartRes() < res
1450 && groups[g].getEndRes() > res)
1452 text.append("<br><em>" + groups[g].getName() + "</em>");
1456 if (features != null)
1458 if (text.length() < 1)
1460 text.append("<area shape=\"rect\" coords=\""
1461 + (idWidth + res * av.getCharWidth()) + "," + sy
1462 + "," + (idWidth + (res + 1) * av.getCharWidth())
1463 + "," + (av.getCharHeight() + sy) + "\""
1464 + " onMouseOver=\"toolTip('" + alIndex + " "
1467 fSize = features.length;
1468 for (f = 0; f < fSize; f++)
1471 if ((features[f].getBegin() <= seq.findPosition(res))
1472 && (features[f].getEnd() >= seq.findPosition(res)))
1474 if (features[f].getType().equals("disulfide bond"))
1476 if (features[f].getBegin() == seq.findPosition(res)
1477 || features[f].getEnd() == seq
1480 text.append("<br>disulfide bond "
1481 + features[f].getBegin() + ":"
1482 + features[f].getEnd());
1487 text.append("<br>");
1488 text.append(features[f].getType());
1489 if (features[f].getDescription() != null
1490 && !features[f].getType().equals(
1491 features[f].getDescription()))
1493 text.append(" " + features[f].getDescription());
1496 if (features[f].getValue("status") != null)
1498 text.append(" (" + features[f].getValue("status")
1506 if (text.length() > 1)
1508 text.append("')\"; onMouseOut=\"toolTip()\"; href=\"#\">");
1509 out.println(text.toString());
1513 out.println("</map></body></html>");
1516 } catch (Exception ex)
1518 ex.printStackTrace();
1520 } // /////////END OF IMAGE MAP
1524 int getWrappedHeight()
1526 int seqPanelWidth = getSeqPanel().seqCanvas.getWidth();
1528 if (System.getProperty("java.awt.headless") != null
1529 && System.getProperty("java.awt.headless").equals("true"))
1531 seqPanelWidth = alignFrame.getWidth() - getVisibleIdWidth()
1532 - vscroll.getPreferredSize().width
1533 - alignFrame.getInsets().left - alignFrame.getInsets().right;
1536 int chunkWidth = getSeqPanel().seqCanvas
1537 .getWrappedCanvasWidth(seqPanelWidth);
1539 int hgap = av.getCharHeight();
1540 if (av.getScaleAboveWrapped())
1542 hgap += av.getCharHeight();
1545 int annotationHeight = 0;
1546 if (av.isShowAnnotation())
1548 annotationHeight = getAnnotationPanel().adjustPanelHeight();
1551 int cHeight = av.getAlignment().getHeight() * av.getCharHeight() + hgap
1554 int maxwidth = av.getAlignment().getWidth();
1555 if (av.hasHiddenColumns())
1557 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
1560 int height = ((maxwidth / chunkWidth) + 1) * cHeight;
1566 * close the panel - deregisters all listeners and nulls any references to
1569 public void closePanel()
1571 PaintRefresher.RemoveComponent(getSeqPanel().seqCanvas);
1572 PaintRefresher.RemoveComponent(getIdPanel().getIdCanvas());
1573 PaintRefresher.RemoveComponent(this);
1576 jalview.structure.StructureSelectionManager ssm = av
1577 .getStructureSelectionManager();
1578 ssm.removeStructureViewerListener(getSeqPanel(), null);
1579 ssm.removeSelectionListener(getSeqPanel());
1580 ssm.removeCommandListener(av);
1581 ssm.removeStructureViewerListener(getSeqPanel(), null);
1582 ssm.removeSelectionListener(getSeqPanel());
1583 av.setAlignment(null);
1588 if (Cache.log.isDebugEnabled())
1590 Cache.log.warn("Closing alignment panel which is already closed.");
1596 * hides or shows dynamic annotation rows based on groups and av state flags
1598 public void updateAnnotation()
1600 updateAnnotation(false, false);
1603 public void updateAnnotation(boolean applyGlobalSettings)
1605 updateAnnotation(applyGlobalSettings, false);
1608 public void updateAnnotation(boolean applyGlobalSettings,
1609 boolean preserveNewGroupSettings)
1611 av.updateGroupAnnotationSettings(applyGlobalSettings,
1612 preserveNewGroupSettings);
1613 adjustAnnotationHeight();
1617 public AlignmentI getAlignment()
1619 return av == null ? null : av.getAlignment();
1623 public String getViewName()
1629 * Make/Unmake this alignment panel the current input focus
1633 public void setSelected(boolean b)
1637 if (alignFrame.getSplitViewContainer() != null)
1640 * bring enclosing SplitFrame to front first if there is one
1642 ((SplitFrame) alignFrame.getSplitViewContainer()).setSelected(b);
1644 alignFrame.setSelected(b);
1645 } catch (Exception ex)
1651 alignFrame.setDisplayedView(this);
1656 public StructureSelectionManager getStructureSelectionManager()
1658 return av.getStructureSelectionManager();
1662 public void raiseOOMWarning(String string, OutOfMemoryError error)
1664 new OOMWarning(string, error, this);
1668 public jalview.api.FeatureRenderer cloneFeatureRenderer()
1671 return new FeatureRenderer(this);
1675 public jalview.api.FeatureRenderer getFeatureRenderer()
1677 return seqPanel.seqCanvas.getFeatureRenderer();
1680 public void updateFeatureRenderer(
1681 jalview.renderer.seqfeatures.FeatureRenderer fr)
1683 fr.transferSettings(getSeqPanel().seqCanvas.getFeatureRenderer());
1686 public void updateFeatureRendererFrom(jalview.api.FeatureRenderer fr)
1688 if (getSeqPanel().seqCanvas.getFeatureRenderer() != null)
1690 getSeqPanel().seqCanvas.getFeatureRenderer().transferSettings(fr);
1694 public ScalePanel getScalePanel()
1699 public void setScalePanel(ScalePanel scalePanel)
1701 this.scalePanel = scalePanel;
1704 public SeqPanel getSeqPanel()
1709 public void setSeqPanel(SeqPanel seqPanel)
1711 this.seqPanel = seqPanel;
1714 public AnnotationPanel getAnnotationPanel()
1716 return annotationPanel;
1719 public void setAnnotationPanel(AnnotationPanel annotationPanel)
1721 this.annotationPanel = annotationPanel;
1724 public AnnotationLabels getAlabels()
1729 public void setAlabels(AnnotationLabels alabels)
1731 this.alabels = alabels;
1734 public IdPanel getIdPanel()
1739 public void setIdPanel(IdPanel idPanel)
1741 this.idPanel = idPanel;
1745 * Follow a scrolling change in the (cDNA/Protein) complementary alignment.
1746 * The aim is to keep the two alignments 'lined up' on their centre columns.
1749 * holds mapped region(s) of this alignment that we are scrolling
1750 * 'to'; may be modified for sequence offset by this method
1751 * @param verticalOffset
1752 * the number of visible sequences to show above the mapped region
1754 public void scrollToCentre(SearchResults sr, int verticalOffset)
1757 * To avoid jumpy vertical scrolling (if some sequences are gapped or not
1758 * mapped), we can make the scroll-to location a sequence above the one
1761 SequenceI mappedTo = sr.getResultSequence(0);
1762 List<SequenceI> seqs = av.getAlignment().getSequences();
1765 * This is like AlignmentI.findIndex(seq) but here we are matching the
1766 * dataset sequence not the aligned sequence
1768 boolean matched = false;
1769 for (SequenceI seq : seqs)
1771 if (mappedTo == seq.getDatasetSequence())
1779 return; // failsafe, shouldn't happen
1783 * Scroll to position but centring the target residue.
1785 scrollToPosition(sr, verticalOffset, true, true);
1789 * Set a flag to say we are scrolling to follow a (cDNA/protein) complement.
1793 protected void setFollowingComplementScroll(boolean b)
1795 this.followingComplementScroll = b;
1798 protected boolean isFollowingComplementScroll()
1800 return this.followingComplementScroll;