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 Dimension e = idPanel.getSize();
570 alabels.setSize(new Dimension(e.width, annotationHeight));
572 annotationSpaceFillerHolder.setPreferredSize(new Dimension(
573 annotationSpaceFillerHolder.getWidth(), annotationHeight));
574 annotationScroller.validate();
575 annotationScroller.addNotify();
579 * update alignment layout for viewport settings
584 public void updateLayout()
587 setAnnotationVisible(av.isShowAnnotation());
588 boolean wrap = av.getWrapAlignment();
590 scalePanelHolder.setVisible(!wrap);
591 hscroll.setVisible(!wrap);
592 idwidthAdjuster.setVisible(!wrap);
596 annotationScroller.setVisible(false);
597 annotationSpaceFillerHolder.setVisible(false);
599 else if (av.isShowAnnotation())
601 annotationScroller.setVisible(true);
602 annotationSpaceFillerHolder.setVisible(true);
605 idSpaceFillerPanel1.setVisible(!wrap);
610 // return value is true if the scroll is valid
611 public boolean scrollUp(boolean up)
615 if (vscroll.getValue() < 1)
621 vscroll.setValue(vscroll.getValue() - 1);
625 if ((vextent + vscroll.getValue()) >= av.getAlignment().getHeight())
631 vscroll.setValue(vscroll.getValue() + 1);
645 * @return DOCUMENT ME!
647 public boolean scrollRight(boolean right)
651 if (hscroll.getValue() < 1)
657 hscroll.setValue(hscroll.getValue() - 1);
661 if ((hextent + hscroll.getValue()) >= av.getAlignment().getWidth())
667 hscroll.setValue(hscroll.getValue() + 1);
676 * Adjust row/column scrollers to show a visible position in the alignment.
679 * visible column to scroll to
681 * visible row to scroll to
684 public void setScrollValues(int x, int y)
686 // System.err.println("Scroll " + this.av.viewName + " to " + x + "," + y);
687 if (av == null || av.getAlignment() == null)
691 int width = av.getAlignment().getWidth();
692 int height = av.getAlignment().getHeight();
694 if (av.hasHiddenColumns())
696 width = av.getColumnSelection().findColumnPosition(width);
699 av.setEndRes((x + (getSeqPanel().seqCanvas.getWidth() / av
700 .getCharWidth())) - 1);
702 hextent = getSeqPanel().seqCanvas.getWidth() / av.getCharWidth();
703 vextent = getSeqPanel().seqCanvas.getHeight() / av.getCharHeight();
710 if (vextent > height)
715 if ((hextent + x) > width)
720 if ((vextent + y) > height)
722 y = height - vextent;
735 hscroll.setValues(x, hextent, 0, width);
736 vscroll.setValues(y, vextent, 0, height);
745 public void adjustmentValueChanged(AdjustmentEvent evt)
747 int oldX = av.getStartRes();
748 int oldY = av.getStartSeq();
750 if (evt.getSource() == hscroll)
752 int x = hscroll.getValue();
754 av.setEndRes((x + (getSeqPanel().seqCanvas.getWidth() / av
755 .getCharWidth())) - 1);
758 if (evt.getSource() == vscroll)
760 int offy = vscroll.getValue();
762 if (av.getWrapAlignment())
766 int rowSize = getSeqPanel().seqCanvas
767 .getWrappedCanvasWidth(getSeqPanel().seqCanvas.getWidth());
768 av.setStartRes(offy * rowSize);
769 av.setEndRes((offy + 1) * rowSize);
773 // This is only called if file loaded is a jar file that
774 // was wrapped when saved and user has wrap alignment true
775 // as preference setting
776 SwingUtilities.invokeLater(new Runnable()
780 setScrollValues(av.getStartRes(), av.getStartSeq());
787 av.setStartSeq(offy);
789 + (getSeqPanel().seqCanvas.getHeight() / av.getCharHeight()));
793 if (overviewPanel != null)
795 overviewPanel.setBoxPosition();
798 int scrollX = av.startRes - oldX;
799 int scrollY = av.startSeq - oldY;
801 if (av.getWrapAlignment() || !fastPaint)
807 // Make sure we're not trying to draw a panel
808 // larger than the visible window
809 if (scrollX > av.endRes - av.startRes)
811 scrollX = av.endRes - av.startRes;
813 else if (scrollX < av.startRes - av.endRes)
815 scrollX = av.startRes - av.endRes;
818 if (scrollX != 0 || scrollY != 0)
820 getIdPanel().getIdCanvas().fastPaint(scrollY);
821 getSeqPanel().seqCanvas.fastPaint(scrollX, scrollY);
822 getScalePanel().repaint();
824 if (av.isShowAnnotation() && scrollX != 0)
826 getAnnotationPanel().fastPaint(scrollX);
831 * If there is one, scroll the (Protein/cDNA) complementary alignment to
832 * match, unless we are ourselves doing that.
834 if (isFollowingComplementScroll())
836 setFollowingComplementScroll(false);
840 av.scrollComplementaryAlignment();
845 * Repaint the alignment including the annotations and overview panels (if
848 public void paintAlignment(boolean updateOverview)
850 final AnnotationSorter sorter = new AnnotationSorter(getAlignment(),
851 av.isShowAutocalculatedAbove());
852 sorter.sort(getAlignment().getAlignmentAnnotation(),
853 av.getSortAnnotationsBy());
858 // TODO: determine if this paintAlignment changed structure colours
859 av.getStructureSelectionManager().sequenceColoursChanged(this);
861 if (overviewPanel != null)
863 overviewPanel.updateOverviewImage();
874 public void paintComponent(Graphics g)
878 Dimension d = getIdPanel().getIdCanvas().getPreferredSize();
879 idPanelHolder.setPreferredSize(d);
880 hscrollFillerPanel.setPreferredSize(new Dimension(d.width, 12));
883 if (av.getWrapAlignment())
885 int maxwidth = av.getAlignment().getWidth();
887 if (av.hasHiddenColumns())
889 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
892 int canvasWidth = getSeqPanel().seqCanvas
893 .getWrappedCanvasWidth(getSeqPanel().seqCanvas.getWidth());
897 / getSeqPanel().seqCanvas
898 .getWrappedCanvasWidth(getSeqPanel().seqCanvas
900 vscroll.setMaximum(max);
901 vscroll.setUnitIncrement(1);
902 vscroll.setVisibleAmount(1);
907 setScrollValues(av.getStartRes(), av.getStartSeq());
921 * @return DOCUMENT ME!
923 * @throws PrinterException
926 public int print(Graphics pg, PageFormat pf, int pi)
927 throws PrinterException
929 pg.translate((int) pf.getImageableX(), (int) pf.getImageableY());
931 int pwidth = (int) pf.getImageableWidth();
932 int pheight = (int) pf.getImageableHeight();
934 if (av.getWrapAlignment())
936 return printWrappedAlignment(pg, pwidth, pheight, pi);
940 return printUnwrapped(pg, pwidth, pheight, pi);
956 * @return DOCUMENT ME!
958 * @throws PrinterException
961 public int printUnwrapped(Graphics pg, int pwidth, int pheight, int pi)
962 throws PrinterException
964 int idWidth = getVisibleIdWidth(false);
965 FontMetrics fm = getFontMetrics(av.getFont());
966 int scaleHeight = av.getCharHeight() + fm.getDescent();
968 pg.setColor(Color.white);
969 pg.fillRect(0, 0, pwidth, pheight);
970 pg.setFont(av.getFont());
972 // //////////////////////////////////
973 // / How many sequences and residues can we fit on a printable page?
974 int totalRes = (pwidth - idWidth) / av.getCharWidth();
976 int totalSeq = (pheight - scaleHeight) / av.getCharHeight() - 1;
978 int pagesWide = (av.getAlignment().getWidth() / totalRes) + 1;
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
992 // ///////////////////////////
993 // / Only print these sequences and residues on this page
995 startRes = (pi % pagesWide) * totalRes;
996 endRes = (startRes + totalRes) - 1;
998 if (endRes > (av.getAlignment().getWidth() - 1))
1000 endRes = av.getAlignment().getWidth() - 1;
1003 startSeq = (pi / pagesWide) * totalSeq;
1004 endSeq = startSeq + totalSeq;
1006 if (endSeq > av.getAlignment().getHeight())
1008 endSeq = av.getAlignment().getHeight();
1011 int pagesHigh = ((av.getAlignment().getHeight() / totalSeq) + 1)
1014 if (av.isShowAnnotation())
1016 pagesHigh += getAnnotationPanel().adjustPanelHeight() + 3;
1019 pagesHigh /= pheight;
1021 if (pi >= (pagesWide * pagesHigh))
1023 return Printable.NO_SUCH_PAGE;
1027 pg.translate(idWidth, 0);
1028 getScalePanel().drawScale(pg, startRes, endRes, pwidth - idWidth,
1030 pg.translate(-idWidth, scaleHeight);
1034 Color currentColor = null;
1035 Color currentTextColor = null;
1037 pg.setFont(getIdPanel().getIdCanvas().getIdfont());
1040 for (int i = startSeq; i < endSeq; i++)
1042 seq = av.getAlignment().getSequenceAt(i);
1043 if ((av.getSelectionGroup() != null)
1044 && av.getSelectionGroup().getSequences(null).contains(seq))
1046 currentColor = Color.gray;
1047 currentTextColor = Color.black;
1051 currentColor = av.getSequenceColour(seq);
1052 currentTextColor = Color.black;
1055 pg.setColor(currentColor);
1056 pg.fillRect(0, (i - startSeq) * av.getCharHeight(), idWidth,
1057 av.getCharHeight());
1059 pg.setColor(currentTextColor);
1062 if (av.isRightAlignIds())
1064 fm = pg.getFontMetrics();
1066 - fm.stringWidth(seq.getDisplayId(av.getShowJVSuffix()))
1070 pg.drawString(seq.getDisplayId(av.getShowJVSuffix()), xPos,
1071 (((i - startSeq) * av.getCharHeight()) + av.getCharHeight())
1072 - (av.getCharHeight() / 5));
1075 pg.setFont(av.getFont());
1077 // draw main sequence panel
1078 pg.translate(idWidth, 0);
1079 getSeqPanel().seqCanvas.drawPanel(pg, startRes, endRes, startSeq,
1082 if (av.isShowAnnotation() && (endSeq == av.getAlignment().getHeight()))
1084 // draw annotation - need to offset for current scroll position
1085 int offset = -getAlabels().getScrollOffset();
1086 pg.translate(0, offset);
1087 pg.translate(-idWidth - 3, (endSeq - startSeq) * av.getCharHeight()
1089 getAlabels().drawComponent(pg, idWidth);
1090 pg.translate(idWidth + 3, 0);
1091 getAnnotationPanel().renderer.drawComponent(getAnnotationPanel(), av,
1092 pg, -1, startRes, endRes + 1);
1093 pg.translate(0, -offset);
1096 return Printable.PAGE_EXISTS;
1111 * @return DOCUMENT ME!
1113 * @throws PrinterException
1116 public int printWrappedAlignment(Graphics pg, int pwidth, int pheight,
1117 int pi) throws PrinterException
1119 int annotationHeight = 0;
1120 AnnotationLabels labels = null;
1121 if (av.isShowAnnotation())
1123 annotationHeight = getAnnotationPanel().adjustPanelHeight();
1124 labels = new AnnotationLabels(av);
1127 int hgap = av.getCharHeight();
1128 if (av.getScaleAboveWrapped())
1130 hgap += av.getCharHeight();
1133 int cHeight = av.getAlignment().getHeight() * av.getCharHeight() + hgap
1136 int idWidth = getVisibleIdWidth(false);
1138 int maxwidth = av.getAlignment().getWidth();
1139 if (av.hasHiddenColumns())
1141 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
1144 int resWidth = getSeqPanel().seqCanvas.getWrappedCanvasWidth(pwidth
1147 int totalHeight = cHeight * (maxwidth / resWidth + 1);
1149 pg.setColor(Color.white);
1150 pg.fillRect(0, 0, pwidth, pheight);
1151 pg.setFont(av.getFont());
1155 pg.setColor(Color.black);
1157 pg.translate(0, -pi * pheight);
1159 pg.setClip(0, pi * pheight, pwidth, pheight);
1165 for (int i = 0; i < av.getAlignment().getHeight(); i++)
1167 pg.setFont(getIdPanel().getIdCanvas().getIdfont());
1168 SequenceI s = av.getAlignment().getSequenceAt(i);
1169 String string = s.getDisplayId(av.getShowJVSuffix());
1171 if (av.isRightAlignIds())
1173 FontMetrics fm = pg.getFontMetrics();
1174 xPos = idWidth - fm.stringWidth(string) - 4;
1176 pg.drawString(string, xPos,
1177 ((i * av.getCharHeight()) + ypos + av.getCharHeight())
1178 - (av.getCharHeight() / 5));
1183 ypos + (av.getAlignment().getHeight() * av.getCharHeight()));
1185 pg.setFont(av.getFont());
1186 labels.drawComponent(pg, idWidth);
1190 - (av.getAlignment().getHeight() * av
1195 } while (ypos < totalHeight);
1197 pg.translate(idWidth, 0);
1199 getSeqPanel().seqCanvas.drawWrappedPanel(pg, pwidth - idWidth,
1202 if ((pi * pheight) < totalHeight)
1204 return Printable.PAGE_EXISTS;
1209 return Printable.NO_SUCH_PAGE;
1214 * get current sequence ID panel width, or nominal value if panel were to be
1215 * displayed using default settings
1219 public int getVisibleIdWidth()
1221 return getVisibleIdWidth(true);
1225 * get current sequence ID panel width, or nominal value if panel were to be
1226 * displayed using default settings
1229 * indicate if the Id width for onscreen or offscreen display should
1233 public int getVisibleIdWidth(boolean onscreen)
1235 // see if rendering offscreen - check preferences and calc width accordingly
1236 if (!onscreen && Cache.getDefault("FIGURE_AUTOIDWIDTH", false))
1238 return calculateIdWidth(-1).width + 4;
1240 Integer idwidth = null;
1242 || (idwidth = Cache.getIntegerProperty("FIGURE_FIXEDIDWIDTH")) == null)
1244 return (getIdPanel().getWidth() > 0 ? getIdPanel().getWidth()
1245 : calculateIdWidth().width + 4);
1247 return idwidth.intValue() + 4;
1250 void makeAlignmentImage(jalview.util.ImageMaker.TYPE type, File file)
1252 long progress = System.currentTimeMillis();
1253 headless = (System.getProperty("java.awt.headless") != null && System
1254 .getProperty("java.awt.headless").equals("true"));
1255 if (alignFrame != null && !headless)
1257 alignFrame.setProgressBar(MessageManager.formatMessage(
1258 "status.saving_file", new Object[] { type.getLabel() }),
1263 AlignmentDimension aDimension = getAlignmentDimension();
1266 jalview.util.ImageMaker im;
1267 final String imageAction, imageTitle;
1268 if (type == jalview.util.ImageMaker.TYPE.PNG)
1270 imageAction = "Create PNG image from alignment";
1273 else if (type == jalview.util.ImageMaker.TYPE.EPS)
1275 imageAction = "Create EPS file from alignment";
1276 imageTitle = alignFrame.getTitle();
1280 imageAction = "Create SVG file from alignment";
1281 imageTitle = alignFrame.getTitle();
1284 im = new jalview.util.ImageMaker(this, type, imageAction,
1285 aDimension.getWidth(), aDimension.getHeight(), file,
1287 if (av.getWrapAlignment())
1289 if (im.getGraphics() != null)
1291 printWrappedAlignment(im.getGraphics(), aDimension.getWidth(),
1292 aDimension.getHeight(), 0);
1298 if (im.getGraphics() != null)
1300 printUnwrapped(im.getGraphics(), aDimension.getWidth(),
1301 aDimension.getHeight(), 0);
1305 } catch (OutOfMemoryError err)
1308 System.out.println("########################\n" + "OUT OF MEMORY "
1309 + file + "\n" + "########################");
1310 new OOMWarning("Creating Image for " + file, err);
1311 // System.out.println("Create IMAGE: " + err);
1312 } catch (Exception ex)
1314 ex.printStackTrace();
1318 if (alignFrame != null && !headless)
1320 alignFrame.setProgressBar(
1321 MessageManager.getString("status.export_complete"),
1327 public AlignmentDimension getAlignmentDimension()
1329 int maxwidth = av.getAlignment().getWidth();
1330 if (av.hasHiddenColumns())
1332 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth);
1335 int height = ((av.getAlignment().getHeight() + 1) * av.getCharHeight())
1336 + getScalePanel().getHeight();
1337 int width = getVisibleIdWidth(false) + (maxwidth * av.getCharWidth());
1339 if (av.getWrapAlignment())
1341 height = getWrappedHeight();
1344 // need to obtain default alignment width and then add in any
1345 // additional allowance for id margin
1346 // this duplicates the calculation in getWrappedHeight but adjusts for
1348 width = alignFrame.getWidth() - vscroll.getPreferredSize().width
1349 - alignFrame.getInsets().left
1350 - alignFrame.getInsets().right - getVisibleIdWidth()
1351 + getVisibleIdWidth(false);
1355 width = getSeqPanel().getWidth() + getVisibleIdWidth(false);
1359 else if (av.isShowAnnotation())
1361 height += getAnnotationPanel().adjustPanelHeight() + 3;
1363 return new AlignmentDimension(width, height);
1370 public void makeEPS(File epsFile)
1372 makeAlignmentImage(jalview.util.ImageMaker.TYPE.EPS, epsFile);
1378 public void makePNG(File pngFile)
1380 makeAlignmentImage(jalview.util.ImageMaker.TYPE.PNG, pngFile);
1383 public void makeSVG(File svgFile)
1385 makeAlignmentImage(jalview.util.ImageMaker.TYPE.SVG, svgFile);
1388 public void makePNGImageMap(File imgMapFile, String imageName)
1390 // /////ONLY WORKS WITH NONE WRAPPED ALIGNMENTS
1391 // ////////////////////////////////////////////
1392 int idWidth = getVisibleIdWidth(false);
1393 FontMetrics fm = getFontMetrics(av.getFont());
1394 int scaleHeight = av.getCharHeight() + fm.getDescent();
1397 // ////////////////////////////////
1398 if (imgMapFile != null)
1402 int s, sSize = av.getAlignment().getHeight(), res, alwidth = av
1403 .getAlignment().getWidth(), g, gSize, f, fSize, sy;
1404 StringBuffer text = new StringBuffer();
1405 PrintWriter out = new PrintWriter(new FileWriter(imgMapFile));
1406 out.println(jalview.io.HTMLOutput.getImageMapHTML());
1407 out.println("<img src=\"" + imageName
1408 + "\" border=\"0\" usemap=\"#Map\" >"
1409 + "<map name=\"Map\">");
1411 for (s = 0; s < sSize; s++)
1413 sy = s * av.getCharHeight() + scaleHeight;
1415 SequenceI seq = av.getAlignment().getSequenceAt(s);
1416 SequenceFeature[] features = seq.getSequenceFeatures();
1417 SequenceGroup[] groups = av.getAlignment().findAllGroups(seq);
1418 for (res = 0; res < alwidth; res++)
1420 text = new StringBuffer();
1421 String triplet = null;
1422 if (av.getAlignment().isNucleotide())
1424 triplet = ResidueProperties.nucleotideName.get(seq
1425 .getCharAt(res) + "");
1429 triplet = ResidueProperties.aa2Triplet.get(seq.getCharAt(res)
1433 if (triplet == null)
1438 int alIndex = seq.findPosition(res);
1439 gSize = groups.length;
1440 for (g = 0; g < gSize; g++)
1442 if (text.length() < 1)
1444 text.append("<area shape=\"rect\" coords=\""
1445 + (idWidth + res * av.getCharWidth()) + "," + sy
1446 + "," + (idWidth + (res + 1) * av.getCharWidth())
1447 + "," + (av.getCharHeight() + sy) + "\""
1448 + " onMouseOver=\"toolTip('" + alIndex + " "
1452 if (groups[g].getStartRes() < res
1453 && groups[g].getEndRes() > res)
1455 text.append("<br><em>" + groups[g].getName() + "</em>");
1459 if (features != null)
1461 if (text.length() < 1)
1463 text.append("<area shape=\"rect\" coords=\""
1464 + (idWidth + res * av.getCharWidth()) + "," + sy
1465 + "," + (idWidth + (res + 1) * av.getCharWidth())
1466 + "," + (av.getCharHeight() + sy) + "\""
1467 + " onMouseOver=\"toolTip('" + alIndex + " "
1470 fSize = features.length;
1471 for (f = 0; f < fSize; f++)
1474 if ((features[f].getBegin() <= seq.findPosition(res))
1475 && (features[f].getEnd() >= seq.findPosition(res)))
1477 if (features[f].getType().equals("disulfide bond"))
1479 if (features[f].getBegin() == seq.findPosition(res)
1480 || features[f].getEnd() == seq
1483 text.append("<br>disulfide bond "
1484 + features[f].getBegin() + ":"
1485 + features[f].getEnd());
1490 text.append("<br>");
1491 text.append(features[f].getType());
1492 if (features[f].getDescription() != null
1493 && !features[f].getType().equals(
1494 features[f].getDescription()))
1496 text.append(" " + features[f].getDescription());
1499 if (features[f].getValue("status") != null)
1501 text.append(" (" + features[f].getValue("status")
1509 if (text.length() > 1)
1511 text.append("')\"; onMouseOut=\"toolTip()\"; href=\"#\">");
1512 out.println(text.toString());
1516 out.println("</map></body></html>");
1519 } catch (Exception ex)
1521 ex.printStackTrace();
1523 } // /////////END OF IMAGE MAP
1527 int getWrappedHeight()
1529 int seqPanelWidth = getSeqPanel().seqCanvas.getWidth();
1531 if (System.getProperty("java.awt.headless") != null
1532 && System.getProperty("java.awt.headless").equals("true"))
1534 seqPanelWidth = alignFrame.getWidth() - getVisibleIdWidth()
1535 - vscroll.getPreferredSize().width
1536 - alignFrame.getInsets().left - alignFrame.getInsets().right;
1539 int chunkWidth = getSeqPanel().seqCanvas
1540 .getWrappedCanvasWidth(seqPanelWidth);
1542 int hgap = av.getCharHeight();
1543 if (av.getScaleAboveWrapped())
1545 hgap += av.getCharHeight();
1548 int annotationHeight = 0;
1549 if (av.isShowAnnotation())
1551 annotationHeight = getAnnotationPanel().adjustPanelHeight();
1554 int cHeight = av.getAlignment().getHeight() * av.getCharHeight() + hgap
1557 int maxwidth = av.getAlignment().getWidth();
1558 if (av.hasHiddenColumns())
1560 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
1563 int height = ((maxwidth / chunkWidth) + 1) * cHeight;
1569 * close the panel - deregisters all listeners and nulls any references to
1572 public void closePanel()
1574 PaintRefresher.RemoveComponent(getSeqPanel().seqCanvas);
1575 PaintRefresher.RemoveComponent(getIdPanel().getIdCanvas());
1576 PaintRefresher.RemoveComponent(this);
1579 jalview.structure.StructureSelectionManager ssm = av
1580 .getStructureSelectionManager();
1581 ssm.removeStructureViewerListener(getSeqPanel(), null);
1582 ssm.removeSelectionListener(getSeqPanel());
1583 ssm.removeCommandListener(av);
1584 ssm.removeStructureViewerListener(getSeqPanel(), null);
1585 ssm.removeSelectionListener(getSeqPanel());
1586 av.setAlignment(null);
1591 if (Cache.log.isDebugEnabled())
1593 Cache.log.warn("Closing alignment panel which is already closed.");
1599 * hides or shows dynamic annotation rows based on groups and av state flags
1601 public void updateAnnotation()
1603 updateAnnotation(false, false);
1606 public void updateAnnotation(boolean applyGlobalSettings)
1608 updateAnnotation(applyGlobalSettings, false);
1611 public void updateAnnotation(boolean applyGlobalSettings,
1612 boolean preserveNewGroupSettings)
1614 av.updateGroupAnnotationSettings(applyGlobalSettings,
1615 preserveNewGroupSettings);
1616 adjustAnnotationHeight();
1620 public AlignmentI getAlignment()
1622 return av == null ? null : av.getAlignment();
1626 public String getViewName()
1632 * Make/Unmake this alignment panel the current input focus
1636 public void setSelected(boolean b)
1640 if (alignFrame.getSplitViewContainer() != null)
1643 * bring enclosing SplitFrame to front first if there is one
1645 ((SplitFrame) alignFrame.getSplitViewContainer()).setSelected(b);
1647 alignFrame.setSelected(b);
1648 } catch (Exception ex)
1654 alignFrame.setDisplayedView(this);
1659 public StructureSelectionManager getStructureSelectionManager()
1661 return av.getStructureSelectionManager();
1665 public void raiseOOMWarning(String string, OutOfMemoryError error)
1667 new OOMWarning(string, error, this);
1671 public jalview.api.FeatureRenderer cloneFeatureRenderer()
1674 return new FeatureRenderer(this);
1678 public jalview.api.FeatureRenderer getFeatureRenderer()
1680 return seqPanel.seqCanvas.getFeatureRenderer();
1683 public void updateFeatureRenderer(
1684 jalview.renderer.seqfeatures.FeatureRenderer fr)
1686 fr.transferSettings(getSeqPanel().seqCanvas.getFeatureRenderer());
1689 public void updateFeatureRendererFrom(jalview.api.FeatureRenderer fr)
1691 if (getSeqPanel().seqCanvas.getFeatureRenderer() != null)
1693 getSeqPanel().seqCanvas.getFeatureRenderer().transferSettings(fr);
1697 public ScalePanel getScalePanel()
1702 public void setScalePanel(ScalePanel scalePanel)
1704 this.scalePanel = scalePanel;
1707 public SeqPanel getSeqPanel()
1712 public void setSeqPanel(SeqPanel seqPanel)
1714 this.seqPanel = seqPanel;
1717 public AnnotationPanel getAnnotationPanel()
1719 return annotationPanel;
1722 public void setAnnotationPanel(AnnotationPanel annotationPanel)
1724 this.annotationPanel = annotationPanel;
1727 public AnnotationLabels getAlabels()
1732 public void setAlabels(AnnotationLabels alabels)
1734 this.alabels = alabels;
1737 public IdPanel getIdPanel()
1742 public void setIdPanel(IdPanel idPanel)
1744 this.idPanel = idPanel;
1748 * Follow a scrolling change in the (cDNA/Protein) complementary alignment.
1749 * The aim is to keep the two alignments 'lined up' on their centre columns.
1752 * holds mapped region(s) of this alignment that we are scrolling
1753 * 'to'; may be modified for sequence offset by this method
1754 * @param verticalOffset
1755 * the number of visible sequences to show above the mapped region
1757 public void scrollToCentre(SearchResults sr, int verticalOffset)
1760 * To avoid jumpy vertical scrolling (if some sequences are gapped or not
1761 * mapped), we can make the scroll-to location a sequence above the one
1764 SequenceI mappedTo = sr.getResultSequence(0);
1765 List<SequenceI> seqs = av.getAlignment().getSequences();
1768 * This is like AlignmentI.findIndex(seq) but here we are matching the
1769 * dataset sequence not the aligned sequence
1771 boolean matched = false;
1772 for (SequenceI seq : seqs)
1774 if (mappedTo == seq.getDatasetSequence())
1782 return; // failsafe, shouldn't happen
1786 * Scroll to position but centring the target residue.
1788 scrollToPosition(sr, verticalOffset, true, true);
1792 * Set a flag to say we are scrolling to follow a (cDNA/protein) complement.
1796 protected void setFollowingComplementScroll(boolean b)
1798 this.followingComplementScroll = b;
1801 protected boolean isFollowingComplementScroll()
1803 return this.followingComplementScroll;