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.
102 public AlignmentPanel(AlignFrame af, final AlignViewport av)
106 setSeqPanel(new SeqPanel(av, this));
107 setIdPanel(new IdPanel(av, this));
109 setScalePanel(new ScalePanel(av, this));
111 idPanelHolder.add(getIdPanel(), BorderLayout.CENTER);
112 idwidthAdjuster = new IdwidthAdjuster(this);
113 idSpaceFillerPanel1.add(idwidthAdjuster, BorderLayout.CENTER);
115 setAnnotationPanel(new AnnotationPanel(this));
116 setAlabels(new AnnotationLabels(this));
118 annotationScroller.setViewportView(getAnnotationPanel());
119 annotationSpaceFillerHolder.add(getAlabels(), BorderLayout.CENTER);
121 scalePanelHolder.add(getScalePanel(), BorderLayout.CENTER);
122 seqPanelHolder.add(getSeqPanel(), BorderLayout.CENTER);
124 setScrollValues(0, 0);
126 hscroll.addAdjustmentListener(this);
127 vscroll.addAdjustmentListener(this);
129 final AlignmentPanel ap = this;
130 av.addPropertyChangeListener(new PropertyChangeListener()
132 public void propertyChange(PropertyChangeEvent evt)
134 if (evt.getPropertyName().equals("alignment"))
136 PaintRefresher.Refresh(ap, av.getSequenceSetId(), true, true);
142 adjustAnnotationHeight();
147 public AlignViewportI getAlignViewport()
151 public void alignmentChanged()
153 av.alignmentChanged(this);
155 alignFrame.updateEditMenuBar();
157 paintAlignment(true);
164 public void fontChanged()
166 // set idCanvas bufferedImage to null
167 // to prevent drawing old image
168 FontMetrics fm = getFontMetrics(av.getFont());
170 scalePanelHolder.setPreferredSize(new Dimension(10, av.getCharHeight()
172 idSpaceFillerPanel1.setPreferredSize(new Dimension(10, av
176 getIdPanel().getIdCanvas().gg = null;
177 getSeqPanel().seqCanvas.img = null;
178 getAnnotationPanel().adjustPanelHeight();
180 Dimension d = calculateIdWidth();
182 d.setSize(d.width + 4, d.height);
183 getIdPanel().getIdCanvas().setPreferredSize(d);
184 hscrollFillerPanel.setPreferredSize(d);
186 if (overviewPanel != null)
188 overviewPanel.setBoxPosition();
195 * Calculate the width of the alignment labels based on the displayed names
196 * and any bounds on label width set in preferences.
198 * @return Dimension giving the maximum width of the alignment label panel
199 * that should be used.
201 public Dimension calculateIdWidth()
203 // calculate sensible default width when no preference is available
205 if (av.getIdWidth() < 0)
207 int afwidth = (alignFrame != null ? alignFrame.getWidth() : 300);
208 int maxwidth = Math.max(20, Math.min(afwidth - 200, 2 * afwidth / 3));
209 r = calculateIdWidth(maxwidth);
210 av.setIdWidth(r.width);
215 r.width = av.getIdWidth();
222 * Calculate the width of the alignment labels based on the displayed names
223 * and any bounds on label width set in preferences.
226 * -1 or maximum width allowed for IdWidth
227 * @return Dimension giving the maximum width of the alignment label panel
228 * that should be used.
230 public Dimension calculateIdWidth(int maxwidth)
232 Container c = new Container();
234 FontMetrics fm = c.getFontMetrics(new Font(av.font.getName(),
235 Font.ITALIC, av.font.getSize()));
237 AlignmentI al = av.getAlignment();
242 while ((i < al.getHeight()) && (al.getSequenceAt(i) != null))
244 SequenceI s = al.getSequenceAt(i);
246 id = s.getDisplayId(av.getShowJVSuffix());
248 if (fm.stringWidth(id) > idWidth)
250 idWidth = fm.stringWidth(id);
256 // Also check annotation label widths
259 if (al.getAlignmentAnnotation() != null)
261 fm = c.getFontMetrics(getAlabels().getFont());
263 while (i < al.getAlignmentAnnotation().length)
265 String label = al.getAlignmentAnnotation()[i].label;
267 if (fm.stringWidth(label) > idWidth)
269 idWidth = fm.stringWidth(label);
276 return new Dimension(maxwidth < 0 ? idWidth : Math.min(maxwidth,
281 * Highlight the given results on the alignment.
284 public void highlightSearchResults(SearchResults results)
286 scrollToPosition(results);
287 getSeqPanel().seqCanvas.highlightSearchResults(results);
291 * scroll the view to show the position of the highlighted region in results
292 * (if any) and redraw the overview
296 public boolean scrollToPosition(SearchResults results)
298 return scrollToPosition(results, true);
302 * scroll the view to show the position of the highlighted region in results
306 * @param redrawOverview
307 * - when set, the overview will be recalculated (takes longer)
308 * @return false if results were not found
310 public boolean scrollToPosition(SearchResults results,
311 boolean redrawOverview)
313 int startv, endv, starts, ends, width;
314 // TODO: properly locate search results in view when large numbers of hidden
315 // columns exist before highlighted region
316 // do we need to scroll the panel?
317 // TODO: tons of nullpointereexceptions raised here.
318 if (results != null && results.getSize() > 0 && av != null
319 && av.getAlignment() != null)
321 int seqIndex = av.getAlignment().findIndex(results);
326 SequenceI seq = av.getAlignment().getSequenceAt(seqIndex);
328 int[] r = results.getResults(seq, 0, av.getAlignment().getWidth());
335 // System.err.println("Seq : "+seqIndex+" Scroll to "+start+","+end); //
341 if (end == seq.getEnd())
345 if (av.hasHiddenColumns())
347 start = av.getColumnSelection().findColumnPosition(start);
348 end = av.getColumnSelection().findColumnPosition(end);
351 if (!av.getColumnSelection().isVisible(r[0]))
353 // don't scroll - position isn't visible
358 if (!av.getWrapAlignment())
360 if ((startv = av.getStartRes()) >= start)
362 setScrollValues(start - 1, seqIndex);
364 else if ((endv = av.getEndRes()) <= end)
366 setScrollValues(startv + 1 + end - endv, seqIndex);
368 else if ((starts = av.getStartSeq()) > seqIndex)
370 setScrollValues(av.getStartRes(), seqIndex);
372 else if ((ends = av.getEndSeq()) <= seqIndex)
374 setScrollValues(av.getStartRes(), starts + seqIndex - ends + 1);
379 scrollToWrappedVisible(start);
382 if (redrawOverview && overviewPanel != null)
384 overviewPanel.setBoxPosition();
386 paintAlignment(redrawOverview);
390 void scrollToWrappedVisible(int res)
392 int cwidth = getSeqPanel().seqCanvas
393 .getWrappedCanvasWidth(getSeqPanel().seqCanvas.getWidth());
394 if (res < av.getStartRes() || res >= (av.getStartRes() + cwidth))
396 vscroll.setValue((res / cwidth));
397 av.startRes = vscroll.getValue() * cwidth;
405 * @return DOCUMENT ME!
407 public OverviewPanel getOverviewPanel()
409 return overviewPanel;
418 public void setOverviewPanel(OverviewPanel op)
426 * Hide or show annotation panel
429 public void setAnnotationVisible(boolean b)
431 if (!av.getWrapAlignment())
433 annotationSpaceFillerHolder.setVisible(b);
434 annotationScroller.setVisible(b);
440 * automatically adjust annotation panel height for new annotation whilst
441 * ensuring the alignment is still visible.
443 public void adjustAnnotationHeight()
445 // TODO: display vertical annotation scrollbar if necessary
446 // this is called after loading new annotation onto alignment
447 if (alignFrame.getHeight() == 0)
449 System.out.println("NEEDS FIXING");
451 validateAnnotationDimensions(true);
453 paintAlignment(true);
457 * calculate the annotation dimensions and refresh slider values accordingly.
458 * need to do repaints/notifys afterwards.
460 protected void validateAnnotationDimensions(boolean adjustPanelHeight)
462 int height = getAnnotationPanel().adjustPanelHeight();
464 int theight = av.getCharHeight()
465 * (av.getAlignment().getHeight() + (!av.hasHiddenRows() ? 0
466 : av.getAlignment().getHiddenSequences().getSize()));
467 float sscaling = (float) (theight / (1.0 * theight + height));
468 float ascaling = (float) (height * 1.0 / alignFrame.getHeight());
469 int rheight = alignFrame.getHeight() - height - av.getCharHeight();
470 if (adjustPanelHeight)
472 // NOTE: this logic is different in the applet. Need a better algorithm to
474 // try and set height according to alignment
475 if (ascaling > 0 && sscaling < 0.5)
477 // if the alignment is too big then
478 // default is 0.5 split
479 height = alignFrame.getHeight() / 2;
483 // if space for more than one sequence row left when annotation is fully
484 // displayed then set height to annotation height
485 // otherwise, leave at least two lines of sequence shown.
486 height = (rheight > av.getCharHeight()) ? height
487 : (-av.getCharHeight() * 3 + (int) (alignFrame.getHeight() * (1 - sscaling)));
492 // maintain same window layout whilst updating sliders
493 height = annotationScroller.getSize().height;
497 annotationScroller.setPreferredSize(new Dimension(annotationScroller
498 .getWidth(), height));
500 annotationSpaceFillerHolder.setPreferredSize(new Dimension(
501 annotationSpaceFillerHolder.getWidth(), height));
502 annotationScroller.validate();// repaint();
503 annotationScroller.addNotify();
507 * update alignment layout for viewport settings
512 public void updateLayout()
515 setAnnotationVisible(av.isShowAnnotation());
516 boolean wrap = av.getWrapAlignment();
518 scalePanelHolder.setVisible(!wrap);
519 hscroll.setVisible(!wrap);
520 idwidthAdjuster.setVisible(!wrap);
524 annotationScroller.setVisible(false);
525 annotationSpaceFillerHolder.setVisible(false);
527 else if (av.isShowAnnotation())
529 annotationScroller.setVisible(true);
530 annotationSpaceFillerHolder.setVisible(true);
533 idSpaceFillerPanel1.setVisible(!wrap);
538 // return value is true if the scroll is valid
539 public boolean scrollUp(boolean up)
543 if (vscroll.getValue() < 1)
549 vscroll.setValue(vscroll.getValue() - 1);
553 if ((vextent + vscroll.getValue()) >= av.getAlignment().getHeight())
559 vscroll.setValue(vscroll.getValue() + 1);
573 * @return DOCUMENT ME!
575 public boolean scrollRight(boolean right)
579 if (hscroll.getValue() < 1)
585 hscroll.setValue(hscroll.getValue() - 1);
589 if ((hextent + hscroll.getValue()) >= av.getAlignment().getWidth())
595 hscroll.setValue(hscroll.getValue() + 1);
604 * Adjust row/column scrollers to show a visible position in the alignment.
607 * visible column to scroll to DOCUMENT ME!
609 * visible row to scroll to
612 public void setScrollValues(int x, int y)
614 // System.err.println("Scroll to "+x+","+y);
615 if (av == null || av.getAlignment() == null)
619 int width = av.getAlignment().getWidth();
620 int height = av.getAlignment().getHeight();
622 if (av.hasHiddenColumns())
624 width = av.getColumnSelection().findColumnPosition(width);
627 av.setEndRes((x + (getSeqPanel().seqCanvas.getWidth() / av
628 .getCharWidth())) - 1);
630 hextent = getSeqPanel().seqCanvas.getWidth() / av.getCharWidth();
631 vextent = getSeqPanel().seqCanvas.getHeight() / av.getCharHeight();
638 if (vextent > height)
643 if ((hextent + x) > width)
648 if ((vextent + y) > height)
650 y = height - vextent;
663 hscroll.setValues(x, hextent, 0, width);
664 vscroll.setValues(y, vextent, 0, height);
673 public void adjustmentValueChanged(AdjustmentEvent evt)
676 int oldX = av.getStartRes();
677 int oldY = av.getStartSeq();
679 if (evt.getSource() == hscroll)
681 int x = hscroll.getValue();
683 av.setEndRes((x + (getSeqPanel().seqCanvas.getWidth() / av.getCharWidth())) - 1);
686 if (evt.getSource() == vscroll)
688 int offy = vscroll.getValue();
690 if (av.getWrapAlignment())
694 int rowSize = getSeqPanel().seqCanvas
695 .getWrappedCanvasWidth(getSeqPanel().seqCanvas.getWidth());
696 av.setStartRes(offy * rowSize);
697 av.setEndRes((offy + 1) * rowSize);
701 // This is only called if file loaded is a jar file that
702 // was wrapped when saved and user has wrap alignment true
703 // as preference setting
704 SwingUtilities.invokeLater(new Runnable()
708 setScrollValues(av.getStartRes(), av.getStartSeq());
715 av.setStartSeq(offy);
717 + (getSeqPanel().seqCanvas.getHeight() / av.getCharHeight()));
721 if (overviewPanel != null)
723 overviewPanel.setBoxPosition();
726 int scrollX = av.startRes - oldX;
727 int scrollY = av.startSeq - oldY;
729 if (av.getWrapAlignment() || !fastPaint)
735 // Make sure we're not trying to draw a panel
736 // larger than the visible window
737 if (scrollX > av.endRes - av.startRes)
739 scrollX = av.endRes - av.startRes;
741 else if (scrollX < av.startRes - av.endRes)
743 scrollX = av.startRes - av.endRes;
746 if (scrollX != 0 || scrollY != 0)
748 getIdPanel().getIdCanvas().fastPaint(scrollY);
749 getSeqPanel().seqCanvas.fastPaint(scrollX, scrollY);
750 getScalePanel().repaint();
752 if (av.isShowAnnotation() && scrollX != 0)
754 getAnnotationPanel().fastPaint(scrollX);
761 * Repaint the alignment including the annotations and overview panels (if
764 public void paintAlignment(boolean updateOverview)
766 final AnnotationSorter sorter = new AnnotationSorter(getAlignment(),
767 av.isShowAutocalculatedAbove());
768 sorter.sort(getAlignment()
769 .getAlignmentAnnotation(),
770 av.getSortAnnotationsBy());
775 av.getStructureSelectionManager().sequenceColoursChanged(this);
777 if (overviewPanel != null)
779 overviewPanel.updateOverviewImage();
790 public void paintComponent(Graphics g)
794 Dimension d = getIdPanel().getIdCanvas().getPreferredSize();
795 idPanelHolder.setPreferredSize(d);
796 hscrollFillerPanel.setPreferredSize(new Dimension(d.width, 12));
799 if (av.getWrapAlignment())
801 int maxwidth = av.getAlignment().getWidth();
803 if (av.hasHiddenColumns())
805 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
808 int canvasWidth = getSeqPanel().seqCanvas
809 .getWrappedCanvasWidth(getSeqPanel().seqCanvas.getWidth());
813 / getSeqPanel().seqCanvas
814 .getWrappedCanvasWidth(getSeqPanel().seqCanvas
816 vscroll.setMaximum(max);
817 vscroll.setUnitIncrement(1);
818 vscroll.setVisibleAmount(1);
823 setScrollValues(av.getStartRes(), av.getStartSeq());
837 * @return DOCUMENT ME!
839 * @throws PrinterException
842 public int print(Graphics pg, PageFormat pf, int pi)
843 throws PrinterException
845 pg.translate((int) pf.getImageableX(), (int) pf.getImageableY());
847 int pwidth = (int) pf.getImageableWidth();
848 int pheight = (int) pf.getImageableHeight();
850 if (av.getWrapAlignment())
852 return printWrappedAlignment(pg, pwidth, pheight, pi);
856 return printUnwrapped(pg, pwidth, pheight, pi);
872 * @return DOCUMENT ME!
874 * @throws PrinterException
877 public int printUnwrapped(Graphics pg, int pwidth, int pheight, int pi)
878 throws PrinterException
880 int idWidth = getVisibleIdWidth(false);
881 FontMetrics fm = getFontMetrics(av.getFont());
882 int scaleHeight = av.getCharHeight() + fm.getDescent();
884 pg.setColor(Color.white);
885 pg.fillRect(0, 0, pwidth, pheight);
886 pg.setFont(av.getFont());
888 // //////////////////////////////////
889 // / How many sequences and residues can we fit on a printable page?
890 int totalRes = (pwidth - idWidth) / av.getCharWidth();
892 int totalSeq = (pheight - scaleHeight) / av.getCharHeight() - 1;
894 int pagesWide = (av.getAlignment().getWidth() / totalRes) + 1;
896 // ///////////////////////////
897 // / Only print these sequences and residues on this page
900 // ///////////////////////////
901 // / Only print these sequences and residues on this page
904 // ///////////////////////////
905 // / Only print these sequences and residues on this page
908 // ///////////////////////////
909 // / Only print these sequences and residues on this page
911 startRes = (pi % pagesWide) * totalRes;
912 endRes = (startRes + totalRes) - 1;
914 if (endRes > (av.getAlignment().getWidth() - 1))
916 endRes = av.getAlignment().getWidth() - 1;
919 startSeq = (pi / pagesWide) * totalSeq;
920 endSeq = startSeq + totalSeq;
922 if (endSeq > av.getAlignment().getHeight())
924 endSeq = av.getAlignment().getHeight();
927 int pagesHigh = ((av.getAlignment().getHeight() / totalSeq) + 1)
930 if (av.isShowAnnotation())
932 pagesHigh += getAnnotationPanel().adjustPanelHeight() + 3;
935 pagesHigh /= pheight;
937 if (pi >= (pagesWide * pagesHigh))
939 return Printable.NO_SUCH_PAGE;
943 pg.translate(idWidth, 0);
944 getScalePanel().drawScale(pg, startRes, endRes, pwidth - idWidth,
946 pg.translate(-idWidth, scaleHeight);
950 Color currentColor = null;
951 Color currentTextColor = null;
953 pg.setFont(getIdPanel().getIdCanvas().getIdfont());
956 for (int i = startSeq; i < endSeq; i++)
958 seq = av.getAlignment().getSequenceAt(i);
959 if ((av.getSelectionGroup() != null)
960 && av.getSelectionGroup().getSequences(null).contains(seq))
962 currentColor = Color.gray;
963 currentTextColor = Color.black;
967 currentColor = av.getSequenceColour(seq);
968 currentTextColor = Color.black;
971 pg.setColor(currentColor);
972 pg.fillRect(0, (i - startSeq) * av.getCharHeight(), idWidth,
975 pg.setColor(currentTextColor);
978 if (av.isRightAlignIds())
980 fm = pg.getFontMetrics();
982 - fm.stringWidth(seq.getDisplayId(av.getShowJVSuffix()))
987 seq.getDisplayId(av.getShowJVSuffix()),
989 (((i - startSeq) * av.getCharHeight()) + av.getCharHeight())
990 - (av.getCharHeight() / 5));
993 pg.setFont(av.getFont());
995 // draw main sequence panel
996 pg.translate(idWidth, 0);
997 getSeqPanel().seqCanvas.drawPanel(pg, startRes, endRes, startSeq, endSeq, 0);
999 if (av.isShowAnnotation() && (endSeq == av.getAlignment().getHeight()))
1001 // draw annotation - need to offset for current scroll position
1002 int offset = -getAlabels().getScrollOffset();
1003 pg.translate(0, offset);
1004 pg.translate(-idWidth - 3, (endSeq - startSeq) * av.getCharHeight()
1006 getAlabels().drawComponent(pg, idWidth);
1007 pg.translate(idWidth + 3, 0);
1008 getAnnotationPanel().renderer.drawComponent(getAnnotationPanel(), av,
1009 pg, -1, startRes, endRes + 1);
1010 pg.translate(0, -offset);
1013 return Printable.PAGE_EXISTS;
1028 * @return DOCUMENT ME!
1030 * @throws PrinterException
1033 public int printWrappedAlignment(Graphics pg, int pwidth, int pheight,
1034 int pi) throws PrinterException
1036 int annotationHeight = 0;
1037 AnnotationLabels labels = null;
1038 if (av.isShowAnnotation())
1040 annotationHeight = getAnnotationPanel().adjustPanelHeight();
1041 labels = new AnnotationLabels(av);
1044 int hgap = av.getCharHeight();
1045 if (av.getScaleAboveWrapped())
1047 hgap += av.getCharHeight();
1050 int cHeight = av.getAlignment().getHeight() * av.getCharHeight() + hgap
1053 int idWidth = getVisibleIdWidth(false);
1055 int maxwidth = av.getAlignment().getWidth();
1056 if (av.hasHiddenColumns())
1058 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
1061 int resWidth = getSeqPanel().seqCanvas.getWrappedCanvasWidth(pwidth
1064 int totalHeight = cHeight * (maxwidth / resWidth + 1);
1066 pg.setColor(Color.white);
1067 pg.fillRect(0, 0, pwidth, pheight);
1068 pg.setFont(av.getFont());
1072 pg.setColor(Color.black);
1074 pg.translate(0, -pi * pheight);
1076 pg.setClip(0, pi * pheight, pwidth, pheight);
1082 for (int i = 0; i < av.getAlignment().getHeight(); i++)
1084 pg.setFont(getIdPanel().getIdCanvas().getIdfont());
1085 SequenceI s = av.getAlignment().getSequenceAt(i);
1086 String string = s.getDisplayId(av.getShowJVSuffix());
1088 if (av.isRightAlignIds())
1090 FontMetrics fm = pg.getFontMetrics();
1091 xPos = idWidth - fm.stringWidth(string) - 4;
1093 pg.drawString(string, xPos,
1094 ((i * av.getCharHeight()) + ypos + av.getCharHeight())
1095 - (av.getCharHeight() / 5));
1099 pg.translate(-3, ypos
1100 + (av.getAlignment().getHeight() * av.getCharHeight()));
1102 pg.setFont(av.getFont());
1103 labels.drawComponent(pg, idWidth);
1104 pg.translate(+3, -ypos
1105 - (av.getAlignment().getHeight() * av
1110 } while (ypos < totalHeight);
1112 pg.translate(idWidth, 0);
1114 getSeqPanel().seqCanvas.drawWrappedPanel(pg, pwidth - idWidth, totalHeight,
1117 if ((pi * pheight) < totalHeight)
1119 return Printable.PAGE_EXISTS;
1124 return Printable.NO_SUCH_PAGE;
1129 * get current sequence ID panel width, or nominal value if panel were to be
1130 * displayed using default settings
1134 public int getVisibleIdWidth()
1136 return getVisibleIdWidth(true);
1140 * get current sequence ID panel width, or nominal value if panel were to be
1141 * displayed using default settings
1144 * indicate if the Id width for onscreen or offscreen display should
1148 public int getVisibleIdWidth(boolean onscreen)
1150 // see if rendering offscreen - check preferences and calc width accordingly
1151 if (!onscreen && Cache.getDefault("FIGURE_AUTOIDWIDTH", false))
1153 return calculateIdWidth(-1).width + 4;
1155 Integer idwidth = null;
1157 || (idwidth = Cache.getIntegerProperty("FIGURE_FIXEDIDWIDTH")) == null)
1159 return (getIdPanel().getWidth() > 0 ? getIdPanel().getWidth()
1160 : calculateIdWidth().width + 4);
1162 return idwidth.intValue() + 4;
1165 void makeAlignmentImage(jalview.util.ImageMaker.TYPE type, File file)
1167 long progress = System.currentTimeMillis();
1168 headless = (System.getProperty("java.awt.headless") != null && System
1169 .getProperty("java.awt.headless").equals("true"));
1170 if (alignFrame != null && !headless)
1172 alignFrame.setProgressBar(MessageManager.formatMessage(
1173 "status.saving_file",
1175 { type.getLabel() }), progress);
1179 AlignmentDimension aDimension = getAlignmentDimension();
1182 jalview.util.ImageMaker im;
1183 final String imageAction, imageTitle;
1184 if (type == jalview.util.ImageMaker.TYPE.PNG)
1186 imageAction = "Create PNG image from alignment";
1189 else if (type == jalview.util.ImageMaker.TYPE.EPS)
1191 imageAction = "Create EPS file from alignment";
1192 imageTitle = alignFrame.getTitle();
1196 imageAction = "Create SVG file from alignment";
1197 imageTitle = alignFrame.getTitle();
1200 im = new jalview.util.ImageMaker(this, type, imageAction,
1201 aDimension.getWidth(), aDimension.getHeight(), file,
1203 if (av.getWrapAlignment())
1205 if (im.getGraphics() != null)
1207 printWrappedAlignment(im.getGraphics(), aDimension.getWidth(),
1208 aDimension.getHeight(), 0);
1214 if (im.getGraphics() != null)
1216 printUnwrapped(im.getGraphics(), aDimension.getWidth(),
1217 aDimension.getHeight(), 0);
1221 } catch (OutOfMemoryError err)
1224 System.out.println("########################\n" + "OUT OF MEMORY "
1225 + file + "\n" + "########################");
1226 new OOMWarning("Creating Image for " + file, err);
1227 // System.out.println("Create IMAGE: " + err);
1228 } catch (Exception ex)
1230 ex.printStackTrace();
1234 if (alignFrame != null && !headless)
1236 alignFrame.setProgressBar(MessageManager.getString("status.export_complete"), progress);
1241 public AlignmentDimension getAlignmentDimension()
1243 int maxwidth = av.getAlignment().getWidth();
1244 if (av.hasHiddenColumns())
1246 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth);
1249 int height = ((av.getAlignment().getHeight() + 1) * av.getCharHeight())
1250 + getScalePanel().getHeight();
1251 int width = getVisibleIdWidth(false) + (maxwidth * av.getCharWidth());
1253 if (av.getWrapAlignment())
1255 height = getWrappedHeight();
1258 // need to obtain default alignment width and then add in any
1259 // additional allowance for id margin
1260 // this duplicates the calculation in getWrappedHeight but adjusts for
1262 width = alignFrame.getWidth() - vscroll.getPreferredSize().width
1263 - alignFrame.getInsets().left
1264 - alignFrame.getInsets().right - getVisibleIdWidth()
1265 + getVisibleIdWidth(false);
1269 width = getSeqPanel().getWidth() + getVisibleIdWidth(false);
1273 else if (av.isShowAnnotation())
1275 height += getAnnotationPanel().adjustPanelHeight() + 3;
1277 return new AlignmentDimension(width, height);
1284 public void makeEPS(File epsFile)
1286 makeAlignmentImage(jalview.util.ImageMaker.TYPE.EPS, epsFile);
1292 public void makePNG(File pngFile)
1294 makeAlignmentImage(jalview.util.ImageMaker.TYPE.PNG, pngFile);
1297 public void makeSVG(File svgFile)
1299 makeAlignmentImage(jalview.util.ImageMaker.TYPE.SVG, svgFile);
1301 public void makePNGImageMap(File imgMapFile, String imageName)
1303 // /////ONLY WORKS WITH NONE WRAPPED ALIGNMENTS
1304 // ////////////////////////////////////////////
1305 int idWidth = getVisibleIdWidth(false);
1306 FontMetrics fm = getFontMetrics(av.getFont());
1307 int scaleHeight = av.getCharHeight() + fm.getDescent();
1310 // ////////////////////////////////
1311 if (imgMapFile != null)
1315 int s, sSize = av.getAlignment().getHeight(), res, alwidth = av
1316 .getAlignment().getWidth(), g, gSize, f, fSize, sy;
1317 StringBuffer text = new StringBuffer();
1318 PrintWriter out = new PrintWriter(new FileWriter(imgMapFile));
1319 out.println(jalview.io.HTMLOutput.getImageMapHTML());
1320 out.println("<img src=\"" + imageName
1321 + "\" border=\"0\" usemap=\"#Map\" >"
1322 + "<map name=\"Map\">");
1324 for (s = 0; s < sSize; s++)
1326 sy = s * av.getCharHeight() + scaleHeight;
1328 SequenceI seq = av.getAlignment().getSequenceAt(s);
1329 SequenceFeature[] features = seq.getDatasetSequence()
1330 .getSequenceFeatures();
1331 SequenceGroup[] groups = av.getAlignment().findAllGroups(seq);
1332 for (res = 0; res < alwidth; res++)
1334 text = new StringBuffer();
1336 if (av.getAlignment().isNucleotide())
1338 obj = ResidueProperties.nucleotideName.get(seq.getCharAt(res)
1343 obj = ResidueProperties.aa2Triplet.get(seq.getCharAt(res)
1352 String triplet = obj.toString();
1353 int alIndex = seq.findPosition(res);
1354 gSize = groups.length;
1355 for (g = 0; g < gSize; g++)
1357 if (text.length() < 1)
1359 text.append("<area shape=\"rect\" coords=\""
1360 + (idWidth + res * av.getCharWidth()) + "," + sy
1361 + "," + (idWidth + (res + 1) * av.getCharWidth())
1363 + (av.getCharHeight() + sy) + "\""
1364 + " onMouseOver=\"toolTip('" + alIndex + " "
1368 if (groups[g].getStartRes() < res
1369 && groups[g].getEndRes() > res)
1371 text.append("<br><em>" + groups[g].getName() + "</em>");
1375 if (features != null)
1377 if (text.length() < 1)
1379 text.append("<area shape=\"rect\" coords=\""
1380 + (idWidth + res * av.getCharWidth()) + "," + sy
1381 + "," + (idWidth + (res + 1) * av.getCharWidth())
1383 + (av.getCharHeight() + sy) + "\""
1384 + " onMouseOver=\"toolTip('" + alIndex + " "
1387 fSize = features.length;
1388 for (f = 0; f < fSize; f++)
1391 if ((features[f].getBegin() <= seq.findPosition(res))
1392 && (features[f].getEnd() >= seq.findPosition(res)))
1394 if (features[f].getType().equals("disulfide bond"))
1396 if (features[f].getBegin() == seq.findPosition(res)
1397 || features[f].getEnd() == seq
1400 text.append("<br>disulfide bond "
1401 + features[f].getBegin() + ":"
1402 + features[f].getEnd());
1407 text.append("<br>");
1408 text.append(features[f].getType());
1409 if (features[f].getDescription() != null
1410 && !features[f].getType().equals(
1411 features[f].getDescription()))
1413 text.append(" " + features[f].getDescription());
1416 if (features[f].getValue("status") != null)
1418 text.append(" (" + features[f].getValue("status")
1426 if (text.length() > 1)
1428 text.append("')\"; onMouseOut=\"toolTip()\"; href=\"#\">");
1429 out.println(text.toString());
1433 out.println("</map></body></html>");
1436 } catch (Exception ex)
1438 ex.printStackTrace();
1440 } // /////////END OF IMAGE MAP
1444 int getWrappedHeight()
1446 int seqPanelWidth = getSeqPanel().seqCanvas.getWidth();
1448 if (System.getProperty("java.awt.headless") != null
1449 && System.getProperty("java.awt.headless").equals("true"))
1451 seqPanelWidth = alignFrame.getWidth() - getVisibleIdWidth()
1452 - vscroll.getPreferredSize().width
1453 - alignFrame.getInsets().left - alignFrame.getInsets().right;
1456 int chunkWidth = getSeqPanel().seqCanvas
1457 .getWrappedCanvasWidth(seqPanelWidth);
1459 int hgap = av.getCharHeight();
1460 if (av.getScaleAboveWrapped())
1462 hgap += av.getCharHeight();
1465 int annotationHeight = 0;
1466 if (av.isShowAnnotation())
1468 annotationHeight = getAnnotationPanel().adjustPanelHeight();
1471 int cHeight = av.getAlignment().getHeight() * av.getCharHeight() + hgap
1474 int maxwidth = av.getAlignment().getWidth();
1475 if (av.hasHiddenColumns())
1477 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
1480 int height = ((maxwidth / chunkWidth) + 1) * cHeight;
1486 * close the panel - deregisters all listeners and nulls any references to
1489 public void closePanel()
1491 PaintRefresher.RemoveComponent(getSeqPanel().seqCanvas);
1492 PaintRefresher.RemoveComponent(getIdPanel().getIdCanvas());
1493 PaintRefresher.RemoveComponent(this);
1496 jalview.structure.StructureSelectionManager ssm = av
1497 .getStructureSelectionManager();
1498 ssm.removeStructureViewerListener(getSeqPanel(), null);
1499 ssm.removeSelectionListener(getSeqPanel());
1500 av.setAlignment(null);
1505 if (Cache.log.isDebugEnabled())
1507 Cache.log.warn("Closing alignment panel which is already closed.");
1513 * hides or shows dynamic annotation rows based on groups and av state flags
1515 public void updateAnnotation()
1517 updateAnnotation(false, false);
1520 public void updateAnnotation(boolean applyGlobalSettings)
1522 updateAnnotation(applyGlobalSettings, false);
1525 public void updateAnnotation(boolean applyGlobalSettings,
1526 boolean preserveNewGroupSettings)
1528 av.updateGroupAnnotationSettings(applyGlobalSettings,
1529 preserveNewGroupSettings);
1530 adjustAnnotationHeight();
1534 public AlignmentI getAlignment()
1536 return av.getAlignment();
1541 public String getViewName()
1547 * Make/Unmake this alignment panel the current input focus
1551 public void setSelected(boolean b)
1555 alignFrame.setSelected(b);
1556 } catch (Exception ex)
1563 alignFrame.setDisplayedView(this);
1568 public StructureSelectionManager getStructureSelectionManager()
1570 return av.getStructureSelectionManager();
1574 public void raiseOOMWarning(String string, OutOfMemoryError error)
1576 new OOMWarning(string, error, this);
1580 public jalview.api.FeatureRenderer cloneFeatureRenderer()
1583 return new FeatureRenderer(this);
1586 public jalview.api.FeatureRenderer getFeatureRenderer()
1588 return seqPanel.seqCanvas.getFeatureRenderer();
1590 public void updateFeatureRenderer(jalview.renderer.seqfeatures.FeatureRenderer fr)
1592 fr.transferSettings(getSeqPanel().seqCanvas.getFeatureRenderer());
1595 public void updateFeatureRendererFrom(jalview.api.FeatureRenderer fr)
1597 if (getSeqPanel().seqCanvas.getFeatureRenderer() != null)
1599 getSeqPanel().seqCanvas.getFeatureRenderer().transferSettings(fr);
1603 public ScalePanel getScalePanel()
1608 public void setScalePanel(ScalePanel scalePanel)
1610 this.scalePanel = scalePanel;
1613 public SeqPanel getSeqPanel()
1618 public void setSeqPanel(SeqPanel seqPanel)
1620 this.seqPanel = seqPanel;
1623 public AnnotationPanel getAnnotationPanel()
1625 return annotationPanel;
1628 public void setAnnotationPanel(AnnotationPanel annotationPanel)
1630 this.annotationPanel = annotationPanel;
1633 public AnnotationLabels getAlabels()
1638 public void setAlabels(AnnotationLabels alabels)
1640 this.alabels = alabels;
1643 public IdPanel getIdPanel()
1648 public void setIdPanel(IdPanel idPanel)
1650 this.idPanel = idPanel;