1 5 package net.sf.panoptes.view.swing.configurator; 6 7 import groovy.lang.GroovyClassLoader; 8 import groovy.lang.GroovyObject; 9 10 import java.awt.BorderLayout ; 11 import java.io.File ; 12 import java.io.FileInputStream ; 13 import java.util.Iterator ; 14 import java.util.Map ; 15 16 import javax.swing.JComponent ; 17 import javax.swing.JMenu ; 18 import javax.swing.JPanel ; 19 20 import net.sf.panoptes.controller.MainController; 21 import net.sf.panoptes.controller.actions.EditScriptAction; 22 import net.sf.panoptes.model.node.Node; 23 import net.sf.panoptes.swing.Operation; 24 import net.sf.panoptes.swing.OperationFactory; 25 import net.sf.panoptes.swing.desktop.MenuContributor; 26 import net.sf.panoptes.view.swing.editors.ScriptEditor; 27 import net.sf.panoptes.view.swing.editors.ScriptEditorListener; 28 29 import org.apache.commons.logging.Log; 30 import org.apache.commons.logging.LogFactory; 31 import org.python.core.PyClass; 32 import org.python.core.PyException; 33 import org.python.core.PyObject; 34 import org.python.core.PySyntaxError; 35 36 40 public class GroovyConfigurator 41 implements SwingConfigurator, MenuContributor, ScriptEditorListener { 42 43 private static Log log = LogFactory.getLog(GroovyConfigurator.class); 44 private Node node = null; 45 private JPanel panel; 46 private Map attributes; 47 private ConfiguratorFrame frame; 48 49 public JComponent build(ConfiguratorFrame frame, Map attributes) throws ConfiguratorException { 50 this.frame = frame; 51 this.attributes = attributes; 52 panel = new JPanel (); 53 panel.setLayout(new BorderLayout ()); 54 return panel; 55 } 56 57 private void execGroovy(Operation op) throws ConfiguratorException { 58 if (node == null) 59 throw new ConfiguratorException("No MBeanWrapper given"); 60 try { 61 op.newTask("Removing existing components"); 62 op.setProgress(1, 4); 63 panel.removeAll(); 64 65 String script = (String ) attributes.get("script"); 66 67 op.newTask("Compiling script"); 68 op.setProgress(2, 4); 69 ClassLoader parent = getClass().getClassLoader(); 70 GroovyClassLoader loader = new GroovyClassLoader(parent); 71 Class groovyClass = loader.parseClass(script); 72 73 op.newTask("Executing Groovy Script"); 74 op.setIndefinite(true); 75 GroovyObject groovyObject = (GroovyObject) groovyClass.newInstance(); 76 Object [] args = { panel, node, node.getContext()}; 77 groovyObject.invokeMethod("createView", args); 78 79 if (frame != null) 80 frame.pack(); 81 op.newTask("Execution completed"); 82 } catch (Exception e) { 83 throw new ConfiguratorException("Unable to execute Groovy script", e); 84 } 85 86 } 87 88 public void setNode(Operation op, Node node) throws ConfiguratorException { 89 this.node = node; 90 execGroovy(op); 91 } 92 93 98 public JMenu contributeMenu() { 99 JMenu m = new JMenu ("Groovy"); 100 m.add(new EditScriptAction(this, (String ) attributes.get("script"), this)); 101 return m; 102 } 103 104 109 public void scriptSaved(ScriptEditor source) { 110 Operation op = OperationFactory.getFactory().createOperation("Refreshing script"); 111 try { 112 execGroovy(op); 113 } catch (ConfiguratorException e) { 114 log.error("Error executing Groovy script", e); 115 MainController.getInstance().displayError( 116 "Unable to execute python script", 117 e.getMessage(), 118 e); 119 } finally { 120 op.operationFinished(); 121 } 122 } 123 124 } | Popular Tags |