GrxJythonPromptView.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2008, AIST, the University of Tokyo and General Robotix Inc.
00003  * All rights reserved. This program is made available under the terms of the
00004  * Eclipse Public License v1.0 which accompanies this distribution, and is
00005  * available at http://www.eclipse.org/legal/epl-v10.html
00006  * Contributors:
00007  * General Robotix Inc.
00008  * National Institute of Advanced Industrial Science and Technology (AIST) 
00009  */
00010 /*
00011  *  GrxJythonPromptView.java
00012  *
00013  *  Copyright (C) 2007 GeneralRobotix, Inc.
00014  *  All Rights Reserved
00015  *
00016  *  @author Yuichiro Kawasumi (General Robotix, Inc.)
00017  */
00018 
00019 package com.generalrobotix.ui.view;
00020 
00021 
00022 import java.awt.Frame;
00023 import java.io.File;
00024 import java.io.IOException;
00025 import java.io.PrintWriter;
00026 import java.io.Writer;
00027 import java.net.URL;
00028 import java.util.LinkedList;
00029 import java.util.List;
00030 
00031 import javax.swing.JOptionPane;
00032 
00033 import org.eclipse.jface.action.Action;
00034 import org.eclipse.jface.action.ActionContributionItem;
00035 import org.eclipse.jface.action.IAction;
00036 import org.eclipse.jface.action.IContributionItem;
00037 import org.eclipse.jface.action.ICoolBarManager;
00038 import org.eclipse.jface.action.IToolBarManager;
00039 import org.eclipse.jface.action.ToolBarContributionItem;
00040 import org.eclipse.jface.dialogs.MessageDialog;
00041 import org.eclipse.jface.window.ApplicationWindow;
00042 import org.eclipse.swt.SWT;
00043 import org.eclipse.swt.awt.SWT_AWT;
00044 import org.eclipse.swt.custom.StyledText;
00045 import org.eclipse.swt.custom.VerifyKeyListener;
00046 import org.eclipse.swt.events.SelectionEvent;
00047 import org.eclipse.swt.events.SelectionListener;
00048 import org.eclipse.swt.events.VerifyEvent;
00049 import org.eclipse.swt.graphics.Image;
00050 import org.eclipse.swt.layout.GridData;
00051 import org.eclipse.swt.layout.GridLayout;
00052 import org.eclipse.swt.widgets.Button;
00053 import org.eclipse.swt.widgets.Composite;
00054 import org.eclipse.swt.widgets.Display;
00055 import org.eclipse.ui.IWorkbenchWindow;
00056 import org.eclipse.ui.PlatformUI;
00057 import org.python.core.Py;
00058 import org.python.core.PyList;
00059 import org.python.core.PyString;
00060 import org.python.util.InteractiveInterpreter;
00061 
00062 import com.generalrobotix.ui.GrxBaseItem;
00063 import com.generalrobotix.ui.GrxBaseView;
00064 import com.generalrobotix.ui.GrxBaseViewPart;
00065 import com.generalrobotix.ui.GrxPluginManager;
00066 import com.generalrobotix.ui.grxui.Activator;
00067 import com.generalrobotix.ui.item.GrxPythonScriptItem;
00068 import com.generalrobotix.ui.util.GrxCorbaUtil;
00069 import com.generalrobotix.ui.util.MessageBundle;
00070 
00071 @SuppressWarnings("serial") //$NON-NLS-1$
00075 public class GrxJythonPromptView extends GrxBaseView { 
00076     private InteractiveInterpreter interpreter_ = new InteractiveInterpreter();
00077     private Thread thread_1_;
00078     private Thread thread_2_;
00079     private String prompt_ = ">>> "; //$NON-NLS-1$
00080     private Display display_;
00081     private Composite parent_;
00082     private StyledText styledText_;
00083     private MenuDialog menuDialog;
00084     private Frame frame_;
00085     private static final int HISTORY_SIZE = 50;
00086     private List<String> history_ = new LinkedList<String>();
00087     private int hpos_ = 0;
00088     private String com_;
00089     private Button btnExec_;
00090     private Writer writer_;
00091     private GrxPythonScriptItem currentItem_;
00092     private String message_;
00093     private Object result_;
00094 
00095     private static final int KS_SHIFT = SWT.SHIFT;
00096     private static final int KS_CONTROL = SWT.CTRL;
00097     private static final int KS_CTRL_U = 'u' | SWT.CTRL;
00098     private static final int KS_CTRL_A = 'a' | SWT.CTRL;
00099     private static final int KS_CTRL_E = 'e' | SWT.CTRL;
00100     private static final int KS_CTRL_F = 'f' | SWT.CTRL;
00101     private static final int KS_CTRL_B = 'b' | SWT.CTRL;
00102     private static final int KS_CTRL_N = 'n' | SWT.CTRL;
00103     private static final int KS_CTRL_P = 'p' | SWT.CTRL;
00104     private static final int KS_UP = SWT.ARROW_UP;
00105     private static final int KS_DOWN = SWT.ARROW_DOWN;
00106     private static final int KS_LEFT = SWT.ARROW_LEFT;
00107     private static final int KS_RIGHT = SWT.ARROW_RIGHT;
00108     private static final int KS_ENTER = SWT.CR;
00109     private static final int KS_ENTER_ALT = SWT.CR | SWT.ALT;
00110     private static final int KS_ENTER_CTRL = SWT.CR | SWT.CTRL;
00111     private static final int KS_ENTER_SHIFT = SWT.CR | SWT.SHIFT;
00112     private static final int KS_BACK_SPACE = SWT.BS;
00113     private static final int KS_DELETE = SWT.DEL;
00114   
00115     private Image simScriptStartIcon_;
00116     private Image simScriptStopIcon_;
00117     private StyledTextWriter stWriter_;
00118     
00119     private static final int INTERVAL=100;  // StyledText の更新間隔(ms)  //
00120     
00128     public GrxJythonPromptView(String name, GrxPluginManager manager, GrxBaseViewPart vp, Composite parent) {
00129         super(name, manager, vp, parent);
00130 
00131         display_ = parent.getDisplay();
00132         parent_ = parent;
00133         composite_.setLayout(new GridLayout(1,false));
00134         simScriptStartIcon_ = Activator.getDefault().getImage( "sim_script_start.png" ); //$NON-NLS-1$
00135         simScriptStopIcon_ = Activator.getDefault().getImage( "sim_script_stop.png"); //$NON-NLS-1$
00136         frame_ = SWT_AWT.new_Frame(new Composite(parent.getShell(),SWT.EMBEDDED));
00137         
00138         btnExec_ = new Button(composite_,SWT.TOGGLE);
00139         btnExec_.setImage(simScriptStartIcon_);
00140         btnExec_.setText(MessageBundle.get("GrxJythonPromptView.button.execute")); //$NON-NLS-1$
00141         //btnExec_.setPreferredSize(GrxBaseView.getDefaultButtonSize());
00142         //btnExec_.setMaximumSize(GrxBaseView.getDefaultButtonSize());
00143         btnExec_.setEnabled(false);
00144         
00145         styledText_ = new StyledText(composite_,SWT.MULTI|SWT.V_SCROLL|SWT.WRAP);
00146         styledText_.setEditable(false);
00147         styledText_.setText(prompt_);
00148         styledText_.addVerifyKeyListener(new ConsoleKeyListener());
00149         styledText_.setEditable(true);
00150         styledText_.setLayoutData(new GridData(GridData.FILL_BOTH));
00151         
00152         btnExec_.addSelectionListener(new SelectionListener(){
00153 
00154             public void widgetDefaultSelected(SelectionEvent e) {
00155             }
00156 
00157             public void widgetSelected(SelectionEvent e) {
00158                 selectedExecBtn();
00159             }
00160             
00161         });
00162              
00163         
00164         stWriter_ = new StyledTextWriter();
00165         writer_ =  new PrintWriter(stWriter_);
00166         interpreter_.setErr(writer_);
00167         interpreter_.setOut(writer_);
00168         interpreter_.set("uimanager", manager_); //$NON-NLS-1$
00169         interpreter_.exec("import sys"); //$NON-NLS-1$
00170         String dir = Activator.getDefault().getPreferenceStore().getString("JYTHON_LIB"); //$NON-NLS-1$
00171         if(dir.equals("")) //$NON-NLS-1$
00172                 dir = System.getenv("JYTHON_LIB"); //$NON-NLS-1$
00173         if(dir!=null){
00174                 String dirs[] = dir.split(":");
00175                 for (int i=0; i<dirs.length; i++){
00176                         File f = new File(dirs[i]);
00177                         if (f.exists()){
00178                                 interpreter_.exec("sys.path.append('"+dirs[i]+"')"); //$NON-NLS-1$ //$NON-NLS-2$
00179                                 File[] files = f.listFiles();
00180                                 for (int j=0; j<files.length; j++){
00181                                         if (files[j].getName().endsWith(".jar")){
00182                                         interpreter_.exec("sys.path.append('"+files[j].getAbsolutePath()+"')"); //$NON-NLS-1$ //$NON-NLS-2$
00183                                         }
00184                                 }
00185                         }
00186                 }
00187         }
00188         dir = Activator.getDefault().getPreferenceStore().getString("PROJECT_DIR"); //$NON-NLS-1$
00189         if(dir.equals("")) //$NON-NLS-1$
00190                 dir = System.getenv("PROJECT_DIR"); //$NON-NLS-1$
00191         if(dir!=null)
00192                 dir += "/../script"; //$NON-NLS-1$
00193         if(dir !=null && new File(dir).exists())
00194                 interpreter_.exec("sys.path.append('"+dir+"')"); //$NON-NLS-1$ //$NON-NLS-2$
00195         URL[] urls = manager_.pluginLoader_.getURLs();
00196         for (int i=0; i<urls.length; i++) {
00197             interpreter_.exec("sys.path.append('"+urls[i].getPath()+"')"); //$NON-NLS-1$ //$NON-NLS-2$
00198             interpreter_.exec("print \"sys.path.append(\'"+urls[i].getPath()+"\')\""); //$NON-NLS-1$ //$NON-NLS-2$
00199         }
00200         if(dir!=null && new File(dir).exists())
00201                 interpreter_.exec("import rbimporter"); //$NON-NLS-1$
00202         interpreter_.exec("import __builtin__"); //$NON-NLS-1$
00203         
00204         interpreter_.set("view", this); //$NON-NLS-1$
00205         interpreter_.exec("__builtin__.waitInput = view.waitInput"); //$NON-NLS-1$
00206         interpreter_.exec("__builtin__.waitInputConfirm = view.waitInputConfirm"); //$NON-NLS-1$
00207         interpreter_.exec("__builtin__.waitInputSelect = view.waitInputSelect"); //$NON-NLS-1$
00208         interpreter_.exec("__builtin__.waitInputMessage = view.waitInputMessage"); //$NON-NLS-1$
00209         interpreter_.exec("__builtin__.waitInputMenu = view.waitInputMenu"); //$NON-NLS-1$
00210         interpreter_.exec("__builtin__.waitInputSetMessage = view.waitInputSetMessage"); //$NON-NLS-1$
00211         interpreter_.exec("del view\n"); //$NON-NLS-1$
00212         interpreter_.exec("del __builtin__"); //$NON-NLS-1$
00213         history_.add(""); //$NON-NLS-1$
00214         setNameService();
00215         
00216         setMenuItem(new InitPythonAction());
00217         setScrollMinSize(SWT.DEFAULT,SWT.DEFAULT);
00218         
00219         setUp();
00220         manager_.registerItemChangeListener(this, GrxPythonScriptItem.class);
00221       
00222         Runnable stringOutRun_ = new Runnable() {
00223                         public void  run() {
00224                                 if(composite_!=null && !composite_.isDisposed()){
00225                                         Display display = composite_.getDisplay();
00226                                         if (!display.isDisposed()){
00227                                                 display.timerExec(INTERVAL, this);
00228                                                 String string=stWriter_.read();
00229                                                 if(!string.equals("")){
00230                                                         styledText_.append(string);
00231                                                         styledText_.setCaretOffset(styledText_.getText().length());
00232                                                         styledText_.setTopIndex(styledText_.getLineCount());
00233                                                 }
00234                                                 if(btnExec_.isEnabled() && btnExec_.getSelection()){
00235                                                         if( (thread_1_==null || !thread_1_.isAlive()) && (thread_2_==null || !thread_2_.isAlive()) ){
00236                                                                 btnExec_.setSelection(false);
00237                                 styleExecBtn(false);
00238                                                         }
00239                                                 }
00240                                         }
00241                                 }
00242                         }
00243                 };
00244                 Display display = composite_.getDisplay();
00245                 if (!display.isDisposed())
00246                         display.timerExec(INTERVAL, stringOutRun_);
00247 
00248                 updateEditerFont();
00249     }
00250      
00251     public void setUp(){
00252         currentItem_ = manager_.<GrxPythonScriptItem>getSelectedItem(GrxPythonScriptItem.class, null);
00253         btnExec_.setEnabled(currentItem_ != null);
00254     }
00255     
00256     private class InitPythonAction extends Action{
00257         public InitPythonAction(){
00258             super("cleanup python",IAction.AS_PUSH_BUTTON); //$NON-NLS-1$
00259         }
00260         public void run(){
00261             interpreter_.cleanup();
00262             interpreter_.exec("rbimporter.refresh()"); //$NON-NLS-1$
00263         }
00264     }
00265     
00266     public void interrupt() {
00267         if (thread_1_ != null) 
00268                 interpreter_.interrupt(Py.getThreadState());
00269         if (thread_2_ != null) {
00270             interpreter_.interrupt(Py.getThreadState());
00271         }
00272     }
00273     
00274     private class ConsoleKeyListener implements VerifyKeyListener {
00275 
00276         public void verifyKey(VerifyEvent event) {
00277             int ks = event.keyCode | event.stateMask;
00278             int len = styledText_.getText().length();
00279             int cp = styledText_.getCaretOffset();
00280 
00281             com_ = getCommand();
00282             event.doit = false;
00283             if (thread_1_ != null) {
00284                 return;
00285             } else if (ks == KS_ENTER      || ks == KS_ENTER_ALT
00286                     || ks == KS_ENTER_CTRL || ks == KS_ENTER_SHIFT) {
00287                 try {
00288                         if (com_.trim().length() <= 0){
00289                                 styledText_.append("\n"+prompt_); //$NON-NLS-1$
00290                                 styledText_.setCaretOffset(styledText_.getText().length());
00291                         styledText_.setTopIndex(styledText_.getLineCount());
00292                         }else{
00293                                 styledText_.append("\n"); //$NON-NLS-1$
00294                         btnExec_.setSelection(true);
00295                         styleExecBtn(true);
00296                         final String com = com_.trim();
00297                         thread_1_ = new Thread() {
00298                                 public void run() {
00299                                         result_=null;
00300                                         try{
00301                                                 if (com.startsWith("dir(")) { //$NON-NLS-1$
00302                                                         interpreter_.exec("__tmp__ = " +com); //$NON-NLS-1$
00303                                                         result_ = interpreter_.eval("__tmp__"); //$NON-NLS-1$
00304                                                 } else {
00305                                                         result_ = interpreter_.eval(com);
00306                                                 }
00307                                         }catch (org.python.core.PyException e) {
00308                                                 try{
00309                                                         interpreter_.exec(com);
00310                                                 }catch( org.python.core.PyException exception ){
00311                                                         result_ = exception.toString();
00312                                                 }
00313                                         }
00314                                         history_.add(1, com);
00315                                 if (history_.size() > HISTORY_SIZE)
00316                                     history_.remove(HISTORY_SIZE-1);
00317                                 if (result_ != null)
00318                                         stWriter_.write(result_.toString()+"\n");
00319                                 stWriter_.write(prompt_);
00320                                         hpos_ = 0;
00321                                 thread_1_ = null;
00322                                 };
00323                         };
00324                         thread_1_.start();
00325                         }
00326                 } catch (Exception e) {
00327                     e.printStackTrace();
00328                 }
00329             } else if (ks == KS_CTRL_U) {
00330                 styledText_.replaceTextRange(len - com_.length(), com_.length(),""); //$NON-NLS-1$
00331             } else if (ks == KS_CTRL_A) {
00332                 styledText_.setCaretOffset(len - com_.length());
00333             } else if (ks == KS_CTRL_E) {//Eclipseの標準キーバインド
00334                 styledText_.setCaretOffset(len);
00335             } else if ((ks == KS_CTRL_F || ks == KS_RIGHT)) {//Eclipseの標準キーバインド
00336                 if (cp < len)
00337                     styledText_.setCaretOffset(cp + 1);
00338             } else if ((ks == KS_CTRL_B || ks == KS_LEFT)) {//Eclipseの標準キーバインド
00339                 if (len - com_.length() < cp)
00340                     styledText_.setCaretOffset(cp - 1);
00341             } else if (ks == KS_SHIFT) {   // ignore input of shift key
00342             } else if (ks == KS_CONTROL) { // and control key
00343             } else if (ks == KS_BACK_SPACE) {
00344                 int p = styledText_.getText().lastIndexOf('\n') + prompt_.length() + 1;
00345                 if (p < cp) {
00346                     styledText_.replaceTextRange(cp - 1, 1,""); //$NON-NLS-1$
00347                 }
00348             } else if (ks == KS_DELETE) {
00349                 if (cp < len) {
00350                     styledText_.replaceTextRange( cp,1,""); //$NON-NLS-1$
00351                 }
00352             } else if (ks == KS_UP || ks == KS_CTRL_P) {
00353                 if (hpos_ < history_.size() - 1) {
00354                     int start = styledText_.getText().lastIndexOf('\n') + prompt_.length() + 1;
00355                     if (start <= len)
00356                         styledText_.replaceTextRange(start, len-start, history_.get(++hpos_));
00357                     styledText_.setCaretOffset(styledText_.getText().length());
00358                 }
00359             } else if (ks == KS_DOWN || ks == KS_CTRL_N) {//KS_CTRL_N
00360                 styledText_.setCaretOffset(len);
00361                 if (hpos_ > 0) {
00362                     int start = styledText_.getText().lastIndexOf('\n') + prompt_.length() + 1;
00363                     if (start <= len)
00364                         styledText_.replaceTextRange(start, len-start,history_.get(--hpos_));
00365                 }
00366             } else {
00367                 if (cp <styledText_.getText().lastIndexOf('\n')) {
00368                     cp = len;
00369                     styledText_.setCaretOffset(len);
00370                 }
00371                 event.doit = true;
00372             }
00373         }
00374     }
00375     
00376     public void execFile() {
00377         if (currentItem_ == null)
00378             return;
00379 
00380         if (currentItem_.isEdited()) {
00381             boolean ans = MessageDialog.openConfirm(parent_.getShell(), MessageBundle.get("GrxJythonPromptView.dialog.title.exec"), MessageBundle.get("GrxJythonPromptView.dialog.message.exec")); //$NON-NLS-1$ //$NON-NLS-2$
00382             if (!ans || !currentItem_.save())
00383                 return;
00384         }
00385         
00386         final String url = currentItem_.getURL(true);
00387         execFile(url);
00388     }
00389 
00390     public void execFile(final String url) {
00391         setNameService();
00392         File f = new File(url);
00393         File p = f.getParentFile();
00394         interpreter_.exec("import sys"); //$NON-NLS-1$
00395         PyString pyStr = new PyString(p.getAbsolutePath());
00396         PyList pathList = (PyList)interpreter_.eval("sys.path"); //$NON-NLS-1$
00397         if (!pathList.__contains__(pyStr)) {
00398             String com = "sys.path.append(\""+p.getAbsolutePath()+"\")"; //$NON-NLS-1$ //$NON-NLS-2$
00399             interpreter_.exec("print '"+com+"'"); //$NON-NLS-1$ //$NON-NLS-2$
00400             interpreter_.exec(com);
00401         }
00402         
00403         thread_2_ = new Thread() {
00404             public void run() {
00405                 try {
00406                     interpreter_.exec("print 'execfile("+url+")'"); //$NON-NLS-1$ //$NON-NLS-2$
00407                     interpreter_.execfile(url);
00408                 } catch (Exception e) {
00409                     e.printStackTrace();
00410                 }
00411                 stWriter_.write(prompt_);
00412                 hpos_ = 0;
00413             }
00414         };
00415         thread_2_.start();
00416     }
00417 
00418     private String getCommand() {
00419         String com = styledText_.getText();
00420         int idx = com.lastIndexOf('\n');
00421         if (idx > 0)
00422             com = com.substring(idx + 1);
00423         return com.replaceFirst(prompt_, ""); //$NON-NLS-1$
00424     }
00425     
00426     public void setNameService(){
00427         String nsHost = manager_.getProjectProperty("nsHost"); //$NON-NLS-1$
00428         String nsPort = manager_.getProjectProperty("nsPort"); //$NON-NLS-1$
00429         if (nsHost == null)
00430             nsHost = GrxCorbaUtil.nsHost();
00431         if (nsPort == null){
00432                 Integer np = new Integer(GrxCorbaUtil.nsPort());
00433             nsPort = np.toString();
00434         }
00435             
00436         String NS_OPT = "-ORBInitRef NameService=corbaloc:iiop:"+nsHost+":"+nsPort+"/NameService"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
00437         System.setProperty("NS_OPT", NS_OPT); //$NON-NLS-1$
00438         interpreter_.cleanup();
00439         interpreter_.exec("print 'NS_OPT="+NS_OPT+"'"); //$NON-NLS-1$ //$NON-NLS-2$
00440         interpreter_.exec("print '"+prompt_+"rbimporter.refresh()'"); //$NON-NLS-1$ //$NON-NLS-2$
00441         interpreter_.exec("rbimporter.refresh()"); //$NON-NLS-1$
00442         String nameservice = "could not parse"; //$NON-NLS-1$
00443         try {
00444             nameservice = NS_OPT.split("=")[1]; //$NON-NLS-1$
00445         } catch (Exception e) {
00446             interpreter_.exec("print 'failed to connect NameService("+nameservice+")'"); //$NON-NLS-1$ //$NON-NLS-2$
00447         }
00448     }
00449     /*
00450     public void restoreProperties() {
00451         super.restoreProperties();
00452         setNameService();
00453         
00454         //TODO:UIスレッド以外から呼ばれることはないならはずしてもいい。
00455         display_.asyncExec(new Thread(){
00456             public void run(){
00457                 styledText_.setCaretOffset(styledText_.getText().length());
00458                 //area_.setMaximumRowCount(getInt("maxRowCount", DEFAULT_MAX_ROW));
00459             }
00460         });
00461         
00462         String defaultScript = System.getProperty("SCRIPT"); //$NON-NLS-1$
00463         if (defaultScript != null) {
00464             System.clearProperty("SCRIPT"); //$NON-NLS-1$
00465             File f = new File(defaultScript);
00466             String name = f.getName().replace(".py", ""); //$NON-NLS-1$ //$NON-NLS-2$
00467             GrxBaseItem newItem = manager_.loadItem(GrxPythonScriptItem.class, name, f.getAbsolutePath());
00468             if(newItem!=null){
00469                     manager_.itemChange(newItem, GrxPluginManager.ADD_ITEM);
00470                     manager_.setSelectedItem(newItem, true);
00471             }
00472             execFile();
00473         }
00474     }
00475    */
00476     public void registerItemChange(GrxBaseItem item, int event){
00477         if(item instanceof GrxPythonScriptItem){
00478                 GrxPythonScriptItem pythonScriptItem = (GrxPythonScriptItem)item;
00479                 switch(event){
00480                 case GrxPluginManager.SELECTED_ITEM:
00481                         currentItem_ = pythonScriptItem;
00482                         btnExec_.setEnabled(true);
00483                         break;
00484                 case GrxPluginManager.REMOVE_ITEM:
00485                 case GrxPluginManager.NOTSELECTED_ITEM:
00486                         currentItem_ = null;
00487                         btnExec_.setEnabled(false);
00488                         break;
00489                 default:
00490                         break;
00491                 }
00492         }
00493     }
00494  
00495     public void waitInput(final String msg) {
00496         JOptionPane.showMessageDialog(frame_, msg);
00497     }
00498     
00499     public void waitInputConfirm(final String msg) {
00500         int ans = JOptionPane.showConfirmDialog(frame_,
00501             msg,"waitInputConfirm", //$NON-NLS-1$
00502             JOptionPane.OK_CANCEL_OPTION,
00503             JOptionPane.INFORMATION_MESSAGE,
00504             null);
00505         if (ans != JOptionPane.OK_OPTION)
00506             interrupt();
00507     }
00508     
00509     public boolean waitInputSelect(final String msg) {
00510         int ans = JOptionPane.showConfirmDialog(frame_,
00511             msg,"waitInputSelect", //$NON-NLS-1$
00512             JOptionPane.YES_NO_CANCEL_OPTION,
00513             JOptionPane.INFORMATION_MESSAGE,
00514             null);
00515         if (ans == JOptionPane.YES_OPTION)
00516             return true;
00517         else if (ans != JOptionPane.NO_OPTION)
00518             interrupt();
00519         return false;
00520     }
00521     
00522     public Object waitInputMessage(String msg) {
00523         return JOptionPane.showInputDialog(frame_,
00524             msg, "waitInputMessage", //$NON-NLS-1$
00525             JOptionPane.INFORMATION_MESSAGE,
00526             null,null,null);
00527     }
00528     
00529     public void waitInputMenu(String[][] menuList) {
00530         menuDialog = new MenuDialog(menuList, interpreter_, message_, stWriter_);
00531         menuDialog.showDialog(frame_, currentItem_.getName(), false);
00532     }
00533     public void waitInputSetMessage(String msg) {
00534         message_ = msg;
00535         if(MenuDialog.getCurrentMenuDialog()!=null)
00536                 menuDialog.setMessage(msg);
00537     }
00538 
00539     public class StyledTextWriter extends Writer {
00540         private StringBuffer outString_ = null;
00541         
00542         public StyledTextWriter() {
00543                 outString_ = new StringBuffer();
00544         }
00545 
00546         public void write(char[] cbuf, int off, int len) throws IOException {
00547             synchronized (lock) {
00548                 if ((off < 0) || (off > cbuf.length) || (len < 0)
00549                         || ((off + len) > cbuf.length) || ((off + len) < 0)) {
00550                     throw new IndexOutOfBoundsException();
00551                 } else if (len == 0) {
00552                 }
00553 
00554                 char[] c = new char[len];
00555                 System.arraycopy(cbuf, off, c, 0, len);
00556                 outString_.append(c);
00557             }
00558         }
00559 
00560         public void close() throws IOException {
00561             synchronized (lock) {
00562             }
00563         }
00564 
00565         public void flush() throws IOException {
00566             synchronized (lock) {
00567             }
00568         }
00569         
00570         public String read(){
00571                 synchronized(lock){
00572                         String str = outString_.toString();
00573                         outString_.delete(0, outString_.length());
00574                         return str;
00575                 }
00576         }
00577         
00578         public void write(String str){
00579                 synchronized(lock){
00580                         outString_.append(str);
00581                 }
00582         }
00583         
00584     }
00585     
00586     public void shutdown(){
00587         manager_.removeItemChangeListener(this, GrxPythonScriptItem.class);
00588     }
00589 
00590     public boolean getEnabledExecBtn(){
00591         return btnExec_.getEnabled();
00592     }
00593 
00594     public void selectExecBtn(){
00595         btnExec_.setSelection(!btnExec_.getSelection());
00596         selectedExecBtn();
00597     }
00598 
00599     private void selectedExecBtn(){
00600         if (btnExec_.getSelection()) {
00601             styleExecBtn(true);
00602             execFile();
00603         } else {
00604             styleExecBtn(false);
00605             interrupt();
00606         }
00607     }
00608 
00609     private void styleExecBtn(boolean selected){
00610         IAction action = getStartSimulationAction();
00611         if(selected){
00612             btnExec_.setImage(simScriptStopIcon_);
00613             btnExec_.setToolTipText(MessageBundle.get("GrxJythonPromptView.text.interrupt"));
00614             if(action != null){
00615                 action.setText("interrupt python threads");
00616                 action.setImageDescriptor(Activator.getDefault().getDescriptor("sim_script_stop.png"));
00617             }
00618         }
00619         else{
00620             btnExec_.setImage(simScriptStartIcon_);
00621             btnExec_.setToolTipText(MessageBundle.get("GrxJythonPromptView.text.execute"));
00622             styledText_.setEnabled(true);
00623             if(action != null){
00624                 action.setToolTipText("Execute Script File");
00625                 action.setImageDescriptor(Activator.getDefault().getDescriptor("sim_script_start.png"));
00626             }
00627         }
00628     }
00629 
00630     private IAction getStartSimulationAction()
00631     {
00632         IWorkbenchWindow[] windows = PlatformUI.getWorkbench().getWorkbenchWindows();
00633         for(IWorkbenchWindow w : windows){
00634             if(!(w instanceof ApplicationWindow))
00635                 continue;
00636             ApplicationWindow window = (ApplicationWindow)w;
00637             ICoolBarManager coolbar = window.getCoolBarManager2();
00638             if(coolbar == null)
00639                 continue;
00640             IContributionItem setitem = coolbar.find("com.generalrobotix.ui.actionSet");
00641             if(setitem != null && setitem instanceof ToolBarContributionItem)
00642             {
00643                 IToolBarManager toolbar = ((ToolBarContributionItem)setitem).getToolBarManager();
00644                 if(toolbar == null)
00645                     continue;
00646                 IContributionItem actitem = toolbar.find("com.generalrobotix.ui.actions.ExecuteScript");
00647                 if(actitem != null && actitem instanceof ActionContributionItem)
00648                     return ((ActionContributionItem)actitem).getAction();
00649             }
00650         }
00651         return null;
00652     }
00653 
00654     public void updateEditerFont(){
00655         styledText_.setFont(Activator.getDefault().getFont("preference_editer"));
00656     }
00657 }


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Sun Apr 2 2017 03:43:53