1 5 package net.sf.panoptes.view.swing.configurator; 6 7 import java.awt.BorderLayout ; 8 import java.util.Iterator ; 9 import java.util.Map ; 10 11 import javax.swing.JComponent ; 12 import javax.swing.JMenu ; 13 import javax.swing.JPanel ; 14 15 import net.sf.panoptes.controller.MainController; 16 import net.sf.panoptes.controller.actions.EditScriptAction; 17 import net.sf.panoptes.model.node.Node; 18 import net.sf.panoptes.swing.Operation; 19 import net.sf.panoptes.swing.OperationFactory; 20 import net.sf.panoptes.swing.desktop.MenuContributor; 21 import net.sf.panoptes.view.swing.editors.ScriptEditor; 22 import net.sf.panoptes.view.swing.editors.ScriptEditorListener; 23 24 import org.apache.commons.logging.Log; 25 import org.apache.commons.logging.LogFactory; 26 import org.python.core.PyClass; 27 import org.python.core.PyException; 28 import org.python.core.PyObject; 29 import org.python.core.PySyntaxError; 30 import org.python.util.PythonInterpreter; 31 32 38 public class JythonConfigurator 39 implements SwingConfigurator, MenuContributor, ScriptEditorListener { 40 41 private static Log log = LogFactory.getLog(JythonConfigurator.class); 42 private Node node = null; 43 private JPanel panel; 44 private Map attributes; 45 private ConfiguratorFrame frame; 46 47 public JComponent build(ConfiguratorFrame frame, Map attributes) throws ConfiguratorException { 48 this.frame = frame; 49 this.attributes = attributes; 50 panel = new JPanel (); 51 panel.setLayout(new BorderLayout ()); 52 return panel; 53 } 54 55 private void execJython(Operation op) throws ConfiguratorException { 56 if (node == null) 57 throw new ConfiguratorException("No MBeanWrapper given"); 58 try { 59 op.newTask("Removing existing components"); 60 op.setProgress(1, 4); 61 panel.removeAll(); 62 PythonInterpreter interp = new PythonInterpreter(); 63 String script = (String ) attributes.get("script"); 64 65 op.newTask("Registering context"); 66 op.setProgress(2, 4); 67 for (Iterator i = node.getContext().entrySet().iterator(); i.hasNext();) { 68 Map.Entry e = (Map.Entry ) i.next(); 69 interp.set((String ) e.getKey(), e.getValue()); 70 } 71 interp.set("panel", panel); 72 try { 73 interp.set("bean", node.getBean()); 74 interp.set("node", node); 75 } catch (Exception e) { 76 throw new ConfiguratorException("Unable to get mbean proxy", e); 77 } 78 op.newTask("Executing Jython script"); 79 op.setIndefinite(true); 80 interp.execfile(script); 81 if (frame != null) frame.pack(); 82 op.newTask("Execution completed"); 83 } catch (PyException e) { 84 throw new ConfiguratorException("Unable to execute jython script", e); 85 } 86 87 } 88 89 public void setNode(Operation op, Node node) throws ConfiguratorException { 90 this.node = node; 91 execJython(op); 92 } 93 94 97 public JMenu contributeMenu() { 98 JMenu m = new JMenu ("Jython"); 99 m.add(new EditScriptAction(this, (String ) attributes.get("script"), this)); 100 return m; 101 } 102 103 106 public void scriptSaved(ScriptEditor source) { 107 Operation op = OperationFactory.getFactory().createOperation("Refreshing script"); 108 try { 109 execJython(op); 110 } catch (ConfiguratorException e) { 111 log.error("Error executing Jython script", e); 112 StackTraceElement [] t = e.getStackTrace(); 113 if (e.getCause() instanceof PyException) { 114 PyException pye = (PyException) e.getCause(); 115 PyObject tb = pye.traceback; 116 if (pye instanceof PySyntaxError) { 117 PySyntaxError syntE = (PySyntaxError) pye; 118 119 String message = pye.value.__getitem__(0).toString(); 120 int line = pye.value.__getitem__(1).__getitem__(1).__int__().getValue(); 121 int col = pye.value.__getitem__(1).__getitem__(2).__int__().getValue(); 122 source.highlightError(message, line, col); 123 return; 124 } else { 125 int line = pye.traceback.tb_lineno; 126 String message = ((PyClass) pye.type).__name__ + ": '" + pye.value.toString() + "'"; 127 source.highlightError(message, line, 1); 128 return; 129 } 130 } 131 MainController.getInstance().displayError( 132 "Unable to execute python script", 133 e.getMessage(), 134 e); 135 } finally { 136 op.operationFinished(); 137 } 138 } 139 140 } | Popular Tags |