/** {@inheritDoc} */
 @Override
 public void onDirtyChanged() {
   for (PreferencePagePresenter p : preferences) {
     if (p.isDirty()) {
       view.enableSaveButton(true);
       return;
     }
   }
   view.enableSaveButton(false);
 }
  @Override
  public void onSaveClicked() {
    for (PreferencePagePresenter preference : preferences) {
      if (preference.isDirty()) {
        preference.storeChanges();
      }
    }

    Promise<Void> promise = Promises.resolve(null);
    final List<PromiseError> promiseErrorList = new ArrayList<>();

    for (final PreferencesManager preferencesManager : managers) {
      promise =
          promise.thenPromise(
              new Function<Void, Promise<Void>>() {
                @Override
                public Promise<Void> apply(Void arg) throws FunctionException {
                  return preferencesManager
                      .flushPreferences()
                      .catchError(
                          new Operation<PromiseError>() {
                            @Override
                            public void apply(PromiseError error) throws OperationException {
                              notificationManagerProvider
                                  .get()
                                  .notify(
                                      locale.unableToSavePreference(),
                                      error.getMessage(),
                                      FAIL,
                                      true);
                              promiseErrorList.add(error);
                            }
                          });
                }
              });
    }

    promise.then(
        new Operation<Void>() {
          @Override
          public void apply(Void aVoid) throws OperationException {
            if (promiseErrorList.isEmpty()) {
              view.enableSaveButton(false);
            }
          }
        });
  }
 /** {@inheritDoc} */
 @Override
 public void onCloseClicked() {
   boolean haveUnsavedData = false;
   for (PreferencePagePresenter preference : preferences) {
     if (preference.isDirty()) {
       haveUnsavedData = true;
     }
   }
   if (haveUnsavedData) {
     dialogFactory
         .createConfirmDialog(
             "", locale.messagesPromptSaveChanges(), getConfirmCallback(), getCancelCallback())
         .show();
   } else {
     view.close();
   }
   view.enableSaveButton(false);
 }
 /**
  * Create presenter.
  *
  * <p>For tests.
  *
  * @param view
  * @param preferences
  * @param dialogFactory
  * @param locale
  * @param managers
  */
 @Inject
 protected PreferencesPresenter(
     PreferencesView view,
     Set<PreferencePagePresenter> preferences,
     DialogFactory dialogFactory,
     CoreLocalizationConstant locale,
     Set<PreferencesManager> managers,
     Provider<NotificationManager> notificationManagerProvider) {
   this.view = view;
   this.preferences = preferences;
   this.dialogFactory = dialogFactory;
   this.locale = locale;
   this.managers = managers;
   this.notificationManagerProvider = notificationManagerProvider;
   this.view.setDelegate(this);
   for (PreferencePagePresenter preference : preferences) {
     preference.setUpdateDelegate(this);
   }
 }
  /** Shows preferences. */
  public void showPreferences() {
    if (preferencesMap != null) {
      view.show();
      return;
    }

    preferencesMap = new HashMap<>();
    for (PreferencePagePresenter preference : preferences) {
      Set<PreferencePagePresenter> prefsList = preferencesMap.get(preference.getCategory());
      if (prefsList == null) {
        prefsList = new HashSet<PreferencePagePresenter>();
        preferencesMap.put(preference.getCategory(), prefsList);
      }

      prefsList.add(preference);
    }
    view.setPreferences(preferencesMap);

    view.show();
    view.enableSaveButton(false);
    view.selectPreference(preferencesMap.entrySet().iterator().next().getValue().iterator().next());
  }
 /** {@inheritDoc} */
 @Override
 public void onPreferenceSelected(PreferencePagePresenter preference) {
   preference.go(view.getContentPanel());
 }