2012-01-08 148 views
2

我正在開發一個讀取和寫入控制檯的Eclipse插件。我期望以下代碼使用「Hello World」來刷新一個警報窗口。Eclipse插件 - 延遲寫入控制檯

public void run(IAction action) { 
     ConsoleCommands.writeToConsole("Hello World!"); 
     Alert(ConsoleCommands.readConsole()); 
    } 

但是,警報只顯示空白。一些調查顯示,讀取是發生之前寫(在控制檯上顯示被罰款,只是提醒已表示儲物以前的狀態),所以我想,

public void run(IAction action) { 
     ConsoleCommands.writeToConsole("Hello Wolrd!"); 
     try { 
      Thread.sleep(4000); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 
     Alert(ConsoleCommands.readConsole()); 
    } 

的情況下,有一個線程問題,但這只是延遲了寫入控制檯的時間。任何想法發生了什麼?

----編輯-----

在情況下,它是非常有用的,下面是這些方法的代碼...

import org.eclipse.jface.text.IDocument; 
import org.eclipse.ui.console.ConsolePlugin; 
import org.eclipse.ui.console.IConsole; 
import org.eclipse.ui.console.IConsoleManager; 
import org.eclipse.ui.console.MessageConsole; 
import org.eclipse.ui.console.MessageConsoleStream; 

import org.eclipse.jface.action.IAction; 
import org.eclipse.jface.dialogs.MessageDialog; 
import org.eclipse.jface.text.IDocument; 
import org.eclipse.jface.viewers.ISelection; 
import org.eclipse.ui.IWorkbenchWindow; 
import org.eclipse.ui.IWorkbenchWindowActionDelegate; 
import org.eclipse.ui.console.ConsolePlugin; 
import org.eclipse.ui.console.IConsole; 
import org.eclipse.ui.console.IConsoleManager; 
import org.eclipse.ui.console.MessageConsole; 
import org.eclipse.ui.console.MessageConsoleStream; 


public class ConsoleCommands { 

    private static MessageConsole findConsole(String name) { 
     ConsolePlugin plugin = ConsolePlugin.getDefault(); 
     IConsoleManager conMan = plugin.getConsoleManager(); 
     IConsole[] existing = conMan.getConsoles(); 
     for (int i = 0; i < existing.length; i++) 
      if (name.equals(existing[i].getName())) 
       return (MessageConsole) existing[i]; 
     // no console found, so create a new one 
     MessageConsole myConsole = new MessageConsole(name, null); 
     conMan.addConsoles(new IConsole[] { myConsole }); 
     return myConsole; 
    } 

    public static String readConsole() { 
     MessageConsole myConsole = findConsole("Joe's Console"); 
     IDocument doc = myConsole.getDocument(); 
     return doc.get(); 
    } 

    public static MessageConsole writeToConsole(String output) { 
     MessageConsole myConsole = findConsole("Joe's Console"); 
     MessageConsoleStream out = myConsole.newMessageStream(); 
     out.println(output); 
     return myConsole; 
    } 

} 

回答

1

嘗試使用myConsole.getDocument().set(output)寫到控制檯,而不是使用流。

我不知道這是否是建議的做法,但它解決了我們的問題...