X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fgui%2FTreePanel.java;h=9f9e32772af1ed82529773d94b96a5f9db518f58;hb=ac07859006686a2c62bcf57b24742eb610bc46f0;hp=f723fdc2673743c4ecb7517729cf72af3f5eebdd;hpb=764f4139569c1135e41348b24943c3e6d22f74ef;p=jalview.git diff --git a/src/jalview/gui/TreePanel.java b/src/jalview/gui/TreePanel.java index f723fdc..9f9e327 100755 --- a/src/jalview/gui/TreePanel.java +++ b/src/jalview/gui/TreePanel.java @@ -11,6 +11,10 @@ import jalview.util.*; import javax.swing.*; import java.awt.print.*; import java.awt.*; +import java.awt.image.*; +import org.jibble.epsgraphics.*; +import javax.imageio.*; + @@ -25,15 +29,10 @@ public class TreePanel extends GTreePanel TreeCanvas treeCanvas; NJTree tree; - FileProperties fp; - PostscriptProperties pp; - - PrintWriter bw; - PrintStream ps; - boolean makeString = false; - StringBuffer out; - - + public NJTree getTree() + { + return tree; + } public TreePanel(AlignViewport av, Vector seqVector, String type, String pwtype, int s, int e) { @@ -55,9 +54,6 @@ public class TreePanel extends GTreePanel } tree = new NJTree(seq, type, pwtype, start, end); - av.setCurrentTree(tree); - - propertiesInit(); treeCanvas = new TreeCanvas(av, tree, scrollPane, longestName); @@ -66,197 +62,67 @@ public class TreePanel extends GTreePanel scrollPane.setViewportView(treeCanvas); } + public TreePanel(AlignViewport av, Vector seqVector, NewickFile newtree, + String type, String pwtype) + { + super(); + // These are probably only arbitrary strings reflecting source of tree + this.type = type; + this.pwtype = pwtype; + start = 0; + end = seqVector.size(); -public void drawPostscript(PostscriptProperties pp) { - try { - int width = 0; - int height = 0; - - printout("%!\n"); - - printout("/" + pp.font + " findfont\n"); - printout(pp.fsize + " scalefont setfont\n"); - - int offx = pp.xoffset; - int offy = pp.yoffset; - - if (pp.orientation == PostscriptProperties.PORTRAIT) { - width = PostscriptProperties.SHORTSIDE; - height = PostscriptProperties.LONGSIDE; - } else { - height = PostscriptProperties.SHORTSIDE; - width = PostscriptProperties.LONGSIDE; - printout(height + " 0 translate\n90 rotate\n"); - } - float wscale = (float)(width*.8-offx*2)/tree.getMaxHeight(); - SequenceNode top = tree.getTopNode(); - - if (top.count == 0) { - top.count = ((SequenceNode)top.left()).count + ((SequenceNode)top.right()).count ; - } - - float chunk = (float)(height-offy*2)/(((SequenceNode)top).count+1); - - drawPostscriptNode(top,chunk,wscale,width,offx,offy); - - printout("showpage\n"); - } catch (java.io.IOException e) { - System.out.println("Exception " + e); - } -} - -public void drawPostscriptNode(SequenceNode node, float chunk, float scale, int width, int offx, int offy) throws java.io.IOException { - if (node == null) { - return; - } - - if (node.left() == null && node.right() == null) { - // Drawing leaf node - - float height = node.height; - float dist = node.dist; - - int xstart = (int)((height-dist)*scale) + offx; - int xend = (int)(height*scale) + offx; - - int ypos = (int)(node.ycount * chunk) + offy; + String longestName = ""; + seq = new Sequence [seqVector.size()]; + for (int i=0;i < seqVector.size();i++) + { + seq[i] = (Sequence) seqVector.elementAt(i); + if(seq[i].getName().length()>longestName.length()) + longestName = seq[i].getName(); + } + // This constructor matches sequence names to treenodes and sets up the tree layouts. + tree = new NJTree(seq, newtree); - // g.setColor(Color.black); - printout("\n" + new Format("%5.3f").form((double)node.color.getRed()/255) + " " + - new Format("%5.3f").form((double)node.color.getGreen()/255) + " " + - new Format("%5.3f").form((double)node.color.getBlue()/255) + " setrgbcolor\n"); + tree.reCount(tree.getTopNode()); + tree.findHeight(tree.getTopNode()); - // Draw horizontal line - // g.drawLine(xstart,ypos,xend,ypos); - printout(xstart + " " + ypos + " moveto " + xend + " " + ypos + " lineto stroke \n"); + treeCanvas = new TreeCanvas(av, tree, scrollPane, longestName); + treeCanvas.setShowBootstrap(newtree.HasBootstrap()); + treeCanvas.setShowDistances(newtree.HasDistances()); + scrollPane.setViewportView(treeCanvas); - if (treeCanvas.showDistances && node.dist > 0) { - // g.drawString(new Format("%5.2f").form(node.dist),xstart,ypos - 5); - printout("(" + new Format("%5.2f").form(node.dist) + ") " + xstart + " " + (ypos+5) + " moveto show\n"); - } - //g.drawString((String)node.element(),xend+20,ypos); - printout("(" + (((SequenceI)node.element()).getName()) + ") " + (xend+20) + " " + (ypos) + " moveto show\n"); - } else { - drawPostscriptNode((SequenceNode)node.left(),chunk,scale,width,offx,offy); - drawPostscriptNode((SequenceNode)node.right(),chunk,scale,width,offx,offy); - - - float height = node.height; - float dist = node.dist; - - int xstart = (int)((height-dist)*scale) + offx; - int xend = (int)(height*scale) + offx; - int ypos = (int)(node.ycount * chunk) + offy; - - printout("\n" + new Format("%5.3f").form((double)node.color.getRed()/255) + " " + - new Format("%5.3f").form((double)node.color.getGreen()/255) + " " + - new Format("%5.3f").form((double)node.color.getBlue()/255) + " setrgbcolor\n"); - // g.setColor(Color.black); - // bw.append("\nblack setrgbcolor\n"); - // Draw horizontal line - // g.drawLine(xstart,ypos,xend,ypos); - printout(xstart + " " + ypos + " moveto " + xend + " " + ypos + " lineto stroke\n"); - int ystart = (int)(((SequenceNode)node.left()).ycount * chunk) + offy; - int yend = (int)(((SequenceNode)node.right()).ycount * chunk) + offy; - - // g.drawLine((int)(height*scale) + offx, ystart, - // (int)(height*scale) + offx, yend); - printout - (((int)(height*scale) + offx) + " " + ystart + " moveto " + ((int)(height*scale) + offx) + " " + - yend + " lineto stroke\n"); - if (treeCanvas.showDistances && node.dist > 0) { - // g.drawString(new Format("%5.2f").form(node.dist),xstart,ypos - 5); - printout("(" +new Format("%5.2f").form(node.dist) + ") " + (xstart) + " " + (ypos+5) + " moveto show\n"); } - } -} - -public void printout(String s) throws IOException { - if (bw != null) { - bw.write(s); - } - if (ps != null) { - ps.print(s); - } - if (makeString == true) { - out.append(s); - } -} -public PostscriptProperties getPostscriptProperties() { - return pp; -} - -public FileProperties getFileProperties() { - return fp; -} - -public void setPostscriptProperties(PostscriptProperties pp) { - this.pp = pp; -} - -public void setFileProperties(FileProperties fp) { - this.fp = fp; -} public String getText(String format) { return null; } -public void getPostscript(PrintWriter bw) { - this.bw = bw; - drawPostscript(pp); - -} - -public void getPostscript(PrintStream bw) { - this.ps = bw; - drawPostscript(pp); - bw.flush(); -} - -public StringBuffer getPostscript() { - makeString = true; - out = new StringBuffer(); - drawPostscript(pp); - return out; -} - -public void propertiesInit() { - this.pp = new PostscriptProperties(); - this.fp = new FileProperties(); -} - - - public void saveButton_actionPerformed(ActionEvent e) - { - - try{ - JFileChooser chooser = new JFileChooser(jalview.bin.Cache.getProperty("LAST_DIRECTORY")); - chooser.setDialogTitle("Save Tree as postscript file"); - chooser.setToolTipText("Save"); - int value = chooser.showSaveDialog(this); - if (value == JFileChooser.APPROVE_OPTION) - { - String choice = chooser.getSelectedFile().getPath(); - jalview.bin.Cache.setProperty("LAST_DIRECTORY", choice); - - bw = new PrintWriter(new FileWriter(choice)); - getPostscript(bw); - bw.close(); - - } - - }catch(Exception ex){ex.printStackTrace();} - - } - protected void saveMenu_actionPerformed(ActionEvent e) { - + JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache. + getProperty("LAST_DIRECTORY")); + chooser.setFileView(new JalviewFileView()); + chooser.setDialogTitle("Save tree as newick file"); + chooser.setToolTipText("Save"); + int value = chooser.showSaveDialog(null); + if (value == JalviewFileChooser.APPROVE_OPTION) { + String choice = chooser.getSelectedFile().getPath(); + jalview.bin.Cache.setProperty("LAST_DIRECTORY", choice); + try{ + jalview.io.NewickFile fout = new jalview.io.NewickFile(tree.getTopNode()); + String output = fout.print(false,true); // distances only + java.io.PrintWriter out = new java.io.PrintWriter( new java.io.FileWriter( choice ) ); + out.println(output); + out.close(); + } + catch (Exception ex) { + ex.printStackTrace(); + } + } } protected void printMenu_actionPerformed(ActionEvent e) @@ -308,4 +174,67 @@ public void propertiesInit() { } + protected void epsTree_actionPerformed(ActionEvent e) + { + int width = treeCanvas.getWidth(), height=treeCanvas.getHeight(); + try + { + jalview.io.JalviewFileChooser chooser = new jalview.io.JalviewFileChooser(jalview.bin.Cache.getProperty( + "LAST_DIRECTORY"), new String[]{"eps"}, "Encapsulated Postscript"); + chooser.setFileView(new jalview.io.JalviewFileView()); + chooser.setDialogTitle("Create EPS file from tree"); + chooser.setToolTipText("Save"); + + int value = chooser.showSaveDialog(this); + if (value != jalview.io.JalviewFileChooser.APPROVE_OPTION) + return; + + jalview.bin.Cache.setProperty("LAST_DIRECTORY",chooser.getSelectedFile().getPath()); + FileOutputStream out = new FileOutputStream(chooser.getSelectedFile()); + EpsGraphics2D pg = new EpsGraphics2D("Tree", out, 0, 0, width, height); + + + treeCanvas.draw(pg, width, height); + + pg.flush(); + pg.close(); + } + catch (Exception ex) + { + ex.printStackTrace(); + } + } + + protected void pngTree_actionPerformed(ActionEvent e) + { + int width = treeCanvas.getWidth(), height = treeCanvas.getHeight(); + try + { + jalview.io.JalviewFileChooser chooser = new jalview.io.JalviewFileChooser(jalview.bin.Cache.getProperty( + "LAST_DIRECTORY"), new String[]{"png"}, "Portable network graphics"); + chooser.setFileView(new jalview.io.JalviewFileView()); + chooser.setDialogTitle("Create PNG image from tree"); + chooser.setToolTipText("Save"); + + int value = chooser.showSaveDialog(this); + if (value != jalview.io.JalviewFileChooser.APPROVE_OPTION) + return; + + jalview.bin.Cache.setProperty("LAST_DIRECTORY",chooser.getSelectedFile().getPath()); + FileOutputStream out = new FileOutputStream(chooser.getSelectedFile()); + + BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); + Graphics png = bi.getGraphics(); + + treeCanvas.draw(png, width, height); + + ImageIO.write(bi, "png", out); + out.close(); + } + catch (Exception ex) + { + ex.printStackTrace(); + } + } + }