X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fgui%2FAlignmentPanel.java;h=50c89f556802e61319f45994459d898db711a4b3;hb=eee02848424734a9466de35ff19636377652e59b;hp=1a48af6fbe1471e03fd96d4fbbb6dc0825674cb5;hpb=588042b69abf8e60bcc950b24c283933c7dd422f;p=jalview.git diff --git a/src/jalview/gui/AlignmentPanel.java b/src/jalview/gui/AlignmentPanel.java index 1a48af6..50c89f5 100755 --- a/src/jalview/gui/AlignmentPanel.java +++ b/src/jalview/gui/AlignmentPanel.java @@ -18,804 +18,1069 @@ */ package jalview.gui; -import java.io.*; -import javax.imageio.*; +import jalview.datamodel.*; + +import jalview.jbgui.*; + +import jalview.schemes.*; + +import org.jibble.epsgraphics.*; import java.awt.*; import java.awt.event.*; import java.awt.image.*; import java.awt.print.*; + +import java.io.*; + +import javax.imageio.*; + import javax.swing.*; -import org.jibble.epsgraphics.*; -import jalview.analysis.*; -import jalview.datamodel.*; -import jalview.jbgui.*; -import jalview.schemes.*; -public class AlignmentPanel - extends GAlignmentPanel implements AdjustmentListener, Printable +/** + * DOCUMENT ME! + * + * @author $author$ + * @version $Revision$ + */ +public class AlignmentPanel extends GAlignmentPanel + implements AdjustmentListener, Printable { - AlignViewport av; - OverviewPanel overviewPanel; - SeqPanel seqPanel; - IdPanel idPanel; - IdwidthAdjuster idwidthAdjuster; - public AlignFrame alignFrame; - ScalePanel scalePanel; - AnnotationPanel annotationPanel; - AnnotationLabels alabels; - - // this value is set false when selection area being dragged - boolean fastPaint = true; - int hextent = 0; - int vextent = 0; - - public AlignmentPanel(AlignFrame af, final AlignViewport av) - { - alignFrame = af; - this.av = av; - seqPanel = new SeqPanel(av, this); - idPanel = new IdPanel(av, this); - - scalePanel = new ScalePanel(av, this); - - idPanelHolder.add(idPanel, BorderLayout.CENTER); - idwidthAdjuster = new IdwidthAdjuster(this); - idSpaceFillerPanel1.add(idwidthAdjuster, BorderLayout.CENTER); - - annotationPanel = new AnnotationPanel(this); - alabels = new AnnotationLabels(this); - - annotationSpaceFillerHolder.setPreferredSize(annotationPanel. - getPreferredSize()); - annotationScroller.setPreferredSize(annotationPanel.getPreferredSize()); - annotationScroller.setViewportView(annotationPanel); - annotationSpaceFillerHolder.add(alabels, BorderLayout.CENTER); - - fontChanged(); - - scalePanelHolder.add(scalePanel, BorderLayout.CENTER); - seqPanelHolder.add(seqPanel, BorderLayout.CENTER); - - setScrollValues(0, 0); - - hscroll.addAdjustmentListener(this); - vscroll.addAdjustmentListener(this); - - setFocusable(true); - addKeyListener(new KeyAdapter() + AlignViewport av; + OverviewPanel overviewPanel; + SeqPanel seqPanel; + IdPanel idPanel; + IdwidthAdjuster idwidthAdjuster; + + /** DOCUMENT ME!! */ + public AlignFrame alignFrame; + ScalePanel scalePanel; + AnnotationPanel annotationPanel; + AnnotationLabels alabels; + + // this value is set false when selection area being dragged + boolean fastPaint = true; + int hextent = 0; + int vextent = 0; + + /** + * Creates a new AlignmentPanel object. + * + * @param af DOCUMENT ME! + * @param av DOCUMENT ME! + */ + public AlignmentPanel(AlignFrame af, final AlignViewport av) { - public void keyPressed(KeyEvent evt) - { - switch (evt.getKeyCode()) - { - case 27: // escape key - av.setSelectionGroup(null); - repaint(); + alignFrame = af; + this.av = av; + seqPanel = new SeqPanel(av, this); + idPanel = new IdPanel(av, this); - break; + scalePanel = new ScalePanel(av, this); - case KeyEvent.VK_DOWN: - alignFrame.moveSelectedSequences(false); + idPanelHolder.add(idPanel, BorderLayout.CENTER); + idwidthAdjuster = new IdwidthAdjuster(this); + idSpaceFillerPanel1.add(idwidthAdjuster, BorderLayout.CENTER); - break; + annotationPanel = new AnnotationPanel(this); + alabels = new AnnotationLabels(this); - case KeyEvent.VK_UP: - alignFrame.moveSelectedSequences(true); + annotationSpaceFillerHolder.setPreferredSize(annotationPanel.getPreferredSize()); + annotationScroller.setPreferredSize(annotationPanel.getPreferredSize()); + annotationScroller.setViewportView(annotationPanel); + annotationSpaceFillerHolder.add(alabels, BorderLayout.CENTER); - break; - } - } - }); - } - - public void fontChanged() - { - // set idCanvas bufferedImage to null - // to prevent drawing old image - FontMetrics fm = getFontMetrics(av.getFont()); - - scalePanelHolder.setPreferredSize(new Dimension(10, - av.charHeight + fm.getDescent())); - idSpaceFillerPanel1.setPreferredSize(new Dimension(10, - av.charHeight + fm.getDescent())); - - idPanel.idCanvas.gg = null; - annotationPanel.adjustPanelHeight(); - - Dimension d = calculateIdWidth(); - d.setSize(d.width + 4, d.height); - idPanel.idCanvas.setPreferredSize(d); - hscrollFillerPanel.setPreferredSize(d); - repaint(); - } - - Dimension calculateIdWidth() - { - Graphics g = this.getGraphics(); - - if (g == null) - { - javax.swing.JFrame f = new javax.swing.JFrame(); - f.addNotify(); - g = f.getGraphics(); - } + fontChanged(); - FontMetrics fm = g.getFontMetrics(av.font); - AlignmentI al = av.getAlignment(); + scalePanelHolder.add(scalePanel, BorderLayout.CENTER); + seqPanelHolder.add(seqPanel, BorderLayout.CENTER); - int i = 0; - int idWidth = 0; - String id; + setScrollValues(0, 0); - while ( (i < al.getHeight()) && (al.getSequenceAt(i) != null)) - { - SequenceI s = al.getSequenceAt(i); + hscroll.addAdjustmentListener(this); + vscroll.addAdjustmentListener(this); - if (av.getShowFullId()) - { - id = s.getDisplayId(); - } - else - { - id = s.getName(); - } + setFocusable(true); + addKeyListener(new KeyAdapter() + { + public void keyPressed(KeyEvent evt) + { + switch (evt.getKeyCode()) + { + case 27: // escape key + av.setSelectionGroup(null); + repaint(); - if (fm.stringWidth(id) > idWidth) - { - idWidth = fm.stringWidth(id); - } + break; - i++; - } + case KeyEvent.VK_DOWN: + alignFrame.moveSelectedSequences(false); - // Also check annotation label widths - i = 0; + break; - if (al.getAlignmentAnnotation() != null) - { - fm = g.getFontMetrics(alabels.getFont()); + case KeyEvent.VK_UP: + alignFrame.moveSelectedSequences(true); - while (i < al.getAlignmentAnnotation().length) - { - String label = al.getAlignmentAnnotation()[i].label; + break; - if (fm.stringWidth(label) > idWidth) - { - idWidth = fm.stringWidth(label); - } + case KeyEvent.VK_BACK_SPACE: + case KeyEvent.VK_DELETE: + alignFrame.cut_actionPerformed(null); + break; + } - i++; - } + } + }); } - return new Dimension(idWidth, 12); - } - - public void highlightSearchResults(int[] results) - { - seqPanel.seqCanvas.highlightSearchResults(results); - - // do we need to scroll the panel? - if (results != null) - { - SequenceI seq = av.alignment.getSequenceAt(results[0]); - int start = seq.findIndex(results[1]) - 1; - int end = seq.findIndex(results[2]) - 1; - - if ( (av.getStartRes() > start) || (av.getEndRes() < end) || - ( (av.getStartSeq() > results[0]) || - (av.getEndSeq() < results[0]))) - { - setScrollValues(start, results[0]); - } - } - } - - public OverviewPanel getOverviewPanel() - { - return overviewPanel; - } - - public void setOverviewPanel(OverviewPanel op) - { - overviewPanel = op; - } - - public void setAnnotationVisible(boolean b) - { - annotationSpaceFillerHolder.setVisible(b); - annotationScroller.setVisible(b); - } - - public void setWrapAlignment(boolean wrap) - { - scalePanelHolder.setVisible(!wrap); - hscroll.setVisible(!wrap); - idwidthAdjuster.setVisible(!wrap); - - av.setShowAnnotation(!wrap); - annotationScroller.setVisible(!wrap); - annotationSpaceFillerHolder.setVisible(!wrap); - idSpaceFillerPanel1.setVisible(!wrap); - - repaint(); - } - - public void setColourScheme() - { - ColourSchemeI cs = av.getGlobalColourScheme(); - - if (av.getConservationSelected()) + /** + * DOCUMENT ME! + */ + public void fontChanged() { - Alignment al = (Alignment) av.getAlignment(); - Conservation c = new Conservation("All", - ResidueProperties.propHash, 3, - al.getSequences(), 0, - al.getWidth()); + // set idCanvas bufferedImage to null + // to prevent drawing old image + FontMetrics fm = getFontMetrics(av.getFont()); - c.calculate(); - c.verdict(false, av.ConsPercGaps); + scalePanelHolder.setPreferredSize(new Dimension(10, + av.charHeight + fm.getDescent())); + idSpaceFillerPanel1.setPreferredSize(new Dimension(10, + av.charHeight + fm.getDescent())); - ConservationColourScheme ccs = new ConservationColourScheme(c, cs); + idPanel.idCanvas.gg = null; + seqPanel.seqCanvas.img = null; + annotationPanel.adjustPanelHeight(); - av.setGlobalColourScheme(ccs); - } + Dimension d = calculateIdWidth(); + d.setSize(d.width + 4, d.height); + idPanel.idCanvas.setPreferredSize(d); + hscrollFillerPanel.setPreferredSize(d); - repaint(); - } + if (av.getWrapAlignment()) + { + int max = av.alignment.getWidth() / + seqPanel.seqCanvas.getWrappedCanvasWidth(seqPanel.seqCanvas.getWidth()); + vscroll.setMaximum(max); + vscroll.setUnitIncrement(1); + vscroll.setVisibleAmount(1); + } + else + { + setScrollValues(av.getStartRes(), av.getStartSeq()); + } - // return value is true if the scroll is valid - public boolean scrollUp(boolean up) - { - if (up) - { - if (vscroll.getValue() < 1) - { - return false; - } + if(overviewPanel!=null) + overviewPanel.setBoxPosition(); - fastPaint = false; - vscroll.setValue(vscroll.getValue() - 1); + repaint(); } - else - { - if ( (vextent + vscroll.getValue()) >= av.getAlignment().getHeight()) - { - return false; - } - fastPaint = false; - vscroll.setValue(vscroll.getValue() + 1); - } + /** + * DOCUMENT ME! + * + * @return DOCUMENT ME! + */ + public Dimension calculateIdWidth() + { + Container c = new Container(); - fastPaint = true; + FontMetrics fm = c.getFontMetrics(av.font); + AlignmentI al = av.getAlignment(); - return true; - } + int i = 0; + int idWidth = 0; + String id; - public boolean scrollRight(boolean right) - { - if (right) - { - if (hscroll.getValue() < 1) - { - return false; - } + while ((i < al.getHeight()) && (al.getSequenceAt(i) != null)) + { + SequenceI s = al.getSequenceAt(i); + + if (av.getShowFullId()) + { + id = s.getDisplayId(); + } + else + { + id = s.getName(); + } + + if (fm.stringWidth(id) > idWidth) + { + idWidth = fm.stringWidth(id); + } + + i++; + } - fastPaint = false; - hscroll.setValue(hscroll.getValue() - 1); - } - else - { - if ( (hextent + hscroll.getValue()) >= av.getAlignment().getWidth()) - { - return false; - } + // Also check annotation label widths + i = 0; - fastPaint = false; - hscroll.setValue(hscroll.getValue() + 1); - } + if (al.getAlignmentAnnotation() != null) + { + fm = c.getFontMetrics(alabels.getFont()); - fastPaint = true; + while (i < al.getAlignmentAnnotation().length) + { + String label = al.getAlignmentAnnotation()[i].label; - return true; - } + if (fm.stringWidth(label) > idWidth) + { + idWidth = fm.stringWidth(label); + } - public void setScrollValues(int x, int y) - { - av.setEndRes( (x + (seqPanel.seqCanvas.getWidth() / av.getCharWidth())) - - 1); + i++; + } + } - hextent = seqPanel.seqCanvas.getWidth() / av.charWidth; - vextent = seqPanel.seqCanvas.getHeight() / av.charHeight; + return new Dimension(idWidth, 12); + } - if (hextent > av.alignment.getWidth()) + /** + * DOCUMENT ME! + * + * @param results DOCUMENT ME! + */ + public void highlightSearchResults(int[] results) { - hextent = av.alignment.getWidth(); + seqPanel.seqCanvas.highlightSearchResults(results); + + // do we need to scroll the panel? + if (results != null) + { + SequenceI seq = av.alignment.getSequenceAt(results[0]); + int start = seq.findIndex(results[1]) - 1; + int end = seq.findIndex(results[2]) - 1; + + if ((av.getStartRes() > start) || (av.getEndRes() < end) || + ((av.getStartSeq() > results[0]) || + (av.getEndSeq() < results[0]))) + { + setScrollValues(start, results[0]); + } + } } - if (vextent > av.alignment.getHeight()) + /** + * DOCUMENT ME! + * + * @return DOCUMENT ME! + */ + public OverviewPanel getOverviewPanel() { - vextent = av.alignment.getHeight(); + return overviewPanel; } - if ( (hextent + x) > av.getAlignment().getWidth()) + /** + * DOCUMENT ME! + * + * @param op DOCUMENT ME! + */ + public void setOverviewPanel(OverviewPanel op) { - x = av.getAlignment().getWidth() - hextent; + overviewPanel = op; } - if ( (vextent + y) > av.getAlignment().getHeight()) + /** + * DOCUMENT ME! + * + * @param b DOCUMENT ME! + */ + public void setAnnotationVisible(boolean b) { - y = av.getAlignment().getHeight() - vextent; + annotationSpaceFillerHolder.setVisible(b); + annotationScroller.setVisible(b); } - if (y < 0) + /** + * DOCUMENT ME! + * + * @param wrap DOCUMENT ME! + */ + public void setWrapAlignment(boolean wrap) { - y = 0; + av.startSeq = 0; + scalePanelHolder.setVisible(!wrap); + hscroll.setVisible(!wrap); + idwidthAdjuster.setVisible(!wrap); + + if(wrap) + { + annotationScroller.setVisible(false); + annotationSpaceFillerHolder.setVisible(false); + } + else if(av.showAnnotation) + { + annotationScroller.setVisible(true); + annotationSpaceFillerHolder.setVisible(true); + } + + idSpaceFillerPanel1.setVisible(!wrap); + + repaint(); } - if (x < 0) + + // return value is true if the scroll is valid + public boolean scrollUp(boolean up) { - x = 0; - } + if (up) + { + if (vscroll.getValue() < 1) + { + return false; + } + + fastPaint = false; + vscroll.setValue(vscroll.getValue() - 1); + } + else + { + if ((vextent + vscroll.getValue()) >= av.getAlignment().getHeight()) + { + return false; + } - hscroll.setValues(x, hextent, 0, av.getAlignment().getWidth()); - vscroll.setValues(y, vextent, 0, av.getAlignment().getHeight()); - } + fastPaint = false; + vscroll.setValue(vscroll.getValue() + 1); + } - public void adjustmentValueChanged(AdjustmentEvent evt) - { - int oldX = av.getStartRes(); - int oldY = av.getStartSeq(); + fastPaint = true; - if (evt.getSource() == hscroll) - { - int x = hscroll.getValue(); - av.setStartRes(x); - av.setEndRes( (x + - (seqPanel.seqCanvas.getWidth() / av.getCharWidth())) - 1); + return true; } - if (evt.getSource() == vscroll) + /** + * DOCUMENT ME! + * + * @param right DOCUMENT ME! + * + * @return DOCUMENT ME! + */ + public boolean scrollRight(boolean right) { - int offy = vscroll.getValue(); + if (right) + { + if (hscroll.getValue() < 1) + { + return false; + } - if (av.getWrapAlignment()) - { - int rowSize = seqPanel.seqCanvas.getWrappedCanvasWidth(seqPanel. - seqCanvas.getWidth()); - av.setStartRes(vscroll.getValue() * rowSize); - av.setEndRes( (vscroll.getValue() + 1) * rowSize); - } - else - { - av.setStartSeq(offy); - av.setEndSeq(offy + - (seqPanel.seqCanvas.getHeight() / av.getCharHeight())); - } - } + fastPaint = false; + hscroll.setValue(hscroll.getValue() - 1); + } + else + { + if ((hextent + hscroll.getValue()) >= av.getAlignment().getWidth()) + { + return false; + } - if (overviewPanel != null) - { - overviewPanel.setBoxPosition(); - } + fastPaint = false; + hscroll.setValue(hscroll.getValue() + 1); + } - if (av.getWrapAlignment() || !fastPaint) - { - repaint(); + fastPaint = true; + + return true; } - else + + /** + * DOCUMENT ME! + * + * @param x DOCUMENT ME! + * @param y DOCUMENT ME! + */ + public void setScrollValues(int x, int y) { - idPanel.idCanvas.fastPaint(av.getStartSeq() - oldY); - seqPanel.seqCanvas.fastPaint(av.getStartRes() - oldX, - av.getStartSeq() - oldY); + av.setEndRes((x + (seqPanel.seqCanvas.getWidth() / av.getCharWidth())) - + 1); - scalePanel.repaint(); + hextent = seqPanel.seqCanvas.getWidth() / av.charWidth; + vextent = seqPanel.seqCanvas.getHeight() / av.charHeight; - if (av.getShowAnnotation()) - { - annotationPanel.fastPaint(av.getStartRes() - oldX); - } - } - } + if (hextent > av.alignment.getWidth()) + { + hextent = av.alignment.getWidth(); + } - public void paintComponent(Graphics g) - { - invalidate(); + if (vextent > av.alignment.getHeight()) + { + vextent = av.alignment.getHeight(); + } - Dimension d = idPanel.idCanvas.getPreferredSize(); - idPanelHolder.setPreferredSize(d); - hscrollFillerPanel.setPreferredSize(new Dimension(d.width, 12)); - validate(); + if ((hextent + x) > av.getAlignment().getWidth()) + { + x = av.getAlignment().getWidth() - hextent; + } - if (av.getWrapAlignment()) - { - int max = av.alignment.getWidth() / - seqPanel.seqCanvas.getWrappedCanvasWidth(seqPanel.seqCanvas.getWidth()); - vscroll.setMaximum(max); - vscroll.setUnitIncrement(1); - vscroll.setVisibleAmount(1); - } - else - { - setScrollValues(av.getStartRes(), av.getStartSeq()); - } - } + if ((vextent + y) > av.getAlignment().getHeight()) + { + y = av.getAlignment().getHeight() - vextent; + } - public int print(Graphics pg, PageFormat pf, int pi) - throws PrinterException - { - pg.translate( (int) pf.getImageableX(), (int) pf.getImageableY()); + if (y < 0) + { + y = 0; + } - int pwidth = (int) pf.getImageableWidth(); - int pheight = (int) pf.getImageableHeight(); + if (x < 0) + { + x = 0; + } - if (av.getWrapAlignment()) - { - return printWrappedAlignment(pg, pwidth, pheight, pi); - } - else - { - return printUnwrapped(pg, pwidth, pheight, pi); + hscroll.setValues(x, hextent, 0, av.getAlignment().getWidth()); + vscroll.setValues(y, vextent, 0, av.getAlignment().getHeight()); } - } - public int printUnwrapped(Graphics pg, int pwidth, int pheight, int pi) - throws PrinterException - { - int idWidth = calculateIdWidth().width + 4; - FontMetrics fm = getFontMetrics(av.getFont()); - int scaleHeight = av.charHeight + fm.getDescent(); - pg.setColor(Color.white); - pg.fillRect(0, 0, pwidth, pheight); - pg.setFont(av.getFont()); + /** + * DOCUMENT ME! + * + * @param evt DOCUMENT ME! + */ + public void adjustmentValueChanged(AdjustmentEvent evt) + { - //////////////////////////////////// - /// How many sequences and residues can we fit on a printable page? - int totalRes = (pwidth - idWidth) / av.getCharWidth(); + int oldX = av.getStartRes(); + int oldY = av.getStartSeq(); - int totalSeq = (int) ( (pheight - scaleHeight) / av.getCharHeight()) - - 1; + if (evt.getSource() == hscroll) + { + int x = hscroll.getValue(); + av.setStartRes(x); + av.setEndRes((x + + (seqPanel.seqCanvas.getWidth() / av.getCharWidth())) - 1); + } - int pagesWide = (av.getAlignment().getWidth() / totalRes) + 1; + if (evt.getSource() == vscroll) + { + int offy = vscroll.getValue(); + + if (av.getWrapAlignment()) + { + int rowSize = seqPanel.seqCanvas.getWrappedCanvasWidth(seqPanel.seqCanvas.getWidth()); + av.setStartRes(vscroll.getValue() * rowSize); + av.setEndRes((vscroll.getValue() + 1) * rowSize); + } + else + { + av.setStartSeq(offy); + av.setEndSeq(offy + + (seqPanel.seqCanvas.getHeight() / av.getCharHeight())); + } + } - ///////////////////////////// - /// Only print these sequences and residues on this page - int startRes; + if (overviewPanel != null) + { + overviewPanel.setBoxPosition(); + } - ///////////////////////////// - /// Only print these sequences and residues on this page - int endRes; + int xShift = av.getStartRes() - oldX; + int yShift = av.getStartSeq() - oldY; - ///////////////////////////// - /// Only print these sequences and residues on this page - int startSeq; + if ( (xShift != 0 && yShift != 0) || + (Math.abs(xShift) > av.getEndRes() - av.getStartRes() + || Math.abs(yShift) > av.getEndSeq() - av.getStartSeq()) + || av.getWrapAlignment() + || !fastPaint) + { + // Either no shift at all, or shift greater than visible amount + repaint(); + } + else + { + idPanel.idCanvas.fastPaint(yShift); + seqPanel.seqCanvas.fastPaint(xShift, yShift); - ///////////////////////////// - /// Only print these sequences and residues on this page - int endSeq; - startRes = (pi % pagesWide) * totalRes; - endRes = (startRes + totalRes) - 1; + scalePanel.repaint(); - if (endRes > (av.getAlignment().getWidth() - 1)) - { - endRes = av.getAlignment().getWidth() - 1; + if (av.getShowAnnotation()) + { + annotationPanel.fastPaint(xShift); + } + } } - startSeq = (pi / pagesWide) * totalSeq; - endSeq = startSeq + totalSeq; - - if (endSeq > av.getAlignment().getHeight()) + /** + * DOCUMENT ME! + * + * @param g DOCUMENT ME! + */ + public void paintComponent(Graphics g) { - endSeq = av.getAlignment().getHeight(); - } + invalidate(); - int pagesHigh = ( (av.alignment.getHeight() / totalSeq) + 1) * pheight; + Dimension d = idPanel.idCanvas.getPreferredSize(); + idPanelHolder.setPreferredSize(d); + hscrollFillerPanel.setPreferredSize(new Dimension(d.width, 12)); + validate(); - if (av.showAnnotation) - { - pagesHigh += annotationPanel.getHeight(); + if (av.getWrapAlignment()) + { + int max = av.alignment.getWidth() / seqPanel.seqCanvas.getWrappedCanvasWidth(seqPanel.seqCanvas.getWidth()); + vscroll.setMaximum(max); + vscroll.setUnitIncrement(1); + vscroll.setVisibleAmount(1); + } + else + { + setScrollValues(av.getStartRes(), av.getStartSeq()); + } } - pagesHigh /= pheight; - - if (pi >= (pagesWide * pagesHigh)) + /** + * DOCUMENT ME! + * + * @param pg DOCUMENT ME! + * @param pf DOCUMENT ME! + * @param pi DOCUMENT ME! + * + * @return DOCUMENT ME! + * + * @throws PrinterException DOCUMENT ME! + */ + public int print(Graphics pg, PageFormat pf, int pi) + throws PrinterException { - return Printable.NO_SUCH_PAGE; - } + pg.translate((int) pf.getImageableX(), (int) pf.getImageableY()); - //draw Scale - pg.translate(idWidth, 0); - scalePanel.drawScale(pg, startRes, endRes, pwidth - idWidth, scaleHeight); - pg.translate( -idWidth, scaleHeight); + int pwidth = (int) pf.getImageableWidth(); + int pheight = (int) pf.getImageableHeight(); - //////////////// - // Draw the ids - Color currentColor = null; - Color currentTextColor = null; + if (av.getWrapAlignment()) + { + return printWrappedAlignment(pg, pwidth, pheight, pi); + } + else + { + return printUnwrapped(pg, pwidth, pheight, pi); + } + } - for (int i = startSeq; i < endSeq; i++) + /** + * DOCUMENT ME! + * + * @param pg DOCUMENT ME! + * @param pwidth DOCUMENT ME! + * @param pheight DOCUMENT ME! + * @param pi DOCUMENT ME! + * + * @return DOCUMENT ME! + * + * @throws PrinterException DOCUMENT ME! + */ + public int printUnwrapped(Graphics pg, int pwidth, int pheight, int pi) + throws PrinterException { - if ( (av.getSelectionGroup() != null) && - av.getSelectionGroup().sequences.contains( - av.getAlignment().getSequenceAt(i))) - { - currentColor = Color.gray; - currentTextColor = Color.black; - } - else - { - currentColor = av.getAlignment().getSequenceAt(i).getColor(); - currentTextColor = Color.black; - } + int idWidth = calculateIdWidth().width + 4; + FontMetrics fm = getFontMetrics(av.getFont()); + int scaleHeight = av.charHeight + fm.getDescent(); - pg.setColor(currentColor); - pg.fillRect(0, - jalview.analysis.AlignmentUtil.getPixelHeight(startSeq, i, - av.getCharHeight()), idWidth, av.getCharHeight()); + pg.setColor(Color.white); + pg.fillRect(0, 0, pwidth, pheight); + pg.setFont(av.getFont()); - pg.setColor(currentTextColor); + //////////////////////////////////// + /// How many sequences and residues can we fit on a printable page? + int totalRes = (pwidth - idWidth) / av.getCharWidth(); - String string = av.getAlignment().getSequenceAt(i).getName(); + int totalSeq = (int) ((pheight - scaleHeight) / av.getCharHeight()) - + 1; - if (av.getShowFullId()) - { - string = av.getAlignment().getSequenceAt(i).getDisplayId(); - } + int pagesWide = (av.getAlignment().getWidth() / totalRes) + 1; - pg.drawString(string, 0, - (jalview.analysis.AlignmentUtil.getPixelHeight(startSeq, i, - av.getCharHeight()) + av.getCharHeight()) - - (av.getCharHeight() / 5)); - } + ///////////////////////////// + /// Only print these sequences and residues on this page + int startRes; - // draw main sequence panel - pg.translate(idWidth, 0); - seqPanel.seqCanvas.drawPanel(pg, startRes, endRes, startSeq, endSeq, - startRes, startSeq, 0); + ///////////////////////////// + /// Only print these sequences and residues on this page + int endRes; - if (av.showAnnotation && (endSeq == av.alignment.getHeight())) - { - pg.translate( -idWidth, (endSeq - startSeq) * av.charHeight); - alabels.drawComponent( (Graphics2D) pg); - pg.translate(idWidth, 0); - annotationPanel.drawComponent( (Graphics2D) pg, startRes, endRes + - 1); - } + ///////////////////////////// + /// Only print these sequences and residues on this page + int startSeq; + + ///////////////////////////// + /// Only print these sequences and residues on this page + int endSeq; + startRes = (pi % pagesWide) * totalRes; + endRes = (startRes + totalRes) - 1; - return Printable.PAGE_EXISTS; - } + if (endRes > (av.getAlignment().getWidth() - 1)) + { + endRes = av.getAlignment().getWidth() - 1; + } - public int printWrappedAlignment(Graphics pg, int pwidth, int pheight, - int pi) - throws PrinterException - { - int idWidth = calculateIdWidth().width + 4; + startSeq = (pi / pagesWide) * totalSeq; + endSeq = startSeq + totalSeq; - int resWidth = seqPanel.seqCanvas.getWrappedCanvasWidth(pwidth - - idWidth); - int totalHeight = totalHeight = (av.alignment.getHeight() + 2) * - ( (av.alignment.getWidth() / resWidth) + - 1) * av.charHeight; + if (endSeq > av.getAlignment().getHeight()) + { + endSeq = av.getAlignment().getHeight(); + } - pg.setColor(Color.white); - pg.fillRect(0, 0, pwidth, pheight); - pg.setFont(av.getFont()); + int pagesHigh = ((av.alignment.getHeight() / totalSeq) + 1) * pheight; - //////////////// - // Draw the ids - pg.setColor(Color.black); + if (av.showAnnotation) + { + pagesHigh += annotationPanel.getHeight(); + } - pg.translate(0, -pi * pheight); + pagesHigh /= pheight; - pg.setClip(0, pi * pheight, pwidth, pheight); + if (pi >= (pagesWide * pagesHigh)) + { + return Printable.NO_SUCH_PAGE; + } - int ypos = 2 * av.charHeight; + //draw Scale + pg.translate(idWidth, 0); + scalePanel.drawScale(pg, startRes, endRes, pwidth - idWidth, scaleHeight); + pg.translate(-idWidth, scaleHeight); - do - { - for (int i = 0; i < av.alignment.getHeight(); i++) - { - SequenceI s = av.alignment.getSequenceAt(i); - String string = s.getName(); + //////////////// + // Draw the ids + Color currentColor = null; + Color currentTextColor = null; - if (av.getShowFullId()) + for (int i = startSeq; i < endSeq; i++) { - string = s.getDisplayId(); + if ((av.getSelectionGroup() != null) && + av.getSelectionGroup().sequences.contains( + av.getAlignment().getSequenceAt(i))) + { + currentColor = Color.gray; + currentTextColor = Color.black; + } + else + { + currentColor = av.getAlignment().getSequenceAt(i).getColor(); + currentTextColor = Color.black; + } + + pg.setColor(currentColor); + pg.fillRect(0, (i-startSeq) * av.charHeight, idWidth, + av.getCharHeight()); + + pg.setColor(currentTextColor); + + String string = av.getAlignment().getSequenceAt(i).getName(); + + if (av.getShowFullId()) + { + string = av.getAlignment().getSequenceAt(i).getDisplayId(); + } + + pg.drawString(string, 0, + (((i-startSeq) * av.charHeight) + av.getCharHeight()) - + (av.getCharHeight() / 5)); } - pg.drawString(string, 0, - (AlignmentUtil.getPixelHeight(0, i, av.charHeight) + ypos + - av.charHeight) - (av.charHeight / 5)); - } + // draw main sequence panel + pg.translate(idWidth, 0); + seqPanel.seqCanvas.drawPanel(pg, startRes, endRes, startSeq, endSeq, + startRes, startSeq, 0); + + if (av.showAnnotation && (endSeq == av.alignment.getHeight())) + { + pg.translate(-idWidth, (endSeq - startSeq) * av.charHeight); + alabels.drawComponent((Graphics2D) pg); + pg.translate(idWidth, 0); + annotationPanel.drawComponent((Graphics2D) pg, startRes, endRes + + 1); + } - ypos += ( (av.alignment.getHeight() + 2) * av.charHeight); + return Printable.PAGE_EXISTS; } - while (ypos < totalHeight); - pg.translate(idWidth, 0); + /** + * DOCUMENT ME! + * + * @param pg DOCUMENT ME! + * @param pwidth DOCUMENT ME! + * @param pheight DOCUMENT ME! + * @param pi DOCUMENT ME! + * + * @return DOCUMENT ME! + * + * @throws PrinterException DOCUMENT ME! + */ + public int printWrappedAlignment(Graphics pg, int pwidth, int pheight, + int pi) throws PrinterException + { + int idWidth = calculateIdWidth().width + 4; - seqPanel.seqCanvas.drawWrappedPanel(pg, pwidth - idWidth, totalHeight, 0); + int resWidth = seqPanel.seqCanvas.getWrappedCanvasWidth(pwidth - + idWidth); + int totalHeight = totalHeight = (av.alignment.getHeight() + 2) * ((av.alignment.getWidth() / resWidth) + + 1) * av.charHeight; - if ( (pi * pheight) < totalHeight) - { - return Printable.PAGE_EXISTS; - } - else - { - return Printable.NO_SUCH_PAGE; - } - } + pg.setColor(Color.white); + pg.fillRect(0, 0, pwidth, pheight); + pg.setFont(av.getFont()); - public void makeEPS() - { - int height = ( (av.alignment.getWidth() / av.getChunkWidth()) + 1) * - av.chunkHeight; - int width = seqPanel.getWidth() + idPanel.getWidth(); + //////////////// + // Draw the ids + pg.setColor(Color.black); - if (!av.getWrapAlignment()) - { - height = ( (av.alignment.getHeight() + 1) * av.charHeight) + 30; - width = idPanel.getWidth() + - (av.alignment.getWidth() * av.charWidth); - } + pg.translate(0, -pi * pheight); - if (av.getShowAnnotation()) - { - height += annotationPanel.getPreferredSize().height; + pg.setClip(0, pi * pheight, pwidth, pheight); + + int ypos = 2 * av.charHeight; + + do + { + for (int i = 0; i < av.alignment.getHeight(); i++) + { + SequenceI s = av.alignment.getSequenceAt(i); + String string = s.getName(); + + if (av.getShowFullId()) + { + string = s.getDisplayId(); + } + + pg.drawString(string, 0, + ((i * av.charHeight) + ypos + av.charHeight) - + (av.charHeight / 5)); + } + + ypos += ((av.alignment.getHeight() + 2) * av.charHeight); + } + while (ypos < totalHeight); + + pg.translate(idWidth, 0); + + seqPanel.seqCanvas.drawWrappedPanel(pg, pwidth - idWidth, totalHeight, 0); + + if ((pi * pheight) < totalHeight) + { + return Printable.PAGE_EXISTS; + } + else + { + return Printable.NO_SUCH_PAGE; + } } - try + /** + * DOCUMENT ME! + */ + public void makeEPS(File epsFile) { - jalview.io.JalviewFileChooser chooser = new jalview.io.JalviewFileChooser( - jalview.bin.Cache.getProperty( - "LAST_DIRECTORY"), new String[] - {"eps"}, - new String[] - {"Encapsulated Postscript"}, - "Encapsulated Postscript"); - chooser.setFileView(new jalview.io.JalviewFileView()); - chooser.setDialogTitle("Create EPS file from alignment"); - chooser.setToolTipText("Save"); - - int value = chooser.showSaveDialog(this); - - if (value != jalview.io.JalviewFileChooser.APPROVE_OPTION) - { - return; - } - - jalview.bin.Cache.setProperty("LAST_DIRECTORY", - chooser.getSelectedFile().getParent()); + boolean accurateText = true; - FileOutputStream out = new FileOutputStream(chooser.getSelectedFile()); - EpsGraphics2D pg = new EpsGraphics2D("Example", out, 0, 0, width, - height); + String renderStyle = jalview.bin.Cache.getDefault("EPS_RENDERING", + "Prompt each time"); - if (av.getWrapAlignment()) + // If we need to prompt, and if the GUI is visible then + // Prompt for EPS rendering style + if (renderStyle.equalsIgnoreCase("Prompt each time") + && ! + (System.getProperty("java.awt.headless") != null + && System.getProperty("java.awt.headless").equals("true"))) { - printWrappedAlignment(pg, width, height, 0); + EPSOptions eps = new EPSOptions(); + renderStyle = eps.getValue(); + + if(eps.cancelled || renderStyle==null) + return; + + renderStyle = eps.getValue(); } - else + + if (renderStyle.equalsIgnoreCase("text")) { - printUnwrapped(pg, width, height, 0); + accurateText = false; } - pg.flush(); - pg.close(); - } - catch (Exception ex) - { - ex.printStackTrace(); - } - } - public void makePNG() - { - int height = ( (av.alignment.getWidth() / av.getChunkWidth()) + 1) * - av.chunkHeight; - int width = seqPanel.getWidth() + idPanel.getWidth(); + if(epsFile == null) + { + jalview.io.JalviewFileChooser chooser = new jalview.io. + JalviewFileChooser(jalview.bin.Cache.getProperty( + "LAST_DIRECTORY"), new String[] + {"eps"}, + new String[] + {"Encapsulated Postscript"}, + "Encapsulated Postscript"); + chooser.setFileView(new jalview.io.JalviewFileView()); + chooser.setDialogTitle("Create EPS file from alignment"); + chooser.setToolTipText("Save"); + + int value = chooser.showSaveDialog(this); + + if (value != jalview.io.JalviewFileChooser.APPROVE_OPTION) + { + return; + } + + epsFile = chooser.getSelectedFile(); + + jalview.bin.Cache.setProperty("LAST_DIRECTORY", + chooser.getSelectedFile().getParent()); + } + + int height = ( (av.alignment.getHeight() + 1) * av.charHeight) + 30; + int width = idPanel.getWidth() + (av.alignment.getWidth() * av.charWidth); - if (!av.getWrapAlignment()) - { - height = ( (av.alignment.getHeight() + 1) * av.charHeight) + 30; - width = idPanel.getWidth() + - (av.alignment.getWidth() * av.charWidth); + if (av.getWrapAlignment()) + { + height = ( (av.alignment.getWidth() / av.getChunkWidth()) + 1) * + av.chunkHeight; + width = seqPanel.getWidth() + idPanel.getWidth(); + + } + + + if (av.getShowAnnotation()) + { + height += annotationPanel.getPreferredSize().height; + } + + try + { + FileOutputStream out = new FileOutputStream(epsFile); + EpsGraphics2D pg = new EpsGraphics2D("Example", out, 0, 0, width, + height); + + pg.setAccurateTextMode(accurateText); + + if (av.getWrapAlignment()) + { + printWrappedAlignment(pg, width, height, 0); + } + else + { + printUnwrapped(pg, width, height, 0); + } + + pg.flush(); + pg.close(); + } + catch (Exception ex) + { + ex.printStackTrace(); + } } - if (av.getShowAnnotation()) + + public void makePNGImageMap(File imgMapFile, String imageName) { - height += annotationPanel.getPreferredSize().height; + ///////ONLY WORKS WITH NONE WRAPPED ALIGNMENTS + ////////////////////////////////////////////// + int idWidth = calculateIdWidth().width + 4; + FontMetrics fm = getFontMetrics(av.getFont()); + int scaleHeight = av.charHeight + fm.getDescent(); + + // Gen image map + ////////////////////////////////// + if(imgMapFile!=null) + { + try + { + PrintWriter out = new PrintWriter(new FileWriter(imgMapFile)); + out.println(jalview.io.HTMLOutput.getImageMapHTML()); + out.println("" + +""); + + for (int s = 0; s < av.alignment.getHeight(); s++) + { + SequenceI seq = av.alignment.getSequenceAt(s); + SequenceGroup[] groups = av.alignment.findAllGroups(seq); + for (int i = 0; i < groups.length; i++) + { + int sy = s * av.charHeight + scaleHeight; + for (int res = groups[i].getStartRes(); + res < groups[i].getEndRes() + 1; res++) + { + int alIndex = seq.findPosition(res); + Object obj = ResidueProperties.aa2Triplet.get( + seq.getCharAt(res) + ""); + if (obj == null) + continue; + + String triplet = obj.toString(); + + out.println( + "" + groups[i].getName() + + "')\"; onMouseOut=\"toolTip()\"; " + + " href=\"#\">"); + } + } + } + + out.println(""); + out.close(); + + } + catch (Exception ex) + { + ex.printStackTrace(); + } + }///////////END OF IMAGE MAP + } - try + /** + * DOCUMENT ME! + */ + public void makePNG(File pngFile) { - jalview.io.JalviewFileChooser chooser = new jalview.io.JalviewFileChooser( - jalview.bin.Cache.getProperty( - "LAST_DIRECTORY"), new String[] - {"png"}, - new String[] - {"Portable network graphics"}, - "Portable network graphics"); - chooser.setFileView(new jalview.io.JalviewFileView()); - chooser.setDialogTitle("Create EPS file from alignment"); - chooser.setToolTipText("Save"); - - int value = chooser.showSaveDialog(this); - - if (value != jalview.io.JalviewFileChooser.APPROVE_OPTION) + if(pngFile==null) { - return; - } + jalview.io.JalviewFileChooser chooser = new jalview.io. + JalviewFileChooser(jalview.bin.Cache.getProperty( + "LAST_DIRECTORY"), new String[] + {"png"}, + new String[] + {"Portable network graphics"}, + "Portable network graphics"); + chooser.setFileView(new jalview.io.JalviewFileView()); + chooser.setDialogTitle("Create EPS file from alignment"); + chooser.setToolTipText("Save"); + + int value = chooser.showSaveDialog(this); + + if (value != jalview.io.JalviewFileChooser.APPROVE_OPTION) + { + return; + } + + pngFile = chooser.getSelectedFile(); - jalview.bin.Cache.setProperty("LAST_DIRECTORY", - chooser.getSelectedFile().getParent()); + jalview.bin.Cache.setProperty("LAST_DIRECTORY", + chooser.getSelectedFile().getParent()); + } - FileOutputStream out = new FileOutputStream(chooser.getSelectedFile()); - BufferedImage bi = new BufferedImage(width, height, - BufferedImage.TYPE_INT_RGB); - Graphics2D png = (Graphics2D) bi.getGraphics(); - png.setRenderingHint(RenderingHints.KEY_ANTIALIASING, - RenderingHints.VALUE_ANTIALIAS_ON); + int height = ( (av.alignment.getHeight() + 1) * av.charHeight) + 30; + int width = idPanel.getWidth() + (av.alignment.getWidth() * av.charWidth); if (av.getWrapAlignment()) { - printWrappedAlignment(png, width, height, 0); - } - else - { - printUnwrapped(png, width, height, 0); + height = ( (av.alignment.getWidth() / av.getChunkWidth()) + 1) * + av.chunkHeight; + width = seqPanel.getWidth() + idPanel.getWidth(); + } - ImageIO.write(bi, "png", out); - out.close(); - } - catch (Exception ex) - { - ex.printStackTrace(); + + if (av.getShowAnnotation()) + { + height += annotationPanel.getPreferredSize().height; + } + + try + { + + FileOutputStream out = new FileOutputStream(pngFile); + + BufferedImage bi = new BufferedImage(width, height, + BufferedImage.TYPE_INT_RGB); + Graphics2D png = (Graphics2D) bi.getGraphics(); + + png.setRenderingHint(RenderingHints.KEY_ANTIALIASING, + RenderingHints.VALUE_ANTIALIAS_ON); + + if (av.getWrapAlignment()) + { + printWrappedAlignment(png, width, height, 0); + } + else + { + printUnwrapped(png, width, height, 0); + } + + ImageIO.write(bi, "png", out); + out.close(); + } + catch(OutOfMemoryError err) + { + System.out.println("########################\n" + +"OUT OF MEMORY "+pngFile+"\n" + +"########################"); + } + catch (Exception ex) + { + ex.printStackTrace(); + } } - } } -class Preview - extends JFrame -{ - public Preview(Image image) - { - setResizable(true); - setSize(image.getWidth(this), image.getHeight(this)); - setVisible(true); - getContentPane().setLayout(new BorderLayout()); - getContentPane().add(new PreviewPanel(image), BorderLayout.CENTER); - validate(); - repaint(); - } -} -class PreviewPanel - extends JPanel +/** + * DOCUMENT ME! + * + * @author $author$ + * @version $Revision$ + */ +class Preview extends JFrame { - Image image; + /** + * Creates a new Preview object. + * + * @param image DOCUMENT ME! + */ + public Preview(Image image) + { + setResizable(true); + setSize(image.getWidth(this), image.getHeight(this)); + setVisible(true); + getContentPane().setLayout(new BorderLayout()); + getContentPane().add(new PreviewPanel(image), BorderLayout.CENTER); + validate(); + repaint(); + } +} - public PreviewPanel(Image image) - { - this.image = image; - } - public void paintComponent(Graphics g) - { - if (image != null) +/** + * DOCUMENT ME! + * + * @author $author$ + * @version $Revision$ + */ +class PreviewPanel extends JPanel +{ + Image image; + + /** + * Creates a new PreviewPanel object. + * + * @param image DOCUMENT ME! + */ + public PreviewPanel(Image image) { - g.drawImage(image, 0, 0, this); + this.image = image; } - else + + /** + * DOCUMENT ME! + * + * @param g DOCUMENT ME! + */ + public void paintComponent(Graphics g) { - System.out.println("DEBUG:image is null"); + if (image != null) + { + g.drawImage(image, 0, 0, this); + } + else + { + System.out.println("DEBUG:image is null"); + } } - } }