import java.io.*;\r
import jalview.util.*;\r
import javax.swing.*;\r
+import java.awt.print.*;\r
+import java.awt.*;\r
+import java.awt.image.*;\r
+import org.jibble.epsgraphics.*;\r
+import javax.imageio.*;\r
+\r
+\r
\r
\r
public class TreePanel extends GTreePanel\r
TreeCanvas treeCanvas;\r
NJTree tree;\r
\r
- FileProperties fp;\r
- PostscriptProperties pp;\r
-\r
- PrintWriter bw;\r
- PrintStream ps;\r
- boolean makeString = false;\r
- StringBuffer out;\r
-\r
-\r
+ public NJTree getTree()\r
+ {\r
+ return tree;\r
+ }\r
\r
public TreePanel(AlignViewport av, Vector seqVector, String type, String pwtype, int s, int e)\r
{\r
}\r
\r
tree = new NJTree(seq, type, pwtype, start, end);\r
- av.setCurrentTree(tree);\r
-\r
- propertiesInit();\r
\r
treeCanvas = new TreeCanvas(av, tree, scrollPane, longestName);\r
\r
scrollPane.setViewportView(treeCanvas);\r
\r
}\r
+ public TreePanel(AlignViewport av, Vector seqVector, NewickFile newtree,\r
+ String type, String pwtype)\r
+ {\r
+ super();\r
+ // These are probably only arbitrary strings reflecting source of tree\r
+ this.type = type;\r
+ this.pwtype = pwtype;\r
\r
+ start = 0;\r
+ end = seqVector.size();\r
\r
-public void drawPostscript(PostscriptProperties pp) {\r
- try {\r
- int width = 0;\r
- int height = 0;\r
-\r
- printout("%!\n");\r
+ String longestName = "";\r
+ seq = new Sequence [seqVector.size()];\r
+ for (int i=0;i < seqVector.size();i++)\r
+ {\r
+ seq[i] = (Sequence) seqVector.elementAt(i);\r
+ if(seq[i].getName().length()>longestName.length())\r
+ longestName = seq[i].getName();\r
+ }\r
+ // This constructor matches sequence names to treenodes and sets up the tree layouts.\r
+ tree = new NJTree(seq, newtree);\r
\r
- printout("/" + pp.font + " findfont\n");\r
- printout(pp.fsize + " scalefont setfont\n");\r
+ tree.reCount(tree.getTopNode());\r
+ tree.findHeight(tree.getTopNode());\r
\r
- int offx = pp.xoffset;\r
- int offy = pp.yoffset;\r
+ treeCanvas = new TreeCanvas(av, tree, scrollPane, longestName);\r
+ treeCanvas.setShowBootstrap(newtree.HasBootstrap());\r
+ treeCanvas.setShowDistances(newtree.HasDistances());\r
+ scrollPane.setViewportView(treeCanvas);\r
\r
- if (pp.orientation == PostscriptProperties.PORTRAIT) {\r
- width = PostscriptProperties.SHORTSIDE;\r
- height = PostscriptProperties.LONGSIDE;\r
- } else {\r
- height = PostscriptProperties.SHORTSIDE;\r
- width = PostscriptProperties.LONGSIDE;\r
- printout(height + " 0 translate\n90 rotate\n");\r
}\r
- float wscale = (float)(width*.8-offx*2)/tree.getMaxHeight();\r
- SequenceNode top = tree.getTopNode();\r
\r
- if (top.count == 0) {\r
- top.count = ((SequenceNode)top.left()).count + ((SequenceNode)top.right()).count ;\r
+ public String getText(String format)\r
+ {\r
+ return null;\r
}\r
\r
- float chunk = (float)(height-offy*2)/(((SequenceNode)top).count+1);\r
-\r
- drawPostscriptNode(top,chunk,wscale,width,offx,offy);\r
-\r
- printout("showpage\n");\r
- } catch (java.io.IOException e) {\r
- System.out.println("Exception " + e);\r
- }\r
-}\r
+ public void saveAsNewick_actionPerformed(ActionEvent e)\r
+ {\r
\r
-public void drawPostscriptNode(SequenceNode node, float chunk, float scale, int width, int offx, int offy) throws java.io.IOException {\r
- if (node == null) {\r
- return;\r
+ JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.\r
+ getProperty("LAST_DIRECTORY"));\r
+ chooser.setFileView(new JalviewFileView());\r
+ chooser.setDialogTitle("Save tree as newick file");\r
+ chooser.setToolTipText("Save");\r
+ int value = chooser.showSaveDialog(null);\r
+ if (value == JalviewFileChooser.APPROVE_OPTION) {\r
+ String choice = chooser.getSelectedFile().getPath();\r
+ jalview.bin.Cache.setProperty("LAST_DIRECTORY", chooser.getSelectedFile().getParent());\r
+ try{\r
+ jalview.io.NewickFile fout = new jalview.io.NewickFile(tree.getTopNode());\r
+ String output = fout.print(false,true); // distances only\r
+ java.io.PrintWriter out = new java.io.PrintWriter( new java.io.FileWriter( choice ) );\r
+ out.println(output);\r
+ out.close();\r
+ }\r
+ catch (Exception ex) {\r
+ ex.printStackTrace();\r
+ }\r
+ }\r
}\r
\r
- if (node.left() == null && node.right() == null) {\r
- // Drawing leaf node\r
-\r
- float height = node.height;\r
- float dist = node.dist;\r
-\r
- int xstart = (int)((height-dist)*scale) + offx;\r
- int xend = (int)(height*scale) + offx;\r
-\r
- int ypos = (int)(node.ycount * chunk) + offy;\r
-\r
- // g.setColor(Color.black);\r
- printout("\n" + new Format("%5.3f").form((double)node.color.getRed()/255) + " " +\r
- new Format("%5.3f").form((double)node.color.getGreen()/255) + " " +\r
- new Format("%5.3f").form((double)node.color.getBlue()/255) + " setrgbcolor\n");\r
-\r
- // Draw horizontal line\r
- // g.drawLine(xstart,ypos,xend,ypos);\r
- printout(xstart + " " + ypos + " moveto " + xend + " " + ypos + " lineto stroke \n");\r
+ protected void printMenu_actionPerformed(ActionEvent e)\r
+ {\r
\r
- if (treeCanvas.showDistances && node.dist > 0) {\r
- // g.drawString(new Format("%5.2f").form(node.dist),xstart,ypos - 5);\r
- printout("(" + new Format("%5.2f").form(node.dist) + ") " + xstart + " " + (ypos+5) + " moveto show\n");\r
- }\r
- //g.drawString((String)node.element(),xend+20,ypos);\r
- printout("(" + (((SequenceI)node.element()).getName()) + ") " + (xend+20) + " " + (ypos) + " moveto show\n");\r
- } else {\r
- drawPostscriptNode((SequenceNode)node.left(),chunk,scale,width,offx,offy);\r
- drawPostscriptNode((SequenceNode)node.right(),chunk,scale,width,offx,offy);\r
-\r
-\r
- float height = node.height;\r
- float dist = node.dist;\r
-\r
- int xstart = (int)((height-dist)*scale) + offx;\r
- int xend = (int)(height*scale) + offx;\r
- int ypos = (int)(node.ycount * chunk) + offy;\r
-\r
- printout("\n" + new Format("%5.3f").form((double)node.color.getRed()/255) + " " +\r
- new Format("%5.3f").form((double)node.color.getGreen()/255) + " " +\r
- new Format("%5.3f").form((double)node.color.getBlue()/255) + " setrgbcolor\n");\r
- // g.setColor(Color.black);\r
- // bw.append("\nblack setrgbcolor\n");\r
- // Draw horizontal line\r
- // g.drawLine(xstart,ypos,xend,ypos);\r
- printout(xstart + " " + ypos + " moveto " + xend + " " + ypos + " lineto stroke\n");\r
- int ystart = (int)(((SequenceNode)node.left()).ycount * chunk) + offy;\r
- int yend = (int)(((SequenceNode)node.right()).ycount * chunk) + offy;\r
-\r
- // g.drawLine((int)(height*scale) + offx, ystart,\r
- // (int)(height*scale) + offx, yend);\r
- printout\r
- (((int)(height*scale) + offx) + " " + ystart + " moveto " + ((int)(height*scale) + offx) + " " +\r
- yend + " lineto stroke\n");\r
- if (treeCanvas.showDistances && node.dist > 0) {\r
- // g.drawString(new Format("%5.2f").form(node.dist),xstart,ypos - 5);\r
- printout("(" +new Format("%5.2f").form(node.dist) + ") " + (xstart) + " " + (ypos+5) + " moveto show\n");\r
- }\r
+ //Putting in a thread avoids Swing painting problems\r
+ treeCanvas.startPrinting();\r
}\r
-}\r
\r
-public void printout(String s) throws IOException {\r
- if (bw != null) {\r
- bw.write(s);\r
- }\r
- if (ps != null) {\r
- ps.print(s);\r
- }\r
- if (makeString == true) {\r
- out.append(s);\r
+ protected void fitToWindow_actionPerformed(ActionEvent e)\r
+ {\r
+ treeCanvas.fitToWindow = fitToWindow.isSelected();\r
+ repaint();\r
}\r
-}\r
-\r
-\r
-public PostscriptProperties getPostscriptProperties() {\r
- return pp;\r
-}\r
-\r
-public FileProperties getFileProperties() {\r
- return fp;\r
-}\r
-\r
-public void setPostscriptProperties(PostscriptProperties pp) {\r
- this.pp = pp;\r
-}\r
\r
-public void setFileProperties(FileProperties fp) {\r
- this.fp = fp;\r
-}\r
\r
-public String getText(String format) {\r
- return null;\r
-}\r
-\r
-public void getPostscript(PrintWriter bw) {\r
- this.bw = bw;\r
- drawPostscript(pp);\r
+ protected void fontSize_actionPerformed(ActionEvent e)\r
+ {\r
+ if( treeCanvas==null )\r
+ return;\r
\r
-}\r
+ String size = fontSize.getText().substring( fontSize.getText().indexOf("-")+1);\r
+\r
+ Object selection = JOptionPane.showInternalInputDialog(Desktop.desktop,\r
+ "Select font size",\r
+ "Font size",\r
+ JOptionPane.QUESTION_MESSAGE,\r
+ null, new String[]{"1","2","4","6","8","10","12","14","16","18","20"}\r
+ ,"Font Size - "+size);\r
+ if(selection!=null)\r
+ {\r
+ fontSize.setText("Font Size - " + selection);\r
+\r
+ int i = Integer.parseInt(selection.toString());\r
+ treeCanvas.setFontSize(i);\r
+ }\r
+ scrollPane.setViewportView(treeCanvas);\r
\r
-public void getPostscript(PrintStream bw) {\r
- this.ps = bw;\r
- drawPostscript(pp);\r
- bw.flush();\r
-}\r
+ }\r
\r
-public StringBuffer getPostscript() {\r
- makeString = true;\r
- out = new StringBuffer();\r
- drawPostscript(pp);\r
- return out;\r
-}\r
+ protected void distanceMenu_actionPerformed(ActionEvent e)\r
+ {\r
+ treeCanvas.setShowDistances(distanceMenu.isSelected());\r
+ }\r
\r
-public void propertiesInit() {\r
- this.pp = new PostscriptProperties();\r
- this.fp = new FileProperties();\r
-}\r
+ protected void bootstrapMenu_actionPerformed(ActionEvent e)\r
+ {\r
+ treeCanvas.setShowBootstrap(bootstrapMenu.isSelected());\r
+ }\r
\r
\r
- public void saveButton_actionPerformed(ActionEvent e)\r
+ protected void epsTree_actionPerformed(ActionEvent e)\r
{\r
-\r
- try{\r
- JFileChooser chooser = new JFileChooser(jalview.bin.Cache.LAST_DIRECTORY);\r
- chooser.setDialogTitle("Save Tree as postscript file");\r
+ int width = treeCanvas.getWidth(), height=treeCanvas.getHeight();\r
+ try\r
+ {\r
+ jalview.io.JalviewFileChooser chooser = new jalview.io.JalviewFileChooser(jalview.bin.Cache.getProperty(\r
+ "LAST_DIRECTORY"),\r
+ new String[]{"eps"},\r
+ new String[]{"Encapsulated Postscript"},\r
+ "Encapsulated Postscript");\r
+ chooser.setFileView(new jalview.io.JalviewFileView());\r
+ chooser.setDialogTitle("Create EPS file from tree");\r
chooser.setToolTipText("Save");\r
+\r
int value = chooser.showSaveDialog(this);\r
- if (value == JFileChooser.APPROVE_OPTION)\r
- {\r
- String choice = chooser.getSelectedFile().getPath();\r
- jalview.bin.Cache.LAST_DIRECTORY = choice;\r
+ if (value != jalview.io.JalviewFileChooser.APPROVE_OPTION)\r
+ return;\r
\r
- bw = new PrintWriter(new FileWriter(choice));\r
- getPostscript(bw);\r
- bw.close();\r
+ jalview.bin.Cache.setProperty("LAST_DIRECTORY",chooser.getSelectedFile().getParent());\r
+ FileOutputStream out = new FileOutputStream(chooser.getSelectedFile());\r
+ EpsGraphics2D pg = new EpsGraphics2D("Tree", out, 0, 0, width, height);\r
\r
- }\r
\r
- }catch(Exception ex){ex.printStackTrace();}\r
+ treeCanvas.draw(pg, width, height);\r
\r
+ pg.flush();\r
+ pg.close();\r
+ }\r
+ catch (Exception ex)\r
+ {\r
+ ex.printStackTrace();\r
+ }\r
}\r
\r
- public void showDistanceCheckbox_actionPerformed(ActionEvent e)\r
- {\r
- treeCanvas.setShowDistances(showDistanceCheckbox.isSelected());\r
- }\r
+ protected void pngTree_actionPerformed(ActionEvent e)\r
+ {\r
+ int width = treeCanvas.getWidth(), height = treeCanvas.getHeight();\r
+ try\r
+ {\r
+ jalview.io.JalviewFileChooser chooser = new jalview.io.JalviewFileChooser(\r
+ jalview.bin.Cache.getProperty("LAST_DIRECTORY"),\r
+ new String[] {"png"},\r
+ new String[] {"Portable network graphics"},\r
+ "Portable network graphics");\r
+\r
+ chooser.setFileView(new jalview.io.JalviewFileView());\r
+ chooser.setDialogTitle("Create PNG image from tree");\r
+ chooser.setToolTipText("Save");\r
\r
- public void fontSizeCombobox_actionPerformed(ActionEvent e)\r
- {\r
- if( treeCanvas==null )\r
- return;\r
+ int value = chooser.showSaveDialog(this);\r
+ if (value != jalview.io.JalviewFileChooser.APPROVE_OPTION)\r
+ return;\r
\r
- int size = Integer.parseInt( fontSizeCombobox.getSelectedItem().toString() );\r
- treeCanvas.setFontSize( size );\r
+ jalview.bin.Cache.setProperty("LAST_DIRECTORY",chooser.getSelectedFile().getParent());\r
+ FileOutputStream out = new FileOutputStream(chooser.getSelectedFile());\r
\r
- scrollPane.setViewportView(treeCanvas);\r
- }\r
+ BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);\r
+ Graphics png = bi.getGraphics();\r
\r
- public void bootStrapCheckBox_actionPerformed(ActionEvent e)\r
- {\r
- treeCanvas.setShowBootstrap(bootStrapCheckBox.isSelected());\r
+ treeCanvas.draw(png, width, height);\r
+\r
+ ImageIO.write(bi, "png", out);\r
+ out.close();\r
+ }\r
+ catch (Exception ex)\r
+ {\r
+ ex.printStackTrace();\r
+ }\r
}\r
\r
}\r