问题
we have the following problem:
In our Eclipse RCP 4 application there are multiple parts and the parts are closable. When the user is closing a part there should be a custom pop-up (depending on some internal part state) which is asking the user if he really wants to close the part or not. It seems to be not that easy to implement in Eclipse RCP 4 or we have just totally overseen something. I'll just give you a short brieifing about the things we tried:
- Use dirtable with a @persist method in the part. Though the problem is, we don't want this standard eclipse save dialog. So is there a way to override this?
- public int promptToSaveOnClose(): This seemed to be promising but not for Eclipse 4 or is there a way to integrate it that way? Compare: http://e-rcp.blogspot.de/2007/09/prevent-that-rcp-editor-is-closed.html
Our last try was to integrate a custom part listener, simple example shown in the following:
partService.addPartListener(new IPartListener() { public void partVisible(MPart part) { } public void partHidden(MPart part) { partService.showPart(part, PartState.ACTIVATE); } public void partDeactivated(MPart part) { } public void partBroughtToTop(MPart part) { } public void partActivated(MPart part) { } });
The problem with this was we are running into a continuous loop. Something similar is posted over here in the last comment: Detect tab close in Eclipse editor
So I could write some more about this problem, but I think that's enough for the moment. If you need some more input just give me a hint. Thanks for helping.
回答1:
The save prompt is generated by the ISaveHandler
registered in the context of the MWindow
containing the MPart
. You can write your own ISaveHandler
and set it in the window context to replace the default.
You might also want to look at the IWindowCloseHandler
also in the window context.
回答2:
Thanks greg, this has helped and I was able to achieve changing the pop-up when the user closes a part. Here's a short description of what I've done:
- Use the MDirtyable for marking the part as dirty whenever it's needed.
- Create a custom save handler which implements ISaveHandler (when a part got closed the save method is called). Add the additional logic to this handler (e.g. a custom message dialog)
Register this handler at application start-up (I just chose a method which is called at the start-up):
@Inject private MWindow window; ... ISaveHandler saveHandler = new CustomSaveHandler(shell); window.getContext().set(ISaveHandler.class, saveHandler);
Note that the registration via a model processor was sadly not that easy because the model processor is called too early. (Take a look at: http://www.eclipse.org/forums/index.php/t/369989/)
The IWindowCloseHandler is just needed when the complete window is closed, though this was not an requirement for us :).
来源:https://stackoverflow.com/questions/22156920/custom-message-when-closing-a-part-in-eclipse-rcp-4