Eclipse的编辑器查看的相互作用(4)

来源:互联网 发布:淘宝红马甲是什么样子 编辑:程序博客网 时间:2024/06/13 10:57

 1.9。创建一个命令调用编辑器
  创建一个命令“rcp.intro.editor.callEditor“默认为handler,“rcp.intro.editor.handler.CallEditor”。
  创建下面的类“rcp.intro.editor.handler.CallEditor”。
    package rcp.intro.editor.handler;
   
    import org.eclipse.core.commands.AbstractHandler;
    import org.eclipse.core.commands.ExecutionEvent;
    import org.eclipse.core.commands.ExecutionException;
    import org.eclipse.core.commands.IHandler;
    import org.eclipse.jface.viewers.ISelection;
    import org.eclipse.jface.viewers.IStructuredSelection;
    import org.eclipse.ui.IWorkbenchPage;
    import org.eclipse.ui.IWorkbenchWindow;
    import org.eclipse.ui.PartInitException;
    import org.eclipse.ui.handlers.HandlerUtil;
   
    import rcp.intro.editor.View;
    import rcp.intro.editor.editors.MyPersonEditor;
    import rcp.intro.editor.editors.MyPersonEditorInput;
    import rcp.intro.editor.model.Person;
   
    public class CallEditor extends AbstractHandler implements IHandler {
   
     @Override
     public Object execute(ExecutionEvent event) throws ExecutionException {
      // Get the view
      IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event);
      IWorkbenchPage page = window.getActivePage();
      View view = (View) page.findView(View.ID);
      // Get the selection
      ISelection selection = view.getSite().getSelectionProvider()
        .getSelection();
      if (selection != null && selection instanceof IStructuredSelection) {
       Object obj = ((IStructuredSelection) selection).getFirstElement();
       // If we had a selection lets open the editor
       if (obj != null) {
        Person person = (Person) obj;
        MyPersonEditorInput input = new MyPersonEditorInput(person);
        try {
         page.openEditor(input, MyPersonEditor.ID);
   
        } catch (PartInitException e) {
         System.out.println(e.getStackTrace());
        }
       }
      }
      return null;
     }
   
    }

1.10. 调用编辑器
  在你的视图器添加双击监听,来调用编辑器。
    package rcp.intro.editor;
   
    import org.eclipse.jface.viewers.DoubleClickEvent;
    import org.eclipse.jface.viewers.IDoubleClickListener;
    import org.eclipse.jface.viewers.TableViewer;
    import org.eclipse.swt.SWT;
    import org.eclipse.swt.widgets.Composite;
    import org.eclipse.ui.handlers.IHandlerService;
    import org.eclipse.ui.part.ViewPart;
   
    import rcp.intro.editor.provider.MyContentProvider;
    import rcp.intro.editor.provider.MyLabelProvider;
   
    public class View extends ViewPart {
     public static final String ID = "rcp.intro.editor.view";
   
     private TableViewer viewer;
   
     /**
      * This is a callback that will allow us to create the viewer and initialize
      * it.
      */
     public void createPartControl(Composite parent) {
      viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL
        | SWT.V_SCROLL);
      viewer.setContentProvider(new MyContentProvider(viewer));
      viewer.setLabelProvider(new MyLabelProvider());
      viewer.setInput(new MyModel());
      getSite().setSelectionProvider(viewer);
      // New
      hookDoubleClickCommand();
     }
   
     // New
     private void hookDoubleClickCommand() {
      viewer.addDoubleClickListener(new IDoubleClickListener() {
       public void doubleClick(DoubleClickEvent event) {
        IHandlerService handlerService = (IHandlerService) getSite()
          .getService(IHandlerService.class);
        try {
         handlerService.executeCommand(
           "rcp.intro.editor.callEditor", null);
        } catch (Exception ex) {
         throw new RuntimeException(
           "rcp.intro.editor.callEditor not found");
        }
       }
      });
     }
   
     /**
      * Passing the focus request to the viewer's control.
      */
     public void setFocus() {
      viewer.getControl().setFocus();
     }
    }
   
  运行应用程序。如果你双击一个项目单击视图,编辑器应该打开,内容应该得到显示。如果您关闭编辑器并重新打开它,在地址内容更改应显示。 

 

(版权归原作者所有)

原创粉丝点击