工具栏撤消重做Eclipse RCP

问题描述 投票:0回答:2

我基本上有以下几点:

<menuContribution locationURI="menu:com.myprog.menus.edit?after=undo">
<command commandId="org.eclipse.ui.edit.undo" label="Undo" style="push">
</command>
<command commandId="org.eclipse.ui.edit.redo" label="Redo" style="push">
</command>
</menuContribution>

================================

<menuContribution locationURI="toolbar:org.eclipse.ui.main.toolbar">     
     <toolbar id="com.myprog.ui.undo">
        <command commandId="org.eclipse.ui.edit.undo" label="Undo" style="push">
        </command>
        <command commandId="org.eclipse.ui.edit.redo" label="Redo" style="push">
        </command>
     </toolbar>
</menuContribution>

这是使用Eclipse 4上的兼容层运行的Eclipse 3.X RCP。

问题是,当我打开任何文本编辑器时,撤消/重做工具栏按钮不会被正确启用或禁用,除非我使用鼠标选择多行或当我点击任何其他选项卡(例如项目浏览器)和然后单击返回编辑器。

知道这种情况发生了,我希望编辑菜单撤消/重做也搞砸了,但我发现它们根据我在编辑器中的操作正确启用和禁用。

另外需要注意的是,无论工具栏撤消/重做按钮的状态如何,键盘快捷键Ctrl + Z和Ctrl + Y都能按预期工作。

什么可能导致这个?让工具栏按钮不起作用是如此奇怪。如果所有撤消/重做机制都不起作用,我会感觉更好。

eclipse eclipse-rcp e4
2个回答
2
投票

对于菜单项,Eclipse会在每次显示菜单时检查启用,但是对于工具栏,您必须告知Eclipse何时更新启用。

您可以使用事件代理执行此操作。在3.x兼容性RCP中使用:

IEventBroker eventBroker = (IEventBroker)PlatformUI.getWorkbench().getService(IEventBroker.class);

eventBroker.send(UIEvents.REQUEST_ENABLEMENT_UPDATE_TOPIC, UIEvents.ALL_ELEMENT_ID);

0
投票

使用greg-449的答案:

IEventBroker eventBroker =(IEventBroker)PlatformUI.getWorkbench()。getService(IEventBroker.class);

eventBroker.send(UIEvents.REQUEST_ENABLEMENT_UPDATE_TOPIC,UIEvents.ALL_ELEMENT_ID);

我通过执行以下操作在documentChanged监听器中使用它:

    try {
       TextEditor editor = (TextEditor)((IEditorReference) editorReference).getEditor(false);
       if(editor != null) {
          editor.getDocumentProvider().getDocument(editor.getEditorInput()).addDocumentListener(new IDocumentListener() {
             @Override
             public void documentChanged(DocumentEvent event) {
                IEventBroker eventBroker = (IEventBroker)PlatformUI.getWorkbench().getService(IEventBroker.class);
                eventBroker.post(UIEvents.REQUEST_ENABLEMENT_UPDATE_TOPIC, UIEvents.ALL_ELEMENT_ID);
             }

             @Override
             public void documentAboutToBeChanged(DocumentEvent event) {
             }
          });
       }  
    }
    catch (ClassCastException e) {
    }

然而,我遇到了一个问题。问题是,如果编辑只是一个字符,则撤消按钮不会启用。然而,如果编辑超出一个字符,它可以完美地工作。

进一步调试,我注意到另一个编辑器,我没有实现并且undo / redo正常工作,调用堆栈从selectionChanged事件开始,而不是documentChanged

我在我的问题中提到,如果我使用鼠标选择多行,则撤消/重做将启用,这显然是selectionChanged

所以基本上,我已经找到了这个解决方案:

       try {
           final TextEditor editor = (TextEditor)((IEditorReference) partRef).getEditor(false);
           editor.getDocumentProvider().getDocument(editor.getEditorInput()).addDocumentListener(new IDocumentListener() {

              @Override
              public void documentChanged(DocumentEvent event) {
                 editor.getSelectionProvider().setSelection(editor.getSelectionProvider().getSelection());
              }

              @Override
              public void documentAboutToBeChanged(DocumentEvent event) {
              }
           });
        }
        catch (ClassCastException e) {
        }

我现在正在做的基本上是触发selectionChanged事件,它从一开始就正确地更新了undo / redo。

© www.soinside.com 2019 - 2024. All rights reserved.