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;
104 private PropertyChangeListener propertyChangeListener;
107 * Creates a new AlignmentPanel object.
112 public AlignmentPanel(AlignFrame af, final AlignViewport av)
116 setSeqPanel(new SeqPanel(av, this));
117 setIdPanel(new IdPanel(av, this));
119 setScalePanel(new ScalePanel(av, this));
121 idPanelHolder.add(getIdPanel(), BorderLayout.CENTER);
122 idwidthAdjuster = new IdwidthAdjuster(this);
123 idSpaceFillerPanel1.add(idwidthAdjuster, BorderLayout.CENTER);
125 setAnnotationPanel(new AnnotationPanel(this));
126 setAlabels(new AnnotationLabels(this));
128 annotationScroller.setViewportView(getAnnotationPanel());
129 annotationSpaceFillerHolder.add(getAlabels(), BorderLayout.CENTER);
131 scalePanelHolder.add(getScalePanel(), BorderLayout.CENTER);
132 seqPanelHolder.add(getSeqPanel(), BorderLayout.CENTER);
134 setScrollValues(0, 0);
136 hscroll.addAdjustmentListener(this);
137 vscroll.addAdjustmentListener(this);
139 final AlignmentPanel ap = this;
140 propertyChangeListener = new PropertyChangeListener()
143 public void propertyChange(PropertyChangeEvent evt)
145 if (evt.getPropertyName().equals("alignment"))
147 PaintRefresher.Refresh(ap, av.getSequenceSetId(), true, true);
152 av.addPropertyChangeListener(propertyChangeListener);
154 adjustAnnotationHeight();
159 public AlignViewportI getAlignViewport()
164 public void alignmentChanged()
166 av.alignmentChanged(this);
168 alignFrame.updateEditMenuBar();
170 paintAlignment(true);
177 public void fontChanged()
179 // set idCanvas bufferedImage to null
180 // to prevent drawing old image
181 FontMetrics fm = getFontMetrics(av.getFont());
183 scalePanelHolder.setPreferredSize(new Dimension(10, av.getCharHeight()
185 idSpaceFillerPanel1.setPreferredSize(new Dimension(10, av
186 .getCharHeight() + fm.getDescent()));
188 getIdPanel().getIdCanvas().gg = null;
189 getSeqPanel().seqCanvas.img = null;
190 getAnnotationPanel().adjustPanelHeight();
192 Dimension d = calculateIdWidth();
194 d.setSize(d.width + 4, d.height);
195 getIdPanel().getIdCanvas().setPreferredSize(d);
196 hscrollFillerPanel.setPreferredSize(d);
198 if (overviewPanel != null)
200 overviewPanel.setBoxPosition();
202 if (this.alignFrame.getSplitViewContainer() != null)
204 ((SplitFrame) this.alignFrame.getSplitViewContainer()).adjustLayout();
211 * Calculate the width of the alignment labels based on the displayed names
212 * and any bounds on label width set in preferences.
214 * @return Dimension giving the maximum width of the alignment label panel
215 * that should be used.
217 public Dimension calculateIdWidth()
219 // calculate sensible default width when no preference is available
221 if (av.getIdWidth() < 0)
223 int afwidth = (alignFrame != null ? alignFrame.getWidth() : 300);
224 int maxwidth = Math.max(20, Math.min(afwidth - 200, 2 * afwidth / 3));
225 r = calculateIdWidth(maxwidth);
226 av.setIdWidth(r.width);
231 r.width = av.getIdWidth();
238 * Calculate the width of the alignment labels based on the displayed names
239 * and any bounds on label width set in preferences.
242 * -1 or maximum width allowed for IdWidth
243 * @return Dimension giving the maximum width of the alignment label panel
244 * that should be used.
246 public Dimension calculateIdWidth(int maxwidth)
248 Container c = new Container();
250 FontMetrics fm = c.getFontMetrics(new Font(av.font.getName(),
251 Font.ITALIC, av.font.getSize()));
253 AlignmentI al = av.getAlignment();
258 while ((i < al.getHeight()) && (al.getSequenceAt(i) != null))
260 SequenceI s = al.getSequenceAt(i);
262 id = s.getDisplayId(av.getShowJVSuffix());
264 if (fm.stringWidth(id) > idWidth)
266 idWidth = fm.stringWidth(id);
272 // Also check annotation label widths
275 if (al.getAlignmentAnnotation() != null)
277 fm = c.getFontMetrics(getAlabels().getFont());
279 while (i < al.getAlignmentAnnotation().length)
281 String label = al.getAlignmentAnnotation()[i].label;
283 if (fm.stringWidth(label) > idWidth)
285 idWidth = fm.stringWidth(label);
292 return new Dimension(maxwidth < 0 ? idWidth : Math.min(maxwidth,
297 * Highlight the given results on the alignment.
300 public void highlightSearchResults(SearchResults results)
302 scrollToPosition(results);
303 getSeqPanel().seqCanvas.highlightSearchResults(results);
307 * Scroll the view to show the position of the highlighted region in results
308 * (if any) and redraw the overview
312 public boolean scrollToPosition(SearchResults results)
314 return scrollToPosition(results, 0, true, false);
318 * Scroll the view to show the position of the highlighted region in results
321 * @param searchResults
322 * @param redrawOverview
325 public boolean scrollToPosition(SearchResults searchResults,
326 boolean redrawOverview)
328 return scrollToPosition(searchResults, 0, redrawOverview, false);
332 * Scroll the view to show the position of the highlighted region in results
336 * @param verticalOffset
337 * if greater than zero, allows scrolling to a position below the
338 * first displayed sequence
339 * @param redrawOverview
340 * - when set, the overview will be recalculated (takes longer)
342 * if true, try to centre the search results horizontally in the view
343 * @return false if results were not found
345 public boolean scrollToPosition(SearchResults results,
346 int verticalOffset, boolean redrawOverview, boolean centre)
348 int startv, endv, starts, ends;
349 // TODO: properly locate search results in view when large numbers of hidden
350 // columns exist before highlighted region
351 // do we need to scroll the panel?
352 // TODO: tons of nullpointerexceptions raised here.
353 if (results != null && results.getSize() > 0 && av != null
354 && av.getAlignment() != null)
356 int seqIndex = av.getAlignment().findIndex(results);
361 SequenceI seq = av.getAlignment().getSequenceAt(seqIndex);
363 int[] r = results.getResults(seq, 0, av.getAlignment().getWidth());
371 // System.err.println(this.av.viewName + " Seq : " + seqIndex
372 // + " Scroll to " + start + "," + end);
375 * To centre results, scroll to positions half the visible width
376 * left/right of the start/end positions
380 int offset = (av.getEndRes() - av.getStartRes() + 1) / 2 - 1;
381 start = Math.max(start - offset, 0);
382 end = end + offset - 1;
388 if (end == seq.getEnd())
392 if (av.hasHiddenColumns())
394 start = av.getColumnSelection().findColumnPosition(start);
395 end = av.getColumnSelection().findColumnPosition(end);
398 if (!av.getColumnSelection().isVisible(r[0]))
400 // don't scroll - position isn't visible
407 * allow for offset of target sequence (actually scroll to one above it)
409 seqIndex = Math.max(0, seqIndex - verticalOffset);
411 // System.out.println("start=" + start + ", end=" + end + ", startv="
412 // + av.getStartRes() + ", endv=" + av.getEndRes() + ", starts="
413 // + av.getStartSeq() + ", ends=" + av.getEndSeq());
414 if (!av.getWrapAlignment())
416 if ((startv = av.getStartRes()) >= start)
419 * Scroll left to make start of search results visible
421 // setScrollValues(start - 1, seqIndex); // plus one residue
422 setScrollValues(start, seqIndex);
424 else if ((endv = av.getEndRes()) <= end)
427 * Scroll right to make end of search results visible
429 // setScrollValues(startv + 1 + end - endv, seqIndex); // plus one
430 setScrollValues(startv + end - endv, seqIndex);
432 else if ((starts = av.getStartSeq()) > seqIndex)
435 * Scroll up to make start of search results visible
437 setScrollValues(av.getStartRes(), seqIndex);
439 else if ((ends = av.getEndSeq()) <= seqIndex)
442 * Scroll down to make end of search results visible
444 setScrollValues(av.getStartRes(), starts + seqIndex - ends + 1);
447 * Else results are already visible - no need to scroll
452 scrollToWrappedVisible(start);
455 if (redrawOverview && overviewPanel != null)
457 overviewPanel.setBoxPosition();
459 paintAlignment(redrawOverview);
463 void scrollToWrappedVisible(int res)
465 int cwidth = getSeqPanel().seqCanvas
466 .getWrappedCanvasWidth(getSeqPanel().seqCanvas.getWidth());
467 if (res < av.getStartRes() || res >= (av.getStartRes() + cwidth))
469 vscroll.setValue((res / cwidth));
470 av.startRes = vscroll.getValue() * cwidth;
478 * @return DOCUMENT ME!
480 public OverviewPanel getOverviewPanel()
482 return overviewPanel;
491 public void setOverviewPanel(OverviewPanel op)
499 * Hide or show annotation panel
502 public void setAnnotationVisible(boolean b)
504 if (!av.getWrapAlignment())
506 annotationSpaceFillerHolder.setVisible(b);
507 annotationScroller.setVisible(b);
513 * automatically adjust annotation panel height for new annotation whilst
514 * ensuring the alignment is still visible.
517 public void adjustAnnotationHeight()
519 // TODO: display vertical annotation scrollbar if necessary
520 // this is called after loading new annotation onto alignment
521 if (alignFrame.getHeight() == 0)
523 System.out.println("NEEDS FIXING");
525 validateAnnotationDimensions(true);
527 paintAlignment(true);
531 * calculate the annotation dimensions and refresh slider values accordingly.
532 * need to do repaints/notifys afterwards.
534 protected void validateAnnotationDimensions(boolean adjustPanelHeight)
536 int annotationHeight = getAnnotationPanel().adjustPanelHeight();
538 if (adjustPanelHeight)
540 int rowHeight = av.getCharHeight();
541 int alignmentHeight = rowHeight * av.getAlignment().getHeight();
544 * Estimate available height in the AlignFrame for alignment +
545 * annotations. Deduct an estimate for title bar, menu bar, scale panel,
546 * hscroll, status bar (as these are not laid out we can't inspect their
547 * actual heights). Insets gives frame borders.
549 int stuff = Platform.isAMac() ? 80 : 100;
550 Insets insets = alignFrame.getInsets();
551 int availableHeight = alignFrame.getHeight() - stuff - insets.top
555 * If not enough vertical space, maximize annotation height while keeping
556 * at least two rows of alignment visible
558 if (annotationHeight + alignmentHeight > availableHeight)
560 annotationHeight = Math.min(annotationHeight, availableHeight - 2
566 // maintain same window layout whilst updating sliders
567 annotationHeight = annotationScroller.getSize().height;
571 annotationScroller.setPreferredSize(new Dimension(annotationScroller
572 .getWidth(), annotationHeight));
574 Dimension e = idPanel.getSize();
575 alabels.setSize(new Dimension(e.width, annotationHeight));
577 annotationSpaceFillerHolder.setPreferredSize(new Dimension(
578 annotationSpaceFillerHolder.getWidth(), annotationHeight));
579 annotationScroller.validate();
580 annotationScroller.addNotify();
584 * update alignment layout for viewport settings
589 public void updateLayout()
592 setAnnotationVisible(av.isShowAnnotation());
593 boolean wrap = av.getWrapAlignment();
595 scalePanelHolder.setVisible(!wrap);
596 hscroll.setVisible(!wrap);
597 idwidthAdjuster.setVisible(!wrap);
601 annotationScroller.setVisible(false);
602 annotationSpaceFillerHolder.setVisible(false);
604 else if (av.isShowAnnotation())
606 annotationScroller.setVisible(true);
607 annotationSpaceFillerHolder.setVisible(true);
610 idSpaceFillerPanel1.setVisible(!wrap);
615 // return value is true if the scroll is valid
616 public boolean scrollUp(boolean up)
620 if (vscroll.getValue() < 1)
626 vscroll.setValue(vscroll.getValue() - 1);
630 if ((vextent + vscroll.getValue()) >= av.getAlignment().getHeight())
636 vscroll.setValue(vscroll.getValue() + 1);
650 * @return DOCUMENT ME!
652 public boolean scrollRight(boolean right)
656 if (hscroll.getValue() < 1)
662 hscroll.setValue(hscroll.getValue() - 1);
666 if ((hextent + hscroll.getValue()) >= av.getAlignment().getWidth())
672 hscroll.setValue(hscroll.getValue() + 1);
681 * Adjust row/column scrollers to show a visible position in the alignment.
684 * visible column to scroll to
686 * visible row to scroll to
689 public void setScrollValues(int x, int y)
691 // System.err.println("Scroll " + this.av.viewName + " to " + x + "," + y);
692 if (av == null || av.getAlignment() == null)
696 int width = av.getAlignment().getWidth();
697 int height = av.getAlignment().getHeight();
699 if (av.hasHiddenColumns())
701 width = av.getColumnSelection().findColumnPosition(width);
704 av.setEndRes((x + (getSeqPanel().seqCanvas.getWidth() / av
705 .getCharWidth())) - 1);
707 hextent = getSeqPanel().seqCanvas.getWidth() / av.getCharWidth();
708 vextent = getSeqPanel().seqCanvas.getHeight() / av.getCharHeight();
715 if (vextent > height)
720 if ((hextent + x) > width)
725 if ((vextent + y) > height)
727 y = height - vextent;
741 * each scroll adjustment triggers adjustmentValueChanged, which resets the
742 * 'do not scroll complement' flag; ensure it is the same for both
745 boolean flag = isDontScrollComplement();
746 hscroll.setValues(x, hextent, 0, width);
747 setDontScrollComplement(flag);
748 vscroll.setValues(y, vextent, 0, height);
758 public void adjustmentValueChanged(AdjustmentEvent evt)
760 int oldX = av.getStartRes();
761 int oldY = av.getStartSeq();
763 if (evt.getSource() == hscroll)
765 int x = hscroll.getValue();
767 av.setEndRes((x + (getSeqPanel().seqCanvas.getWidth() / av
768 .getCharWidth())) - 1);
771 if (evt.getSource() == vscroll)
773 int offy = vscroll.getValue();
775 if (av.getWrapAlignment())
779 int rowSize = getSeqPanel().seqCanvas
780 .getWrappedCanvasWidth(getSeqPanel().seqCanvas.getWidth());
781 av.setStartRes(offy * rowSize);
782 av.setEndRes((offy + 1) * rowSize);
786 // This is only called if file loaded is a jar file that
787 // was wrapped when saved and user has wrap alignment true
788 // as preference setting
789 SwingUtilities.invokeLater(new Runnable()
794 setScrollValues(av.getStartRes(), av.getStartSeq());
801 av.setStartSeq(offy);
803 + (getSeqPanel().seqCanvas.getHeight() / av.getCharHeight()));
807 if (overviewPanel != null)
809 overviewPanel.setBoxPosition();
812 int scrollX = av.startRes - oldX;
813 int scrollY = av.startSeq - oldY;
815 if (av.getWrapAlignment() || !fastPaint)
821 // Make sure we're not trying to draw a panel
822 // larger than the visible window
823 if (scrollX > av.endRes - av.startRes)
825 scrollX = av.endRes - av.startRes;
827 else if (scrollX < av.startRes - av.endRes)
829 scrollX = av.startRes - av.endRes;
832 if (scrollX != 0 || scrollY != 0)
834 getIdPanel().getIdCanvas().fastPaint(scrollY);
835 getSeqPanel().seqCanvas.fastPaint(scrollX, scrollY);
836 getScalePanel().repaint();
838 if (av.isShowAnnotation() && scrollX != 0)
840 getAnnotationPanel().fastPaint(scrollX);
845 * If there is one, scroll the (Protein/cDNA) complementary alignment to
846 * match, unless we are ourselves doing that.
848 if (isDontScrollComplement())
850 setDontScrollComplement(false);
854 av.scrollComplementaryAlignment();
859 * Repaint the alignment including the annotations and overview panels (if
863 public void paintAlignment(boolean updateOverview)
865 final AnnotationSorter sorter = new AnnotationSorter(getAlignment(),
866 av.isShowAutocalculatedAbove());
867 sorter.sort(getAlignment().getAlignmentAnnotation(),
868 av.getSortAnnotationsBy());
873 // TODO: determine if this paintAlignment changed structure colours
874 av.getStructureSelectionManager().sequenceColoursChanged(this);
876 if (overviewPanel != null)
878 overviewPanel.updateOverviewImage();
890 public void paintComponent(Graphics g)
894 Dimension d = getIdPanel().getIdCanvas().getPreferredSize();
895 idPanelHolder.setPreferredSize(d);
896 hscrollFillerPanel.setPreferredSize(new Dimension(d.width, 12));
900 * set scroll bar positions; first suppress this being 'followed' in any
901 * complementary split pane
903 setDontScrollComplement(true);
905 if (av.getWrapAlignment())
907 int maxwidth = av.getAlignment().getWidth();
909 if (av.hasHiddenColumns())
911 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
914 int canvasWidth = getSeqPanel().seqCanvas
915 .getWrappedCanvasWidth(getSeqPanel().seqCanvas.getWidth());
919 / getSeqPanel().seqCanvas
920 .getWrappedCanvasWidth(getSeqPanel().seqCanvas
922 vscroll.setMaximum(max);
923 vscroll.setUnitIncrement(1);
924 vscroll.setVisibleAmount(1);
929 setScrollValues(av.getStartRes(), av.getStartSeq());
943 * @return DOCUMENT ME!
945 * @throws PrinterException
949 public int print(Graphics pg, PageFormat pf, int pi)
950 throws PrinterException
952 pg.translate((int) pf.getImageableX(), (int) pf.getImageableY());
954 int pwidth = (int) pf.getImageableWidth();
955 int pheight = (int) pf.getImageableHeight();
957 if (av.getWrapAlignment())
959 return printWrappedAlignment(pwidth, pheight, pi, pg);
963 return printUnwrapped(pwidth, pheight, pi, pg);
979 * @return DOCUMENT ME!
981 * @throws PrinterException
984 public int printUnwrapped(int pwidth, int pheight, int pi,
986 throws PrinterException
988 boolean isMultiGraphics = pg.length > 1;
989 int G0 = 0; // Graphic index of idPanel graphics in multi-graphics mode or
990 // entire graphics for non mulit-graphics mode
991 int G1 = 0; // Graphic index of alignmentPanel graphics for multi-graphics
999 int idWidth = getVisibleIdWidth(false);
1000 FontMetrics fm = getFontMetrics(av.getFont());
1001 int scaleHeight = av.getCharHeight() + fm.getDescent();
1003 pg[G0].setColor(Color.white);
1004 pg[G0].fillRect(0, 0, pwidth, pheight);
1005 pg[G0].setFont(av.getFont());
1007 // //////////////////////////////////
1008 // / How many sequences and residues can we fit on a printable page?
1009 int totalRes = (pwidth - idWidth) / av.getCharWidth();
1011 int totalSeq = (pheight - scaleHeight) / av.getCharHeight() - 1;
1013 int pagesWide = (av.getAlignment().getWidth() / totalRes) + 1;
1015 // ///////////////////////////
1016 // / Only print these sequences and residues on this page
1019 // ///////////////////////////
1020 // / Only print these sequences and residues on this page
1023 // ///////////////////////////
1024 // / Only print these sequences and residues on this page
1027 // ///////////////////////////
1028 // / Only print these sequences and residues on this page
1030 startRes = (pi % pagesWide) * totalRes;
1031 endRes = (startRes + totalRes) - 1;
1033 if (endRes > (av.getAlignment().getWidth() - 1))
1035 endRes = av.getAlignment().getWidth() - 1;
1038 startSeq = (pi / pagesWide) * totalSeq;
1039 endSeq = startSeq + totalSeq;
1041 if (endSeq > av.getAlignment().getHeight())
1043 endSeq = av.getAlignment().getHeight();
1046 int pagesHigh = ((av.getAlignment().getHeight() / totalSeq) + 1)
1049 if (av.isShowAnnotation())
1051 pagesHigh += getAnnotationPanel().adjustPanelHeight() + 3;
1054 pagesHigh /= pheight;
1056 if (pi >= (pagesWide * pagesHigh))
1058 return Printable.NO_SUCH_PAGE;
1062 if (isMultiGraphics)
1064 pg[G1].translate(0, 0);
1065 getScalePanel().drawScale(pg[G1], startRes, endRes,
1066 pwidth - idWidth, scaleHeight);
1067 pg[G1].translate(-idWidth, scaleHeight);
1071 pg[G0].translate(idWidth, 0);
1072 getScalePanel().drawScale(pg[G0], startRes, endRes, pwidth - idWidth,
1074 pg[G0].translate(-idWidth, scaleHeight);
1079 Color currentColor = null;
1080 Color currentTextColor = null;
1082 if (isMultiGraphics)
1084 pg[G0].translate(0, scaleHeight);
1086 pg[G0].setFont(getIdPanel().getIdCanvas().getIdfont());
1089 for (int i = startSeq; i < endSeq; i++)
1091 seq = av.getAlignment().getSequenceAt(i);
1092 if ((av.getSelectionGroup() != null)
1093 && av.getSelectionGroup().getSequences(null).contains(seq))
1095 currentColor = Color.gray;
1096 currentTextColor = Color.black;
1100 currentColor = av.getSequenceColour(seq);
1101 currentTextColor = Color.black;
1104 pg[G0].setColor(currentColor);
1105 pg[G0].fillRect(0, (i - startSeq) * av.getCharHeight(), idWidth,
1106 av.getCharHeight());
1108 pg[G0].setColor(currentTextColor);
1111 if (av.isRightAlignIds())
1113 fm = pg[G0].getFontMetrics();
1115 - fm.stringWidth(seq.getDisplayId(av.getShowJVSuffix()))
1119 pg[G0].drawString(seq.getDisplayId(av.getShowJVSuffix()), xPos,
1120 (((i - startSeq) * av.getCharHeight()) + av.getCharHeight())
1121 - (av.getCharHeight() / 5));
1124 pg[G0].setFont(av.getFont());
1127 // draw main sequence panel
1128 pg[G0].translate(idWidth, 0);
1129 if (isMultiGraphics)
1131 pg[G1].translate(idWidth, 0);
1132 getSeqPanel().seqCanvas.drawPanel(pg[G1], startRes, endRes,
1133 startSeq, endSeq, 0);
1137 getSeqPanel().seqCanvas.drawPanel(pg[G0], startRes, endRes, startSeq,
1141 if (av.isShowAnnotation() && (endSeq == av.getAlignment().getHeight()))
1143 // draw annotation label - need to offset for current scroll position
1144 int offset = -getAlabels().getScrollOffset();
1145 pg[G0].translate(0, offset);
1146 pg[G0].translate(-idWidth - 3,
1147 (endSeq - startSeq) * av.getCharHeight() + 3);
1148 getAlabels().drawComponent(pg[G0], idWidth);
1149 pg[G0].translate(idWidth + 3, 0);
1150 pg[G0].translate(0, -offset);
1151 if (isMultiGraphics)
1153 // draw annotation - need to offset for current scroll position
1154 pg[G1].translate(0, offset);
1155 pg[G1].translate(-idWidth - 3,
1156 (endSeq - startSeq) * av.getCharHeight() + 3);
1157 pg[G1].translate(idWidth + 3, 0);
1158 getAnnotationPanel().renderer.drawComponent(getAnnotationPanel(),
1159 av, pg[G1], -1, startRes, endRes + 1);
1160 pg[G1].translate(0, -offset);
1164 getAnnotationPanel().renderer.drawComponent(getAnnotationPanel(),
1165 av, pg[G0], -1, startRes, endRes + 1);
1166 pg[G0].translate(0, -offset);
1170 return Printable.PAGE_EXISTS;
1185 * @return DOCUMENT ME!
1187 * @throws PrinterException
1190 public int printWrappedAlignment(int pwidth, int pheight, int pi,
1191 Graphics pg) throws PrinterException
1193 int annotationHeight = 0;
1194 AnnotationLabels labels = null;
1195 if (av.isShowAnnotation())
1197 annotationHeight = getAnnotationPanel().adjustPanelHeight();
1198 labels = new AnnotationLabels(av);
1201 int hgap = av.getCharHeight();
1202 if (av.getScaleAboveWrapped())
1204 hgap += av.getCharHeight();
1207 int cHeight = av.getAlignment().getHeight() * av.getCharHeight() + hgap
1210 int idWidth = getVisibleIdWidth(false);
1212 int maxwidth = av.getAlignment().getWidth();
1213 if (av.hasHiddenColumns())
1215 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
1218 int resWidth = getSeqPanel().seqCanvas.getWrappedCanvasWidth(pwidth
1221 int totalHeight = cHeight * (maxwidth / resWidth + 1);
1223 pg.setColor(Color.white);
1224 pg.fillRect(0, 0, pwidth, pheight);
1225 pg.setFont(av.getFont());
1229 pg.setColor(Color.black);
1231 pg.translate(0, -pi * pheight);
1233 pg.setClip(0, pi * pheight, pwidth, pheight);
1239 for (int i = 0; i < av.getAlignment().getHeight(); i++)
1241 pg.setFont(getIdPanel().getIdCanvas().getIdfont());
1242 SequenceI s = av.getAlignment().getSequenceAt(i);
1243 String string = s.getDisplayId(av.getShowJVSuffix());
1245 if (av.isRightAlignIds())
1247 FontMetrics fm = pg.getFontMetrics();
1248 xPos = idWidth - fm.stringWidth(string) - 4;
1250 pg.drawString(string, xPos,
1251 ((i * av.getCharHeight()) + ypos + av.getCharHeight())
1252 - (av.getCharHeight() / 5));
1257 ypos + (av.getAlignment().getHeight() * av.getCharHeight()));
1259 pg.setFont(av.getFont());
1260 labels.drawComponent(pg, idWidth);
1264 - (av.getAlignment().getHeight() * av
1269 } while (ypos < totalHeight);
1271 pg.translate(idWidth, 0);
1273 getSeqPanel().seqCanvas.drawWrappedPanel(pg, pwidth - idWidth,
1276 if ((pi * pheight) < totalHeight)
1278 return Printable.PAGE_EXISTS;
1283 return Printable.NO_SUCH_PAGE;
1288 * get current sequence ID panel width, or nominal value if panel were to be
1289 * displayed using default settings
1293 public int getVisibleIdWidth()
1295 return getVisibleIdWidth(true);
1299 * get current sequence ID panel width, or nominal value if panel were to be
1300 * displayed using default settings
1303 * indicate if the Id width for onscreen or offscreen display should
1307 public int getVisibleIdWidth(boolean onscreen)
1309 // see if rendering offscreen - check preferences and calc width accordingly
1310 if (!onscreen && Cache.getDefault("FIGURE_AUTOIDWIDTH", false))
1312 return calculateIdWidth(-1).width + 4;
1314 Integer idwidth = null;
1316 || (idwidth = Cache.getIntegerProperty("FIGURE_FIXEDIDWIDTH")) == null)
1318 return (getIdPanel().getWidth() > 0 ? getIdPanel().getWidth()
1319 : calculateIdWidth().width + 4);
1321 return idwidth.intValue() + 4;
1324 void makeAlignmentImage(jalview.util.ImageMaker.TYPE type, File file)
1326 int boarderBottomOffset = 5;
1327 long pSessionId = System.currentTimeMillis();
1328 headless = (System.getProperty("java.awt.headless") != null && System
1329 .getProperty("java.awt.headless").equals("true"));
1330 if (alignFrame != null && !headless)
1334 alignFrame.setProgressBar(MessageManager.formatMessage(
1335 "status.saving_file", new Object[] { type.getLabel() }),
1341 AlignmentDimension aDimension = getAlignmentDimension();
1344 jalview.util.ImageMaker im;
1345 final String imageAction, imageTitle;
1346 if (type == jalview.util.ImageMaker.TYPE.PNG)
1348 imageAction = "Create PNG image from alignment";
1351 else if (type == jalview.util.ImageMaker.TYPE.EPS)
1353 imageAction = "Create EPS file from alignment";
1354 imageTitle = alignFrame.getTitle();
1358 imageAction = "Create SVG file from alignment";
1359 imageTitle = alignFrame.getTitle();
1362 im = new jalview.util.ImageMaker(this, type, imageAction,
1363 aDimension.getWidth(), aDimension.getHeight()
1364 + boarderBottomOffset, file, imageTitle,
1365 alignFrame, pSessionId, headless);
1366 if (av.getWrapAlignment())
1368 if (im.getGraphics() != null)
1370 printWrappedAlignment(aDimension.getWidth(),
1371 aDimension.getHeight() + boarderBottomOffset, 0,
1378 if (im.getGraphics() != null)
1380 printUnwrapped(aDimension.getWidth(), aDimension.getHeight(),
1381 0, im.getGraphics());
1386 } catch (OutOfMemoryError err)
1389 System.out.println("########################\n" + "OUT OF MEMORY "
1390 + file + "\n" + "########################");
1391 new OOMWarning("Creating Image for " + file, err);
1392 // System.out.println("Create IMAGE: " + err);
1393 } catch (Exception ex)
1395 ex.printStackTrace();
1403 public AlignmentDimension getAlignmentDimension()
1405 int maxwidth = av.getAlignment().getWidth();
1406 if (av.hasHiddenColumns())
1408 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth);
1411 int height = ((av.getAlignment().getHeight() + 1) * av.getCharHeight())
1412 + getScalePanel().getHeight();
1413 int width = getVisibleIdWidth(false) + (maxwidth * av.getCharWidth());
1415 if (av.getWrapAlignment())
1417 height = getWrappedHeight();
1420 // need to obtain default alignment width and then add in any
1421 // additional allowance for id margin
1422 // this duplicates the calculation in getWrappedHeight but adjusts for
1424 width = alignFrame.getWidth() - vscroll.getPreferredSize().width
1425 - alignFrame.getInsets().left
1426 - alignFrame.getInsets().right - getVisibleIdWidth()
1427 + getVisibleIdWidth(false);
1431 width = getSeqPanel().getWidth() + getVisibleIdWidth(false);
1435 else if (av.isShowAnnotation())
1437 height += getAnnotationPanel().adjustPanelHeight() + 3;
1439 return new AlignmentDimension(width, height);
1446 public void makeEPS(File epsFile)
1448 makeAlignmentImage(jalview.util.ImageMaker.TYPE.EPS, epsFile);
1454 public void makePNG(File pngFile)
1456 makeAlignmentImage(jalview.util.ImageMaker.TYPE.PNG, pngFile);
1459 public void makeSVG(File svgFile)
1461 makeAlignmentImage(jalview.util.ImageMaker.TYPE.SVG, svgFile);
1464 public void makePNGImageMap(File imgMapFile, String imageName)
1466 // /////ONLY WORKS WITH NONE WRAPPED ALIGNMENTS
1467 // ////////////////////////////////////////////
1468 int idWidth = getVisibleIdWidth(false);
1469 FontMetrics fm = getFontMetrics(av.getFont());
1470 int scaleHeight = av.getCharHeight() + fm.getDescent();
1473 // ////////////////////////////////
1474 if (imgMapFile != null)
1478 int s, sSize = av.getAlignment().getHeight(), res, alwidth = av
1479 .getAlignment().getWidth(), g, gSize, f, fSize, sy;
1480 StringBuffer text = new StringBuffer();
1481 PrintWriter out = new PrintWriter(new FileWriter(imgMapFile));
1482 out.println(jalview.io.HTMLOutput.getImageMapHTML());
1483 out.println("<img src=\"" + imageName
1484 + "\" border=\"0\" usemap=\"#Map\" >"
1485 + "<map name=\"Map\">");
1487 for (s = 0; s < sSize; s++)
1489 sy = s * av.getCharHeight() + scaleHeight;
1491 SequenceI seq = av.getAlignment().getSequenceAt(s);
1492 SequenceFeature[] features = seq.getSequenceFeatures();
1493 SequenceGroup[] groups = av.getAlignment().findAllGroups(seq);
1494 for (res = 0; res < alwidth; res++)
1496 text = new StringBuffer();
1497 String triplet = null;
1498 if (av.getAlignment().isNucleotide())
1500 triplet = ResidueProperties.nucleotideName.get(seq
1501 .getCharAt(res) + "");
1505 triplet = ResidueProperties.aa2Triplet.get(seq.getCharAt(res)
1509 if (triplet == null)
1514 int alIndex = seq.findPosition(res);
1515 gSize = groups.length;
1516 for (g = 0; g < gSize; g++)
1518 if (text.length() < 1)
1520 text.append("<area shape=\"rect\" coords=\""
1521 + (idWidth + res * av.getCharWidth()) + "," + sy
1522 + "," + (idWidth + (res + 1) * av.getCharWidth())
1523 + "," + (av.getCharHeight() + sy) + "\""
1524 + " onMouseOver=\"toolTip('" + alIndex + " "
1528 if (groups[g].getStartRes() < res
1529 && groups[g].getEndRes() > res)
1531 text.append("<br><em>" + groups[g].getName() + "</em>");
1535 if (features != null)
1537 if (text.length() < 1)
1539 text.append("<area shape=\"rect\" coords=\""
1540 + (idWidth + res * av.getCharWidth()) + "," + sy
1541 + "," + (idWidth + (res + 1) * av.getCharWidth())
1542 + "," + (av.getCharHeight() + sy) + "\""
1543 + " onMouseOver=\"toolTip('" + alIndex + " "
1546 fSize = features.length;
1547 for (f = 0; f < fSize; f++)
1550 if ((features[f].getBegin() <= seq.findPosition(res))
1551 && (features[f].getEnd() >= seq.findPosition(res)))
1553 if (features[f].getType().equals("disulfide bond"))
1555 if (features[f].getBegin() == seq.findPosition(res)
1556 || features[f].getEnd() == seq
1559 text.append("<br>disulfide bond "
1560 + features[f].getBegin() + ":"
1561 + features[f].getEnd());
1566 text.append("<br>");
1567 text.append(features[f].getType());
1568 if (features[f].getDescription() != null
1569 && !features[f].getType().equals(
1570 features[f].getDescription()))
1572 text.append(" " + features[f].getDescription());
1575 if (features[f].getValue("status") != null)
1577 text.append(" (" + features[f].getValue("status")
1585 if (text.length() > 1)
1587 text.append("')\"; onMouseOut=\"toolTip()\"; href=\"#\">");
1588 out.println(text.toString());
1592 out.println("</map></body></html>");
1595 } catch (Exception ex)
1597 ex.printStackTrace();
1599 } // /////////END OF IMAGE MAP
1603 int getWrappedHeight()
1605 int seqPanelWidth = getSeqPanel().seqCanvas.getWidth();
1607 if (System.getProperty("java.awt.headless") != null
1608 && System.getProperty("java.awt.headless").equals("true"))
1610 seqPanelWidth = alignFrame.getWidth() - getVisibleIdWidth()
1611 - vscroll.getPreferredSize().width
1612 - alignFrame.getInsets().left - alignFrame.getInsets().right;
1615 int chunkWidth = getSeqPanel().seqCanvas
1616 .getWrappedCanvasWidth(seqPanelWidth);
1618 int hgap = av.getCharHeight();
1619 if (av.getScaleAboveWrapped())
1621 hgap += av.getCharHeight();
1624 int annotationHeight = 0;
1625 if (av.isShowAnnotation())
1627 annotationHeight = getAnnotationPanel().adjustPanelHeight();
1630 int cHeight = av.getAlignment().getHeight() * av.getCharHeight() + hgap
1633 int maxwidth = av.getAlignment().getWidth();
1634 if (av.hasHiddenColumns())
1636 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
1639 int height = ((maxwidth / chunkWidth) + 1) * cHeight;
1645 * close the panel - deregisters all listeners and nulls any references to
1648 public void closePanel()
1650 PaintRefresher.RemoveComponent(getSeqPanel().seqCanvas);
1651 PaintRefresher.RemoveComponent(getIdPanel().getIdCanvas());
1652 PaintRefresher.RemoveComponent(this);
1655 * try to ensure references are nulled
1657 if (annotationPanel != null)
1659 annotationPanel.dispose();
1664 av.removePropertyChangeListener(propertyChangeListener);
1665 jalview.structure.StructureSelectionManager ssm = av
1666 .getStructureSelectionManager();
1667 ssm.removeStructureViewerListener(getSeqPanel(), null);
1668 ssm.removeSelectionListener(getSeqPanel());
1669 ssm.removeCommandListener(av);
1670 ssm.removeStructureViewerListener(getSeqPanel(), null);
1671 ssm.removeSelectionListener(getSeqPanel());
1677 if (Cache.log.isDebugEnabled())
1679 Cache.log.warn("Closing alignment panel which is already closed.");
1685 * hides or shows dynamic annotation rows based on groups and av state flags
1687 public void updateAnnotation()
1689 updateAnnotation(false, false);
1692 public void updateAnnotation(boolean applyGlobalSettings)
1694 updateAnnotation(applyGlobalSettings, false);
1697 public void updateAnnotation(boolean applyGlobalSettings,
1698 boolean preserveNewGroupSettings)
1700 av.updateGroupAnnotationSettings(applyGlobalSettings,
1701 preserveNewGroupSettings);
1702 adjustAnnotationHeight();
1706 public AlignmentI getAlignment()
1708 return av == null ? null : av.getAlignment();
1712 public String getViewName()
1718 * Make/Unmake this alignment panel the current input focus
1722 public void setSelected(boolean b)
1726 if (alignFrame.getSplitViewContainer() != null)
1729 * bring enclosing SplitFrame to front first if there is one
1731 ((SplitFrame) alignFrame.getSplitViewContainer()).setSelected(b);
1733 alignFrame.setSelected(b);
1734 } catch (Exception ex)
1740 alignFrame.setDisplayedView(this);
1745 public StructureSelectionManager getStructureSelectionManager()
1747 return av.getStructureSelectionManager();
1751 public void raiseOOMWarning(String string, OutOfMemoryError error)
1753 new OOMWarning(string, error, this);
1757 public jalview.api.FeatureRenderer cloneFeatureRenderer()
1760 return new FeatureRenderer(this);
1764 public jalview.api.FeatureRenderer getFeatureRenderer()
1766 return seqPanel.seqCanvas.getFeatureRenderer();
1769 public void updateFeatureRenderer(
1770 jalview.renderer.seqfeatures.FeatureRenderer fr)
1772 fr.transferSettings(getSeqPanel().seqCanvas.getFeatureRenderer());
1775 public void updateFeatureRendererFrom(jalview.api.FeatureRenderer fr)
1777 if (getSeqPanel().seqCanvas.getFeatureRenderer() != null)
1779 getSeqPanel().seqCanvas.getFeatureRenderer().transferSettings(fr);
1783 public ScalePanel getScalePanel()
1788 public void setScalePanel(ScalePanel scalePanel)
1790 this.scalePanel = scalePanel;
1793 public SeqPanel getSeqPanel()
1798 public void setSeqPanel(SeqPanel seqPanel)
1800 this.seqPanel = seqPanel;
1803 public AnnotationPanel getAnnotationPanel()
1805 return annotationPanel;
1808 public void setAnnotationPanel(AnnotationPanel annotationPanel)
1810 this.annotationPanel = annotationPanel;
1813 public AnnotationLabels getAlabels()
1818 public void setAlabels(AnnotationLabels alabels)
1820 this.alabels = alabels;
1823 public IdPanel getIdPanel()
1828 public void setIdPanel(IdPanel idPanel)
1830 this.idPanel = idPanel;
1834 * Follow a scrolling change in the (cDNA/Protein) complementary alignment.
1835 * The aim is to keep the two alignments 'lined up' on their centre columns.
1838 * holds mapped region(s) of this alignment that we are scrolling
1839 * 'to'; may be modified for sequence offset by this method
1840 * @param verticalOffset
1841 * the number of visible sequences to show above the mapped region
1843 public void scrollToCentre(SearchResults sr, int verticalOffset)
1846 * To avoid jumpy vertical scrolling (if some sequences are gapped or not
1847 * mapped), we can make the scroll-to location a sequence above the one
1850 SequenceI mappedTo = sr.getResultSequence(0);
1851 List<SequenceI> seqs = av.getAlignment().getSequences();
1854 * This is like AlignmentI.findIndex(seq) but here we are matching the
1855 * dataset sequence not the aligned sequence
1857 boolean matched = false;
1858 for (SequenceI seq : seqs)
1860 if (mappedTo == seq.getDatasetSequence())
1868 return; // failsafe, shouldn't happen
1872 * Scroll to position but centring the target residue.
1874 scrollToPosition(sr, verticalOffset, true, true);
1878 * Set a flag to say do not scroll any (cDNA/protein) complement.
1882 protected void setDontScrollComplement(boolean b)
1884 this.dontScrollComplement = b;
1887 protected boolean isDontScrollComplement()
1889 return this.dontScrollComplement;