Eclipse RCP:自定义控制台
我正在尝试创建一个控制台,作为自定义编程语言的shell。 它与pydev交互式控制台非常相似。
目前,我的RCP使用基本的TextConsole并通过管道连接到shell,因此它只显示shell显示的内容,如果用户在RCP控制台中输入任何内容,则会在shell中写入相同内容。
我希望能够做更多的事情,例如移动插入位置,添加向上和向下箭头键等事件。我相信这样做我需要将一个StyledText小部件添加到控制台,这是通过ConsoleViewer完成的。
所以我的问题是,有没有办法让我覆盖TextConsole的ConsoleViewer,或者如果我要扩展TextConsole并创建我自己的,然后如何将它与启动配置(通过管道连接shell的那个)链接起来?
另外,要获取当前的默认控制台,我使用DebugUITools.getConsole(process)
。
如果我没有提供所需的所有信息,我很抱歉; 这有点难以解释。 我很乐意添加更多信息。
一个想法……据我所知,我可以使用createPage(ConsoleView)
TextConsolePage
从TextConsole
创建一个TextConsolePage
。 一旦我有了页面,我就可以通过setViewer(viewer)
设置查看setViewer(viewer)
。 在这里,我想如果我创建自己的查看器(将具有适当的样式组件),那么这可能是一个领先者。 唯一的问题是观众需要一个复合材料,我似乎无法弄清楚从哪里得到它。
你为什么不跟随PyDev做什么(如果你能够应付EPL许可证)?
相关代码可在以下位置找到:
所以我想我会自己回答,因为我终于能够完成控制台了。 它仍然是一个工作原型,但我想,随着你不断添加东西,你可以越来越多地清理代码。 对于我目前的目的,这是它的工作方式。
如果你想要短版本,那么我基本上模仿Eclipse提供的ProcessConsole
,因为我需要的是:一个控制台,我可以在其中连接一个进程,但由于ProcessConsole
是内部的,我想避免扩展这些类。
以下是我用来与我的控制台进行交互的类的概述。 我不会以MyConsole
创建位置为借口。 基本上,我使用自己的myProcess.getConsole()
方法而不是使用DebugUITools.getConsole(myProcess)
。 MyProcess
扩展了RuntimeProcess
。
class MyConsole extends IOConsole { private IOConsoleInputStream fInput; private IOConsoleOutputStream fOutput; private IStreamsProxy fStreamsProxy; private ConsoleHistory history; //This is to remember the caret position after the prompt private int caretAtPrompt; /* in the console so when you need to replace the command on up and down * arrow keys you have the position. * I just did a caretAtPrompt += String.Length wherever string was * appended to the console. Mainly in the streamlistener and * InputJob unless you specifically output something to the output * stream. */ //In the constructor you assign all the above fields. Below are some //to point out. //fInput = getInputStream(); // fStreamsProxy = process.getStreamsProxy(); // fOutput = newOutputStream(); //We must override the following method to get access to the caret @Override public IPageBookViewPage createPage(IConsoleView view) { return new MyConsolePage(this, view); } //After this I followed the ProcessConsole and added the //InputJob and StreamListener //defined in there. } class MyConsolePage extends TextConsolePage { //Not much in this class, just override the createViewer // to return MyConsoleViewer } class MyConsoleViewer extends TextConsoleViewer { //This is the most important class and most of the work is done here //Again I basically copied everything from IOConsoleViewer and then //updated whatever I needed //I added a VerifyKeyListener for the up and down arrow //keys for the console history MyConsoleViewer (Composite parent, MyConsole console) { //I have omitted a lot of code as it was too much to put up, //just highlighted a few getTextWidget().addVerifyKeyListener(new MyKeyChecker()); } class MyKeyChecker implements VerifyKeyListener {...} }
这是ProcessConsole
的代码。 这是IOConsoleViewer
的代码。
我创建的ConsoleHistory
类只有一个双向链接的字符串列表,用于保存用户输入的所有命令。 相当简单的类来创建。
一旦你看了Eclipse类( ProcessConsole
和IOConsoleViewer
),它实际上都是非常自我解释的。 我没有在这里添加太多代码因为有相当多的代码。 但希望这能给我一些方向,因为当我开始时我完全失去了。
我很乐意回答问题并添加更具体的代码,如果有人问。