Abstract NonExistingFileEditorInput changes between Eclipse 3.1 and Eclipse 3.2

From Aptana Development

This is definitely not recommended as a long-term solution, but in a pinch, this helps manage the difference between Eclipse 3.1 and Eclipse 3.2. Eclipse 3.2 introduced the concept of IFileStorage, which abstracts file systems. This is great, except they changed the constructor on NonExistingFileEditorInput, which means you need to build a version for 3.1 and one for 3.2. With reflection, we make that easy:

static
{
   String version = System.getProperty("osgi.framework.version"); //$NON-NLS-1$
   if (version != null && version.startsWith("3.2")) //$NON-NLS-1$
   {
       inEclipse32 = true;
   }
}
/**
 * Creates a new NonExistingFileEditorInput, abstracting the difference in constructors between Eclipse 3.1 and Eclipse 3.2
 * @param file the file to create
 * @param fileName the name to give the file
 * @return a new NonExistingFileEditorInput
 */
public static IEditorInput createNonExistingFileEditorInput(File file, String fileName)
{
   IEditorInput input = null;
   try
   {
       if(inEclipse32)
       {
           BundleClassLoader bcl = new BundleClassLoader();
           Bundle bundle = Platform.getBundle("org.eclipse.core.filesystem");
           bcl.addBundle(bundle);
           Class efs = bcl.loadClass("org.eclipse.core.filesystem.EFS");
           Method getLocalFileSystem = efs.getDeclaredMethod("getLocalFileSystem", new Class[] {});
           Object fs = getLocalFileSystem.invoke(null, new Object[] {});

           Class localFileSystemClass = bcl.loadClass("org.eclipse.core.filesystem.IFileSystem");
           Method fromLocalFile = localFileSystemClass.getDeclaredMethod("fromLocalFile", new Class[] {File.class});
           Object localFile = fromLocalFile.invoke(fs, new Object[] {file});
           
           Class iFileStoreClass = bcl.loadClass("org.eclipse.core.filesystem.IFileStore");

           Class jfei = Class.forName("org.eclipse.ui.internal.editors.text.NonExistingFileEditorInput"); //$NON-NLS-1$
           Constructor constructor = jfei.getDeclaredConstructor(new Class[] {iFileStoreClass, String.class});
           input = (IEditorInput)constructor.newInstance(new Object[] {localFile, fileName});
       }
       else
       {
           Class jfei = Class.forName("org.eclipse.ui.internal.editors.text.NonExistingFileEditorInput"); //$NON-NLS-1$
           Constructor constructor = jfei.getDeclaredConstructor(new Class[] {File.class, String.class});
           input = (IEditorInput)constructor.newInstance(new Object[] {file, fileName});
       }
   }
   catch(Exception e)
   {
       IdeLog.logError(AptanaCorePlugin.getDefault(), "Unable to create new NonExistingFileEditorInput", e);
   }
   return input;
}

Aptana Developer Home