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 setAnnotationVisible(av.isShowAnnotation());
128 hscroll.addAdjustmentListener(this);
129 vscroll.addAdjustmentListener(this);
131 final AlignmentPanel ap = this;
132 av.addPropertyChangeListener(new PropertyChangeListener()
134 public void propertyChange(PropertyChangeEvent evt)
136 if (evt.getPropertyName().equals("alignment"))
138 PaintRefresher.Refresh(ap, av.getSequenceSetId(), true, true);
144 adjustAnnotationHeight();
149 public AlignViewportI getAlignViewport()
153 public void alignmentChanged()
155 av.alignmentChanged(this);
157 alignFrame.updateEditMenuBar();
159 paintAlignment(true);
166 public void fontChanged()
168 // set idCanvas bufferedImage to null
169 // to prevent drawing old image
170 FontMetrics fm = getFontMetrics(av.getFont());
172 scalePanelHolder.setPreferredSize(new Dimension(10, av.getCharHeight()
174 idSpaceFillerPanel1.setPreferredSize(new Dimension(10, av
178 getIdPanel().getIdCanvas().gg = null;
179 getSeqPanel().seqCanvas.img = null;
180 getAnnotationPanel().adjustPanelHeight();
182 Dimension d = calculateIdWidth();
183 d.setSize(d.width + 4, d.height);
184 getIdPanel().getIdCanvas().setPreferredSize(d);
185 hscrollFillerPanel.setPreferredSize(d);
187 if (overviewPanel != null)
189 overviewPanel.setBoxPosition();
196 * Calculate the width of the alignment labels based on the displayed names
197 * and any bounds on label width set in preferences.
199 * @return Dimension giving the maximum width of the alignment label panel
200 * that should be used.
202 public Dimension calculateIdWidth()
204 // calculate sensible default width when no preference is available
206 int afwidth = (alignFrame != null ? alignFrame.getWidth() : 300);
207 int maxwidth = Math.max(20,
208 Math.min(afwidth - 200, 2 * afwidth / 3));
209 return calculateIdWidth(maxwidth);
213 * Calculate the width of the alignment labels based on the displayed names
214 * and any bounds on label width set in preferences.
217 * -1 or maximum width allowed for IdWidth
218 * @return Dimension giving the maximum width of the alignment label panel
219 * that should be used.
221 public Dimension calculateIdWidth(int maxwidth)
223 Container c = new Container();
225 FontMetrics fm = c.getFontMetrics(new Font(av.font.getName(),
226 Font.ITALIC, av.font.getSize()));
228 AlignmentI al = av.getAlignment();
233 while ((i < al.getHeight()) && (al.getSequenceAt(i) != null))
235 SequenceI s = al.getSequenceAt(i);
237 id = s.getDisplayId(av.getShowJVSuffix());
239 if (fm.stringWidth(id) > idWidth)
241 idWidth = fm.stringWidth(id);
247 // Also check annotation label widths
250 if (al.getAlignmentAnnotation() != null)
252 fm = c.getFontMetrics(getAlabels().getFont());
254 while (i < al.getAlignmentAnnotation().length)
256 String label = al.getAlignmentAnnotation()[i].label;
258 if (fm.stringWidth(label) > idWidth)
260 idWidth = fm.stringWidth(label);
267 return new Dimension(maxwidth < 0 ? idWidth : Math.min(maxwidth,
272 * Highlight the given results on the alignment.
275 public void highlightSearchResults(SearchResults results)
277 scrollToPosition(results);
278 getSeqPanel().seqCanvas.highlightSearchResults(results);
282 * scroll the view to show the position of the highlighted region in results
283 * (if any) and redraw the overview
287 public boolean scrollToPosition(SearchResults results)
289 return scrollToPosition(results, true);
293 * scroll the view to show the position of the highlighted region in results
297 * @param redrawOverview
298 * - when set, the overview will be recalculated (takes longer)
299 * @return false if results were not found
301 public boolean scrollToPosition(SearchResults results,
302 boolean redrawOverview)
304 int startv, endv, starts, ends, width;
305 // TODO: properly locate search results in view when large numbers of hidden
306 // columns exist before highlighted region
307 // do we need to scroll the panel?
308 // TODO: tons of nullpointereexceptions raised here.
309 if (results != null && results.getSize() > 0 && av != null
310 && av.getAlignment() != null)
312 int seqIndex = av.getAlignment().findIndex(results);
317 SequenceI seq = av.getAlignment().getSequenceAt(seqIndex);
319 int[] r = results.getResults(seq, 0, av.getAlignment().getWidth());
326 // System.err.println("Seq : "+seqIndex+" Scroll to "+start+","+end); //
332 if (end == seq.getEnd())
336 if (av.hasHiddenColumns())
338 start = av.getColumnSelection().findColumnPosition(start);
339 end = av.getColumnSelection().findColumnPosition(end);
342 if (!av.getColumnSelection().isVisible(r[0]))
344 // don't scroll - position isn't visible
349 if (!av.getWrapAlignment())
351 if ((startv = av.getStartRes()) >= start)
353 setScrollValues(start - 1, seqIndex);
355 else if ((endv = av.getEndRes()) <= end)
357 setScrollValues(startv + 1 + end - endv, seqIndex);
359 else if ((starts = av.getStartSeq()) > seqIndex)
361 setScrollValues(av.getStartRes(), seqIndex);
363 else if ((ends = av.getEndSeq()) <= seqIndex)
365 setScrollValues(av.getStartRes(), starts + seqIndex - ends + 1);
370 scrollToWrappedVisible(start);
373 if (redrawOverview && overviewPanel != null)
375 overviewPanel.setBoxPosition();
377 paintAlignment(redrawOverview);
381 void scrollToWrappedVisible(int res)
383 int cwidth = getSeqPanel().seqCanvas
384 .getWrappedCanvasWidth(getSeqPanel().seqCanvas.getWidth());
385 if (res < av.getStartRes() || res >= (av.getStartRes() + cwidth))
387 vscroll.setValue((res / cwidth));
388 av.startRes = vscroll.getValue() * cwidth;
396 * @return DOCUMENT ME!
398 public OverviewPanel getOverviewPanel()
400 return overviewPanel;
409 public void setOverviewPanel(OverviewPanel op)
417 * Hide or show annotation panel
420 public void setAnnotationVisible(boolean b)
422 if (!av.getWrapAlignment())
424 annotationSpaceFillerHolder.setVisible(b);
425 annotationScroller.setVisible(b);
431 * automatically adjust annotation panel height for new annotation whilst
432 * ensuring the alignment is still visible.
434 public void adjustAnnotationHeight()
436 // TODO: display vertical annotation scrollbar if necessary
437 // this is called after loading new annotation onto alignment
438 if (alignFrame.getHeight() == 0)
440 System.out.println("NEEDS FIXING");
442 validateAnnotationDimensions(true);
444 paintAlignment(true);
448 * calculate the annotation dimensions and refresh slider values accordingly.
449 * need to do repaints/notifys afterwards.
451 protected void validateAnnotationDimensions(boolean adjustPanelHeight)
453 int height = getAnnotationPanel().adjustPanelHeight();
455 int theight = av.getCharHeight()
456 * (av.getAlignment().getHeight() + (!av.hasHiddenRows() ? 0
457 : av.getAlignment().getHiddenSequences().getSize()));
458 float sscaling = (float) (theight / (1.0 * theight + height));
459 float ascaling = (float) (height * 1.0 / alignFrame.getHeight());
460 int rheight = alignFrame.getHeight() - height - av.getCharHeight();
461 if (adjustPanelHeight)
463 // NOTE: this logic is different in the applet. Need a better algorithm to
465 // try and set height according to alignment
466 if (ascaling > 0 && sscaling < 0.5)
468 // if the alignment is too big then
469 // default is 0.5 split
470 height = alignFrame.getHeight() / 2;
474 // if space for more than one sequence row left when annotation is fully
475 // displayed then set height to annotation height
476 // otherwise, leave at least two lines of sequence shown.
477 height = (rheight > av.getCharHeight()) ? height
478 : (-av.getCharHeight() * 3 + (int) (alignFrame.getHeight() * (1 - sscaling)));
483 // maintain same window layout whilst updating sliders
484 height = annotationScroller.getSize().height;
488 annotationScroller.setPreferredSize(new Dimension(annotationScroller
489 .getWidth(), height));
491 annotationSpaceFillerHolder.setPreferredSize(new Dimension(
492 annotationSpaceFillerHolder.getWidth(), height));
493 annotationScroller.validate();// repaint();
494 annotationScroller.addNotify();
503 public void setWrapAlignment(boolean wrap)
506 scalePanelHolder.setVisible(!wrap);
507 hscroll.setVisible(!wrap);
508 idwidthAdjuster.setVisible(!wrap);
512 annotationScroller.setVisible(false);
513 annotationSpaceFillerHolder.setVisible(false);
515 else if (av.isShowAnnotation())
517 annotationScroller.setVisible(true);
518 annotationSpaceFillerHolder.setVisible(true);
521 idSpaceFillerPanel1.setVisible(!wrap);
526 // return value is true if the scroll is valid
527 public boolean scrollUp(boolean up)
531 if (vscroll.getValue() < 1)
537 vscroll.setValue(vscroll.getValue() - 1);
541 if ((vextent + vscroll.getValue()) >= av.getAlignment().getHeight())
547 vscroll.setValue(vscroll.getValue() + 1);
561 * @return DOCUMENT ME!
563 public boolean scrollRight(boolean right)
567 if (hscroll.getValue() < 1)
573 hscroll.setValue(hscroll.getValue() - 1);
577 if ((hextent + hscroll.getValue()) >= av.getAlignment().getWidth())
583 hscroll.setValue(hscroll.getValue() + 1);
592 * Adjust row/column scrollers to show a visible position in the alignment.
595 * visible column to scroll to DOCUMENT ME!
597 * visible row to scroll to
600 public void setScrollValues(int x, int y)
602 // System.err.println("Scroll to "+x+","+y);
603 if (av == null || av.getAlignment() == null)
607 int width = av.getAlignment().getWidth();
608 int height = av.getAlignment().getHeight();
610 if (av.hasHiddenColumns())
612 width = av.getColumnSelection().findColumnPosition(width);
615 av.setEndRes((x + (getSeqPanel().seqCanvas.getWidth() / av
616 .getCharWidth())) - 1);
618 hextent = getSeqPanel().seqCanvas.getWidth() / av.getCharWidth();
619 vextent = getSeqPanel().seqCanvas.getHeight() / av.getCharHeight();
626 if (vextent > height)
631 if ((hextent + x) > width)
636 if ((vextent + y) > height)
638 y = height - vextent;
651 hscroll.setValues(x, hextent, 0, width);
652 vscroll.setValues(y, vextent, 0, height);
661 public void adjustmentValueChanged(AdjustmentEvent evt)
664 int oldX = av.getStartRes();
665 int oldY = av.getStartSeq();
667 if (evt.getSource() == hscroll)
669 int x = hscroll.getValue();
671 av.setEndRes((x + (getSeqPanel().seqCanvas.getWidth() / av.getCharWidth())) - 1);
674 if (evt.getSource() == vscroll)
676 int offy = vscroll.getValue();
678 if (av.getWrapAlignment())
682 int rowSize = getSeqPanel().seqCanvas
683 .getWrappedCanvasWidth(getSeqPanel().seqCanvas.getWidth());
684 av.setStartRes(offy * rowSize);
685 av.setEndRes((offy + 1) * rowSize);
689 // This is only called if file loaded is a jar file that
690 // was wrapped when saved and user has wrap alignment true
691 // as preference setting
692 SwingUtilities.invokeLater(new Runnable()
696 setScrollValues(av.getStartRes(), av.getStartSeq());
703 av.setStartSeq(offy);
705 + (getSeqPanel().seqCanvas.getHeight() / av.getCharHeight()));
709 if (overviewPanel != null)
711 overviewPanel.setBoxPosition();
714 int scrollX = av.startRes - oldX;
715 int scrollY = av.startSeq - oldY;
717 if (av.getWrapAlignment() || !fastPaint)
723 // Make sure we're not trying to draw a panel
724 // larger than the visible window
725 if (scrollX > av.endRes - av.startRes)
727 scrollX = av.endRes - av.startRes;
729 else if (scrollX < av.startRes - av.endRes)
731 scrollX = av.startRes - av.endRes;
734 if (scrollX != 0 || scrollY != 0)
736 getIdPanel().getIdCanvas().fastPaint(scrollY);
737 getSeqPanel().seqCanvas.fastPaint(scrollX, scrollY);
738 getScalePanel().repaint();
740 if (av.isShowAnnotation() && scrollX != 0)
742 getAnnotationPanel().fastPaint(scrollX);
749 * Repaint the alignment including the annotations and overview panels (if
752 public void paintAlignment(boolean updateOverview)
754 final AnnotationSorter sorter = new AnnotationSorter(getAlignment(),
755 av.isShowAutocalculatedAbove());
756 sorter.sort(getAlignment()
757 .getAlignmentAnnotation(),
758 av.getSortAnnotationsBy());
763 av.getStructureSelectionManager().sequenceColoursChanged(this);
765 if (overviewPanel != null)
767 overviewPanel.updateOverviewImage();
778 public void paintComponent(Graphics g)
782 Dimension d = getIdPanel().getIdCanvas().getPreferredSize();
783 idPanelHolder.setPreferredSize(d);
784 hscrollFillerPanel.setPreferredSize(new Dimension(d.width, 12));
787 if (av.getWrapAlignment())
789 int maxwidth = av.getAlignment().getWidth();
791 if (av.hasHiddenColumns())
793 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
796 int canvasWidth = getSeqPanel().seqCanvas
797 .getWrappedCanvasWidth(getSeqPanel().seqCanvas.getWidth());
801 / getSeqPanel().seqCanvas
802 .getWrappedCanvasWidth(getSeqPanel().seqCanvas
804 vscroll.setMaximum(max);
805 vscroll.setUnitIncrement(1);
806 vscroll.setVisibleAmount(1);
811 setScrollValues(av.getStartRes(), av.getStartSeq());
825 * @return DOCUMENT ME!
827 * @throws PrinterException
830 public int print(Graphics pg, PageFormat pf, int pi)
831 throws PrinterException
833 pg.translate((int) pf.getImageableX(), (int) pf.getImageableY());
835 int pwidth = (int) pf.getImageableWidth();
836 int pheight = (int) pf.getImageableHeight();
838 if (av.getWrapAlignment())
840 return printWrappedAlignment(pg, pwidth, pheight, pi);
844 return printUnwrapped(pg, pwidth, pheight, pi);
860 * @return DOCUMENT ME!
862 * @throws PrinterException
865 public int printUnwrapped(Graphics pg, int pwidth, int pheight, int pi)
866 throws PrinterException
868 int idWidth = getVisibleIdWidth(false);
869 FontMetrics fm = getFontMetrics(av.getFont());
870 int scaleHeight = av.getCharHeight() + fm.getDescent();
872 pg.setColor(Color.white);
873 pg.fillRect(0, 0, pwidth, pheight);
874 pg.setFont(av.getFont());
876 // //////////////////////////////////
877 // / How many sequences and residues can we fit on a printable page?
878 int totalRes = (pwidth - idWidth) / av.getCharWidth();
880 int totalSeq = (pheight - scaleHeight) / av.getCharHeight() - 1;
882 int pagesWide = (av.getAlignment().getWidth() / totalRes) + 1;
884 // ///////////////////////////
885 // / Only print these sequences and residues on this page
888 // ///////////////////////////
889 // / Only print these sequences and residues on this page
892 // ///////////////////////////
893 // / Only print these sequences and residues on this page
896 // ///////////////////////////
897 // / Only print these sequences and residues on this page
899 startRes = (pi % pagesWide) * totalRes;
900 endRes = (startRes + totalRes) - 1;
902 if (endRes > (av.getAlignment().getWidth() - 1))
904 endRes = av.getAlignment().getWidth() - 1;
907 startSeq = (pi / pagesWide) * totalSeq;
908 endSeq = startSeq + totalSeq;
910 if (endSeq > av.getAlignment().getHeight())
912 endSeq = av.getAlignment().getHeight();
915 int pagesHigh = ((av.getAlignment().getHeight() / totalSeq) + 1)
918 if (av.isShowAnnotation())
920 pagesHigh += getAnnotationPanel().adjustPanelHeight() + 3;
923 pagesHigh /= pheight;
925 if (pi >= (pagesWide * pagesHigh))
927 return Printable.NO_SUCH_PAGE;
931 pg.translate(idWidth, 0);
932 getScalePanel().drawScale(pg, startRes, endRes, pwidth - idWidth,
934 pg.translate(-idWidth, scaleHeight);
938 Color currentColor = null;
939 Color currentTextColor = null;
941 pg.setFont(getIdPanel().getIdCanvas().getIdfont());
944 for (int i = startSeq; i < endSeq; i++)
946 seq = av.getAlignment().getSequenceAt(i);
947 if ((av.getSelectionGroup() != null)
948 && av.getSelectionGroup().getSequences(null).contains(seq))
950 currentColor = Color.gray;
951 currentTextColor = Color.black;
955 currentColor = av.getSequenceColour(seq);
956 currentTextColor = Color.black;
959 pg.setColor(currentColor);
960 pg.fillRect(0, (i - startSeq) * av.getCharHeight(), idWidth,
963 pg.setColor(currentTextColor);
966 if (av.isRightAlignIds())
968 fm = pg.getFontMetrics();
970 - fm.stringWidth(seq.getDisplayId(av.getShowJVSuffix()))
975 seq.getDisplayId(av.getShowJVSuffix()),
977 (((i - startSeq) * av.getCharHeight()) + av.getCharHeight())
978 - (av.getCharHeight() / 5));
981 pg.setFont(av.getFont());
983 // draw main sequence panel
984 pg.translate(idWidth, 0);
985 getSeqPanel().seqCanvas.drawPanel(pg, startRes, endRes, startSeq, endSeq, 0);
987 if (av.isShowAnnotation() && (endSeq == av.getAlignment().getHeight()))
989 // draw annotation - need to offset for current scroll position
990 int offset = -getAlabels().getScrollOffset();
991 pg.translate(0, offset);
992 pg.translate(-idWidth - 3, (endSeq - startSeq) * av.getCharHeight()
994 getAlabels().drawComponent(pg, idWidth);
995 pg.translate(idWidth + 3, 0);
996 getAnnotationPanel().renderer.drawComponent(getAnnotationPanel(), av,
997 pg, -1, startRes, endRes + 1);
998 pg.translate(0, -offset);
1001 return Printable.PAGE_EXISTS;
1016 * @return DOCUMENT ME!
1018 * @throws PrinterException
1021 public int printWrappedAlignment(Graphics pg, int pwidth, int pheight,
1022 int pi) throws PrinterException
1024 int annotationHeight = 0;
1025 AnnotationLabels labels = null;
1026 if (av.isShowAnnotation())
1028 annotationHeight = getAnnotationPanel().adjustPanelHeight();
1029 labels = new AnnotationLabels(av);
1032 int hgap = av.getCharHeight();
1033 if (av.getScaleAboveWrapped())
1035 hgap += av.getCharHeight();
1038 int cHeight = av.getAlignment().getHeight() * av.getCharHeight() + hgap
1041 int idWidth = getVisibleIdWidth(false);
1043 int maxwidth = av.getAlignment().getWidth();
1044 if (av.hasHiddenColumns())
1046 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
1049 int resWidth = getSeqPanel().seqCanvas.getWrappedCanvasWidth(pwidth
1052 int totalHeight = cHeight * (maxwidth / resWidth + 1);
1054 pg.setColor(Color.white);
1055 pg.fillRect(0, 0, pwidth, pheight);
1056 pg.setFont(av.getFont());
1060 pg.setColor(Color.black);
1062 pg.translate(0, -pi * pheight);
1064 pg.setClip(0, pi * pheight, pwidth, pheight);
1070 for (int i = 0; i < av.getAlignment().getHeight(); i++)
1072 pg.setFont(getIdPanel().getIdCanvas().getIdfont());
1073 SequenceI s = av.getAlignment().getSequenceAt(i);
1074 String string = s.getDisplayId(av.getShowJVSuffix());
1076 if (av.isRightAlignIds())
1078 FontMetrics fm = pg.getFontMetrics();
1079 xPos = idWidth - fm.stringWidth(string) - 4;
1081 pg.drawString(string, xPos,
1082 ((i * av.getCharHeight()) + ypos + av.getCharHeight())
1083 - (av.getCharHeight() / 5));
1087 pg.translate(-3, ypos
1088 + (av.getAlignment().getHeight() * av.getCharHeight()));
1090 pg.setFont(av.getFont());
1091 labels.drawComponent(pg, idWidth);
1092 pg.translate(+3, -ypos
1093 - (av.getAlignment().getHeight() * av
1098 } while (ypos < totalHeight);
1100 pg.translate(idWidth, 0);
1102 getSeqPanel().seqCanvas.drawWrappedPanel(pg, pwidth - idWidth, totalHeight,
1105 if ((pi * pheight) < totalHeight)
1107 return Printable.PAGE_EXISTS;
1112 return Printable.NO_SUCH_PAGE;
1117 * get current sequence ID panel width, or nominal value if panel were to be
1118 * displayed using default settings
1122 public int getVisibleIdWidth()
1124 return getVisibleIdWidth(true);
1128 * get current sequence ID panel width, or nominal value if panel were to be
1129 * displayed using default settings
1132 * indicate if the Id width for onscreen or offscreen display should
1136 public int getVisibleIdWidth(boolean onscreen)
1138 // see if rendering offscreen - check preferences and calc width accordingly
1139 if (!onscreen && Cache.getDefault("FIGURE_AUTOIDWIDTH", false))
1141 return calculateIdWidth(-1).width + 4;
1143 Integer idwidth = null;
1145 || (idwidth = Cache.getIntegerProperty("FIGURE_FIXEDIDWIDTH")) == null)
1147 return (getIdPanel().getWidth() > 0 ? getIdPanel().getWidth()
1148 : calculateIdWidth().width + 4);
1150 return idwidth.intValue() + 4;
1153 void makeAlignmentImage(jalview.util.ImageMaker.TYPE type, File file)
1155 long progress = System.currentTimeMillis();
1156 headless = (System.getProperty("java.awt.headless") != null && System
1157 .getProperty("java.awt.headless").equals("true"));
1158 if (alignFrame != null && !headless)
1160 alignFrame.setProgressBar(MessageManager.formatMessage(
1161 "status.saving_file",
1163 { type.getLabel() }), progress);
1167 AlignmentDimension aDimension = getAlignmentDimension();
1170 jalview.util.ImageMaker im;
1171 final String imageAction, imageTitle;
1172 if (type == jalview.util.ImageMaker.TYPE.PNG)
1174 imageAction = "Create PNG image from alignment";
1177 else if (type == jalview.util.ImageMaker.TYPE.EPS)
1179 imageAction = "Create EPS file from alignment";
1180 imageTitle = alignFrame.getTitle();
1184 imageAction = "Create SVG file from alignment";
1185 imageTitle = alignFrame.getTitle();
1188 im = new jalview.util.ImageMaker(this, type, imageAction,
1189 aDimension.getWidth(), aDimension.getHeight(), file,
1191 if (av.getWrapAlignment())
1193 if (im.getGraphics() != null)
1195 printWrappedAlignment(im.getGraphics(), aDimension.getWidth(),
1196 aDimension.getHeight(), 0);
1202 if (im.getGraphics() != null)
1204 printUnwrapped(im.getGraphics(), aDimension.getWidth(),
1205 aDimension.getHeight(), 0);
1209 } catch (OutOfMemoryError err)
1212 System.out.println("########################\n" + "OUT OF MEMORY "
1213 + file + "\n" + "########################");
1214 new OOMWarning("Creating Image for " + file, err);
1215 // System.out.println("Create IMAGE: " + err);
1216 } catch (Exception ex)
1218 ex.printStackTrace();
1222 if (alignFrame != null && !headless)
1224 alignFrame.setProgressBar(MessageManager.getString("status.export_complete"), progress);
1229 public AlignmentDimension getAlignmentDimension()
1231 int maxwidth = av.getAlignment().getWidth();
1232 if (av.hasHiddenColumns())
1234 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth);
1237 int height = ((av.getAlignment().getHeight() + 1) * av.getCharHeight())
1238 + getScalePanel().getHeight();
1239 int width = getVisibleIdWidth(false) + (maxwidth * av.getCharWidth());
1241 if (av.getWrapAlignment())
1243 height = getWrappedHeight();
1246 // need to obtain default alignment width and then add in any
1247 // additional allowance for id margin
1248 // this duplicates the calculation in getWrappedHeight but adjusts for
1250 width = alignFrame.getWidth() - vscroll.getPreferredSize().width
1251 - alignFrame.getInsets().left
1252 - alignFrame.getInsets().right - getVisibleIdWidth()
1253 + getVisibleIdWidth(false);
1257 width = getSeqPanel().getWidth() + getVisibleIdWidth(false);
1261 else if (av.isShowAnnotation())
1263 height += getAnnotationPanel().adjustPanelHeight() + 3;
1265 return new AlignmentDimension(width, height);
1272 public void makeEPS(File epsFile)
1274 makeAlignmentImage(jalview.util.ImageMaker.TYPE.EPS, epsFile);
1280 public void makePNG(File pngFile)
1282 makeAlignmentImage(jalview.util.ImageMaker.TYPE.PNG, pngFile);
1285 public void makeSVG(File svgFile)
1287 makeAlignmentImage(jalview.util.ImageMaker.TYPE.SVG, svgFile);
1289 public void makePNGImageMap(File imgMapFile, String imageName)
1291 // /////ONLY WORKS WITH NONE WRAPPED ALIGNMENTS
1292 // ////////////////////////////////////////////
1293 int idWidth = getVisibleIdWidth(false);
1294 FontMetrics fm = getFontMetrics(av.getFont());
1295 int scaleHeight = av.getCharHeight() + fm.getDescent();
1298 // ////////////////////////////////
1299 if (imgMapFile != null)
1303 int s, sSize = av.getAlignment().getHeight(), res, alwidth = av
1304 .getAlignment().getWidth(), g, gSize, f, fSize, sy;
1305 StringBuffer text = new StringBuffer();
1306 PrintWriter out = new PrintWriter(new FileWriter(imgMapFile));
1307 out.println(jalview.io.HTMLOutput.getImageMapHTML());
1308 out.println("<img src=\"" + imageName
1309 + "\" border=\"0\" usemap=\"#Map\" >"
1310 + "<map name=\"Map\">");
1312 for (s = 0; s < sSize; s++)
1314 sy = s * av.getCharHeight() + scaleHeight;
1316 SequenceI seq = av.getAlignment().getSequenceAt(s);
1317 SequenceFeature[] features = seq.getDatasetSequence()
1318 .getSequenceFeatures();
1319 SequenceGroup[] groups = av.getAlignment().findAllGroups(seq);
1320 for (res = 0; res < alwidth; res++)
1322 text = new StringBuffer();
1324 if (av.getAlignment().isNucleotide())
1326 obj = ResidueProperties.nucleotideName.get(seq.getCharAt(res)
1331 obj = ResidueProperties.aa2Triplet.get(seq.getCharAt(res)
1340 String triplet = obj.toString();
1341 int alIndex = seq.findPosition(res);
1342 gSize = groups.length;
1343 for (g = 0; g < gSize; g++)
1345 if (text.length() < 1)
1347 text.append("<area shape=\"rect\" coords=\""
1348 + (idWidth + res * av.getCharWidth()) + "," + sy
1349 + "," + (idWidth + (res + 1) * av.getCharWidth())
1351 + (av.getCharHeight() + sy) + "\""
1352 + " onMouseOver=\"toolTip('" + alIndex + " "
1356 if (groups[g].getStartRes() < res
1357 && groups[g].getEndRes() > res)
1359 text.append("<br><em>" + groups[g].getName() + "</em>");
1363 if (features != null)
1365 if (text.length() < 1)
1367 text.append("<area shape=\"rect\" coords=\""
1368 + (idWidth + res * av.getCharWidth()) + "," + sy
1369 + "," + (idWidth + (res + 1) * av.getCharWidth())
1371 + (av.getCharHeight() + sy) + "\""
1372 + " onMouseOver=\"toolTip('" + alIndex + " "
1375 fSize = features.length;
1376 for (f = 0; f < fSize; f++)
1379 if ((features[f].getBegin() <= seq.findPosition(res))
1380 && (features[f].getEnd() >= seq.findPosition(res)))
1382 if (features[f].getType().equals("disulfide bond"))
1384 if (features[f].getBegin() == seq.findPosition(res)
1385 || features[f].getEnd() == seq
1388 text.append("<br>disulfide bond "
1389 + features[f].getBegin() + ":"
1390 + features[f].getEnd());
1395 text.append("<br>");
1396 text.append(features[f].getType());
1397 if (features[f].getDescription() != null
1398 && !features[f].getType().equals(
1399 features[f].getDescription()))
1401 text.append(" " + features[f].getDescription());
1404 if (features[f].getValue("status") != null)
1406 text.append(" (" + features[f].getValue("status")
1414 if (text.length() > 1)
1416 text.append("')\"; onMouseOut=\"toolTip()\"; href=\"#\">");
1417 out.println(text.toString());
1421 out.println("</map></body></html>");
1424 } catch (Exception ex)
1426 ex.printStackTrace();
1428 } // /////////END OF IMAGE MAP
1432 int getWrappedHeight()
1434 int seqPanelWidth = getSeqPanel().seqCanvas.getWidth();
1436 if (System.getProperty("java.awt.headless") != null
1437 && System.getProperty("java.awt.headless").equals("true"))
1439 seqPanelWidth = alignFrame.getWidth() - getVisibleIdWidth()
1440 - vscroll.getPreferredSize().width
1441 - alignFrame.getInsets().left - alignFrame.getInsets().right;
1444 int chunkWidth = getSeqPanel().seqCanvas
1445 .getWrappedCanvasWidth(seqPanelWidth);
1447 int hgap = av.getCharHeight();
1448 if (av.getScaleAboveWrapped())
1450 hgap += av.getCharHeight();
1453 int annotationHeight = 0;
1454 if (av.isShowAnnotation())
1456 annotationHeight = getAnnotationPanel().adjustPanelHeight();
1459 int cHeight = av.getAlignment().getHeight() * av.getCharHeight() + hgap
1462 int maxwidth = av.getAlignment().getWidth();
1463 if (av.hasHiddenColumns())
1465 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
1468 int height = ((maxwidth / chunkWidth) + 1) * cHeight;
1474 * close the panel - deregisters all listeners and nulls any references to
1477 public void closePanel()
1479 PaintRefresher.RemoveComponent(getSeqPanel().seqCanvas);
1480 PaintRefresher.RemoveComponent(getIdPanel().getIdCanvas());
1481 PaintRefresher.RemoveComponent(this);
1484 jalview.structure.StructureSelectionManager ssm = av
1485 .getStructureSelectionManager();
1486 ssm.removeStructureViewerListener(getSeqPanel(), null);
1487 ssm.removeSelectionListener(getSeqPanel());
1488 av.setAlignment(null);
1493 if (Cache.log.isDebugEnabled())
1495 Cache.log.warn("Closing alignment panel which is already closed.");
1501 * hides or shows dynamic annotation rows based on groups and av state flags
1503 public void updateAnnotation()
1505 updateAnnotation(false, false);
1508 public void updateAnnotation(boolean applyGlobalSettings)
1510 updateAnnotation(applyGlobalSettings, false);
1513 public void updateAnnotation(boolean applyGlobalSettings,
1514 boolean preserveNewGroupSettings)
1516 av.updateGroupAnnotationSettings(applyGlobalSettings,
1517 preserveNewGroupSettings);
1518 adjustAnnotationHeight();
1522 public AlignmentI getAlignment()
1524 return av.getAlignment();
1529 public String getViewName()
1535 * Make/Unmake this alignment panel the current input focus
1539 public void setSelected(boolean b)
1543 alignFrame.setSelected(b);
1544 } catch (Exception ex)
1551 alignFrame.setDisplayedView(this);
1556 public StructureSelectionManager getStructureSelectionManager()
1558 return av.getStructureSelectionManager();
1562 public void raiseOOMWarning(String string, OutOfMemoryError error)
1564 new OOMWarning(string, error, this);
1568 public jalview.api.FeatureRenderer cloneFeatureRenderer()
1571 return new FeatureRenderer(this);
1574 public jalview.api.FeatureRenderer getFeatureRenderer()
1576 return seqPanel.seqCanvas.getFeatureRenderer();
1578 public void updateFeatureRenderer(jalview.renderer.seqfeatures.FeatureRenderer fr)
1580 fr.transferSettings(getSeqPanel().seqCanvas.getFeatureRenderer());
1583 public void updateFeatureRendererFrom(jalview.api.FeatureRenderer fr)
1585 if (getSeqPanel().seqCanvas.getFeatureRenderer() != null)
1587 getSeqPanel().seqCanvas.getFeatureRenderer().transferSettings(fr);
1591 public ScalePanel getScalePanel()
1596 public void setScalePanel(ScalePanel scalePanel)
1598 this.scalePanel = scalePanel;
1601 public SeqPanel getSeqPanel()
1606 public void setSeqPanel(SeqPanel seqPanel)
1608 this.seqPanel = seqPanel;
1611 public AnnotationPanel getAnnotationPanel()
1613 return annotationPanel;
1616 public void setAnnotationPanel(AnnotationPanel annotationPanel)
1618 this.annotationPanel = annotationPanel;
1621 public AnnotationLabels getAlabels()
1626 public void setAlabels(AnnotationLabels alabels)
1628 this.alabels = alabels;
1631 public IdPanel getIdPanel()
1636 public void setIdPanel(IdPanel idPanel)
1638 this.idPanel = idPanel;