J Smith
J Smith

Reputation: 2405

How can I tell if an SWT Shell was closed programmatically or by the user?

I know I can use this code to tell when the shell is closed,

shell.addShellListener(new ShellAdapter()
{
    @Override
    public void shellClosed(ShellEvent e)
    {
        System.out.println("closed");
    }
}

But the ShellEvent object doesn't tell me whether the Shell was closed programmatically or when the user clicked on the X button.

Is there a way to tell?

Upvotes: 1

Views: 717

Answers (1)

Niranjan
Niranjan

Reputation: 1834

I spent some time to distinguish if the Close ShellEvent is generated by User or System.

After inspecting the ShelEvent on both the cases the only variable with a different value through out the ObjectGraph of ShellEvent is captureChanged in the Display class whose scope is default

The below code should help you find the source of the ShellEvent

shell.addShellListener(new ShellAdapter() {
        @Override
        public void shellClosed(ShellEvent e) {

                Field f = Display.class.getDeclaredField("captureChanged");
                f.setAccessible(true);
                System.out.println("captureChanged = " + f.get(e.display)); //true = If User triggered the Event
                System.out.println("closed");
        }
    });

Upvotes: 2

Related Questions