From: gmungoc Date: Fri, 22 Apr 2016 15:16:55 +0000 (+0100) Subject: JAL-1812 JAL-2068 add Run Groovy Script to AlignFrame Calculate menu X-Git-Tag: Release_2_10_0~249^2~11 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=d953e0e3aa04e00c1d1b8fd94e05c6ba744f8416;p=jalview.git JAL-1812 JAL-2068 add Run Groovy Script to AlignFrame Calculate menu --- diff --git a/resources/lang/Messages.properties b/resources/lang/Messages.properties index 05505a4..bc6def9 100644 --- a/resources/lang/Messages.properties +++ b/resources/lang/Messages.properties @@ -1292,3 +1292,6 @@ status.cancelled_image_export_operation = Cancelled {0} export operation. info.error_creating_file = Error creating {0} file. exception.outofmemory_loading_mmcif_file = Out of memory loading mmCIF File info.error_creating_file = Error creating {0} file. +label.run_groovy = Run Groovy console script +label.run_groovy_tip = Run the script in the Groovy console over this alignment +label.couldnt_run_groovy_script = Failed to run Groovy script diff --git a/src/jalview/gui/AlignFrame.java b/src/jalview/gui/AlignFrame.java index 28ff0a1..e439696 100644 --- a/src/jalview/gui/AlignFrame.java +++ b/src/jalview/gui/AlignFrame.java @@ -119,6 +119,8 @@ import java.awt.dnd.DropTargetEvent; import java.awt.dnd.DropTargetListener; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.awt.event.FocusAdapter; +import java.awt.event.FocusEvent; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.awt.event.KeyAdapter; @@ -129,6 +131,7 @@ import java.awt.print.PageFormat; import java.awt.print.PrinterJob; import java.beans.PropertyChangeEvent; import java.io.File; +import java.lang.reflect.Method; import java.net.URL; import java.util.ArrayList; import java.util.Arrays; @@ -466,6 +469,14 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener, { formatMenu.add(vsel); } + addFocusListener(new FocusAdapter() + { + @Override + public void focusGained(FocusEvent e) + { + Desktop.setCurrentAlignFrame(AlignFrame.this); + } + }); } @@ -911,10 +922,21 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener, .setSelected(av.getGlobalColourScheme() instanceof jalview.schemes.RNAHelicesColour); showProducts.setEnabled(canShowProducts()); + setGroovyEnabled(Desktop.instance.getGroovyConsole() != null); updateEditMenuBar(); } + /** + * Set the enabled state of the 'Run Groovy' option in the Calculate menu + * + * @param b + */ + public void setGroovyEnabled(boolean b) + { + runGroovy.setEnabled(b); + } + private IProgressIndicator progressBar; /* @@ -6090,6 +6112,45 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener, return; } } + + /** + * Try to run a script in the Groovy console, having first ensured that this + * AlignFrame is set as currentAlignFrame in Desktop, to allow the script to + * be targeted at this alignment. + */ + @Override + protected void runGroovy_actionPerformed() + { + Desktop.setCurrentAlignFrame(this); + Object console = Desktop.instance.getGroovyConsole(); + if (console != null) + { + /* + * use reflection here to avoid compile-time dependency + * on Groovy libraries + */ + try + { + Class gcClass = getClass().getClassLoader().loadClass( + "groovy.ui.Console"); + Method runScript = gcClass.getMethod("runScript"); + runScript.invoke(console); + } catch (Exception ex) + { + System.err.println((ex.toString())); + JOptionPane + .showInternalMessageDialog(Desktop.desktop, MessageManager + .getString("label.couldnt_run_groovy_script"), + MessageManager + .getString("label.groovy_support_failed"), + JOptionPane.ERROR_MESSAGE); + } + } + else + { + System.err.println("Can't run Groovy script as console not found"); + } + } } class PrintThread extends Thread diff --git a/src/jalview/gui/Desktop.java b/src/jalview/gui/Desktop.java index 27f60ea..ba7f255 100644 --- a/src/jalview/gui/Desktop.java +++ b/src/jalview/gui/Desktop.java @@ -2507,9 +2507,21 @@ public class Desktop extends jalview.jbgui.GDesktop implements java.lang.reflect.Method setvar = gcClass.getMethod("setVariable", new Class[] { String.class, Object.class }); java.lang.reflect.Method run = gcClass.getMethod("run"); - Object gc = gccons.newInstance(); - setvar.invoke(gc, new Object[] { "Jalview", this }); - run.invoke(gc); + groovyConsole = gccons.newInstance(); + setvar.invoke(groovyConsole, new Object[] { "Jalview", this }); + run.invoke(groovyConsole); + /* + * and rebuild alignframe menus to enable 'Run Groovy' + */ + + AlignFrame[] alignFrames = getAlignFrames(); + if (alignFrames != null) + { + for (AlignFrame af : alignFrames) + { + af.setGroovyEnabled(true); + } + } } catch (Exception ex) { jalview.bin.Cache.log.error("Groovy Shell Creation failed.", ex); @@ -2917,6 +2929,13 @@ public class Desktop extends jalview.jbgui.GDesktop implements */ private java.util.concurrent.Semaphore block = new Semaphore(0); + /* + * groovy.ui.Console object - if Groovy jars are present and the + * user has activated the Groovy console. Use via reflection to + * avoid compile-time dependency on Groovy libraries. + */ + private static Object groovyConsole; + /** * add another dialog thread to the queue * @@ -3145,4 +3164,9 @@ public class Desktop extends jalview.jbgui.GDesktop implements Desktop.currentAlignFrame = currentAlignFrame; } + public Object getGroovyConsole() + { + return groovyConsole; + } + } diff --git a/src/jalview/jbgui/GAlignFrame.java b/src/jalview/jbgui/GAlignFrame.java index 205b9c6..1eb558e 100755 --- a/src/jalview/jbgui/GAlignFrame.java +++ b/src/jalview/jbgui/GAlignFrame.java @@ -179,6 +179,8 @@ public class GAlignFrame extends JInternalFrame protected JMenu showProducts = new JMenu(); + protected JMenuItem runGroovy = new JMenuItem(); + protected JMenuItem rnahelicesColour = new JMenuItem(); protected JCheckBoxMenuItem autoCalculate = new JCheckBoxMenuItem(); @@ -1726,6 +1728,17 @@ public class GAlignFrame extends JInternalFrame // for show products actions see AlignFrame.canShowProducts showProducts.setText(MessageManager.getString("label.get_cross_refs")); + runGroovy.setText(MessageManager.getString("label.run_groovy")); + runGroovy.setToolTipText(MessageManager.getString("label.run_groovy_tip")); + runGroovy.addActionListener(new ActionListener() + { + @Override + public void actionPerformed(ActionEvent e) + { + runGroovy_actionPerformed(); + } + }); + JMenuItem openFeatureSettings = new JMenuItem( MessageManager.getString("action.feature_settings")); openFeatureSettings.addActionListener(new ActionListener() @@ -2160,6 +2173,7 @@ public class GAlignFrame extends JInternalFrame alignFrameMenuBar.add(colourMenu); alignFrameMenuBar.add(calculateMenu); alignFrameMenuBar.add(webService); + fileMenu.add(fetchSequence); fileMenu.add(addSequenceMenu); fileMenu.add(reload); @@ -2178,6 +2192,9 @@ public class GAlignFrame extends JInternalFrame fileMenu.add(associatedData); fileMenu.addSeparator(); fileMenu.add(closeMenuItem); + + pasteMenu.add(pasteNew); + pasteMenu.add(pasteThis); editMenu.add(undoMenuItem); editMenu.add(redoMenuItem); editMenu.add(cut); @@ -2198,6 +2215,13 @@ public class GAlignFrame extends JInternalFrame // editMenu.addSeparator(); editMenu.add(padGapsMenuitem); + showMenu.add(showAllColumns); + showMenu.add(showAllSeqs); + showMenu.add(showAllhidden); + hideMenu.add(hideSelColumns); + hideMenu.add(hideSelSequences); + hideMenu.add(hideAllSelection); + hideMenu.add(hideAllButSelection); viewMenu.add(newView); viewMenu.add(expandViews); viewMenu.add(gatherViews); @@ -2268,6 +2292,12 @@ public class GAlignFrame extends JInternalFrame colourMenu.add(modifyPID); colourMenu.add(annotationColour); colourMenu.add(rnahelicesColour); + + sort.add(sortIDMenuItem); + sort.add(sortLengthMenuItem); + sort.add(sortGroupMenuItem); + sort.add(sortPairwiseMenuItem); + sort.add(sortByTreeMenu); calculateMenu.add(sort); calculateMenu.add(calculateTree); calculateMenu.addSeparator(); @@ -2281,17 +2311,14 @@ public class GAlignFrame extends JInternalFrame calculateMenu.add(autoCalculate); calculateMenu.add(sortByTree); calculateMenu.addSeparator(); + calculateMenu.add(expandAlignment); calculateMenu.add(extractScores); + calculateMenu.addSeparator(); + calculateMenu.add(runGroovy); + webServiceNoServices = new JMenuItem( MessageManager.getString("label.no_services")); webService.add(webServiceNoServices); - pasteMenu.add(pasteNew); - pasteMenu.add(pasteThis); - sort.add(sortIDMenuItem); - sort.add(sortLengthMenuItem); - sort.add(sortGroupMenuItem); - sort.add(sortPairwiseMenuItem); - sort.add(sortByTreeMenu); exportImageMenu.add(htmlMenuItem); exportImageMenu.add(epsFile); exportImageMenu.add(createPNG); @@ -2303,13 +2330,6 @@ public class GAlignFrame extends JInternalFrame this.getContentPane().add(statusPanel, java.awt.BorderLayout.SOUTH); statusPanel.add(statusBar, null); this.getContentPane().add(tabbedPane, java.awt.BorderLayout.CENTER); - showMenu.add(showAllColumns); - showMenu.add(showAllSeqs); - showMenu.add(showAllhidden); - hideMenu.add(hideSelColumns); - hideMenu.add(hideSelSequences); - hideMenu.add(hideAllSelection); - hideMenu.add(hideAllButSelection); formatMenu.add(font); formatMenu.addSeparator(); @@ -2337,7 +2357,6 @@ public class GAlignFrame extends JInternalFrame selectMenu.add(grpsFromSelection); selectMenu.add(deleteGroups); selectMenu.add(annotationColumn); - calculateMenu.add(expandAlignment); // TODO - determine if the listenToViewSelections button is needed : see bug // JAL-574 // selectMenu.addSeparator(); @@ -2355,6 +2374,14 @@ public class GAlignFrame extends JInternalFrame } /** + * Try to run script in a Groovy console, having first ensured that this + * alignframe is set as currentAlignFrame in Desktop + */ + protected void runGroovy_actionPerformed() + { + + } + /** * Adds the given action listener and key accelerator to the given menu item. * Also saves in a lookup table to support lookup of action by key stroke. *