从java中的插件中的控制台获取输入 [英] Taking input from console in a plugin in java
问题描述
我正在开发一个Eclipse插件,我想从用户那里获取输入。我有一个问题,在处理控制台,因为我有2个控制台1在原来的Eclipse应用程序和另一个在新打开的Eclipse插件窗口。我使用下面显示的方法在控制台打印插件
I am developing an Eclipse plugin and I want to take input from the user. I had a problem before with dealing with the console as I have 2 consoles 1 in the original Eclipse application and the other in the new opened Eclipse plugin window. I used the method shown below to print in the Console of the Plugin
public static void writeToConsole(String output) {
MessageConsole console = new MessageConsole("My Console", null);
console.activate();
ConsolePlugin.getDefault().getConsoleManager().addConsoles(new IConsole[]{ console });
MessageConsoleStream stream = console.newMessageStream();
stream.println(output);
}
我只是不知道如何在控制台中写,在变量
I just don't know how to write in the console and take what's written in a variable
NB中
正常的方法如
BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));
String s = bufferRead.readLine();
System.out.println(s);
适用于原始的Eclipse而不是插件 b $ b
推荐答案
您不能绑定UI等待用户输入,因此您必须在接收到用户输入时处理。一种方法是使用文档侦听器。下面是打印输入的最后一行的示例:
You can't tie up the UI waiting for user input so you have to handle user input when it is received. One way to do this is with a document listener. Here is an example that prints the last line typed:
console.getDocument().addDocumentListener(new IDocumentListener() {
@Override
public void documentChanged(final DocumentEvent event) {
if ("\r\n".equals(event.getText())) {
final IDocument doc = event.getDocument();
try {
final IRegion region = doc.getLineInformationOfOffset(event.getOffset());
try {
final String line = doc.get(region.getOffset(), region.getLength());
System.out.println(line);
} catch (BadLocationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (BadLocationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public void documentAboutToBeChanged(final DocumentEvent event) {
// TODO Auto-generated method stub
}
});
这篇关于从java中的插件中的控制台获取输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!