X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fgui%2FTreePanel.java;h=c45863817185173ab0dda713f701cb6ab826019c;hb=1026b7a36483fe6fec0a3d90f1496e49dc9b00ad;hp=9e9e70bd7498839574c5afd51b8b9e2ebb35c09b;hpb=1ecf6419aba86993b3c223bf5ec0fa79427baf85;p=jalview.git diff --git a/src/jalview/gui/TreePanel.java b/src/jalview/gui/TreePanel.java index 9e9e70b..c458638 100755 --- a/src/jalview/gui/TreePanel.java +++ b/src/jalview/gui/TreePanel.java @@ -9,6 +9,13 @@ import java.util.*; import java.io.*; import jalview.util.*; import javax.swing.*; +import java.awt.print.*; +import java.awt.*; +import java.awt.image.*; +import org.jibble.epsgraphics.*; +import javax.imageio.*; + + public class TreePanel extends GTreePanel @@ -22,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) { @@ -52,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); @@ -63,212 +62,185 @@ 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"); + 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); - printout("/" + pp.font + " findfont\n"); - printout(pp.fsize + " scalefont setfont\n"); + tree.reCount(tree.getTopNode()); + tree.findHeight(tree.getTopNode()); - int offx = pp.xoffset; - int offy = pp.yoffset; + treeCanvas = new TreeCanvas(av, tree, scrollPane, longestName); + treeCanvas.setShowBootstrap(newtree.HasBootstrap()); + treeCanvas.setShowDistances(newtree.HasDistances()); + scrollPane.setViewportView(treeCanvas); - 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 ; + public String getText(String format) + { + return null; } - 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 saveAsNewick_actionPerformed(ActionEvent e) + { -public void drawPostscriptNode(SequenceNode node, float chunk, float scale, int width, int offx, int offy) throws java.io.IOException { - if (node == null) { - return; + 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", chooser.getSelectedFile().getParent()); + 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(); + } + } } - 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; - - // 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"); - - // Draw horizontal line - // g.drawLine(xstart,ypos,xend,ypos); - printout(xstart + " " + ypos + " moveto " + xend + " " + ypos + " lineto stroke \n"); + protected void printMenu_actionPerformed(ActionEvent e) + { - 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"); - } + //Putting in a thread avoids Swing painting problems + treeCanvas.startPrinting(); } -} -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); + protected void fitToWindow_actionPerformed(ActionEvent e) + { + treeCanvas.fitToWindow = fitToWindow.isSelected(); + repaint(); } -} - - -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); + protected void fontSize_actionPerformed(ActionEvent e) + { + if( treeCanvas==null ) + return; -} + String size = fontSize.getText().substring( fontSize.getText().indexOf("-")+1); + + Object selection = JOptionPane.showInternalInputDialog(Desktop.desktop, + "Select font size", + "Font size", + JOptionPane.QUESTION_MESSAGE, + null, new String[]{"1","2","4","6","8","10","12","14","16","18","20"} + ,"Font Size - "+size); + if(selection!=null) + { + fontSize.setText("Font Size - " + selection); + + int i = Integer.parseInt(selection.toString()); + treeCanvas.setFontSize(i); + } + scrollPane.setViewportView(treeCanvas); -public void getPostscript(PrintStream bw) { - this.ps = bw; - drawPostscript(pp); - bw.flush(); -} + } -public StringBuffer getPostscript() { - makeString = true; - out = new StringBuffer(); - drawPostscript(pp); - return out; -} + protected void distanceMenu_actionPerformed(ActionEvent e) + { + treeCanvas.setShowDistances(distanceMenu.isSelected()); + } -public void propertiesInit() { - this.pp = new PostscriptProperties(); - this.fp = new FileProperties(); -} + protected void bootstrapMenu_actionPerformed(ActionEvent e) + { + treeCanvas.setShowBootstrap(bootstrapMenu.isSelected()); + } - public void saveButton_actionPerformed(ActionEvent e) + protected void epsTree_actionPerformed(ActionEvent e) { - - try{ - JFileChooser chooser = new JFileChooser(jalview.bin.Cache.LAST_DIRECTORY); - chooser.setDialogTitle("Save Tree as postscript file"); + 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"}, + new String[]{"Encapsulated Postscript"}, + "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 == JFileChooser.APPROVE_OPTION) - { - String choice = chooser.getSelectedFile().getPath(); - jalview.bin.Cache.LAST_DIRECTORY = choice; + if (value != jalview.io.JalviewFileChooser.APPROVE_OPTION) + return; - bw = new PrintWriter(new FileWriter(choice)); - getPostscript(bw); - bw.close(); + jalview.bin.Cache.setProperty("LAST_DIRECTORY",chooser.getSelectedFile().getParent()); + FileOutputStream out = new FileOutputStream(chooser.getSelectedFile()); + EpsGraphics2D pg = new EpsGraphics2D("Tree", out, 0, 0, width, height); - } - }catch(Exception ex){ex.printStackTrace();} + treeCanvas.draw(pg, width, height); + pg.flush(); + pg.close(); + } + catch (Exception ex) + { + ex.printStackTrace(); + } } - public void showDistanceCheckbox_actionPerformed(ActionEvent e) - { - treeCanvas.setShowDistances(showDistanceCheckbox.isSelected()); - } + 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"}, + new String[] {"Portable network graphics"}, + "Portable network graphics"); + + chooser.setFileView(new jalview.io.JalviewFileView()); + chooser.setDialogTitle("Create PNG image from tree"); + chooser.setToolTipText("Save"); - public void fontSizeCombobox_actionPerformed(ActionEvent e) - { - if( treeCanvas==null ) - return; + int value = chooser.showSaveDialog(this); + if (value != jalview.io.JalviewFileChooser.APPROVE_OPTION) + return; - int size = Integer.parseInt( fontSizeCombobox.getSelectedItem().toString() ); - treeCanvas.setFontSize( size ); + jalview.bin.Cache.setProperty("LAST_DIRECTORY",chooser.getSelectedFile().getParent()); + FileOutputStream out = new FileOutputStream(chooser.getSelectedFile()); - scrollPane.setViewportView(treeCanvas); - } + BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); + Graphics png = bi.getGraphics(); - public void bootStrapCheckBox_actionPerformed(ActionEvent e) - { - treeCanvas.setShowBootstrap(bootStrapCheckBox.isSelected()); + treeCanvas.draw(png, width, height); + + ImageIO.write(bi, "png", out); + out.close(); + } + catch (Exception ex) + { + ex.printStackTrace(); + } } }