/*
 * Copyright 2000-2016 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.editor.colors.impl;

import com.intellij.openapi.editor.colors.ColorKey;
import com.intellij.openapi.editor.colors.EditorColorsScheme;
import com.intellij.openapi.editor.colors.TextAttributesKey;
import com.intellij.openapi.editor.markup.TextAttributes;
import com.intellij.openapi.options.ExternalizableScheme;
import com.intellij.openapi.util.Comparing;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.awt.*;

/**
 * @author Yura Cangea
 */
public class EditorColorsSchemeImpl extends AbstractColorsScheme implements ExternalizableScheme {
  public EditorColorsSchemeImpl(EditorColorsScheme parentScheme) {
    super(parentScheme);
  }

  @Override
  public void setAttributes(@NotNull TextAttributesKey key, TextAttributes attributes) {
    TextAttributes ownAttributes = getOwnAttributes(key);
    if (ownAttributes != null && ownAttributes.equals(attributes)) {
      return;
    }
    TextAttributes parentAttributes = myParentScheme.getAttributes(key);
    if (parentAttributes != null && parentAttributes.equals(attributes)) {
      myAttributesMap.remove(key);
      return;
    }

    myAttributesMap.put(key, attributes);
  }

  @Override
  public void setColor(ColorKey key, Color color) {
    if (!Comparing.equal(color, getColor(key))) {
      myColorsMap.put(key, color);
    }
  }

  @Override
  public TextAttributes getAttributes(@Nullable TextAttributesKey key) {
    TextAttributes attributes = key == null ? null : getOwnAttributes(key);
    return attributes == null ? myParentScheme.getAttributes(key) : attributes;
  }

  @Nullable
  private TextAttributes getOwnAttributes(@NotNull TextAttributesKey key) {
    TextAttributesKey fallbackKey = key.getFallbackAttributeKey();
    TextAttributes attributes = getDirectlyDefinedAttributes(key);
    if (fallbackKey == null) {
      if (containsValue(attributes)) {
        return attributes;
      }
    }
    else {
      if (containsValue(attributes) && !attributes.isFallbackEnabled()) {
        return attributes;
      }
      attributes = getFallbackAttributes(fallbackKey);
      if (containsValue(attributes)) {
        return attributes;
      }
    }
    return null;
  }

  @Nullable
  @Override
  public Color getColor(ColorKey key) {
    if (myColorsMap.containsKey(key)) {
      return myColorsMap.get(key);
    }
    else {
      return myParentScheme.getColor(key);
    }
  }

  @Override
  public Object clone() {
    EditorColorsSchemeImpl newScheme = new EditorColorsSchemeImpl(myParentScheme);
    copyTo(newScheme);
    newScheme.setName(getName());
    newScheme.setDefaultMetaInfo(this);
    return newScheme;
  }
}

@@ -1421,355 +1421,50 @@
     
-TextAttributes ownAttributes = getOwnAttributes(key);%0A    if (ownAttributes != null && ownAttributes.equals(attributes)) %7B%0A      return;%0A    %7D%0A    TextAttributes parentAttributes = myParentScheme.getAttributes(key);%0A    if (parentAttributes != null && parentAttributes.equals(attributes)) %7B%0A      myAttributesMap.remove(key);%0A      return;%0A    %7D%0A%0A
+if (attributes != getAttributes(key)) %7B%0A  
     
@@ -1501,16 +1501,22 @@
 butes);%0A
+    %7D%0A
   %7D%0A%0A  @
@@ -2367,24 +2367,25 @@
 es;%0A      %7D%0A
+%0A
       attrib

revert EditorColorsSchemeImpl.setAttributes — doesn't help to fix "save unmodified" and don't fix "remove unmodified"