KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > panoptes > view > swing > configurator > JythonConfigurator


1 /*
2  * Created on Aug 30, 2003
3  *
4  */

5 package net.sf.panoptes.view.swing.configurator;
6
7 import java.awt.BorderLayout JavaDoc;
8 import java.util.Iterator JavaDoc;
9 import java.util.Map JavaDoc;
10
11 import javax.swing.JComponent JavaDoc;
12 import javax.swing.JMenu JavaDoc;
13 import javax.swing.JPanel JavaDoc;
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 /**
33  *
34  *
35  * @author Dag Liodden
36  * @version 0.1
37  */

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 JavaDoc panel;
44     private Map JavaDoc attributes;
45     private ConfiguratorFrame frame;
46
47     public JComponent JavaDoc build(ConfiguratorFrame frame, Map JavaDoc attributes) throws ConfiguratorException {
48         this.frame = frame;
49         this.attributes = attributes;
50         panel = new JPanel JavaDoc();
51         panel.setLayout(new BorderLayout JavaDoc());
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 JavaDoc script = (String JavaDoc) attributes.get("script");
64
65             op.newTask("Registering context");
66             op.setProgress(2, 4);
67             for (Iterator JavaDoc i = node.getContext().entrySet().iterator(); i.hasNext();) {
68                 Map.Entry JavaDoc e = (Map.Entry JavaDoc) i.next();
69                 interp.set((String JavaDoc) 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 JavaDoc 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     /* (non-Javadoc)
95      * @see net.sf.panoptes.swing.desktop.MenuContributor#contributeMenu()
96      */

97     public JMenu JavaDoc contributeMenu() {
98         JMenu JavaDoc m = new JMenu JavaDoc("Jython");
99         m.add(new EditScriptAction(this, (String JavaDoc) attributes.get("script"), this));
100         return m;
101     }
102
103     /* (non-Javadoc)
104      * @see net.sf.panoptes.view.swing.editor.ScriptEditorListener#scriptSaved()
105      */

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 JavaDoc[] 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 JavaDoc 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 JavaDoc 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