fix cyclic initialization of FileColorManager

GitOrigin-RevId: 005cc812ab901c0ea86eb93a73ba00a1bda20c1e
This commit is contained in:
Vladimir Krivosheev
2019-09-05 13:02:24 +00:00
committed by intellij-monorepo-bot
parent 483287d8a1
commit 63675d0853
4 changed files with 41 additions and 82 deletions
@@ -1,19 +1,4 @@
/*
* Copyright 2000-2015 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.
*/
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.ui;
import com.intellij.openapi.components.ServiceManager;
@@ -49,9 +34,6 @@ public abstract class FileColorManager {
public abstract Collection<String> getColorNames();
@Nullable
public abstract Color getFileColor(@NotNull final PsiFile file);
@Nullable
public abstract Color getFileColor(@NotNull final VirtualFile file);
@@ -60,8 +42,6 @@ public abstract class FileColorManager {
public abstract boolean isShared(@NotNull final String scopeName);
public abstract boolean isColored(@NotNull String scopeName, final boolean shared);
@Nullable
public abstract Color getRendererBackground(VirtualFile file);
@@ -1,9 +1,11 @@
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.ui.tabs;
import com.intellij.ide.util.PropertiesComponent;
import com.intellij.openapi.components.*;
import com.intellij.openapi.components.PersistentStateComponent;
import com.intellij.openapi.components.State;
import com.intellij.openapi.components.Storage;
import com.intellij.openapi.components.StoragePathMacros;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiFile;
@@ -24,10 +26,8 @@ import java.util.*;
* @author spleaner
* @author Konstantin Bulenkov
*/
@State(
name = "FileColors",
storages = {@Storage(StoragePathMacros.WORKSPACE_FILE)})
public class FileColorManagerImpl extends FileColorManager implements PersistentStateComponent<Element> {
@State(name = "FileColors", storages = @Storage(StoragePathMacros.WORKSPACE_FILE))
public final class FileColorManagerImpl extends FileColorManager implements PersistentStateComponent<Element> {
public static final String FC_ENABLED = "FileColorsEnabled";
public static final String FC_TABS_ENABLED = "FileColorsForTabsEnabled";
public static final String FC_PROJECT_VIEW_ENABLED = "FileColorsForProjectViewEnabled";
@@ -44,14 +44,14 @@ public class FileColorManagerImpl extends FileColorManager implements Persistent
.put("Yellow", JBColor.namedColor("FileColor.Yellow", new JBColor(0xffffe4, 0x4f4b41)))
.build();
public FileColorManagerImpl(@NotNull final Project project) {
public FileColorManagerImpl(@NotNull Project project) {
myProject = project;
myModel = new FileColorsModel(project);
}
private void initProjectLevelConfigurations() {
if (myProjectLevelConfigurationManager == null) {
myProjectLevelConfigurationManager = ServiceManager.getService(myProject, FileColorProjectLevelConfigurationManager.class);
myProjectLevelConfigurationManager = myProject.getService(FileColorProjectLevelConfigurationManager.class);
}
}
@@ -95,30 +95,16 @@ public class FileColorManagerImpl extends FileColorManager implements Persistent
PropertiesComponent.getInstance().setValue(FC_PROJECT_VIEW_ENABLED, Boolean.toString(enabled));
}
public Element getState(final boolean shared) {
Element element = new Element("state");
myModel.save(element, shared);
return element;
}
@Override
@Nullable
public Color getColor(@NotNull final String name) {
public Color getColor(@NotNull String name) {
Color color = ourDefaultColors.get(name);
if (color != null) {
return color;
}
return ColorUtil.fromHex(name, null);
return color == null ? ColorUtil.fromHex(name, null) : color;
}
@Override
public Element getState() {
initProjectLevelConfigurations();
return getState(false);
}
void loadState(Element state, final boolean shared) {
myModel.load(state, shared);
return myModel.save(false);
}
@Override
@@ -130,13 +116,7 @@ public class FileColorManagerImpl extends FileColorManager implements Persistent
@Override
public void loadState(@NotNull Element state) {
initProjectLevelConfigurations();
loadState(state, false);
}
@Override
public boolean isColored(@NotNull final String scopeName, final boolean shared) {
return myModel.isColored(scopeName, shared);
myModel.load(state, false);
}
@Nullable
@@ -158,7 +138,7 @@ public class FileColorManagerImpl extends FileColorManager implements Persistent
public Color getRendererBackground(PsiFile file) {
if (file == null) return null;
final VirtualFile vFile = file.getVirtualFile();
VirtualFile vFile = file.getVirtualFile();
if (vFile == null) return null;
return getRendererBackground(vFile);
@@ -166,24 +146,17 @@ public class FileColorManagerImpl extends FileColorManager implements Persistent
@Override
public void addScopeColor(@NotNull String scopeName, @NotNull String colorName, boolean isProjectLevel) {
initProjectLevelConfigurations();
myModel.add(scopeName, colorName, isProjectLevel);
}
@Override
@Nullable
public Color getFileColor(@NotNull final PsiFile file) {
public Color getFileColor(@NotNull VirtualFile file) {
initProjectLevelConfigurations();
final String colorName = myModel.getColor(file);
return colorName == null ? null : getColor(colorName);
}
@Override
@Nullable
public Color getFileColor(@NotNull final VirtualFile file) {
initProjectLevelConfigurations();
final String colorName = myModel.getColor(file, getProject());
String colorName = myModel.getColor(file, getProject());
return colorName == null ? null : getColor(colorName);
}
@@ -192,17 +165,21 @@ public class FileColorManagerImpl extends FileColorManager implements Persistent
public Color getScopeColor(@NotNull String scopeName) {
initProjectLevelConfigurations();
final String colorName = myModel.getScopeColor(scopeName, getProject());
String colorName = myModel.getScopeColor(scopeName, getProject());
return colorName == null ? null : getColor(colorName);
}
@Override
public boolean isShared(@NotNull final String scopeName) {
public boolean isShared(@NotNull String scopeName) {
initProjectLevelConfigurations();
return myModel.isProjectLevel(scopeName);
}
@NotNull
FileColorsModel getModel() {
initProjectLevelConfigurations();
return myModel;
}
@@ -217,6 +194,8 @@ public class FileColorManagerImpl extends FileColorManager implements Persistent
}
public List<FileColorConfiguration> getProjectLevelConfigurations() {
initProjectLevelConfigurations();
return myModel.getProjectLevelConfigurations();
}
@@ -1,5 +1,4 @@
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.ui.tabs;
import com.intellij.openapi.components.PersistentStateComponent;
@@ -11,20 +10,20 @@ import org.jdom.Element;
import org.jetbrains.annotations.NotNull;
@State(name="SharedFileColors", storages = @Storage("fileColors.xml"))
public class FileColorProjectLevelConfigurationManager implements PersistentStateComponent<Element> {
final class FileColorProjectLevelConfigurationManager implements PersistentStateComponent<Element> {
private final Project myProject;
public FileColorProjectLevelConfigurationManager(@NotNull final Project project) {
FileColorProjectLevelConfigurationManager(@NotNull Project project) {
myProject = project;
}
@Override
public Element getState() {
return ((FileColorManagerImpl)FileColorManager.getInstance(myProject)).getState(true);
return ((FileColorManagerImpl)FileColorManager.getInstance(myProject)).getModel().save(true);
}
@Override
public void loadState(@NotNull Element state) {
((FileColorManagerImpl)FileColorManager.getInstance(myProject)).loadState(state, true);
((FileColorManagerImpl)FileColorManager.getInstance(myProject)).getModel().load(state, true);
}
}
@@ -1,5 +1,4 @@
// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.ui.tabs;
import com.intellij.ide.util.PropertiesComponent;
@@ -30,8 +29,7 @@ import java.util.Map;
* @author spleaner
* @author Konstantin Bulenkov
*/
// todo[spL]: listen to scope rename
public class FileColorsModel implements Cloneable {
public final class FileColorsModel implements Cloneable {
public static final String FILE_COLOR = "fileColor";
private final List<FileColorConfiguration> myApplicationLevelConfigurations = new ArrayList<>();
@@ -42,7 +40,7 @@ public class FileColorsModel implements Cloneable {
@NotNull
private final Project myProject;
FileColorsModel(@NotNull final Project project) {
FileColorsModel(@NotNull Project project) {
myProject = project;
initPredefinedAndGlobalScopes();
}
@@ -105,7 +103,9 @@ public class FileColorsModel implements Cloneable {
return colorName;
}
public void save(@NotNull Element e, boolean isProjectLevel) {
@NotNull
Element save(boolean isProjectLevel) {
Element e = new Element("state");
List<FileColorConfiguration> configurations = isProjectLevel ? myProjectLevelConfigurations : myApplicationLevelConfigurations;
for (FileColorConfiguration configuration : configurations) {
String scopeName = configuration.getScopeName();
@@ -120,6 +120,7 @@ public class FileColorsModel implements Cloneable {
PropertiesComponent.getInstance().setValue(scopeName, null);
}
}
return e;
}
public void load(@NotNull Element e, boolean isProjectLevel) {
@@ -212,8 +213,8 @@ public class FileColorsModel implements Cloneable {
}
return null;
}
@Nullable
@Nullable
public String getScopeColor(@NotNull String scopeName, Project project) {
FileColorConfiguration configuration = null;
for (FileColorConfiguration each : getConfigurations()) {
@@ -242,7 +243,7 @@ public class FileColorsModel implements Cloneable {
}
return null;
}
@NotNull
private List<FileColorConfiguration> getConfigurations() {
return ContainerUtil.concat(myApplicationLevelConfigurations, myProjectLevelConfigurations);