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;
38 import java.awt.BorderLayout;
39 import java.awt.Color;
40 import java.awt.Container;
41 import java.awt.Dimension;
43 import java.awt.FontMetrics;
44 import java.awt.Graphics;
45 import java.awt.Insets;
46 import java.awt.event.AdjustmentEvent;
47 import java.awt.event.AdjustmentListener;
48 import java.awt.print.PageFormat;
49 import java.awt.print.Printable;
50 import java.awt.print.PrinterException;
51 import java.beans.PropertyChangeEvent;
52 import java.beans.PropertyChangeListener;
54 import java.io.FileWriter;
55 import java.io.PrintWriter;
56 import java.util.List;
58 import javax.swing.SwingUtilities;
64 * @version $Revision: 1.161 $
66 public class AlignmentPanel extends GAlignmentPanel implements
67 AdjustmentListener, Printable, AlignmentViewPanel
69 public AlignViewport av;
71 OverviewPanel overviewPanel;
73 private SeqPanel seqPanel;
75 private IdPanel idPanel;
77 private boolean headless;
78 IdwidthAdjuster idwidthAdjuster;
81 public AlignFrame alignFrame;
83 private ScalePanel scalePanel;
85 private AnnotationPanel annotationPanel;
87 private AnnotationLabels alabels;
89 // this value is set false when selection area being dragged
90 boolean fastPaint = true;
97 * Flag set while scrolling to follow complementary cDNA/protein scroll. When
98 * true, suppresses invoking the same method recursively.
100 private boolean followingComplementScroll;
103 * Creates a new AlignmentPanel object.
108 public AlignmentPanel(AlignFrame af, final AlignViewport av)
112 setSeqPanel(new SeqPanel(av, this));
113 setIdPanel(new IdPanel(av, this));
115 setScalePanel(new ScalePanel(av, this));
117 idPanelHolder.add(getIdPanel(), BorderLayout.CENTER);
118 idwidthAdjuster = new IdwidthAdjuster(this);
119 idSpaceFillerPanel1.add(idwidthAdjuster, BorderLayout.CENTER);
121 setAnnotationPanel(new AnnotationPanel(this));
122 setAlabels(new AnnotationLabels(this));
124 annotationScroller.setViewportView(getAnnotationPanel());
125 annotationSpaceFillerHolder.add(getAlabels(), BorderLayout.CENTER);
127 scalePanelHolder.add(getScalePanel(), BorderLayout.CENTER);
128 seqPanelHolder.add(getSeqPanel(), BorderLayout.CENTER);
130 setScrollValues(0, 0);
132 hscroll.addAdjustmentListener(this);
133 vscroll.addAdjustmentListener(this);
135 final AlignmentPanel ap = this;
136 av.addPropertyChangeListener(new PropertyChangeListener()
138 public void propertyChange(PropertyChangeEvent evt)
140 if (evt.getPropertyName().equals("alignment"))
142 PaintRefresher.Refresh(ap, av.getSequenceSetId(), true, true);
148 adjustAnnotationHeight();
153 public AlignViewportI getAlignViewport()
157 public void alignmentChanged()
159 av.alignmentChanged(this);
161 alignFrame.updateEditMenuBar();
163 paintAlignment(true);
170 public void fontChanged()
172 // set idCanvas bufferedImage to null
173 // to prevent drawing old image
174 FontMetrics fm = getFontMetrics(av.getFont());
176 scalePanelHolder.setPreferredSize(new Dimension(10, av.getCharHeight()
178 idSpaceFillerPanel1.setPreferredSize(new Dimension(10, av
182 getIdPanel().getIdCanvas().gg = null;
183 getSeqPanel().seqCanvas.img = null;
184 getAnnotationPanel().adjustPanelHeight();
186 Dimension d = calculateIdWidth();
188 d.setSize(d.width + 4, d.height);
189 getIdPanel().getIdCanvas().setPreferredSize(d);
190 hscrollFillerPanel.setPreferredSize(d);
192 if (overviewPanel != null)
194 overviewPanel.setBoxPosition();
196 if (this.alignFrame.getSplitViewContainer() != null)
198 ((SplitFrame) this.alignFrame.getSplitViewContainer()).adjustLayout();
205 * Calculate the width of the alignment labels based on the displayed names
206 * and any bounds on label width set in preferences.
208 * @return Dimension giving the maximum width of the alignment label panel
209 * that should be used.
211 public Dimension calculateIdWidth()
213 // calculate sensible default width when no preference is available
215 if (av.getIdWidth() < 0)
217 int afwidth = (alignFrame != null ? alignFrame.getWidth() : 300);
218 int maxwidth = Math.max(20, Math.min(afwidth - 200, 2 * afwidth / 3));
219 r = calculateIdWidth(maxwidth);
220 av.setIdWidth(r.width);
225 r.width = av.getIdWidth();
232 * Calculate the width of the alignment labels based on the displayed names
233 * and any bounds on label width set in preferences.
236 * -1 or maximum width allowed for IdWidth
237 * @return Dimension giving the maximum width of the alignment label panel
238 * that should be used.
240 public Dimension calculateIdWidth(int maxwidth)
242 Container c = new Container();
244 FontMetrics fm = c.getFontMetrics(new Font(av.font.getName(),
245 Font.ITALIC, av.font.getSize()));
247 AlignmentI al = av.getAlignment();
252 while ((i < al.getHeight()) && (al.getSequenceAt(i) != null))
254 SequenceI s = al.getSequenceAt(i);
256 id = s.getDisplayId(av.getShowJVSuffix());
258 if (fm.stringWidth(id) > idWidth)
260 idWidth = fm.stringWidth(id);
266 // Also check annotation label widths
269 if (al.getAlignmentAnnotation() != null)
271 fm = c.getFontMetrics(getAlabels().getFont());
273 while (i < al.getAlignmentAnnotation().length)
275 String label = al.getAlignmentAnnotation()[i].label;
277 if (fm.stringWidth(label) > idWidth)
279 idWidth = fm.stringWidth(label);
286 return new Dimension(maxwidth < 0 ? idWidth : Math.min(maxwidth,
291 * Highlight the given results on the alignment.
294 public void highlightSearchResults(SearchResults results)
296 scrollToPosition(results);
297 getSeqPanel().seqCanvas.highlightSearchResults(results);
301 * Scroll the view to show the position of the highlighted region in results
302 * (if any) and redraw the overview
306 public boolean scrollToPosition(SearchResults results)
308 return scrollToPosition(results, 0, true, false);
312 * Scroll the view to show the position of the highlighted region in results
315 * @param searchResults
316 * @param redrawOverview
319 public boolean scrollToPosition(SearchResults searchResults, boolean redrawOverview)
321 return scrollToPosition(searchResults, 0, redrawOverview, false);
325 * Scroll the view to show the position of the highlighted region in results
329 * @param verticalOffset
330 * if greater than zero, allows scrolling to a position below the
331 * first displayed sequence
332 * @param redrawOverview
333 * - when set, the overview will be recalculated (takes longer)
335 * if true, try to centre the search results horizontally in the view
336 * @return false if results were not found
338 public boolean scrollToPosition(SearchResults results,
340 boolean redrawOverview, boolean centre)
342 int startv, endv, starts, ends;
343 // TODO: properly locate search results in view when large numbers of hidden
344 // columns exist before highlighted region
345 // do we need to scroll the panel?
346 // TODO: tons of nullpointerexceptions raised here.
347 if (results != null && results.getSize() > 0 && av != null
348 && av.getAlignment() != null)
350 int seqIndex = av.getAlignment().findIndex(results);
355 SequenceI seq = av.getAlignment().getSequenceAt(seqIndex);
357 int[] r = results.getResults(seq, 0, av.getAlignment().getWidth());
364 // System.err.println("Seq : "+seqIndex+" Scroll to "+start+","+end); //
368 * To centre results, scroll to positions half the visible width
369 * left/right of the start/end positions
373 int offset = (av.getEndRes() - av.getStartRes() + 1) / 2 - 1;
374 start = Math.max(start - offset, 0);
375 end = Math.min(end + offset, seq.getEnd() - 1);
381 if (end == seq.getEnd())
385 if (av.hasHiddenColumns())
387 start = av.getColumnSelection().findColumnPosition(start);
388 end = av.getColumnSelection().findColumnPosition(end);
391 if (!av.getColumnSelection().isVisible(r[0]))
393 // don't scroll - position isn't visible
400 * allow for offset of target sequence (actually scroll to one above it)
402 seqIndex = Math.max(0, seqIndex - verticalOffset);
404 if (!av.getWrapAlignment())
406 if ((startv = av.getStartRes()) >= start)
409 * Scroll left to make start of search results visible
411 // setScrollValues(start - 1, seqIndex); // plus one residue
412 setScrollValues(start, seqIndex);
414 else if ((endv = av.getEndRes()) <= end)
417 * Scroll right to make end of search results visible
419 // setScrollValues(startv + 1 + end - endv, seqIndex); // plus one
420 setScrollValues(startv + end - endv, seqIndex);
422 else if ((starts = av.getStartSeq()) > seqIndex)
425 * Scroll up to make start of search results visible
427 setScrollValues(av.getStartRes(), seqIndex);
429 else if ((ends = av.getEndSeq()) <= seqIndex)
432 * Scroll down to make end of search results visible
434 setScrollValues(av.getStartRes(), starts + seqIndex - ends + 1);
437 * Else results are already visible - no need to scroll
442 scrollToWrappedVisible(start);
445 if (redrawOverview && overviewPanel != null)
447 overviewPanel.setBoxPosition();
449 paintAlignment(redrawOverview);
453 void scrollToWrappedVisible(int res)
455 int cwidth = getSeqPanel().seqCanvas
456 .getWrappedCanvasWidth(getSeqPanel().seqCanvas.getWidth());
457 if (res < av.getStartRes() || res >= (av.getStartRes() + cwidth))
459 vscroll.setValue((res / cwidth));
460 av.startRes = vscroll.getValue() * cwidth;
468 * @return DOCUMENT ME!
470 public OverviewPanel getOverviewPanel()
472 return overviewPanel;
481 public void setOverviewPanel(OverviewPanel op)
489 * Hide or show annotation panel
492 public void setAnnotationVisible(boolean b)
494 if (!av.getWrapAlignment())
496 annotationSpaceFillerHolder.setVisible(b);
497 annotationScroller.setVisible(b);
503 * automatically adjust annotation panel height for new annotation whilst
504 * ensuring the alignment is still visible.
506 public void adjustAnnotationHeight()
508 // TODO: display vertical annotation scrollbar if necessary
509 // this is called after loading new annotation onto alignment
510 if (alignFrame.getHeight() == 0)
512 System.out.println("NEEDS FIXING");
514 validateAnnotationDimensions(true);
516 paintAlignment(true);
520 * calculate the annotation dimensions and refresh slider values accordingly.
521 * need to do repaints/notifys afterwards.
523 protected void validateAnnotationDimensions(boolean adjustPanelHeight)
525 int annotationHeight = getAnnotationPanel().adjustPanelHeight();
527 if (adjustPanelHeight)
529 int rowHeight = av.getCharHeight();
530 int alignmentHeight = rowHeight * av.getAlignment().getHeight();
533 * Estimate available height in the AlignFrame for alignment +
534 * annotations. Deduct an estimate of 75 for menu bar, scale panel,
535 * hscroll, status bar (as these are not laid out we can't inspect their
536 * actual heights). Insets gives borders including title bar.
538 Insets insets = alignFrame.getInsets();
539 int availableHeight = alignFrame.getHeight() - 75 - insets.top
543 * If not enough vertical space, maximize annotation height while keeping
544 * at least two rows of alignment visible
546 if (annotationHeight + alignmentHeight > availableHeight)
548 annotationHeight = Math.min(annotationHeight, availableHeight - 2
554 // maintain same window layout whilst updating sliders
555 annotationHeight = annotationScroller.getSize().height;
559 annotationScroller.setPreferredSize(new Dimension(annotationScroller
560 .getWidth(), annotationHeight));
562 annotationSpaceFillerHolder.setPreferredSize(new Dimension(
563 annotationSpaceFillerHolder.getWidth(), annotationHeight));
564 annotationScroller.validate();
565 annotationScroller.addNotify();
569 * update alignment layout for viewport settings
574 public void updateLayout()
577 setAnnotationVisible(av.isShowAnnotation());
578 boolean wrap = av.getWrapAlignment();
580 scalePanelHolder.setVisible(!wrap);
581 hscroll.setVisible(!wrap);
582 idwidthAdjuster.setVisible(!wrap);
586 annotationScroller.setVisible(false);
587 annotationSpaceFillerHolder.setVisible(false);
589 else if (av.isShowAnnotation())
591 annotationScroller.setVisible(true);
592 annotationSpaceFillerHolder.setVisible(true);
595 idSpaceFillerPanel1.setVisible(!wrap);
600 // return value is true if the scroll is valid
601 public boolean scrollUp(boolean up)
605 if (vscroll.getValue() < 1)
611 vscroll.setValue(vscroll.getValue() - 1);
615 if ((vextent + vscroll.getValue()) >= av.getAlignment().getHeight())
621 vscroll.setValue(vscroll.getValue() + 1);
635 * @return DOCUMENT ME!
637 public boolean scrollRight(boolean right)
641 if (hscroll.getValue() < 1)
647 hscroll.setValue(hscroll.getValue() - 1);
651 if ((hextent + hscroll.getValue()) >= av.getAlignment().getWidth())
657 hscroll.setValue(hscroll.getValue() + 1);
666 * Adjust row/column scrollers to show a visible position in the alignment.
669 * visible column to scroll to DOCUMENT ME!
671 * visible row to scroll to
674 public void setScrollValues(int x, int y)
676 // System.err.println("Scroll to "+x+","+y);
677 if (av == null || av.getAlignment() == null)
681 int width = av.getAlignment().getWidth();
682 int height = av.getAlignment().getHeight();
684 if (av.hasHiddenColumns())
686 width = av.getColumnSelection().findColumnPosition(width);
689 av.setEndRes((x + (getSeqPanel().seqCanvas.getWidth() / av
690 .getCharWidth())) - 1);
692 hextent = getSeqPanel().seqCanvas.getWidth() / av.getCharWidth();
693 vextent = getSeqPanel().seqCanvas.getHeight() / av.getCharHeight();
700 if (vextent > height)
705 if ((hextent + x) > width)
710 if ((vextent + y) > height)
712 y = height - vextent;
725 hscroll.setValues(x, hextent, 0, width);
726 vscroll.setValues(y, vextent, 0, height);
735 public void adjustmentValueChanged(AdjustmentEvent evt)
737 int oldX = av.getStartRes();
738 int oldY = av.getStartSeq();
740 if (evt.getSource() == hscroll)
742 int x = hscroll.getValue();
744 av.setEndRes((x + (getSeqPanel().seqCanvas.getWidth() / av.getCharWidth())) - 1);
747 if (evt.getSource() == vscroll)
749 int offy = vscroll.getValue();
751 if (av.getWrapAlignment())
755 int rowSize = getSeqPanel().seqCanvas
756 .getWrappedCanvasWidth(getSeqPanel().seqCanvas.getWidth());
757 av.setStartRes(offy * rowSize);
758 av.setEndRes((offy + 1) * rowSize);
762 // This is only called if file loaded is a jar file that
763 // was wrapped when saved and user has wrap alignment true
764 // as preference setting
765 SwingUtilities.invokeLater(new Runnable()
769 setScrollValues(av.getStartRes(), av.getStartSeq());
776 av.setStartSeq(offy);
778 + (getSeqPanel().seqCanvas.getHeight() / av.getCharHeight()));
782 if (overviewPanel != null)
784 overviewPanel.setBoxPosition();
787 int scrollX = av.startRes - oldX;
788 int scrollY = av.startSeq - oldY;
790 if (av.getWrapAlignment() || !fastPaint)
796 // Make sure we're not trying to draw a panel
797 // larger than the visible window
798 if (scrollX > av.endRes - av.startRes)
800 scrollX = av.endRes - av.startRes;
802 else if (scrollX < av.startRes - av.endRes)
804 scrollX = av.startRes - av.endRes;
807 if (scrollX != 0 || scrollY != 0)
809 getIdPanel().getIdCanvas().fastPaint(scrollY);
810 getSeqPanel().seqCanvas.fastPaint(scrollX, scrollY);
811 getScalePanel().repaint();
813 if (av.isShowAnnotation() && scrollX != 0)
815 getAnnotationPanel().fastPaint(scrollX);
820 * If there is one, scroll the (Protein/cDNA) complementary alignment to
821 * match, unless we are ourselves doing that.
823 if (isFollowingComplementScroll())
825 setFollowingComplementScroll(false);
829 av.scrollComplementaryAlignment();
834 * Repaint the alignment including the annotations and overview panels (if
837 public void paintAlignment(boolean updateOverview)
839 final AnnotationSorter sorter = new AnnotationSorter(getAlignment(),
840 av.isShowAutocalculatedAbove());
841 sorter.sort(getAlignment()
842 .getAlignmentAnnotation(),
843 av.getSortAnnotationsBy());
848 av.getStructureSelectionManager().sequenceColoursChanged(this);
850 if (overviewPanel != null)
852 overviewPanel.updateOverviewImage();
863 public void paintComponent(Graphics g)
867 Dimension d = getIdPanel().getIdCanvas().getPreferredSize();
868 idPanelHolder.setPreferredSize(d);
869 hscrollFillerPanel.setPreferredSize(new Dimension(d.width, 12));
872 if (av.getWrapAlignment())
874 int maxwidth = av.getAlignment().getWidth();
876 if (av.hasHiddenColumns())
878 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
881 int canvasWidth = getSeqPanel().seqCanvas
882 .getWrappedCanvasWidth(getSeqPanel().seqCanvas.getWidth());
886 / getSeqPanel().seqCanvas
887 .getWrappedCanvasWidth(getSeqPanel().seqCanvas
889 vscroll.setMaximum(max);
890 vscroll.setUnitIncrement(1);
891 vscroll.setVisibleAmount(1);
896 setScrollValues(av.getStartRes(), av.getStartSeq());
910 * @return DOCUMENT ME!
912 * @throws PrinterException
915 public int print(Graphics pg, PageFormat pf, int pi)
916 throws PrinterException
918 pg.translate((int) pf.getImageableX(), (int) pf.getImageableY());
920 int pwidth = (int) pf.getImageableWidth();
921 int pheight = (int) pf.getImageableHeight();
923 if (av.getWrapAlignment())
925 return printWrappedAlignment(pg, pwidth, pheight, pi);
929 return printUnwrapped(pg, pwidth, pheight, pi);
945 * @return DOCUMENT ME!
947 * @throws PrinterException
950 public int printUnwrapped(Graphics pg, int pwidth, int pheight, int pi)
951 throws PrinterException
953 int idWidth = getVisibleIdWidth(false);
954 FontMetrics fm = getFontMetrics(av.getFont());
955 int scaleHeight = av.getCharHeight() + fm.getDescent();
957 pg.setColor(Color.white);
958 pg.fillRect(0, 0, pwidth, pheight);
959 pg.setFont(av.getFont());
961 // //////////////////////////////////
962 // / How many sequences and residues can we fit on a printable page?
963 int totalRes = (pwidth - idWidth) / av.getCharWidth();
965 int totalSeq = (pheight - scaleHeight) / av.getCharHeight() - 1;
967 int pagesWide = (av.getAlignment().getWidth() / totalRes) + 1;
969 // ///////////////////////////
970 // / Only print these sequences and residues on this page
973 // ///////////////////////////
974 // / Only print these sequences and residues on this page
977 // ///////////////////////////
978 // / Only print these sequences and residues on this page
981 // ///////////////////////////
982 // / Only print these sequences and residues on this page
984 startRes = (pi % pagesWide) * totalRes;
985 endRes = (startRes + totalRes) - 1;
987 if (endRes > (av.getAlignment().getWidth() - 1))
989 endRes = av.getAlignment().getWidth() - 1;
992 startSeq = (pi / pagesWide) * totalSeq;
993 endSeq = startSeq + totalSeq;
995 if (endSeq > av.getAlignment().getHeight())
997 endSeq = av.getAlignment().getHeight();
1000 int pagesHigh = ((av.getAlignment().getHeight() / totalSeq) + 1)
1003 if (av.isShowAnnotation())
1005 pagesHigh += getAnnotationPanel().adjustPanelHeight() + 3;
1008 pagesHigh /= pheight;
1010 if (pi >= (pagesWide * pagesHigh))
1012 return Printable.NO_SUCH_PAGE;
1016 pg.translate(idWidth, 0);
1017 getScalePanel().drawScale(pg, startRes, endRes, pwidth - idWidth,
1019 pg.translate(-idWidth, scaleHeight);
1023 Color currentColor = null;
1024 Color currentTextColor = null;
1026 pg.setFont(getIdPanel().getIdCanvas().getIdfont());
1029 for (int i = startSeq; i < endSeq; i++)
1031 seq = av.getAlignment().getSequenceAt(i);
1032 if ((av.getSelectionGroup() != null)
1033 && av.getSelectionGroup().getSequences(null).contains(seq))
1035 currentColor = Color.gray;
1036 currentTextColor = Color.black;
1040 currentColor = av.getSequenceColour(seq);
1041 currentTextColor = Color.black;
1044 pg.setColor(currentColor);
1045 pg.fillRect(0, (i - startSeq) * av.getCharHeight(), idWidth,
1046 av.getCharHeight());
1048 pg.setColor(currentTextColor);
1051 if (av.isRightAlignIds())
1053 fm = pg.getFontMetrics();
1055 - fm.stringWidth(seq.getDisplayId(av.getShowJVSuffix()))
1060 seq.getDisplayId(av.getShowJVSuffix()),
1062 (((i - startSeq) * av.getCharHeight()) + av.getCharHeight())
1063 - (av.getCharHeight() / 5));
1066 pg.setFont(av.getFont());
1068 // draw main sequence panel
1069 pg.translate(idWidth, 0);
1070 getSeqPanel().seqCanvas.drawPanel(pg, startRes, endRes, startSeq, endSeq, 0);
1072 if (av.isShowAnnotation() && (endSeq == av.getAlignment().getHeight()))
1074 // draw annotation - need to offset for current scroll position
1075 int offset = -getAlabels().getScrollOffset();
1076 pg.translate(0, offset);
1077 pg.translate(-idWidth - 3, (endSeq - startSeq) * av.getCharHeight()
1079 getAlabels().drawComponent(pg, idWidth);
1080 pg.translate(idWidth + 3, 0);
1081 getAnnotationPanel().renderer.drawComponent(getAnnotationPanel(), av,
1082 pg, -1, startRes, endRes + 1);
1083 pg.translate(0, -offset);
1086 return Printable.PAGE_EXISTS;
1101 * @return DOCUMENT ME!
1103 * @throws PrinterException
1106 public int printWrappedAlignment(Graphics pg, int pwidth, int pheight,
1107 int pi) throws PrinterException
1109 int annotationHeight = 0;
1110 AnnotationLabels labels = null;
1111 if (av.isShowAnnotation())
1113 annotationHeight = getAnnotationPanel().adjustPanelHeight();
1114 labels = new AnnotationLabels(av);
1117 int hgap = av.getCharHeight();
1118 if (av.getScaleAboveWrapped())
1120 hgap += av.getCharHeight();
1123 int cHeight = av.getAlignment().getHeight() * av.getCharHeight() + hgap
1126 int idWidth = getVisibleIdWidth(false);
1128 int maxwidth = av.getAlignment().getWidth();
1129 if (av.hasHiddenColumns())
1131 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
1134 int resWidth = getSeqPanel().seqCanvas.getWrappedCanvasWidth(pwidth
1137 int totalHeight = cHeight * (maxwidth / resWidth + 1);
1139 pg.setColor(Color.white);
1140 pg.fillRect(0, 0, pwidth, pheight);
1141 pg.setFont(av.getFont());
1145 pg.setColor(Color.black);
1147 pg.translate(0, -pi * pheight);
1149 pg.setClip(0, pi * pheight, pwidth, pheight);
1155 for (int i = 0; i < av.getAlignment().getHeight(); i++)
1157 pg.setFont(getIdPanel().getIdCanvas().getIdfont());
1158 SequenceI s = av.getAlignment().getSequenceAt(i);
1159 String string = s.getDisplayId(av.getShowJVSuffix());
1161 if (av.isRightAlignIds())
1163 FontMetrics fm = pg.getFontMetrics();
1164 xPos = idWidth - fm.stringWidth(string) - 4;
1166 pg.drawString(string, xPos,
1167 ((i * av.getCharHeight()) + ypos + av.getCharHeight())
1168 - (av.getCharHeight() / 5));
1172 pg.translate(-3, ypos
1173 + (av.getAlignment().getHeight() * av.getCharHeight()));
1175 pg.setFont(av.getFont());
1176 labels.drawComponent(pg, idWidth);
1177 pg.translate(+3, -ypos
1178 - (av.getAlignment().getHeight() * av
1183 } while (ypos < totalHeight);
1185 pg.translate(idWidth, 0);
1187 getSeqPanel().seqCanvas.drawWrappedPanel(pg, pwidth - idWidth, totalHeight,
1190 if ((pi * pheight) < totalHeight)
1192 return Printable.PAGE_EXISTS;
1197 return Printable.NO_SUCH_PAGE;
1202 * get current sequence ID panel width, or nominal value if panel were to be
1203 * displayed using default settings
1207 public int getVisibleIdWidth()
1209 return getVisibleIdWidth(true);
1213 * get current sequence ID panel width, or nominal value if panel were to be
1214 * displayed using default settings
1217 * indicate if the Id width for onscreen or offscreen display should
1221 public int getVisibleIdWidth(boolean onscreen)
1223 // see if rendering offscreen - check preferences and calc width accordingly
1224 if (!onscreen && Cache.getDefault("FIGURE_AUTOIDWIDTH", false))
1226 return calculateIdWidth(-1).width + 4;
1228 Integer idwidth = null;
1230 || (idwidth = Cache.getIntegerProperty("FIGURE_FIXEDIDWIDTH")) == null)
1232 return (getIdPanel().getWidth() > 0 ? getIdPanel().getWidth()
1233 : calculateIdWidth().width + 4);
1235 return idwidth.intValue() + 4;
1238 void makeAlignmentImage(jalview.util.ImageMaker.TYPE type, File file)
1240 long progress = System.currentTimeMillis();
1241 headless = (System.getProperty("java.awt.headless") != null && System
1242 .getProperty("java.awt.headless").equals("true"));
1243 if (alignFrame != null && !headless)
1245 alignFrame.setProgressBar(MessageManager.formatMessage(
1246 "status.saving_file", new Object[]
1247 { type.getLabel() }), progress);
1251 AlignmentDimension aDimension = getAlignmentDimension();
1254 jalview.util.ImageMaker im;
1255 final String imageAction, imageTitle;
1256 if (type == jalview.util.ImageMaker.TYPE.PNG)
1258 imageAction = "Create PNG image from alignment";
1261 else if (type == jalview.util.ImageMaker.TYPE.EPS)
1263 imageAction = "Create EPS file from alignment";
1264 imageTitle = alignFrame.getTitle();
1268 imageAction = "Create SVG file from alignment";
1269 imageTitle = alignFrame.getTitle();
1272 im = new jalview.util.ImageMaker(this, type, imageAction,
1273 aDimension.getWidth(), aDimension.getHeight(), file,
1275 if (av.getWrapAlignment())
1277 if (im.getGraphics() != null)
1279 printWrappedAlignment(im.getGraphics(), aDimension.getWidth(),
1280 aDimension.getHeight(), 0);
1286 if (im.getGraphics() != null)
1288 printUnwrapped(im.getGraphics(), aDimension.getWidth(),
1289 aDimension.getHeight(), 0);
1293 } catch (OutOfMemoryError err)
1296 System.out.println("########################\n" + "OUT OF MEMORY "
1297 + file + "\n" + "########################");
1298 new OOMWarning("Creating Image for " + file, err);
1299 // System.out.println("Create IMAGE: " + err);
1300 } catch (Exception ex)
1302 ex.printStackTrace();
1306 if (alignFrame != null && !headless)
1308 alignFrame.setProgressBar(MessageManager.getString("status.export_complete"), progress);
1313 public AlignmentDimension getAlignmentDimension()
1315 int maxwidth = av.getAlignment().getWidth();
1316 if (av.hasHiddenColumns())
1318 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth);
1321 int height = ((av.getAlignment().getHeight() + 1) * av.getCharHeight())
1322 + getScalePanel().getHeight();
1323 int width = getVisibleIdWidth(false) + (maxwidth * av.getCharWidth());
1325 if (av.getWrapAlignment())
1327 height = getWrappedHeight();
1330 // need to obtain default alignment width and then add in any
1331 // additional allowance for id margin
1332 // this duplicates the calculation in getWrappedHeight but adjusts for
1334 width = alignFrame.getWidth() - vscroll.getPreferredSize().width
1335 - alignFrame.getInsets().left
1336 - alignFrame.getInsets().right - getVisibleIdWidth()
1337 + getVisibleIdWidth(false);
1341 width = getSeqPanel().getWidth() + getVisibleIdWidth(false);
1345 else if (av.isShowAnnotation())
1347 height += getAnnotationPanel().adjustPanelHeight() + 3;
1349 return new AlignmentDimension(width, height);
1356 public void makeEPS(File epsFile)
1358 makeAlignmentImage(jalview.util.ImageMaker.TYPE.EPS, epsFile);
1364 public void makePNG(File pngFile)
1366 makeAlignmentImage(jalview.util.ImageMaker.TYPE.PNG, pngFile);
1369 public void makeSVG(File svgFile)
1371 makeAlignmentImage(jalview.util.ImageMaker.TYPE.SVG, svgFile);
1373 public void makePNGImageMap(File imgMapFile, String imageName)
1375 // /////ONLY WORKS WITH NONE WRAPPED ALIGNMENTS
1376 // ////////////////////////////////////////////
1377 int idWidth = getVisibleIdWidth(false);
1378 FontMetrics fm = getFontMetrics(av.getFont());
1379 int scaleHeight = av.getCharHeight() + fm.getDescent();
1382 // ////////////////////////////////
1383 if (imgMapFile != null)
1387 int s, sSize = av.getAlignment().getHeight(), res, alwidth = av
1388 .getAlignment().getWidth(), g, gSize, f, fSize, sy;
1389 StringBuffer text = new StringBuffer();
1390 PrintWriter out = new PrintWriter(new FileWriter(imgMapFile));
1391 out.println(jalview.io.HTMLOutput.getImageMapHTML());
1392 out.println("<img src=\"" + imageName
1393 + "\" border=\"0\" usemap=\"#Map\" >"
1394 + "<map name=\"Map\">");
1396 for (s = 0; s < sSize; s++)
1398 sy = s * av.getCharHeight() + scaleHeight;
1400 SequenceI seq = av.getAlignment().getSequenceAt(s);
1401 SequenceFeature[] features = seq.getSequenceFeatures();
1402 SequenceGroup[] groups = av.getAlignment().findAllGroups(seq);
1403 for (res = 0; res < alwidth; res++)
1405 text = new StringBuffer();
1406 String triplet = null;
1407 if (av.getAlignment().isNucleotide())
1409 triplet = ResidueProperties.nucleotideName.get(seq
1415 triplet = ResidueProperties.aa2Triplet.get(seq.getCharAt(res)
1419 if (triplet == null)
1424 int alIndex = seq.findPosition(res);
1425 gSize = groups.length;
1426 for (g = 0; g < gSize; g++)
1428 if (text.length() < 1)
1430 text.append("<area shape=\"rect\" coords=\""
1431 + (idWidth + res * av.getCharWidth()) + "," + sy
1432 + "," + (idWidth + (res + 1) * av.getCharWidth())
1434 + (av.getCharHeight() + sy) + "\""
1435 + " onMouseOver=\"toolTip('" + alIndex + " "
1439 if (groups[g].getStartRes() < res
1440 && groups[g].getEndRes() > res)
1442 text.append("<br><em>" + groups[g].getName() + "</em>");
1446 if (features != null)
1448 if (text.length() < 1)
1450 text.append("<area shape=\"rect\" coords=\""
1451 + (idWidth + res * av.getCharWidth()) + "," + sy
1452 + "," + (idWidth + (res + 1) * av.getCharWidth())
1454 + (av.getCharHeight() + sy) + "\""
1455 + " onMouseOver=\"toolTip('" + alIndex + " "
1458 fSize = features.length;
1459 for (f = 0; f < fSize; f++)
1462 if ((features[f].getBegin() <= seq.findPosition(res))
1463 && (features[f].getEnd() >= seq.findPosition(res)))
1465 if (features[f].getType().equals("disulfide bond"))
1467 if (features[f].getBegin() == seq.findPosition(res)
1468 || features[f].getEnd() == seq
1471 text.append("<br>disulfide bond "
1472 + features[f].getBegin() + ":"
1473 + features[f].getEnd());
1478 text.append("<br>");
1479 text.append(features[f].getType());
1480 if (features[f].getDescription() != null
1481 && !features[f].getType().equals(
1482 features[f].getDescription()))
1484 text.append(" " + features[f].getDescription());
1487 if (features[f].getValue("status") != null)
1489 text.append(" (" + features[f].getValue("status")
1497 if (text.length() > 1)
1499 text.append("')\"; onMouseOut=\"toolTip()\"; href=\"#\">");
1500 out.println(text.toString());
1504 out.println("</map></body></html>");
1507 } catch (Exception ex)
1509 ex.printStackTrace();
1511 } // /////////END OF IMAGE MAP
1515 int getWrappedHeight()
1517 int seqPanelWidth = getSeqPanel().seqCanvas.getWidth();
1519 if (System.getProperty("java.awt.headless") != null
1520 && System.getProperty("java.awt.headless").equals("true"))
1522 seqPanelWidth = alignFrame.getWidth() - getVisibleIdWidth()
1523 - vscroll.getPreferredSize().width
1524 - alignFrame.getInsets().left - alignFrame.getInsets().right;
1527 int chunkWidth = getSeqPanel().seqCanvas
1528 .getWrappedCanvasWidth(seqPanelWidth);
1530 int hgap = av.getCharHeight();
1531 if (av.getScaleAboveWrapped())
1533 hgap += av.getCharHeight();
1536 int annotationHeight = 0;
1537 if (av.isShowAnnotation())
1539 annotationHeight = getAnnotationPanel().adjustPanelHeight();
1542 int cHeight = av.getAlignment().getHeight() * av.getCharHeight() + hgap
1545 int maxwidth = av.getAlignment().getWidth();
1546 if (av.hasHiddenColumns())
1548 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
1551 int height = ((maxwidth / chunkWidth) + 1) * cHeight;
1557 * close the panel - deregisters all listeners and nulls any references to
1560 public void closePanel()
1562 PaintRefresher.RemoveComponent(getSeqPanel().seqCanvas);
1563 PaintRefresher.RemoveComponent(getIdPanel().getIdCanvas());
1564 PaintRefresher.RemoveComponent(this);
1567 jalview.structure.StructureSelectionManager ssm = av
1568 .getStructureSelectionManager();
1569 ssm.removeStructureViewerListener(getSeqPanel(), null);
1570 ssm.removeSelectionListener(getSeqPanel());
1571 ssm.removeCommandListener(av);
1572 ssm.removeStructureViewerListener(getSeqPanel(), null);
1573 ssm.removeSelectionListener(getSeqPanel());
1574 av.setAlignment(null);
1579 if (Cache.log.isDebugEnabled())
1581 Cache.log.warn("Closing alignment panel which is already closed.");
1587 * hides or shows dynamic annotation rows based on groups and av state flags
1589 public void updateAnnotation()
1591 updateAnnotation(false, false);
1594 public void updateAnnotation(boolean applyGlobalSettings)
1596 updateAnnotation(applyGlobalSettings, false);
1599 public void updateAnnotation(boolean applyGlobalSettings,
1600 boolean preserveNewGroupSettings)
1602 av.updateGroupAnnotationSettings(applyGlobalSettings,
1603 preserveNewGroupSettings);
1604 adjustAnnotationHeight();
1608 public AlignmentI getAlignment()
1610 return av.getAlignment();
1615 public String getViewName()
1621 * Make/Unmake this alignment panel the current input focus
1625 public void setSelected(boolean b)
1629 if (alignFrame.getSplitViewContainer() != null)
1632 * bring enclosing SplitFrame to front first if there is one
1634 ((SplitFrame) alignFrame.getSplitViewContainer()).setSelected(b);
1636 alignFrame.setSelected(b);
1637 } catch (Exception ex)
1643 alignFrame.setDisplayedView(this);
1648 public StructureSelectionManager getStructureSelectionManager()
1650 return av.getStructureSelectionManager();
1654 public void raiseOOMWarning(String string, OutOfMemoryError error)
1656 new OOMWarning(string, error, this);
1660 public jalview.api.FeatureRenderer cloneFeatureRenderer()
1663 return new FeatureRenderer(this);
1666 public jalview.api.FeatureRenderer getFeatureRenderer()
1668 return seqPanel.seqCanvas.getFeatureRenderer();
1670 public void updateFeatureRenderer(jalview.renderer.seqfeatures.FeatureRenderer fr)
1672 fr.transferSettings(getSeqPanel().seqCanvas.getFeatureRenderer());
1675 public void updateFeatureRendererFrom(jalview.api.FeatureRenderer fr)
1677 if (getSeqPanel().seqCanvas.getFeatureRenderer() != null)
1679 getSeqPanel().seqCanvas.getFeatureRenderer().transferSettings(fr);
1683 public ScalePanel getScalePanel()
1688 public void setScalePanel(ScalePanel scalePanel)
1690 this.scalePanel = scalePanel;
1693 public SeqPanel getSeqPanel()
1698 public void setSeqPanel(SeqPanel seqPanel)
1700 this.seqPanel = seqPanel;
1703 public AnnotationPanel getAnnotationPanel()
1705 return annotationPanel;
1708 public void setAnnotationPanel(AnnotationPanel annotationPanel)
1710 this.annotationPanel = annotationPanel;
1713 public AnnotationLabels getAlabels()
1718 public void setAlabels(AnnotationLabels alabels)
1720 this.alabels = alabels;
1723 public IdPanel getIdPanel()
1728 public void setIdPanel(IdPanel idPanel)
1730 this.idPanel = idPanel;
1734 * Follow a scrolling change in the (cDNA/Protein) complementary alignment.
1735 * The aim is to keep the two alignments 'lined up' on their centre columns.
1738 * holds mapped region(s) of this alignment that we are scrolling
1739 * 'to'; may be modified for sequence offset by this method
1740 * @param verticalOffset
1741 * the number of visible sequences to show above the mapped region
1743 public void scrollToCentre(SearchResults sr, int verticalOffset)
1746 * To avoid jumpy vertical scrolling (if some sequences are gapped or not
1747 * mapped), we can make the scroll-to location a sequence above the one
1750 SequenceI mappedTo = sr.getResultSequence(0);
1751 List<SequenceI> seqs = av.getAlignment().getSequences();
1754 * This is like AlignmentI.findIndex(seq) but here we are matching the
1755 * dataset sequence not the aligned sequence
1757 boolean matched = false;
1758 for (SequenceI seq : seqs)
1760 if (mappedTo == seq.getDatasetSequence())
1768 return; // failsafe, shouldn't happen
1772 * Scroll to position but centring the target residue.
1774 scrollToPosition(sr, verticalOffset, true, true);
1778 * Set a flag to say we are scrolling to follow a (cDNA/protein) complement.
1782 protected void setFollowingComplementScroll(boolean b)
1784 this.followingComplementScroll = b;
1787 protected boolean isFollowingComplementScroll()
1789 return this.followingComplementScroll;