Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » How to determine whether the resouce/file is opened by some editor
How to determine whether the resouce/file is opened by some editor [message #1063936] Sun, 16 June 2013 09:47 Go to next message
Missing name Mising name is currently offline Missing name Mising nameFriend
Messages: 7
Registered: July 2010
Junior Member
In my own plugin I want to know whether the given file/resouce is already opened in some editor. If yes, I want to close it before going on.
Any idea?
Re: How to determine whether the resouce/file is opened by some editor [message #1063949 is a reply to message #1063936] Sun, 16 June 2013 14:04 Go to previous messageGo to next message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1853
Registered: July 2009
Senior Member
Have a look at org.eclipse.ui.IWorkbenchPage.findEditor(IEditorInput).

If you want to ask about a given file, the editor input can be obtained as new FileEditorInput(file).

HTH,
Stephan
Re: How to determine whether the resouce/file is opened by some editor [message #1064002 is a reply to message #1063949] Mon, 17 June 2013 08:45 Go to previous messageGo to next message
Missing name Mising name is currently offline Missing name Mising nameFriend
Messages: 7
Registered: July 2010
Junior Member
It works well, thank you!
Re: How to determine whether the resouce/file is opened by some editor [message #1064008 is a reply to message #1064002] Mon, 17 June 2013 09:06 Go to previous message
Missing name Mising name is currently offline Missing name Mising nameFriend
Messages: 7
Registered: July 2010
Junior Member
Here is the full code to share

//
//Code to close editors
//

private static void closeIfOpened(IFile iFile,boolean save){
IWorkbench iWorkbench = PlatformUI.getWorkbench();
IWorkbenchWindow[] iWorkbenchWindows=iWorkbench.getWorkbenchWindows();
for (IWorkbenchWindow iWorkbenchWindow:iWorkbenchWindows){
closeIfOpened(iFile,iWorkbenchWindow,save);
}
}
private static void closeIfOpened(IFile iFile,IWorkbenchWindow iWorkbenchWindow,boolean save){
System.out.println("@@@@@@Check workbench window:"+iWorkbenchWindow);
IWorkbenchPage[] iWorkbenchPages=iWorkbenchWindow.getPages();
for (IWorkbenchPage iWorkbenchPage:iWorkbenchPages){
closeIfOpened(iFile,iWorkbenchPage,save);
}
}
private static void closeIfOpened(IFile iFile,final IWorkbenchPage iWorkbenchPage,final boolean save){
FileEditorInput input=new FileEditorInput(iFile);
IEditorPart lastEditorPart=null;
IEditorPart iEditorPart=null;
System.out.println("@@@@@@Check workbench page:"+iWorkbenchPage);
while ((iEditorPart=iWorkbenchPage.findEditor(input))!=null){
if(iEditorPart==lastEditorPart){
//Avoid infinite loop
break;
}
lastEditorPart=iEditorPart;
//
System.out.println("@@@@@@Closing editor:"+iEditorPart);
//It should run sync,otherwise the new obtained editor is same as the previous one
Display.getDefault().syncExec(new CloseEditorThread(iWorkbenchPage,save,iEditorPart));
}
}


//
//Utility class
//
class CloseEditorThread implements Runnable{
private IWorkbenchPage iWorkbenchPage=null;
boolean save=false;
IEditorPart iEditorPart=null;
public CloseEditorThread(IWorkbenchPage iWorkbenchPage,boolean save,IEditorPart iEditorPart){
this.iWorkbenchPage=iWorkbenchPage;
this.save=save;
this.iEditorPart=iEditorPart;
}
public void run() {
iWorkbenchPage.closeEditor(iEditorPart, save);
}
}
Previous Topic:Plugin - how to add package explorer to a custom perspective
Next Topic:UI Template based code formatter
Goto Forum:
  


Current Time: Thu Apr 18 02:15:10 GMT 2024

Powered by FUDForum. Page generated in 0.02332 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top