2011-10-05 14 views
6

Bir Eclipse RCP yazıyorum ve kullanıcıya uygulama kapatıldığında veritabanını yedekleyip yedeklememesini istiyorum.Çalışma tezgahı penceresinin Java RCP uygulamasında kapatılmasını sağlayın

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; 
}} 

Sonra bir menü girdisi denilen Çık bu bağlantılı olan ve bu çalışma hakkı: Bir komut çıkış tanımlanan Dosyadan bunu yapmak> Çıkış menü kolaydı. Ancak kullanıcı, uygulamayı "kapat penceresi" düğmesine basarak da kapatabilir. Bu olayı yakalamanın bir yolu var mı?

Bir stopHook kullanarak önceki bir konuda (see here) bir öneri buldum. Ancak, yürütmek istediğim iş parçacığı bir iletişim kutusu açmalı ve anladığım kadarıyla, bu bir dış iş parçacığı tarafından gerçekleştirilemez.

Teşekkür ederiz!

Düzenle Burada kullanıyorum shutdownHook kodunu ekleyin. Uygulama sınıfında: public class Uygulama IApplication uygular {

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)); 
       } 
      }}); 


    } 

} 
} 

bunu çalıştırmayı denediğimde olsun hatadır:

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) 

Tekrar teşekkürler.

cevap

İlgili konular