Search code examples
javaeclipsepreference

Calling Restart workbench code only once


I created a preference page with multiple fields. On preference store I added propertychangelistener when contains code to restart application.

getPreferenceStore().addPropertyChangeListener(new IPropertyChangeListener() {

      @Override
      public void propertyChange(PropertyChangeEvent event) {
        if(MessageDialog.openConfirm(shell,"Restart Required", "Restart now?"))
          PlatformUI.getWorkbench().restart();
      }
    });

Problem is when I change multiple entries in preference page this restart code will be called that many times. What condition should I put so that even after changing multiple entries this restart will be called once only.


Solution

  • Just set a flag in the property change listener and do the restart prompt in the performOk method of the page.

    // Field in the page:
    boolean restartRequired = false;
    
    
    getPreferenceStore().addPropertyChangeListener(new IPropertyChangeListener() {
    
      @Override
      public void propertyChange(PropertyChangeEvent event) {
        restartRequired = true;
      }
    });
    
    
    @Override
    public boolean performOk()
    { 
      if (restartRequired) {
        ... prompt / restart
      }
      return super.performOk();
    }