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 dontScrollComplement;
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()
141 public void propertyChange(PropertyChangeEvent evt)
143 if (evt.getPropertyName().equals("alignment"))
145 PaintRefresher.Refresh(ap, av.getSequenceSetId(), true, true);
151 adjustAnnotationHeight();
156 public AlignViewportI getAlignViewport()
161 public void alignmentChanged()
163 av.alignmentChanged(this);
165 alignFrame.updateEditMenuBar();
167 paintAlignment(true);
174 public void fontChanged()
176 // set idCanvas bufferedImage to null
177 // to prevent drawing old image
178 FontMetrics fm = getFontMetrics(av.getFont());
180 scalePanelHolder.setPreferredSize(new Dimension(10, av.getCharHeight()
182 idSpaceFillerPanel1.setPreferredSize(new Dimension(10, av
183 .getCharHeight() + fm.getDescent()));
185 getIdPanel().getIdCanvas().gg = null;
186 getSeqPanel().seqCanvas.img = null;
187 getAnnotationPanel().adjustPanelHeight();
189 Dimension d = calculateIdWidth();
191 d.setSize(d.width + 4, d.height);
192 getIdPanel().getIdCanvas().setPreferredSize(d);
193 hscrollFillerPanel.setPreferredSize(d);
195 if (overviewPanel != null)
197 overviewPanel.setBoxPosition();
199 if (this.alignFrame.getSplitViewContainer() != null)
201 ((SplitFrame) this.alignFrame.getSplitViewContainer()).adjustLayout();
208 * Calculate the width of the alignment labels based on the displayed names
209 * and any bounds on label width set in preferences.
211 * @return Dimension giving the maximum width of the alignment label panel
212 * that should be used.
214 public Dimension calculateIdWidth()
216 // calculate sensible default width when no preference is available
218 if (av.getIdWidth() < 0)
220 int afwidth = (alignFrame != null ? alignFrame.getWidth() : 300);
221 int maxwidth = Math.max(20, Math.min(afwidth - 200, 2 * afwidth / 3));
222 r = calculateIdWidth(maxwidth);
223 av.setIdWidth(r.width);
228 r.width = av.getIdWidth();
235 * Calculate the width of the alignment labels based on the displayed names
236 * and any bounds on label width set in preferences.
239 * -1 or maximum width allowed for IdWidth
240 * @return Dimension giving the maximum width of the alignment label panel
241 * that should be used.
243 public Dimension calculateIdWidth(int maxwidth)
245 Container c = new Container();
247 FontMetrics fm = c.getFontMetrics(new Font(av.font.getName(),
248 Font.ITALIC, av.font.getSize()));
250 AlignmentI al = av.getAlignment();
255 while ((i < al.getHeight()) && (al.getSequenceAt(i) != null))
257 SequenceI s = al.getSequenceAt(i);
259 id = s.getDisplayId(av.getShowJVSuffix());
261 if (fm.stringWidth(id) > idWidth)
263 idWidth = fm.stringWidth(id);
269 // Also check annotation label widths
272 if (al.getAlignmentAnnotation() != null)
274 fm = c.getFontMetrics(getAlabels().getFont());
276 while (i < al.getAlignmentAnnotation().length)
278 String label = al.getAlignmentAnnotation()[i].label;
280 if (fm.stringWidth(label) > idWidth)
282 idWidth = fm.stringWidth(label);
289 return new Dimension(maxwidth < 0 ? idWidth : Math.min(maxwidth,
294 * Highlight the given results on the alignment.
297 public void highlightSearchResults(SearchResults results)
299 scrollToPosition(results);
300 getSeqPanel().seqCanvas.highlightSearchResults(results);
304 * Scroll the view to show the position of the highlighted region in results
305 * (if any) and redraw the overview
309 public boolean scrollToPosition(SearchResults results)
311 return scrollToPosition(results, 0, true, false);
315 * Scroll the view to show the position of the highlighted region in results
318 * @param searchResults
319 * @param redrawOverview
322 public boolean scrollToPosition(SearchResults searchResults,
323 boolean redrawOverview)
325 return scrollToPosition(searchResults, 0, redrawOverview, false);
329 * Scroll the view to show the position of the highlighted region in results
333 * @param verticalOffset
334 * if greater than zero, allows scrolling to a position below the
335 * first displayed sequence
336 * @param redrawOverview
337 * - when set, the overview will be recalculated (takes longer)
339 * if true, try to centre the search results horizontally in the view
340 * @return false if results were not found
342 public boolean scrollToPosition(SearchResults results,
343 int verticalOffset, boolean redrawOverview, boolean centre)
345 int startv, endv, starts, ends;
346 // TODO: properly locate search results in view when large numbers of hidden
347 // columns exist before highlighted region
348 // do we need to scroll the panel?
349 // TODO: tons of nullpointerexceptions raised here.
350 if (results != null && results.getSize() > 0 && av != null
351 && av.getAlignment() != null)
353 int seqIndex = av.getAlignment().findIndex(results);
358 SequenceI seq = av.getAlignment().getSequenceAt(seqIndex);
360 int[] r = results.getResults(seq, 0, av.getAlignment().getWidth());
368 // System.err.println(this.av.viewName + " Seq : " + seqIndex
369 // + " Scroll to " + start + "," + end);
372 * To centre results, scroll to positions half the visible width
373 * left/right of the start/end positions
377 int offset = (av.getEndRes() - av.getStartRes() + 1) / 2 - 1;
378 start = Math.max(start - offset, 0);
379 end = end + offset - 1;
385 if (end == seq.getEnd())
389 if (av.hasHiddenColumns())
391 start = av.getColumnSelection().findColumnPosition(start);
392 end = av.getColumnSelection().findColumnPosition(end);
395 if (!av.getColumnSelection().isVisible(r[0]))
397 // don't scroll - position isn't visible
404 * allow for offset of target sequence (actually scroll to one above it)
406 seqIndex = Math.max(0, seqIndex - verticalOffset);
408 // System.out.println("start=" + start + ", end=" + end + ", startv="
409 // + av.getStartRes() + ", endv=" + av.getEndRes() + ", starts="
410 // + av.getStartSeq() + ", ends=" + av.getEndSeq());
411 if (!av.getWrapAlignment())
413 if ((startv = av.getStartRes()) >= start)
416 * Scroll left to make start of search results visible
418 // setScrollValues(start - 1, seqIndex); // plus one residue
419 setScrollValues(start, seqIndex);
421 else if ((endv = av.getEndRes()) <= end)
424 * Scroll right to make end of search results visible
426 // setScrollValues(startv + 1 + end - endv, seqIndex); // plus one
427 setScrollValues(startv + end - endv, seqIndex);
429 else if ((starts = av.getStartSeq()) > seqIndex)
432 * Scroll up to make start of search results visible
434 setScrollValues(av.getStartRes(), seqIndex);
436 else if ((ends = av.getEndSeq()) <= seqIndex)
439 * Scroll down to make end of search results visible
441 setScrollValues(av.getStartRes(), starts + seqIndex - ends + 1);
444 * Else results are already visible - no need to scroll
449 scrollToWrappedVisible(start);
452 if (redrawOverview && overviewPanel != null)
454 overviewPanel.setBoxPosition();
456 paintAlignment(redrawOverview);
460 void scrollToWrappedVisible(int res)
462 int cwidth = getSeqPanel().seqCanvas
463 .getWrappedCanvasWidth(getSeqPanel().seqCanvas.getWidth());
464 if (res < av.getStartRes() || res >= (av.getStartRes() + cwidth))
466 vscroll.setValue((res / cwidth));
467 av.startRes = vscroll.getValue() * cwidth;
475 * @return DOCUMENT ME!
477 public OverviewPanel getOverviewPanel()
479 return overviewPanel;
488 public void setOverviewPanel(OverviewPanel op)
496 * Hide or show annotation panel
499 public void setAnnotationVisible(boolean b)
501 if (!av.getWrapAlignment())
503 annotationSpaceFillerHolder.setVisible(b);
504 annotationScroller.setVisible(b);
510 * automatically adjust annotation panel height for new annotation whilst
511 * ensuring the alignment is still visible.
514 public void adjustAnnotationHeight()
516 // TODO: display vertical annotation scrollbar if necessary
517 // this is called after loading new annotation onto alignment
518 if (alignFrame.getHeight() == 0)
520 System.out.println("NEEDS FIXING");
522 validateAnnotationDimensions(true);
524 paintAlignment(true);
528 * calculate the annotation dimensions and refresh slider values accordingly.
529 * need to do repaints/notifys afterwards.
531 protected void validateAnnotationDimensions(boolean adjustPanelHeight)
533 int annotationHeight = getAnnotationPanel().adjustPanelHeight();
535 if (adjustPanelHeight)
537 int rowHeight = av.getCharHeight();
538 int alignmentHeight = rowHeight * av.getAlignment().getHeight();
541 * Estimate available height in the AlignFrame for alignment +
542 * annotations. Deduct an estimate for title bar, menu bar, scale panel,
543 * hscroll, status bar (as these are not laid out we can't inspect their
544 * actual heights). Insets gives frame borders.
546 int stuff = Platform.isAMac() ? 80 : 100;
547 Insets insets = alignFrame.getInsets();
548 int availableHeight = alignFrame.getHeight() - stuff - insets.top
552 * If not enough vertical space, maximize annotation height while keeping
553 * at least two rows of alignment visible
555 if (annotationHeight + alignmentHeight > availableHeight)
557 annotationHeight = Math.min(annotationHeight, availableHeight - 2
563 // maintain same window layout whilst updating sliders
564 annotationHeight = annotationScroller.getSize().height;
568 annotationScroller.setPreferredSize(new Dimension(annotationScroller
569 .getWidth(), annotationHeight));
571 Dimension e = idPanel.getSize();
572 alabels.setSize(new Dimension(e.width, annotationHeight));
574 annotationSpaceFillerHolder.setPreferredSize(new Dimension(
575 annotationSpaceFillerHolder.getWidth(), annotationHeight));
576 annotationScroller.validate();
577 annotationScroller.addNotify();
581 * update alignment layout for viewport settings
586 public void updateLayout()
589 setAnnotationVisible(av.isShowAnnotation());
590 boolean wrap = av.getWrapAlignment();
592 scalePanelHolder.setVisible(!wrap);
593 hscroll.setVisible(!wrap);
594 idwidthAdjuster.setVisible(!wrap);
598 annotationScroller.setVisible(false);
599 annotationSpaceFillerHolder.setVisible(false);
601 else if (av.isShowAnnotation())
603 annotationScroller.setVisible(true);
604 annotationSpaceFillerHolder.setVisible(true);
607 idSpaceFillerPanel1.setVisible(!wrap);
612 // return value is true if the scroll is valid
613 public boolean scrollUp(boolean up)
617 if (vscroll.getValue() < 1)
623 vscroll.setValue(vscroll.getValue() - 1);
627 if ((vextent + vscroll.getValue()) >= av.getAlignment().getHeight())
633 vscroll.setValue(vscroll.getValue() + 1);
647 * @return DOCUMENT ME!
649 public boolean scrollRight(boolean right)
653 if (hscroll.getValue() < 1)
659 hscroll.setValue(hscroll.getValue() - 1);
663 if ((hextent + hscroll.getValue()) >= av.getAlignment().getWidth())
669 hscroll.setValue(hscroll.getValue() + 1);
678 * Adjust row/column scrollers to show a visible position in the alignment.
681 * visible column to scroll to
683 * visible row to scroll to
686 public void setScrollValues(int x, int y)
688 // System.err.println("Scroll " + this.av.viewName + " to " + x + "," + y);
689 if (av == null || av.getAlignment() == null)
693 int width = av.getAlignment().getWidth();
694 int height = av.getAlignment().getHeight();
696 if (av.hasHiddenColumns())
698 width = av.getColumnSelection().findColumnPosition(width);
701 av.setEndRes((x + (getSeqPanel().seqCanvas.getWidth() / av
702 .getCharWidth())) - 1);
704 hextent = getSeqPanel().seqCanvas.getWidth() / av.getCharWidth();
705 vextent = getSeqPanel().seqCanvas.getHeight() / av.getCharHeight();
712 if (vextent > height)
717 if ((hextent + x) > width)
722 if ((vextent + y) > height)
724 y = height - vextent;
738 * each scroll adjustment triggers adjustmentValueChanged, which resets the
739 * 'do not scroll complement' flag; ensure it is the same for both
742 boolean flag = isDontScrollComplement();
743 hscroll.setValues(x, hextent, 0, width);
744 setDontScrollComplement(flag);
745 vscroll.setValues(y, vextent, 0, height);
755 public void adjustmentValueChanged(AdjustmentEvent evt)
757 int oldX = av.getStartRes();
758 int oldY = av.getStartSeq();
760 if (evt.getSource() == hscroll)
762 int x = hscroll.getValue();
764 av.setEndRes((x + (getSeqPanel().seqCanvas.getWidth() / av
765 .getCharWidth())) - 1);
768 if (evt.getSource() == vscroll)
770 int offy = vscroll.getValue();
772 if (av.getWrapAlignment())
776 int rowSize = getSeqPanel().seqCanvas
777 .getWrappedCanvasWidth(getSeqPanel().seqCanvas.getWidth());
778 av.setStartRes(offy * rowSize);
779 av.setEndRes((offy + 1) * rowSize);
783 // This is only called if file loaded is a jar file that
784 // was wrapped when saved and user has wrap alignment true
785 // as preference setting
786 SwingUtilities.invokeLater(new Runnable()
791 setScrollValues(av.getStartRes(), av.getStartSeq());
798 av.setStartSeq(offy);
800 + (getSeqPanel().seqCanvas.getHeight() / av.getCharHeight()));
804 if (overviewPanel != null)
806 overviewPanel.setBoxPosition();
809 int scrollX = av.startRes - oldX;
810 int scrollY = av.startSeq - oldY;
812 if (av.getWrapAlignment() || !fastPaint)
818 // Make sure we're not trying to draw a panel
819 // larger than the visible window
820 if (scrollX > av.endRes - av.startRes)
822 scrollX = av.endRes - av.startRes;
824 else if (scrollX < av.startRes - av.endRes)
826 scrollX = av.startRes - av.endRes;
829 if (scrollX != 0 || scrollY != 0)
831 getIdPanel().getIdCanvas().fastPaint(scrollY);
832 getSeqPanel().seqCanvas.fastPaint(scrollX, scrollY);
833 getScalePanel().repaint();
835 if (av.isShowAnnotation() && scrollX != 0)
837 getAnnotationPanel().fastPaint(scrollX);
842 * If there is one, scroll the (Protein/cDNA) complementary alignment to
843 * match, unless we are ourselves doing that.
845 if (isDontScrollComplement())
847 setDontScrollComplement(false);
851 av.scrollComplementaryAlignment();
856 * Repaint the alignment including the annotations and overview panels (if
860 public void paintAlignment(boolean updateOverview)
862 final AnnotationSorter sorter = new AnnotationSorter(getAlignment(),
863 av.isShowAutocalculatedAbove());
864 sorter.sort(getAlignment().getAlignmentAnnotation(),
865 av.getSortAnnotationsBy());
870 // TODO: determine if this paintAlignment changed structure colours
871 av.getStructureSelectionManager().sequenceColoursChanged(this);
873 if (overviewPanel != null)
875 overviewPanel.updateOverviewImage();
887 public void paintComponent(Graphics g)
891 Dimension d = getIdPanel().getIdCanvas().getPreferredSize();
892 idPanelHolder.setPreferredSize(d);
893 hscrollFillerPanel.setPreferredSize(new Dimension(d.width, 12));
897 * set scroll bar positions; first suppress this being 'followed' in any
898 * complementary split pane
900 setDontScrollComplement(true);
902 if (av.getWrapAlignment())
904 int maxwidth = av.getAlignment().getWidth();
906 if (av.hasHiddenColumns())
908 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
911 int canvasWidth = getSeqPanel().seqCanvas
912 .getWrappedCanvasWidth(getSeqPanel().seqCanvas.getWidth());
916 / getSeqPanel().seqCanvas
917 .getWrappedCanvasWidth(getSeqPanel().seqCanvas
919 vscroll.setMaximum(max);
920 vscroll.setUnitIncrement(1);
921 vscroll.setVisibleAmount(1);
926 setScrollValues(av.getStartRes(), av.getStartSeq());
940 * @return DOCUMENT ME!
942 * @throws PrinterException
946 public int print(Graphics pg, PageFormat pf, int pi)
947 throws PrinterException
949 pg.translate((int) pf.getImageableX(), (int) pf.getImageableY());
951 int pwidth = (int) pf.getImageableWidth();
952 int pheight = (int) pf.getImageableHeight();
954 if (av.getWrapAlignment())
956 return printWrappedAlignment(pg, pwidth, pheight, pi);
960 return printUnwrapped(pg, pwidth, pheight, pi);
976 * @return DOCUMENT ME!
978 * @throws PrinterException
981 public int printUnwrapped(Graphics pg, int pwidth, int pheight, int pi)
982 throws PrinterException
984 int idWidth = getVisibleIdWidth(false);
985 FontMetrics fm = getFontMetrics(av.getFont());
986 int scaleHeight = av.getCharHeight() + fm.getDescent();
988 pg.setColor(Color.white);
989 pg.fillRect(0, 0, pwidth, pheight);
990 pg.setFont(av.getFont());
992 // //////////////////////////////////
993 // / How many sequences and residues can we fit on a printable page?
994 int totalRes = (pwidth - idWidth) / av.getCharWidth();
996 int totalSeq = (pheight - scaleHeight) / av.getCharHeight() - 1;
998 int pagesWide = (av.getAlignment().getWidth() / totalRes) + 1;
1000 // ///////////////////////////
1001 // / Only print these sequences and residues on this page
1004 // ///////////////////////////
1005 // / Only print these sequences and residues on this page
1008 // ///////////////////////////
1009 // / Only print these sequences and residues on this page
1012 // ///////////////////////////
1013 // / Only print these sequences and residues on this page
1015 startRes = (pi % pagesWide) * totalRes;
1016 endRes = (startRes + totalRes) - 1;
1018 if (endRes > (av.getAlignment().getWidth() - 1))
1020 endRes = av.getAlignment().getWidth() - 1;
1023 startSeq = (pi / pagesWide) * totalSeq;
1024 endSeq = startSeq + totalSeq;
1026 if (endSeq > av.getAlignment().getHeight())
1028 endSeq = av.getAlignment().getHeight();
1031 int pagesHigh = ((av.getAlignment().getHeight() / totalSeq) + 1)
1034 if (av.isShowAnnotation())
1036 pagesHigh += getAnnotationPanel().adjustPanelHeight() + 3;
1039 pagesHigh /= pheight;
1041 if (pi >= (pagesWide * pagesHigh))
1043 return Printable.NO_SUCH_PAGE;
1047 pg.translate(idWidth, 0);
1048 getScalePanel().drawScale(pg, startRes, endRes, pwidth - idWidth,
1050 pg.translate(-idWidth, scaleHeight);
1054 Color currentColor = null;
1055 Color currentTextColor = null;
1057 pg.setFont(getIdPanel().getIdCanvas().getIdfont());
1060 for (int i = startSeq; i < endSeq; i++)
1062 seq = av.getAlignment().getSequenceAt(i);
1063 if ((av.getSelectionGroup() != null)
1064 && av.getSelectionGroup().getSequences(null).contains(seq))
1066 currentColor = Color.gray;
1067 currentTextColor = Color.black;
1071 currentColor = av.getSequenceColour(seq);
1072 currentTextColor = Color.black;
1075 pg.setColor(currentColor);
1076 pg.fillRect(0, (i - startSeq) * av.getCharHeight(), idWidth,
1077 av.getCharHeight());
1079 pg.setColor(currentTextColor);
1082 if (av.isRightAlignIds())
1084 fm = pg.getFontMetrics();
1086 - fm.stringWidth(seq.getDisplayId(av.getShowJVSuffix()))
1090 pg.drawString(seq.getDisplayId(av.getShowJVSuffix()), xPos,
1091 (((i - startSeq) * av.getCharHeight()) + av.getCharHeight())
1092 - (av.getCharHeight() / 5));
1095 pg.setFont(av.getFont());
1097 // draw main sequence panel
1098 pg.translate(idWidth, 0);
1099 getSeqPanel().seqCanvas.drawPanel(pg, startRes, endRes, startSeq,
1102 if (av.isShowAnnotation() && (endSeq == av.getAlignment().getHeight()))
1104 // draw annotation - need to offset for current scroll position
1105 int offset = -getAlabels().getScrollOffset();
1106 pg.translate(0, offset);
1107 pg.translate(-idWidth - 3, (endSeq - startSeq) * av.getCharHeight()
1109 getAlabels().drawComponent(pg, idWidth);
1110 pg.translate(idWidth + 3, 0);
1111 getAnnotationPanel().renderer.drawComponent(getAnnotationPanel(), av,
1112 pg, -1, startRes, endRes + 1);
1113 pg.translate(0, -offset);
1116 return Printable.PAGE_EXISTS;
1131 * @return DOCUMENT ME!
1133 * @throws PrinterException
1136 public int printWrappedAlignment(Graphics pg, int pwidth, int pheight,
1137 int pi) throws PrinterException
1139 int annotationHeight = 0;
1140 AnnotationLabels labels = null;
1141 if (av.isShowAnnotation())
1143 annotationHeight = getAnnotationPanel().adjustPanelHeight();
1144 labels = new AnnotationLabels(av);
1147 int hgap = av.getCharHeight();
1148 if (av.getScaleAboveWrapped())
1150 hgap += av.getCharHeight();
1153 int cHeight = av.getAlignment().getHeight() * av.getCharHeight() + hgap
1156 int idWidth = getVisibleIdWidth(false);
1158 int maxwidth = av.getAlignment().getWidth();
1159 if (av.hasHiddenColumns())
1161 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
1164 int resWidth = getSeqPanel().seqCanvas.getWrappedCanvasWidth(pwidth
1167 int totalHeight = cHeight * (maxwidth / resWidth + 1);
1169 pg.setColor(Color.white);
1170 pg.fillRect(0, 0, pwidth, pheight);
1171 pg.setFont(av.getFont());
1175 pg.setColor(Color.black);
1177 pg.translate(0, -pi * pheight);
1179 pg.setClip(0, pi * pheight, pwidth, pheight);
1185 for (int i = 0; i < av.getAlignment().getHeight(); i++)
1187 pg.setFont(getIdPanel().getIdCanvas().getIdfont());
1188 SequenceI s = av.getAlignment().getSequenceAt(i);
1189 String string = s.getDisplayId(av.getShowJVSuffix());
1191 if (av.isRightAlignIds())
1193 FontMetrics fm = pg.getFontMetrics();
1194 xPos = idWidth - fm.stringWidth(string) - 4;
1196 pg.drawString(string, xPos,
1197 ((i * av.getCharHeight()) + ypos + av.getCharHeight())
1198 - (av.getCharHeight() / 5));
1203 ypos + (av.getAlignment().getHeight() * av.getCharHeight()));
1205 pg.setFont(av.getFont());
1206 labels.drawComponent(pg, idWidth);
1210 - (av.getAlignment().getHeight() * av
1215 } while (ypos < totalHeight);
1217 pg.translate(idWidth, 0);
1219 getSeqPanel().seqCanvas.drawWrappedPanel(pg, pwidth - idWidth,
1222 if ((pi * pheight) < totalHeight)
1224 return Printable.PAGE_EXISTS;
1229 return Printable.NO_SUCH_PAGE;
1234 * get current sequence ID panel width, or nominal value if panel were to be
1235 * displayed using default settings
1239 public int getVisibleIdWidth()
1241 return getVisibleIdWidth(true);
1245 * get current sequence ID panel width, or nominal value if panel were to be
1246 * displayed using default settings
1249 * indicate if the Id width for onscreen or offscreen display should
1253 public int getVisibleIdWidth(boolean onscreen)
1255 // see if rendering offscreen - check preferences and calc width accordingly
1256 if (!onscreen && Cache.getDefault("FIGURE_AUTOIDWIDTH", false))
1258 return calculateIdWidth(-1).width + 4;
1260 Integer idwidth = null;
1262 || (idwidth = Cache.getIntegerProperty("FIGURE_FIXEDIDWIDTH")) == null)
1264 return (getIdPanel().getWidth() > 0 ? getIdPanel().getWidth()
1265 : calculateIdWidth().width + 4);
1267 return idwidth.intValue() + 4;
1270 void makeAlignmentImage(jalview.util.ImageMaker.TYPE type, File file)
1272 long pSessionId = System.currentTimeMillis();
1273 headless = (System.getProperty("java.awt.headless") != null && System
1274 .getProperty("java.awt.headless").equals("true"));
1275 if (alignFrame != null && !headless)
1279 alignFrame.setProgressBar(MessageManager.formatMessage(
1280 "status.saving_file", new Object[] { type.getLabel() }),
1286 AlignmentDimension aDimension = getAlignmentDimension();
1289 jalview.util.ImageMaker im;
1290 final String imageAction, imageTitle;
1291 if (type == jalview.util.ImageMaker.TYPE.PNG)
1293 imageAction = "Create PNG image from alignment";
1296 else if (type == jalview.util.ImageMaker.TYPE.EPS)
1298 imageAction = "Create EPS file from alignment";
1299 imageTitle = alignFrame.getTitle();
1303 imageAction = "Create SVG file from alignment";
1304 imageTitle = alignFrame.getTitle();
1307 im = new jalview.util.ImageMaker(this, type, imageAction,
1308 aDimension.getWidth(), aDimension.getHeight(), file,
1309 imageTitle, alignFrame, pSessionId, headless);
1310 if (av.getWrapAlignment())
1312 if (im.getGraphics() != null)
1314 printWrappedAlignment(im.getGraphics(), aDimension.getWidth(),
1315 aDimension.getHeight(), 0);
1321 if (im.getGraphics() != null)
1323 printUnwrapped(im.getGraphics(), aDimension.getWidth(),
1324 aDimension.getHeight(), 0);
1329 } catch (OutOfMemoryError err)
1332 System.out.println("########################\n" + "OUT OF MEMORY "
1333 + file + "\n" + "########################");
1334 new OOMWarning("Creating Image for " + file, err);
1335 // System.out.println("Create IMAGE: " + err);
1336 } catch (Exception ex)
1338 ex.printStackTrace();
1346 public AlignmentDimension getAlignmentDimension()
1348 int maxwidth = av.getAlignment().getWidth();
1349 if (av.hasHiddenColumns())
1351 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth);
1354 int height = ((av.getAlignment().getHeight() + 1) * av.getCharHeight())
1355 + getScalePanel().getHeight();
1356 int width = getVisibleIdWidth(false) + (maxwidth * av.getCharWidth());
1358 if (av.getWrapAlignment())
1360 height = getWrappedHeight();
1363 // need to obtain default alignment width and then add in any
1364 // additional allowance for id margin
1365 // this duplicates the calculation in getWrappedHeight but adjusts for
1367 width = alignFrame.getWidth() - vscroll.getPreferredSize().width
1368 - alignFrame.getInsets().left
1369 - alignFrame.getInsets().right - getVisibleIdWidth()
1370 + getVisibleIdWidth(false);
1374 width = getSeqPanel().getWidth() + getVisibleIdWidth(false);
1378 else if (av.isShowAnnotation())
1380 height += getAnnotationPanel().adjustPanelHeight() + 3;
1382 return new AlignmentDimension(width, height);
1389 public void makeEPS(File epsFile)
1391 makeAlignmentImage(jalview.util.ImageMaker.TYPE.EPS, epsFile);
1397 public void makePNG(File pngFile)
1399 makeAlignmentImage(jalview.util.ImageMaker.TYPE.PNG, pngFile);
1402 public void makeSVG(File svgFile)
1404 makeAlignmentImage(jalview.util.ImageMaker.TYPE.SVG, svgFile);
1407 public void makePNGImageMap(File imgMapFile, String imageName)
1409 // /////ONLY WORKS WITH NONE WRAPPED ALIGNMENTS
1410 // ////////////////////////////////////////////
1411 int idWidth = getVisibleIdWidth(false);
1412 FontMetrics fm = getFontMetrics(av.getFont());
1413 int scaleHeight = av.getCharHeight() + fm.getDescent();
1416 // ////////////////////////////////
1417 if (imgMapFile != null)
1421 int s, sSize = av.getAlignment().getHeight(), res, alwidth = av
1422 .getAlignment().getWidth(), g, gSize, f, fSize, sy;
1423 StringBuffer text = new StringBuffer();
1424 PrintWriter out = new PrintWriter(new FileWriter(imgMapFile));
1425 out.println(jalview.io.HTMLOutput.getImageMapHTML());
1426 out.println("<img src=\"" + imageName
1427 + "\" border=\"0\" usemap=\"#Map\" >"
1428 + "<map name=\"Map\">");
1430 for (s = 0; s < sSize; s++)
1432 sy = s * av.getCharHeight() + scaleHeight;
1434 SequenceI seq = av.getAlignment().getSequenceAt(s);
1435 SequenceFeature[] features = seq.getSequenceFeatures();
1436 SequenceGroup[] groups = av.getAlignment().findAllGroups(seq);
1437 for (res = 0; res < alwidth; res++)
1439 text = new StringBuffer();
1440 String triplet = null;
1441 if (av.getAlignment().isNucleotide())
1443 triplet = ResidueProperties.nucleotideName.get(seq
1444 .getCharAt(res) + "");
1448 triplet = ResidueProperties.aa2Triplet.get(seq.getCharAt(res)
1452 if (triplet == null)
1457 int alIndex = seq.findPosition(res);
1458 gSize = groups.length;
1459 for (g = 0; g < gSize; g++)
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 + " "
1471 if (groups[g].getStartRes() < res
1472 && groups[g].getEndRes() > res)
1474 text.append("<br><em>" + groups[g].getName() + "</em>");
1478 if (features != null)
1480 if (text.length() < 1)
1482 text.append("<area shape=\"rect\" coords=\""
1483 + (idWidth + res * av.getCharWidth()) + "," + sy
1484 + "," + (idWidth + (res + 1) * av.getCharWidth())
1485 + "," + (av.getCharHeight() + sy) + "\""
1486 + " onMouseOver=\"toolTip('" + alIndex + " "
1489 fSize = features.length;
1490 for (f = 0; f < fSize; f++)
1493 if ((features[f].getBegin() <= seq.findPosition(res))
1494 && (features[f].getEnd() >= seq.findPosition(res)))
1496 if (features[f].getType().equals("disulfide bond"))
1498 if (features[f].getBegin() == seq.findPosition(res)
1499 || features[f].getEnd() == seq
1502 text.append("<br>disulfide bond "
1503 + features[f].getBegin() + ":"
1504 + features[f].getEnd());
1509 text.append("<br>");
1510 text.append(features[f].getType());
1511 if (features[f].getDescription() != null
1512 && !features[f].getType().equals(
1513 features[f].getDescription()))
1515 text.append(" " + features[f].getDescription());
1518 if (features[f].getValue("status") != null)
1520 text.append(" (" + features[f].getValue("status")
1528 if (text.length() > 1)
1530 text.append("')\"; onMouseOut=\"toolTip()\"; href=\"#\">");
1531 out.println(text.toString());
1535 out.println("</map></body></html>");
1538 } catch (Exception ex)
1540 ex.printStackTrace();
1542 } // /////////END OF IMAGE MAP
1546 int getWrappedHeight()
1548 int seqPanelWidth = getSeqPanel().seqCanvas.getWidth();
1550 if (System.getProperty("java.awt.headless") != null
1551 && System.getProperty("java.awt.headless").equals("true"))
1553 seqPanelWidth = alignFrame.getWidth() - getVisibleIdWidth()
1554 - vscroll.getPreferredSize().width
1555 - alignFrame.getInsets().left - alignFrame.getInsets().right;
1558 int chunkWidth = getSeqPanel().seqCanvas
1559 .getWrappedCanvasWidth(seqPanelWidth);
1561 int hgap = av.getCharHeight();
1562 if (av.getScaleAboveWrapped())
1564 hgap += av.getCharHeight();
1567 int annotationHeight = 0;
1568 if (av.isShowAnnotation())
1570 annotationHeight = getAnnotationPanel().adjustPanelHeight();
1573 int cHeight = av.getAlignment().getHeight() * av.getCharHeight() + hgap
1576 int maxwidth = av.getAlignment().getWidth();
1577 if (av.hasHiddenColumns())
1579 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
1582 int height = ((maxwidth / chunkWidth) + 1) * cHeight;
1588 * close the panel - deregisters all listeners and nulls any references to
1591 public void closePanel()
1593 PaintRefresher.RemoveComponent(getSeqPanel().seqCanvas);
1594 PaintRefresher.RemoveComponent(getIdPanel().getIdCanvas());
1595 PaintRefresher.RemoveComponent(this);
1598 jalview.structure.StructureSelectionManager ssm = av
1599 .getStructureSelectionManager();
1600 ssm.removeStructureViewerListener(getSeqPanel(), null);
1601 ssm.removeSelectionListener(getSeqPanel());
1602 ssm.removeCommandListener(av);
1603 ssm.removeStructureViewerListener(getSeqPanel(), null);
1604 ssm.removeSelectionListener(getSeqPanel());
1605 av.setAlignment(null);
1610 if (Cache.log.isDebugEnabled())
1612 Cache.log.warn("Closing alignment panel which is already closed.");
1618 * hides or shows dynamic annotation rows based on groups and av state flags
1620 public void updateAnnotation()
1622 updateAnnotation(false, false);
1625 public void updateAnnotation(boolean applyGlobalSettings)
1627 updateAnnotation(applyGlobalSettings, false);
1630 public void updateAnnotation(boolean applyGlobalSettings,
1631 boolean preserveNewGroupSettings)
1633 av.updateGroupAnnotationSettings(applyGlobalSettings,
1634 preserveNewGroupSettings);
1635 adjustAnnotationHeight();
1639 public AlignmentI getAlignment()
1641 return av == null ? null : av.getAlignment();
1645 public String getViewName()
1651 * Make/Unmake this alignment panel the current input focus
1655 public void setSelected(boolean b)
1659 if (alignFrame.getSplitViewContainer() != null)
1662 * bring enclosing SplitFrame to front first if there is one
1664 ((SplitFrame) alignFrame.getSplitViewContainer()).setSelected(b);
1666 alignFrame.setSelected(b);
1667 } catch (Exception ex)
1673 alignFrame.setDisplayedView(this);
1678 public StructureSelectionManager getStructureSelectionManager()
1680 return av.getStructureSelectionManager();
1684 public void raiseOOMWarning(String string, OutOfMemoryError error)
1686 new OOMWarning(string, error, this);
1690 public jalview.api.FeatureRenderer cloneFeatureRenderer()
1693 return new FeatureRenderer(this);
1697 public jalview.api.FeatureRenderer getFeatureRenderer()
1699 return seqPanel.seqCanvas.getFeatureRenderer();
1702 public void updateFeatureRenderer(
1703 jalview.renderer.seqfeatures.FeatureRenderer fr)
1705 fr.transferSettings(getSeqPanel().seqCanvas.getFeatureRenderer());
1708 public void updateFeatureRendererFrom(jalview.api.FeatureRenderer fr)
1710 if (getSeqPanel().seqCanvas.getFeatureRenderer() != null)
1712 getSeqPanel().seqCanvas.getFeatureRenderer().transferSettings(fr);
1716 public ScalePanel getScalePanel()
1721 public void setScalePanel(ScalePanel scalePanel)
1723 this.scalePanel = scalePanel;
1726 public SeqPanel getSeqPanel()
1731 public void setSeqPanel(SeqPanel seqPanel)
1733 this.seqPanel = seqPanel;
1736 public AnnotationPanel getAnnotationPanel()
1738 return annotationPanel;
1741 public void setAnnotationPanel(AnnotationPanel annotationPanel)
1743 this.annotationPanel = annotationPanel;
1746 public AnnotationLabels getAlabels()
1751 public void setAlabels(AnnotationLabels alabels)
1753 this.alabels = alabels;
1756 public IdPanel getIdPanel()
1761 public void setIdPanel(IdPanel idPanel)
1763 this.idPanel = idPanel;
1767 * Follow a scrolling change in the (cDNA/Protein) complementary alignment.
1768 * The aim is to keep the two alignments 'lined up' on their centre columns.
1771 * holds mapped region(s) of this alignment that we are scrolling
1772 * 'to'; may be modified for sequence offset by this method
1773 * @param verticalOffset
1774 * the number of visible sequences to show above the mapped region
1776 public void scrollToCentre(SearchResults sr, int verticalOffset)
1779 * To avoid jumpy vertical scrolling (if some sequences are gapped or not
1780 * mapped), we can make the scroll-to location a sequence above the one
1783 SequenceI mappedTo = sr.getResultSequence(0);
1784 List<SequenceI> seqs = av.getAlignment().getSequences();
1787 * This is like AlignmentI.findIndex(seq) but here we are matching the
1788 * dataset sequence not the aligned sequence
1790 boolean matched = false;
1791 for (SequenceI seq : seqs)
1793 if (mappedTo == seq.getDatasetSequence())
1801 return; // failsafe, shouldn't happen
1805 * Scroll to position but centring the target residue.
1807 scrollToPosition(sr, verticalOffset, true, true);
1811 * Set a flag to say do not scroll any (cDNA/protein) complement.
1815 protected void setDontScrollComplement(boolean b)
1817 this.dontScrollComplement = b;
1820 protected boolean isDontScrollComplement()
1822 return this.dontScrollComplement;