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 javax.swing.*;
\r
48 * @version $Revision$
\r
50 public class TreePanel extends GTreePanel
\r
57 TreeCanvas treeCanvas;
\r
61 * Creates a new TreePanel object.
\r
63 * @param av DOCUMENT ME!
\r
64 * @param seqVector DOCUMENT ME!
\r
65 * @param type DOCUMENT ME!
\r
66 * @param pwtype DOCUMENT ME!
\r
67 * @param s DOCUMENT ME!
\r
68 * @param e DOCUMENT ME!
\r
70 public TreePanel(AlignViewport av, Vector seqVector, String type,
\r
71 String pwtype, int s, int e)
\r
76 this.pwtype = pwtype;
\r
81 String longestName = "";
\r
82 seq = new Sequence[seqVector.size()];
\r
84 for (int i = 0; i < seqVector.size(); i++)
\r
86 seq[i] = (Sequence) seqVector.elementAt(i);
\r
88 if (seq[i].getName().length() > longestName.length())
\r
90 longestName = seq[i].getName();
\r
94 tree = new NJTree(seq, type, pwtype, start, end);
\r
96 treeCanvas = new TreeCanvas(av, tree, scrollPane, longestName);
\r
97 treeCanvas.setShowDistances(true); // We know this tree has distances. JBPNote TODO: prolly should add this as a userdefined default
\r
98 setViewStateFromTreeCanvas(treeCanvas);
\r
99 tree.reCount(tree.getTopNode());
\r
100 tree.findHeight(tree.getTopNode());
\r
101 scrollPane.setViewportView(treeCanvas);
\r
105 * Creates a new TreePanel object.
\r
107 * @param av DOCUMENT ME!
\r
108 * @param seqVector DOCUMENT ME!
\r
109 * @param newtree DOCUMENT ME!
\r
110 * @param type DOCUMENT ME!
\r
111 * @param pwtype DOCUMENT ME!
\r
113 public TreePanel(AlignViewport av, Vector seqVector, NewickFile newtree,
\r
114 String type, String pwtype)
\r
118 // These are probably only arbitrary strings reflecting source of tree
\r
120 this.pwtype = pwtype;
\r
123 end = seqVector.size();
\r
125 String longestName = "";
\r
126 seq = new Sequence[seqVector.size()];
\r
128 for (int i = 0; i < seqVector.size(); i++)
\r
130 seq[i] = (Sequence) seqVector.elementAt(i);
\r
133 // This constructor matches sequence names to treenodes and sets up the tree layouts.
\r
134 tree = new NJTree(seq, newtree);
\r
136 // Now have to calculate longest name based on the leaves
\r
137 Vector leaves = tree.findLeaves(tree.getTopNode(), new Vector());
\r
138 boolean has_placeholders = false;
\r
140 for (int i = 0; i < leaves.size(); i++)
\r
142 SequenceNode lf = (SequenceNode) leaves.elementAt(i);
\r
144 if (lf.isPlaceholder())
\r
146 has_placeholders = true;
\r
149 if (longestName.length() < ((Sequence) lf.element()).getName()
\r
152 longestName = TreeCanvas.PLACEHOLDER +
\r
153 ((Sequence) lf.element()).getName();
\r
157 tree.reCount(tree.getTopNode());
\r
158 tree.findHeight(tree.getTopNode());
\r
160 treeCanvas = new TreeCanvas(av, tree, scrollPane, longestName);
\r
161 treeCanvas.setShowBootstrap(newtree.HasBootstrap());
\r
162 treeCanvas.setShowDistances(newtree.HasDistances());
\r
163 scrollPane.setViewportView(treeCanvas);
\r
164 treeCanvas.setMarkPlaceholders(has_placeholders);
\r
165 setViewStateFromTreeCanvas(treeCanvas);
\r
167 // JBPNote TODO: preference for always marking placeholders in new associated tree
\r
173 * @return DOCUMENT ME!
\r
175 public NJTree getTree()
\r
183 * @param tree DOCUMENT ME!
\r
185 private void setViewStateFromTreeCanvas(TreeCanvas tree)
\r
187 // update view menu state from treeCanvas render state
\r
188 distanceMenu.setSelected(tree.showDistances);
\r
189 bootstrapMenu.setSelected(tree.showBootstrap);
\r
190 placeholdersMenu.setSelected(tree.markPlaceholders);
\r
196 * @param tree DOCUMENT ME!
\r
198 private void setTreeCanvasFromViewState(TreeCanvas tree)
\r
200 // update view menu state from treeCanvas render state
\r
201 tree.showDistances = distanceMenu.isSelected();
\r
202 tree.showBootstrap = bootstrapMenu.isSelected();
\r
203 tree.markPlaceholders = placeholdersMenu.isSelected();
\r
209 * @param e DOCUMENT ME!
\r
211 public void textbox_actionPerformed(ActionEvent e)
\r
213 CutAndPasteTransfer cap = new CutAndPasteTransfer();
\r
215 StringBuffer buffer = new StringBuffer();
\r
217 if (type.equals("AV"))
\r
219 buffer.append("Average distance tree using ");
\r
223 buffer.append("Neighbour joining tree using ");
\r
226 if (pwtype.equals("BL"))
\r
228 buffer.append("BLOSUM62");
\r
232 buffer.append("PID");
\r
235 Desktop.addInternalFrame(cap, buffer.toString(), 500, 100);
\r
237 jalview.io.NewickFile fout = new jalview.io.NewickFile(tree.getTopNode());
\r
238 cap.setText(fout.print(false, true));
\r
244 * @param e DOCUMENT ME!
\r
246 public void saveAsNewick_actionPerformed(ActionEvent e)
\r
248 JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.getProperty(
\r
249 "LAST_DIRECTORY"));
\r
250 chooser.setFileView(new JalviewFileView());
\r
251 chooser.setDialogTitle("Save tree as newick file");
\r
252 chooser.setToolTipText("Save");
\r
254 int value = chooser.showSaveDialog(null);
\r
256 if (value == JalviewFileChooser.APPROVE_OPTION)
\r
258 String choice = chooser.getSelectedFile().getPath();
\r
259 jalview.bin.Cache.setProperty("LAST_DIRECTORY",
\r
260 chooser.getSelectedFile().getParent());
\r
264 jalview.io.NewickFile fout = new jalview.io.NewickFile(tree.getTopNode());
\r
265 String output = fout.print(false, true); // distances only
\r
266 java.io.PrintWriter out = new java.io.PrintWriter(new java.io.FileWriter(
\r
268 out.println(output);
\r
271 catch (Exception ex)
\r
273 ex.printStackTrace();
\r
281 * @param e DOCUMENT ME!
\r
283 public void printMenu_actionPerformed(ActionEvent e)
\r
285 //Putting in a thread avoids Swing painting problems
\r
286 treeCanvas.startPrinting();
\r
292 * @param e DOCUMENT ME!
\r
294 public void fitToWindow_actionPerformed(ActionEvent e)
\r
296 treeCanvas.fitToWindow = fitToWindow.isSelected();
\r
303 * @param e DOCUMENT ME!
\r
305 public void fontSize_actionPerformed(ActionEvent e)
\r
307 if (treeCanvas == null)
\r
312 String size = fontSize.getText().substring(fontSize.getText().indexOf("-") +
\r
315 Object selection = JOptionPane.showInternalInputDialog(Desktop.desktop,
\r
316 "Select font size", "Font size", JOptionPane.QUESTION_MESSAGE,
\r
320 "1", "2", "4", "6", "8", "10", "12", "14", "16", "18", "20"
\r
321 }, "Font Size - " + size);
\r
323 if (selection != null)
\r
325 fontSize.setText("Font Size - " + selection);
\r
327 int i = Integer.parseInt(selection.toString());
\r
328 treeCanvas.setFontSize(i);
\r
331 scrollPane.setViewportView(treeCanvas);
\r
337 * @param e DOCUMENT ME!
\r
339 public void distanceMenu_actionPerformed(ActionEvent e)
\r
341 treeCanvas.setShowDistances(distanceMenu.isSelected());
\r
347 * @param e DOCUMENT ME!
\r
349 public void bootstrapMenu_actionPerformed(ActionEvent e)
\r
351 treeCanvas.setShowBootstrap(bootstrapMenu.isSelected());
\r
357 * @param e DOCUMENT ME!
\r
359 public void placeholdersMenu_actionPerformed(ActionEvent e)
\r
361 treeCanvas.setMarkPlaceholders(placeholdersMenu.isSelected());
\r
367 * @param e DOCUMENT ME!
\r
369 public void epsTree_actionPerformed(ActionEvent e)
\r
371 int width = treeCanvas.getWidth();
\r
372 int height = treeCanvas.getHeight();
\r
376 jalview.io.JalviewFileChooser chooser = new jalview.io.JalviewFileChooser(jalview.bin.Cache.getProperty(
\r
377 "LAST_DIRECTORY"), new String[] { "eps" },
\r
378 new String[] { "Encapsulated Postscript" },
\r
379 "Encapsulated Postscript");
\r
380 chooser.setFileView(new jalview.io.JalviewFileView());
\r
381 chooser.setDialogTitle("Create EPS file from tree");
\r
382 chooser.setToolTipText("Save");
\r
384 int value = chooser.showSaveDialog(this);
\r
386 if (value != jalview.io.JalviewFileChooser.APPROVE_OPTION)
\r
391 jalview.bin.Cache.setProperty("LAST_DIRECTORY",
\r
392 chooser.getSelectedFile().getParent());
\r
394 FileOutputStream out = new FileOutputStream(chooser.getSelectedFile());
\r
395 EpsGraphics2D pg = new EpsGraphics2D("Tree", out, 0, 0, width,
\r
398 treeCanvas.draw(pg, width, height);
\r
403 catch (Exception ex)
\r
405 ex.printStackTrace();
\r
412 * @param e DOCUMENT ME!
\r
414 public void pngTree_actionPerformed(ActionEvent e)
\r
416 int width = treeCanvas.getWidth();
\r
417 int height = treeCanvas.getHeight();
\r
421 jalview.io.JalviewFileChooser chooser = new jalview.io.JalviewFileChooser(jalview.bin.Cache.getProperty(
\r
422 "LAST_DIRECTORY"), new String[] { "png" },
\r
423 new String[] { "Portable network graphics" },
\r
424 "Portable network graphics");
\r
426 chooser.setFileView(new jalview.io.JalviewFileView());
\r
427 chooser.setDialogTitle("Create PNG image from tree");
\r
428 chooser.setToolTipText("Save");
\r
430 int value = chooser.showSaveDialog(this);
\r
432 if (value != jalview.io.JalviewFileChooser.APPROVE_OPTION)
\r
437 jalview.bin.Cache.setProperty("LAST_DIRECTORY",
\r
438 chooser.getSelectedFile().getParent());
\r
440 FileOutputStream out = new FileOutputStream(chooser.getSelectedFile());
\r
442 BufferedImage bi = new BufferedImage(width, height,
\r
443 BufferedImage.TYPE_INT_RGB);
\r
444 Graphics png = bi.getGraphics();
\r
446 treeCanvas.draw(png, width, height);
\r
448 ImageIO.write(bi, "png", out);
\r
451 catch (Exception ex)
\r
453 ex.printStackTrace();
\r