|
Re: Prevent part from closing [message #1052642 is a reply to message #1052541] |
Tue, 30 April 2013 10:41 |
Eclipse User |
|
|
|
In the part's context there is an ISaveHandler which you can override and provide the close-no close behavior. IIRC the default implementation asks to save if the part has its dirty flag on.
|
|
|
Re: Prevent part from closing [message #1052691 is a reply to message #1052642] |
Tue, 30 April 2013 12:07 |
Bastian Wagenfeld Messages: 183 Registered: January 2013 |
Senior Member |
|
|
Hi,
thanks for that, but I don't fully understand that approach. I created a class that implements the ISaveHandler interface and I set it as the ISaveHandler in the part's context.
First of all, the class isn't called, but the standard SaveDialog is opened, if it is dirty. Do I have to register it somewhere else, too?
Also, the part is marked as dirty (with the *). This indicates that there is unsaved content, but that isn't exactly what this is about. So it would be nice, if I could remove this *. Is there a way to do that?
Thank you!
Bastian
|
|
|
|
Re: Prevent part from closing [message #1060418 is a reply to message #1053730] |
Fri, 24 May 2013 14:36 |
Patrick Rusk Messages: 35 Registered: June 2012 |
Member |
|
|
Andrzej Szczepanski wrote on Tue, 07 May 2013 08:15Bastian Wagenfeld wrote on Tue, 30 April 2013 14:07 I created a class that implements the ISaveHandler interface and I set it as the ISaveHandler in the part's context.
First of all, the class isn't called, but the standard SaveDialog is opened, if it is dirty. Do I have to register it somewhere else, too?
It'll work if you set it in MWindow's context. Obviously, that means that it will then work for all parts, not only the one you are interested in (unless you swap ISaveHandler each time focus changes or something).
Andrzej is correct there. I was able to implement this with this code (slightly edited to remove non-essential stuff):
@PostConstruct
public void createPartControl(Composite parent, MPart part, MWindow window) {
* * *
ISaveHandler saveHandler = new ISaveHandler() {
@Override
public Save[] promptToSave(Collection<MPart> arg0) {
return null; // I don't have any situations where this could happen
}
@Override
public Save promptToSave(MPart part) {
if (part.isDirty()) {
boolean confirm = MessageDialog.openConfirm(shell, "Lose changes?",
"Closing this editor will lose any unsaved changes.\n" +
"Are you sure you want to do that?");
return (confirm ? Save.NO : Save.CANCEL);
} else {
return Save.NO; // Mine don't actually save
}
}
};
window.getContext().set(ISaveHandler.class, saveHandler);
IWindowCloseHandler handler = new IWindowCloseHandler() {
@Override
public boolean close(MWindow window) {
if (!partService.getDirtyParts().isEmpty()) {
return MessageDialog.openConfirm(shell, "Lose changes?",
"Closing this window will lose any unsaved changes in editors.\n" +
"Are you sure you want to do that?");
} else {
return true;
}
}
};
window.getContext().set(IWindowCloseHandler.class, handler);
Hope that helps!
Patrick
|
|
|
|
Powered by
FUDForum. Page generated in 0.04681 seconds