4b605bf3379de18248c612a35c76724c51ed06d2
[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 import java.awt.print.*;\r
13 import java.awt.*;\r
14 \r
15 \r
16 \r
17 public class TreePanel extends GTreePanel\r
18 {\r
19   SequenceI[] seq;\r
20   String type;\r
21   String pwtype;\r
22   AlignViewport av;\r
23   int start;\r
24   int end;\r
25   TreeCanvas treeCanvas;\r
26   NJTree tree;\r
27 \r
28   FileProperties       fp;\r
29   PostscriptProperties pp;\r
30 \r
31   PrintWriter bw;\r
32   PrintStream ps;\r
33   boolean makeString = false;\r
34   StringBuffer out;\r
35 \r
36 \r
37 \r
38   public TreePanel(AlignViewport av, Vector seqVector, String type, String pwtype, int s, int e)\r
39   {\r
40     super();\r
41 \r
42     this.type = type;\r
43     this.pwtype = pwtype;\r
44 \r
45     start = s;\r
46     end = e;\r
47 \r
48     String longestName = "";\r
49     seq = new Sequence [seqVector.size()];\r
50     for (int i=0;i < seqVector.size();i++)\r
51     {\r
52       seq[i] = (Sequence) seqVector.elementAt(i);\r
53       if(seq[i].getName().length()>longestName.length())\r
54         longestName = seq[i].getName();\r
55     }\r
56 \r
57     tree = new NJTree(seq, type, pwtype, start, end);\r
58     av.setCurrentTree(tree);\r
59 \r
60     propertiesInit();\r
61 \r
62     treeCanvas = new TreeCanvas(av, tree, scrollPane, longestName);\r
63 \r
64     tree.reCount(tree.getTopNode());\r
65     tree.findHeight(tree.getTopNode());\r
66     scrollPane.setViewportView(treeCanvas);\r
67 \r
68   }\r
69 \r
70 \r
71 public void drawPostscript(PostscriptProperties pp) {\r
72   try {\r
73     int width = 0;\r
74     int height = 0;\r
75 \r
76     printout("%!\n");\r
77 \r
78     printout("/" + pp.font + " findfont\n");\r
79     printout(pp.fsize + " scalefont setfont\n");\r
80 \r
81     int offx = pp.xoffset;\r
82     int offy = pp.yoffset;\r
83 \r
84     if (pp.orientation == PostscriptProperties.PORTRAIT) {\r
85       width = PostscriptProperties.SHORTSIDE;\r
86       height = PostscriptProperties.LONGSIDE;\r
87     } else {\r
88       height = PostscriptProperties.SHORTSIDE;\r
89       width = PostscriptProperties.LONGSIDE;\r
90       printout(height + " 0 translate\n90 rotate\n");\r
91     }\r
92     float wscale = (float)(width*.8-offx*2)/tree.getMaxHeight();\r
93     SequenceNode top = tree.getTopNode();\r
94 \r
95     if (top.count == 0) {\r
96         top.count = ((SequenceNode)top.left()).count + ((SequenceNode)top.right()).count ;\r
97     }\r
98 \r
99     float chunk = (float)(height-offy*2)/(((SequenceNode)top).count+1);\r
100 \r
101     drawPostscriptNode(top,chunk,wscale,width,offx,offy);\r
102 \r
103     printout("showpage\n");\r
104   } catch (java.io.IOException e) {\r
105     System.out.println("Exception " + e);\r
106   }\r
107 }\r
108 \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
111     return;\r
112   }\r
113 \r
114   if (node.left() == null && node.right() == null) {\r
115     // Drawing leaf node\r
116 \r
117     float height = node.height;\r
118     float dist = node.dist;\r
119 \r
120     int xstart = (int)((height-dist)*scale) + offx;\r
121     int xend =   (int)(height*scale) + offx;\r
122 \r
123     int ypos = (int)(node.ycount * chunk) + offy;\r
124 \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
129 \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
133 \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
137     }\r
138     //g.drawString((String)node.element(),xend+20,ypos);\r
139     printout("(" + (((SequenceI)node.element()).getName()) + ") " + (xend+20) + " " + (ypos) + " moveto show\n");\r
140   } else {\r
141     drawPostscriptNode((SequenceNode)node.left(),chunk,scale,width,offx,offy);\r
142     drawPostscriptNode((SequenceNode)node.right(),chunk,scale,width,offx,offy);\r
143 \r
144 \r
145     float height = node.height;\r
146     float dist = node.dist;\r
147 \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
151 \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
162 \r
163     //      g.drawLine((int)(height*scale) + offx, ystart,\r
164     //           (int)(height*scale) + offx, yend);\r
165     printout\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
171     }\r
172   }\r
173 }\r
174 \r
175 public void printout(String s) throws IOException {\r
176   if (bw != null) {\r
177     bw.write(s);\r
178   }\r
179   if (ps != null) {\r
180     ps.print(s);\r
181   }\r
182   if (makeString == true) {\r
183     out.append(s);\r
184   }\r
185 }\r
186 \r
187 \r
188 public PostscriptProperties getPostscriptProperties() {\r
189   return pp;\r
190 }\r
191 \r
192 public FileProperties getFileProperties() {\r
193   return fp;\r
194 }\r
195 \r
196 public void setPostscriptProperties(PostscriptProperties pp) {\r
197   this.pp = pp;\r
198 }\r
199 \r
200 public void setFileProperties(FileProperties fp) {\r
201   this.fp = fp;\r
202 }\r
203 \r
204 public String getText(String format) {\r
205   return null;\r
206 }\r
207 \r
208 public void getPostscript(PrintWriter bw) {\r
209   this.bw = bw;\r
210   drawPostscript(pp);\r
211 \r
212 }\r
213 \r
214 public void getPostscript(PrintStream bw) {\r
215   this.ps = bw;\r
216   drawPostscript(pp);\r
217   bw.flush();\r
218 }\r
219 \r
220 public StringBuffer getPostscript() {\r
221   makeString = true;\r
222   out = new StringBuffer();\r
223   drawPostscript(pp);\r
224   return out;\r
225 }\r
226 \r
227 public void propertiesInit() {\r
228   this.pp = new PostscriptProperties();\r
229   this.fp = new FileProperties();\r
230 }\r
231 \r
232 \r
233   public void saveButton_actionPerformed(ActionEvent e)\r
234   {\r
235 \r
236     try{\r
237       JFileChooser chooser = new JFileChooser(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 == JFileChooser.APPROVE_OPTION)\r
242       {\r
243         String choice = chooser.getSelectedFile().getPath();\r
244         jalview.bin.Cache.setProperty("LAST_DIRECTORY", choice);\r
245 \r
246         bw = new PrintWriter(new FileWriter(choice));\r
247         getPostscript(bw);\r
248         bw.close();\r
249 \r
250       }\r
251 \r
252     }catch(Exception ex){ex.printStackTrace();}\r
253 \r
254   }\r
255 \r
256 \r
257   protected void saveMenu_actionPerformed(ActionEvent e)\r
258   {\r
259 \r
260   }\r
261 \r
262   protected void printMenu_actionPerformed(ActionEvent e)\r
263   {\r
264 \r
265     //Putting in a thread avoids Swing painting problems\r
266     treeCanvas.startPrinting();\r
267   }\r
268 \r
269   protected void fontSize_actionPerformed(ActionEvent e)\r
270   {\r
271     if( treeCanvas==null )\r
272      return;\r
273 \r
274    String size = fontSize.getText().substring( fontSize.getText().indexOf("-")+1);\r
275 \r
276    Object selection = JOptionPane.showInternalInputDialog(Desktop.desktop,\r
277                                "Select font size",\r
278                                "Font size",\r
279                                JOptionPane.QUESTION_MESSAGE,\r
280                                null, new String[]{"1","2","4","6","8","10","12","14","16","18","20"}\r
281                                ,"Font Size - "+size);\r
282    if(selection!=null)\r
283    {\r
284      fontSize.setText("Font Size - " + selection);\r
285 \r
286      int i = Integer.parseInt(selection.toString());\r
287      treeCanvas.setFontSize(i);\r
288    }\r
289     scrollPane.setViewportView(treeCanvas);\r
290 \r
291   }\r
292 \r
293   protected void distanceMenu_actionPerformed(ActionEvent e)\r
294   {\r
295     treeCanvas.setShowDistances(distanceMenu.isSelected());\r
296   }\r
297 \r
298   protected void bootstrapMenu_actionPerformed(ActionEvent e)\r
299   {\r
300     treeCanvas.setShowBootstrap(bootstrapMenu.isSelected());\r
301   }\r
302 \r
303 \r
304 }\r