简体   繁体   English

如何以编程方式在Eclipse中打开编辑器上的文件?

[英]How to programatically open a file on Editor in eclipse?

In Java, I am trying to programatically open a file on Editor using eclipse. 在Java中,我试图以编程方式使用Eclipse在Editor上打开文件。 It is important to me that the editor is instance of IFileEditorInput . 对我来说,重要的是,编辑器是IFileEditorInput的实例。 I used the following code: 我使用以下代码:

            IPath path = new Path(filePath);
            System.out.println("PATH:"+path);
            IFile ifile=ResourcesPlugin.getWorkspace().getRoot().getFile(path);
            System.out.println("IFILE: "+ifile);
            //IFileEditorInput editorInput= new FileEditorInput(ifile);
            IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
            try {
                IDE.openEditor(page, ifile);
            } catch (PartInitException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

Later at some point, I need to access the file, specifically in the init() method of the Editor class, as the following, 稍后,我需要访问文件,特别是在Editor类的init()方法中,如下所示,

@Override //IEditor
    public void init(IEditorSite site, IEditorInput input) throws PartInitException {

        if (input instanceof IFileEditorInput) {
            try {

                setSite(site);
                setInput(input);    
                IFile ifile=((IFileEditorInput)input).getFile();

                File file=null;
                if(ifile.getLocation()==null)
                {
                    System.out.println("file location is NULL..exiting");
                    System.exit(0);
                }
                else
                file = ifile.getLocation().toFile();

The problem is that ifile.getLocation() always returns null, and hence, I cannot access the file using File class. 问题在于ifile.getLocation()始终返回null,因此,我无法使用File类访问文件。 What did I do wrong? 我做错了什么? Thank You. 谢谢。

EDIT: The output of my program is: 编辑:我的程序的输出是:

PATH:D:/EbticDATA/Etisalat/Zaid/.EBTIC8/Service_Corridor.xml
IFILE: L/EbticDATA/Etisalat/Zaid/.EBTIC8/Service_Corridor.xml
file location is NULL..exiting

The path you give to 你给的路

ResourcesPlugin.getWorkspace().getRoot().getFile(path);

must be a path relative to the workspace root of a file which exists in the workspace. 必须是相对于工作空间中存在的文件的工作空间根目录的路径。

You can use getFileForLocation to specify a path which is a full file system path: 您可以使用getFileForLocation指定一个路径,该路径是完整的文件系统路径:

ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path);

but this will only work if the resulting file is in the workspace. 但这仅在结果文件在工作空间中时才有效。

In general IFile will only work for files in the workspace - but you can create 'links' to files outside the workspace. 通常, IFile仅适用于工作空间中的文件-但您可以为工作空间外部的文件创建“链接”。

If you want to edit a file which is not in the workspace use the 如果要编辑不在工作空间中的文件,请使用

public static IEditorPart openEditor(IWorkbenchPage page, URI uri,
        String editorId, boolean activate)

IDE method passing in the URI of the file to edit and the editor id. IDE方法传入要编辑的文件的URI和编辑器ID。

In this case in the editor the IEditorInput will be an instance of IURIEditorInput , not IFileEditorInput . 在这种情况下,在编辑器中, IEditorInput将是IURIEditorInput的实例, 而不是 IFileEditorInput

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM