eclipse中如何打开一个编辑器

eclipse中打开一个编辑器要调用

IWorkbenchPage.openEditor(IEditorInput input, String editorId)

因此要打开一个编辑器要获得3个对象,IWorkbenchPage实例、IEditorInput实例、editorId
1. 获得IWorkbenchPage实例

IWorkbenchWindow dwindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
IWorkbenchPage wbPage = dwindow.getActivePage();

2. 获得IEditorInput实例

private IProject getActiveProject() {
        IEditorPart editor = PlatformUI.getWorkbench()
                .getActiveWorkbenchWindow().getActivePage().getActiveEditor();
        if (editor.getEditorInput() instanceof FileEditorInputProxy) {
            FileEditorInputProxy fei = (FileEditorInputProxy) editor
                    .getEditorInput();
            IFile f = fei.getFile();
            return f.getProject();
        }
        return null;
    }
private IFile getFile(String fileName) {
        IProject prj = getActiveProject();
        if (prj == null)
            return null;
        try {
            IResource[] members = prj.members();
            for (IResource r : members) {
                if (r.getName().equals(fileName)) {
                    return (IFile) r;
                }
            }
        } catch (CoreException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

  3. 获得打开Editor的editorId
editor plugin.xml里面定义的editorId,如果不知道的话可以用下面代码获得:

private String getEditorID(IFile file) {

        IEditorDescriptor desc = null;
        try {
            desc = IDE.getEditorDescriptor(file);
        } catch (PartInitException e) {
            // TODO Auto-generated catch block

        }
        if (desc == null)
            return YourPlugin.getDefault().getWorkbench()
                    .getEditorRegistry().findEditor(
                            IEditorRegistry.SYSTEM_EXTERNAL_EDITOR_ID).getId();
        else
            return desc.getId();
    }

 

三个对象实例都有了,就可以调用上面第一行代码打开编辑器了!

你可能感兴趣的:(eclipse,xml,F#,ide)