Skip to main content



      Home
Home » Eclipse Projects » Eclipse Platform » Close window when file is deleted
Close window when file is deleted [message #301119] Thu, 23 March 2006 18:15 Go to next message
Eclipse UserFriend
Originally posted by: thor_bjorn.hotmail.com

Hello,

Im writing an Eclipse plugin with an MultiPageEditor. If I open a file I see
the information in my editor just fine. If I keep my file open but delete
the file from the resources view I want my editor to close automatically but
it don't. How can't I make this happen?

Many regards
Bj
Re: Close window when file is deleted [message #301168 is a reply to message #301119] Fri, 24 March 2006 07:42 Go to previous messageGo to next message
Eclipse UserFriend
Look at the way the classes in the hierarchy for TextEditor do it (I
think it's in AbstractTextEditor). Basically, you have to register an
IResourceChangeListener and close on a delete.

Later,
PW
Re: Close window when file is deleted [message #301295 is a reply to message #301168] Mon, 27 March 2006 10:18 Go to previous messageGo to next message
Eclipse UserFriend
Create a class that implements IResourceChangeListener and pass the editor
into the constructor. So e.g.

class MyResource implements IResourceChangeListener

public MyEditor editor

public MyResource (MyEditor e) {
editor = e;
}

public void resourceChanged(IResourceChangeEvent event) {
IFile file = ((FileEditorInput)editor.getEditorInput()).getFile();
LOG.debug("event.getType()" + event.getType());
if ((event.getType() == IResourceChangeEvent.PRE_DELETE
|| event.getType() == IResourceChangeEvent.PRE_CLOSE)
&& event.getResource().equals(file.getProject())) {
closeEditor();
}
}

private void closeEditor() {
Display.getDefault().syncExec(new Runnable() {
public void run() {
editor.getSite().getPage().closeEditor(editor, false);
}
//probably remove listener here
}


Then in your MyEditor class add into the init method

listener = new MyResource(this);
ResourcesPlugin.getWorkspace().addResourceChangeListener(lis tener);
Re: Close window when file is deleted [message #301388 is a reply to message #301295] Thu, 30 March 2006 01:27 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: thor_bjorn.hotmail.com

Thanks for the tip! I worked out fine.
Re: Close window when file is deleted [message #318789 is a reply to message #301119] Mon, 06 August 2007 13:56 Go to previous message
Eclipse UserFriend
Originally posted by: automatic.javalobby.org

------=_Part_949_698641844.1186423038277
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

I found the code example to be somewhat incomplete. See attached file for more complete version.
------=_Part_949_698641844.1186423038277
Content-Type: application/octet-stream; name=resourcechangelistener.java
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename=resourcechangelistener.java

public class ResourceChangeListener implements IResourceChangeListener,
IResourceDeltaVisitor
{
private IEditorPart editorPart;

public ResourceChangeListener(IEditorPart editorPart)
{
this.editorPart = editorPart;
}

public void resourceChanged(IResourceChangeEvent event)
{
IResourceDelta delta = event.getDelta();
try
{
if (delta != null)
delta.accept(this);

}

catch (CoreException exception)
{
Util.showMessageDialog(PlatformUI.getWorkbench()
.getActiveWorkbenchWindow().getShell(),
exception.getMessage(), exception.getClass().getName(),
SWT.ICON_ERROR);
}
}

private void closeEditorDoNotSave()
{
Display.getDefault().syncExec(new Runnable()
{
public void run()
{
editorPart.getSite().getPage().closeEditor(editorPart,
false);
}
});


ResourcesPlugin.getWorkspace().removeResourceChangeListener( this);
}

public boolean visit(IResourceDelta delta)
{
if (delta == null)
return true;

IEditorInput editorInput = getEditorInput();
if(editorInput instanceof FileEditorInput){

if(!delta.getResource().equals(((FileEditorInput)editorInput ).getFile())){
return true;
}
}
else
{
return true; // this is not an input type our editor handles
}

if (delta.getKind() == IResourceDelta.REMOVED)
{
if ((IResourceDelta.MOVED_TO & delta.getFlags()) == 0)
{
/*
if the file was deleted
NOTE: The case where an open, unsaved file is deleted
being handled by the PartListener added to the Workbench
in the initialize() method.
*/

if (!isDirty())
{
closeEditorDoNotSave();
}
}

else
{ // else if it was moved or renamed
final IFile newFile =
ResourcesPlugin.getWorkspace().getRoot().getFile(delta.getMo vedToPath());

Display display = getSite().getShell().getDisplay();
display.asyncExec(new Runnable()
{
public void run()
{
setInput(new FileEditorInput(newFile));
}
});
}
}
return false;
}
}
------=_Part_949_698641844.1186423038277--
Previous Topic:focusing a view prior to closing it ???
Next Topic:Editor not closing after corresponding file is deleted from navigator
Goto Forum:
  


Current Time: Thu May 08 02:33:58 EDT 2025

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

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

Back to the top