2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
\r
5 * This program is free software; you can redistribute it and/or
\r
6 * modify it under the terms of the GNU General Public License
\r
7 * as published by the Free Software Foundation; either version 2
\r
8 * of the License, or (at your option) any later version.
\r
10 * This program is distributed in the hope that it will be useful,
\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 * GNU General Public License for more details.
\r
15 * You should have received a copy of the GNU General Public License
\r
16 * along with this program; if not, write to the Free Software
\r
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
\r
19 package jalview.gui;
\r
21 import jalview.analysis.*;
\r
23 import jalview.datamodel.*;
\r
25 import jalview.io.*;
\r
27 import jalview.jbgui.*;
\r
29 import org.jibble.epsgraphics.*;
\r
32 import java.awt.event.*;
\r
33 import java.awt.image.*;
\r
39 import javax.imageio.*;
\r
41 import java.beans.PropertyChangeEvent;
\r
48 * @version $Revision$
\r
50 public class TreePanel extends GTreePanel
\r
54 TreeCanvas treeCanvas;
\r
59 * Creates a new TreePanel object.
\r
61 * @param av DOCUMENT ME!
\r
62 * @param seqVector DOCUMENT ME!
\r
63 * @param type DOCUMENT ME!
\r
64 * @param pwtype DOCUMENT ME!
\r
65 * @param s DOCUMENT ME!
\r
66 * @param e DOCUMENT ME!
\r
68 public TreePanel(AlignViewport av, String type, String pwtype)
\r
71 initTreePanel(av, type, pwtype, null);
\r
73 // We know this tree has distances. JBPNote TODO: prolly should add this as a userdefined default
\r
74 showDistances(true);
\r
78 * Creates a new TreePanel object.
\r
80 * @param av DOCUMENT ME!
\r
81 * @param seqVector DOCUMENT ME!
\r
82 * @param newtree DOCUMENT ME!
\r
83 * @param type DOCUMENT ME!
\r
84 * @param pwtype DOCUMENT ME!
\r
86 public TreePanel(AlignViewport av,
\r
92 initTreePanel(av, type, pwtype, newtree);
\r
95 public AlignmentI getAlignment()
\r
97 return treeCanvas.av.getAlignment();
\r
101 void initTreePanel(AlignViewport av, String type, String pwtype,
\r
102 NewickFile newTree)
\r
106 this.pwtype = pwtype;
\r
108 treeCanvas = new TreeCanvas(av, scrollPane);
\r
109 scrollPane.setViewportView(treeCanvas);
\r
111 av.addPropertyChangeListener(new java.beans.PropertyChangeListener()
\r
113 public void propertyChange(PropertyChangeEvent evt)
\r
115 if (evt.getPropertyName().equals("alignment"))
\r
118 System.out.println("tree is null");
\r
119 if(evt.getNewValue()==null)
\r
120 System.out.println("new value is null");
\r
122 tree.UpdatePlaceHolders( (Vector) evt.getNewValue());
\r
132 TreeLoader tl = new TreeLoader(newTree);
\r
137 class TreeLoader extends Thread
\r
139 NewickFile newtree;
\r
141 public TreeLoader(NewickFile newtree)
\r
143 this.newtree = newtree;
\r
144 if (newtree != null)
\r
146 // Must be outside run(), as Jalview2XML tries to
\r
147 // update distance/bootstrap visibility at the same time
\r
148 showBootstrap(newtree.HasBootstrap());
\r
149 showDistances(newtree.HasDistances());
\r
157 tree = new NJTree(av.alignment.getSequencesArray(),
\r
163 String [] seqStrings = null;
\r
164 if (av.hasHiddenColumns)
\r
166 seqStrings = av.getSelectionAsString();
\r
169 if(av.getSelectionGroup()==null)
\r
172 end = av.alignment.getWidth();
\r
173 seqs = av.alignment.getSequencesArray();
\r
177 start = av.getSelectionGroup().getStartRes();
\r
178 end = av.getSelectionGroup().getEndRes()+1;
\r
179 seqs = av.getSelectionGroup().getSequencesInOrder(av.alignment);
\r
182 tree = new NJTree(seqs, seqStrings, type, pwtype, start, end);
\r
186 tree.reCount(tree.getTopNode());
\r
187 tree.findHeight(tree.getTopNode());
\r
188 treeCanvas.setTree(tree);
\r
190 treeCanvas.repaint();
\r
192 av.setCurrentTree(tree);
\r
197 public void showDistances(boolean b)
\r
199 treeCanvas.setShowDistances(b);
\r
200 distanceMenu.setSelected(b);
\r
203 public void showBootstrap(boolean b)
\r
205 treeCanvas.setShowBootstrap(b);
\r
206 bootstrapMenu.setSelected(b);
\r
209 public void showPlaceholders(boolean b)
\r
211 placeholdersMenu.setState(b);
\r
212 treeCanvas.setMarkPlaceholders(b);
\r
221 * @return DOCUMENT ME!
\r
223 public NJTree getTree()
\r
232 * @param e DOCUMENT ME!
\r
234 public void textbox_actionPerformed(ActionEvent e)
\r
236 CutAndPasteTransfer cap = new CutAndPasteTransfer();
\r
238 StringBuffer buffer = new StringBuffer();
\r
240 if (type.equals("AV"))
\r
242 buffer.append("Average distance tree using ");
\r
246 buffer.append("Neighbour joining tree using ");
\r
249 if (pwtype.equals("BL"))
\r
251 buffer.append("BLOSUM62");
\r
255 buffer.append("PID");
\r
258 Desktop.addInternalFrame(cap, buffer.toString(), 500, 100);
\r
260 jalview.io.NewickFile fout = new jalview.io.NewickFile(tree.getTopNode());
\r
261 cap.setText(fout.print(tree.isHasBootstrap(), tree.isHasDistances(), tree.isHasRootDistance()));
\r
267 * @param e DOCUMENT ME!
\r
269 public void saveAsNewick_actionPerformed(ActionEvent e)
\r
271 JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.getProperty(
\r
272 "LAST_DIRECTORY"));
\r
273 chooser.setFileView(new JalviewFileView());
\r
274 chooser.setDialogTitle("Save tree as newick file");
\r
275 chooser.setToolTipText("Save");
\r
277 int value = chooser.showSaveDialog(null);
\r
279 if (value == JalviewFileChooser.APPROVE_OPTION)
\r
281 String choice = chooser.getSelectedFile().getPath();
\r
282 jalview.bin.Cache.setProperty("LAST_DIRECTORY",
\r
283 chooser.getSelectedFile().getParent());
\r
287 jalview.io.NewickFile fout = new jalview.io.NewickFile(tree.getTopNode());
\r
288 String output = fout.print(tree.isHasBootstrap(), tree.isHasDistances(), tree.isHasRootDistance());
\r
289 java.io.PrintWriter out = new java.io.PrintWriter(new java.io.FileWriter(
\r
291 out.println(output);
\r
294 catch (Exception ex)
\r
296 ex.printStackTrace();
\r
304 * @param e DOCUMENT ME!
\r
306 public void printMenu_actionPerformed(ActionEvent e)
\r
308 //Putting in a thread avoids Swing painting problems
\r
309 treeCanvas.startPrinting();
\r
313 public void originalSeqData_actionPerformed(ActionEvent e)
\r
315 CutAndPasteTransfer cap = new CutAndPasteTransfer();
\r
316 cap.setText(tree.printOriginalSequenceData());
\r
317 Desktop.addInternalFrame(cap, "Original Data",
\r
325 * @param e DOCUMENT ME!
\r
327 public void fitToWindow_actionPerformed(ActionEvent e)
\r
329 treeCanvas.fitToWindow = fitToWindow.isSelected();
\r
336 * @param e DOCUMENT ME!
\r
338 public void font_actionPerformed(ActionEvent e)
\r
340 if (treeCanvas == null)
\r
345 new FontChooser(this);
\r
348 public Font getTreeFont()
\r
350 return treeCanvas.font;
\r
353 public void setTreeFont(Font font)
\r
355 if(treeCanvas!=null)
\r
356 treeCanvas.setFont(font);
\r
362 * @param e DOCUMENT ME!
\r
364 public void distanceMenu_actionPerformed(ActionEvent e)
\r
366 treeCanvas.setShowDistances(distanceMenu.isSelected());
\r
372 * @param e DOCUMENT ME!
\r
374 public void bootstrapMenu_actionPerformed(ActionEvent e)
\r
376 treeCanvas.setShowBootstrap(bootstrapMenu.isSelected());
\r
382 * @param e DOCUMENT ME!
\r
384 public void placeholdersMenu_actionPerformed(ActionEvent e)
\r
386 treeCanvas.setMarkPlaceholders(placeholdersMenu.isSelected());
\r
392 * @param e DOCUMENT ME!
\r
394 public void epsTree_actionPerformed(ActionEvent e)
\r
396 boolean accurateText = true;
\r
398 String renderStyle = jalview.bin.Cache.getDefault("EPS_RENDERING",
\r
399 "Prompt each time");
\r
401 // If we need to prompt, and if the GUI is visible then
\r
402 // Prompt for EPS rendering style
\r
403 if (renderStyle.equalsIgnoreCase("Prompt each time")
\r
405 (System.getProperty("java.awt.headless") != null
\r
406 && System.getProperty("java.awt.headless").equals("true")))
\r
408 EPSOptions eps = new EPSOptions();
\r
409 renderStyle = eps.getValue();
\r
411 if (renderStyle==null || eps.cancelled)
\r
417 if (renderStyle.equalsIgnoreCase("text"))
\r
419 accurateText = false;
\r
422 int width = treeCanvas.getWidth();
\r
423 int height = treeCanvas.getHeight();
\r
427 jalview.io.JalviewFileChooser chooser = new jalview.io.JalviewFileChooser(jalview.bin.Cache.getProperty(
\r
428 "LAST_DIRECTORY"), new String[] { "eps" },
\r
429 new String[] { "Encapsulated Postscript" },
\r
430 "Encapsulated Postscript");
\r
431 chooser.setFileView(new jalview.io.JalviewFileView());
\r
432 chooser.setDialogTitle("Create EPS file from tree");
\r
433 chooser.setToolTipText("Save");
\r
435 int value = chooser.showSaveDialog(this);
\r
437 if (value != jalview.io.JalviewFileChooser.APPROVE_OPTION)
\r
442 jalview.bin.Cache.setProperty("LAST_DIRECTORY",
\r
443 chooser.getSelectedFile().getParent());
\r
445 FileOutputStream out = new FileOutputStream(chooser.getSelectedFile());
\r
446 EpsGraphics2D pg = new EpsGraphics2D("Tree", out, 0, 0, width,
\r
449 pg.setAccurateTextMode(accurateText);
\r
451 treeCanvas.draw(pg, width, height);
\r
456 catch (Exception ex)
\r
458 ex.printStackTrace();
\r
465 * @param e DOCUMENT ME!
\r
467 public void pngTree_actionPerformed(ActionEvent e)
\r
469 int width = treeCanvas.getWidth();
\r
470 int height = treeCanvas.getHeight();
\r
474 jalview.io.JalviewFileChooser chooser = new jalview.io.JalviewFileChooser(jalview.bin.Cache.getProperty(
\r
475 "LAST_DIRECTORY"), new String[] { "png" },
\r
476 new String[] { "Portable network graphics" },
\r
477 "Portable network graphics");
\r
479 chooser.setFileView(new jalview.io.JalviewFileView());
\r
480 chooser.setDialogTitle("Create PNG image from tree");
\r
481 chooser.setToolTipText("Save");
\r
483 int value = chooser.showSaveDialog(this);
\r
485 if (value != jalview.io.JalviewFileChooser.APPROVE_OPTION)
\r
490 jalview.bin.Cache.setProperty("LAST_DIRECTORY",
\r
491 chooser.getSelectedFile().getParent());
\r
493 FileOutputStream out = new FileOutputStream(chooser.getSelectedFile());
\r
495 BufferedImage bi = new BufferedImage(width, height,
\r
496 BufferedImage.TYPE_INT_RGB);
\r
497 Graphics png = bi.getGraphics();
\r
499 treeCanvas.draw(png, width, height);
\r
501 ImageIO.write(bi, "png", out);
\r
504 catch (Exception ex)
\r
506 ex.printStackTrace();
\r