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 java.awt.BorderLayout;
24 import java.awt.Color;
25 import java.awt.Container;
26 import java.awt.Dimension;
28 import java.awt.FontMetrics;
29 import java.awt.Graphics;
30 import java.awt.event.AdjustmentEvent;
31 import java.awt.event.AdjustmentListener;
32 import java.awt.print.PageFormat;
33 import java.awt.print.Printable;
34 import java.awt.print.PrinterException;
35 import java.beans.PropertyChangeEvent;
36 import java.beans.PropertyChangeListener;
38 import java.io.FileWriter;
39 import java.io.PrintWriter;
40 import java.util.List;
42 import javax.swing.SwingUtilities;
44 import jalview.analysis.AnnotationSorter;
45 import jalview.api.AlignViewportI;
46 import jalview.api.AlignmentViewPanel;
47 import jalview.bin.Cache;
48 import jalview.datamodel.AlignmentI;
49 import jalview.datamodel.SearchResults;
50 import jalview.datamodel.SequenceFeature;
51 import jalview.datamodel.SequenceGroup;
52 import jalview.datamodel.SequenceI;
53 import jalview.jbgui.GAlignmentPanel;
54 import jalview.math.AlignmentDimension;
55 import jalview.schemes.ResidueProperties;
56 import jalview.structure.StructureSelectionManager;
57 import jalview.util.MessageManager;
63 * @version $Revision: 1.161 $
65 public class AlignmentPanel extends GAlignmentPanel implements
66 AdjustmentListener, Printable, AlignmentViewPanel
68 public AlignViewport av;
70 OverviewPanel overviewPanel;
72 private SeqPanel seqPanel;
74 private IdPanel idPanel;
76 private boolean headless;
77 IdwidthAdjuster idwidthAdjuster;
80 public AlignFrame alignFrame;
82 private ScalePanel scalePanel;
84 private AnnotationPanel annotationPanel;
86 private AnnotationLabels alabels;
88 // this value is set false when selection area being dragged
89 boolean fastPaint = true;
96 * Flag set while scrolling to follow complementary cDNA/protein scroll. When
97 * true, suppresses invoking the same method recursively.
99 private boolean followingComplementScroll;
102 * Creates a new AlignmentPanel object.
107 public AlignmentPanel(AlignFrame af, final AlignViewport av)
111 setSeqPanel(new SeqPanel(av, this));
112 setIdPanel(new IdPanel(av, this));
114 setScalePanel(new ScalePanel(av, this));
116 idPanelHolder.add(getIdPanel(), BorderLayout.CENTER);
117 idwidthAdjuster = new IdwidthAdjuster(this);
118 idSpaceFillerPanel1.add(idwidthAdjuster, BorderLayout.CENTER);
120 setAnnotationPanel(new AnnotationPanel(this));
121 setAlabels(new AnnotationLabels(this));
123 annotationScroller.setViewportView(getAnnotationPanel());
124 annotationSpaceFillerHolder.add(getAlabels(), BorderLayout.CENTER);
126 scalePanelHolder.add(getScalePanel(), BorderLayout.CENTER);
127 seqPanelHolder.add(getSeqPanel(), BorderLayout.CENTER);
129 setScrollValues(0, 0);
131 hscroll.addAdjustmentListener(this);
132 vscroll.addAdjustmentListener(this);
134 final AlignmentPanel ap = this;
135 av.addPropertyChangeListener(new PropertyChangeListener()
137 public void propertyChange(PropertyChangeEvent evt)
139 if (evt.getPropertyName().equals("alignment"))
141 PaintRefresher.Refresh(ap, av.getSequenceSetId(), true, true);
147 adjustAnnotationHeight();
152 public AlignViewportI getAlignViewport()
156 public void alignmentChanged()
158 av.alignmentChanged(this);
160 alignFrame.updateEditMenuBar();
162 paintAlignment(true);
169 public void fontChanged()
171 // set idCanvas bufferedImage to null
172 // to prevent drawing old image
173 FontMetrics fm = getFontMetrics(av.getFont());
175 scalePanelHolder.setPreferredSize(new Dimension(10, av.getCharHeight()
177 idSpaceFillerPanel1.setPreferredSize(new Dimension(10, av
181 getIdPanel().getIdCanvas().gg = null;
182 getSeqPanel().seqCanvas.img = null;
183 getAnnotationPanel().adjustPanelHeight();
185 Dimension d = calculateIdWidth();
187 d.setSize(d.width + 4, d.height);
188 getIdPanel().getIdCanvas().setPreferredSize(d);
189 hscrollFillerPanel.setPreferredSize(d);
191 if (overviewPanel != null)
193 overviewPanel.setBoxPosition();
195 if (this.alignFrame.getSplitViewContainer() != null)
197 ((SplitFrame) this.alignFrame.getSplitViewContainer()).adjustLayout();
204 * Calculate the width of the alignment labels based on the displayed names
205 * and any bounds on label width set in preferences.
207 * @return Dimension giving the maximum width of the alignment label panel
208 * that should be used.
210 public Dimension calculateIdWidth()
212 // calculate sensible default width when no preference is available
214 if (av.getIdWidth() < 0)
216 int afwidth = (alignFrame != null ? alignFrame.getWidth() : 300);
217 int maxwidth = Math.max(20, Math.min(afwidth - 200, 2 * afwidth / 3));
218 r = calculateIdWidth(maxwidth);
219 av.setIdWidth(r.width);
224 r.width = av.getIdWidth();
231 * Calculate the width of the alignment labels based on the displayed names
232 * and any bounds on label width set in preferences.
235 * -1 or maximum width allowed for IdWidth
236 * @return Dimension giving the maximum width of the alignment label panel
237 * that should be used.
239 public Dimension calculateIdWidth(int maxwidth)
241 Container c = new Container();
243 FontMetrics fm = c.getFontMetrics(new Font(av.font.getName(),
244 Font.ITALIC, av.font.getSize()));
246 AlignmentI al = av.getAlignment();
251 while ((i < al.getHeight()) && (al.getSequenceAt(i) != null))
253 SequenceI s = al.getSequenceAt(i);
255 id = s.getDisplayId(av.getShowJVSuffix());
257 if (fm.stringWidth(id) > idWidth)
259 idWidth = fm.stringWidth(id);
265 // Also check annotation label widths
268 if (al.getAlignmentAnnotation() != null)
270 fm = c.getFontMetrics(getAlabels().getFont());
272 while (i < al.getAlignmentAnnotation().length)
274 String label = al.getAlignmentAnnotation()[i].label;
276 if (fm.stringWidth(label) > idWidth)
278 idWidth = fm.stringWidth(label);
285 return new Dimension(maxwidth < 0 ? idWidth : Math.min(maxwidth,
290 * Highlight the given results on the alignment.
293 public void highlightSearchResults(SearchResults results)
295 scrollToPosition(results);
296 getSeqPanel().seqCanvas.highlightSearchResults(results);
300 * Scroll the view to show the position of the highlighted region in results
301 * (if any) and redraw the overview
305 public boolean scrollToPosition(SearchResults results)
307 return scrollToPosition(results, true, false);
311 * Scroll the view to show the position of the highlighted region in results
314 * @param searchResults
315 * @param redrawOverview
318 public boolean scrollToPosition(SearchResults searchResults, boolean redrawOverview)
320 return scrollToPosition(searchResults, redrawOverview, false);
324 * Scroll the view to show the position of the highlighted region in results
328 * @param redrawOverview
329 * - when set, the overview will be recalculated (takes longer)
331 * if true, try to centre the search results horizontally in the view
332 * @return false if results were not found
334 public boolean scrollToPosition(SearchResults results,
335 boolean redrawOverview, boolean centre)
337 int startv, endv, starts, ends;
338 // TODO: properly locate search results in view when large numbers of hidden
339 // columns exist before highlighted region
340 // do we need to scroll the panel?
341 // TODO: tons of nullpointerexceptions raised here.
342 if (results != null && results.getSize() > 0 && av != null
343 && av.getAlignment() != null)
345 int seqIndex = av.getAlignment().findIndex(results);
350 SequenceI seq = av.getAlignment().getSequenceAt(seqIndex);
352 int[] r = results.getResults(seq, 0, av.getAlignment().getWidth());
359 // System.err.println("Seq : "+seqIndex+" Scroll to "+start+","+end); //
363 * To centre results, scroll to positions half the visible width
364 * left/right of the start/end positions
368 int offset = (av.getEndRes() - av.getStartRes() + 1) / 2 - 1;
369 start = Math.max(start - offset, 0);
370 end = Math.min(end + offset, seq.getEnd() - 1);
376 if (end == seq.getEnd())
380 if (av.hasHiddenColumns())
382 start = av.getColumnSelection().findColumnPosition(start);
383 end = av.getColumnSelection().findColumnPosition(end);
386 if (!av.getColumnSelection().isVisible(r[0]))
388 // don't scroll - position isn't visible
393 if (!av.getWrapAlignment())
395 if ((startv = av.getStartRes()) >= start)
398 * Scroll left to make start of search results visible
400 // setScrollValues(start - 1, seqIndex); // plus one residue
401 setScrollValues(start, seqIndex);
403 else if ((endv = av.getEndRes()) <= end)
406 * Scroll right to make end of search results visible
408 // setScrollValues(startv + 1 + end - endv, seqIndex); // plus one
409 setScrollValues(startv + end - endv, seqIndex);
411 else if ((starts = av.getStartSeq()) > seqIndex)
414 * Scroll up to make start of search results visible
416 setScrollValues(av.getStartRes(), seqIndex);
418 else if ((ends = av.getEndSeq()) <= seqIndex)
421 * Scroll down to make end of search results visible
423 setScrollValues(av.getStartRes(), starts + seqIndex - ends + 1);
426 * Else results are already visible - no need to scroll
431 scrollToWrappedVisible(start);
434 if (redrawOverview && overviewPanel != null)
436 overviewPanel.setBoxPosition();
438 paintAlignment(redrawOverview);
442 void scrollToWrappedVisible(int res)
444 int cwidth = getSeqPanel().seqCanvas
445 .getWrappedCanvasWidth(getSeqPanel().seqCanvas.getWidth());
446 if (res < av.getStartRes() || res >= (av.getStartRes() + cwidth))
448 vscroll.setValue((res / cwidth));
449 av.startRes = vscroll.getValue() * cwidth;
457 * @return DOCUMENT ME!
459 public OverviewPanel getOverviewPanel()
461 return overviewPanel;
470 public void setOverviewPanel(OverviewPanel op)
478 * Hide or show annotation panel
481 public void setAnnotationVisible(boolean b)
483 if (!av.getWrapAlignment())
485 annotationSpaceFillerHolder.setVisible(b);
486 annotationScroller.setVisible(b);
492 * automatically adjust annotation panel height for new annotation whilst
493 * ensuring the alignment is still visible.
495 public void adjustAnnotationHeight()
497 // TODO: display vertical annotation scrollbar if necessary
498 // this is called after loading new annotation onto alignment
499 if (alignFrame.getHeight() == 0)
501 System.out.println("NEEDS FIXING");
503 validateAnnotationDimensions(true);
505 paintAlignment(true);
509 * calculate the annotation dimensions and refresh slider values accordingly.
510 * need to do repaints/notifys afterwards.
512 protected void validateAnnotationDimensions(boolean adjustPanelHeight)
514 int height = getAnnotationPanel().adjustPanelHeight();
516 int theight = av.getCharHeight()
517 * (av.getAlignment().getHeight() + (!av.hasHiddenRows() ? 0
518 : av.getAlignment().getHiddenSequences().getSize()));
519 float sscaling = (float) (theight / (1.0 * theight + height));
520 float ascaling = (float) (height * 1.0 / alignFrame.getHeight());
521 int rheight = alignFrame.getHeight() - height - av.getCharHeight();
522 if (adjustPanelHeight)
524 // NOTE: this logic is different in the applet. Need a better algorithm to
526 // try and set height according to alignment
527 if (ascaling > 0 && sscaling < 0.5)
529 // if the alignment is too big then
530 // default is 0.5 split
531 height = alignFrame.getHeight() / 2;
535 // if space for more than one sequence row left when annotation is fully
536 // displayed then set height to annotation height
537 // otherwise, leave at least two lines of sequence shown.
538 height = (rheight > av.getCharHeight()) ? height
539 : (-av.getCharHeight() * 3 + (int) (alignFrame.getHeight() * (1 - sscaling)));
544 // maintain same window layout whilst updating sliders
545 height = annotationScroller.getSize().height;
549 annotationScroller.setPreferredSize(new Dimension(annotationScroller
550 .getWidth(), height));
552 annotationSpaceFillerHolder.setPreferredSize(new Dimension(
553 annotationSpaceFillerHolder.getWidth(), height));
554 annotationScroller.validate();// repaint();
555 annotationScroller.addNotify();
559 * update alignment layout for viewport settings
564 public void updateLayout()
567 setAnnotationVisible(av.isShowAnnotation());
568 boolean wrap = av.getWrapAlignment();
570 scalePanelHolder.setVisible(!wrap);
571 hscroll.setVisible(!wrap);
572 idwidthAdjuster.setVisible(!wrap);
576 annotationScroller.setVisible(false);
577 annotationSpaceFillerHolder.setVisible(false);
579 else if (av.isShowAnnotation())
581 annotationScroller.setVisible(true);
582 annotationSpaceFillerHolder.setVisible(true);
585 idSpaceFillerPanel1.setVisible(!wrap);
590 // return value is true if the scroll is valid
591 public boolean scrollUp(boolean up)
595 if (vscroll.getValue() < 1)
601 vscroll.setValue(vscroll.getValue() - 1);
605 if ((vextent + vscroll.getValue()) >= av.getAlignment().getHeight())
611 vscroll.setValue(vscroll.getValue() + 1);
625 * @return DOCUMENT ME!
627 public boolean scrollRight(boolean right)
631 if (hscroll.getValue() < 1)
637 hscroll.setValue(hscroll.getValue() - 1);
641 if ((hextent + hscroll.getValue()) >= av.getAlignment().getWidth())
647 hscroll.setValue(hscroll.getValue() + 1);
656 * Adjust row/column scrollers to show a visible position in the alignment.
659 * visible column to scroll to DOCUMENT ME!
661 * visible row to scroll to
664 public void setScrollValues(int x, int y)
666 // System.err.println("Scroll to "+x+","+y);
667 if (av == null || av.getAlignment() == null)
671 int width = av.getAlignment().getWidth();
672 int height = av.getAlignment().getHeight();
674 if (av.hasHiddenColumns())
676 width = av.getColumnSelection().findColumnPosition(width);
679 av.setEndRes((x + (getSeqPanel().seqCanvas.getWidth() / av
680 .getCharWidth())) - 1);
682 hextent = getSeqPanel().seqCanvas.getWidth() / av.getCharWidth();
683 vextent = getSeqPanel().seqCanvas.getHeight() / av.getCharHeight();
690 if (vextent > height)
695 if ((hextent + x) > width)
700 if ((vextent + y) > height)
702 y = height - vextent;
715 hscroll.setValues(x, hextent, 0, width);
716 vscroll.setValues(y, vextent, 0, height);
725 public void adjustmentValueChanged(AdjustmentEvent evt)
727 int oldX = av.getStartRes();
728 int oldY = av.getStartSeq();
730 if (evt.getSource() == hscroll)
732 int x = hscroll.getValue();
734 av.setEndRes((x + (getSeqPanel().seqCanvas.getWidth() / av.getCharWidth())) - 1);
737 if (evt.getSource() == vscroll)
739 int offy = vscroll.getValue();
741 if (av.getWrapAlignment())
745 int rowSize = getSeqPanel().seqCanvas
746 .getWrappedCanvasWidth(getSeqPanel().seqCanvas.getWidth());
747 av.setStartRes(offy * rowSize);
748 av.setEndRes((offy + 1) * rowSize);
752 // This is only called if file loaded is a jar file that
753 // was wrapped when saved and user has wrap alignment true
754 // as preference setting
755 SwingUtilities.invokeLater(new Runnable()
759 setScrollValues(av.getStartRes(), av.getStartSeq());
766 av.setStartSeq(offy);
768 + (getSeqPanel().seqCanvas.getHeight() / av.getCharHeight()));
772 if (overviewPanel != null)
774 overviewPanel.setBoxPosition();
777 int scrollX = av.startRes - oldX;
778 int scrollY = av.startSeq - oldY;
780 if (av.getWrapAlignment() || !fastPaint)
786 // Make sure we're not trying to draw a panel
787 // larger than the visible window
788 if (scrollX > av.endRes - av.startRes)
790 scrollX = av.endRes - av.startRes;
792 else if (scrollX < av.startRes - av.endRes)
794 scrollX = av.startRes - av.endRes;
797 if (scrollX != 0 || scrollY != 0)
799 getIdPanel().getIdCanvas().fastPaint(scrollY);
800 getSeqPanel().seqCanvas.fastPaint(scrollX, scrollY);
801 getScalePanel().repaint();
803 if (av.isShowAnnotation() && scrollX != 0)
805 getAnnotationPanel().fastPaint(scrollX);
810 * If there is one, scroll the (Protein/cDNA) complementary alignment to
811 * match, unless we are ourselves doing that.
813 if (isFollowingComplementScroll())
815 setFollowingComplementScroll(false);
819 av.scrollComplementaryAlignment();
824 * Repaint the alignment including the annotations and overview panels (if
827 public void paintAlignment(boolean updateOverview)
829 final AnnotationSorter sorter = new AnnotationSorter(getAlignment(),
830 av.isShowAutocalculatedAbove());
831 sorter.sort(getAlignment()
832 .getAlignmentAnnotation(),
833 av.getSortAnnotationsBy());
838 av.getStructureSelectionManager().sequenceColoursChanged(this);
840 if (overviewPanel != null)
842 overviewPanel.updateOverviewImage();
853 public void paintComponent(Graphics g)
857 Dimension d = getIdPanel().getIdCanvas().getPreferredSize();
858 idPanelHolder.setPreferredSize(d);
859 hscrollFillerPanel.setPreferredSize(new Dimension(d.width, 12));
862 if (av.getWrapAlignment())
864 int maxwidth = av.getAlignment().getWidth();
866 if (av.hasHiddenColumns())
868 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
871 int canvasWidth = getSeqPanel().seqCanvas
872 .getWrappedCanvasWidth(getSeqPanel().seqCanvas.getWidth());
876 / getSeqPanel().seqCanvas
877 .getWrappedCanvasWidth(getSeqPanel().seqCanvas
879 vscroll.setMaximum(max);
880 vscroll.setUnitIncrement(1);
881 vscroll.setVisibleAmount(1);
886 setScrollValues(av.getStartRes(), av.getStartSeq());
900 * @return DOCUMENT ME!
902 * @throws PrinterException
905 public int print(Graphics pg, PageFormat pf, int pi)
906 throws PrinterException
908 pg.translate((int) pf.getImageableX(), (int) pf.getImageableY());
910 int pwidth = (int) pf.getImageableWidth();
911 int pheight = (int) pf.getImageableHeight();
913 if (av.getWrapAlignment())
915 return printWrappedAlignment(pg, pwidth, pheight, pi);
919 return printUnwrapped(pg, pwidth, pheight, pi);
935 * @return DOCUMENT ME!
937 * @throws PrinterException
940 public int printUnwrapped(Graphics pg, int pwidth, int pheight, int pi)
941 throws PrinterException
943 int idWidth = getVisibleIdWidth(false);
944 FontMetrics fm = getFontMetrics(av.getFont());
945 int scaleHeight = av.getCharHeight() + fm.getDescent();
947 pg.setColor(Color.white);
948 pg.fillRect(0, 0, pwidth, pheight);
949 pg.setFont(av.getFont());
951 // //////////////////////////////////
952 // / How many sequences and residues can we fit on a printable page?
953 int totalRes = (pwidth - idWidth) / av.getCharWidth();
955 int totalSeq = (pheight - scaleHeight) / av.getCharHeight() - 1;
957 int pagesWide = (av.getAlignment().getWidth() / totalRes) + 1;
959 // ///////////////////////////
960 // / Only print these sequences and residues on this page
963 // ///////////////////////////
964 // / Only print these sequences and residues on this page
967 // ///////////////////////////
968 // / Only print these sequences and residues on this page
971 // ///////////////////////////
972 // / Only print these sequences and residues on this page
974 startRes = (pi % pagesWide) * totalRes;
975 endRes = (startRes + totalRes) - 1;
977 if (endRes > (av.getAlignment().getWidth() - 1))
979 endRes = av.getAlignment().getWidth() - 1;
982 startSeq = (pi / pagesWide) * totalSeq;
983 endSeq = startSeq + totalSeq;
985 if (endSeq > av.getAlignment().getHeight())
987 endSeq = av.getAlignment().getHeight();
990 int pagesHigh = ((av.getAlignment().getHeight() / totalSeq) + 1)
993 if (av.isShowAnnotation())
995 pagesHigh += getAnnotationPanel().adjustPanelHeight() + 3;
998 pagesHigh /= pheight;
1000 if (pi >= (pagesWide * pagesHigh))
1002 return Printable.NO_SUCH_PAGE;
1006 pg.translate(idWidth, 0);
1007 getScalePanel().drawScale(pg, startRes, endRes, pwidth - idWidth,
1009 pg.translate(-idWidth, scaleHeight);
1013 Color currentColor = null;
1014 Color currentTextColor = null;
1016 pg.setFont(getIdPanel().getIdCanvas().getIdfont());
1019 for (int i = startSeq; i < endSeq; i++)
1021 seq = av.getAlignment().getSequenceAt(i);
1022 if ((av.getSelectionGroup() != null)
1023 && av.getSelectionGroup().getSequences(null).contains(seq))
1025 currentColor = Color.gray;
1026 currentTextColor = Color.black;
1030 currentColor = av.getSequenceColour(seq);
1031 currentTextColor = Color.black;
1034 pg.setColor(currentColor);
1035 pg.fillRect(0, (i - startSeq) * av.getCharHeight(), idWidth,
1036 av.getCharHeight());
1038 pg.setColor(currentTextColor);
1041 if (av.isRightAlignIds())
1043 fm = pg.getFontMetrics();
1045 - fm.stringWidth(seq.getDisplayId(av.getShowJVSuffix()))
1050 seq.getDisplayId(av.getShowJVSuffix()),
1052 (((i - startSeq) * av.getCharHeight()) + av.getCharHeight())
1053 - (av.getCharHeight() / 5));
1056 pg.setFont(av.getFont());
1058 // draw main sequence panel
1059 pg.translate(idWidth, 0);
1060 getSeqPanel().seqCanvas.drawPanel(pg, startRes, endRes, startSeq, endSeq, 0);
1062 if (av.isShowAnnotation() && (endSeq == av.getAlignment().getHeight()))
1064 // draw annotation - need to offset for current scroll position
1065 int offset = -getAlabels().getScrollOffset();
1066 pg.translate(0, offset);
1067 pg.translate(-idWidth - 3, (endSeq - startSeq) * av.getCharHeight()
1069 getAlabels().drawComponent(pg, idWidth);
1070 pg.translate(idWidth + 3, 0);
1071 getAnnotationPanel().renderer.drawComponent(getAnnotationPanel(), av,
1072 pg, -1, startRes, endRes + 1);
1073 pg.translate(0, -offset);
1076 return Printable.PAGE_EXISTS;
1091 * @return DOCUMENT ME!
1093 * @throws PrinterException
1096 public int printWrappedAlignment(Graphics pg, int pwidth, int pheight,
1097 int pi) throws PrinterException
1099 int annotationHeight = 0;
1100 AnnotationLabels labels = null;
1101 if (av.isShowAnnotation())
1103 annotationHeight = getAnnotationPanel().adjustPanelHeight();
1104 labels = new AnnotationLabels(av);
1107 int hgap = av.getCharHeight();
1108 if (av.getScaleAboveWrapped())
1110 hgap += av.getCharHeight();
1113 int cHeight = av.getAlignment().getHeight() * av.getCharHeight() + hgap
1116 int idWidth = getVisibleIdWidth(false);
1118 int maxwidth = av.getAlignment().getWidth();
1119 if (av.hasHiddenColumns())
1121 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
1124 int resWidth = getSeqPanel().seqCanvas.getWrappedCanvasWidth(pwidth
1127 int totalHeight = cHeight * (maxwidth / resWidth + 1);
1129 pg.setColor(Color.white);
1130 pg.fillRect(0, 0, pwidth, pheight);
1131 pg.setFont(av.getFont());
1135 pg.setColor(Color.black);
1137 pg.translate(0, -pi * pheight);
1139 pg.setClip(0, pi * pheight, pwidth, pheight);
1145 for (int i = 0; i < av.getAlignment().getHeight(); i++)
1147 pg.setFont(getIdPanel().getIdCanvas().getIdfont());
1148 SequenceI s = av.getAlignment().getSequenceAt(i);
1149 String string = s.getDisplayId(av.getShowJVSuffix());
1151 if (av.isRightAlignIds())
1153 FontMetrics fm = pg.getFontMetrics();
1154 xPos = idWidth - fm.stringWidth(string) - 4;
1156 pg.drawString(string, xPos,
1157 ((i * av.getCharHeight()) + ypos + av.getCharHeight())
1158 - (av.getCharHeight() / 5));
1162 pg.translate(-3, ypos
1163 + (av.getAlignment().getHeight() * av.getCharHeight()));
1165 pg.setFont(av.getFont());
1166 labels.drawComponent(pg, idWidth);
1167 pg.translate(+3, -ypos
1168 - (av.getAlignment().getHeight() * av
1173 } while (ypos < totalHeight);
1175 pg.translate(idWidth, 0);
1177 getSeqPanel().seqCanvas.drawWrappedPanel(pg, pwidth - idWidth, totalHeight,
1180 if ((pi * pheight) < totalHeight)
1182 return Printable.PAGE_EXISTS;
1187 return Printable.NO_SUCH_PAGE;
1192 * get current sequence ID panel width, or nominal value if panel were to be
1193 * displayed using default settings
1197 public int getVisibleIdWidth()
1199 return getVisibleIdWidth(true);
1203 * get current sequence ID panel width, or nominal value if panel were to be
1204 * displayed using default settings
1207 * indicate if the Id width for onscreen or offscreen display should
1211 public int getVisibleIdWidth(boolean onscreen)
1213 // see if rendering offscreen - check preferences and calc width accordingly
1214 if (!onscreen && Cache.getDefault("FIGURE_AUTOIDWIDTH", false))
1216 return calculateIdWidth(-1).width + 4;
1218 Integer idwidth = null;
1220 || (idwidth = Cache.getIntegerProperty("FIGURE_FIXEDIDWIDTH")) == null)
1222 return (getIdPanel().getWidth() > 0 ? getIdPanel().getWidth()
1223 : calculateIdWidth().width + 4);
1225 return idwidth.intValue() + 4;
1228 void makeAlignmentImage(jalview.util.ImageMaker.TYPE type, File file)
1230 long progress = System.currentTimeMillis();
1231 headless = (System.getProperty("java.awt.headless") != null && System
1232 .getProperty("java.awt.headless").equals("true"));
1233 if (alignFrame != null && !headless)
1235 alignFrame.setProgressBar(MessageManager.formatMessage(
1236 "status.saving_file", new Object[]
1237 { type.getLabel() }), progress);
1241 AlignmentDimension aDimension = getAlignmentDimension();
1244 jalview.util.ImageMaker im;
1245 final String imageAction, imageTitle;
1246 if (type == jalview.util.ImageMaker.TYPE.PNG)
1248 imageAction = "Create PNG image from alignment";
1251 else if (type == jalview.util.ImageMaker.TYPE.EPS)
1253 imageAction = "Create EPS file from alignment";
1254 imageTitle = alignFrame.getTitle();
1258 imageAction = "Create SVG file from alignment";
1259 imageTitle = alignFrame.getTitle();
1262 im = new jalview.util.ImageMaker(this, type, imageAction,
1263 aDimension.getWidth(), aDimension.getHeight(), file,
1265 if (av.getWrapAlignment())
1267 if (im.getGraphics() != null)
1269 printWrappedAlignment(im.getGraphics(), aDimension.getWidth(),
1270 aDimension.getHeight(), 0);
1276 if (im.getGraphics() != null)
1278 printUnwrapped(im.getGraphics(), aDimension.getWidth(),
1279 aDimension.getHeight(), 0);
1283 } catch (OutOfMemoryError err)
1286 System.out.println("########################\n" + "OUT OF MEMORY "
1287 + file + "\n" + "########################");
1288 new OOMWarning("Creating Image for " + file, err);
1289 // System.out.println("Create IMAGE: " + err);
1290 } catch (Exception ex)
1292 ex.printStackTrace();
1296 if (alignFrame != null && !headless)
1298 alignFrame.setProgressBar(MessageManager.getString("status.export_complete"), progress);
1303 public AlignmentDimension getAlignmentDimension()
1305 int maxwidth = av.getAlignment().getWidth();
1306 if (av.hasHiddenColumns())
1308 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth);
1311 int height = ((av.getAlignment().getHeight() + 1) * av.getCharHeight())
1312 + getScalePanel().getHeight();
1313 int width = getVisibleIdWidth(false) + (maxwidth * av.getCharWidth());
1315 if (av.getWrapAlignment())
1317 height = getWrappedHeight();
1320 // need to obtain default alignment width and then add in any
1321 // additional allowance for id margin
1322 // this duplicates the calculation in getWrappedHeight but adjusts for
1324 width = alignFrame.getWidth() - vscroll.getPreferredSize().width
1325 - alignFrame.getInsets().left
1326 - alignFrame.getInsets().right - getVisibleIdWidth()
1327 + getVisibleIdWidth(false);
1331 width = getSeqPanel().getWidth() + getVisibleIdWidth(false);
1335 else if (av.isShowAnnotation())
1337 height += getAnnotationPanel().adjustPanelHeight() + 3;
1339 return new AlignmentDimension(width, height);
1346 public void makeEPS(File epsFile)
1348 makeAlignmentImage(jalview.util.ImageMaker.TYPE.EPS, epsFile);
1354 public void makePNG(File pngFile)
1356 makeAlignmentImage(jalview.util.ImageMaker.TYPE.PNG, pngFile);
1359 public void makeSVG(File svgFile)
1361 makeAlignmentImage(jalview.util.ImageMaker.TYPE.SVG, svgFile);
1363 public void makePNGImageMap(File imgMapFile, String imageName)
1365 // /////ONLY WORKS WITH NONE WRAPPED ALIGNMENTS
1366 // ////////////////////////////////////////////
1367 int idWidth = getVisibleIdWidth(false);
1368 FontMetrics fm = getFontMetrics(av.getFont());
1369 int scaleHeight = av.getCharHeight() + fm.getDescent();
1372 // ////////////////////////////////
1373 if (imgMapFile != null)
1377 int s, sSize = av.getAlignment().getHeight(), res, alwidth = av
1378 .getAlignment().getWidth(), g, gSize, f, fSize, sy;
1379 StringBuffer text = new StringBuffer();
1380 PrintWriter out = new PrintWriter(new FileWriter(imgMapFile));
1381 out.println(jalview.io.HTMLOutput.getImageMapHTML());
1382 out.println("<img src=\"" + imageName
1383 + "\" border=\"0\" usemap=\"#Map\" >"
1384 + "<map name=\"Map\">");
1386 for (s = 0; s < sSize; s++)
1388 sy = s * av.getCharHeight() + scaleHeight;
1390 SequenceI seq = av.getAlignment().getSequenceAt(s);
1391 SequenceFeature[] features = seq.getSequenceFeatures();
1392 SequenceGroup[] groups = av.getAlignment().findAllGroups(seq);
1393 for (res = 0; res < alwidth; res++)
1395 text = new StringBuffer();
1396 String triplet = null;
1397 if (av.getAlignment().isNucleotide())
1399 triplet = ResidueProperties.nucleotideName.get(seq
1405 triplet = ResidueProperties.aa2Triplet.get(seq.getCharAt(res)
1409 if (triplet == null)
1414 int alIndex = seq.findPosition(res);
1415 gSize = groups.length;
1416 for (g = 0; g < gSize; g++)
1418 if (text.length() < 1)
1420 text.append("<area shape=\"rect\" coords=\""
1421 + (idWidth + res * av.getCharWidth()) + "," + sy
1422 + "," + (idWidth + (res + 1) * av.getCharWidth())
1424 + (av.getCharHeight() + sy) + "\""
1425 + " onMouseOver=\"toolTip('" + alIndex + " "
1429 if (groups[g].getStartRes() < res
1430 && groups[g].getEndRes() > res)
1432 text.append("<br><em>" + groups[g].getName() + "</em>");
1436 if (features != null)
1438 if (text.length() < 1)
1440 text.append("<area shape=\"rect\" coords=\""
1441 + (idWidth + res * av.getCharWidth()) + "," + sy
1442 + "," + (idWidth + (res + 1) * av.getCharWidth())
1444 + (av.getCharHeight() + sy) + "\""
1445 + " onMouseOver=\"toolTip('" + alIndex + " "
1448 fSize = features.length;
1449 for (f = 0; f < fSize; f++)
1452 if ((features[f].getBegin() <= seq.findPosition(res))
1453 && (features[f].getEnd() >= seq.findPosition(res)))
1455 if (features[f].getType().equals("disulfide bond"))
1457 if (features[f].getBegin() == seq.findPosition(res)
1458 || features[f].getEnd() == seq
1461 text.append("<br>disulfide bond "
1462 + features[f].getBegin() + ":"
1463 + features[f].getEnd());
1468 text.append("<br>");
1469 text.append(features[f].getType());
1470 if (features[f].getDescription() != null
1471 && !features[f].getType().equals(
1472 features[f].getDescription()))
1474 text.append(" " + features[f].getDescription());
1477 if (features[f].getValue("status") != null)
1479 text.append(" (" + features[f].getValue("status")
1487 if (text.length() > 1)
1489 text.append("')\"; onMouseOut=\"toolTip()\"; href=\"#\">");
1490 out.println(text.toString());
1494 out.println("</map></body></html>");
1497 } catch (Exception ex)
1499 ex.printStackTrace();
1501 } // /////////END OF IMAGE MAP
1505 int getWrappedHeight()
1507 int seqPanelWidth = getSeqPanel().seqCanvas.getWidth();
1509 if (System.getProperty("java.awt.headless") != null
1510 && System.getProperty("java.awt.headless").equals("true"))
1512 seqPanelWidth = alignFrame.getWidth() - getVisibleIdWidth()
1513 - vscroll.getPreferredSize().width
1514 - alignFrame.getInsets().left - alignFrame.getInsets().right;
1517 int chunkWidth = getSeqPanel().seqCanvas
1518 .getWrappedCanvasWidth(seqPanelWidth);
1520 int hgap = av.getCharHeight();
1521 if (av.getScaleAboveWrapped())
1523 hgap += av.getCharHeight();
1526 int annotationHeight = 0;
1527 if (av.isShowAnnotation())
1529 annotationHeight = getAnnotationPanel().adjustPanelHeight();
1532 int cHeight = av.getAlignment().getHeight() * av.getCharHeight() + hgap
1535 int maxwidth = av.getAlignment().getWidth();
1536 if (av.hasHiddenColumns())
1538 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
1541 int height = ((maxwidth / chunkWidth) + 1) * cHeight;
1547 * close the panel - deregisters all listeners and nulls any references to
1550 public void closePanel()
1552 PaintRefresher.RemoveComponent(getSeqPanel().seqCanvas);
1553 PaintRefresher.RemoveComponent(getIdPanel().getIdCanvas());
1554 PaintRefresher.RemoveComponent(this);
1557 jalview.structure.StructureSelectionManager ssm = av
1558 .getStructureSelectionManager();
1559 ssm.removeStructureViewerListener(getSeqPanel(), null);
1560 ssm.removeSelectionListener(getSeqPanel());
1561 ssm.removeCommandListener(av);
1562 ssm.removeStructureViewerListener(getSeqPanel(), null);
1563 ssm.removeSelectionListener(getSeqPanel());
1564 av.setAlignment(null);
1569 if (Cache.log.isDebugEnabled())
1571 Cache.log.warn("Closing alignment panel which is already closed.");
1577 * hides or shows dynamic annotation rows based on groups and av state flags
1579 public void updateAnnotation()
1581 updateAnnotation(false, false);
1584 public void updateAnnotation(boolean applyGlobalSettings)
1586 updateAnnotation(applyGlobalSettings, false);
1589 public void updateAnnotation(boolean applyGlobalSettings,
1590 boolean preserveNewGroupSettings)
1592 av.updateGroupAnnotationSettings(applyGlobalSettings,
1593 preserveNewGroupSettings);
1594 adjustAnnotationHeight();
1598 public AlignmentI getAlignment()
1600 return av.getAlignment();
1605 public String getViewName()
1611 * Make/Unmake this alignment panel the current input focus
1615 public void setSelected(boolean b)
1619 if (alignFrame.getSplitViewContainer() != null)
1622 * bring enclosing SplitFrame to front first if there is one
1624 ((SplitFrame) alignFrame.getSplitViewContainer()).setSelected(b);
1626 alignFrame.setSelected(b);
1627 } catch (Exception ex)
1633 alignFrame.setDisplayedView(this);
1638 public StructureSelectionManager getStructureSelectionManager()
1640 return av.getStructureSelectionManager();
1644 public void raiseOOMWarning(String string, OutOfMemoryError error)
1646 new OOMWarning(string, error, this);
1650 public jalview.api.FeatureRenderer cloneFeatureRenderer()
1653 return new FeatureRenderer(this);
1656 public jalview.api.FeatureRenderer getFeatureRenderer()
1658 return seqPanel.seqCanvas.getFeatureRenderer();
1660 public void updateFeatureRenderer(jalview.renderer.seqfeatures.FeatureRenderer fr)
1662 fr.transferSettings(getSeqPanel().seqCanvas.getFeatureRenderer());
1665 public void updateFeatureRendererFrom(jalview.api.FeatureRenderer fr)
1667 if (getSeqPanel().seqCanvas.getFeatureRenderer() != null)
1669 getSeqPanel().seqCanvas.getFeatureRenderer().transferSettings(fr);
1673 public ScalePanel getScalePanel()
1678 public void setScalePanel(ScalePanel scalePanel)
1680 this.scalePanel = scalePanel;
1683 public SeqPanel getSeqPanel()
1688 public void setSeqPanel(SeqPanel seqPanel)
1690 this.seqPanel = seqPanel;
1693 public AnnotationPanel getAnnotationPanel()
1695 return annotationPanel;
1698 public void setAnnotationPanel(AnnotationPanel annotationPanel)
1700 this.annotationPanel = annotationPanel;
1703 public AnnotationLabels getAlabels()
1708 public void setAlabels(AnnotationLabels alabels)
1710 this.alabels = alabels;
1713 public IdPanel getIdPanel()
1718 public void setIdPanel(IdPanel idPanel)
1720 this.idPanel = idPanel;
1724 * Follow a scrolling change in the (cDNA/Protein) complementary alignment.
1725 * The aim is to keep the two alignments 'lined up' on their centre columns.
1728 * holds mapped region(s) of this alignment that we are scrolling
1729 * 'to'; may be modified for sequence offset by this method
1731 * the number of visible sequences to show above the mapped region
1733 public void scrollToCentre(SearchResults sr, int seqOffset)
1736 * To avoid jumpy vertical scrolling (if some sequences are gapped or not
1737 * mapped), we can make the scroll-to location a sequence above the one
1740 SequenceI mappedTo = sr.getResultSequence(0);
1741 List<SequenceI> seqs = av.getAlignment().getSequences();
1744 * This is like AlignmentI.findIndex(seq) but here we are matching the
1745 * dataset sequence not the aligned sequence
1747 int sequenceIndex = 0;
1748 boolean matched = false;
1749 for (SequenceI seq : seqs)
1751 if (mappedTo == seq.getDatasetSequence())
1760 return; // failsafe, shouldn't happen
1762 sequenceIndex = Math.max(0, sequenceIndex - seqOffset);
1763 sr.getResults().get(0)
1764 .setSequence(av.getAlignment().getSequenceAt(sequenceIndex));
1767 * Scroll to position but centring the target residue.
1769 scrollToPosition(sr, true, true);
1773 * Set a flag to say we are scrolling to follow a (cDNA/protein) complement.
1777 protected void setFollowingComplementScroll(boolean b)
1779 this.followingComplementScroll = b;
1782 protected boolean isFollowingComplementScroll()
1784 return this.followingComplementScroll;