如何设置 IOConsole 的插入符号

2022-01-16 00:00:00 console eclipse-plugin java eclipse-rcp rcp

I'm writing an eclipse-plugin which creating a new Console. Please see my source code:

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 = "
";
    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);
    }

}

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.

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.

相关文章