/*
 * Copyright 2000-2009 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.openapi.options;

import com.intellij.openapi.Disposable;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.util.Factory;
import com.intellij.openapi.util.Key;
import com.intellij.ui.UserActivityListener;
import com.intellij.ui.UserActivityWatcher;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.ui.UIUtil;
import com.intellij.util.ui.update.MergingUpdateQueue;
import com.intellij.util.ui.update.Update;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.util.List;

/**
 * This class presents an abstraction of user interface transactional editor provider of some abstract data type.
 * {@link #getComponent()} should be called before {@link #resetFrom(Object)}
 */
public abstract class SettingsEditor<Settings> implements Disposable {
  public static final Key<Boolean> MERGE_EVENTS = Key.create("MERGE_EVENTS");

  private final List<SettingsEditorListener<Settings>> myListeners = ContainerUtil.createLockFreeCopyOnWriteList();
  private UserActivityWatcher myWatcher;
  private boolean myIsInUpdate = false;
  private final Factory<Settings> mySettingsFactory;
  private CompositeSettingsEditor<Settings> myOwner;
  private JComponent myEditorComponent;
  private MergingUpdateQueue myUpdateQueue;

  protected abstract void resetEditorFrom(@NotNull Settings s);
  protected abstract void applyEditorTo(@NotNull Settings s) throws ConfigurationException;

  @NotNull
  protected abstract JComponent createEditor();

  protected void disposeEditor() {
  }

  public SettingsEditor() {
    this(null);
  }

  public SettingsEditor(Factory<Settings> settingsFactory) {
    mySettingsFactory = settingsFactory;
    Disposer.register(this, new Disposable() {
      @Override
      public void dispose() {
        disposeEditor();
        uninstallWatcher();
      }
    });
  }

  public Settings getSnapshot() throws ConfigurationException {
    if (myOwner != null) return myOwner.getSnapshot();

    Settings settings = mySettingsFactory.create();
    applyTo(settings);
    return settings;
  }

  final void setOwner(CompositeSettingsEditor<Settings> owner) {
    myOwner = owner;
  }

  public final CompositeSettingsEditor<Settings> getOwner() {
    return myOwner;
  }

  public Factory<Settings> getFactory() {
    return mySettingsFactory;
  }

  public final void resetFrom(Settings s) {
    bulkUpdate(() -> resetEditorFrom(s));
  }

  public final void bulkUpdate(Runnable runnable) {
    boolean wasInUpdate = myIsInUpdate;
    try {
      myIsInUpdate = true;
      runnable.run();
    }
    finally {
      myIsInUpdate = wasInUpdate;
    }
    fireEditorStateChanged();
  }

  public final void applyTo(Settings s) throws ConfigurationException {
    applyEditorTo(s);
  }

  public final JComponent getComponent() {
    if (myEditorComponent == null) {
      myEditorComponent = createEditor();
      installWatcher(myEditorComponent);
    }
    return myEditorComponent;
  }

  @Override
  public final void dispose() {
  }

  protected void uninstallWatcher() {
    myWatcher = null;
  }

  protected void installWatcher(JComponent c) {
    final Update myUpdate;
    if (UIUtil.isClientPropertyTrue(c, MERGE_EVENTS)) {
      myUpdateQueue = new MergingUpdateQueue("SettingsEditor.fireEditorStateChanged()", 100, true, MergingUpdateQueue.ANY_COMPONENT, this);
      myUpdate = new Update(SettingsEditor.this) {
        @Override
        public void run() {
          fireEditorStateChanged();
        }
      };
    } else {
      myUpdate = null;
    }

    myWatcher = new UserActivityWatcher();
    myWatcher.register(c);
    UserActivityListener userActivityListener = new UserActivityListener() {
      @Override
      public void stateChanged() {
        if (myUpdateQueue != null && myUpdate != null) {
          myUpdateQueue.queue(myUpdate);
        } else {
          fireEditorStateChanged();
        }
      }
    };
    myWatcher.addUserActivityListener(userActivityListener, this);
  }


  public final void addSettingsEditorListener(SettingsEditorListener<Settings> listener) {
    myListeners.add(listener);
  }

  public final void removeSettingsEditorListener(SettingsEditorListener<Settings> listener) {
    myListeners.remove(listener);
  }

  protected final void fireEditorStateChanged() {
    if (myIsInUpdate || myListeners == null) return;
    for (SettingsEditorListener listener : myListeners) {
      listener.stateChanged(this);
    }
  }
}
@@ -762,46 +762,8 @@
 ry;%0A
-import com.intellij.openapi.util.Key;%0A
 impo
@@ -799,24 +799,24 @@
 tyListener;%0A
+
 import com.i
@@ -902,142 +902,8 @@
 il;%0A
-import com.intellij.util.ui.UIUtil;%0Aimport com.intellij.util.ui.update.MergingUpdateQueue;%0Aimport com.intellij.util.ui.update.Update;%0A
 impo
@@ -1262,87 +1262,8 @@
 e %7B%0A
-  public static final Key%3CBoolean%3E MERGE_EVENTS = Key.create(%22MERGE_EVENTS%22);%0A%0A
   pr
@@ -1604,52 +1604,8 @@
 ent;
-%0A  private MergingUpdateQueue myUpdateQueue;
 %0A%0A  
@@ -3459,426 +3459,8 @@
 ) %7B%0A
-    final Update myUpdate;%0A    if (UIUtil.isClientPropertyTrue(c, MERGE_EVENTS)) %7B%0A      myUpdateQueue = new MergingUpdateQueue(%22SettingsEditor.fireEditorStateChanged()%22, 100, true, MergingUpdateQueue.ANY_COMPONENT, this);%0A      myUpdate = new Update(SettingsEditor.this) %7B%0A        @Override%0A        public void run() %7B%0A          fireEditorStateChanged();%0A        %7D%0A      %7D;%0A    %7D else %7B%0A      myUpdate = null;%0A    %7D%0A%0A
     
@@ -3657,125 +3657,8 @@
 ) %7B%0A
-        if (myUpdateQueue != null && myUpdate != null) %7B%0A          myUpdateQueue.queue(myUpdate);%0A        %7D else %7B%0A  
     
@@ -3691,18 +3691,8 @@
 ();%0A
-        %7D%0A
     
@@ -3773,14 +3773,13 @@
 s);%0A
+
   %7D%0A%0A
-%0A
   pu

Merge updates in SettingsEditor