简体   繁体   中英

How to programatically open a file on Editor in eclipse?

In Java, I am trying to programatically open a file on Editor using eclipse. It is important to me that the editor is instance of 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,

@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. 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:

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.

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.

In this case in the editor the IEditorInput will be an instance of IURIEditorInput , not IFileEditorInput .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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