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