|
|
|
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 |
Missing name Mising name 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);
}
}
|
|
|
Powered by
FUDForum. Page generated in 0.02723 seconds