19 package com.generalrobotix.ui.view;
22 import java.awt.Frame;
24 import java.io.IOException;
25 import java.io.PrintWriter;
26 import java.io.Writer;
28 import java.util.LinkedList;
29 import java.util.List;
31 import javax.swing.JOptionPane;
33 import org.eclipse.jface.action.Action;
34 import org.eclipse.jface.action.ActionContributionItem;
35 import org.eclipse.jface.action.IAction;
36 import org.eclipse.jface.action.IContributionItem;
37 import org.eclipse.jface.action.ICoolBarManager;
38 import org.eclipse.jface.action.IToolBarManager;
39 import org.eclipse.jface.action.ToolBarContributionItem;
40 import org.eclipse.jface.dialogs.MessageDialog;
41 import org.eclipse.jface.window.ApplicationWindow;
42 import org.eclipse.swt.SWT;
43 import org.eclipse.swt.awt.SWT_AWT;
44 import org.eclipse.swt.custom.StyledText;
45 import org.eclipse.swt.custom.VerifyKeyListener;
46 import org.eclipse.swt.events.SelectionEvent;
47 import org.eclipse.swt.events.SelectionListener;
48 import org.eclipse.swt.events.VerifyEvent;
49 import org.eclipse.swt.graphics.Image;
50 import org.eclipse.swt.layout.GridData;
51 import org.eclipse.swt.layout.GridLayout;
52 import org.eclipse.swt.widgets.Button;
53 import org.eclipse.swt.widgets.Composite;
54 import org.eclipse.swt.widgets.Display;
55 import org.eclipse.ui.IWorkbenchWindow;
56 import org.eclipse.ui.PlatformUI;
57 import org.python.core.Py;
58 import org.python.core.PyList;
59 import org.python.core.PyString;
60 import org.python.util.InteractiveInterpreter;
71 @SuppressWarnings(
"serial")
76 private InteractiveInterpreter interpreter_ =
new InteractiveInterpreter();
79 private String prompt_ =
">>> ";
85 private static final int HISTORY_SIZE = 50;
86 private List<String> history_ =
new LinkedList<String>();
87 private int hpos_ = 0;
95 private static final int KS_SHIFT = SWT.SHIFT;
96 private static final int KS_CONTROL = SWT.CTRL;
97 private static final int KS_CTRL_U =
'u' | SWT.CTRL;
98 private static final int KS_CTRL_A =
'a' | SWT.CTRL;
99 private static final int KS_CTRL_E =
'e' | SWT.CTRL;
100 private static final int KS_CTRL_F =
'f' | SWT.CTRL;
101 private static final int KS_CTRL_B =
'b' | SWT.CTRL;
102 private static final int KS_CTRL_N =
'n' | SWT.CTRL;
103 private static final int KS_CTRL_P =
'p' | SWT.CTRL;
104 private static final int KS_UP = SWT.ARROW_UP;
105 private static final int KS_DOWN = SWT.ARROW_DOWN;
106 private static final int KS_LEFT = SWT.ARROW_LEFT;
107 private static final int KS_RIGHT = SWT.ARROW_RIGHT;
108 private static final int KS_ENTER = SWT.CR;
109 private static final int KS_ENTER_ALT = SWT.CR | SWT.ALT;
110 private static final int KS_ENTER_CTRL = SWT.CR | SWT.CTRL;
111 private static final int KS_ENTER_SHIFT = SWT.CR | SWT.SHIFT;
112 private static final int KS_BACK_SPACE = SWT.BS;
113 private static final int KS_DELETE = SWT.DEL;
119 private static final int INTERVAL=100;
129 super(
name, manager, vp, parent);
131 display_ = parent.getDisplay();
133 composite_.setLayout(
new GridLayout(1,
false));
136 frame_ = SWT_AWT.new_Frame(
new Composite(parent.getShell(),SWT.EMBEDDED));
138 btnExec_ =
new Button(composite_,SWT.TOGGLE);
139 btnExec_.setImage(simScriptStartIcon_);
143 btnExec_.setEnabled(
false);
145 styledText_ =
new StyledText(composite_,SWT.MULTI|SWT.V_SCROLL|SWT.WRAP);
146 styledText_.setEditable(
false);
147 styledText_.setText(prompt_);
149 styledText_.setEditable(
true);
150 styledText_.setLayoutData(
new GridData(GridData.FILL_BOTH));
152 btnExec_.addSelectionListener(
new SelectionListener(){
154 public void widgetDefaultSelected(SelectionEvent e) {
157 public void widgetSelected(SelectionEvent e) {
165 writer_ =
new PrintWriter(stWriter_);
166 interpreter_.setErr(writer_);
167 interpreter_.setOut(writer_);
168 interpreter_.set(
"uimanager", manager_);
169 interpreter_.exec(
"import sys");
172 dir = System.getenv(
"JYTHON_LIB");
174 String dirs[] = dir.split(
":");
175 for (
int i=0;
i<dirs.length;
i++){
176 File
f =
new File(dirs[
i]);
178 interpreter_.exec(
"sys.path.append('"+dirs[
i]+
"')");
179 File[] files =
f.listFiles();
180 for (
int j=0; j<files.length; j++){
181 if (files[j].getName().endsWith(
".jar")){
182 interpreter_.exec(
"sys.path.append('"+files[j].getAbsolutePath()+
"')");
190 dir = System.getenv(
"PROJECT_DIR");
193 if(dir !=
null &&
new File(dir).exists())
194 interpreter_.exec(
"sys.path.append('"+dir+
"')");
195 URL[] urls = manager_.pluginLoader_.getURLs();
196 for (
int i=0;
i<urls.length;
i++) {
197 interpreter_.exec(
"sys.path.append('"+urls[
i].getPath()+
"')");
198 interpreter_.exec(
"print \"sys.path.append(\'"+urls[
i].getPath()+
"\')\"");
200 if(dir!=
null &&
new File(dir).exists())
201 interpreter_.exec(
"import rbimporter");
202 interpreter_.exec(
"import __builtin__");
204 interpreter_.set(
"view",
this);
205 interpreter_.exec(
"__builtin__.waitInput = view.waitInput");
206 interpreter_.exec(
"__builtin__.waitInputConfirm = view.waitInputConfirm");
207 interpreter_.exec(
"__builtin__.waitInputSelect = view.waitInputSelect");
208 interpreter_.exec(
"__builtin__.waitInputMessage = view.waitInputMessage");
209 interpreter_.exec(
"__builtin__.waitInputMenu = view.waitInputMenu");
210 interpreter_.exec(
"__builtin__.waitInputSetMessage = view.waitInputSetMessage");
211 interpreter_.exec(
"del view\n");
212 interpreter_.exec(
"del __builtin__");
217 setScrollMinSize(SWT.DEFAULT,SWT.DEFAULT);
222 Runnable stringOutRun_ =
new Runnable() {
224 if(composite_!=
null && !composite_.isDisposed()){
225 Display display = composite_.getDisplay();
226 if (!display.isDisposed()){
227 display.timerExec(INTERVAL,
this);
228 String
string=stWriter_.
read();
229 if(!
string.equals(
"")){
230 styledText_.append(
string);
231 styledText_.setCaretOffset(styledText_.getText().length());
232 styledText_.setTopIndex(styledText_.getLineCount());
234 if(btnExec_.isEnabled() && btnExec_.getSelection()){
235 if( (thread_1_==
null || !thread_1_.isAlive()) && (thread_2_==
null || !thread_2_.isAlive()) ){
236 btnExec_.setSelection(
false);
244 Display display = composite_.getDisplay();
245 if (!display.isDisposed())
246 display.timerExec(INTERVAL, stringOutRun_);
253 btnExec_.setEnabled(currentItem_ !=
null);
258 super(
"cleanup python",IAction.AS_PUSH_BUTTON);
261 interpreter_.cleanup();
262 interpreter_.exec(
"rbimporter.refresh()");
267 if (thread_1_ !=
null)
268 interpreter_.interrupt(Py.getThreadState());
269 if (thread_2_ !=
null) {
270 interpreter_.interrupt(Py.getThreadState());
277 int ks =
event.keyCode |
event.stateMask;
278 int len = styledText_.getText().length();
279 int cp = styledText_.getCaretOffset();
283 if (thread_1_ !=
null) {
285 }
else if (ks == KS_ENTER || ks == KS_ENTER_ALT
286 || ks == KS_ENTER_CTRL || ks == KS_ENTER_SHIFT) {
288 if (com_.trim().length() <= 0){
289 styledText_.append(
"\n"+prompt_);
290 styledText_.setCaretOffset(styledText_.getText().length());
291 styledText_.setTopIndex(styledText_.getLineCount());
293 styledText_.append(
"\n");
294 btnExec_.setSelection(
true);
296 final String
com = com_.trim();
297 thread_1_ =
new Thread() {
301 if (
com.startsWith(
"dir(")) {
302 interpreter_.exec(
"__tmp__ = " +
com);
303 result_ = interpreter_.eval(
"__tmp__");
305 result_ = interpreter_.eval(
com);
307 }
catch (org.python.core.PyException e) {
309 interpreter_.exec(
com);
310 }
catch( org.python.core.PyException exception ){
311 result_ = exception.toString();
314 history_.add(1,
com);
315 if (history_.size() > HISTORY_SIZE)
316 history_.remove(HISTORY_SIZE-1);
318 stWriter_.
write(result_.toString()+
"\n");
319 stWriter_.
write(prompt_);
326 }
catch (Exception e) {
329 }
else if (ks == KS_CTRL_U) {
330 styledText_.replaceTextRange(len - com_.length(), com_.length(),
"");
331 }
else if (ks == KS_CTRL_A) {
332 styledText_.setCaretOffset(len - com_.length());
333 }
else if (ks == KS_CTRL_E) {
334 styledText_.setCaretOffset(len);
335 }
else if ((ks == KS_CTRL_F || ks == KS_RIGHT)) {
337 styledText_.setCaretOffset(cp + 1);
338 }
else if ((ks == KS_CTRL_B || ks == KS_LEFT)) {
339 if (len - com_.length() < cp)
340 styledText_.setCaretOffset(cp - 1);
341 }
else if (ks == KS_SHIFT) {
342 }
else if (ks == KS_CONTROL) {
343 }
else if (ks == KS_BACK_SPACE) {
344 int p = styledText_.getText().lastIndexOf(
'\n') + prompt_.length() + 1;
346 styledText_.replaceTextRange(cp - 1, 1,
"");
348 }
else if (ks == KS_DELETE) {
350 styledText_.replaceTextRange( cp,1,
"");
352 }
else if (ks == KS_UP || ks == KS_CTRL_P) {
353 if (hpos_ < history_.size() - 1) {
354 int start = styledText_.getText().lastIndexOf(
'\n') + prompt_.length() + 1;
356 styledText_.replaceTextRange(
start, len-
start, history_.get(++hpos_));
357 styledText_.setCaretOffset(styledText_.getText().length());
359 }
else if (ks == KS_DOWN || ks == KS_CTRL_N) {
360 styledText_.setCaretOffset(len);
362 int start = styledText_.getText().lastIndexOf(
'\n') + prompt_.length() + 1;
364 styledText_.replaceTextRange(
start, len-
start,history_.get(--hpos_));
367 if (cp <styledText_.getText().lastIndexOf(
'\n')) {
369 styledText_.setCaretOffset(len);
377 if (currentItem_ ==
null)
381 boolean ans = MessageDialog.openConfirm(parent_.getShell(),
MessageBundle.
get(
"GrxJythonPromptView.dialog.title.exec"),
MessageBundle.
get(
"GrxJythonPromptView.dialog.message.exec"));
382 if (!ans || !currentItem_.
save())
386 final String url = currentItem_.
getURL(
true);
392 File
f =
new File(url);
393 File p =
f.getParentFile();
394 interpreter_.exec(
"import sys");
395 PyString pyStr =
new PyString(p.getAbsolutePath());
396 PyList pathList = (PyList)interpreter_.eval(
"sys.path");
397 if (!pathList.__contains__(pyStr)) {
398 String
com =
"sys.path.append(\""+p.getAbsolutePath()+
"\")";
399 interpreter_.exec(
"print '"+
com+
"'");
400 interpreter_.exec(
com);
403 thread_2_ =
new Thread() {
406 interpreter_.exec(
"print 'execfile("+url+
")'");
407 interpreter_.execfile(url);
408 }
catch (Exception e) {
411 stWriter_.
write(prompt_);
419 String
com = styledText_.getText();
420 int idx =
com.lastIndexOf(
'\n');
422 com =
com.substring(idx + 1);
423 return com.replaceFirst(prompt_,
"");
427 String nsHost = manager_.getProjectProperty(
"nsHost");
428 String nsPort = manager_.getProjectProperty(
"nsPort");
433 nsPort = np.toString();
436 String NS_OPT =
"-ORBInitRef NameService=corbaloc:iiop:"+nsHost+
":"+nsPort+
"/NameService";
437 System.setProperty(
"NS_OPT", NS_OPT);
438 interpreter_.cleanup();
439 interpreter_.exec(
"print 'NS_OPT="+NS_OPT+
"'");
440 interpreter_.exec(
"print '"+prompt_+
"rbimporter.refresh()'");
441 interpreter_.exec(
"rbimporter.refresh()");
442 String nameservice =
"could not parse";
444 nameservice = NS_OPT.split(
"=")[1];
445 }
catch (Exception e) {
446 interpreter_.exec(
"print 'failed to connect NameService("+nameservice+
")'");
481 currentItem_ = pythonScriptItem;
482 btnExec_.setEnabled(
true);
487 btnExec_.setEnabled(
false);
496 JOptionPane.showMessageDialog(frame_, msg);
500 int ans = JOptionPane.showConfirmDialog(frame_,
501 msg,
"waitInputConfirm",
502 JOptionPane.OK_CANCEL_OPTION,
503 JOptionPane.INFORMATION_MESSAGE,
505 if (ans != JOptionPane.OK_OPTION)
510 int ans = JOptionPane.showConfirmDialog(frame_,
511 msg,
"waitInputSelect",
512 JOptionPane.YES_NO_CANCEL_OPTION,
513 JOptionPane.INFORMATION_MESSAGE,
515 if (ans == JOptionPane.YES_OPTION)
517 else if (ans != JOptionPane.NO_OPTION)
523 return JOptionPane.showInputDialog(frame_,
524 msg,
"waitInputMessage",
525 JOptionPane.INFORMATION_MESSAGE,
530 menuDialog =
new MenuDialog(menuList, interpreter_, message_, stWriter_);
546 public void write(
char[] cbuf,
int off,
int len)
throws IOException {
547 synchronized (lock) {
548 if ((off < 0) || (off > cbuf.length) || (len < 0)
549 || ((off + len) > cbuf.length) || ((off + len) < 0)) {
550 throw new IndexOutOfBoundsException();
551 }
else if (len == 0) {
554 char[]
c =
new char[len];
555 System.arraycopy(cbuf, off,
c, 0, len);
560 public void close() throws IOException {
561 synchronized (lock) {
565 public void flush() throws IOException {
566 synchronized (lock) {
591 return btnExec_.getEnabled();
595 btnExec_.setSelection(!btnExec_.getSelection());
600 if (btnExec_.getSelection()) {
610 IAction action = getStartSimulationAction();
612 btnExec_.setImage(simScriptStopIcon_);
613 btnExec_.setToolTipText(
MessageBundle.
get(
"GrxJythonPromptView.text.interrupt"));
615 action.setText(
"interrupt python threads");
620 btnExec_.setImage(simScriptStartIcon_);
621 btnExec_.setToolTipText(
MessageBundle.
get(
"GrxJythonPromptView.text.execute"));
622 styledText_.setEnabled(
true);
624 action.setToolTipText(
"Execute Script File");
632 IWorkbenchWindow[] windows = PlatformUI.getWorkbench().getWorkbenchWindows();
633 for(IWorkbenchWindow w : windows){
634 if(!(w instanceof ApplicationWindow))
636 ApplicationWindow window = (ApplicationWindow)w;
637 ICoolBarManager coolbar = window.getCoolBarManager2();
640 IContributionItem setitem = coolbar.find(
"com.generalrobotix.ui.actionSet");
641 if(setitem !=
null && setitem instanceof ToolBarContributionItem)
643 IToolBarManager toolbar = ((ToolBarContributionItem)setitem).getToolBarManager();
646 IContributionItem actitem = toolbar.find(
"com.generalrobotix.ui.actions.ExecuteScript");
647 if(actitem !=
null && actitem instanceof ActionContributionItem)
648 return ((ActionContributionItem)actitem).getAction();