RCP的EditorPart保存后焦点控制

from:http://blog.csdn.net/bee2518/archive/2007/09/21/1795390.aspx
今天为了实现在EditorPart中保存后,提示“是否继续增加”后,能够初始化数据并实现焦点移到指定TEXT中,必须扩展org.eclipse.ui.internal.SaveAction。

仅仅在doSave事件中做txLdlpNo.setFocus();是无效到的,查看源代码可以看到执行保存的代码如下:
IRunnableWithProgress progressOp = new IRunnableWithProgress() {
   public void run(IProgressMonitor monitor) {
    IProgressMonitor monitorWrap = new EventLoopProgressMonitor(monitor);
    saveable.doSave(monitorWrap); // 此处调用EditorPart.doSave();
   }
  };



IRunnableWithProgress中无法对EditorPart的控件焦点进行控制,浏览了一下SaveAction保存事件代码,发现在EditorPart没有提供保存后控制焦点的函数,决定在扩展org.eclipse.ui.internal.SaveAction,实现保存后对焦点的控制。实现代码如下:
public class CustomSaveAction extends SaveAction {

 public CustomSaveAction(IWorkbenchWindow window) {
  super(window);
 }

 public void run() {
  super.run();

//在此处扩展保存后设置默认焦点,其他不变
  IEditorPart part = getActiveEditor();
  part.setFocus();
 }

// 不破坏原来创建,模仿 ActionFactory 创建这个CustomSaveAction

public static final ActionFactory SAVE = new ActionFactory("save") {//$NON-NLS-1$

  /*
   * (non-Javadoc)
   *
   * @see org.eclipse.ui.actions.ActionFactory#create(org.eclipse.ui.IWorkbenchWindow)
   */
  public IWorkbenchAction create(IWorkbenchWindow window) {
   if (window == null) {
    throw new IllegalArgumentException();
   }
   IWorkbenchAction action = new CustomSaveAction(window);// 这个地方改为CustomSaveAction
   action.setId(getId());
   return action;
  }
 };

}


这样,在ApplicationActionBarAdvisor中创建保存action代码如下:
saveAction = CustomSaveAction.SAVE.create(window);
  register(saveAction);



通过以上改造,实现了焦点控制

你可能感兴趣的:(eclipse,.net,UI,Blog)