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
12 import java.awt.print.*;
\r
17 public class TreePanel extends GTreePanel
\r
25 TreeCanvas treeCanvas;
\r
29 PostscriptProperties pp;
\r
33 boolean makeString = false;
\r
38 public TreePanel(AlignViewport av, Vector seqVector, String type, String pwtype, int s, int e)
\r
43 this.pwtype = pwtype;
\r
48 String longestName = "";
\r
49 seq = new Sequence [seqVector.size()];
\r
50 for (int i=0;i < seqVector.size();i++)
\r
52 seq[i] = (Sequence) seqVector.elementAt(i);
\r
53 if(seq[i].getName().length()>longestName.length())
\r
54 longestName = seq[i].getName();
\r
57 tree = new NJTree(seq, type, pwtype, start, end);
\r
58 av.setCurrentTree(tree);
\r
62 treeCanvas = new TreeCanvas(av, tree, scrollPane, longestName);
\r
64 tree.reCount(tree.getTopNode());
\r
65 tree.findHeight(tree.getTopNode());
\r
66 scrollPane.setViewportView(treeCanvas);
\r
71 public void drawPostscript(PostscriptProperties pp) {
\r
78 printout("/" + pp.font + " findfont\n");
\r
79 printout(pp.fsize + " scalefont setfont\n");
\r
81 int offx = pp.xoffset;
\r
82 int offy = pp.yoffset;
\r
84 if (pp.orientation == PostscriptProperties.PORTRAIT) {
\r
85 width = PostscriptProperties.SHORTSIDE;
\r
86 height = PostscriptProperties.LONGSIDE;
\r
88 height = PostscriptProperties.SHORTSIDE;
\r
89 width = PostscriptProperties.LONGSIDE;
\r
90 printout(height + " 0 translate\n90 rotate\n");
\r
92 float wscale = (float)(width*.8-offx*2)/tree.getMaxHeight();
\r
93 SequenceNode top = tree.getTopNode();
\r
95 if (top.count == 0) {
\r
96 top.count = ((SequenceNode)top.left()).count + ((SequenceNode)top.right()).count ;
\r
99 float chunk = (float)(height-offy*2)/(((SequenceNode)top).count+1);
\r
101 drawPostscriptNode(top,chunk,wscale,width,offx,offy);
\r
103 printout("showpage\n");
\r
104 } catch (java.io.IOException e) {
\r
105 System.out.println("Exception " + e);
\r
109 public void drawPostscriptNode(SequenceNode node, float chunk, float scale, int width, int offx, int offy) throws java.io.IOException {
\r
110 if (node == null) {
\r
114 if (node.left() == null && node.right() == null) {
\r
115 // Drawing leaf node
\r
117 float height = node.height;
\r
118 float dist = node.dist;
\r
120 int xstart = (int)((height-dist)*scale) + offx;
\r
121 int xend = (int)(height*scale) + offx;
\r
123 int ypos = (int)(node.ycount * chunk) + offy;
\r
125 // g.setColor(Color.black);
\r
126 printout("\n" + new Format("%5.3f").form((double)node.color.getRed()/255) + " " +
\r
127 new Format("%5.3f").form((double)node.color.getGreen()/255) + " " +
\r
128 new Format("%5.3f").form((double)node.color.getBlue()/255) + " setrgbcolor\n");
\r
130 // Draw horizontal line
\r
131 // g.drawLine(xstart,ypos,xend,ypos);
\r
132 printout(xstart + " " + ypos + " moveto " + xend + " " + ypos + " lineto stroke \n");
\r
134 if (treeCanvas.showDistances && node.dist > 0) {
\r
135 // g.drawString(new Format("%5.2f").form(node.dist),xstart,ypos - 5);
\r
136 printout("(" + new Format("%5.2f").form(node.dist) + ") " + xstart + " " + (ypos+5) + " moveto show\n");
\r
138 //g.drawString((String)node.element(),xend+20,ypos);
\r
139 printout("(" + (((SequenceI)node.element()).getName()) + ") " + (xend+20) + " " + (ypos) + " moveto show\n");
\r
141 drawPostscriptNode((SequenceNode)node.left(),chunk,scale,width,offx,offy);
\r
142 drawPostscriptNode((SequenceNode)node.right(),chunk,scale,width,offx,offy);
\r
145 float height = node.height;
\r
146 float dist = node.dist;
\r
148 int xstart = (int)((height-dist)*scale) + offx;
\r
149 int xend = (int)(height*scale) + offx;
\r
150 int ypos = (int)(node.ycount * chunk) + offy;
\r
152 printout("\n" + new Format("%5.3f").form((double)node.color.getRed()/255) + " " +
\r
153 new Format("%5.3f").form((double)node.color.getGreen()/255) + " " +
\r
154 new Format("%5.3f").form((double)node.color.getBlue()/255) + " setrgbcolor\n");
\r
155 // g.setColor(Color.black);
\r
156 // bw.append("\nblack setrgbcolor\n");
\r
157 // Draw horizontal line
\r
158 // g.drawLine(xstart,ypos,xend,ypos);
\r
159 printout(xstart + " " + ypos + " moveto " + xend + " " + ypos + " lineto stroke\n");
\r
160 int ystart = (int)(((SequenceNode)node.left()).ycount * chunk) + offy;
\r
161 int yend = (int)(((SequenceNode)node.right()).ycount * chunk) + offy;
\r
163 // g.drawLine((int)(height*scale) + offx, ystart,
\r
164 // (int)(height*scale) + offx, yend);
\r
166 (((int)(height*scale) + offx) + " " + ystart + " moveto " + ((int)(height*scale) + offx) + " " +
\r
167 yend + " lineto stroke\n");
\r
168 if (treeCanvas.showDistances && node.dist > 0) {
\r
169 // g.drawString(new Format("%5.2f").form(node.dist),xstart,ypos - 5);
\r
170 printout("(" +new Format("%5.2f").form(node.dist) + ") " + (xstart) + " " + (ypos+5) + " moveto show\n");
\r
175 public void printout(String s) throws IOException {
\r
182 if (makeString == true) {
\r
188 public PostscriptProperties getPostscriptProperties() {
\r
192 public FileProperties getFileProperties() {
\r
196 public void setPostscriptProperties(PostscriptProperties pp) {
\r
200 public void setFileProperties(FileProperties fp) {
\r
204 public String getText(String format) {
\r
208 public void getPostscript(PrintWriter bw) {
\r
210 drawPostscript(pp);
\r
214 public void getPostscript(PrintStream bw) {
\r
216 drawPostscript(pp);
\r
220 public StringBuffer getPostscript() {
\r
222 out = new StringBuffer();
\r
223 drawPostscript(pp);
\r
227 public void propertiesInit() {
\r
228 this.pp = new PostscriptProperties();
\r
229 this.fp = new FileProperties();
\r
233 public void saveButton_actionPerformed(ActionEvent e)
\r
237 JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.getProperty("LAST_DIRECTORY"));
\r
238 chooser.setDialogTitle("Save Tree as postscript file");
\r
239 chooser.setToolTipText("Save");
\r
240 int value = chooser.showSaveDialog(this);
\r
241 if (value == JalviewFileChooser.APPROVE_OPTION)
\r
243 String choice = chooser.getSelectedFile().getPath();
\r
244 jalview.bin.Cache.setProperty("LAST_DIRECTORY", choice);
\r
246 bw = new PrintWriter(new FileWriter(choice));
\r
252 }catch(Exception ex){ex.printStackTrace();}
\r
257 protected void saveMenu_actionPerformed(ActionEvent e)
\r
262 protected void printMenu_actionPerformed(ActionEvent e)
\r
265 //Putting in a thread avoids Swing painting problems
\r
266 treeCanvas.startPrinting();
\r
269 protected void fitToWindow_actionPerformed(ActionEvent e)
\r
271 treeCanvas.fitToWindow = fitToWindow.isSelected();
\r
276 protected void fontSize_actionPerformed(ActionEvent e)
\r
278 if( treeCanvas==null )
\r
281 String size = fontSize.getText().substring( fontSize.getText().indexOf("-")+1);
\r
283 Object selection = JOptionPane.showInternalInputDialog(Desktop.desktop,
\r
284 "Select font size",
\r
286 JOptionPane.QUESTION_MESSAGE,
\r
287 null, new String[]{"1","2","4","6","8","10","12","14","16","18","20"}
\r
288 ,"Font Size - "+size);
\r
289 if(selection!=null)
\r
291 fontSize.setText("Font Size - " + selection);
\r
293 int i = Integer.parseInt(selection.toString());
\r
294 treeCanvas.setFontSize(i);
\r
296 scrollPane.setViewportView(treeCanvas);
\r
300 protected void distanceMenu_actionPerformed(ActionEvent e)
\r
302 treeCanvas.setShowDistances(distanceMenu.isSelected());
\r
305 protected void bootstrapMenu_actionPerformed(ActionEvent e)
\r
307 treeCanvas.setShowBootstrap(bootstrapMenu.isSelected());
\r