package jalview.gui; import jalview.datamodel.*; import jalview.analysis.*; import jalview.jbgui.GTreePanel; import jalview.io.*; import java.awt.event.*; 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 { SequenceI[] seq; String type; String pwtype; AlignViewport av; int start; int end; TreeCanvas treeCanvas; NJTree tree; public NJTree getTree() { return tree; } public TreePanel(AlignViewport av, Vector seqVector, String type, String pwtype, int s, int e) { super(); this.type = type; this.pwtype = pwtype; start = s; end = e; 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(); } tree = new NJTree(seq, type, pwtype, start, end); treeCanvas = new TreeCanvas(av, tree, scrollPane, longestName); tree.reCount(tree.getTopNode()); tree.findHeight(tree.getTopNode()); 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(); 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); tree.reCount(tree.getTopNode()); tree.findHeight(tree.getTopNode()); treeCanvas = new TreeCanvas(av, tree, scrollPane, longestName); treeCanvas.setShowBootstrap(newtree.HasBootstrap()); treeCanvas.setShowDistances(newtree.HasDistances()); scrollPane.setViewportView(treeCanvas); } public String getText(String format) { return null; } public void saveAsNewick_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) { //Putting in a thread avoids Swing painting problems treeCanvas.startPrinting(); } protected void fitToWindow_actionPerformed(ActionEvent e) { treeCanvas.fitToWindow = fitToWindow.isSelected(); repaint(); } 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); } protected void distanceMenu_actionPerformed(ActionEvent e) { treeCanvas.setShowDistances(distanceMenu.isSelected()); } protected void bootstrapMenu_actionPerformed(ActionEvent e) { treeCanvas.setShowBootstrap(bootstrapMenu.isSelected()); } 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(); } } }