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);
402 else if ((endv = av.getEndRes()) <= end)
405 * Scroll right to make end of search results visible
407 setScrollValues(startv + 1 + end - endv, seqIndex);
409 else if ((starts = av.getStartSeq()) > seqIndex)
412 * Scroll up to make start of search results visible
414 setScrollValues(av.getStartRes(), seqIndex);
416 else if ((ends = av.getEndSeq()) <= seqIndex)
419 * Scroll down to make end of search results visible
421 setScrollValues(av.getStartRes(), starts + seqIndex - ends + 1);
424 * Else results are already visible - no need to scroll
429 scrollToWrappedVisible(start);
432 if (redrawOverview && overviewPanel != null)
434 overviewPanel.setBoxPosition();
436 paintAlignment(redrawOverview);
440 void scrollToWrappedVisible(int res)
442 int cwidth = getSeqPanel().seqCanvas
443 .getWrappedCanvasWidth(getSeqPanel().seqCanvas.getWidth());
444 if (res < av.getStartRes() || res >= (av.getStartRes() + cwidth))
446 vscroll.setValue((res / cwidth));
447 av.startRes = vscroll.getValue() * cwidth;
455 * @return DOCUMENT ME!
457 public OverviewPanel getOverviewPanel()
459 return overviewPanel;
468 public void setOverviewPanel(OverviewPanel op)
476 * Hide or show annotation panel
479 public void setAnnotationVisible(boolean b)
481 if (!av.getWrapAlignment())
483 annotationSpaceFillerHolder.setVisible(b);
484 annotationScroller.setVisible(b);
490 * automatically adjust annotation panel height for new annotation whilst
491 * ensuring the alignment is still visible.
493 public void adjustAnnotationHeight()
495 // TODO: display vertical annotation scrollbar if necessary
496 // this is called after loading new annotation onto alignment
497 if (alignFrame.getHeight() == 0)
499 System.out.println("NEEDS FIXING");
501 validateAnnotationDimensions(true);
503 paintAlignment(true);
507 * calculate the annotation dimensions and refresh slider values accordingly.
508 * need to do repaints/notifys afterwards.
510 protected void validateAnnotationDimensions(boolean adjustPanelHeight)
512 int height = getAnnotationPanel().adjustPanelHeight();
514 int theight = av.getCharHeight()
515 * (av.getAlignment().getHeight() + (!av.hasHiddenRows() ? 0
516 : av.getAlignment().getHiddenSequences().getSize()));
517 float sscaling = (float) (theight / (1.0 * theight + height));
518 float ascaling = (float) (height * 1.0 / alignFrame.getHeight());
519 int rheight = alignFrame.getHeight() - height - av.getCharHeight();
520 if (adjustPanelHeight)
522 // NOTE: this logic is different in the applet. Need a better algorithm to
524 // try and set height according to alignment
525 if (ascaling > 0 && sscaling < 0.5)
527 // if the alignment is too big then
528 // default is 0.5 split
529 height = alignFrame.getHeight() / 2;
533 // if space for more than one sequence row left when annotation is fully
534 // displayed then set height to annotation height
535 // otherwise, leave at least two lines of sequence shown.
536 height = (rheight > av.getCharHeight()) ? height
537 : (-av.getCharHeight() * 3 + (int) (alignFrame.getHeight() * (1 - sscaling)));
542 // maintain same window layout whilst updating sliders
543 height = annotationScroller.getSize().height;
547 annotationScroller.setPreferredSize(new Dimension(annotationScroller
548 .getWidth(), height));
550 annotationSpaceFillerHolder.setPreferredSize(new Dimension(
551 annotationSpaceFillerHolder.getWidth(), height));
552 annotationScroller.validate();// repaint();
553 annotationScroller.addNotify();
557 * update alignment layout for viewport settings
562 public void updateLayout()
565 setAnnotationVisible(av.isShowAnnotation());
566 boolean wrap = av.getWrapAlignment();
568 scalePanelHolder.setVisible(!wrap);
569 hscroll.setVisible(!wrap);
570 idwidthAdjuster.setVisible(!wrap);
574 annotationScroller.setVisible(false);
575 annotationSpaceFillerHolder.setVisible(false);
577 else if (av.isShowAnnotation())
579 annotationScroller.setVisible(true);
580 annotationSpaceFillerHolder.setVisible(true);
583 idSpaceFillerPanel1.setVisible(!wrap);
588 // return value is true if the scroll is valid
589 public boolean scrollUp(boolean up)
593 if (vscroll.getValue() < 1)
599 vscroll.setValue(vscroll.getValue() - 1);
603 if ((vextent + vscroll.getValue()) >= av.getAlignment().getHeight())
609 vscroll.setValue(vscroll.getValue() + 1);
623 * @return DOCUMENT ME!
625 public boolean scrollRight(boolean right)
629 if (hscroll.getValue() < 1)
635 hscroll.setValue(hscroll.getValue() - 1);
639 if ((hextent + hscroll.getValue()) >= av.getAlignment().getWidth())
645 hscroll.setValue(hscroll.getValue() + 1);
654 * Adjust row/column scrollers to show a visible position in the alignment.
657 * visible column to scroll to DOCUMENT ME!
659 * visible row to scroll to
662 public void setScrollValues(int x, int y)
664 // System.err.println("Scroll to "+x+","+y);
665 if (av == null || av.getAlignment() == null)
669 int width = av.getAlignment().getWidth();
670 int height = av.getAlignment().getHeight();
672 if (av.hasHiddenColumns())
674 width = av.getColumnSelection().findColumnPosition(width);
677 av.setEndRes((x + (getSeqPanel().seqCanvas.getWidth() / av
678 .getCharWidth())) - 1);
680 hextent = getSeqPanel().seqCanvas.getWidth() / av.getCharWidth();
681 vextent = getSeqPanel().seqCanvas.getHeight() / av.getCharHeight();
688 if (vextent > height)
693 if ((hextent + x) > width)
698 if ((vextent + y) > height)
700 y = height - vextent;
713 hscroll.setValues(x, hextent, 0, width);
714 vscroll.setValues(y, vextent, 0, height);
723 public void adjustmentValueChanged(AdjustmentEvent evt)
725 int oldX = av.getStartRes();
726 int oldY = av.getStartSeq();
728 if (evt.getSource() == hscroll)
730 int x = hscroll.getValue();
732 av.setEndRes((x + (getSeqPanel().seqCanvas.getWidth() / av.getCharWidth())) - 1);
735 if (evt.getSource() == vscroll)
737 int offy = vscroll.getValue();
739 if (av.getWrapAlignment())
743 int rowSize = getSeqPanel().seqCanvas
744 .getWrappedCanvasWidth(getSeqPanel().seqCanvas.getWidth());
745 av.setStartRes(offy * rowSize);
746 av.setEndRes((offy + 1) * rowSize);
750 // This is only called if file loaded is a jar file that
751 // was wrapped when saved and user has wrap alignment true
752 // as preference setting
753 SwingUtilities.invokeLater(new Runnable()
757 setScrollValues(av.getStartRes(), av.getStartSeq());
764 av.setStartSeq(offy);
766 + (getSeqPanel().seqCanvas.getHeight() / av.getCharHeight()));
770 if (overviewPanel != null)
772 overviewPanel.setBoxPosition();
775 int scrollX = av.startRes - oldX;
776 int scrollY = av.startSeq - oldY;
778 if (av.getWrapAlignment() || !fastPaint)
784 // Make sure we're not trying to draw a panel
785 // larger than the visible window
786 if (scrollX > av.endRes - av.startRes)
788 scrollX = av.endRes - av.startRes;
790 else if (scrollX < av.startRes - av.endRes)
792 scrollX = av.startRes - av.endRes;
795 if (scrollX != 0 || scrollY != 0)
797 getIdPanel().getIdCanvas().fastPaint(scrollY);
798 getSeqPanel().seqCanvas.fastPaint(scrollX, scrollY);
799 getScalePanel().repaint();
801 if (av.isShowAnnotation() && scrollX != 0)
803 getAnnotationPanel().fastPaint(scrollX);
808 * If there is one, scroll the (Protein/cDNA) complementary alignment to
809 * match, unless we are ourselves doing that.
811 if (isFollowingComplementScroll())
813 setFollowingComplementScroll(false);
817 av.scrollComplementaryAlignment();
822 * Repaint the alignment including the annotations and overview panels (if
825 public void paintAlignment(boolean updateOverview)
827 final AnnotationSorter sorter = new AnnotationSorter(getAlignment(),
828 av.isShowAutocalculatedAbove());
829 sorter.sort(getAlignment()
830 .getAlignmentAnnotation(),
831 av.getSortAnnotationsBy());
836 av.getStructureSelectionManager().sequenceColoursChanged(this);
838 if (overviewPanel != null)
840 overviewPanel.updateOverviewImage();
851 public void paintComponent(Graphics g)
855 Dimension d = getIdPanel().getIdCanvas().getPreferredSize();
856 idPanelHolder.setPreferredSize(d);
857 hscrollFillerPanel.setPreferredSize(new Dimension(d.width, 12));
860 if (av.getWrapAlignment())
862 int maxwidth = av.getAlignment().getWidth();
864 if (av.hasHiddenColumns())
866 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
869 int canvasWidth = getSeqPanel().seqCanvas
870 .getWrappedCanvasWidth(getSeqPanel().seqCanvas.getWidth());
874 / getSeqPanel().seqCanvas
875 .getWrappedCanvasWidth(getSeqPanel().seqCanvas
877 vscroll.setMaximum(max);
878 vscroll.setUnitIncrement(1);
879 vscroll.setVisibleAmount(1);
884 setScrollValues(av.getStartRes(), av.getStartSeq());
898 * @return DOCUMENT ME!
900 * @throws PrinterException
903 public int print(Graphics pg, PageFormat pf, int pi)
904 throws PrinterException
906 pg.translate((int) pf.getImageableX(), (int) pf.getImageableY());
908 int pwidth = (int) pf.getImageableWidth();
909 int pheight = (int) pf.getImageableHeight();
911 if (av.getWrapAlignment())
913 return printWrappedAlignment(pg, pwidth, pheight, pi);
917 return printUnwrapped(pg, pwidth, pheight, pi);
933 * @return DOCUMENT ME!
935 * @throws PrinterException
938 public int printUnwrapped(Graphics pg, int pwidth, int pheight, int pi)
939 throws PrinterException
941 int idWidth = getVisibleIdWidth(false);
942 FontMetrics fm = getFontMetrics(av.getFont());
943 int scaleHeight = av.getCharHeight() + fm.getDescent();
945 pg.setColor(Color.white);
946 pg.fillRect(0, 0, pwidth, pheight);
947 pg.setFont(av.getFont());
949 // //////////////////////////////////
950 // / How many sequences and residues can we fit on a printable page?
951 int totalRes = (pwidth - idWidth) / av.getCharWidth();
953 int totalSeq = (pheight - scaleHeight) / av.getCharHeight() - 1;
955 int pagesWide = (av.getAlignment().getWidth() / totalRes) + 1;
957 // ///////////////////////////
958 // / Only print these sequences and residues on this page
961 // ///////////////////////////
962 // / Only print these sequences and residues on this page
965 // ///////////////////////////
966 // / Only print these sequences and residues on this page
969 // ///////////////////////////
970 // / Only print these sequences and residues on this page
972 startRes = (pi % pagesWide) * totalRes;
973 endRes = (startRes + totalRes) - 1;
975 if (endRes > (av.getAlignment().getWidth() - 1))
977 endRes = av.getAlignment().getWidth() - 1;
980 startSeq = (pi / pagesWide) * totalSeq;
981 endSeq = startSeq + totalSeq;
983 if (endSeq > av.getAlignment().getHeight())
985 endSeq = av.getAlignment().getHeight();
988 int pagesHigh = ((av.getAlignment().getHeight() / totalSeq) + 1)
991 if (av.isShowAnnotation())
993 pagesHigh += getAnnotationPanel().adjustPanelHeight() + 3;
996 pagesHigh /= pheight;
998 if (pi >= (pagesWide * pagesHigh))
1000 return Printable.NO_SUCH_PAGE;
1004 pg.translate(idWidth, 0);
1005 getScalePanel().drawScale(pg, startRes, endRes, pwidth - idWidth,
1007 pg.translate(-idWidth, scaleHeight);
1011 Color currentColor = null;
1012 Color currentTextColor = null;
1014 pg.setFont(getIdPanel().getIdCanvas().getIdfont());
1017 for (int i = startSeq; i < endSeq; i++)
1019 seq = av.getAlignment().getSequenceAt(i);
1020 if ((av.getSelectionGroup() != null)
1021 && av.getSelectionGroup().getSequences(null).contains(seq))
1023 currentColor = Color.gray;
1024 currentTextColor = Color.black;
1028 currentColor = av.getSequenceColour(seq);
1029 currentTextColor = Color.black;
1032 pg.setColor(currentColor);
1033 pg.fillRect(0, (i - startSeq) * av.getCharHeight(), idWidth,
1034 av.getCharHeight());
1036 pg.setColor(currentTextColor);
1039 if (av.isRightAlignIds())
1041 fm = pg.getFontMetrics();
1043 - fm.stringWidth(seq.getDisplayId(av.getShowJVSuffix()))
1048 seq.getDisplayId(av.getShowJVSuffix()),
1050 (((i - startSeq) * av.getCharHeight()) + av.getCharHeight())
1051 - (av.getCharHeight() / 5));
1054 pg.setFont(av.getFont());
1056 // draw main sequence panel
1057 pg.translate(idWidth, 0);
1058 getSeqPanel().seqCanvas.drawPanel(pg, startRes, endRes, startSeq, endSeq, 0);
1060 if (av.isShowAnnotation() && (endSeq == av.getAlignment().getHeight()))
1062 // draw annotation - need to offset for current scroll position
1063 int offset = -getAlabels().getScrollOffset();
1064 pg.translate(0, offset);
1065 pg.translate(-idWidth - 3, (endSeq - startSeq) * av.getCharHeight()
1067 getAlabels().drawComponent(pg, idWidth);
1068 pg.translate(idWidth + 3, 0);
1069 getAnnotationPanel().renderer.drawComponent(getAnnotationPanel(), av,
1070 pg, -1, startRes, endRes + 1);
1071 pg.translate(0, -offset);
1074 return Printable.PAGE_EXISTS;
1089 * @return DOCUMENT ME!
1091 * @throws PrinterException
1094 public int printWrappedAlignment(Graphics pg, int pwidth, int pheight,
1095 int pi) throws PrinterException
1097 int annotationHeight = 0;
1098 AnnotationLabels labels = null;
1099 if (av.isShowAnnotation())
1101 annotationHeight = getAnnotationPanel().adjustPanelHeight();
1102 labels = new AnnotationLabels(av);
1105 int hgap = av.getCharHeight();
1106 if (av.getScaleAboveWrapped())
1108 hgap += av.getCharHeight();
1111 int cHeight = av.getAlignment().getHeight() * av.getCharHeight() + hgap
1114 int idWidth = getVisibleIdWidth(false);
1116 int maxwidth = av.getAlignment().getWidth();
1117 if (av.hasHiddenColumns())
1119 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
1122 int resWidth = getSeqPanel().seqCanvas.getWrappedCanvasWidth(pwidth
1125 int totalHeight = cHeight * (maxwidth / resWidth + 1);
1127 pg.setColor(Color.white);
1128 pg.fillRect(0, 0, pwidth, pheight);
1129 pg.setFont(av.getFont());
1133 pg.setColor(Color.black);
1135 pg.translate(0, -pi * pheight);
1137 pg.setClip(0, pi * pheight, pwidth, pheight);
1143 for (int i = 0; i < av.getAlignment().getHeight(); i++)
1145 pg.setFont(getIdPanel().getIdCanvas().getIdfont());
1146 SequenceI s = av.getAlignment().getSequenceAt(i);
1147 String string = s.getDisplayId(av.getShowJVSuffix());
1149 if (av.isRightAlignIds())
1151 FontMetrics fm = pg.getFontMetrics();
1152 xPos = idWidth - fm.stringWidth(string) - 4;
1154 pg.drawString(string, xPos,
1155 ((i * av.getCharHeight()) + ypos + av.getCharHeight())
1156 - (av.getCharHeight() / 5));
1160 pg.translate(-3, ypos
1161 + (av.getAlignment().getHeight() * av.getCharHeight()));
1163 pg.setFont(av.getFont());
1164 labels.drawComponent(pg, idWidth);
1165 pg.translate(+3, -ypos
1166 - (av.getAlignment().getHeight() * av
1171 } while (ypos < totalHeight);
1173 pg.translate(idWidth, 0);
1175 getSeqPanel().seqCanvas.drawWrappedPanel(pg, pwidth - idWidth, totalHeight,
1178 if ((pi * pheight) < totalHeight)
1180 return Printable.PAGE_EXISTS;
1185 return Printable.NO_SUCH_PAGE;
1190 * get current sequence ID panel width, or nominal value if panel were to be
1191 * displayed using default settings
1195 public int getVisibleIdWidth()
1197 return getVisibleIdWidth(true);
1201 * get current sequence ID panel width, or nominal value if panel were to be
1202 * displayed using default settings
1205 * indicate if the Id width for onscreen or offscreen display should
1209 public int getVisibleIdWidth(boolean onscreen)
1211 // see if rendering offscreen - check preferences and calc width accordingly
1212 if (!onscreen && Cache.getDefault("FIGURE_AUTOIDWIDTH", false))
1214 return calculateIdWidth(-1).width + 4;
1216 Integer idwidth = null;
1218 || (idwidth = Cache.getIntegerProperty("FIGURE_FIXEDIDWIDTH")) == null)
1220 return (getIdPanel().getWidth() > 0 ? getIdPanel().getWidth()
1221 : calculateIdWidth().width + 4);
1223 return idwidth.intValue() + 4;
1226 void makeAlignmentImage(jalview.util.ImageMaker.TYPE type, File file)
1228 long progress = System.currentTimeMillis();
1229 headless = (System.getProperty("java.awt.headless") != null && System
1230 .getProperty("java.awt.headless").equals("true"));
1231 if (alignFrame != null && !headless)
1233 alignFrame.setProgressBar(MessageManager.formatMessage(
1234 "status.saving_file", new Object[]
1235 { type.getLabel() }), progress);
1239 AlignmentDimension aDimension = getAlignmentDimension();
1242 jalview.util.ImageMaker im;
1243 final String imageAction, imageTitle;
1244 if (type == jalview.util.ImageMaker.TYPE.PNG)
1246 imageAction = "Create PNG image from alignment";
1249 else if (type == jalview.util.ImageMaker.TYPE.EPS)
1251 imageAction = "Create EPS file from alignment";
1252 imageTitle = alignFrame.getTitle();
1256 imageAction = "Create SVG file from alignment";
1257 imageTitle = alignFrame.getTitle();
1260 im = new jalview.util.ImageMaker(this, type, imageAction,
1261 aDimension.getWidth(), aDimension.getHeight(), file,
1263 if (av.getWrapAlignment())
1265 if (im.getGraphics() != null)
1267 printWrappedAlignment(im.getGraphics(), aDimension.getWidth(),
1268 aDimension.getHeight(), 0);
1274 if (im.getGraphics() != null)
1276 printUnwrapped(im.getGraphics(), aDimension.getWidth(),
1277 aDimension.getHeight(), 0);
1281 } catch (OutOfMemoryError err)
1284 System.out.println("########################\n" + "OUT OF MEMORY "
1285 + file + "\n" + "########################");
1286 new OOMWarning("Creating Image for " + file, err);
1287 // System.out.println("Create IMAGE: " + err);
1288 } catch (Exception ex)
1290 ex.printStackTrace();
1294 if (alignFrame != null && !headless)
1296 alignFrame.setProgressBar(MessageManager.getString("status.export_complete"), progress);
1301 public AlignmentDimension getAlignmentDimension()
1303 int maxwidth = av.getAlignment().getWidth();
1304 if (av.hasHiddenColumns())
1306 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth);
1309 int height = ((av.getAlignment().getHeight() + 1) * av.getCharHeight())
1310 + getScalePanel().getHeight();
1311 int width = getVisibleIdWidth(false) + (maxwidth * av.getCharWidth());
1313 if (av.getWrapAlignment())
1315 height = getWrappedHeight();
1318 // need to obtain default alignment width and then add in any
1319 // additional allowance for id margin
1320 // this duplicates the calculation in getWrappedHeight but adjusts for
1322 width = alignFrame.getWidth() - vscroll.getPreferredSize().width
1323 - alignFrame.getInsets().left
1324 - alignFrame.getInsets().right - getVisibleIdWidth()
1325 + getVisibleIdWidth(false);
1329 width = getSeqPanel().getWidth() + getVisibleIdWidth(false);
1333 else if (av.isShowAnnotation())
1335 height += getAnnotationPanel().adjustPanelHeight() + 3;
1337 return new AlignmentDimension(width, height);
1344 public void makeEPS(File epsFile)
1346 makeAlignmentImage(jalview.util.ImageMaker.TYPE.EPS, epsFile);
1352 public void makePNG(File pngFile)
1354 makeAlignmentImage(jalview.util.ImageMaker.TYPE.PNG, pngFile);
1357 public void makeSVG(File svgFile)
1359 makeAlignmentImage(jalview.util.ImageMaker.TYPE.SVG, svgFile);
1361 public void makePNGImageMap(File imgMapFile, String imageName)
1363 // /////ONLY WORKS WITH NONE WRAPPED ALIGNMENTS
1364 // ////////////////////////////////////////////
1365 int idWidth = getVisibleIdWidth(false);
1366 FontMetrics fm = getFontMetrics(av.getFont());
1367 int scaleHeight = av.getCharHeight() + fm.getDescent();
1370 // ////////////////////////////////
1371 if (imgMapFile != null)
1375 int s, sSize = av.getAlignment().getHeight(), res, alwidth = av
1376 .getAlignment().getWidth(), g, gSize, f, fSize, sy;
1377 StringBuffer text = new StringBuffer();
1378 PrintWriter out = new PrintWriter(new FileWriter(imgMapFile));
1379 out.println(jalview.io.HTMLOutput.getImageMapHTML());
1380 out.println("<img src=\"" + imageName
1381 + "\" border=\"0\" usemap=\"#Map\" >"
1382 + "<map name=\"Map\">");
1384 for (s = 0; s < sSize; s++)
1386 sy = s * av.getCharHeight() + scaleHeight;
1388 SequenceI seq = av.getAlignment().getSequenceAt(s);
1389 SequenceFeature[] features = seq.getSequenceFeatures();
1390 SequenceGroup[] groups = av.getAlignment().findAllGroups(seq);
1391 for (res = 0; res < alwidth; res++)
1393 text = new StringBuffer();
1394 String triplet = null;
1395 if (av.getAlignment().isNucleotide())
1397 triplet = ResidueProperties.nucleotideName.get(seq
1403 triplet = ResidueProperties.aa2Triplet.get(seq.getCharAt(res)
1407 if (triplet == null)
1412 int alIndex = seq.findPosition(res);
1413 gSize = groups.length;
1414 for (g = 0; g < gSize; g++)
1416 if (text.length() < 1)
1418 text.append("<area shape=\"rect\" coords=\""
1419 + (idWidth + res * av.getCharWidth()) + "," + sy
1420 + "," + (idWidth + (res + 1) * av.getCharWidth())
1422 + (av.getCharHeight() + sy) + "\""
1423 + " onMouseOver=\"toolTip('" + alIndex + " "
1427 if (groups[g].getStartRes() < res
1428 && groups[g].getEndRes() > res)
1430 text.append("<br><em>" + groups[g].getName() + "</em>");
1434 if (features != null)
1436 if (text.length() < 1)
1438 text.append("<area shape=\"rect\" coords=\""
1439 + (idWidth + res * av.getCharWidth()) + "," + sy
1440 + "," + (idWidth + (res + 1) * av.getCharWidth())
1442 + (av.getCharHeight() + sy) + "\""
1443 + " onMouseOver=\"toolTip('" + alIndex + " "
1446 fSize = features.length;
1447 for (f = 0; f < fSize; f++)
1450 if ((features[f].getBegin() <= seq.findPosition(res))
1451 && (features[f].getEnd() >= seq.findPosition(res)))
1453 if (features[f].getType().equals("disulfide bond"))
1455 if (features[f].getBegin() == seq.findPosition(res)
1456 || features[f].getEnd() == seq
1459 text.append("<br>disulfide bond "
1460 + features[f].getBegin() + ":"
1461 + features[f].getEnd());
1466 text.append("<br>");
1467 text.append(features[f].getType());
1468 if (features[f].getDescription() != null
1469 && !features[f].getType().equals(
1470 features[f].getDescription()))
1472 text.append(" " + features[f].getDescription());
1475 if (features[f].getValue("status") != null)
1477 text.append(" (" + features[f].getValue("status")
1485 if (text.length() > 1)
1487 text.append("')\"; onMouseOut=\"toolTip()\"; href=\"#\">");
1488 out.println(text.toString());
1492 out.println("</map></body></html>");
1495 } catch (Exception ex)
1497 ex.printStackTrace();
1499 } // /////////END OF IMAGE MAP
1503 int getWrappedHeight()
1505 int seqPanelWidth = getSeqPanel().seqCanvas.getWidth();
1507 if (System.getProperty("java.awt.headless") != null
1508 && System.getProperty("java.awt.headless").equals("true"))
1510 seqPanelWidth = alignFrame.getWidth() - getVisibleIdWidth()
1511 - vscroll.getPreferredSize().width
1512 - alignFrame.getInsets().left - alignFrame.getInsets().right;
1515 int chunkWidth = getSeqPanel().seqCanvas
1516 .getWrappedCanvasWidth(seqPanelWidth);
1518 int hgap = av.getCharHeight();
1519 if (av.getScaleAboveWrapped())
1521 hgap += av.getCharHeight();
1524 int annotationHeight = 0;
1525 if (av.isShowAnnotation())
1527 annotationHeight = getAnnotationPanel().adjustPanelHeight();
1530 int cHeight = av.getAlignment().getHeight() * av.getCharHeight() + hgap
1533 int maxwidth = av.getAlignment().getWidth();
1534 if (av.hasHiddenColumns())
1536 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
1539 int height = ((maxwidth / chunkWidth) + 1) * cHeight;
1545 * close the panel - deregisters all listeners and nulls any references to
1548 public void closePanel()
1550 PaintRefresher.RemoveComponent(getSeqPanel().seqCanvas);
1551 PaintRefresher.RemoveComponent(getIdPanel().getIdCanvas());
1552 PaintRefresher.RemoveComponent(this);
1555 jalview.structure.StructureSelectionManager ssm = av
1556 .getStructureSelectionManager();
1557 ssm.removeStructureViewerListener(getSeqPanel(), null);
1558 ssm.removeSelectionListener(getSeqPanel());
1559 ssm.removeCommandListener(av);
1560 ssm.removeStructureViewerListener(getSeqPanel(), null);
1561 ssm.removeSelectionListener(getSeqPanel());
1562 av.setAlignment(null);
1567 if (Cache.log.isDebugEnabled())
1569 Cache.log.warn("Closing alignment panel which is already closed.");
1575 * hides or shows dynamic annotation rows based on groups and av state flags
1577 public void updateAnnotation()
1579 updateAnnotation(false, false);
1582 public void updateAnnotation(boolean applyGlobalSettings)
1584 updateAnnotation(applyGlobalSettings, false);
1587 public void updateAnnotation(boolean applyGlobalSettings,
1588 boolean preserveNewGroupSettings)
1590 av.updateGroupAnnotationSettings(applyGlobalSettings,
1591 preserveNewGroupSettings);
1592 adjustAnnotationHeight();
1596 public AlignmentI getAlignment()
1598 return av.getAlignment();
1603 public String getViewName()
1609 * Make/Unmake this alignment panel the current input focus
1613 public void setSelected(boolean b)
1617 alignFrame.setSelected(b);
1618 } catch (Exception ex)
1625 alignFrame.setDisplayedView(this);
1630 public StructureSelectionManager getStructureSelectionManager()
1632 return av.getStructureSelectionManager();
1636 public void raiseOOMWarning(String string, OutOfMemoryError error)
1638 new OOMWarning(string, error, this);
1642 public jalview.api.FeatureRenderer cloneFeatureRenderer()
1645 return new FeatureRenderer(this);
1648 public jalview.api.FeatureRenderer getFeatureRenderer()
1650 return seqPanel.seqCanvas.getFeatureRenderer();
1652 public void updateFeatureRenderer(jalview.renderer.seqfeatures.FeatureRenderer fr)
1654 fr.transferSettings(getSeqPanel().seqCanvas.getFeatureRenderer());
1657 public void updateFeatureRendererFrom(jalview.api.FeatureRenderer fr)
1659 if (getSeqPanel().seqCanvas.getFeatureRenderer() != null)
1661 getSeqPanel().seqCanvas.getFeatureRenderer().transferSettings(fr);
1665 public ScalePanel getScalePanel()
1670 public void setScalePanel(ScalePanel scalePanel)
1672 this.scalePanel = scalePanel;
1675 public SeqPanel getSeqPanel()
1680 public void setSeqPanel(SeqPanel seqPanel)
1682 this.seqPanel = seqPanel;
1685 public AnnotationPanel getAnnotationPanel()
1687 return annotationPanel;
1690 public void setAnnotationPanel(AnnotationPanel annotationPanel)
1692 this.annotationPanel = annotationPanel;
1695 public AnnotationLabels getAlabels()
1700 public void setAlabels(AnnotationLabels alabels)
1702 this.alabels = alabels;
1705 public IdPanel getIdPanel()
1710 public void setIdPanel(IdPanel idPanel)
1712 this.idPanel = idPanel;
1716 * Follow a scrolling change in the (cDNA/Protein) complementary alignment.
1717 * The aim is to keep the two alignments 'lined up' on their centre columns.
1720 * holds mapped region(s) of this alignment that we are scrolling
1721 * 'to'; may be modified for sequence offset by this method
1723 * the number of visible sequences to show above the mapped region
1725 public void scrollToCentre(SearchResults sr, int seqOffset)
1728 * To avoid jumpy vertical scrolling (if some sequences are gapped or not
1729 * mapped), we can make the scroll-to location a sequence above the one
1732 SequenceI mappedTo = sr.getResultSequence(0);
1733 List<SequenceI> seqs = av.getAlignment().getSequences();
1736 * This is like AlignmentI.findIndex(seq) but here we are matching the
1737 * dataset sequence not the aligned sequence
1739 int sequenceIndex = 0;
1740 boolean matched = false;
1741 for (SequenceI seq : seqs)
1743 if (mappedTo == seq.getDatasetSequence())
1752 return; // failsafe, shouldn't happen
1754 sequenceIndex = Math.max(0, sequenceIndex - seqOffset);
1755 sr.getResults().get(0)
1756 .setSequence(av.getAlignment().getSequenceAt(sequenceIndex));
1759 * Scroll to position but centring the target residue.
1761 scrollToPosition(sr, true, true);
1765 * Set a flag to say we are scrolling to follow a (cDNA/protein) complement.
1769 protected void setFollowingComplementScroll(boolean b)
1771 this.followingComplementScroll = b;
1774 protected boolean isFollowingComplementScroll()
1776 return this.followingComplementScroll;