From dcb38f9bc76ca72b6e25ffe8caa9ca45fc9f101d Mon Sep 17 00:00:00 2001 From: Rustam Vishnyakov Date: Wed, 15 Aug 2012 14:06:07 +0400 Subject: [PATCH] IDEA-89379 Export Settings Includes other.xml When Only Code Style Selected (Code style settings part) --- .../psi/codeStyle/CodeStyleSchemes.java | 10 +- .../codeStyle/CodeStyleSchemesImpl.java | 54 +------- .../LegacyCodeStyleSchemesSettings.java | 51 ++++++++ .../PersistableCodeStyleSchemes.java | 121 ++++++++++++++++++ .../src/META-INF/LangExtensions.xml | 4 +- 5 files changed, 190 insertions(+), 50 deletions(-) create mode 100644 platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/LegacyCodeStyleSchemesSettings.java create mode 100644 platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/PersistableCodeStyleSchemes.java diff --git a/platform/lang-api/src/com/intellij/psi/codeStyle/CodeStyleSchemes.java b/platform/lang-api/src/com/intellij/psi/codeStyle/CodeStyleSchemes.java index 807d9a41711f..a5fb8c08102b 100644 --- a/platform/lang-api/src/com/intellij/psi/codeStyle/CodeStyleSchemes.java +++ b/platform/lang-api/src/com/intellij/psi/codeStyle/CodeStyleSchemes.java @@ -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(); } diff --git a/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/CodeStyleSchemesImpl.java b/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/CodeStyleSchemesImpl.java index b55915dbddd8..da7895aa08c9 100644 --- a/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/CodeStyleSchemesImpl.java +++ b/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/CodeStyleSchemesImpl.java @@ -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 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 getSchemesManager() { return mySchemesManager; } + + } diff --git a/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/LegacyCodeStyleSchemesSettings.java b/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/LegacyCodeStyleSchemesSettings.java new file mode 100644 index 000000000000..b78e90ebaf7f --- /dev/null +++ b/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/LegacyCodeStyleSchemesSettings.java @@ -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"; + } +} diff --git a/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/PersistableCodeStyleSchemes.java b/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/PersistableCodeStyleSchemes.java new file mode 100644 index 000000000000..2684cce775ff --- /dev/null +++ b/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/PersistableCodeStyleSchemes.java @@ -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, 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; + } + +} diff --git a/platform/platform-resources/src/META-INF/LangExtensions.xml b/platform/platform-resources/src/META-INF/LangExtensions.xml index 194a748a1e1d..8373ecb3ddaf 100644 --- a/platform/platform-resources/src/META-INF/LangExtensions.xml +++ b/platform/platform-resources/src/META-INF/LangExtensions.xml @@ -110,7 +110,9 @@ + serviceImplementation="com.intellij.psi.impl.source.codeStyle.PersistableCodeStyleSchemes"/> +