/*
 * Copyright 2000-2012 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.psi.codeStyle;

import com.intellij.openapi.components.PersistentStateComponent;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.DefaultJDOMExternalizer;
import com.intellij.openapi.util.InvalidDataException;
import com.intellij.util.xmlb.XmlSerializerUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class CodeStyleSettingsManager implements PersistentStateComponent<CodeStyleSettingsManager> {
  private static final Logger LOG = Logger.getInstance(CodeStyleSettingsManager.class);

  public volatile CodeStyleSettings PER_PROJECT_SETTINGS = null;
  public volatile boolean USE_PER_PROJECT_SETTINGS = false;
  public volatile String PREFERRED_PROJECT_CODE_STYLE = null;

  private volatile CodeStyleSettings myTemporarySettings;
  private volatile boolean myIsLoaded = false;

  public static CodeStyleSettingsManager getInstance(@Nullable Project project) {
    if (project == null || project.isDefault()) return getInstance();
    ProjectCodeStyleSettingsManager projectSettingsManager = ServiceManager.getService(project, ProjectCodeStyleSettingsManager.class);
    if (!projectSettingsManager.isLoaded()) {
      synchronized (projectSettingsManager) {
        if (!projectSettingsManager.isLoaded()) {
          LegacyCodeStyleSettingsManager legacySettingsManager = ServiceManager.getService(project, LegacyCodeStyleSettingsManager.class);
          if (legacySettingsManager != null && legacySettingsManager.getState() != null) {
            try {
              //noinspection deprecation
              DefaultJDOMExternalizer.readExternal(projectSettingsManager, legacySettingsManager.getState());
            }
            catch (InvalidDataException e) {
              LOG.error(e);
            }
            LOG.info("Imported old project code style settings.");
          }
        }
      }
    }
    return projectSettingsManager;
  }

  public static CodeStyleSettingsManager getInstance() {
    return ServiceManager.getService(AppCodeStyleSettingsManager.class);
  }

  @SuppressWarnings({"UnusedDeclaration"})
  public CodeStyleSettingsManager(Project project) {
  }
  public CodeStyleSettingsManager() {}

  @NotNull
  public static CodeStyleSettings getSettings(@Nullable final Project project) {
    return getInstance(project).getCurrentSettings();
  }

  @NotNull
  public CodeStyleSettings getCurrentSettings() {
    CodeStyleSettings temporarySettings = myTemporarySettings;
    if (temporarySettings != null) return temporarySettings;
    CodeStyleSettings projectSettings = PER_PROJECT_SETTINGS;
    if (USE_PER_PROJECT_SETTINGS && projectSettings != null) return projectSettings;
    return CodeStyleSchemes.getInstance().findPreferredScheme(PREFERRED_PROJECT_CODE_STYLE).getCodeStyleSettings();
  }

  @Override
  public CodeStyleSettingsManager getState() {
    return this;
  }

  @Override
  public void loadState(CodeStyleSettingsManager state) {
    XmlSerializerUtil.copyBean(state, this);
  }

  public CodeStyleSettings getTemporarySettings() {
    return myTemporarySettings;
  }

  public void setTemporarySettings(CodeStyleSettings settings) {
    myTemporarySettings = settings;
  }

  public void dropTemporarySettings() {
    myTemporarySettings = null;
  }

  public boolean isLoaded() {
    return myIsLoaded;
  }
}

@@ -897,24 +897,75 @@
 ternalizer;%0A
+import com.intellij.openapi.util.DifferenceFilter;%0A
 import com.i
@@ -1031,35 +1031,68 @@
 lij.
-util.xmlb.XmlSerializerUtil
+openapi.util.WriteExternalException;%0Aimport org.jdom.Element
 ;%0Aim
@@ -1253,32 +1253,15 @@
 ent%3C
-CodeStyleSettingsManager
+Element
 %3E %7B%0A
@@ -1315,16 +1315,22 @@
 nstance(
+%22#%22 + 
 CodeStyl
@@ -1351,16 +1351,26 @@
 er.class
+.getName()
 );%0A%0A  pu
@@ -1552,17 +1552,16 @@
 = null;%0A
-%0A
   privat
@@ -2334,106 +2334,8 @@
     
-try %7B%0A              //noinspection deprecation%0A              DefaultJDOMExternalizer.readExternal(
 proj
@@ -2352,18 +2352,27 @@
 sManager
-, 
+.loadState(
 legacySe
@@ -2402,109 +2402,8 @@
 ));%0A
-            %7D%0A            catch (InvalidDataException e) %7B%0A              LOG.error(e);%0A            %7D%0A
     
@@ -3426,201 +3426,772 @@
 %0A%0A  
-@Override%0A  public CodeStyleSettingsManager getState() %7B%0A    return this;%0A  %7D%0A%0A  @Override%0A  public void loadState(CodeStyleSettingsManager state) %7B%0A    XmlSerializerUtil.copyBean(state, this);
+private void readExternal(Element element) throws InvalidDataException %7B%0A    DefaultJDOMExternalizer.readExternal(this, element);%0A  %7D%0A%0A  private void writeExternal(Element element) throws WriteExternalException %7B%0A    DefaultJDOMExternalizer.writeExternal(this, element, new DifferenceFilter%3CCodeStyleSettingsManager%3E(this, new CodeStyleSettingsManager()));%0A  %7D%0A%0A  @Override%0A  public Element getState() %7B%0A    Element result = new Element(%22state%22);%0A    try %7B%0A      writeExternal(result);%0A    %7D%0A    catch (WriteExternalException e) %7B%0A      LOG.error(e);%0A    %7D%0A    return result;%0A  %7D%0A%0A  @Override%0A  public void loadState(Element state) %7B%0A    try %7B%0A      readExternal(state);%0A      myIsLoaded = true;%0A    %7D%0A    catch (InvalidDataException e) %7B%0A      LOG.error(e);%0A    %7D
 %0A  %7D
@@ -4316,16 +4316,25 @@
 ettings(
+@NotNull 
 CodeStyl

revert CodeStyleSettingsManager