Prevent part from closing [message #1052541] |
Tue, 30 April 2013 04:06  |
Eclipse User |
|
|
|
Hi,
my RCP has a PartStack where a user can add parts. These parts should be closeable. But if the user tries to close a part, he should be asked, if he really wants to close it.
My approach was to show a MessageBox in a method annotated with @PreDestroy. But how can I prevent the part from closing, if the user decides to not close the part?
Best regards
Bastian
|
|
|
|
Re: Prevent part from closing [message #1052691 is a reply to message #1052642] |
Tue, 30 April 2013 08:07   |
Eclipse User |
|
|
|
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 10:36   |
Eclipse User |
|
|
|
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.28702 seconds