编程语言
首页 > 编程语言> > java – 如何设置IOConsole的Caret

java – 如何设置IOConsole的Caret

作者:互联网

我正在写一个eclipse-plugin,它创建了一个新的Console.请参阅我的源代码:

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();
                }
            }
        }
    }
}

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>”,但是Caret位置不合适,它位于该行的第一个位置而不是最后一个位置.我想让Caret搬到最后一个位置.谁知道请帮帮我.!!!

解决方法:

要访问插入符号位置,您需要实现一个控制台查看器.

这是我自定义控制台的设置,

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
}

根据您覆盖的方法,有多种方法可以获取样式化文本窗口小部件.我还创建了自己的控制台历史记录类,它跟踪插入符号偏移量,因为我需要使用向上和向下箭头键来浏览先前输入的命令的附加功能.

实现MyConsoleViewer的最佳方法是使用Eclipse的庞大源代码来设置一个完美的例子.我几乎重用了所有这个类org.eclipse.ui.internal.console.IOConsoleViewer.它甚至显示了设置插入符的示例.

希望这仍然有助于你的问题不久前.

标签:rcp,java,console,eclipse-plugin,eclipse-rcp
来源: https://codeday.me/bug/20191002/1841670.html