random notes on putting a Groovy interpreter into Jalview.
[jalview.git] / doc / AddingGroovySupport.html
1 <html>\r
2 <title>Adding Groovy Support to Jalview\r
3 </title>\r
4 <body>\r
5 <h1>\r
6 Adding Groovy Support to Jalview\r
7 </h1>\r
8 <p>\r
9 There is currently no scripting language \r
10 extension within Jalview, in part because a \r
11 scripting API has not been developed.\r
12 </p>\r
13 <p>It is, however, really easy to embed scripting\r
14 engines within Jalview. We haven't done it\r
15 with the Bean Scripting Framework, but the\r
16 code snippets below show you how to get going\r
17 with groovy.\r
18 </p>\r
19 <h2>Modifications</h2>\r
20 <p>\r
21 For each class below, add the following objects and methods to their definitions.\r
22 </p>\r
23 <ul><li>\r
24 jalview.jbgui.GDesktop\r
25 <pre>\r
26 ..\r
27 protected JMenuItem groovyShell = new JMenuItem();\r
28 ..\r
29 jbInit() {\r
30 ..\r
31 groovyShell.setText("Groovy Shell...");\r
32 groovyShell.addActionListener(new ActionListener() \r
33 {\r
34     public void actionPerformed(ActionEvent e) {\r
35         groovyShell_actionPerformed(e);\r
36     }  \r
37 });\r
38 ..\r
39 }\r
40 ..\r
41 protected void groovyShell_actionPerformed(ActionEvent e) \r
42 {\r
43 }\r
44 ..\r
45 </pre></li>\r
46 <li>jalview.gui.Desktop\r
47 <pre>\r
48 ..\r
49 /** \r
50  * Accessor method to quickly get all the AlignmentFrames\r
51  * loaded.  \r
52  */    \r
53 protected AlignFrame[] getAlignframes() {\r
54     JInternalFrame[] frames = Desktop.desktop.getAllFrames();\r
55 \r
56     if (frames == null)\r
57     {\r
58         return null;\r
59     }\r
60     Vector avp=new Vector();\r
61     try\r
62     {\r
63         //REVERSE ORDER\r
64         for (int i = frames.length - 1; i > -1; i--)\r
65         {\r
66             if (frames[i] instanceof AlignFrame)\r
67             {\r
68                 AlignFrame af = (AlignFrame) frames[i];\r
69                 avp.addElement(af);\r
70             }\r
71         }\r
72     }\r
73     catch (Exception ex)\r
74     {\r
75         ex.printStackTrace();\r
76     }\r
77     if (avp.size()==0)\r
78     {\r
79         return null;\r
80     }\r
81     AlignFrame afs[] = new AlignFrame[avp.size()];\r
82     for (int i=0,j=avp.size(); i&lt;j; i++) {\r
83         afs[i] = (AlignFrame) avp.elementAt(i);\r
84     }\r
85     avp.clear();\r
86     return afs;\r
87 }\r
88 \r
89 /**\r
90   * Add Groovy Support to Jalview\r
91   */\r
92 public void groovyShell_actionPerformed(ActionEvent e) {\r
93     Console gc = new Console();\r
94     gc.setVariable("Jalview", this);\r
95     gc.run();\r
96 }\r
97 ..\r
98 </pre>\r
99 </li>\r
100 </ul>\r
101 <p>\r
102 Finally, compile and run with the groovy-all-*.jar (get the jar \r
103 from the <em>embedded</em> directory within the <a \r
104 href="http://dist.codehaus.org/groovy/distributions"/>groovy distribution</a>).\r
105 Then, you should be able to open the Groovy shell \r
106 window from the Desktop's Tools menu. To check things are working,\r
107 try a simple test script :<br>\r
108 <pre>\r
109   \r
110   print Jalview.getAlignframes()[0].getTitle();\r
111 </pre>\r
112 Executing this will print the title of the first alignment loaded into Jalview.</p>\r
113 </hr>\r
114 <h2>TODO</h2>\r
115 <p>\r
116 Using Java class methods from Groovy is straightforward, but currently, there isn't a set of easy to use methods for the jalview objects. A Jalview Scripting API needs to be developed to make this easier.</p>\r
117 <h3>Making it easier</h3>\r
118 <p>jalview.bin.JalviewScript could be a top level jalview instance of a script execution thread, creating and maintaining the context for scripts operating on the jalview datamodel and interfacing with the Jalview GUI.\r
119 </p> \r
120 </body>\r
121 </html>\r
122 \r