score:1

Accepted answer

the input to an editor does not have to support adapting to ifile. the input will usually implement one or more of ifileeditorinput, ipatheditorinput, iurieditorinput and ilocationprovider.

this code will find the ifile or ipath if possible:

/**
 * get a file from the editor input if possible.
 *
 * @param input the editor input
 * @return the file or <code>null</code>
 */
public static ifile getfilefromeditorinput(final ieditorinput input)
{
  if (input == null)
    return null;

  if (input instanceof ifileeditorinput)
    return ((ifileeditorinput)input).getfile();

  final ipath path = getpathfromeditorinput(input);
  if (path == null)
    return null;

  return resourcesplugin.getworkspace().getroot().getfile(path);
}


/**
 * get the file path from the editor input.
 *
 * @param input the editor input
 * @return the path or <code>null</code>
 */
public static ipath getpathfromeditorinput(final ieditorinput input)
{
  if (input instanceof ilocationprovider)
    return ((ilocationprovider)input).getpath(input);

  if (input instanceof iurieditorinput)
   {
     final uri uri = ((iurieditorinput)input).geturi();
     if (uri != null)
      {
        final ipath path = uriutil.topath(uri);
        if (path != null)
          return path;
      }
   }

  if (input instanceof ifileeditorinput)
   {
     final ifile file = ((ifileeditorinput)input).getfile();
     if (file != null)
       return file.getlocation();
   }

  return null;
}

Related Query

More Query from same tag