2 * Copyright 2000-2016 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.application.options.colors;
18 import com.intellij.codeHighlighting.RainbowHighlighter;
19 import com.intellij.openapi.editor.colors.EditorColorsScheme;
20 import com.intellij.openapi.editor.colors.EditorSchemeAttributeDescriptorWithPath;
21 import com.intellij.openapi.editor.colors.TextAttributesKey;
22 import com.intellij.openapi.editor.markup.TextAttributes;
23 import com.intellij.openapi.util.Pair;
24 import org.jetbrains.annotations.NotNull;
27 import java.util.ArrayList;
28 import java.util.List;
30 class RainbowAttributeDescriptor implements EditorSchemeAttributeDescriptorWithPath {
31 private final String myGroup;
32 private final String myDisplayName;
33 private final EditorColorsScheme myScheme;
35 // can be shared between instances
36 private final RainbowInSchemeState myCurState;
37 private final RainbowInSchemeState myInitState;
39 public RainbowAttributeDescriptor(@NotNull String group,
40 @NotNull String displayNameWithPath,
41 @NotNull EditorColorsScheme scheme,
42 RainbowInSchemeState initState,
43 RainbowInSchemeState curState) {
44 myDisplayName = displayNameWithPath;
45 myInitState = initState;
46 myCurState = curState;
52 public String toString() {
57 public String getGroup() {
62 public String getType() {
63 return RainbowHighlighter.RAINBOW_TYPE;
67 public EditorColorsScheme getScheme() {
72 public void apply(@NotNull EditorColorsScheme scheme) {
73 myCurState.apply(scheme);
77 public boolean isModified() {
78 return !myInitState.equals(myCurState);
81 public List<Pair<Boolean, Color>> getRainbowCurState() {
82 return myCurState.myRainbowState;
85 public Color getDefaultColor(int index) {
86 return RainbowHighlighter.RAINBOW_COLOR_KEYS[index].getDefaultAttributes().getForegroundColor();
89 public static class RainbowInSchemeState {
90 private final List<Pair<Boolean, Color>> myRainbowState = new ArrayList<>();
92 public RainbowInSchemeState(@NotNull EditorColorsScheme scheme) {
93 for (TextAttributesKey rainbowKey : RainbowHighlighter.RAINBOW_COLOR_KEYS) {
94 myRainbowState.add(getColorStateFromScheme(scheme, rainbowKey));
98 public RainbowInSchemeState(@NotNull RainbowInSchemeState state) {
102 public void copyFrom(@NotNull RainbowInSchemeState state) {
103 assert this != state;
105 myRainbowState.clear();
106 myRainbowState.addAll(state.myRainbowState);
109 public void apply(@NotNull EditorColorsScheme scheme) {
111 for (TextAttributesKey rainbowKey : RainbowHighlighter.RAINBOW_COLOR_KEYS) {
112 Pair<Boolean, Color> pair = myRainbowState.get(i);
113 scheme.setAttributes(rainbowKey, pair.first ? new TextAttributes(pair.second, null, null, null, Font.PLAIN)
114 : rainbowKey.getDefaultAttributes());
120 private static Pair<Boolean, Color> getColorStateFromScheme(@NotNull EditorColorsScheme scheme, TextAttributesKey rainbowKey) {
121 TextAttributes schemeAttributes = scheme.getAttributes(rainbowKey);
122 @NotNull Color defaultRainbow = rainbowKey.getDefaultAttributes().getForegroundColor();
123 Pair<Boolean, Color> pair;
124 if (schemeAttributes == null) {
125 pair = Pair.create(false, defaultRainbow);
128 Color schemeColor = schemeAttributes.getForegroundColor();
129 if (schemeColor == null) {
130 pair = Pair.create(false, defaultRainbow);
133 pair = Pair.create(!defaultRainbow.equals(schemeColor), schemeColor);
140 public boolean equals(Object o) {
141 if (this == o) return true;
142 if (o == null || getClass() != o.getClass()) return false;
144 RainbowInSchemeState state = (RainbowInSchemeState)o;
145 return myRainbowState.equals(state.myRainbowState);
149 public int hashCode() {
150 return myRainbowState.hashCode();