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.ColorUtils;
37 import jalview.util.MessageManager;
38 import jalview.util.Platform;
40 import java.awt.BorderLayout;
41 import java.awt.Color;
42 import java.awt.Container;
43 import java.awt.Dimension;
45 import java.awt.FontMetrics;
46 import java.awt.Graphics;
47 import java.awt.Insets;
48 import java.awt.event.AdjustmentEvent;
49 import java.awt.event.AdjustmentListener;
50 import java.awt.print.PageFormat;
51 import java.awt.print.Printable;
52 import java.awt.print.PrinterException;
53 import java.beans.PropertyChangeEvent;
54 import java.beans.PropertyChangeListener;
56 import java.io.FileWriter;
57 import java.io.PrintWriter;
58 import java.util.List;
60 import javax.swing.SwingUtilities;
66 * @version $Revision: 1.161 $
68 public class AlignmentPanel extends GAlignmentPanel implements
69 AdjustmentListener, Printable, AlignmentViewPanel
71 public AlignViewport av;
73 OverviewPanel overviewPanel;
75 private SeqPanel seqPanel;
77 private IdPanel idPanel;
79 private boolean headless;
81 IdwidthAdjuster idwidthAdjuster;
84 public AlignFrame alignFrame;
86 private ScalePanel scalePanel;
88 private AnnotationPanel annotationPanel;
90 private AnnotationLabels alabels;
92 // this value is set false when selection area being dragged
93 boolean fastPaint = true;
100 * Flag set while scrolling to follow complementary cDNA/protein scroll. When
101 * true, suppresses invoking the same method recursively.
103 private boolean dontScrollComplement;
106 * Creates a new AlignmentPanel object.
111 public AlignmentPanel(AlignFrame af, final AlignViewport av)
115 setSeqPanel(new SeqPanel(av, this));
116 setIdPanel(new IdPanel(av, this));
118 setScalePanel(new ScalePanel(av, this));
120 idPanelHolder.add(getIdPanel(), BorderLayout.CENTER);
121 idwidthAdjuster = new IdwidthAdjuster(this);
122 idSpaceFillerPanel1.add(idwidthAdjuster, BorderLayout.CENTER);
124 setAnnotationPanel(new AnnotationPanel(this));
125 setAlabels(new AnnotationLabels(this));
127 annotationScroller.setViewportView(getAnnotationPanel());
128 annotationSpaceFillerHolder.add(getAlabels(), BorderLayout.CENTER);
130 scalePanelHolder.add(getScalePanel(), BorderLayout.CENTER);
131 seqPanelHolder.add(getSeqPanel(), BorderLayout.CENTER);
133 setScrollValues(0, 0);
135 hscroll.addAdjustmentListener(this);
136 vscroll.addAdjustmentListener(this);
138 final AlignmentPanel ap = this;
139 av.addPropertyChangeListener(new PropertyChangeListener()
142 public void propertyChange(PropertyChangeEvent evt)
144 if (evt.getPropertyName().equals("alignment"))
146 PaintRefresher.Refresh(ap, av.getSequenceSetId(), true, true);
152 adjustAnnotationHeight();
157 public AlignViewportI getAlignViewport()
162 public void alignmentChanged()
164 av.alignmentChanged(this);
166 alignFrame.updateEditMenuBar();
168 paintAlignment(true);
175 public void fontChanged()
177 // set idCanvas bufferedImage to null
178 // to prevent drawing old image
179 FontMetrics fm = getFontMetrics(av.getFont());
181 scalePanelHolder.setPreferredSize(new Dimension(10, av.getCharHeight()
183 idSpaceFillerPanel1.setPreferredSize(new Dimension(10, av
184 .getCharHeight() + fm.getDescent()));
186 getIdPanel().getIdCanvas().gg = null;
187 getSeqPanel().seqCanvas.img = null;
188 getAnnotationPanel().adjustPanelHeight();
190 Dimension d = calculateIdWidth();
192 d.setSize(d.width + 4, d.height);
193 getIdPanel().getIdCanvas().setPreferredSize(d);
194 hscrollFillerPanel.setPreferredSize(d);
196 if (overviewPanel != null)
198 overviewPanel.setBoxPosition();
200 if (this.alignFrame.getSplitViewContainer() != null)
202 ((SplitFrame) this.alignFrame.getSplitViewContainer()).adjustLayout();
209 * Calculate the width of the alignment labels based on the displayed names
210 * and any bounds on label width set in preferences.
212 * @return Dimension giving the maximum width of the alignment label panel
213 * that should be used.
215 public Dimension calculateIdWidth()
217 // calculate sensible default width when no preference is available
219 if (av.getIdWidth() < 0)
221 int afwidth = (alignFrame != null ? alignFrame.getWidth() : 300);
222 int maxwidth = Math.max(20, Math.min(afwidth - 200, 2 * afwidth / 3));
223 r = calculateIdWidth(maxwidth);
224 av.setIdWidth(r.width);
229 r.width = av.getIdWidth();
236 * Calculate the width of the alignment labels based on the displayed names
237 * and any bounds on label width set in preferences.
240 * -1 or maximum width allowed for IdWidth
241 * @return Dimension giving the maximum width of the alignment label panel
242 * that should be used.
244 public Dimension calculateIdWidth(int maxwidth)
246 Container c = new Container();
248 FontMetrics fm = c.getFontMetrics(new Font(av.font.getName(),
249 Font.ITALIC, av.font.getSize()));
251 AlignmentI al = av.getAlignment();
256 while ((i < al.getHeight()) && (al.getSequenceAt(i) != null))
258 SequenceI s = al.getSequenceAt(i);
260 id = s.getDisplayId(av.getShowJVSuffix());
262 if (fm.stringWidth(id) > idWidth)
264 idWidth = fm.stringWidth(id);
270 // Also check annotation label widths
273 if (al.getAlignmentAnnotation() != null)
275 fm = c.getFontMetrics(getAlabels().getFont());
277 while (i < al.getAlignmentAnnotation().length)
279 String label = al.getAlignmentAnnotation()[i].label;
281 if (fm.stringWidth(label) > idWidth)
283 idWidth = fm.stringWidth(label);
290 return new Dimension(maxwidth < 0 ? idWidth : Math.min(maxwidth,
295 * Highlight the given results on the alignment.
298 public void highlightSearchResults(SearchResults results)
300 scrollToPosition(results);
301 getSeqPanel().seqCanvas.highlightSearchResults(results);
305 * Scroll the view to show the position of the highlighted region in results
306 * (if any) and redraw the overview
310 public boolean scrollToPosition(SearchResults results)
312 return scrollToPosition(results, 0, true, false);
316 * Scroll the view to show the position of the highlighted region in results
319 * @param searchResults
320 * @param redrawOverview
323 public boolean scrollToPosition(SearchResults searchResults,
324 boolean redrawOverview)
326 return scrollToPosition(searchResults, 0, redrawOverview, false);
330 * Scroll the view to show the position of the highlighted region in results
334 * @param verticalOffset
335 * if greater than zero, allows scrolling to a position below the
336 * first displayed sequence
337 * @param redrawOverview
338 * - when set, the overview will be recalculated (takes longer)
340 * if true, try to centre the search results horizontally in the view
341 * @return false if results were not found
343 public boolean scrollToPosition(SearchResults results,
344 int verticalOffset, boolean redrawOverview, boolean centre)
346 int startv, endv, starts, ends;
347 // TODO: properly locate search results in view when large numbers of hidden
348 // columns exist before highlighted region
349 // do we need to scroll the panel?
350 // TODO: tons of nullpointerexceptions raised here.
351 if (results != null && results.getSize() > 0 && av != null
352 && av.getAlignment() != null)
354 int seqIndex = av.getAlignment().findIndex(results);
359 SequenceI seq = av.getAlignment().getSequenceAt(seqIndex);
361 int[] r = results.getResults(seq, 0, av.getAlignment().getWidth());
369 // System.err.println(this.av.viewName + " Seq : " + seqIndex
370 // + " Scroll to " + start + "," + end);
373 * To centre results, scroll to positions half the visible width
374 * left/right of the start/end positions
378 int offset = (av.getEndRes() - av.getStartRes() + 1) / 2 - 1;
379 start = Math.max(start - offset, 0);
380 end = end + offset - 1;
386 if (end == seq.getEnd())
390 if (av.hasHiddenColumns())
392 start = av.getColumnSelection().findColumnPosition(start);
393 end = av.getColumnSelection().findColumnPosition(end);
396 if (!av.getColumnSelection().isVisible(r[0]))
398 // don't scroll - position isn't visible
405 * allow for offset of target sequence (actually scroll to one above it)
407 seqIndex = Math.max(0, seqIndex - verticalOffset);
409 // System.out.println("start=" + start + ", end=" + end + ", startv="
410 // + av.getStartRes() + ", endv=" + av.getEndRes() + ", starts="
411 // + av.getStartSeq() + ", ends=" + av.getEndSeq());
412 if (!av.getWrapAlignment())
414 if ((startv = av.getStartRes()) >= start)
417 * Scroll left to make start of search results visible
419 // setScrollValues(start - 1, seqIndex); // plus one residue
420 setScrollValues(start, seqIndex);
422 else if ((endv = av.getEndRes()) <= end)
425 * Scroll right to make end of search results visible
427 // setScrollValues(startv + 1 + end - endv, seqIndex); // plus one
428 setScrollValues(startv + end - endv, seqIndex);
430 else if ((starts = av.getStartSeq()) > seqIndex)
433 * Scroll up to make start of search results visible
435 setScrollValues(av.getStartRes(), seqIndex);
437 else if ((ends = av.getEndSeq()) <= seqIndex)
440 * Scroll down to make end of search results visible
442 setScrollValues(av.getStartRes(), starts + seqIndex - ends + 1);
445 * Else results are already visible - no need to scroll
450 scrollToWrappedVisible(start);
453 if (redrawOverview && overviewPanel != null)
455 overviewPanel.setBoxPosition();
457 paintAlignment(redrawOverview);
461 void scrollToWrappedVisible(int res)
463 int cwidth = getSeqPanel().seqCanvas
464 .getWrappedCanvasWidth(getSeqPanel().seqCanvas.getWidth());
465 if (res < av.getStartRes() || res >= (av.getStartRes() + cwidth))
467 vscroll.setValue((res / cwidth));
468 av.startRes = vscroll.getValue() * cwidth;
476 * @return DOCUMENT ME!
478 public OverviewPanel getOverviewPanel()
480 return overviewPanel;
489 public void setOverviewPanel(OverviewPanel op)
497 * Hide or show annotation panel
500 public void setAnnotationVisible(boolean b)
502 if (!av.getWrapAlignment())
504 annotationSpaceFillerHolder.setVisible(b);
505 annotationScroller.setVisible(b);
511 * automatically adjust annotation panel height for new annotation whilst
512 * ensuring the alignment is still visible.
515 public void adjustAnnotationHeight()
517 // TODO: display vertical annotation scrollbar if necessary
518 // this is called after loading new annotation onto alignment
519 if (alignFrame.getHeight() == 0)
521 System.out.println("NEEDS FIXING");
523 validateAnnotationDimensions(true);
525 paintAlignment(true);
529 * calculate the annotation dimensions and refresh slider values accordingly.
530 * need to do repaints/notifys afterwards.
532 protected void validateAnnotationDimensions(boolean adjustPanelHeight)
534 int annotationHeight = getAnnotationPanel().adjustPanelHeight();
536 if (adjustPanelHeight)
538 int rowHeight = av.getCharHeight();
539 int alignmentHeight = rowHeight * av.getAlignment().getHeight();
542 * Estimate available height in the AlignFrame for alignment +
543 * annotations. Deduct an estimate for title bar, menu bar, scale panel,
544 * hscroll, status bar (as these are not laid out we can't inspect their
545 * actual heights). Insets gives frame borders.
547 int stuff = Platform.isAMac() ? 80 : 100;
548 Insets insets = alignFrame.getInsets();
549 int availableHeight = alignFrame.getHeight() - stuff - insets.top
553 * If not enough vertical space, maximize annotation height while keeping
554 * at least two rows of alignment visible
556 if (annotationHeight + alignmentHeight > availableHeight)
558 annotationHeight = Math.min(annotationHeight, availableHeight - 2
564 // maintain same window layout whilst updating sliders
565 annotationHeight = annotationScroller.getSize().height;
569 annotationScroller.setPreferredSize(new Dimension(annotationScroller
570 .getWidth(), annotationHeight));
572 Dimension e = idPanel.getSize();
573 alabels.setSize(new Dimension(e.width, annotationHeight));
575 annotationSpaceFillerHolder.setPreferredSize(new Dimension(
576 annotationSpaceFillerHolder.getWidth(), annotationHeight));
577 annotationScroller.validate();
578 annotationScroller.addNotify();
582 * update alignment layout for viewport settings
587 public void updateLayout()
590 setAnnotationVisible(av.isShowAnnotation());
591 boolean wrap = av.getWrapAlignment();
593 scalePanelHolder.setVisible(!wrap);
594 hscroll.setVisible(!wrap);
595 idwidthAdjuster.setVisible(!wrap);
599 annotationScroller.setVisible(false);
600 annotationSpaceFillerHolder.setVisible(false);
602 else if (av.isShowAnnotation())
604 annotationScroller.setVisible(true);
605 annotationSpaceFillerHolder.setVisible(true);
608 idSpaceFillerPanel1.setVisible(!wrap);
613 // return value is true if the scroll is valid
614 public boolean scrollUp(boolean up)
618 if (vscroll.getValue() < 1)
624 vscroll.setValue(vscroll.getValue() - 1);
628 if ((vextent + vscroll.getValue()) >= av.getAlignment().getHeight())
634 vscroll.setValue(vscroll.getValue() + 1);
648 * @return DOCUMENT ME!
650 public boolean scrollRight(boolean right)
654 if (hscroll.getValue() < 1)
660 hscroll.setValue(hscroll.getValue() - 1);
664 if ((hextent + hscroll.getValue()) >= av.getAlignment().getWidth())
670 hscroll.setValue(hscroll.getValue() + 1);
679 * Adjust row/column scrollers to show a visible position in the alignment.
682 * visible column to scroll to
684 * visible row to scroll to
687 public void setScrollValues(int x, int y)
689 // System.err.println("Scroll " + this.av.viewName + " to " + x + "," + y);
690 if (av == null || av.getAlignment() == null)
694 int width = av.getAlignment().getWidth();
695 int height = av.getAlignment().getHeight();
697 if (av.hasHiddenColumns())
699 width = av.getColumnSelection().findColumnPosition(width);
702 av.setEndRes((x + (getSeqPanel().seqCanvas.getWidth() / av
703 .getCharWidth())) - 1);
705 hextent = getSeqPanel().seqCanvas.getWidth() / av.getCharWidth();
706 vextent = getSeqPanel().seqCanvas.getHeight() / av.getCharHeight();
713 if (vextent > height)
718 if ((hextent + x) > width)
723 if ((vextent + y) > height)
725 y = height - vextent;
739 * each scroll adjustment triggers adjustmentValueChanged, which resets the
740 * 'do not scroll complement' flag; ensure it is the same for both
743 boolean flag = isDontScrollComplement();
744 hscroll.setValues(x, hextent, 0, width);
745 setDontScrollComplement(flag);
746 vscroll.setValues(y, vextent, 0, height);
756 public void adjustmentValueChanged(AdjustmentEvent evt)
758 int oldX = av.getStartRes();
759 int oldY = av.getStartSeq();
761 if (evt.getSource() == hscroll)
763 int x = hscroll.getValue();
765 av.setEndRes((x + (getSeqPanel().seqCanvas.getWidth() / av
766 .getCharWidth())) - 1);
769 if (evt.getSource() == vscroll)
771 int offy = vscroll.getValue();
773 if (av.getWrapAlignment())
777 int rowSize = getSeqPanel().seqCanvas
778 .getWrappedCanvasWidth(getSeqPanel().seqCanvas.getWidth());
779 av.setStartRes(offy * rowSize);
780 av.setEndRes((offy + 1) * rowSize);
784 // This is only called if file loaded is a jar file that
785 // was wrapped when saved and user has wrap alignment true
786 // as preference setting
787 SwingUtilities.invokeLater(new Runnable()
792 setScrollValues(av.getStartRes(), av.getStartSeq());
799 av.setStartSeq(offy);
801 + (getSeqPanel().seqCanvas.getHeight() / av.getCharHeight()));
805 if (overviewPanel != null)
807 overviewPanel.setBoxPosition();
810 int scrollX = av.startRes - oldX;
811 int scrollY = av.startSeq - oldY;
813 if (av.getWrapAlignment() || !fastPaint)
819 // Make sure we're not trying to draw a panel
820 // larger than the visible window
821 if (scrollX > av.endRes - av.startRes)
823 scrollX = av.endRes - av.startRes;
825 else if (scrollX < av.startRes - av.endRes)
827 scrollX = av.startRes - av.endRes;
830 if (scrollX != 0 || scrollY != 0)
832 getIdPanel().getIdCanvas().fastPaint(scrollY);
833 getSeqPanel().seqCanvas.fastPaint(scrollX, scrollY);
834 getScalePanel().repaint();
836 if (av.isShowAnnotation() && scrollX != 0)
838 getAnnotationPanel().fastPaint(scrollX);
843 * If there is one, scroll the (Protein/cDNA) complementary alignment to
844 * match, unless we are ourselves doing that.
846 if (isDontScrollComplement())
848 setDontScrollComplement(false);
852 av.scrollComplementaryAlignment();
857 * Repaint the alignment including the annotations and overview panels (if
861 public void paintAlignment(boolean updateOverview)
863 final AnnotationSorter sorter = new AnnotationSorter(getAlignment(),
864 av.isShowAutocalculatedAbove());
865 sorter.sort(getAlignment().getAlignmentAnnotation(),
866 av.getSortAnnotationsBy());
871 // TODO: determine if this paintAlignment changed structure colours
872 av.getStructureSelectionManager().sequenceColoursChanged(this);
874 if (overviewPanel != null)
876 overviewPanel.updateOverviewImage();
888 public void paintComponent(Graphics g)
892 Dimension d = getIdPanel().getIdCanvas().getPreferredSize();
893 idPanelHolder.setPreferredSize(d);
894 hscrollFillerPanel.setPreferredSize(new Dimension(d.width, 12));
898 * set scroll bar positions; first suppress this being 'followed' in any
899 * complementary split pane
901 setDontScrollComplement(true);
903 if (av.getWrapAlignment())
905 int maxwidth = av.getAlignment().getWidth();
907 if (av.hasHiddenColumns())
909 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
912 int canvasWidth = getSeqPanel().seqCanvas
913 .getWrappedCanvasWidth(getSeqPanel().seqCanvas.getWidth());
917 / getSeqPanel().seqCanvas
918 .getWrappedCanvasWidth(getSeqPanel().seqCanvas
920 vscroll.setMaximum(max);
921 vscroll.setUnitIncrement(1);
922 vscroll.setVisibleAmount(1);
927 setScrollValues(av.getStartRes(), av.getStartSeq());
941 * @return DOCUMENT ME!
943 * @throws PrinterException
947 public int print(Graphics pg, PageFormat pf, int pi)
948 throws PrinterException
950 pg.translate((int) pf.getImageableX(), (int) pf.getImageableY());
952 int pwidth = (int) pf.getImageableWidth();
953 int pheight = (int) pf.getImageableHeight();
955 if (av.getWrapAlignment())
957 return printWrappedAlignment(pg, pwidth, pheight, pi);
961 return printUnwrapped(pg, pwidth, pheight, pi);
977 * @return DOCUMENT ME!
979 * @throws PrinterException
982 public int printUnwrapped(Graphics pg, int pwidth, int pheight, int pi)
983 throws PrinterException
985 int idWidth = getVisibleIdWidth(false);
986 FontMetrics fm = getFontMetrics(av.getFont());
987 int scaleHeight = av.getCharHeight() + fm.getDescent();
989 pg.setColor(Color.white);
990 pg.fillRect(0, 0, pwidth, pheight);
991 pg.setFont(av.getFont());
993 // //////////////////////////////////
994 // / How many sequences and residues can we fit on a printable page?
995 int totalRes = (pwidth - idWidth) / av.getCharWidth();
997 int totalSeq = (pheight - scaleHeight) / av.getCharHeight() - 1;
999 int pagesWide = (av.getAlignment().getWidth() / totalRes) + 1;
1001 // ///////////////////////////
1002 // / Only print these sequences and residues on this page
1005 // ///////////////////////////
1006 // / Only print these sequences and residues on this page
1009 // ///////////////////////////
1010 // / Only print these sequences and residues on this page
1013 // ///////////////////////////
1014 // / Only print these sequences and residues on this page
1016 startRes = (pi % pagesWide) * totalRes;
1017 endRes = (startRes + totalRes) - 1;
1019 if (endRes > (av.getAlignment().getWidth() - 1))
1021 endRes = av.getAlignment().getWidth() - 1;
1024 startSeq = (pi / pagesWide) * totalSeq;
1025 endSeq = startSeq + totalSeq;
1027 if (endSeq > av.getAlignment().getHeight())
1029 endSeq = av.getAlignment().getHeight();
1032 int pagesHigh = ((av.getAlignment().getHeight() / totalSeq) + 1)
1035 if (av.isShowAnnotation())
1037 pagesHigh += getAnnotationPanel().adjustPanelHeight() + 3;
1040 pagesHigh /= pheight;
1042 if (pi >= (pagesWide * pagesHigh))
1044 return Printable.NO_SUCH_PAGE;
1048 pg.translate(idWidth, 0);
1049 getScalePanel().drawScale(pg, startRes, endRes, pwidth - idWidth,
1051 pg.translate(-idWidth, scaleHeight);
1055 Color currentColor = null;
1056 Color currentTextColor = null;
1058 pg.setFont(getIdPanel().getIdCanvas().getIdfont());
1061 for (int i = startSeq; i < endSeq; i++)
1063 seq = av.getAlignment().getSequenceAt(i);
1064 if ((av.getSelectionGroup() != null)
1065 && av.getSelectionGroup().getSequences(null).contains(seq))
1067 currentColor = Color.gray;
1068 currentTextColor = Color.black;
1072 currentColor = ColorUtils.getColor(av.getSequenceColour(seq));
1073 currentTextColor = Color.black;
1076 pg.setColor(currentColor);
1077 pg.fillRect(0, (i - startSeq) * av.getCharHeight(), idWidth,
1078 av.getCharHeight());
1080 pg.setColor(currentTextColor);
1083 if (av.isRightAlignIds())
1085 fm = pg.getFontMetrics();
1087 - fm.stringWidth(seq.getDisplayId(av.getShowJVSuffix()))
1091 pg.drawString(seq.getDisplayId(av.getShowJVSuffix()), xPos,
1092 (((i - startSeq) * av.getCharHeight()) + av.getCharHeight())
1093 - (av.getCharHeight() / 5));
1096 pg.setFont(av.getFont());
1098 // draw main sequence panel
1099 pg.translate(idWidth, 0);
1100 getSeqPanel().seqCanvas.drawPanel(pg, startRes, endRes, startSeq,
1103 if (av.isShowAnnotation() && (endSeq == av.getAlignment().getHeight()))
1105 // draw annotation - need to offset for current scroll position
1106 int offset = -getAlabels().getScrollOffset();
1107 pg.translate(0, offset);
1108 pg.translate(-idWidth - 3, (endSeq - startSeq) * av.getCharHeight()
1110 getAlabels().drawComponent(pg, idWidth);
1111 pg.translate(idWidth + 3, 0);
1112 getAnnotationPanel().renderer.drawComponent(getAnnotationPanel(), av,
1113 pg, -1, startRes, endRes + 1);
1114 pg.translate(0, -offset);
1117 return Printable.PAGE_EXISTS;
1132 * @return DOCUMENT ME!
1134 * @throws PrinterException
1137 public int printWrappedAlignment(Graphics pg, int pwidth, int pheight,
1138 int pi) throws PrinterException
1140 int annotationHeight = 0;
1141 AnnotationLabels labels = null;
1142 if (av.isShowAnnotation())
1144 annotationHeight = getAnnotationPanel().adjustPanelHeight();
1145 labels = new AnnotationLabels(av);
1148 int hgap = av.getCharHeight();
1149 if (av.getScaleAboveWrapped())
1151 hgap += av.getCharHeight();
1154 int cHeight = av.getAlignment().getHeight() * av.getCharHeight() + hgap
1157 int idWidth = getVisibleIdWidth(false);
1159 int maxwidth = av.getAlignment().getWidth();
1160 if (av.hasHiddenColumns())
1162 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
1165 int resWidth = getSeqPanel().seqCanvas.getWrappedCanvasWidth(pwidth
1168 int totalHeight = cHeight * (maxwidth / resWidth + 1);
1170 pg.setColor(Color.white);
1171 pg.fillRect(0, 0, pwidth, pheight);
1172 pg.setFont(av.getFont());
1176 pg.setColor(Color.black);
1178 pg.translate(0, -pi * pheight);
1180 pg.setClip(0, pi * pheight, pwidth, pheight);
1186 for (int i = 0; i < av.getAlignment().getHeight(); i++)
1188 pg.setFont(getIdPanel().getIdCanvas().getIdfont());
1189 SequenceI s = av.getAlignment().getSequenceAt(i);
1190 String string = s.getDisplayId(av.getShowJVSuffix());
1192 if (av.isRightAlignIds())
1194 FontMetrics fm = pg.getFontMetrics();
1195 xPos = idWidth - fm.stringWidth(string) - 4;
1197 pg.drawString(string, xPos,
1198 ((i * av.getCharHeight()) + ypos + av.getCharHeight())
1199 - (av.getCharHeight() / 5));
1204 ypos + (av.getAlignment().getHeight() * av.getCharHeight()));
1206 pg.setFont(av.getFont());
1207 labels.drawComponent(pg, idWidth);
1211 - (av.getAlignment().getHeight() * av
1216 } while (ypos < totalHeight);
1218 pg.translate(idWidth, 0);
1220 getSeqPanel().seqCanvas.drawWrappedPanel(pg, pwidth - idWidth,
1223 if ((pi * pheight) < totalHeight)
1225 return Printable.PAGE_EXISTS;
1230 return Printable.NO_SUCH_PAGE;
1235 * get current sequence ID panel width, or nominal value if panel were to be
1236 * displayed using default settings
1240 public int getVisibleIdWidth()
1242 return getVisibleIdWidth(true);
1246 * get current sequence ID panel width, or nominal value if panel were to be
1247 * displayed using default settings
1250 * indicate if the Id width for onscreen or offscreen display should
1254 public int getVisibleIdWidth(boolean onscreen)
1256 // see if rendering offscreen - check preferences and calc width accordingly
1257 if (!onscreen && Cache.getDefault("FIGURE_AUTOIDWIDTH", false))
1259 return calculateIdWidth(-1).width + 4;
1261 Integer idwidth = null;
1263 || (idwidth = Cache.getIntegerProperty("FIGURE_FIXEDIDWIDTH")) == null)
1265 return (getIdPanel().getWidth() > 0 ? getIdPanel().getWidth()
1266 : calculateIdWidth().width + 4);
1268 return idwidth.intValue() + 4;
1271 void makeAlignmentImage(jalview.util.ImageMaker.TYPE type, File file)
1273 long pSessionId = System.currentTimeMillis();
1274 headless = (System.getProperty("java.awt.headless") != null && System
1275 .getProperty("java.awt.headless").equals("true"));
1276 if (alignFrame != null && !headless)
1280 alignFrame.setProgressBar(MessageManager.formatMessage(
1281 "status.saving_file", new Object[] { type.getLabel() }),
1287 AlignmentDimension aDimension = getAlignmentDimension();
1290 jalview.util.ImageMaker im;
1291 final String imageAction, imageTitle;
1292 if (type == jalview.util.ImageMaker.TYPE.PNG)
1294 imageAction = "Create PNG image from alignment";
1297 else if (type == jalview.util.ImageMaker.TYPE.EPS)
1299 imageAction = "Create EPS file from alignment";
1300 imageTitle = alignFrame.getTitle();
1304 imageAction = "Create SVG file from alignment";
1305 imageTitle = alignFrame.getTitle();
1308 im = new jalview.util.ImageMaker(this, type, imageAction,
1309 aDimension.getWidth(), aDimension.getHeight(), file,
1310 imageTitle, alignFrame, pSessionId, headless);
1311 if (av.getWrapAlignment())
1313 if (im.getGraphics() != null)
1315 printWrappedAlignment(im.getGraphics(), aDimension.getWidth(),
1316 aDimension.getHeight(), 0);
1322 if (im.getGraphics() != null)
1324 printUnwrapped(im.getGraphics(), aDimension.getWidth(),
1325 aDimension.getHeight(), 0);
1330 } catch (OutOfMemoryError err)
1333 System.out.println("########################\n" + "OUT OF MEMORY "
1334 + file + "\n" + "########################");
1335 new OOMWarning("Creating Image for " + file, err);
1336 // System.out.println("Create IMAGE: " + err);
1337 } catch (Exception ex)
1339 ex.printStackTrace();
1347 public AlignmentDimension getAlignmentDimension()
1349 int maxwidth = av.getAlignment().getWidth();
1350 if (av.hasHiddenColumns())
1352 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth);
1355 int height = ((av.getAlignment().getHeight() + 1) * av.getCharHeight())
1356 + getScalePanel().getHeight();
1357 int width = getVisibleIdWidth(false) + (maxwidth * av.getCharWidth());
1359 if (av.getWrapAlignment())
1361 height = getWrappedHeight();
1364 // need to obtain default alignment width and then add in any
1365 // additional allowance for id margin
1366 // this duplicates the calculation in getWrappedHeight but adjusts for
1368 width = alignFrame.getWidth() - vscroll.getPreferredSize().width
1369 - alignFrame.getInsets().left
1370 - alignFrame.getInsets().right - getVisibleIdWidth()
1371 + getVisibleIdWidth(false);
1375 width = getSeqPanel().getWidth() + getVisibleIdWidth(false);
1379 else if (av.isShowAnnotation())
1381 height += getAnnotationPanel().adjustPanelHeight() + 3;
1383 return new AlignmentDimension(width, height);
1390 public void makeEPS(File epsFile)
1392 makeAlignmentImage(jalview.util.ImageMaker.TYPE.EPS, epsFile);
1398 public void makePNG(File pngFile)
1400 makeAlignmentImage(jalview.util.ImageMaker.TYPE.PNG, pngFile);
1403 public void makeSVG(File svgFile)
1405 makeAlignmentImage(jalview.util.ImageMaker.TYPE.SVG, svgFile);
1408 public void makePNGImageMap(File imgMapFile, String imageName)
1410 // /////ONLY WORKS WITH NONE WRAPPED ALIGNMENTS
1411 // ////////////////////////////////////////////
1412 int idWidth = getVisibleIdWidth(false);
1413 FontMetrics fm = getFontMetrics(av.getFont());
1414 int scaleHeight = av.getCharHeight() + fm.getDescent();
1417 // ////////////////////////////////
1418 if (imgMapFile != null)
1422 int s, sSize = av.getAlignment().getHeight(), res, alwidth = av
1423 .getAlignment().getWidth(), g, gSize, f, fSize, sy;
1424 StringBuffer text = new StringBuffer();
1425 PrintWriter out = new PrintWriter(new FileWriter(imgMapFile));
1426 out.println(jalview.io.HTMLOutput.getImageMapHTML());
1427 out.println("<img src=\"" + imageName
1428 + "\" border=\"0\" usemap=\"#Map\" >"
1429 + "<map name=\"Map\">");
1431 for (s = 0; s < sSize; s++)
1433 sy = s * av.getCharHeight() + scaleHeight;
1435 SequenceI seq = av.getAlignment().getSequenceAt(s);
1436 SequenceFeature[] features = seq.getSequenceFeatures();
1437 SequenceGroup[] groups = av.getAlignment().findAllGroups(seq);
1438 for (res = 0; res < alwidth; res++)
1440 text = new StringBuffer();
1441 String triplet = null;
1442 if (av.getAlignment().isNucleotide())
1444 triplet = ResidueProperties.nucleotideName.get(seq
1445 .getCharAt(res) + "");
1449 triplet = ResidueProperties.aa2Triplet.get(seq.getCharAt(res)
1453 if (triplet == null)
1458 int alIndex = seq.findPosition(res);
1459 gSize = groups.length;
1460 for (g = 0; g < gSize; g++)
1462 if (text.length() < 1)
1464 text.append("<area shape=\"rect\" coords=\""
1465 + (idWidth + res * av.getCharWidth()) + "," + sy
1466 + "," + (idWidth + (res + 1) * av.getCharWidth())
1467 + "," + (av.getCharHeight() + sy) + "\""
1468 + " onMouseOver=\"toolTip('" + alIndex + " "
1472 if (groups[g].getStartRes() < res
1473 && groups[g].getEndRes() > res)
1475 text.append("<br><em>" + groups[g].getName() + "</em>");
1479 if (features != null)
1481 if (text.length() < 1)
1483 text.append("<area shape=\"rect\" coords=\""
1484 + (idWidth + res * av.getCharWidth()) + "," + sy
1485 + "," + (idWidth + (res + 1) * av.getCharWidth())
1486 + "," + (av.getCharHeight() + sy) + "\""
1487 + " onMouseOver=\"toolTip('" + alIndex + " "
1490 fSize = features.length;
1491 for (f = 0; f < fSize; f++)
1494 if ((features[f].getBegin() <= seq.findPosition(res))
1495 && (features[f].getEnd() >= seq.findPosition(res)))
1497 if (features[f].getType().equals("disulfide bond"))
1499 if (features[f].getBegin() == seq.findPosition(res)
1500 || features[f].getEnd() == seq
1503 text.append("<br>disulfide bond "
1504 + features[f].getBegin() + ":"
1505 + features[f].getEnd());
1510 text.append("<br>");
1511 text.append(features[f].getType());
1512 if (features[f].getDescription() != null
1513 && !features[f].getType().equals(
1514 features[f].getDescription()))
1516 text.append(" " + features[f].getDescription());
1519 if (features[f].getValue("status") != null)
1521 text.append(" (" + features[f].getValue("status")
1529 if (text.length() > 1)
1531 text.append("')\"; onMouseOut=\"toolTip()\"; href=\"#\">");
1532 out.println(text.toString());
1536 out.println("</map></body></html>");
1539 } catch (Exception ex)
1541 ex.printStackTrace();
1543 } // /////////END OF IMAGE MAP
1547 int getWrappedHeight()
1549 int seqPanelWidth = getSeqPanel().seqCanvas.getWidth();
1551 if (System.getProperty("java.awt.headless") != null
1552 && System.getProperty("java.awt.headless").equals("true"))
1554 seqPanelWidth = alignFrame.getWidth() - getVisibleIdWidth()
1555 - vscroll.getPreferredSize().width
1556 - alignFrame.getInsets().left - alignFrame.getInsets().right;
1559 int chunkWidth = getSeqPanel().seqCanvas
1560 .getWrappedCanvasWidth(seqPanelWidth);
1562 int hgap = av.getCharHeight();
1563 if (av.getScaleAboveWrapped())
1565 hgap += av.getCharHeight();
1568 int annotationHeight = 0;
1569 if (av.isShowAnnotation())
1571 annotationHeight = getAnnotationPanel().adjustPanelHeight();
1574 int cHeight = av.getAlignment().getHeight() * av.getCharHeight() + hgap
1577 int maxwidth = av.getAlignment().getWidth();
1578 if (av.hasHiddenColumns())
1580 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
1583 int height = ((maxwidth / chunkWidth) + 1) * cHeight;
1589 * close the panel - deregisters all listeners and nulls any references to
1592 public void closePanel()
1594 PaintRefresher.RemoveComponent(getSeqPanel().seqCanvas);
1595 PaintRefresher.RemoveComponent(getIdPanel().getIdCanvas());
1596 PaintRefresher.RemoveComponent(this);
1599 jalview.structure.StructureSelectionManager ssm = av
1600 .getStructureSelectionManager();
1601 ssm.removeStructureViewerListener(getSeqPanel(), null);
1602 ssm.removeSelectionListener(getSeqPanel());
1603 ssm.removeCommandListener(av);
1604 ssm.removeStructureViewerListener(getSeqPanel(), null);
1605 ssm.removeSelectionListener(getSeqPanel());
1606 av.setAlignment(null);
1611 if (Cache.log.isDebugEnabled())
1613 Cache.log.warn("Closing alignment panel which is already closed.");
1619 * hides or shows dynamic annotation rows based on groups and av state flags
1621 public void updateAnnotation()
1623 updateAnnotation(false, false);
1626 public void updateAnnotation(boolean applyGlobalSettings)
1628 updateAnnotation(applyGlobalSettings, false);
1631 public void updateAnnotation(boolean applyGlobalSettings,
1632 boolean preserveNewGroupSettings)
1634 av.updateGroupAnnotationSettings(applyGlobalSettings,
1635 preserveNewGroupSettings);
1636 adjustAnnotationHeight();
1640 public AlignmentI getAlignment()
1642 return av == null ? null : av.getAlignment();
1646 public String getViewName()
1652 * Make/Unmake this alignment panel the current input focus
1656 public void setSelected(boolean b)
1660 if (alignFrame.getSplitViewContainer() != null)
1663 * bring enclosing SplitFrame to front first if there is one
1665 ((SplitFrame) alignFrame.getSplitViewContainer()).setSelected(b);
1667 alignFrame.setSelected(b);
1668 } catch (Exception ex)
1674 alignFrame.setDisplayedView(this);
1679 public StructureSelectionManager getStructureSelectionManager()
1681 return av.getStructureSelectionManager();
1685 public void raiseOOMWarning(String string, OutOfMemoryError error)
1687 new OOMWarning(string, error, this);
1691 public jalview.api.FeatureRenderer cloneFeatureRenderer()
1694 return new FeatureRenderer(this);
1698 public jalview.api.FeatureRenderer getFeatureRenderer()
1700 return seqPanel.seqCanvas.getFeatureRenderer();
1703 public void updateFeatureRenderer(
1704 jalview.renderer.seqfeatures.FeatureRenderer fr)
1706 fr.transferSettings(getSeqPanel().seqCanvas.getFeatureRenderer());
1709 public void updateFeatureRendererFrom(jalview.api.FeatureRenderer fr)
1711 if (getSeqPanel().seqCanvas.getFeatureRenderer() != null)
1713 getSeqPanel().seqCanvas.getFeatureRenderer().transferSettings(fr);
1717 public ScalePanel getScalePanel()
1722 public void setScalePanel(ScalePanel scalePanel)
1724 this.scalePanel = scalePanel;
1727 public SeqPanel getSeqPanel()
1732 public void setSeqPanel(SeqPanel seqPanel)
1734 this.seqPanel = seqPanel;
1737 public AnnotationPanel getAnnotationPanel()
1739 return annotationPanel;
1742 public void setAnnotationPanel(AnnotationPanel annotationPanel)
1744 this.annotationPanel = annotationPanel;
1747 public AnnotationLabels getAlabels()
1752 public void setAlabels(AnnotationLabels alabels)
1754 this.alabels = alabels;
1757 public IdPanel getIdPanel()
1762 public void setIdPanel(IdPanel idPanel)
1764 this.idPanel = idPanel;
1768 * Follow a scrolling change in the (cDNA/Protein) complementary alignment.
1769 * The aim is to keep the two alignments 'lined up' on their centre columns.
1772 * holds mapped region(s) of this alignment that we are scrolling
1773 * 'to'; may be modified for sequence offset by this method
1774 * @param verticalOffset
1775 * the number of visible sequences to show above the mapped region
1777 public void scrollToCentre(SearchResults sr, int verticalOffset)
1780 * To avoid jumpy vertical scrolling (if some sequences are gapped or not
1781 * mapped), we can make the scroll-to location a sequence above the one
1784 SequenceI mappedTo = sr.getResultSequence(0);
1785 List<SequenceI> seqs = av.getAlignment().getSequences();
1788 * This is like AlignmentI.findIndex(seq) but here we are matching the
1789 * dataset sequence not the aligned sequence
1791 boolean matched = false;
1792 for (SequenceI seq : seqs)
1794 if (mappedTo == seq.getDatasetSequence())
1802 return; // failsafe, shouldn't happen
1806 * Scroll to position but centring the target residue.
1808 scrollToPosition(sr, verticalOffset, true, true);
1812 * Set a flag to say do not scroll any (cDNA/protein) complement.
1816 protected void setDontScrollComplement(boolean b)
1818 this.dontScrollComplement = b;
1821 protected boolean isDontScrollComplement()
1823 return this.dontScrollComplement;