Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6656547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:39:25+00:00 2026-05-26T01:39:25+00:00

I’m writing an Eclipse RCP and I want to ask the user whether to

  • 0

I’m writing an Eclipse RCP and I want to ask the user whether to backup the database when the application is closed. Doing it from the File>Exit menu was easy as I defined a command exit:

public class ExitCommand extends AbstractHandler implements IHandler {

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    final IWorkbench workbench = PlatformUI.getWorkbench();
    if (workbench == null)
        return null;

    // Ask whether the user wants to back up the information
    Shell shell = new Shell(workbench.getDisplay());
    MessageBox messageBox = new MessageBox(shell, SWT.ICON_QUESTION
            | SWT.YES | SWT.NO);
    messageBox.setMessage("You are leaving CatSysPD. Do you want to make a backup of the DataBase? (recommended)");
    messageBox.setText("On Exit Backup");
    int response = messageBox.open();
    if (response == SWT.YES){
        new BackupDataBaseAction(shell);
    }

    final Display display = workbench.getDisplay();
    display.syncExec(new Runnable() {
        public void run() {
            if (!display.isDisposed())
                workbench.close();
        }
    });
    return null;
}}

I then linked this to a menu entry called Exit and this work right. However the user could close the application also by pressing the “close window” button. Is there any way of catching this event?

I found a suggestion in a previous topic (see here) using a shutdownHook. However the thread that I want execute has to open a dialog and, as I understand, this cannot be done by an external thread.

Thank you!

Edit
I add here the code for the shutdownHook I’m using. In the Application class:
public class Application implements IApplication {

final double NIDAQmxPortingVersionDependency = 1.001;

public final static String PLUGIN_ID = "CatsysPD";
private static Logger logger = Logger.getLogger(Application.class
        .toString());

/*
 * (non-Javadoc)
 * 
 * @see org.eclipse.equinox.app.IApplication#start(org.eclipse.equinox.app.
 * IApplicationContext)
 */
public Object start(IApplicationContext context) {
    logger.info("Starting the application");
    Display display = PlatformUI.createDisplay();
    systemCheck(display);
    initializeApplication(display);
    try {
        int returnCode = PlatformUI.createAndRunWorkbench(display,
                new ApplicationWorkbenchAdvisor());
        if (returnCode == PlatformUI.RETURN_RESTART) {
            return IApplication.EXIT_RESTART;
        }
        BackupOnExitHook backupOnExitHook = new BackupOnExitHook(PlatformUI.getWorkbench().getDisplay());
        Runtime.getRuntime().addShutdownHook(backupOnExitHook);
        return IApplication.EXIT_OK;
    } finally {
        display.dispose();
    }
}

private void systemCheck(Display display) {...}

public void stop() {...}

public void initializeApplication(Display display) {...}

private class BackupOnExitHook extends Thread {

    private Display display;

    public BackupOnExitHook(Display display){
        this.display = display;
    }

    @Override
    public void run(){
        display.syncExec(new Runnable(){

            @Override
            public void run() {
                MessageBox messageBox = new MessageBox(new Shell(display), SWT.ICON_QUESTION
                        | SWT.YES | SWT.NO);
                messageBox.setMessage("You are leaving CatSysPD. Do you want to make a backup of the DataBase? (recommended)");
                messageBox.setText("On Exit Backup");
                int response = messageBox.open();
                if (response == SWT.YES){
                    new BackupDataBaseAction(new Shell(display));
                }
            }});


    }

}
}

The error I get when I try to run it is:

Exception in thread "Thread-5" org.eclipse.swt.SWTException: Device is disposed
at org.eclipse.swt.SWT.error(SWT.java:4083)
at org.eclipse.swt.SWT.error(SWT.java:3998)
at org.eclipse.swt.SWT.error(SWT.java:3969)
at org.eclipse.swt.widgets.Display.error(Display.java:1249)
at org.eclipse.swt.widgets.Display.syncExec(Display.java:4581)
at dk.catsys.pd.Application$BackupOnExitHook.run(Application.java:128)

Thanks again.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T01:39:25+00:00Added an answer on May 26, 2026 at 1:39 am

    How about preWindowShellClose from WorkbenchWIndowAdvisor?

    http://help.eclipse.org/helios/topic/org.eclipse.platform.doc.isv/reference/api/org/eclipse/ui/application/WorkbenchWindowAdvisor.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want use html5's new tag to play a wav file (currently only supported
i want to parse a xhtml file and display in UITableView. what is the
I have a reasonable size flat file database of text documents mostly saved in
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.