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()
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;
736 * each scroll adjustment triggers adjustmentValueChanged, which resets the
737 * 'do not scroll complement' flag; ensure it is the same for both
740 boolean flag = isDontScrollComplement();
741 hscroll.setValues(x, hextent, 0, width);
742 setDontScrollComplement(flag);
743 vscroll.setValues(y, vextent, 0, height);
752 public void adjustmentValueChanged(AdjustmentEvent evt)
754 int oldX = av.getStartRes();
755 int oldY = av.getStartSeq();
757 if (evt.getSource() == hscroll)
759 int x = hscroll.getValue();
761 av.setEndRes((x + (getSeqPanel().seqCanvas.getWidth() / av
762 .getCharWidth())) - 1);
765 if (evt.getSource() == vscroll)
767 int offy = vscroll.getValue();
769 if (av.getWrapAlignment())
773 int rowSize = getSeqPanel().seqCanvas
774 .getWrappedCanvasWidth(getSeqPanel().seqCanvas.getWidth());
775 av.setStartRes(offy * rowSize);
776 av.setEndRes((offy + 1) * rowSize);
780 // This is only called if file loaded is a jar file that
781 // was wrapped when saved and user has wrap alignment true
782 // as preference setting
783 SwingUtilities.invokeLater(new Runnable()
787 setScrollValues(av.getStartRes(), av.getStartSeq());
794 av.setStartSeq(offy);
796 + (getSeqPanel().seqCanvas.getHeight() / av.getCharHeight()));
800 if (overviewPanel != null)
802 overviewPanel.setBoxPosition();
805 int scrollX = av.startRes - oldX;
806 int scrollY = av.startSeq - oldY;
808 if (av.getWrapAlignment() || !fastPaint)
814 // Make sure we're not trying to draw a panel
815 // larger than the visible window
816 if (scrollX > av.endRes - av.startRes)
818 scrollX = av.endRes - av.startRes;
820 else if (scrollX < av.startRes - av.endRes)
822 scrollX = av.startRes - av.endRes;
825 if (scrollX != 0 || scrollY != 0)
827 getIdPanel().getIdCanvas().fastPaint(scrollY);
828 getSeqPanel().seqCanvas.fastPaint(scrollX, scrollY);
829 getScalePanel().repaint();
831 if (av.isShowAnnotation() && scrollX != 0)
833 getAnnotationPanel().fastPaint(scrollX);
838 * If there is one, scroll the (Protein/cDNA) complementary alignment to
839 * match, unless we are ourselves doing that.
841 if (isDontScrollComplement())
843 setDontScrollComplement(false);
847 av.scrollComplementaryAlignment();
852 * Repaint the alignment including the annotations and overview panels (if
855 public void paintAlignment(boolean updateOverview)
857 final AnnotationSorter sorter = new AnnotationSorter(getAlignment(),
858 av.isShowAutocalculatedAbove());
859 sorter.sort(getAlignment().getAlignmentAnnotation(),
860 av.getSortAnnotationsBy());
865 // TODO: determine if this paintAlignment changed structure colours
866 av.getStructureSelectionManager().sequenceColoursChanged(this);
868 if (overviewPanel != null)
870 overviewPanel.updateOverviewImage();
881 public void paintComponent(Graphics g)
885 Dimension d = getIdPanel().getIdCanvas().getPreferredSize();
886 idPanelHolder.setPreferredSize(d);
887 hscrollFillerPanel.setPreferredSize(new Dimension(d.width, 12));
891 * set scroll bar positions; first suppress this being 'followed' in any
892 * complementary split pane
894 setDontScrollComplement(true);
896 if (av.getWrapAlignment())
898 int maxwidth = av.getAlignment().getWidth();
900 if (av.hasHiddenColumns())
902 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
905 int canvasWidth = getSeqPanel().seqCanvas
906 .getWrappedCanvasWidth(getSeqPanel().seqCanvas.getWidth());
910 / getSeqPanel().seqCanvas
911 .getWrappedCanvasWidth(getSeqPanel().seqCanvas
913 vscroll.setMaximum(max);
914 vscroll.setUnitIncrement(1);
915 vscroll.setVisibleAmount(1);
920 setScrollValues(av.getStartRes(), av.getStartSeq());
934 * @return DOCUMENT ME!
936 * @throws PrinterException
939 public int print(Graphics pg, PageFormat pf, int pi)
940 throws PrinterException
942 pg.translate((int) pf.getImageableX(), (int) pf.getImageableY());
944 int pwidth = (int) pf.getImageableWidth();
945 int pheight = (int) pf.getImageableHeight();
947 if (av.getWrapAlignment())
949 return printWrappedAlignment(pg, pwidth, pheight, pi);
953 return printUnwrapped(pg, pwidth, pheight, pi);
969 * @return DOCUMENT ME!
971 * @throws PrinterException
974 public int printUnwrapped(Graphics pg, int pwidth, int pheight, int pi)
975 throws PrinterException
977 int idWidth = getVisibleIdWidth(false);
978 FontMetrics fm = getFontMetrics(av.getFont());
979 int scaleHeight = av.getCharHeight() + fm.getDescent();
981 pg.setColor(Color.white);
982 pg.fillRect(0, 0, pwidth, pheight);
983 pg.setFont(av.getFont());
985 // //////////////////////////////////
986 // / How many sequences and residues can we fit on a printable page?
987 int totalRes = (pwidth - idWidth) / av.getCharWidth();
989 int totalSeq = (pheight - scaleHeight) / av.getCharHeight() - 1;
991 int pagesWide = (av.getAlignment().getWidth() / totalRes) + 1;
993 // ///////////////////////////
994 // / Only print these sequences and residues on this page
997 // ///////////////////////////
998 // / Only print these sequences and residues on this page
1001 // ///////////////////////////
1002 // / Only print these sequences and residues on this page
1005 // ///////////////////////////
1006 // / Only print these sequences and residues on this page
1008 startRes = (pi % pagesWide) * totalRes;
1009 endRes = (startRes + totalRes) - 1;
1011 if (endRes > (av.getAlignment().getWidth() - 1))
1013 endRes = av.getAlignment().getWidth() - 1;
1016 startSeq = (pi / pagesWide) * totalSeq;
1017 endSeq = startSeq + totalSeq;
1019 if (endSeq > av.getAlignment().getHeight())
1021 endSeq = av.getAlignment().getHeight();
1024 int pagesHigh = ((av.getAlignment().getHeight() / totalSeq) + 1)
1027 if (av.isShowAnnotation())
1029 pagesHigh += getAnnotationPanel().adjustPanelHeight() + 3;
1032 pagesHigh /= pheight;
1034 if (pi >= (pagesWide * pagesHigh))
1036 return Printable.NO_SUCH_PAGE;
1040 pg.translate(idWidth, 0);
1041 getScalePanel().drawScale(pg, startRes, endRes, pwidth - idWidth,
1043 pg.translate(-idWidth, scaleHeight);
1047 Color currentColor = null;
1048 Color currentTextColor = null;
1050 pg.setFont(getIdPanel().getIdCanvas().getIdfont());
1053 for (int i = startSeq; i < endSeq; i++)
1055 seq = av.getAlignment().getSequenceAt(i);
1056 if ((av.getSelectionGroup() != null)
1057 && av.getSelectionGroup().getSequences(null).contains(seq))
1059 currentColor = Color.gray;
1060 currentTextColor = Color.black;
1064 currentColor = av.getSequenceColour(seq);
1065 currentTextColor = Color.black;
1068 pg.setColor(currentColor);
1069 pg.fillRect(0, (i - startSeq) * av.getCharHeight(), idWidth,
1070 av.getCharHeight());
1072 pg.setColor(currentTextColor);
1075 if (av.isRightAlignIds())
1077 fm = pg.getFontMetrics();
1079 - fm.stringWidth(seq.getDisplayId(av.getShowJVSuffix()))
1083 pg.drawString(seq.getDisplayId(av.getShowJVSuffix()), xPos,
1084 (((i - startSeq) * av.getCharHeight()) + av.getCharHeight())
1085 - (av.getCharHeight() / 5));
1088 pg.setFont(av.getFont());
1090 // draw main sequence panel
1091 pg.translate(idWidth, 0);
1092 getSeqPanel().seqCanvas.drawPanel(pg, startRes, endRes, startSeq,
1095 if (av.isShowAnnotation() && (endSeq == av.getAlignment().getHeight()))
1097 // draw annotation - need to offset for current scroll position
1098 int offset = -getAlabels().getScrollOffset();
1099 pg.translate(0, offset);
1100 pg.translate(-idWidth - 3, (endSeq - startSeq) * av.getCharHeight()
1102 getAlabels().drawComponent(pg, idWidth);
1103 pg.translate(idWidth + 3, 0);
1104 getAnnotationPanel().renderer.drawComponent(getAnnotationPanel(), av,
1105 pg, -1, startRes, endRes + 1);
1106 pg.translate(0, -offset);
1109 return Printable.PAGE_EXISTS;
1124 * @return DOCUMENT ME!
1126 * @throws PrinterException
1129 public int printWrappedAlignment(Graphics pg, int pwidth, int pheight,
1130 int pi) throws PrinterException
1132 int annotationHeight = 0;
1133 AnnotationLabels labels = null;
1134 if (av.isShowAnnotation())
1136 annotationHeight = getAnnotationPanel().adjustPanelHeight();
1137 labels = new AnnotationLabels(av);
1140 int hgap = av.getCharHeight();
1141 if (av.getScaleAboveWrapped())
1143 hgap += av.getCharHeight();
1146 int cHeight = av.getAlignment().getHeight() * av.getCharHeight() + hgap
1149 int idWidth = getVisibleIdWidth(false);
1151 int maxwidth = av.getAlignment().getWidth();
1152 if (av.hasHiddenColumns())
1154 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
1157 int resWidth = getSeqPanel().seqCanvas.getWrappedCanvasWidth(pwidth
1160 int totalHeight = cHeight * (maxwidth / resWidth + 1);
1162 pg.setColor(Color.white);
1163 pg.fillRect(0, 0, pwidth, pheight);
1164 pg.setFont(av.getFont());
1168 pg.setColor(Color.black);
1170 pg.translate(0, -pi * pheight);
1172 pg.setClip(0, pi * pheight, pwidth, pheight);
1178 for (int i = 0; i < av.getAlignment().getHeight(); i++)
1180 pg.setFont(getIdPanel().getIdCanvas().getIdfont());
1181 SequenceI s = av.getAlignment().getSequenceAt(i);
1182 String string = s.getDisplayId(av.getShowJVSuffix());
1184 if (av.isRightAlignIds())
1186 FontMetrics fm = pg.getFontMetrics();
1187 xPos = idWidth - fm.stringWidth(string) - 4;
1189 pg.drawString(string, xPos,
1190 ((i * av.getCharHeight()) + ypos + av.getCharHeight())
1191 - (av.getCharHeight() / 5));
1196 ypos + (av.getAlignment().getHeight() * av.getCharHeight()));
1198 pg.setFont(av.getFont());
1199 labels.drawComponent(pg, idWidth);
1203 - (av.getAlignment().getHeight() * av
1208 } while (ypos < totalHeight);
1210 pg.translate(idWidth, 0);
1212 getSeqPanel().seqCanvas.drawWrappedPanel(pg, pwidth - idWidth,
1215 if ((pi * pheight) < totalHeight)
1217 return Printable.PAGE_EXISTS;
1222 return Printable.NO_SUCH_PAGE;
1227 * get current sequence ID panel width, or nominal value if panel were to be
1228 * displayed using default settings
1232 public int getVisibleIdWidth()
1234 return getVisibleIdWidth(true);
1238 * get current sequence ID panel width, or nominal value if panel were to be
1239 * displayed using default settings
1242 * indicate if the Id width for onscreen or offscreen display should
1246 public int getVisibleIdWidth(boolean onscreen)
1248 // see if rendering offscreen - check preferences and calc width accordingly
1249 if (!onscreen && Cache.getDefault("FIGURE_AUTOIDWIDTH", false))
1251 return calculateIdWidth(-1).width + 4;
1253 Integer idwidth = null;
1255 || (idwidth = Cache.getIntegerProperty("FIGURE_FIXEDIDWIDTH")) == null)
1257 return (getIdPanel().getWidth() > 0 ? getIdPanel().getWidth()
1258 : calculateIdWidth().width + 4);
1260 return idwidth.intValue() + 4;
1263 void makeAlignmentImage(jalview.util.ImageMaker.TYPE type, File file)
1265 long progress = System.currentTimeMillis();
1266 headless = (System.getProperty("java.awt.headless") != null && System
1267 .getProperty("java.awt.headless").equals("true"));
1268 if (alignFrame != null && !headless)
1270 alignFrame.setProgressBar(MessageManager.formatMessage(
1271 "status.saving_file", new Object[] { type.getLabel() }),
1276 AlignmentDimension aDimension = getAlignmentDimension();
1279 jalview.util.ImageMaker im;
1280 final String imageAction, imageTitle;
1281 if (type == jalview.util.ImageMaker.TYPE.PNG)
1283 imageAction = "Create PNG image from alignment";
1286 else if (type == jalview.util.ImageMaker.TYPE.EPS)
1288 imageAction = "Create EPS file from alignment";
1289 imageTitle = alignFrame.getTitle();
1293 imageAction = "Create SVG file from alignment";
1294 imageTitle = alignFrame.getTitle();
1297 im = new jalview.util.ImageMaker(this, type, imageAction,
1298 aDimension.getWidth(), aDimension.getHeight(), file,
1300 if (av.getWrapAlignment())
1302 if (im.getGraphics() != null)
1304 printWrappedAlignment(im.getGraphics(), aDimension.getWidth(),
1305 aDimension.getHeight(), 0);
1311 if (im.getGraphics() != null)
1313 printUnwrapped(im.getGraphics(), aDimension.getWidth(),
1314 aDimension.getHeight(), 0);
1318 } catch (OutOfMemoryError err)
1321 System.out.println("########################\n" + "OUT OF MEMORY "
1322 + file + "\n" + "########################");
1323 new OOMWarning("Creating Image for " + file, err);
1324 // System.out.println("Create IMAGE: " + err);
1325 } catch (Exception ex)
1327 ex.printStackTrace();
1331 if (alignFrame != null && !headless)
1333 alignFrame.setProgressBar(
1334 MessageManager.getString("status.export_complete"),
1340 public AlignmentDimension getAlignmentDimension()
1342 int maxwidth = av.getAlignment().getWidth();
1343 if (av.hasHiddenColumns())
1345 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth);
1348 int height = ((av.getAlignment().getHeight() + 1) * av.getCharHeight())
1349 + getScalePanel().getHeight();
1350 int width = getVisibleIdWidth(false) + (maxwidth * av.getCharWidth());
1352 if (av.getWrapAlignment())
1354 height = getWrappedHeight();
1357 // need to obtain default alignment width and then add in any
1358 // additional allowance for id margin
1359 // this duplicates the calculation in getWrappedHeight but adjusts for
1361 width = alignFrame.getWidth() - vscroll.getPreferredSize().width
1362 - alignFrame.getInsets().left
1363 - alignFrame.getInsets().right - getVisibleIdWidth()
1364 + getVisibleIdWidth(false);
1368 width = getSeqPanel().getWidth() + getVisibleIdWidth(false);
1372 else if (av.isShowAnnotation())
1374 height += getAnnotationPanel().adjustPanelHeight() + 3;
1376 return new AlignmentDimension(width, height);
1383 public void makeEPS(File epsFile)
1385 makeAlignmentImage(jalview.util.ImageMaker.TYPE.EPS, epsFile);
1391 public void makePNG(File pngFile)
1393 makeAlignmentImage(jalview.util.ImageMaker.TYPE.PNG, pngFile);
1396 public void makeSVG(File svgFile)
1398 makeAlignmentImage(jalview.util.ImageMaker.TYPE.SVG, svgFile);
1401 public void makePNGImageMap(File imgMapFile, String imageName)
1403 // /////ONLY WORKS WITH NONE WRAPPED ALIGNMENTS
1404 // ////////////////////////////////////////////
1405 int idWidth = getVisibleIdWidth(false);
1406 FontMetrics fm = getFontMetrics(av.getFont());
1407 int scaleHeight = av.getCharHeight() + fm.getDescent();
1410 // ////////////////////////////////
1411 if (imgMapFile != null)
1415 int s, sSize = av.getAlignment().getHeight(), res, alwidth = av
1416 .getAlignment().getWidth(), g, gSize, f, fSize, sy;
1417 StringBuffer text = new StringBuffer();
1418 PrintWriter out = new PrintWriter(new FileWriter(imgMapFile));
1419 out.println(jalview.io.HTMLOutput.getImageMapHTML());
1420 out.println("<img src=\"" + imageName
1421 + "\" border=\"0\" usemap=\"#Map\" >"
1422 + "<map name=\"Map\">");
1424 for (s = 0; s < sSize; s++)
1426 sy = s * av.getCharHeight() + scaleHeight;
1428 SequenceI seq = av.getAlignment().getSequenceAt(s);
1429 SequenceFeature[] features = seq.getSequenceFeatures();
1430 SequenceGroup[] groups = av.getAlignment().findAllGroups(seq);
1431 for (res = 0; res < alwidth; res++)
1433 text = new StringBuffer();
1434 String triplet = null;
1435 if (av.getAlignment().isNucleotide())
1437 triplet = ResidueProperties.nucleotideName.get(seq
1438 .getCharAt(res) + "");
1442 triplet = ResidueProperties.aa2Triplet.get(seq.getCharAt(res)
1446 if (triplet == null)
1451 int alIndex = seq.findPosition(res);
1452 gSize = groups.length;
1453 for (g = 0; g < gSize; g++)
1455 if (text.length() < 1)
1457 text.append("<area shape=\"rect\" coords=\""
1458 + (idWidth + res * av.getCharWidth()) + "," + sy
1459 + "," + (idWidth + (res + 1) * av.getCharWidth())
1460 + "," + (av.getCharHeight() + sy) + "\""
1461 + " onMouseOver=\"toolTip('" + alIndex + " "
1465 if (groups[g].getStartRes() < res
1466 && groups[g].getEndRes() > res)
1468 text.append("<br><em>" + groups[g].getName() + "</em>");
1472 if (features != null)
1474 if (text.length() < 1)
1476 text.append("<area shape=\"rect\" coords=\""
1477 + (idWidth + res * av.getCharWidth()) + "," + sy
1478 + "," + (idWidth + (res + 1) * av.getCharWidth())
1479 + "," + (av.getCharHeight() + sy) + "\""
1480 + " onMouseOver=\"toolTip('" + alIndex + " "
1483 fSize = features.length;
1484 for (f = 0; f < fSize; f++)
1487 if ((features[f].getBegin() <= seq.findPosition(res))
1488 && (features[f].getEnd() >= seq.findPosition(res)))
1490 if (features[f].getType().equals("disulfide bond"))
1492 if (features[f].getBegin() == seq.findPosition(res)
1493 || features[f].getEnd() == seq
1496 text.append("<br>disulfide bond "
1497 + features[f].getBegin() + ":"
1498 + features[f].getEnd());
1503 text.append("<br>");
1504 text.append(features[f].getType());
1505 if (features[f].getDescription() != null
1506 && !features[f].getType().equals(
1507 features[f].getDescription()))
1509 text.append(" " + features[f].getDescription());
1512 if (features[f].getValue("status") != null)
1514 text.append(" (" + features[f].getValue("status")
1522 if (text.length() > 1)
1524 text.append("')\"; onMouseOut=\"toolTip()\"; href=\"#\">");
1525 out.println(text.toString());
1529 out.println("</map></body></html>");
1532 } catch (Exception ex)
1534 ex.printStackTrace();
1536 } // /////////END OF IMAGE MAP
1540 int getWrappedHeight()
1542 int seqPanelWidth = getSeqPanel().seqCanvas.getWidth();
1544 if (System.getProperty("java.awt.headless") != null
1545 && System.getProperty("java.awt.headless").equals("true"))
1547 seqPanelWidth = alignFrame.getWidth() - getVisibleIdWidth()
1548 - vscroll.getPreferredSize().width
1549 - alignFrame.getInsets().left - alignFrame.getInsets().right;
1552 int chunkWidth = getSeqPanel().seqCanvas
1553 .getWrappedCanvasWidth(seqPanelWidth);
1555 int hgap = av.getCharHeight();
1556 if (av.getScaleAboveWrapped())
1558 hgap += av.getCharHeight();
1561 int annotationHeight = 0;
1562 if (av.isShowAnnotation())
1564 annotationHeight = getAnnotationPanel().adjustPanelHeight();
1567 int cHeight = av.getAlignment().getHeight() * av.getCharHeight() + hgap
1570 int maxwidth = av.getAlignment().getWidth();
1571 if (av.hasHiddenColumns())
1573 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
1576 int height = ((maxwidth / chunkWidth) + 1) * cHeight;
1582 * close the panel - deregisters all listeners and nulls any references to
1585 public void closePanel()
1587 PaintRefresher.RemoveComponent(getSeqPanel().seqCanvas);
1588 PaintRefresher.RemoveComponent(getIdPanel().getIdCanvas());
1589 PaintRefresher.RemoveComponent(this);
1592 jalview.structure.StructureSelectionManager ssm = av
1593 .getStructureSelectionManager();
1594 ssm.removeStructureViewerListener(getSeqPanel(), null);
1595 ssm.removeSelectionListener(getSeqPanel());
1596 ssm.removeCommandListener(av);
1597 ssm.removeStructureViewerListener(getSeqPanel(), null);
1598 ssm.removeSelectionListener(getSeqPanel());
1599 av.setAlignment(null);
1604 if (Cache.log.isDebugEnabled())
1606 Cache.log.warn("Closing alignment panel which is already closed.");
1612 * hides or shows dynamic annotation rows based on groups and av state flags
1614 public void updateAnnotation()
1616 updateAnnotation(false, false);
1619 public void updateAnnotation(boolean applyGlobalSettings)
1621 updateAnnotation(applyGlobalSettings, false);
1624 public void updateAnnotation(boolean applyGlobalSettings,
1625 boolean preserveNewGroupSettings)
1627 av.updateGroupAnnotationSettings(applyGlobalSettings,
1628 preserveNewGroupSettings);
1629 adjustAnnotationHeight();
1633 public AlignmentI getAlignment()
1635 return av == null ? null : av.getAlignment();
1639 public String getViewName()
1645 * Make/Unmake this alignment panel the current input focus
1649 public void setSelected(boolean b)
1653 if (alignFrame.getSplitViewContainer() != null)
1656 * bring enclosing SplitFrame to front first if there is one
1658 ((SplitFrame) alignFrame.getSplitViewContainer()).setSelected(b);
1660 alignFrame.setSelected(b);
1661 } catch (Exception ex)
1667 alignFrame.setDisplayedView(this);
1672 public StructureSelectionManager getStructureSelectionManager()
1674 return av.getStructureSelectionManager();
1678 public void raiseOOMWarning(String string, OutOfMemoryError error)
1680 new OOMWarning(string, error, this);
1684 public jalview.api.FeatureRenderer cloneFeatureRenderer()
1687 return new FeatureRenderer(this);
1691 public jalview.api.FeatureRenderer getFeatureRenderer()
1693 return seqPanel.seqCanvas.getFeatureRenderer();
1696 public void updateFeatureRenderer(
1697 jalview.renderer.seqfeatures.FeatureRenderer fr)
1699 fr.transferSettings(getSeqPanel().seqCanvas.getFeatureRenderer());
1702 public void updateFeatureRendererFrom(jalview.api.FeatureRenderer fr)
1704 if (getSeqPanel().seqCanvas.getFeatureRenderer() != null)
1706 getSeqPanel().seqCanvas.getFeatureRenderer().transferSettings(fr);
1710 public ScalePanel getScalePanel()
1715 public void setScalePanel(ScalePanel scalePanel)
1717 this.scalePanel = scalePanel;
1720 public SeqPanel getSeqPanel()
1725 public void setSeqPanel(SeqPanel seqPanel)
1727 this.seqPanel = seqPanel;
1730 public AnnotationPanel getAnnotationPanel()
1732 return annotationPanel;
1735 public void setAnnotationPanel(AnnotationPanel annotationPanel)
1737 this.annotationPanel = annotationPanel;
1740 public AnnotationLabels getAlabels()
1745 public void setAlabels(AnnotationLabels alabels)
1747 this.alabels = alabels;
1750 public IdPanel getIdPanel()
1755 public void setIdPanel(IdPanel idPanel)
1757 this.idPanel = idPanel;
1761 * Follow a scrolling change in the (cDNA/Protein) complementary alignment.
1762 * The aim is to keep the two alignments 'lined up' on their centre columns.
1765 * holds mapped region(s) of this alignment that we are scrolling
1766 * 'to'; may be modified for sequence offset by this method
1767 * @param verticalOffset
1768 * the number of visible sequences to show above the mapped region
1770 public void scrollToCentre(SearchResults sr, int verticalOffset)
1773 * To avoid jumpy vertical scrolling (if some sequences are gapped or not
1774 * mapped), we can make the scroll-to location a sequence above the one
1777 SequenceI mappedTo = sr.getResultSequence(0);
1778 List<SequenceI> seqs = av.getAlignment().getSequences();
1781 * This is like AlignmentI.findIndex(seq) but here we are matching the
1782 * dataset sequence not the aligned sequence
1784 boolean matched = false;
1785 for (SequenceI seq : seqs)
1787 if (mappedTo == seq.getDatasetSequence())
1795 return; // failsafe, shouldn't happen
1799 * Scroll to position but centring the target residue.
1801 scrollToPosition(sr, verticalOffset, true, true);
1805 * Set a flag to say do not scroll any (cDNA/protein) complement.
1809 protected void setDontScrollComplement(boolean b)
1811 this.dontScrollComplement = b;
1814 protected boolean isDontScrollComplement()
1816 return this.dontScrollComplement;