X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fgui%2FAlignmentPanel.java;h=43fb5fac17366c74ad8740bb2dec6d27385d3d0a;hb=32ce9ddb7ce1a68add53dd81785ae428ca136a83;hp=1a48af6fbe1471e03fd96d4fbbb6dc0825674cb5;hpb=6e71f42b0a3713ddbc3054e7ea9d8f5088130ec9;p=jalview.git diff --git a/src/jalview/gui/AlignmentPanel.java b/src/jalview/gui/AlignmentPanel.java index 1a48af6..43fb5fa 100755 --- a/src/jalview/gui/AlignmentPanel.java +++ b/src/jalview/gui/AlignmentPanel.java @@ -18,29 +18,37 @@ */ package jalview.gui; -import java.io.*; -import javax.imageio.*; +import jalview.datamodel.*; + +import jalview.jbgui.*; + +import jalview.schemes.*; import java.awt.*; import java.awt.event.*; -import java.awt.image.*; import java.awt.print.*; + +import java.io.*; + 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; + public AlignViewport av; OverviewPanel overviewPanel; SeqPanel seqPanel; IdPanel idPanel; IdwidthAdjuster idwidthAdjuster; + + /** DOCUMENT ME!! */ public AlignFrame alignFrame; ScalePanel scalePanel; AnnotationPanel annotationPanel; @@ -51,6 +59,12 @@ public class AlignmentPanel 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) { alignFrame = af; @@ -80,11 +94,17 @@ public class AlignmentPanel setScrollValues(0, 0); + annotationPanel.adjustPanelHeight(); + annotationSpaceFillerHolder.setPreferredSize(annotationPanel. + getPreferredSize()); + annotationScroller.setPreferredSize(annotationPanel. + getPreferredSize()); + setAnnotationVisible(av.getShowAnnotation()); + hscroll.addAdjustmentListener(this); vscroll.addAdjustmentListener(this); - setFocusable(true); - addKeyListener(new KeyAdapter() + af.addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent evt) { @@ -105,11 +125,26 @@ public class AlignmentPanel alignFrame.moveSelectedSequences(true); break; + + case KeyEvent.VK_BACK_SPACE: + case KeyEvent.VK_DELETE: + alignFrame.cut_actionPerformed(null); + break; + + case KeyEvent.VK_P: + seqPanel.seqCanvas.increaseAARatio(); + break; + case KeyEvent.VK_L: + seqPanel.seqCanvas.decreaseAARation(); + break; } } }); } + /** + * DOCUMENT ME! + */ public void fontChanged() { // set idCanvas bufferedImage to null @@ -122,27 +157,43 @@ public class AlignmentPanel av.charHeight + fm.getDescent())); idPanel.idCanvas.gg = null; + seqPanel.seqCanvas.img = null; annotationPanel.adjustPanelHeight(); Dimension d = calculateIdWidth(); d.setSize(d.width + 4, d.height); idPanel.idCanvas.setPreferredSize(d); hscrollFillerPanel.setPreferredSize(d); + + 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 (overviewPanel != null) + overviewPanel.setBoxPosition(); + repaint(); } - Dimension calculateIdWidth() + /** + * DOCUMENT ME! + * + * @return DOCUMENT ME! + */ + public Dimension calculateIdWidth() { - Graphics g = this.getGraphics(); - - if (g == null) - { - javax.swing.JFrame f = new javax.swing.JFrame(); - f.addNotify(); - g = f.getGraphics(); - } + Container c = new Container(); - FontMetrics fm = g.getFontMetrics(av.font); + FontMetrics fm = c.getFontMetrics(av.font); AlignmentI al = av.getAlignment(); int i = 0; @@ -153,14 +204,7 @@ public class AlignmentPanel { SequenceI s = al.getSequenceAt(i); - if (av.getShowFullId()) - { - id = s.getDisplayId(); - } - else - { - id = s.getName(); - } + id = s.getDisplayId(av.getShowJVSuffix()); if (fm.stringWidth(id) > idWidth) { @@ -175,7 +219,7 @@ public class AlignmentPanel if (al.getAlignmentAnnotation() != null) { - fm = g.getFontMetrics(alabels.getFont()); + fm = c.getFontMetrics(alabels.getFont()); while (i < al.getAlignmentAnnotation().length) { @@ -193,6 +237,11 @@ public class AlignmentPanel return new Dimension(idWidth, 12); } + /** + * DOCUMENT ME! + * + * @param results DOCUMENT ME! + */ public void highlightSearchResults(int[] results) { seqPanel.seqCanvas.highlightSearchResults(results); @@ -213,56 +262,66 @@ public class AlignmentPanel } } + /** + * DOCUMENT ME! + * + * @return DOCUMENT ME! + */ public OverviewPanel getOverviewPanel() { return overviewPanel; } + /** + * DOCUMENT ME! + * + * @param op DOCUMENT ME! + */ public void setOverviewPanel(OverviewPanel op) { overviewPanel = op; } + /** + * DOCUMENT ME! + * + * @param b DOCUMENT ME! + */ public void setAnnotationVisible(boolean b) { - annotationSpaceFillerHolder.setVisible(b); - annotationScroller.setVisible(b); + if (!av.wrapAlignment) + { + annotationSpaceFillerHolder.setVisible(b); + annotationScroller.setVisible(b); + } + repaint(); } + /** + * DOCUMENT ME! + * + * @param wrap DOCUMENT ME! + */ public void setWrapAlignment(boolean wrap) { + av.startSeq = 0; 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()) + if (wrap) { - Alignment al = (Alignment) av.getAlignment(); - Conservation c = new Conservation("All", - ResidueProperties.propHash, 3, - al.getSequences(), 0, - al.getWidth()); - - c.calculate(); - c.verdict(false, av.ConsPercGaps); - - ConservationColourScheme ccs = new ConservationColourScheme(c, cs); - - av.setGlobalColourScheme(ccs); + annotationScroller.setVisible(false); + annotationSpaceFillerHolder.setVisible(false); + } + else if (av.showAnnotation) + { + annotationScroller.setVisible(true); + annotationSpaceFillerHolder.setVisible(true); } + idSpaceFillerPanel1.setVisible(!wrap); + repaint(); } @@ -295,6 +354,13 @@ public class AlignmentPanel return true; } + /** + * DOCUMENT ME! + * + * @param right DOCUMENT ME! + * + * @return DOCUMENT ME! + */ public boolean scrollRight(boolean right) { if (right) @@ -323,6 +389,12 @@ public class AlignmentPanel return true; } + /** + * DOCUMENT ME! + * + * @param x DOCUMENT ME! + * @param y DOCUMENT ME! + */ public void setScrollValues(int x, int y) { av.setEndRes( (x + (seqPanel.seqCanvas.getWidth() / av.getCharWidth())) - @@ -365,8 +437,14 @@ public class AlignmentPanel vscroll.setValues(y, vextent, 0, av.getAlignment().getHeight()); } + /** + * DOCUMENT ME! + * + * @param evt DOCUMENT ME! + */ public void adjustmentValueChanged(AdjustmentEvent evt) { + int oldX = av.getStartRes(); int oldY = av.getStartSeq(); @@ -402,25 +480,37 @@ public class AlignmentPanel overviewPanel.setBoxPosition(); } - if (av.getWrapAlignment() || !fastPaint) + int xShift = av.getStartRes() - oldX; + int yShift = av.getStartSeq() - oldY; + + 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(av.getStartSeq() - oldY); - seqPanel.seqCanvas.fastPaint(av.getStartRes() - oldX, - av.getStartSeq() - oldY); + idPanel.idCanvas.fastPaint(yShift); + seqPanel.seqCanvas.fastPaint(xShift, yShift); scalePanel.repaint(); if (av.getShowAnnotation()) { - annotationPanel.fastPaint(av.getStartRes() - oldX); + annotationPanel.fastPaint(xShift); } } } + /** + * DOCUMENT ME! + * + * @param g DOCUMENT ME! + */ public void paintComponent(Graphics g) { invalidate(); @@ -442,8 +532,26 @@ public class AlignmentPanel { setScrollValues(av.getStartRes(), av.getStartSeq()); } + + + if( this.getVisibleRect().getBounds() == g.getClipBounds() ) + { + if (overviewPanel != null) + overviewPanel.updateOverviewImage(); + } } + /** + * 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 { @@ -462,6 +570,18 @@ public class AlignmentPanel } } + /** + * 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 { @@ -517,7 +637,7 @@ public class AlignmentPanel if (av.showAnnotation) { - pagesHigh += annotationPanel.getHeight(); + pagesHigh += annotationPanel.adjustPanelHeight() + 3; } pagesHigh /= pheight; @@ -553,22 +673,16 @@ public class AlignmentPanel } pg.setColor(currentColor); - pg.fillRect(0, - jalview.analysis.AlignmentUtil.getPixelHeight(startSeq, i, - av.getCharHeight()), idWidth, av.getCharHeight()); + 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(); - } + String string = av.getAlignment().getSequenceAt(i).getDisplayId + ( av.getShowJVSuffix()); pg.drawString(string, 0, - (jalview.analysis.AlignmentUtil.getPixelHeight(startSeq, i, - av.getCharHeight()) + av.getCharHeight()) - + ( ( (i - startSeq) * av.charHeight) + av.getCharHeight()) - (av.getCharHeight() / 5)); } @@ -579,8 +693,8 @@ public class AlignmentPanel if (av.showAnnotation && (endSeq == av.alignment.getHeight())) { - pg.translate( -idWidth, (endSeq - startSeq) * av.charHeight); - alabels.drawComponent( (Graphics2D) pg); + pg.translate( -idWidth, (endSeq - startSeq) * av.charHeight + 3); + alabels.drawComponent( (Graphics2D) pg, idWidth); pg.translate(idWidth, 0); annotationPanel.drawComponent( (Graphics2D) pg, startRes, endRes + 1); @@ -589,17 +703,45 @@ public class AlignmentPanel return Printable.PAGE_EXISTS; } + /** + * 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 annotationHeight = 0; + AnnotationLabels labels = null; + if (av.showAnnotation) + { + annotationHeight = annotationPanel.adjustPanelHeight(); + labels = new AnnotationLabels(av); + } + + int hgap = av.charHeight; + if (av.scaleAboveWrapped) + hgap += av.charHeight; + + int cHeight = av.getAlignment().getHeight() * av.charHeight + + hgap + + annotationHeight; + int idWidth = calculateIdWidth().width + 4; int resWidth = seqPanel.seqCanvas.getWrappedCanvasWidth(pwidth - idWidth); - int totalHeight = totalHeight = (av.alignment.getHeight() + 2) * - ( (av.alignment.getWidth() / resWidth) + - 1) * av.charHeight; + + int totalHeight = cHeight * (av.alignment.getWidth() / resWidth + 1); pg.setColor(Color.white); pg.fillRect(0, 0, pwidth, pheight); @@ -613,26 +755,37 @@ public class AlignmentPanel pg.setClip(0, pi * pheight, pwidth, pheight); - int ypos = 2 * av.charHeight; + int ypos = hgap; + Font italic = new Font(av.getFont().getName(), Font.ITALIC, + av.getFont().getSize()); + pg.setFont(italic); 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(); - } + String string = s.getDisplayId( av.getShowJVSuffix()); pg.drawString(string, 0, - (AlignmentUtil.getPixelHeight(0, i, av.charHeight) + ypos + - av.charHeight) - (av.charHeight / 5)); + ( (i * av.charHeight) + ypos + av.charHeight) - + (av.charHeight / 5)); + } + if (labels != null) + { + pg.translate(0, + ypos + + (av.getAlignment().getHeight() * av.charHeight)); + + pg.setFont(av.getFont()); + labels.drawComponent(pg, idWidth); + pg.setFont(italic); + pg.translate(0, + -ypos - + (av.getAlignment().getHeight() * av.charHeight)); } - ypos += ( (av.alignment.getHeight() + 2) * av.charHeight); + ypos += cHeight; } while (ypos < totalHeight); @@ -643,6 +796,7 @@ public class AlignmentPanel if ( (pi * pheight) < totalHeight) { return Printable.PAGE_EXISTS; + } else { @@ -650,172 +804,237 @@ public class AlignmentPanel } } - public void makeEPS() + void makeAlignmentImage(int type, File file) { - int height = ( (av.alignment.getWidth() / av.getChunkWidth()) + 1) * - av.chunkHeight; - int width = seqPanel.getWidth() + idPanel.getWidth(); + int height = ( (av.alignment.getHeight() + 1) * av.charHeight) + 30; + int width = idPanel.getWidth() + (av.alignment.getWidth() * av.charWidth); - if (!av.getWrapAlignment()) + if (av.getWrapAlignment()) { - height = ( (av.alignment.getHeight() + 1) * av.charHeight) + 30; - width = idPanel.getWidth() + - (av.alignment.getWidth() * av.charWidth); + height = getWrappedHeight(); + width = seqPanel.getWidth() + idPanel.getWidth(); } - - if (av.getShowAnnotation()) + else if (av.getShowAnnotation()) { - height += annotationPanel.getPreferredSize().height; + height += annotationPanel.adjustPanelHeight() + 3; } + jalview.util.ImageMaker im; + if(type==jalview.util.ImageMaker.PNG) + im = new jalview.util.ImageMaker(this, + jalview.util.ImageMaker.PNG, + "Create PNG image from alignment", + width, height, file, null); + else + im = new jalview.util.ImageMaker(this, + jalview.util.ImageMaker.EPS, + "Create EPS file from alignment", + width, height, file, alignFrame.getTitle() ); + try { - 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()); - - FileOutputStream out = new FileOutputStream(chooser.getSelectedFile()); - EpsGraphics2D pg = new EpsGraphics2D("Example", out, 0, 0, width, - height); - if (av.getWrapAlignment()) { - printWrappedAlignment(pg, width, height, 0); + if(im.getGraphics()!=null) + printWrappedAlignment(im.getGraphics(), width, height, 0); } else { - printUnwrapped(pg, width, height, 0); + if(im.getGraphics()!=null) + printUnwrapped(im.getGraphics(), width, height, 0); } - pg.flush(); - pg.close(); + im.writeImage(); + } + catch (OutOfMemoryError err) + { + System.out.println("########################\n" + + "OUT OF MEMORY " + file + "\n" + + "########################"); + + JOptionPane.showInternalMessageDialog(Desktop.desktop, + "Out of Memory Creating Image!!" + + + "\nSee help files for increasing Java Virtual Machine memory." + , "Out of memory", + JOptionPane.WARNING_MESSAGE); + System.out.println("Create IMAGE: " + err); + System.gc(); + } catch (Exception ex) { ex.printStackTrace(); } } + /** + * DOCUMENT ME! + */ + public void makeEPS(File epsFile) + { + makeAlignmentImage(jalview.util.ImageMaker.EPS, epsFile); + } - public void makePNG() + /** + * DOCUMENT ME! + */ + public void makePNG(File pngFile) { - int height = ( (av.alignment.getWidth() / av.getChunkWidth()) + 1) * - av.chunkHeight; - int width = seqPanel.getWidth() + idPanel.getWidth(); + makeAlignmentImage(jalview.util.ImageMaker.PNG, pngFile); + } - if (!av.getWrapAlignment()) - { - height = ( (av.alignment.getHeight() + 1) * av.charHeight) + 30; - width = idPanel.getWidth() + - (av.alignment.getWidth() * av.charWidth); - } + public void makePNGImageMap(File imgMapFile, String imageName) + { + ///////ONLY WORKS WITH NONE WRAPPED ALIGNMENTS + ////////////////////////////////////////////// + int idWidth = calculateIdWidth().width + 4; + FontMetrics fm = getFontMetrics(av.getFont()); + int scaleHeight = av.charHeight + fm.getDescent(); - if (av.getShowAnnotation()) + // Gen image map + ////////////////////////////////// + if (imgMapFile != null) { - height += annotationPanel.getPreferredSize().height; - } + try + { + PrintWriter out = new PrintWriter(new FileWriter(imgMapFile)); + out.println(jalview.io.HTMLOutput.getImageMapHTML()); + out.println("" + + ""); - try - { - 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"); + 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=\"#\">"); + } + } + } - int value = chooser.showSaveDialog(this); + out.println(""); + out.close(); - if (value != jalview.io.JalviewFileChooser.APPROVE_OPTION) + } + catch (Exception ex) { - return; + ex.printStackTrace(); } + } ///////////END OF IMAGE MAP - jalview.bin.Cache.setProperty("LAST_DIRECTORY", - chooser.getSelectedFile().getParent()); + } - FileOutputStream out = new FileOutputStream(chooser.getSelectedFile()); + int getWrappedHeight() + { - BufferedImage bi = new BufferedImage(width, height, - BufferedImage.TYPE_INT_RGB); - Graphics2D png = (Graphics2D) bi.getGraphics(); + int chunkWidth = seqPanel.seqCanvas.getWrappedCanvasWidth( + seqPanel.seqCanvas.getWidth()); - png.setRenderingHint(RenderingHints.KEY_ANTIALIASING, - RenderingHints.VALUE_ANTIALIAS_ON); + int hgap = av.charHeight; + if (av.scaleAboveWrapped) + hgap += av.charHeight; - if (av.getWrapAlignment()) - { - printWrappedAlignment(png, width, height, 0); - } - else - { - printUnwrapped(png, width, height, 0); - } - - ImageIO.write(bi, "png", out); - out.close(); - } - catch (Exception ex) + int annotationHeight = 0; + if (av.showAnnotation) { - ex.printStackTrace(); + annotationHeight = annotationPanel.adjustPanelHeight(); } - } -} -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(); - } -} + int cHeight = av.getAlignment().getHeight() * av.charHeight + + hgap + + annotationHeight; -class PreviewPanel - extends JPanel -{ - Image image; + int height = ( (av.alignment.getWidth() / chunkWidth) + 1) * cHeight; + + return height; + } - public PreviewPanel(Image image) + /** + * DOCUMENT ME! + * + * @author $author$ + * @version $Revision$ + */ + class Preview + extends JFrame { - this.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 void paintComponent(Graphics g) + /** + * DOCUMENT ME! + * + * @author $author$ + * @version $Revision$ + */ + class PreviewPanel + extends JPanel { - if (image != null) + 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"); + } } } }