From 88bc025708f1b7881bababb92d14351760e0f9da Mon Sep 17 00:00:00 2001
From: jprocter
There is currently no scripting language
extension within Jalview, in part because a
-scripting API has not been developed.
+scripting API has not yet been developed.
It is, however, really easy to embed scripting
-engines within Jalview. We haven't done it
-with the Bean Scripting Framework, but the
-code snippets below show you how to get going
-with groovy.
+engines like groovy. If groovy is detected on the
+classpath, a new menu entry on the Desktop's Tools
+menu will open the GroovyShell.
-For each class below, add the following objects and methods to their definitions.
- Here are some scripts to get you started:
-Finally, compile and run with the groovy-all-*.jar (get the jar
-from the embedded directory within the
+ The is obtained from the embedded directory within the groovy distributionModifications
-
+
-..
-protected JMenuItem groovyShell = new JMenuItem();
-..
-jbInit() {
-..
-groovyShell.setText("Groovy Shell...");
-groovyShell.addActionListener(new ActionListener()
-{
- public void actionPerformed(ActionEvent e) {
- groovyShell_actionPerformed(e);
- }
-});
-..
-}
-..
-protected void groovyShell_actionPerformed(ActionEvent e)
-{
-}
-..
-
-..
-/**
- * Accessor method to quickly get all the AlignmentFrames
- * loaded.
- */
-protected AlignFrame[] getAlignframes() {
- JInternalFrame[] frames = Desktop.desktop.getAllFrames();
-
- if (frames == null)
- {
- return null;
- }
- Vector avp=new Vector();
- try
- {
- //REVERSE ORDER
- for (int i = frames.length - 1; i > -1; i--)
- {
- if (frames[i] instanceof AlignFrame)
- {
- AlignFrame af = (AlignFrame) frames[i];
- avp.addElement(af);
- }
- }
- }
- catch (Exception ex)
- {
- ex.printStackTrace();
- }
- if (avp.size()==0)
- {
- return null;
- }
- AlignFrame afs[] = new AlignFrame[avp.size()];
- for (int i=0,j=avp.size(); i<j; i++) {
- afs[i] = (AlignFrame) avp.elementAt(i);
- }
- avp.clear();
- return afs;
-}
-
-/**
- * Add Groovy Support to Jalview
- */
-public void groovyShell_actionPerformed(ActionEvent e) {
- Console gc = new Console();
- gc.setVariable("Jalview", this);
- gc.run();
-}
-..
+def alf = Jalview.getAlignframes();
+print alf[0].getTitle();
+def alignment = alf[0].viewport.alignment;
+def seq = alignment.getSequenceAt(0);
Getting Groovy...
-
-
- print Jalview.getAlignframes()[0].getTitle();
-
-Executing this will print the title of the first alignment loaded into Jalview.
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.
diff --git a/src/jalview/bin/Cache.java b/src/jalview/bin/Cache.java index 72fec6c..195ad29 100755 --- a/src/jalview/bin/Cache.java +++ b/src/jalview/bin/Cache.java @@ -334,5 +334,42 @@ public class Cache } return (vamsasJarsArePresent > 0); } + /** + * internal vamsas class discovery state + */ + private static int groovyJarsArePresent = -1; + /** + * Searches for vamsas client classes on class path. + * @return true if vamsas client is present on classpath + */ + public static boolean groovyJarsPresent() + { + if (groovyJarsArePresent == -1) + { + try + { + if (Cache.class.getClassLoader().loadClass( + "groovy.lang.GroovyObject") != null) + { + jalview.bin.Cache.log.debug( + "Found Groovy (groovy.lang.GroovyObject can be loaded)"); + groovyJarsArePresent = 1; + Logger lgclient = Logger.getLogger("groovy"); + lgclient.setLevel(Level.toLevel(Cache.getDefault("logs.Groovy.Level", + Level.INFO.toString()))); + + lgclient.addAppender(log.getAppender("JalviewLogger")); + // Tell the user that debug is enabled + lgclient.debug("Jalview Groovy Client Debugging Output Follows."); + } + } + catch (Exception e) + { + groovyJarsArePresent = 0; + jalview.bin.Cache.log.debug("Groovy Classes are not present"); + } + } + return (groovyJarsArePresent > 0); + } } diff --git a/src/jalview/gui/Desktop.java b/src/jalview/gui/Desktop.java index 0304ea1..0310f2a 100755 --- a/src/jalview/gui/Desktop.java +++ b/src/jalview/gui/Desktop.java @@ -23,6 +23,7 @@ import java.awt.*; import java.awt.datatransfer.*; import java.awt.dnd.*; import java.awt.event.*; +import java.lang.reflect.Constructor; import java.util.*; import javax.swing.*; @@ -63,6 +64,7 @@ public class Desktop { instance = this; doVamsasClientCheck(); + doGroovyCheck(); Image image = null; @@ -1104,5 +1106,87 @@ public class Desktop } } }*/ + protected JMenuItem groovyShell; + public void doGroovyCheck() { + if (jalview.bin.Cache.groovyJarsPresent()) + { + groovyShell = new JMenuItem(); + groovyShell.setText("Groovy Shell..."); + groovyShell.addActionListener(new ActionListener() + { + public void actionPerformed(ActionEvent e) { + groovyShell_actionPerformed(e); + } + }); + toolsMenu.add(groovyShell); + groovyShell.setVisible(true); + } + } + /** + * Accessor method to quickly get all the AlignmentFrames + * loaded. + */ + protected AlignFrame[] getAlignframes() { + JInternalFrame[] frames = Desktop.desktop.getAllFrames(); + + if (frames == null) + { + return null; + } + Vector avp=new Vector(); + try + { + //REVERSE ORDER + for (int i = frames.length - 1; i > -1; i--) + { + if (frames[i] instanceof AlignFrame) + { + AlignFrame af = (AlignFrame) frames[i]; + avp.addElement(af); + } + } + } + catch (Exception ex) + { + ex.printStackTrace(); + } + if (avp.size()==0) + { + return null; + } + AlignFrame afs[] = new AlignFrame[avp.size()]; + for (int i=0,j=avp.size(); i