Closing an Editor

Hi,

I'm trying to close an editor from a project-component plugin using the following code:

FileEditorManager fileEditorManager = FileEditorManager.getInstance(project);
FileDocumentManager fileDocManager = FileDocumentManager.getInstance();
VirtualFile vf = fileDocManager.getFile(editor.getDocument());
fileEditorManager.closeFile(vf);

where project is passed into the plugin as a constructor argument and editor is passed into the method.

This results in a NullPointerException:
at com.intellij.ide.palette.impl.PaletteManager.a(PaletteManager.java:47)
at com.intellij.ide.palette.impl.PaletteManager.access$400(PaletteManager.java:33)
at com.intellij.ide.palette.impl.PaletteManager$MyFileEditorManagerListener.fileClosed(PaletteManager.java:3)

Any ideas?

This is my first plugin, so I could easily be missing the plot here!

Thanks,
Steve

5 comments

Your code is basically the same code I use. I've never seen anything with the PaletteManager before. There's very little info in the OpenAPI info on the ide.palette package. My guess it has to do with the UI designer. Did you get this trying to close a form?

0

This was trying to close Java and XML files, although in fact any editor may be closed (in theory, anyway :) ). The idea for the plugin is that it lets you close all editors of a certain type, e.g. all open Java files.

The relevant code is attached, snippets below:

public void closeEditorsOfType(String type,
DataContext dataContext)
{
List editors = getEditorsForType(type); FileEditorManager fileEditorManager = FileEditorManager.getInstance(project); FileDocumentManager fileDocManager = FileDocumentManager.getInstance(); for (Editor editor : editors) { VirtualFile vf = fileDocManager.getFile(editor.getDocument()); fileEditorManager.closeFile(vf); } } private List getEditorsForType(@NonNls String type) { List matchingEditors = new ArrayList]]>();
Editor[] editors = EditorFactory.getInstance().getAllEditors();
FileDocumentManager fileDocManager = FileDocumentManager.getInstance();
for (Editor editor : editors)
{
VirtualFile vf = fileDocManager.getFile(editor.getDocument());
if (type.equalsIgnoreCase(vf.getExtension()))
{
matchingEditors.add(editor);
}
}
return matchingEditors;
}

The DataContext is passed in from the invoking action:
public void actionPerformed(AnActionEvent e)
{
String extension = getExtension(e.getDataContext());
if (extension != null)
{
Project project = ProjectManager.getInstance().getDefaultProject();
CloseAllXComponent closeAllXComponent = project.getComponent(CloseAllXComponent.class);
closeAllXComponent.closeEditorsOfType(extension,
e.getDataContext());
}
}

Nothing in my code refers to the palette code, but looking at the decompiled FileEditorManager, it may well be that its PaletteManager member is null. As yet I haven't been able to debug inside the idea.jar code so I can't confirm this.



Attachment(s):
CloseAllXEditorAction.java
CloseAllXAction.java
CloseAllXComponent.java
0

Hello Steve,

When exactly does this code run? It looks like the NPE can happen if you
try to close the editors before the project startup is complete. I'll fix
the NPE, but I'm not sure that you're doing the right thing anyway. :)

I'm trying to close an editor from a project-component plugin using
the following code:

FileEditorManager fileEditorManager =
FileEditorManager.getInstance(project); FileDocumentManager
fileDocManager = FileDocumentManager.getInstance(); VirtualFile vf =
fileDocManager.getFile(editor.getDocument());
fileEditorManager.closeFile(vf);

where project is passed into the plugin as a constructor argument and
editor is passed into the method.

This results in a NullPointerException:

at
com.intellij.ide.palette.impl.PaletteManager.a(PaletteManager.java:47)

at
com.intellij.ide.palette.impl.PaletteManager.access$400(PaletteManager
.java:33)

at
com.intellij.ide.palette.impl.PaletteManager$MyFileEditorManagerListen
er.fileClosed(PaletteManager.java:3)

Any ideas?

This is my first plugin, so I could easily be missing the plot here!

--
Dmitry Jemerov
Software Developer
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0

Hi,

the code can be run on two occasions:
1. From the Window menu - this action will pop up a list of all the file types currently open. Selecting one closes all files of that type.
2. From the Editor Tab menu - this action will close all files of the type in the clicked-on tab.

The NPE I reported is occuring when the project has been open for a few seconds.

- Steve

0

As predicted, I was missing the plot. I was getting hold of the default project instead of the project in the data context. Changed the code, works fine.

Thanks to all who helped.

- Steve

0

Please sign in to leave a comment.