3 import jalview.datamodel.*;
\r
4 import jalview.analysis.*;
\r
5 import jalview.jbgui.GTreePanel;
\r
7 import java.awt.event.*;
\r
10 import jalview.util.*;
\r
11 import javax.swing.*;
\r
14 public class TreePanel extends GTreePanel
\r
22 TreeCanvas treeCanvas;
\r
26 PostscriptProperties pp;
\r
30 boolean makeString = false;
\r
35 public TreePanel(AlignViewport av, Vector seqVector, String type, String pwtype, int s, int e)
\r
40 this.pwtype = pwtype;
\r
45 String longestName = "";
\r
46 seq = new Sequence [seqVector.size()];
\r
47 for (int i=0;i < seqVector.size();i++)
\r
49 seq[i] = (Sequence) seqVector.elementAt(i);
\r
50 if(seq[i].getName().length()>longestName.length())
\r
51 longestName = seq[i].getName();
\r
54 tree = new NJTree(seq, type, pwtype, start, end);
\r
55 av.setCurrentTree(tree);
\r
59 treeCanvas = new TreeCanvas(av, tree, scrollPane, longestName);
\r
61 tree.reCount(tree.getTopNode());
\r
62 tree.findHeight(tree.getTopNode());
\r
63 scrollPane.setViewportView(treeCanvas);
\r
68 public void drawPostscript(PostscriptProperties pp) {
\r
75 printout("/" + pp.font + " findfont\n");
\r
76 printout(pp.fsize + " scalefont setfont\n");
\r
78 int offx = pp.xoffset;
\r
79 int offy = pp.yoffset;
\r
81 if (pp.orientation == PostscriptProperties.PORTRAIT) {
\r
82 width = PostscriptProperties.SHORTSIDE;
\r
83 height = PostscriptProperties.LONGSIDE;
\r
85 height = PostscriptProperties.SHORTSIDE;
\r
86 width = PostscriptProperties.LONGSIDE;
\r
87 printout(height + " 0 translate\n90 rotate\n");
\r
89 float wscale = (float)(width*.8-offx*2)/tree.getMaxHeight();
\r
90 SequenceNode top = tree.getTopNode();
\r
92 if (top.count == 0) {
\r
93 top.count = ((SequenceNode)top.left()).count + ((SequenceNode)top.right()).count ;
\r
96 float chunk = (float)(height-offy*2)/(((SequenceNode)top).count+1);
\r
98 drawPostscriptNode(top,chunk,wscale,width,offx,offy);
\r
100 printout("showpage\n");
\r
101 } catch (java.io.IOException e) {
\r
102 System.out.println("Exception " + e);
\r
106 public void drawPostscriptNode(SequenceNode node, float chunk, float scale, int width, int offx, int offy) throws java.io.IOException {
\r
107 if (node == null) {
\r
111 if (node.left() == null && node.right() == null) {
\r
112 // Drawing leaf node
\r
114 float height = node.height;
\r
115 float dist = node.dist;
\r
117 int xstart = (int)((height-dist)*scale) + offx;
\r
118 int xend = (int)(height*scale) + offx;
\r
120 int ypos = (int)(node.ycount * chunk) + offy;
\r
122 // g.setColor(Color.black);
\r
123 printout("\n" + new Format("%5.3f").form((double)node.color.getRed()/255) + " " +
\r
124 new Format("%5.3f").form((double)node.color.getGreen()/255) + " " +
\r
125 new Format("%5.3f").form((double)node.color.getBlue()/255) + " setrgbcolor\n");
\r
127 // Draw horizontal line
\r
128 // g.drawLine(xstart,ypos,xend,ypos);
\r
129 printout(xstart + " " + ypos + " moveto " + xend + " " + ypos + " lineto stroke \n");
\r
131 if (treeCanvas.showDistances && node.dist > 0) {
\r
132 // g.drawString(new Format("%5.2f").form(node.dist),xstart,ypos - 5);
\r
133 printout("(" + new Format("%5.2f").form(node.dist) + ") " + xstart + " " + (ypos+5) + " moveto show\n");
\r
135 //g.drawString((String)node.element(),xend+20,ypos);
\r
136 printout("(" + (((SequenceI)node.element()).getName()) + ") " + (xend+20) + " " + (ypos) + " moveto show\n");
\r
138 drawPostscriptNode((SequenceNode)node.left(),chunk,scale,width,offx,offy);
\r
139 drawPostscriptNode((SequenceNode)node.right(),chunk,scale,width,offx,offy);
\r
142 float height = node.height;
\r
143 float dist = node.dist;
\r
145 int xstart = (int)((height-dist)*scale) + offx;
\r
146 int xend = (int)(height*scale) + offx;
\r
147 int ypos = (int)(node.ycount * chunk) + offy;
\r
149 printout("\n" + new Format("%5.3f").form((double)node.color.getRed()/255) + " " +
\r
150 new Format("%5.3f").form((double)node.color.getGreen()/255) + " " +
\r
151 new Format("%5.3f").form((double)node.color.getBlue()/255) + " setrgbcolor\n");
\r
152 // g.setColor(Color.black);
\r
153 // bw.append("\nblack setrgbcolor\n");
\r
154 // Draw horizontal line
\r
155 // g.drawLine(xstart,ypos,xend,ypos);
\r
156 printout(xstart + " " + ypos + " moveto " + xend + " " + ypos + " lineto stroke\n");
\r
157 int ystart = (int)(((SequenceNode)node.left()).ycount * chunk) + offy;
\r
158 int yend = (int)(((SequenceNode)node.right()).ycount * chunk) + offy;
\r
160 // g.drawLine((int)(height*scale) + offx, ystart,
\r
161 // (int)(height*scale) + offx, yend);
\r
163 (((int)(height*scale) + offx) + " " + ystart + " moveto " + ((int)(height*scale) + offx) + " " +
\r
164 yend + " lineto stroke\n");
\r
165 if (treeCanvas.showDistances && node.dist > 0) {
\r
166 // g.drawString(new Format("%5.2f").form(node.dist),xstart,ypos - 5);
\r
167 printout("(" +new Format("%5.2f").form(node.dist) + ") " + (xstart) + " " + (ypos+5) + " moveto show\n");
\r
172 public void printout(String s) throws IOException {
\r
179 if (makeString == true) {
\r
185 public PostscriptProperties getPostscriptProperties() {
\r
189 public FileProperties getFileProperties() {
\r
193 public void setPostscriptProperties(PostscriptProperties pp) {
\r
197 public void setFileProperties(FileProperties fp) {
\r
201 public String getText(String format) {
\r
205 public void getPostscript(PrintWriter bw) {
\r
207 drawPostscript(pp);
\r
211 public void getPostscript(PrintStream bw) {
\r
213 drawPostscript(pp);
\r
217 public StringBuffer getPostscript() {
\r
219 out = new StringBuffer();
\r
220 drawPostscript(pp);
\r
224 public void propertiesInit() {
\r
225 this.pp = new PostscriptProperties();
\r
226 this.fp = new FileProperties();
\r
230 public void saveButton_actionPerformed(ActionEvent e)
\r
234 JFileChooser chooser = new JFileChooser(jalview.bin.Cache.LAST_DIRECTORY);
\r
235 chooser.setDialogTitle("Save Tree as postscript file");
\r
236 chooser.setToolTipText("Save");
\r
237 int value = chooser.showSaveDialog(this);
\r
238 if (value == JFileChooser.APPROVE_OPTION)
\r
240 String choice = chooser.getSelectedFile().getPath();
\r
241 jalview.bin.Cache.LAST_DIRECTORY = choice;
\r
243 bw = new PrintWriter(new FileWriter(choice));
\r
249 }catch(Exception ex){ex.printStackTrace();}
\r
253 public void showDistanceCheckbox_actionPerformed(ActionEvent e)
\r
255 treeCanvas.setShowDistances(showDistanceCheckbox.isSelected());
\r
258 public void fontSizeCombobox_actionPerformed(ActionEvent e)
\r
260 if( treeCanvas==null )
\r
263 int size = Integer.parseInt( fontSizeCombobox.getSelectedItem().toString() );
\r
264 treeCanvas.setFontSize( size );
\r
266 scrollPane.setViewportView(treeCanvas);
\r
269 public void bootStrapCheckBox_actionPerformed(ActionEvent e)
\r
271 treeCanvas.setShowBootstrap(bootStrapCheckBox.isSelected());
\r