IDEA-89379 Export Settings Includes other.xml When Only Code Style Selected (Code style settings part)

This commit is contained in:
Rustam Vishnyakov
2012-08-15 14:09:52 +04:00
parent cc751815f1
commit dcb38f9bc7
5 changed files with 190 additions and 50 deletions
@@ -24,7 +24,11 @@ import com.intellij.openapi.components.ServiceManager;
public abstract class CodeStyleSchemes {
public static CodeStyleSchemes getInstance(){
return ServiceManager.getService(CodeStyleSchemes.class);
CodeStyleSchemes schemes = ServiceManager.getService(CodeStyleSchemes.class);
if (!schemes.isLoaded()) {
schemes.loadSettings();
}
return schemes;
}
public abstract CodeStyleScheme[] getSchemes();
@@ -42,5 +46,9 @@ public abstract class CodeStyleSchemes {
public abstract CodeStyleScheme getDefaultScheme();
public abstract void addScheme(CodeStyleScheme currentScheme);
public abstract boolean isLoaded();
public abstract void loadSettings();
}
@@ -25,15 +25,12 @@ import com.intellij.openapi.options.SchemeProcessor;
import com.intellij.openapi.options.SchemesManager;
import com.intellij.openapi.options.SchemesManagerFactory;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.util.DefaultJDOMExternalizer;
import com.intellij.openapi.util.InvalidDataException;
import com.intellij.openapi.util.JDOMExternalizable;
import com.intellij.openapi.util.WriteExternalException;
import com.intellij.psi.PsiBundle;
import com.intellij.psi.codeStyle.CodeStyleScheme;
import com.intellij.psi.codeStyle.CodeStyleSchemes;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
@@ -46,12 +43,12 @@ import java.util.Collection;
* @author MYakovlev
* Date: Jul 16, 2002
*/
public class CodeStyleSchemesImpl extends CodeStyleSchemes implements ExportableComponent, JDOMExternalizable, NamedComponent {
@NonNls private static final String DEFAULT_SCHEME_NAME = "Default";
public abstract class CodeStyleSchemesImpl extends CodeStyleSchemes implements ExportableComponent {
@NonNls public static final String DEFAULT_SCHEME_NAME = "Default";
public String CURRENT_SCHEME_NAME = DEFAULT_SCHEME_NAME;
private boolean myIsInitialized = false;
@NonNls private static final String CODESTYLES_DIRECTORY = "codestyles";
@NonNls static final String CODESTYLES_DIRECTORY = "codestyles";
private final SchemesManager<CodeStyleScheme, CodeStyleSchemeImpl> mySchemesManager;
@NonNls private static final String FILE_SPEC = "$ROOT_CONFIG$/" + CODESTYLES_DIRECTORY;
@@ -164,60 +161,21 @@ public class CodeStyleSchemesImpl extends CodeStyleSchemes implements Exportable
mySchemesManager.removeScheme(scheme);
}
@Override
public void readExternal(Element element) throws InvalidDataException {
init();
DefaultJDOMExternalizer.readExternal(this, element);
CodeStyleScheme current = findSchemeByName(CURRENT_SCHEME_NAME);
if (current == null) current = getDefaultScheme();
setCurrentScheme(current);
}
private void init() {
protected void init() {
if (myIsInitialized) return;
myIsInitialized = true;
mySchemesManager.loadSchemes();
}
@Override
public void writeExternal(Element element) throws WriteExternalException {
DefaultJDOMExternalizer.writeExternal(this, element);
}
@Override
@NotNull
public File[] getExportFiles() {
return new File[]{getDir(true), PathManager.getDefaultOptionsFile()};
}
@Override
@NotNull
public String getPresentableName() {
return PsiBundle.message("codestyle.export.display.name");
}
private static File getDir(boolean create) {
String directoryPath = PathManager.getConfigPath() + File.separator + CODESTYLES_DIRECTORY;
File directory = new File(directoryPath);
if (!directory.exists()) {
if (!create) return null;
if (!directory.mkdir()) {
Messages.showErrorDialog(PsiBundle.message("codestyle.cannot.save.settings.directory.cant.be.created.message", directoryPath),
PsiBundle.message("codestyle.cannot.save.settings.directory.cant.be.created.title"));
return null;
}
}
return directory;
}
@NotNull
@Override
public String getComponentName() {
return "CodeStyleSchemes";
}
public SchemesManager<CodeStyleScheme, CodeStyleSchemeImpl> getSchemesManager() {
return mySchemesManager;
}
}
@@ -0,0 +1,51 @@
/*
* 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.impl.source.codeStyle;
import com.intellij.openapi.components.NamedComponent;
import com.intellij.openapi.util.DefaultJDOMExternalizer;
import com.intellij.openapi.util.InvalidDataException;
import com.intellij.openapi.util.JDOMExternalizable;
import com.intellij.openapi.util.WriteExternalException;
import org.jdom.Element;
import org.jetbrains.annotations.NotNull;
/**
* @author Rustam Vishnyakov
*/
@SuppressWarnings("deprecation")
public class LegacyCodeStyleSchemesSettings implements JDOMExternalizable, NamedComponent {
public String CURRENT_SCHEME_NAME = CodeStyleSchemesImpl.DEFAULT_SCHEME_NAME;
@Override
public void readExternal(Element element) throws InvalidDataException {
DefaultJDOMExternalizer.readExternal(this, element);
}
@Override
public void writeExternal(Element element) throws WriteExternalException {
DefaultJDOMExternalizer.writeExternal(this, element);
}
@NotNull
@Override
public String getComponentName() {
return "CodeStyleSchemes";
}
}
@@ -0,0 +1,121 @@
/*
* 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.impl.source.codeStyle;
import com.intellij.openapi.application.PathManager;
import com.intellij.openapi.components.*;
import com.intellij.openapi.options.SchemesManagerFactory;
import com.intellij.openapi.ui.Messages;
import com.intellij.psi.PsiBundle;
import com.intellij.psi.codeStyle.CodeStyleScheme;
import com.intellij.util.xmlb.Accessor;
import com.intellij.util.xmlb.SerializationFilter;
import com.intellij.util.xmlb.XmlSerializer;
import org.jdom.Element;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.File;
/**
* @author Rustam Vishnyakov
*/
@State(
name = "CodeStyleSchemeSettings",
storages = {@Storage(
file = StoragePathMacros.APP_CONFIG + "/" + PersistableCodeStyleSchemes.CODE_STYLE_SCHEMES_FILE
)}
)
public class PersistableCodeStyleSchemes extends CodeStyleSchemesImpl implements PersistentStateComponent<Element>, NamedComponent {
public final static String CODE_STYLE_SCHEMES_FILE = "code.style.schemes.xml";
private boolean isLoaded;
public PersistableCodeStyleSchemes(SchemesManagerFactory schemesManagerFactory) {
super(schemesManagerFactory);
}
@Nullable
@Override
public Element getState() {
return XmlSerializer.serialize(this, new SerializationFilter() {
@Override
public boolean accepts(Accessor accessor, Object bean) {
return accessor.getValueClass().equals(String.class);
}
});
}
@Override
public void loadState(Element state) {
init();
XmlSerializer.deserializeInto(this, state);
this.isLoaded = true;
updateCurrentScheme();
}
@NotNull
@Override
public String getComponentName() {
return "CodeStyleSchemeSettings";
}
@Override
public boolean isLoaded() {
return this.isLoaded;
}
@Override
public void loadSettings() {
init();
LegacyCodeStyleSchemesSettings legacySettings = ServiceManager.getService(LegacyCodeStyleSchemesSettings.class);
if (legacySettings != null) {
CURRENT_SCHEME_NAME = legacySettings.CURRENT_SCHEME_NAME;
}
this.isLoaded = true;
updateCurrentScheme();
}
private void updateCurrentScheme() {
CodeStyleScheme current = findSchemeByName(CURRENT_SCHEME_NAME);
if (current == null) current = getDefaultScheme();
setCurrentScheme(current);
}
@Override
@NotNull
public File[] getExportFiles() {
File schemesFile = new File(PathManager.getOptionsPath() + File.separator + CODE_STYLE_SCHEMES_FILE);
return new File[]{getDir(true), schemesFile};
}
@Nullable
private static File getDir(boolean create) {
String directoryPath = PathManager.getConfigPath() + File.separator + CODESTYLES_DIRECTORY;
File directory = new File(directoryPath);
if (!directory.exists()) {
if (!create) return null;
if (!directory.mkdir()) {
Messages.showErrorDialog(PsiBundle.message("codestyle.cannot.save.settings.directory.cant.be.created.message", directoryPath),
PsiBundle.message("codestyle.cannot.save.settings.directory.cant.be.created.title"));
return null;
}
}
return directory;
}
}
@@ -110,7 +110,9 @@
<exportable serviceInterface="com.intellij.openapi.roots.impl.libraries.ApplicationLibraryTable"/>
<applicationService serviceInterface="com.intellij.psi.codeStyle.CodeStyleSchemes"
serviceImplementation="com.intellij.psi.impl.source.codeStyle.CodeStyleSchemesImpl"/>
serviceImplementation="com.intellij.psi.impl.source.codeStyle.PersistableCodeStyleSchemes"/>
<applicationService serviceInterface="com.intellij.psi.impl.source.codeStyle.LegacyCodeStyleSchemesSettings"
serviceImplementation="com.intellij.psi.impl.source.codeStyle.LegacyCodeStyleSchemesSettings"/>
<exportable serviceInterface="com.intellij.psi.codeStyle.CodeStyleSchemes"/>
<schemeOwner serviceInterface="com.intellij.psi.codeStyle.CodeStyleSchemes"/>