SWT工具栏动作图像'看起来'在编辑器的焦点上启用

时间:2017-02-21 15:30:34

标签: java eclipse-plugin swt eclipse-rcp

请参阅附件:

enter image description here

选择编辑器标题后,将启用该操作。在将焦点更改为其他视图或选择下面的行时,将禁用此操作。 另一点需要注意的是,当工具栏中添加了多个操作时,只有第一个操作看起来已启用。请参阅:

enter image description here

我只想在Action.setChecked()上启用启用功能。 如何摆脱这种默认的启用禁用行为?

FormPage

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.editor.FormPage;
import org.eclipse.ui.forms.widgets.ScrolledForm;

public class TestEditorPage extends FormPage {

  private Action myAction;

  public TestEditorPage(final TestEditor testEditor) {
    super(testEditor, "1", "MyTitle");
  }

  @Override
  protected void createFormContent(final IManagedForm managedForm) {
    Composite composite = managedForm.getForm().getBody();
    composite.setLayout(new GridLayout());
    composite.setLayoutData(new GridData(GridData.FILL_BOTH));
    createTree(composite);
    createActions(managedForm.getForm());
  }

  private Tree createTree(final Composite composite) {
    Tree tree = new Tree(composite, SWT.MULTI | SWT.FULL_SELECTION | SWT.H_SCROLL | SWT.V_SCROLL);
    tree.setLayoutData(new GridData(GridData.FILL_BOTH));
    tree.setHeaderVisible(true);
    tree.setLinesVisible(true);
    return tree;
  }

  private void createActions(final ScrolledForm scrolledForm) {
    ImageDescriptor imgDesc =
        PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_OBJ_FOLDER);
    this.myAction = new Action("MyAction", IAction.AS_CHECK_BOX) {

      @Override
      public void run() {
        // Do some stuff
      }
    };
    this.myAction.setImageDescriptor(imgDesc);
    scrolledForm.getToolBarManager().add(this.myAction);
    scrolledForm.updateToolBar();
  }
}

FormEditor

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.forms.editor.FormEditor;

public class TestEditor extends FormEditor {

  private TestEditorPage page;

  @Override
  protected Composite createPageContainer(final Composite parent) {
    return super.createPageContainer(parent);
  }

  @Override
  protected void addPages() {
    try {
      this.page = new TestEditorPage(this);
      addPage(this.page);
    }
    catch (PartInitException e) {}
  }

  @Override
  public void doSave(final IProgressMonitor monitor) {}

  @Override
  public void doSaveAs() {}
  @Override
  public boolean isSaveAsAllowed() {
    return false;
  }
}

将此编辑器添加到编辑器扩展点:org.eclipse.ui.editors

<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
   <extension point="org.eclipse.ui.editors">
     <editor
           class="test.treecursor.handlers.TestEditor"
           default="false"
           id="test.treecursor.editor1"
           name="TestEditor">
     </editor>
   </extension>
</plugin>

1 个答案:

答案 0 :(得分:0)

FormEditor的行为方式。它的setFocus()委托与所选标签页或其控件关联的setFocus()的{​​{1}}。前一种情况适用于非形式的多页面编辑器,其中页面以IEditPart的形式添加,而后一种情况是在添加IEditPart时的(就像您的代码一样)。

似乎无法控制表单中的焦点设置,因此您可以在IFormPage中重新定义setFocus()以将其定向到TestEditor实例。我想有多种方法可以做到这一点,但我可以建议例如在TestEditorPage中将Composite的数据设置为此,然后在TestEditorPage.createFormContent()中获取它:

TestEditor.setFocus()

顺便说一句,Eclipse上有一个很老的bug,看起来很相关:https://bugs.eclipse.org/bugs/show_bug.cgi?id=311916

也遇到同样的问题,所以分享我的解决方案。