2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 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.event.AdjustmentEvent;
46 import java.awt.event.AdjustmentListener;
47 import java.awt.print.PageFormat;
48 import java.awt.print.Printable;
49 import java.awt.print.PrinterException;
50 import java.beans.PropertyChangeEvent;
51 import java.beans.PropertyChangeListener;
53 import java.io.FileWriter;
54 import java.io.PrintWriter;
56 import javax.swing.SwingUtilities;
62 * @version $Revision: 1.161 $
64 public class AlignmentPanel extends GAlignmentPanel implements
65 AdjustmentListener, Printable, AlignmentViewPanel
67 public AlignViewport av;
69 OverviewPanel overviewPanel;
71 private SeqPanel seqPanel;
73 private IdPanel idPanel;
75 private boolean headless;
76 IdwidthAdjuster idwidthAdjuster;
79 public AlignFrame alignFrame;
81 private ScalePanel scalePanel;
83 private AnnotationPanel annotationPanel;
85 private AnnotationLabels alabels;
87 // this value is set false when selection area being dragged
88 boolean fastPaint = true;
95 * Creates a new AlignmentPanel object.
100 public AlignmentPanel(AlignFrame af, final AlignViewport av)
104 setSeqPanel(new SeqPanel(av, this));
105 setIdPanel(new IdPanel(av, this));
107 setScalePanel(new ScalePanel(av, this));
109 idPanelHolder.add(getIdPanel(), BorderLayout.CENTER);
110 idwidthAdjuster = new IdwidthAdjuster(this);
111 idSpaceFillerPanel1.add(idwidthAdjuster, BorderLayout.CENTER);
113 setAnnotationPanel(new AnnotationPanel(this));
114 setAlabels(new AnnotationLabels(this));
116 annotationScroller.setViewportView(getAnnotationPanel());
117 annotationSpaceFillerHolder.add(getAlabels(), BorderLayout.CENTER);
119 scalePanelHolder.add(getScalePanel(), BorderLayout.CENTER);
120 seqPanelHolder.add(getSeqPanel(), BorderLayout.CENTER);
122 setScrollValues(0, 0);
124 hscroll.addAdjustmentListener(this);
125 vscroll.addAdjustmentListener(this);
127 final AlignmentPanel ap = this;
128 av.addPropertyChangeListener(new PropertyChangeListener()
130 public void propertyChange(PropertyChangeEvent evt)
132 if (evt.getPropertyName().equals("alignment"))
134 PaintRefresher.Refresh(ap, av.getSequenceSetId(), true, true);
140 adjustAnnotationHeight();
145 public AlignViewportI getAlignViewport()
149 public void alignmentChanged()
151 av.alignmentChanged(this);
153 alignFrame.updateEditMenuBar();
155 paintAlignment(true);
162 public void fontChanged()
164 // set idCanvas bufferedImage to null
165 // to prevent drawing old image
166 FontMetrics fm = getFontMetrics(av.getFont());
168 scalePanelHolder.setPreferredSize(new Dimension(10, av.getCharHeight()
170 idSpaceFillerPanel1.setPreferredSize(new Dimension(10, av
174 getIdPanel().getIdCanvas().gg = null;
175 getSeqPanel().seqCanvas.img = null;
176 getAnnotationPanel().adjustPanelHeight();
178 Dimension d = calculateIdWidth();
180 d.setSize(d.width + 4, d.height);
181 getIdPanel().getIdCanvas().setPreferredSize(d);
182 hscrollFillerPanel.setPreferredSize(d);
184 if (overviewPanel != null)
186 overviewPanel.setBoxPosition();
193 * Calculate the width of the alignment labels based on the displayed names
194 * and any bounds on label width set in preferences.
196 * @return Dimension giving the maximum width of the alignment label panel
197 * that should be used.
199 public Dimension calculateIdWidth()
201 // calculate sensible default width when no preference is available
203 if (av.getIdWidth() < 0)
205 int afwidth = (alignFrame != null ? alignFrame.getWidth() : 300);
206 int maxwidth = Math.max(20, Math.min(afwidth - 200, 2 * afwidth / 3));
207 r = calculateIdWidth(maxwidth);
208 av.setIdWidth(r.width);
213 r.width = av.getIdWidth();
220 * Calculate the width of the alignment labels based on the displayed names
221 * and any bounds on label width set in preferences.
224 * -1 or maximum width allowed for IdWidth
225 * @return Dimension giving the maximum width of the alignment label panel
226 * that should be used.
228 public Dimension calculateIdWidth(int maxwidth)
230 Container c = new Container();
232 FontMetrics fm = c.getFontMetrics(new Font(av.font.getName(),
233 Font.ITALIC, av.font.getSize()));
235 AlignmentI al = av.getAlignment();
240 while ((i < al.getHeight()) && (al.getSequenceAt(i) != null))
242 SequenceI s = al.getSequenceAt(i);
244 id = s.getDisplayId(av.getShowJVSuffix());
246 if (fm.stringWidth(id) > idWidth)
248 idWidth = fm.stringWidth(id);
254 // Also check annotation label widths
257 if (al.getAlignmentAnnotation() != null)
259 fm = c.getFontMetrics(getAlabels().getFont());
261 while (i < al.getAlignmentAnnotation().length)
263 String label = al.getAlignmentAnnotation()[i].label;
265 if (fm.stringWidth(label) > idWidth)
267 idWidth = fm.stringWidth(label);
274 return new Dimension(maxwidth < 0 ? idWidth : Math.min(maxwidth,
279 * Highlight the given results on the alignment.
282 public void highlightSearchResults(SearchResults results)
284 scrollToPosition(results);
285 getSeqPanel().seqCanvas.highlightSearchResults(results);
289 * scroll the view to show the position of the highlighted region in results
290 * (if any) and redraw the overview
294 public boolean scrollToPosition(SearchResults results)
296 return scrollToPosition(results, true);
300 * scroll the view to show the position of the highlighted region in results
304 * @param redrawOverview
305 * - when set, the overview will be recalculated (takes longer)
306 * @return false if results were not found
308 public boolean scrollToPosition(SearchResults results,
309 boolean redrawOverview)
311 int startv, endv, starts, ends, width;
312 // TODO: properly locate search results in view when large numbers of hidden
313 // columns exist before highlighted region
314 // do we need to scroll the panel?
315 // TODO: tons of nullpointereexceptions raised here.
316 if (results != null && results.getSize() > 0 && av != null
317 && av.getAlignment() != null)
319 int seqIndex = av.getAlignment().findIndex(results);
324 SequenceI seq = av.getAlignment().getSequenceAt(seqIndex);
326 int[] r = results.getResults(seq, 0, av.getAlignment().getWidth());
333 // System.err.println("Seq : "+seqIndex+" Scroll to "+start+","+end); //
339 if (end == seq.getEnd())
343 if (av.hasHiddenColumns())
345 start = av.getColumnSelection().findColumnPosition(start);
346 end = av.getColumnSelection().findColumnPosition(end);
349 if (!av.getColumnSelection().isVisible(r[0]))
351 // don't scroll - position isn't visible
356 if (!av.getWrapAlignment())
358 if ((startv = av.getStartRes()) >= start)
360 setScrollValues(start - 1, seqIndex);
362 else if ((endv = av.getEndRes()) <= end)
364 setScrollValues(startv + 1 + end - endv, seqIndex);
366 else if ((starts = av.getStartSeq()) > seqIndex)
368 setScrollValues(av.getStartRes(), seqIndex);
370 else if ((ends = av.getEndSeq()) <= seqIndex)
372 setScrollValues(av.getStartRes(), starts + seqIndex - ends + 1);
377 scrollToWrappedVisible(start);
380 if (redrawOverview && overviewPanel != null)
382 overviewPanel.setBoxPosition();
384 paintAlignment(redrawOverview);
388 void scrollToWrappedVisible(int res)
390 int cwidth = getSeqPanel().seqCanvas
391 .getWrappedCanvasWidth(getSeqPanel().seqCanvas.getWidth());
392 if (res < av.getStartRes() || res >= (av.getStartRes() + cwidth))
394 vscroll.setValue((res / cwidth));
395 av.startRes = vscroll.getValue() * cwidth;
403 * @return DOCUMENT ME!
405 public OverviewPanel getOverviewPanel()
407 return overviewPanel;
416 public void setOverviewPanel(OverviewPanel op)
424 * Hide or show annotation panel
427 public void setAnnotationVisible(boolean b)
429 if (!av.getWrapAlignment())
431 annotationSpaceFillerHolder.setVisible(b);
432 annotationScroller.setVisible(b);
438 * automatically adjust annotation panel height for new annotation whilst
439 * ensuring the alignment is still visible.
441 public void adjustAnnotationHeight()
443 // TODO: display vertical annotation scrollbar if necessary
444 // this is called after loading new annotation onto alignment
445 if (alignFrame.getHeight() == 0)
447 System.out.println("NEEDS FIXING");
449 validateAnnotationDimensions(true);
451 paintAlignment(true);
455 * calculate the annotation dimensions and refresh slider values accordingly.
456 * need to do repaints/notifys afterwards.
458 protected void validateAnnotationDimensions(boolean adjustPanelHeight)
460 int height = getAnnotationPanel().adjustPanelHeight();
462 int theight = av.getCharHeight()
463 * (av.getAlignment().getHeight() + (!av.hasHiddenRows() ? 0
464 : av.getAlignment().getHiddenSequences().getSize()));
465 float sscaling = (float) (theight / (1.0 * theight + height));
466 float ascaling = (float) (height * 1.0 / alignFrame.getHeight());
467 int rheight = alignFrame.getHeight() - height - av.getCharHeight();
468 if (adjustPanelHeight)
470 // NOTE: this logic is different in the applet. Need a better algorithm to
472 // try and set height according to alignment
473 if (ascaling > 0 && sscaling < 0.5)
475 // if the alignment is too big then
476 // default is 0.5 split
477 height = alignFrame.getHeight() / 2;
481 // if space for more than one sequence row left when annotation is fully
482 // displayed then set height to annotation height
483 // otherwise, leave at least two lines of sequence shown.
484 height = (rheight > av.getCharHeight()) ? height
485 : (-av.getCharHeight() * 3 + (int) (alignFrame.getHeight() * (1 - sscaling)));
490 // maintain same window layout whilst updating sliders
491 height = annotationScroller.getSize().height;
495 annotationScroller.setPreferredSize(new Dimension(annotationScroller
496 .getWidth(), height));
498 annotationSpaceFillerHolder.setPreferredSize(new Dimension(
499 annotationSpaceFillerHolder.getWidth(), height));
500 annotationScroller.validate();// repaint();
501 annotationScroller.addNotify();
505 * update alignment layout for viewport settings
510 public void updateLayout()
513 setAnnotationVisible(av.isShowAnnotation());
514 boolean wrap = av.getWrapAlignment();
516 scalePanelHolder.setVisible(!wrap);
517 hscroll.setVisible(!wrap);
518 idwidthAdjuster.setVisible(!wrap);
522 annotationScroller.setVisible(false);
523 annotationSpaceFillerHolder.setVisible(false);
525 else if (av.isShowAnnotation())
527 annotationScroller.setVisible(true);
528 annotationSpaceFillerHolder.setVisible(true);
531 idSpaceFillerPanel1.setVisible(!wrap);
536 // return value is true if the scroll is valid
537 public boolean scrollUp(boolean up)
541 if (vscroll.getValue() < 1)
547 vscroll.setValue(vscroll.getValue() - 1);
551 if ((vextent + vscroll.getValue()) >= av.getAlignment().getHeight())
557 vscroll.setValue(vscroll.getValue() + 1);
571 * @return DOCUMENT ME!
573 public boolean scrollRight(boolean right)
577 if (hscroll.getValue() < 1)
583 hscroll.setValue(hscroll.getValue() - 1);
587 if ((hextent + hscroll.getValue()) >= av.getAlignment().getWidth())
593 hscroll.setValue(hscroll.getValue() + 1);
602 * Adjust row/column scrollers to show a visible position in the alignment.
605 * visible column to scroll to DOCUMENT ME!
607 * visible row to scroll to
610 public void setScrollValues(int x, int y)
612 // System.err.println("Scroll to "+x+","+y);
613 if (av == null || av.getAlignment() == null)
617 int width = av.getAlignment().getWidth();
618 int height = av.getAlignment().getHeight();
620 if (av.hasHiddenColumns())
622 width = av.getColumnSelection().findColumnPosition(width);
625 av.setEndRes((x + (getSeqPanel().seqCanvas.getWidth() / av
626 .getCharWidth())) - 1);
628 hextent = getSeqPanel().seqCanvas.getWidth() / av.getCharWidth();
629 vextent = getSeqPanel().seqCanvas.getHeight() / av.getCharHeight();
636 if (vextent > height)
641 if ((hextent + x) > width)
646 if ((vextent + y) > height)
648 y = height - vextent;
661 hscroll.setValues(x, hextent, 0, width);
662 vscroll.setValues(y, vextent, 0, height);
671 public void adjustmentValueChanged(AdjustmentEvent evt)
673 int oldX = av.getStartRes();
674 int oldY = av.getStartSeq();
676 if (evt.getSource() == hscroll)
678 int x = hscroll.getValue();
680 av.setEndRes((x + (getSeqPanel().seqCanvas.getWidth() / av.getCharWidth())) - 1);
683 if (evt.getSource() == vscroll)
685 int offy = vscroll.getValue();
687 if (av.getWrapAlignment())
691 int rowSize = getSeqPanel().seqCanvas
692 .getWrappedCanvasWidth(getSeqPanel().seqCanvas.getWidth());
693 av.setStartRes(offy * rowSize);
694 av.setEndRes((offy + 1) * rowSize);
698 // This is only called if file loaded is a jar file that
699 // was wrapped when saved and user has wrap alignment true
700 // as preference setting
701 SwingUtilities.invokeLater(new Runnable()
705 setScrollValues(av.getStartRes(), av.getStartSeq());
712 av.setStartSeq(offy);
714 + (getSeqPanel().seqCanvas.getHeight() / av.getCharHeight()));
718 if (overviewPanel != null)
720 overviewPanel.setBoxPosition();
723 int scrollX = av.startRes - oldX;
724 int scrollY = av.startSeq - oldY;
726 if (av.getWrapAlignment() || !fastPaint)
732 // Make sure we're not trying to draw a panel
733 // larger than the visible window
734 if (scrollX > av.endRes - av.startRes)
736 scrollX = av.endRes - av.startRes;
738 else if (scrollX < av.startRes - av.endRes)
740 scrollX = av.startRes - av.endRes;
743 if (scrollX != 0 || scrollY != 0)
745 getIdPanel().getIdCanvas().fastPaint(scrollY);
746 getSeqPanel().seqCanvas.fastPaint(scrollX, scrollY);
747 getScalePanel().repaint();
749 if (av.isShowAnnotation() && scrollX != 0)
751 getAnnotationPanel().fastPaint(scrollX);
758 * Repaint the alignment including the annotations and overview panels (if
761 public void paintAlignment(boolean updateOverview)
763 final AnnotationSorter sorter = new AnnotationSorter(getAlignment(),
764 av.isShowAutocalculatedAbove());
765 sorter.sort(getAlignment()
766 .getAlignmentAnnotation(),
767 av.getSortAnnotationsBy());
772 av.getStructureSelectionManager().sequenceColoursChanged(this);
774 if (overviewPanel != null)
776 overviewPanel.updateOverviewImage();
787 public void paintComponent(Graphics g)
791 Dimension d = getIdPanel().getIdCanvas().getPreferredSize();
792 idPanelHolder.setPreferredSize(d);
793 hscrollFillerPanel.setPreferredSize(new Dimension(d.width, 12));
796 if (av.getWrapAlignment())
798 int maxwidth = av.getAlignment().getWidth();
800 if (av.hasHiddenColumns())
802 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
805 int canvasWidth = getSeqPanel().seqCanvas
806 .getWrappedCanvasWidth(getSeqPanel().seqCanvas.getWidth());
810 / getSeqPanel().seqCanvas
811 .getWrappedCanvasWidth(getSeqPanel().seqCanvas
813 vscroll.setMaximum(max);
814 vscroll.setUnitIncrement(1);
815 vscroll.setVisibleAmount(1);
820 setScrollValues(av.getStartRes(), av.getStartSeq());
834 * @return DOCUMENT ME!
836 * @throws PrinterException
839 public int print(Graphics pg, PageFormat pf, int pi)
840 throws PrinterException
842 pg.translate((int) pf.getImageableX(), (int) pf.getImageableY());
844 int pwidth = (int) pf.getImageableWidth();
845 int pheight = (int) pf.getImageableHeight();
847 if (av.getWrapAlignment())
849 return printWrappedAlignment(pg, pwidth, pheight, pi);
853 return printUnwrapped(pg, pwidth, pheight, pi);
869 * @return DOCUMENT ME!
871 * @throws PrinterException
874 public int printUnwrapped(Graphics pg, int pwidth, int pheight, int pi)
875 throws PrinterException
877 int idWidth = getVisibleIdWidth(false);
878 FontMetrics fm = getFontMetrics(av.getFont());
879 int scaleHeight = av.getCharHeight() + fm.getDescent();
881 pg.setColor(Color.white);
882 pg.fillRect(0, 0, pwidth, pheight);
883 pg.setFont(av.getFont());
885 // //////////////////////////////////
886 // / How many sequences and residues can we fit on a printable page?
887 int totalRes = (pwidth - idWidth) / av.getCharWidth();
889 int totalSeq = (pheight - scaleHeight) / av.getCharHeight() - 1;
891 int pagesWide = (av.getAlignment().getWidth() / totalRes) + 1;
893 // ///////////////////////////
894 // / Only print these sequences and residues on this page
897 // ///////////////////////////
898 // / Only print these sequences and residues on this page
901 // ///////////////////////////
902 // / Only print these sequences and residues on this page
905 // ///////////////////////////
906 // / Only print these sequences and residues on this page
908 startRes = (pi % pagesWide) * totalRes;
909 endRes = (startRes + totalRes) - 1;
911 if (endRes > (av.getAlignment().getWidth() - 1))
913 endRes = av.getAlignment().getWidth() - 1;
916 startSeq = (pi / pagesWide) * totalSeq;
917 endSeq = startSeq + totalSeq;
919 if (endSeq > av.getAlignment().getHeight())
921 endSeq = av.getAlignment().getHeight();
924 int pagesHigh = ((av.getAlignment().getHeight() / totalSeq) + 1)
927 if (av.isShowAnnotation())
929 pagesHigh += getAnnotationPanel().adjustPanelHeight() + 3;
932 pagesHigh /= pheight;
934 if (pi >= (pagesWide * pagesHigh))
936 return Printable.NO_SUCH_PAGE;
940 pg.translate(idWidth, 0);
941 getScalePanel().drawScale(pg, startRes, endRes, pwidth - idWidth,
943 pg.translate(-idWidth, scaleHeight);
947 Color currentColor = null;
948 Color currentTextColor = null;
950 pg.setFont(getIdPanel().getIdCanvas().getIdfont());
953 for (int i = startSeq; i < endSeq; i++)
955 seq = av.getAlignment().getSequenceAt(i);
956 if ((av.getSelectionGroup() != null)
957 && av.getSelectionGroup().getSequences(null).contains(seq))
959 currentColor = Color.gray;
960 currentTextColor = Color.black;
964 currentColor = av.getSequenceColour(seq);
965 currentTextColor = Color.black;
968 pg.setColor(currentColor);
969 pg.fillRect(0, (i - startSeq) * av.getCharHeight(), idWidth,
972 pg.setColor(currentTextColor);
975 if (av.isRightAlignIds())
977 fm = pg.getFontMetrics();
979 - fm.stringWidth(seq.getDisplayId(av.getShowJVSuffix()))
984 seq.getDisplayId(av.getShowJVSuffix()),
986 (((i - startSeq) * av.getCharHeight()) + av.getCharHeight())
987 - (av.getCharHeight() / 5));
990 pg.setFont(av.getFont());
992 // draw main sequence panel
993 pg.translate(idWidth, 0);
994 getSeqPanel().seqCanvas.drawPanel(pg, startRes, endRes, startSeq, endSeq, 0);
996 if (av.isShowAnnotation() && (endSeq == av.getAlignment().getHeight()))
998 // draw annotation - need to offset for current scroll position
999 int offset = -getAlabels().getScrollOffset();
1000 pg.translate(0, offset);
1001 pg.translate(-idWidth - 3, (endSeq - startSeq) * av.getCharHeight()
1003 getAlabels().drawComponent(pg, idWidth);
1004 pg.translate(idWidth + 3, 0);
1005 getAnnotationPanel().renderer.drawComponent(getAnnotationPanel(), av,
1006 pg, -1, startRes, endRes + 1);
1007 pg.translate(0, -offset);
1010 return Printable.PAGE_EXISTS;
1025 * @return DOCUMENT ME!
1027 * @throws PrinterException
1030 public int printWrappedAlignment(Graphics pg, int pwidth, int pheight,
1031 int pi) throws PrinterException
1033 int annotationHeight = 0;
1034 AnnotationLabels labels = null;
1035 if (av.isShowAnnotation())
1037 annotationHeight = getAnnotationPanel().adjustPanelHeight();
1038 labels = new AnnotationLabels(av);
1041 int hgap = av.getCharHeight();
1042 if (av.getScaleAboveWrapped())
1044 hgap += av.getCharHeight();
1047 int cHeight = av.getAlignment().getHeight() * av.getCharHeight() + hgap
1050 int idWidth = getVisibleIdWidth(false);
1052 int maxwidth = av.getAlignment().getWidth();
1053 if (av.hasHiddenColumns())
1055 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
1058 int resWidth = getSeqPanel().seqCanvas.getWrappedCanvasWidth(pwidth
1061 int totalHeight = cHeight * (maxwidth / resWidth + 1);
1063 pg.setColor(Color.white);
1064 pg.fillRect(0, 0, pwidth, pheight);
1065 pg.setFont(av.getFont());
1069 pg.setColor(Color.black);
1071 pg.translate(0, -pi * pheight);
1073 pg.setClip(0, pi * pheight, pwidth, pheight);
1079 for (int i = 0; i < av.getAlignment().getHeight(); i++)
1081 pg.setFont(getIdPanel().getIdCanvas().getIdfont());
1082 SequenceI s = av.getAlignment().getSequenceAt(i);
1083 String string = s.getDisplayId(av.getShowJVSuffix());
1085 if (av.isRightAlignIds())
1087 FontMetrics fm = pg.getFontMetrics();
1088 xPos = idWidth - fm.stringWidth(string) - 4;
1090 pg.drawString(string, xPos,
1091 ((i * av.getCharHeight()) + ypos + av.getCharHeight())
1092 - (av.getCharHeight() / 5));
1096 pg.translate(-3, ypos
1097 + (av.getAlignment().getHeight() * av.getCharHeight()));
1099 pg.setFont(av.getFont());
1100 labels.drawComponent(pg, idWidth);
1101 pg.translate(+3, -ypos
1102 - (av.getAlignment().getHeight() * av
1107 } while (ypos < totalHeight);
1109 pg.translate(idWidth, 0);
1111 getSeqPanel().seqCanvas.drawWrappedPanel(pg, pwidth - idWidth, totalHeight,
1114 if ((pi * pheight) < totalHeight)
1116 return Printable.PAGE_EXISTS;
1121 return Printable.NO_SUCH_PAGE;
1126 * get current sequence ID panel width, or nominal value if panel were to be
1127 * displayed using default settings
1131 public int getVisibleIdWidth()
1133 return getVisibleIdWidth(true);
1137 * get current sequence ID panel width, or nominal value if panel were to be
1138 * displayed using default settings
1141 * indicate if the Id width for onscreen or offscreen display should
1145 public int getVisibleIdWidth(boolean onscreen)
1147 // see if rendering offscreen - check preferences and calc width accordingly
1148 if (!onscreen && Cache.getDefault("FIGURE_AUTOIDWIDTH", false))
1150 return calculateIdWidth(-1).width + 4;
1152 Integer idwidth = null;
1154 || (idwidth = Cache.getIntegerProperty("FIGURE_FIXEDIDWIDTH")) == null)
1156 return (getIdPanel().getWidth() > 0 ? getIdPanel().getWidth()
1157 : calculateIdWidth().width + 4);
1159 return idwidth.intValue() + 4;
1162 void makeAlignmentImage(jalview.util.ImageMaker.TYPE type, File file)
1164 long progress = System.currentTimeMillis();
1165 headless = (System.getProperty("java.awt.headless") != null && System
1166 .getProperty("java.awt.headless").equals("true"));
1167 if (alignFrame != null && !headless)
1169 alignFrame.setProgressBar(MessageManager.formatMessage(
1170 "status.saving_file", new Object[]
1171 { type.getLabel() }), progress);
1175 AlignmentDimension aDimension = getAlignmentDimension();
1178 jalview.util.ImageMaker im;
1179 final String imageAction, imageTitle;
1180 if (type == jalview.util.ImageMaker.TYPE.PNG)
1182 imageAction = "Create PNG image from alignment";
1185 else if (type == jalview.util.ImageMaker.TYPE.EPS)
1187 imageAction = "Create EPS file from alignment";
1188 imageTitle = alignFrame.getTitle();
1192 imageAction = "Create SVG file from alignment";
1193 imageTitle = alignFrame.getTitle();
1196 im = new jalview.util.ImageMaker(this, type, imageAction,
1197 aDimension.getWidth(), aDimension.getHeight(), file,
1199 if (av.getWrapAlignment())
1201 if (im.getGraphics() != null)
1203 printWrappedAlignment(im.getGraphics(), aDimension.getWidth(),
1204 aDimension.getHeight(), 0);
1210 if (im.getGraphics() != null)
1212 printUnwrapped(im.getGraphics(), aDimension.getWidth(),
1213 aDimension.getHeight(), 0);
1217 } catch (OutOfMemoryError err)
1220 System.out.println("########################\n" + "OUT OF MEMORY "
1221 + file + "\n" + "########################");
1222 new OOMWarning("Creating Image for " + file, err);
1223 // System.out.println("Create IMAGE: " + err);
1224 } catch (Exception ex)
1226 ex.printStackTrace();
1230 if (alignFrame != null && !headless)
1232 alignFrame.setProgressBar(MessageManager.getString("status.export_complete"), progress);
1237 public AlignmentDimension getAlignmentDimension()
1239 int maxwidth = av.getAlignment().getWidth();
1240 if (av.hasHiddenColumns())
1242 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth);
1245 int height = ((av.getAlignment().getHeight() + 1) * av.getCharHeight())
1246 + getScalePanel().getHeight();
1247 int width = getVisibleIdWidth(false) + (maxwidth * av.getCharWidth());
1249 if (av.getWrapAlignment())
1251 height = getWrappedHeight();
1254 // need to obtain default alignment width and then add in any
1255 // additional allowance for id margin
1256 // this duplicates the calculation in getWrappedHeight but adjusts for
1258 width = alignFrame.getWidth() - vscroll.getPreferredSize().width
1259 - alignFrame.getInsets().left
1260 - alignFrame.getInsets().right - getVisibleIdWidth()
1261 + getVisibleIdWidth(false);
1265 width = getSeqPanel().getWidth() + getVisibleIdWidth(false);
1269 else if (av.isShowAnnotation())
1271 height += getAnnotationPanel().adjustPanelHeight() + 3;
1273 return new AlignmentDimension(width, height);
1280 public void makeEPS(File epsFile)
1282 makeAlignmentImage(jalview.util.ImageMaker.TYPE.EPS, epsFile);
1288 public void makePNG(File pngFile)
1290 makeAlignmentImage(jalview.util.ImageMaker.TYPE.PNG, pngFile);
1293 public void makeSVG(File svgFile)
1295 makeAlignmentImage(jalview.util.ImageMaker.TYPE.SVG, svgFile);
1297 public void makePNGImageMap(File imgMapFile, String imageName)
1299 // /////ONLY WORKS WITH NONE WRAPPED ALIGNMENTS
1300 // ////////////////////////////////////////////
1301 int idWidth = getVisibleIdWidth(false);
1302 FontMetrics fm = getFontMetrics(av.getFont());
1303 int scaleHeight = av.getCharHeight() + fm.getDescent();
1306 // ////////////////////////////////
1307 if (imgMapFile != null)
1311 int s, sSize = av.getAlignment().getHeight(), res, alwidth = av
1312 .getAlignment().getWidth(), g, gSize, f, fSize, sy;
1313 StringBuffer text = new StringBuffer();
1314 PrintWriter out = new PrintWriter(new FileWriter(imgMapFile));
1315 out.println(jalview.io.HTMLOutput.getImageMapHTML());
1316 out.println("<img src=\"" + imageName
1317 + "\" border=\"0\" usemap=\"#Map\" >"
1318 + "<map name=\"Map\">");
1320 for (s = 0; s < sSize; s++)
1322 sy = s * av.getCharHeight() + scaleHeight;
1324 SequenceI seq = av.getAlignment().getSequenceAt(s);
1325 SequenceFeature[] features = seq.getDatasetSequence()
1326 .getSequenceFeatures();
1327 SequenceGroup[] groups = av.getAlignment().findAllGroups(seq);
1328 for (res = 0; res < alwidth; res++)
1330 text = new StringBuffer();
1332 if (av.getAlignment().isNucleotide())
1334 obj = ResidueProperties.nucleotideName.get(seq.getCharAt(res)
1339 obj = ResidueProperties.aa2Triplet.get(seq.getCharAt(res)
1348 String triplet = obj.toString();
1349 int alIndex = seq.findPosition(res);
1350 gSize = groups.length;
1351 for (g = 0; g < gSize; g++)
1353 if (text.length() < 1)
1355 text.append("<area shape=\"rect\" coords=\""
1356 + (idWidth + res * av.getCharWidth()) + "," + sy
1357 + "," + (idWidth + (res + 1) * av.getCharWidth())
1359 + (av.getCharHeight() + sy) + "\""
1360 + " onMouseOver=\"toolTip('" + alIndex + " "
1364 if (groups[g].getStartRes() < res
1365 && groups[g].getEndRes() > res)
1367 text.append("<br><em>" + groups[g].getName() + "</em>");
1371 if (features != null)
1373 if (text.length() < 1)
1375 text.append("<area shape=\"rect\" coords=\""
1376 + (idWidth + res * av.getCharWidth()) + "," + sy
1377 + "," + (idWidth + (res + 1) * av.getCharWidth())
1379 + (av.getCharHeight() + sy) + "\""
1380 + " onMouseOver=\"toolTip('" + alIndex + " "
1383 fSize = features.length;
1384 for (f = 0; f < fSize; f++)
1387 if ((features[f].getBegin() <= seq.findPosition(res))
1388 && (features[f].getEnd() >= seq.findPosition(res)))
1390 if (features[f].getType().equals("disulfide bond"))
1392 if (features[f].getBegin() == seq.findPosition(res)
1393 || features[f].getEnd() == seq
1396 text.append("<br>disulfide bond "
1397 + features[f].getBegin() + ":"
1398 + features[f].getEnd());
1403 text.append("<br>");
1404 text.append(features[f].getType());
1405 if (features[f].getDescription() != null
1406 && !features[f].getType().equals(
1407 features[f].getDescription()))
1409 text.append(" " + features[f].getDescription());
1412 if (features[f].getValue("status") != null)
1414 text.append(" (" + features[f].getValue("status")
1422 if (text.length() > 1)
1424 text.append("')\"; onMouseOut=\"toolTip()\"; href=\"#\">");
1425 out.println(text.toString());
1429 out.println("</map></body></html>");
1432 } catch (Exception ex)
1434 ex.printStackTrace();
1436 } // /////////END OF IMAGE MAP
1440 int getWrappedHeight()
1442 int seqPanelWidth = getSeqPanel().seqCanvas.getWidth();
1444 if (System.getProperty("java.awt.headless") != null
1445 && System.getProperty("java.awt.headless").equals("true"))
1447 seqPanelWidth = alignFrame.getWidth() - getVisibleIdWidth()
1448 - vscroll.getPreferredSize().width
1449 - alignFrame.getInsets().left - alignFrame.getInsets().right;
1452 int chunkWidth = getSeqPanel().seqCanvas
1453 .getWrappedCanvasWidth(seqPanelWidth);
1455 int hgap = av.getCharHeight();
1456 if (av.getScaleAboveWrapped())
1458 hgap += av.getCharHeight();
1461 int annotationHeight = 0;
1462 if (av.isShowAnnotation())
1464 annotationHeight = getAnnotationPanel().adjustPanelHeight();
1467 int cHeight = av.getAlignment().getHeight() * av.getCharHeight() + hgap
1470 int maxwidth = av.getAlignment().getWidth();
1471 if (av.hasHiddenColumns())
1473 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
1476 int height = ((maxwidth / chunkWidth) + 1) * cHeight;
1482 * close the panel - deregisters all listeners and nulls any references to
1485 public void closePanel()
1487 PaintRefresher.RemoveComponent(getSeqPanel().seqCanvas);
1488 PaintRefresher.RemoveComponent(getIdPanel().getIdCanvas());
1489 PaintRefresher.RemoveComponent(this);
1492 jalview.structure.StructureSelectionManager ssm = av
1493 .getStructureSelectionManager();
1494 ssm.removeStructureViewerListener(getSeqPanel(), null);
1495 ssm.removeSelectionListener(getSeqPanel());
1496 ssm.removeEditListener(av);
1497 ssm.removeStructureViewerListener(getSeqPanel(), null);
1498 ssm.removeSelectionListener(getSeqPanel());
1499 av.setAlignment(null);
1504 if (Cache.log.isDebugEnabled())
1506 Cache.log.warn("Closing alignment panel which is already closed.");
1512 * hides or shows dynamic annotation rows based on groups and av state flags
1514 public void updateAnnotation()
1516 updateAnnotation(false, false);
1519 public void updateAnnotation(boolean applyGlobalSettings)
1521 updateAnnotation(applyGlobalSettings, false);
1524 public void updateAnnotation(boolean applyGlobalSettings,
1525 boolean preserveNewGroupSettings)
1527 av.updateGroupAnnotationSettings(applyGlobalSettings,
1528 preserveNewGroupSettings);
1529 adjustAnnotationHeight();
1533 public AlignmentI getAlignment()
1535 return av.getAlignment();
1540 public String getViewName()
1546 * Make/Unmake this alignment panel the current input focus
1550 public void setSelected(boolean b)
1554 alignFrame.setSelected(b);
1555 } catch (Exception ex)
1562 alignFrame.setDisplayedView(this);
1567 public StructureSelectionManager getStructureSelectionManager()
1569 return av.getStructureSelectionManager();
1573 public void raiseOOMWarning(String string, OutOfMemoryError error)
1575 new OOMWarning(string, error, this);
1579 public jalview.api.FeatureRenderer cloneFeatureRenderer()
1582 return new FeatureRenderer(this);
1585 public jalview.api.FeatureRenderer getFeatureRenderer()
1587 return seqPanel.seqCanvas.getFeatureRenderer();
1589 public void updateFeatureRenderer(jalview.renderer.seqfeatures.FeatureRenderer fr)
1591 fr.transferSettings(getSeqPanel().seqCanvas.getFeatureRenderer());
1594 public void updateFeatureRendererFrom(jalview.api.FeatureRenderer fr)
1596 if (getSeqPanel().seqCanvas.getFeatureRenderer() != null)
1598 getSeqPanel().seqCanvas.getFeatureRenderer().transferSettings(fr);
1602 public ScalePanel getScalePanel()
1607 public void setScalePanel(ScalePanel scalePanel)
1609 this.scalePanel = scalePanel;
1612 public SeqPanel getSeqPanel()
1617 public void setSeqPanel(SeqPanel seqPanel)
1619 this.seqPanel = seqPanel;
1622 public AnnotationPanel getAnnotationPanel()
1624 return annotationPanel;
1627 public void setAnnotationPanel(AnnotationPanel annotationPanel)
1629 this.annotationPanel = annotationPanel;
1632 public AnnotationLabels getAlabels()
1637 public void setAlabels(AnnotationLabels alabels)
1639 this.alabels = alabels;
1642 public IdPanel getIdPanel()
1647 public void setIdPanel(IdPanel idPanel)
1649 this.idPanel = idPanel;