Eclipse插件:创建一个新文件 [英] Eclipse plugin: create a new file

查看:201
本文介绍了Eclipse插件:创建一个新文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在eclipse插件中创建一个新的文件。它不一定是一个Java文件,它可以是一个HTML文件,例如。



现在我这样做:

  IProject project = ...; 
IFile file = project.getFile(/ somepath / somefilename); //如file.exists()== false
String contents =Whatever;
InputStream source = new ByteArrayInputStream(contents.getBytes());
file.create(source,false,null);

该文件被创建,但问题是它不会被识别为任何类型;我无法在任何内部编辑器中打开它。那是直到我重新启动Eclipse(刷新或关闭,然后打开该项目并不帮助)。重新启动后,该文件是完全可用的,并在其类型的正确的默认编辑器中打开。



是否有任何方法需要调用以将该文件limbo状态?

解决方案

线程确实提到了 createFile 调用,而且还引用了一个 FileEditorInput 打开它:


而不是 java.io.File ,您应该使用 IFile.create(..) IFile.createLink(..)。您将需要从项目中使用 IProject.getFile(..)获得 IFile 句柄,然后创建使用该句柄的文件。

创建文件后,您可以从中创建 FileEditorInput 并使用 IWorkbenchPage.openEditor(.. )在编辑器中打开文件。


现在,这种方法(从这个< a href =http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.emf/org.eclipse.emf/plugins/org.eclipse.emf.common.ui/src/org/eclipse/ emf / common / ui / wizard / AbstractExampleInstallerWizard.java?root = Modeling_Project& view = corel =nofollow noreferrer> AbstractExampleInstallerWizard )在这种情况下?

  protected void openEditor(IFile file,String editorID)throws PartInitException 
{
IEditorRegistry editorRegistry = getWorkbench()。getEditorRegistry();
if(editorID == null || editorRegistry.findEditor(editorID)== null)
{
editorID = getWorkbench()。getEditorRegistry()。getDefaultEditor(file.getFullPath()。toString ())的getId();
}

IWorkbenchPage page = getWorkbench()。getActiveWorkbenchWindow()。getActivePage();
page.openEditor(new FileEditorInput(file),editorID,true,IWorkbenchPage.MATCH_ID);
}

另请参见 SDOModelWizard 在新的 IFile

  //在新文件上打开一个编辑器。 
//
try
{
page.openEditor
(new FileEditorInput(modelFile),
workbench.getEditorRegistry()。getDefaultEditor(modelFile.getFullPath )的ToString())的getId())。
}
catch(PartInitException异常)
{
MessageDialog.openError(workbenchWindow.getShell(),SDOEditorPlugin.INSTANCE.getString(_ UI_OpenEditorError_label),exception.getMessage()) ;
返回false;
}


I'm trying to create a new file in an eclipse plugin. It's not necessarily a Java file, it can be an HTML file for example.

Right now I'm doing this:

IProject project = ...;
IFile file = project.getFile("/somepath/somefilename"); // such as file.exists() == false
String contents = "Whatever";
InputStream source = new ByteArrayInputStream(contents.getBytes());
file.create(source, false, null);

The file gets created, but the problem is that it doesn't get recognized as any type; I can't open it in any internal editor. That's until I restart Eclipse (refresh or close then open the project doesn't help). After a restart, the file is perfectly usable and opens in the correct default editor for its type.

Is there any method I need to call to get the file outside of that "limbo" state?

解决方案

That thread does mention the createFile call, but also refers to a FileEditorInput to open it:

Instead of java.io.File, you should use IFile.create(..) or IFile.createLink(..). You will need to get an IFile handle from the project using IProject.getFile(..) first, then create the file using that handle.
Once the file is created you can create FileEditorInput from it and use IWorkbenchPage.openEditor(..) to open the file in an editor.

Now, would that kind of method (from this AbstractExampleInstallerWizard) be of any help in this case?

  protected void openEditor(IFile file, String editorID) throws PartInitException
  {
    IEditorRegistry editorRegistry = getWorkbench().getEditorRegistry();
    if (editorID == null || editorRegistry.findEditor(editorID) == null)
    {
      editorID = getWorkbench().getEditorRegistry().getDefaultEditor(file.getFullPath().toString()).getId();
    }

    IWorkbenchPage page = getWorkbench().getActiveWorkbenchWindow().getActivePage();
    page.openEditor(new FileEditorInput(file), editorID, true, IWorkbenchPage.MATCH_ID);
  }  

See also this SDOModelWizard opening an editor on a new IFile:

  // Open an editor on the new file.
  //
  try
  {
    page.openEditor
      (new FileEditorInput(modelFile),
       workbench.getEditorRegistry().getDefaultEditor(modelFile.getFullPath().toString()).getId());
  }
  catch (PartInitException exception)
  {
    MessageDialog.openError(workbenchWindow.getShell(), SDOEditorPlugin.INSTANCE.getString("_UI_OpenEditorError_label"), exception.getMessage());
    return false;
  }

这篇关于Eclipse插件:创建一个新文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆