如何设置IOConsole的插入符号 [英] How to set the Caret of the IOConsole
问题描述
我在写一个eclipse插件,创建一个新的控制台。请查看我的源代码:
I'm writing an eclipse-plugin which creating a new Console. Please see my source code:
CliConsoleFactory.java
CliConsoleFactory.java
import java.io.IOException;
import org.eclipse.jface.text.DocumentEvent;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IDocumentListener;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.IConsole;
import org.eclipse.ui.console.IConsoleConstants;
import org.eclipse.ui.console.IConsoleFactory;
import org.eclipse.ui.console.IConsoleView;
import org.eclipse.ui.console.IOConsoleOutputStream;
public class CliConsoleFactory implements IConsoleFactory {
private static final String ENTER_KEY = "\r\n";
private static final String CLI_PROMPT = "CLI> ";
private IConsoleView m_consoleView = null;
@Override
public void openConsole() {
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
try {
m_consoleView = (IConsoleView) page.showView(IConsoleConstants.ID_CONSOLE_VIEW);
} catch (PartInitException e1) {
e1.printStackTrace();
}
if (m_consoleView == null) {
return;
}
final MyIOConsole myConsole = new MyIOConsole("CLI", null);
final IDocument document = myConsole.getDocument();
document.addDocumentListener(new IDocumentListener() {
@Override
public void documentChanged(DocumentEvent event) {
if (ENTER_KEY.equals(event.getText())) {
// Print the Prompt
writeToConsole(myConsole, CLI_PROMPT);
}
}
@Override
public void documentAboutToBeChanged(DocumentEvent event) {
}
});
ConsolePlugin.getDefault().getConsoleManager().addConsoles(new IConsole[] { myConsole });
m_consoleView.display(myConsole);
writeToConsole(myConsole, CLI_PROMPT);
}
private void writeToConsole(final MyIOConsole myConsole, String msg) {
IOConsoleOutputStream stream = myConsole.newOutputStream();
stream.setActivateOnWrite(true);
try {
stream.write(msg);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
$ b b
MyIOConsole.java
MyIOConsole.java
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.console.IOConsole;
public class MyIOConsole extends IOConsole {
public MyIOConsole(String name, ImageDescriptor imageDescriptor) {
super(name, imageDescriptor);
}
}
当我进入一个新的行,提示是CLI>,但光标位置不是okie,它是在行的第一个位置,而不是最后一个位置。我想让光标移动到最后一个位置。谁知道请帮助我。
It works great. When I enter to a new line, the Prompt is "CLI> ", but the Caret position is not okie, it is at the first position of the line instead of the last position. I want to make the Caret move to the last position. Who know please help me.!!!
推荐答案
要获得对插入位置的访问,您需要实现一个控制台查看器。
To gain access to the caret position, you will need to implement a console viewer.
这是我为自定义控制台设置的
This is the setup I have for my custom console,
public class MyConsole extends IOConsole
{
....
@Override
public IPageBookViewPage createPage(IConsoleView view) {
return new MyConsolePage(this, view);
}
}
public class MyConsolePage extends TextConsolePage
{
....
@Override
protected TextConsoleViewer createViewer(Composite parent) {
return new MyConsoleViewer(parent, (MyConsole) this.getConsole());
}
}
public class MyConsoleViewer extends TextConsoleViewer
{
//This class gives you access to setting the caret position
//by getting the styled text widget and then using setCaretOffset
}
有多种方式样式文本小部件取决于您覆盖的方法。我还创建了我自己的控制台历史记录类,它记录了插入符号偏移,因为我需要使用向上和向下箭头键浏览以前输入的命令的额外功能。
There are multiple ways of getting the styled text widget depending on which method you are overriding. I also created my own Console history class which kept track of the caret offset since I needed additional functionality of using the up and down arrow keys to navigate through previously entered commands.
实现MyConsoleViewer的最好方法是使用Eclipse的庞大的源代码,设置一个完美的例子。我实际上重用了这个类 org.eclipse.ui.internal.console.IOConsoleViewer
。它甚至显示了设置插入符号的示例。
The best way to implement the MyConsoleViewer is to use Eclipse's vast source code that sets a perfect example. I practically reused all of this class org.eclipse.ui.internal.console.IOConsoleViewer
. It even shows examples of setting the caret.
希望这仍然有助于您的问题是前一段时间。
Hope this still helps as your question was a while ago.
这篇关于如何设置IOConsole的插入符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!