Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » Prevent part from closing
Prevent part from closing [message #1052541] Tue, 30 April 2013 04:06 Go to next message
Bastian Wagenfeld is currently offline Bastian Wagenfeld
Messages: 159
Registered: January 2013
Senior Member
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 #1052642 is a reply to message #1052541] Tue, 30 April 2013 06:41 Go to previous messageGo to next message
Sopot Cela is currently offline Sopot Cela
Messages: 589
Registered: December 2010
Senior Member

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 08:07 Go to previous messageGo to next message
Bastian Wagenfeld is currently offline Bastian Wagenfeld
Messages: 159
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 #1053730 is a reply to message #1052691] Tue, 07 May 2013 08:15 Go to previous messageGo to next message
Andrzej Szczepanski is currently offline Andrzej Szczepanski
Messages: 16
Registered: May 2013
Junior Member
Bastian 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).

Re: Prevent part from closing [message #1060418 is a reply to message #1053730] Fri, 24 May 2013 10:36 Go to previous messageGo to next message
Patrick Rusk is currently offline Patrick Rusk
Messages: 35
Registered: June 2012
Member
Andrzej Szczepanski wrote on Tue, 07 May 2013 08:15
Bastian 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
Re: Prevent part from closing [message #1060566 is a reply to message #1060418] Mon, 27 May 2013 01:37 Go to previous message
Bastian Wagenfeld is currently offline Bastian Wagenfeld
Messages: 159
Registered: January 2013
Senior Member
Hey,

thank you. Thank worked for me, too. Great Smile

Best regards
Bastian
Previous Topic:e4 custom renderer not working
Next Topic:partService.hidePart(part, true) does not respect MDirtyable
Goto Forum:
  


Current Time: Sat Apr 19 03:02:00 EDT 2014

Powered by FUDForum. Page generated in 0.02707 seconds