Jalview Imported Sources
[jalview.git] / src / jalview / gui / TreePanel.java
1 package jalview.gui;\r
2 \r
3 import jalview.datamodel.*;\r
4 import jalview.analysis.*;\r
5 import jalview.jbgui.GTreePanel;\r
6 import jalview.io.*;\r
7 import java.awt.event.*;\r
8 import java.util.*;\r
9 import java.io.*;\r
10 import jalview.util.*;\r
11 import javax.swing.*;\r
12 \r
13 \r
14 public class TreePanel extends GTreePanel\r
15 {\r
16   SequenceI[] seq;\r
17   String type;\r
18   String pwtype;\r
19   AlignViewport av;\r
20   int start;\r
21   int end;\r
22   TreeCanvas treeCanvas;\r
23   NJTree tree;\r
24 \r
25   FileProperties       fp;\r
26   PostscriptProperties pp;\r
27 \r
28   PrintWriter bw;\r
29   PrintStream ps;\r
30   boolean makeString = false;\r
31   StringBuffer out;\r
32 \r
33 \r
34 \r
35   public TreePanel(AlignViewport av, Vector seqVector, String type, String pwtype, int s, int e)\r
36   {\r
37     super();\r
38 \r
39     this.type = type;\r
40     this.pwtype = pwtype;\r
41 \r
42     start = s;\r
43     end = e;\r
44 \r
45     String longestName = "";\r
46     seq = new Sequence [seqVector.size()];\r
47     for (int i=0;i < seqVector.size();i++)\r
48     {\r
49       seq[i] = (Sequence) seqVector.elementAt(i);\r
50       if(seq[i].getName().length()>longestName.length())\r
51         longestName = seq[i].getName();\r
52     }\r
53 \r
54     tree = new NJTree(seq, type, pwtype, start, end);\r
55     av.setCurrentTree(tree);\r
56 \r
57     propertiesInit();\r
58 \r
59     treeCanvas = new TreeCanvas(av, tree, scrollPane, longestName);\r
60 \r
61     tree.reCount(tree.getTopNode());\r
62     tree.findHeight(tree.getTopNode());\r
63     scrollPane.setViewportView(treeCanvas);\r
64 \r
65   }\r
66 \r
67 \r
68 public void drawPostscript(PostscriptProperties pp) {\r
69   try {\r
70     int width = 0;\r
71     int height = 0;\r
72 \r
73     printout("%!\n");\r
74 \r
75     printout("/" + pp.font + " findfont\n");\r
76     printout(pp.fsize + " scalefont setfont\n");\r
77 \r
78     int offx = pp.xoffset;\r
79     int offy = pp.yoffset;\r
80 \r
81     if (pp.orientation == PostscriptProperties.PORTRAIT) {\r
82       width = PostscriptProperties.SHORTSIDE;\r
83       height = PostscriptProperties.LONGSIDE;\r
84     } else {\r
85       height = PostscriptProperties.SHORTSIDE;\r
86       width = PostscriptProperties.LONGSIDE;\r
87       printout(height + " 0 translate\n90 rotate\n");\r
88     }\r
89     float wscale = (float)(width*.8-offx*2)/tree.getMaxHeight();\r
90     SequenceNode top = tree.getTopNode();\r
91 \r
92     if (top.count == 0) {\r
93         top.count = ((SequenceNode)top.left()).count + ((SequenceNode)top.right()).count ;\r
94     }\r
95 \r
96     float chunk = (float)(height-offy*2)/(((SequenceNode)top).count+1);\r
97 \r
98     drawPostscriptNode(top,chunk,wscale,width,offx,offy);\r
99 \r
100     printout("showpage\n");\r
101   } catch (java.io.IOException e) {\r
102     System.out.println("Exception " + e);\r
103   }\r
104 }\r
105 \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
108     return;\r
109   }\r
110 \r
111   if (node.left() == null && node.right() == null) {\r
112     // Drawing leaf node\r
113 \r
114     float height = node.height;\r
115     float dist = node.dist;\r
116 \r
117     int xstart = (int)((height-dist)*scale) + offx;\r
118     int xend =   (int)(height*scale) + offx;\r
119 \r
120     int ypos = (int)(node.ycount * chunk) + offy;\r
121 \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
126 \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
130 \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
134     }\r
135     //g.drawString((String)node.element(),xend+20,ypos);\r
136     printout("(" + (((SequenceI)node.element()).getName()) + ") " + (xend+20) + " " + (ypos) + " moveto show\n");\r
137   } else {\r
138     drawPostscriptNode((SequenceNode)node.left(),chunk,scale,width,offx,offy);\r
139     drawPostscriptNode((SequenceNode)node.right(),chunk,scale,width,offx,offy);\r
140 \r
141 \r
142     float height = node.height;\r
143     float dist = node.dist;\r
144 \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
148 \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
159 \r
160     //      g.drawLine((int)(height*scale) + offx, ystart,\r
161     //           (int)(height*scale) + offx, yend);\r
162     printout\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
168     }\r
169   }\r
170 }\r
171 \r
172 public void printout(String s) throws IOException {\r
173   if (bw != null) {\r
174     bw.write(s);\r
175   }\r
176   if (ps != null) {\r
177     ps.print(s);\r
178   }\r
179   if (makeString == true) {\r
180     out.append(s);\r
181   }\r
182 }\r
183 \r
184 \r
185 public PostscriptProperties getPostscriptProperties() {\r
186   return pp;\r
187 }\r
188 \r
189 public FileProperties getFileProperties() {\r
190   return fp;\r
191 }\r
192 \r
193 public void setPostscriptProperties(PostscriptProperties pp) {\r
194   this.pp = pp;\r
195 }\r
196 \r
197 public void setFileProperties(FileProperties fp) {\r
198   this.fp = fp;\r
199 }\r
200 \r
201 public String getText(String format) {\r
202   return null;\r
203 }\r
204 \r
205 public void getPostscript(PrintWriter bw) {\r
206   this.bw = bw;\r
207   drawPostscript(pp);\r
208 \r
209 }\r
210 \r
211 public void getPostscript(PrintStream bw) {\r
212   this.ps = bw;\r
213   drawPostscript(pp);\r
214   bw.flush();\r
215 }\r
216 \r
217 public StringBuffer getPostscript() {\r
218   makeString = true;\r
219   out = new StringBuffer();\r
220   drawPostscript(pp);\r
221   return out;\r
222 }\r
223 \r
224 public void propertiesInit() {\r
225   this.pp = new PostscriptProperties();\r
226   this.fp = new FileProperties();\r
227 }\r
228 \r
229 \r
230   public void saveButton_actionPerformed(ActionEvent e)\r
231   {\r
232 \r
233     try{\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
239       {\r
240         String choice = chooser.getSelectedFile().getPath();\r
241         jalview.bin.Cache.LAST_DIRECTORY = choice;\r
242 \r
243         bw = new PrintWriter(new FileWriter(choice));\r
244         getPostscript(bw);\r
245         bw.close();\r
246 \r
247       }\r
248 \r
249     }catch(Exception ex){ex.printStackTrace();}\r
250 \r
251   }\r
252 \r
253   public void showDistanceCheckbox_actionPerformed(ActionEvent e)\r
254   {\r
255     treeCanvas.setShowDistances(showDistanceCheckbox.isSelected());\r
256   }\r
257 \r
258   public void fontSizeCombobox_actionPerformed(ActionEvent e)\r
259   {\r
260     if( treeCanvas==null )\r
261      return;\r
262 \r
263     int size = Integer.parseInt( fontSizeCombobox.getSelectedItem().toString() );\r
264         treeCanvas.setFontSize( size );\r
265 \r
266     scrollPane.setViewportView(treeCanvas);\r
267   }\r
268 \r
269   public void bootStrapCheckBox_actionPerformed(ActionEvent e)\r
270   {\r
271     treeCanvas.setShowBootstrap(bootStrapCheckBox.isSelected());\r
272   }\r
273 \r
274 }\r