Domanda

Sto cercando di creare un nuovo file in un plugin eclipse. Non è necessariamente un file Java, ad esempio può essere un file HTML.

In questo momento sto facendo questo:

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);

Il file viene creato, ma il problema è che non viene riconosciuto come alcun tipo; Non riesco ad aprirlo in nessun editor interno. Questo finché non riavvio Eclipse (aggiorna o chiudi quindi apri il progetto non aiuta). Dopo un riavvio, il file è perfettamente utilizzabile e si apre nell'editor predefinito corretto per il suo tipo.

Esiste un metodo che devo chiamare per ottenere il file al di fuori di quel "limbo" Stato?

È stato utile?

Soluzione

Che thread menziona il createFile , ma si riferisce anche a un FileEditorInput per aprirlo:

  

Invece di java.io.File , dovresti usare IFile.create (..) o IFile.createLink (..) . Dovrai prima ottenere un handle IFile dal progetto usando IProject.getFile (..) , quindi crea il file usando quell'handle.
  Una volta creato il file, puoi creare FileEditorInput da esso e utilizzare IWorkbenchPage.openEditor (..) per aprire il file in un editor.

Ora, quel tipo di metodo (da questo AbstractExampleInstallerWizard ) essere di aiuto in questo caso?

  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);
  }  

Vedi anche questo SDOModelWizard apertura di un editor su un nuovo 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;
  }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top