From 24171ee9037c8bb62eac886e6785c8538adf29c8 Mon Sep 17 00:00:00 2001 From: Vladimir Krivosheev Date: Wed, 31 Aug 2016 15:29:06 +0200 Subject: [PATCH] scheme-based copyright manager --- .../ProjectInspectionProfileManager.kt | 10 +- .../src/SchemeManagerImpl.kt | 8 +- .../configurationStore/scheme-impl.kt | 30 ++ .../src/com/intellij/jdom.kt | 2 + plugins/copyright/copyright.iml | 1 + plugins/copyright/src/CopyrightManager.kt | 277 +++++++++++++++++ plugins/copyright/src/META-INF/plugin.xml | 92 +++--- .../idea/copyright/CopyrightManager.java | 292 ------------------ .../actions/AbstractFileProcessor.java | 47 +-- .../actions/GenerateCopyrightAction.java | 3 +- .../actions/UpdateCopyrightAction.java | 2 +- .../actions/UpdateCopyrightProcessor.java | 19 +- .../copyright/pattern/VelocityHelper.java | 4 +- .../psi/AbstractUpdateCopyright.java | 4 +- .../copyright/psi/UpdatePsiFileCopyright.java | 4 +- .../copyright/ui/CopyrightConfigurable.java | 8 +- .../copyright/ui/CopyrightProfilesPanel.java | 33 +- .../copyright/ui/ProjectSettingsPanel.java | 27 +- .../copyright/ui/TemplateCommentPanel.java | 11 +- .../idea/copyright/util/NewFileTracker.java | 14 +- 20 files changed, 417 insertions(+), 471 deletions(-) create mode 100644 plugins/copyright/src/CopyrightManager.kt delete mode 100644 plugins/copyright/src/com/maddyhome/idea/copyright/CopyrightManager.java diff --git a/platform/analysis-impl/src/com/intellij/profile/codeInspection/ProjectInspectionProfileManager.kt b/platform/analysis-impl/src/com/intellij/profile/codeInspection/ProjectInspectionProfileManager.kt index a0ea74611b0d..dcd1697edf70 100644 --- a/platform/analysis-impl/src/com/intellij/profile/codeInspection/ProjectInspectionProfileManager.kt +++ b/platform/analysis-impl/src/com/intellij/profile/codeInspection/ProjectInspectionProfileManager.kt @@ -21,6 +21,7 @@ import com.intellij.codeInspection.ex.InspectionToolRegistrar import com.intellij.configurationStore.SchemeDataHolder import com.intellij.configurationStore.SchemeManagerIprProvider import com.intellij.configurationStore.digest +import com.intellij.configurationStore.wrapState import com.intellij.openapi.Disposable import com.intellij.openapi.application.ApplicationManager import com.intellij.openapi.components.PersistentStateComponent @@ -32,7 +33,6 @@ import com.intellij.openapi.project.ProjectManager import com.intellij.openapi.project.ProjectManagerListener import com.intellij.openapi.startup.StartupActivity import com.intellij.openapi.util.Disposer -import com.intellij.openapi.util.JDOMUtil import com.intellij.openapi.util.text.StringUtil import com.intellij.packageDependencies.DependencyValidationManager import com.intellij.profile.Profile @@ -264,13 +264,7 @@ class ProjectInspectionProfileManager(val project: Project, severityRegistrar.writeExternal(result) - if (JDOMUtil.isEmpty(result)) { - result.name = "state" - return result - } - else { - return Element("state").addContent(result) - } + return wrapState(result) } override fun getScopesManager() = scopeManager diff --git a/platform/configuration-store-impl/src/SchemeManagerImpl.kt b/platform/configuration-store-impl/src/SchemeManagerImpl.kt index 50634e5b4433..3b759da23c11 100644 --- a/platform/configuration-store-impl/src/SchemeManagerImpl.kt +++ b/platform/configuration-store-impl/src/SchemeManagerImpl.kt @@ -370,12 +370,16 @@ class SchemeManagerImpl(val fileSpec: String, override fun updateDigest(scheme: MUTABLE_SCHEME) { try { - externalInfo.digest = (processor.writeScheme(scheme) as Element).digest() + updateDigest(processor.writeScheme(scheme) as Element) } catch (e: WriteExternalException) { LOG.error("Cannot update digest", e) } } + + override fun updateDigest(data: Element) { + externalInfo.digest = data.digest() + } } private fun loadScheme(fileName: String, input: InputStream, schemes: MutableList, filesToDelete: MutableSet? = null): MUTABLE_SCHEME? { @@ -438,7 +442,7 @@ class SchemeManagerImpl(val fileSpec: String, XmlPullParser.START_TAG -> { if (!isUseOldFileNameSanitize || parser.name != "component") { var name: String? = null - if (isUseOldFileNameSanitize && parser.name == "profile") { + if (isUseOldFileNameSanitize && (parser.name == "profile" || parser.name == "copyright")) { eventType = parser.next() findName@ while (eventType != XmlPullParser.END_DOCUMENT) { when (eventType) { diff --git a/platform/projectModel-impl/src/com/intellij/configurationStore/scheme-impl.kt b/platform/projectModel-impl/src/com/intellij/configurationStore/scheme-impl.kt index a00f58e52583..eda2ab4802b8 100644 --- a/platform/projectModel-impl/src/com/intellij/configurationStore/scheme-impl.kt +++ b/platform/projectModel-impl/src/com/intellij/configurationStore/scheme-impl.kt @@ -15,8 +15,11 @@ */ package com.intellij.configurationStore +import com.intellij.openapi.options.ExternalizableSchemeAdapter import com.intellij.openapi.options.Scheme import com.intellij.openapi.options.SchemeProcessor +import com.intellij.openapi.options.SchemeState +import com.intellij.util.isEmpty import org.jdom.Element import java.io.OutputStream import java.security.MessageDigest @@ -29,6 +32,8 @@ interface SchemeDataHolder { fun read(): Element fun updateDigest(scheme: MUTABLE_SCHEME) + + fun updateDigest(data: Element) } interface SerializableScheme { @@ -84,4 +89,29 @@ fun Element.digest(): ByteArray { val digest = MessageDigest.getInstance("SHA-1") serializeElementToBinary(this, DigestOutputStream(digest)) return digest.digest() +} + +abstract class SchemeWrapper(name: String) : ExternalizableSchemeAdapter(), SerializableScheme { + protected abstract val lazyScheme: Lazy + + val scheme: T + get() = lazyScheme.value + + val schemeState: SchemeState + get() = if (lazyScheme.isInitialized()) SchemeState.POSSIBLY_CHANGED else SchemeState.UNCHANGED + + init { + this.name = name + } +} + +fun wrapState(element: Element): Element { + if (element.isEmpty()) { + element.name = "state" + return element + } + + val wrapper = Element("state") + wrapper.addContent(element) + return wrapper } \ No newline at end of file diff --git a/platform/projectModel-impl/src/com/intellij/jdom.kt b/platform/projectModel-impl/src/com/intellij/jdom.kt index ef460e6902cd..f1e45ae5c543 100644 --- a/platform/projectModel-impl/src/com/intellij/jdom.kt +++ b/platform/projectModel-impl/src/com/intellij/jdom.kt @@ -82,6 +82,8 @@ fun Element.element(name: String): Element { return element } +fun Element.attribute(name: String, value: String?): Element = setAttribute(name, value) + fun Element.remove(name: String, transform: (child: Element) -> T): List { val result = SmartList() val groupIterator = getContent(ElementFilter(name)).iterator() diff --git a/plugins/copyright/copyright.iml b/plugins/copyright/copyright.iml index 126c6110fa43..e9b97f5b12bf 100644 --- a/plugins/copyright/copyright.iml +++ b/plugins/copyright/copyright.iml @@ -14,5 +14,6 @@ + \ No newline at end of file diff --git a/plugins/copyright/src/CopyrightManager.kt b/plugins/copyright/src/CopyrightManager.kt new file mode 100644 index 000000000000..b9605666c44c --- /dev/null +++ b/plugins/copyright/src/CopyrightManager.kt @@ -0,0 +1,277 @@ +/* + * 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.copyright + +import com.intellij.configurationStore.LazySchemeProcessor +import com.intellij.configurationStore.SchemeDataHolder +import com.intellij.configurationStore.SchemeWrapper +import com.intellij.configurationStore.wrapState +import com.intellij.openapi.Disposable +import com.intellij.openapi.application.ApplicationManager +import com.intellij.openapi.application.ModalityState +import com.intellij.openapi.components.PersistentStateComponent +import com.intellij.openapi.components.State +import com.intellij.openapi.components.Storage +import com.intellij.openapi.components.service +import com.intellij.openapi.diagnostic.Logger +import com.intellij.openapi.editor.EditorFactory +import com.intellij.openapi.editor.event.DocumentAdapter +import com.intellij.openapi.editor.event.DocumentEvent +import com.intellij.openapi.fileEditor.FileDocumentManager +import com.intellij.openapi.options.SchemeManagerFactory +import com.intellij.openapi.project.Project +import com.intellij.openapi.roots.ProjectRootManager +import com.intellij.openapi.startup.StartupActivity +import com.intellij.openapi.util.Disposer +import com.intellij.openapi.util.InvalidDataException +import com.intellij.openapi.util.WriteExternalException +import com.intellij.openapi.util.text.StringUtil +import com.intellij.packageDependencies.DependencyValidationManager +import com.intellij.project.isDirectoryBased +import com.intellij.psi.PsiFile +import com.intellij.psi.PsiManager +import com.intellij.util.attribute +import com.intellij.util.element +import com.maddyhome.idea.copyright.CopyrightProfile +import com.maddyhome.idea.copyright.actions.UpdateCopyrightProcessor +import com.maddyhome.idea.copyright.options.LanguageOptions +import com.maddyhome.idea.copyright.options.Options +import com.maddyhome.idea.copyright.util.FileTypeUtil +import com.maddyhome.idea.copyright.util.NewFileTracker +import org.jdom.Element +import java.util.* +import java.util.concurrent.atomic.AtomicReference +import java.util.function.Function + +private const val DEFAULT = "default" +private const val MODULE2COPYRIGHT = "module2copyright" +private const val COPYRIGHT = "copyright" +private const val ELEMENT = "element" +private const val MODULE = "module" + +private val LOG = Logger.getInstance(CopyrightManager::class.java) + +@State(name = "CopyrightManager", storages = arrayOf(Storage(value = "copyright/profiles_settings.xml", exclusive = true))) +class CopyrightManager(private val project: Project, schemeManagerFactory: SchemeManagerFactory) : PersistentStateComponent { + companion object { + @JvmStatic + fun getInstance(project: Project) = project.service() + } + + var defaultCopyright: CopyrightProfile? = null + val scopeToCopyright = LinkedHashMap() + val options = Options() + + val schemeManager = schemeManagerFactory.create("copyright", object : LazySchemeProcessor, SchemeWrapper>() { + override fun createScheme(dataHolder: SchemeDataHolder>, + name: String, + attributeProvider: Function): SchemeWrapper { + return LazySchemeWrapper(name, dataHolder, schemeWriter) + } + + override fun getState(scheme: SchemeWrapper) = scheme.schemeState + + override fun isSchemeFile(name: CharSequence) = !StringUtil.equals(name, "profiles_settings.xml") + }, isUseOldFileNameSanitize = true) + + init { + val app = ApplicationManager.getApplication() + if (project.isDirectoryBased || !app.isUnitTestMode) { + schemeManager.loadSchemes() + } + } + + fun mapCopyright(scopeName: String, copyrightProfileName: String) { + scopeToCopyright.put(scopeName, copyrightProfileName) + } + + fun unmapCopyright(scopeName: String) { + scopeToCopyright.remove(scopeName) + } + + fun hasAnyCopyrights(): Boolean { + return defaultCopyright != null || !scopeToCopyright.isEmpty() + } + + override fun getState(): Element? { + val state = Element("settings") + try { + if (!scopeToCopyright.isEmpty()) { + val map = Element(MODULE2COPYRIGHT) + for ((scopeName, profileName) in scopeToCopyright) { + map.element(ELEMENT) + .attribute(MODULE, scopeName) + .attribute(COPYRIGHT, profileName) + } + state.addContent(map) + } + + options.writeExternal(state) + } + catch (e: WriteExternalException) { + LOG.error(e) + return null + } + + if (defaultCopyright != null) { + state.setAttribute(DEFAULT, defaultCopyright!!.name) + } + + return wrapState(state) + } + + override fun loadState(state: Element) { + val moduleToCopyright = state.getChild(MODULE2COPYRIGHT) + if (moduleToCopyright != null) { + for (element in moduleToCopyright.getChildren(ELEMENT)) { + scopeToCopyright.put(element.getAttributeValue(MODULE), element.getAttributeValue(COPYRIGHT)) + } + } + + try { + defaultCopyright = schemeManager.findSchemeByName(state.getAttributeValue(DEFAULT).orEmpty())?.scheme + options.readExternal(state) + } + catch (e: InvalidDataException) { + LOG.error(e) + } + } + + fun addCopyright(profile: CopyrightProfile) { + schemeManager.addScheme(InitializedSchemeWrapper(profile, schemeWriter)) + } + + fun getCopyrights(): Collection = schemeManager.allSchemes.map { it.scheme } + + fun clearMappings() { + scopeToCopyright.clear() + } + + fun removeCopyright(copyrightProfile: CopyrightProfile) { + schemeManager.removeScheme(copyrightProfile.name) + + val it = scopeToCopyright.keys.iterator() + while (it.hasNext()) { + if (scopeToCopyright.get(it.next()) == copyrightProfile.name) { + it.remove() + } + } + } + + fun replaceCopyright(name: String, profile: CopyrightProfile) { + if (defaultCopyright?.name == name) { + defaultCopyright = profile + } + + val existingScheme = schemeManager.findSchemeByName(name) + if (existingScheme == null) { + addCopyright(profile) + } + else { + existingScheme.scheme.copyFrom(profile) + } + } + + fun getCopyrightOptions(file: PsiFile): CopyrightProfile? { + val virtualFile = file.virtualFile + if (virtualFile == null || options.getOptions(virtualFile.fileType.name).getFileTypeOverride() == LanguageOptions.NO_COPYRIGHT) { + return null + } + + val validationManager = DependencyValidationManager.getInstance(project) + for (scopeName in scopeToCopyright.keys) { + val packageSet = validationManager.getScope(scopeName)?.value ?: continue + if (packageSet.contains(file, validationManager)) { + scopeToCopyright.get(scopeName)?.let { schemeManager.findSchemeByName(it) }?.let { return it.scheme } + } + } + return defaultCopyright + } +} + +private class CopyrightManagerPostStartupActivity : StartupActivity { + val newFileTracker = NewFileTracker() + + override fun runActivity(project: Project) { + Disposer.register(project, Disposable { newFileTracker.clear() }) + + EditorFactory.getInstance().eventMulticaster.addDocumentListener(object : DocumentAdapter() { + override fun documentChanged(e: DocumentEvent) { + val virtualFile = FileDocumentManager.getInstance().getFile(e.document) ?: return + val module = ProjectRootManager.getInstance(project).fileIndex.getModuleForFile(virtualFile) ?: return + if (!newFileTracker.poll(virtualFile) || + !FileTypeUtil.getInstance().isSupportedFile(virtualFile) || + PsiManager.getInstance(project).findFile(virtualFile) == null) { + return + } + + ApplicationManager.getApplication().invokeLater(Runnable { + if (!virtualFile.isValid) { + return@Runnable + } + + val file = PsiManager.getInstance(project).findFile(virtualFile) + if (file != null && file.isWritable) { + CopyrightManager.getInstance(project).getCopyrightOptions(file)?.let { + UpdateCopyrightProcessor(project, module, file).run() + } + } + }, ModalityState.NON_MODAL, project.disposed) + } + }, project) + } +} + +private fun wrapScheme(element: Element): Element { + val wrapper = Element("component") + .attribute("name", "CopyrightManager") + wrapper.addContent(element) + return wrapper +} + +private val schemeWriter = { scheme: CopyrightProfile -> + val element = Element("copyright") + scheme.writeExternal(element) + wrapScheme(element) +} + +private class InitializedSchemeWrapper(scheme: CopyrightProfile, private val writer: (scheme: CopyrightProfile) -> Element) : SchemeWrapper(scheme.name) { + override val lazyScheme = lazyOf(scheme) + + override fun writeScheme() = writer(scheme) +} + +private class LazySchemeWrapper(name: String, + dataHolder: SchemeDataHolder>, + private val writer: (scheme: CopyrightProfile) -> Element, + private val subStateTagName: String = "copyright") : SchemeWrapper(name) { + private val dataHolder = AtomicReference(dataHolder) + + override val lazyScheme = lazy { + val scheme = CopyrightProfile() + @Suppress("NAME_SHADOWING") + val dataHolder = this.dataHolder.getAndSet(null) + val element = dataHolder.read().getChild(subStateTagName) + scheme.readExternal(element) + dataHolder.updateDigest(writer(scheme)) + scheme + } + + override fun writeScheme(): Element { + val dataHolder = dataHolder.get() + return if (dataHolder == null) writer(scheme) else dataHolder.read() + } +} \ No newline at end of file diff --git a/plugins/copyright/src/META-INF/plugin.xml b/plugins/copyright/src/META-INF/plugin.xml index 87b49a360163..0a583be018b5 100644 --- a/plugins/copyright/src/META-INF/plugin.xml +++ b/plugins/copyright/src/META-INF/plugin.xml @@ -1,56 +1,50 @@ - - - - Copyright - com.intellij.copyright - - Copyright Notice. This plugin is used to ensure files in a project or module have - a consistent copyright notice. - - 8.1 - JetBrains - com.intellij.modules.java + Copyright + com.intellij.copyright + + Copyright Notice. This plugin is used to ensure files in a project or module have a consistent copyright notice. + + 8.1 + JetBrains + com.intellij.modules.java - - - - - - + + + + + + - - - com.maddyhome.idea.copyright.CopyrightManager - - - + + - - - - - - - - - - - - - + + + + + + + + + + + + - - - - - - - - - - + + + + + + + + + + diff --git a/plugins/copyright/src/com/maddyhome/idea/copyright/CopyrightManager.java b/plugins/copyright/src/com/maddyhome/idea/copyright/CopyrightManager.java deleted file mode 100644 index be5cebfb2d87..000000000000 --- a/plugins/copyright/src/com/maddyhome/idea/copyright/CopyrightManager.java +++ /dev/null @@ -1,292 +0,0 @@ -/* - * 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.maddyhome.idea.copyright; - -import com.intellij.openapi.Disposable; -import com.intellij.openapi.application.Application; -import com.intellij.openapi.application.ModalityState; -import com.intellij.openapi.components.*; -import com.intellij.openapi.diagnostic.Logger; -import com.intellij.openapi.editor.Document; -import com.intellij.openapi.editor.EditorFactory; -import com.intellij.openapi.editor.event.DocumentAdapter; -import com.intellij.openapi.editor.event.DocumentEvent; -import com.intellij.openapi.editor.event.DocumentListener; -import com.intellij.openapi.fileEditor.FileDocumentManager; -import com.intellij.openapi.module.Module; -import com.intellij.openapi.project.Project; -import com.intellij.openapi.roots.ProjectRootManager; -import com.intellij.openapi.startup.StartupManager; -import com.intellij.openapi.util.*; -import com.intellij.openapi.util.text.StringUtil; -import com.intellij.openapi.vfs.VirtualFile; -import com.intellij.packageDependencies.DependencyValidationManager; -import com.intellij.psi.PsiFile; -import com.intellij.psi.PsiManager; -import com.intellij.psi.search.scope.packageSet.NamedScope; -import com.intellij.psi.search.scope.packageSet.PackageSet; -import com.maddyhome.idea.copyright.actions.UpdateCopyrightProcessor; -import com.maddyhome.idea.copyright.options.LanguageOptions; -import com.maddyhome.idea.copyright.options.Options; -import com.maddyhome.idea.copyright.util.FileTypeUtil; -import com.maddyhome.idea.copyright.util.NewFileTracker; -import org.jdom.Element; -import org.jetbrains.annotations.NonNls; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import java.util.*; - -@State(name = "CopyrightManager", storages = @Storage(value = "copyright", stateSplitter = CopyrightManager.CopyrightStateSplitter.class)) -public class CopyrightManager extends AbstractProjectComponent implements PersistentStateComponent { - private static final Logger LOG = Logger.getInstance("#" + CopyrightManager.class.getName()); - @Nullable - private CopyrightProfile myDefaultCopyright = null; - private final LinkedHashMap myModuleToCopyrights = new LinkedHashMap<>(); - private final Map myCopyrights = new TreeMap<>(); - private final Options myOptions = new Options(); - - public CopyrightManager(@NotNull Project project, - @NotNull final EditorFactory editorFactory, - @NotNull final Application application, - @NotNull final FileDocumentManager fileDocumentManager, - @NotNull final FileTypeUtil fileTypeUtil, - @NotNull final ProjectRootManager projectRootManager, - @NotNull final PsiManager psiManager, - @NotNull StartupManager startupManager) { - super(project); - if (!myProject.isDefault()) { - final NewFileTracker newFileTracker = NewFileTracker.getInstance(); - Disposer.register(myProject, new Disposable() { - @Override - public void dispose() { - newFileTracker.clear(); - } - }); - startupManager.runWhenProjectIsInitialized(() -> { - DocumentListener listener = new DocumentAdapter() { - @Override - public void documentChanged(DocumentEvent e) { - final Document document = e.getDocument(); - final VirtualFile virtualFile = fileDocumentManager.getFile(document); - if (virtualFile == null) return; - final Module module = projectRootManager.getFileIndex().getModuleForFile(virtualFile); - if (module == null) return; - if (!newFileTracker.poll(virtualFile)) return; - if (!fileTypeUtil.isSupportedFile(virtualFile)) return; - if (psiManager.findFile(virtualFile) == null) return; - application.invokeLater(() -> { - if (!virtualFile.isValid()) return; - final PsiFile file = psiManager.findFile(virtualFile); - if (file != null && file.isWritable()) { - final CopyrightProfile opts = getCopyrightOptions(file); - if (opts != null) { - new UpdateCopyrightProcessor(myProject, module, file).run(); - } - } - }, ModalityState.NON_MODAL, myProject.getDisposed()); - } - }; - editorFactory.getEventMulticaster().addDocumentListener(listener, myProject); - }); - } - } - - @NonNls - static final String COPYRIGHT = "copyright"; - @NonNls - private static final String MODULE2COPYRIGHT = "module2copyright"; - @NonNls - private static final String ELEMENT = "element"; - @NonNls - private static final String MODULE = "module"; - @NonNls - private static final String DEFAULT = "default"; - - public static CopyrightManager getInstance(Project project) { - return project.getComponent(CopyrightManager.class); - } - - @Override - @NonNls - @NotNull - public String getComponentName() { - return "CopyrightManager"; - } - - @Override - public Element getState() { - Element state = new Element("settings"); - - try { - if (!myCopyrights.isEmpty()) { - for (CopyrightProfile copyright : myCopyrights.values()) { - Element copyrightElement = new Element(COPYRIGHT); - copyright.serializeInto(copyrightElement, true); - if (!JDOMUtil.isEmpty(copyrightElement)) { - state.addContent(copyrightElement); - } - } - } - - if (!myModuleToCopyrights.isEmpty()) { - final Element map = new Element(MODULE2COPYRIGHT); - for (String moduleName : myModuleToCopyrights.keySet()) { - final Element setting = new Element(ELEMENT); - setting.setAttribute(MODULE, moduleName); - setting.setAttribute(COPYRIGHT, myModuleToCopyrights.get(moduleName)); - map.addContent(setting); - } - state.addContent(map); - } - - myOptions.writeExternal(state); - } - catch (WriteExternalException e) { - LOG.error(e); - return null; - } - - if (myDefaultCopyright != null) { - state.setAttribute(DEFAULT, myDefaultCopyright.getName()); - } - - return state; - } - - @Override - public void loadState(Element state) { - clearCopyrights(); - - final Element moduleToCopyright = state.getChild(MODULE2COPYRIGHT); - if (moduleToCopyright != null) { - for (Element element : moduleToCopyright.getChildren(ELEMENT)) { - myModuleToCopyrights.put(element.getAttributeValue(MODULE), element.getAttributeValue(COPYRIGHT)); - } - } - - try { - for (Element element : state.getChildren(COPYRIGHT)) { - final CopyrightProfile copyrightProfile = new CopyrightProfile(); - copyrightProfile.readExternal(element); - myCopyrights.put(copyrightProfile.getName(), copyrightProfile); - } - myDefaultCopyright = myCopyrights.get(StringUtil.notNullize(state.getAttributeValue(DEFAULT))); - myOptions.readExternal(state); - } - catch (InvalidDataException e) { - LOG.error(e); - } - } - - public Map getCopyrightsMapping() { - return myModuleToCopyrights; - } - - public void setDefaultCopyright(@Nullable CopyrightProfile copyright) { - myDefaultCopyright = copyright; - } - - @Nullable - public CopyrightProfile getDefaultCopyright() { - return myDefaultCopyright; - } - - public void addCopyright(CopyrightProfile copyrightProfile) { - myCopyrights.put(copyrightProfile.getName(), copyrightProfile); - } - - public void removeCopyright(CopyrightProfile copyrightProfile) { - myCopyrights.values().remove(copyrightProfile); - for (Iterator it = myModuleToCopyrights.keySet().iterator(); it.hasNext();) { - final String profileName = myModuleToCopyrights.get(it.next()); - if (profileName.equals(copyrightProfile.getName())) { - it.remove(); - } - } - } - - public void clearCopyrights() { - myDefaultCopyright = null; - myCopyrights.clear(); - myModuleToCopyrights.clear(); - } - - public void mapCopyright(String scopeName, String copyrightProfileName) { - myModuleToCopyrights.put(scopeName, copyrightProfileName); - } - - public void unmapCopyright(String scopeName) { - myModuleToCopyrights.remove(scopeName); - } - - public Collection getCopyrights() { - return myCopyrights.values(); - } - - public boolean hasAnyCopyrights() { - return myDefaultCopyright != null || !myModuleToCopyrights.isEmpty(); - } - - @Nullable - public CopyrightProfile getCopyrightOptions(@NotNull PsiFile file) { - final VirtualFile virtualFile = file.getVirtualFile(); - if (virtualFile == null || myOptions.getOptions(virtualFile.getFileType().getName()).getFileTypeOverride() == LanguageOptions.NO_COPYRIGHT) return null; - final DependencyValidationManager validationManager = DependencyValidationManager.getInstance(myProject); - for (String scopeName : myModuleToCopyrights.keySet()) { - final NamedScope namedScope = validationManager.getScope(scopeName); - if (namedScope != null) { - final PackageSet packageSet = namedScope.getValue(); - if (packageSet != null) { - if (packageSet.contains(file, validationManager)) { - final CopyrightProfile profile = myCopyrights.get(myModuleToCopyrights.get(scopeName)); - if (profile != null) { - return profile; - } - } - } - } - } - return myDefaultCopyright != null ? myDefaultCopyright : null; - } - - public Options getOptions() { - return myOptions; - } - - public void replaceCopyright(String displayName, CopyrightProfile copyrightProfile) { - if (myDefaultCopyright != null && Comparing.strEqual(myDefaultCopyright.getName(), displayName)) { - myDefaultCopyright = copyrightProfile; - } - myCopyrights.remove(displayName); - addCopyright(copyrightProfile); - } - - static final class CopyrightStateSplitter extends MainConfigurationStateSplitter { - @NotNull - @Override - protected String getComponentStateFileName() { - return "profiles_settings"; - } - - @NotNull - @Override - protected String getSubStateTagName() { - return COPYRIGHT; - } - } -} diff --git a/plugins/copyright/src/com/maddyhome/idea/copyright/actions/AbstractFileProcessor.java b/plugins/copyright/src/com/maddyhome/idea/copyright/actions/AbstractFileProcessor.java index fe6870b349f4..7b020a0e6597 100644 --- a/plugins/copyright/src/com/maddyhome/idea/copyright/actions/AbstractFileProcessor.java +++ b/plugins/copyright/src/com/maddyhome/idea/copyright/actions/AbstractFileProcessor.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2014 JetBrains s.r.o. + * 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. @@ -17,6 +17,7 @@ package com.maddyhome.idea.copyright.actions; import com.intellij.codeInsight.FileModificationService; +import com.intellij.copyright.CopyrightManager; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.application.Result; import com.intellij.openapi.command.WriteCommandAction; @@ -30,13 +31,12 @@ import com.intellij.openapi.roots.ContentIterator; import com.intellij.openapi.roots.ModuleFileIndex; import com.intellij.openapi.roots.ModuleRootManager; import com.intellij.openapi.vfs.ReadonlyStatusHandler; -import com.intellij.openapi.vfs.VfsUtil; +import com.intellij.openapi.vfs.VfsUtilCore; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.PsiDirectory; import com.intellij.psi.PsiFile; import com.intellij.psi.PsiManager; import com.intellij.util.IncorrectOperationException; -import com.maddyhome.idea.copyright.CopyrightManager; import com.maddyhome.idea.copyright.CopyrightProfile; import com.maddyhome.idea.copyright.util.FileTypeUtil; import org.jetbrains.annotations.NotNull; @@ -51,39 +51,11 @@ public abstract class AbstractFileProcessor { private PsiDirectory directory = null; private PsiFile file = null; private PsiFile[] files = null; - private boolean subdirs = false; private final String message; private final String title; protected abstract Runnable preprocessFile(PsiFile file, boolean allowReplacement) throws IncorrectOperationException; - protected AbstractFileProcessor(Project project, String title, String message) { - myProject = project; - myModule = null; - directory = null; - subdirs = true; - this.title = title; - this.message = message; - } - - protected AbstractFileProcessor(Project project, Module module, String title, String message) { - myProject = project; - myModule = module; - directory = null; - subdirs = true; - this.title = title; - this.message = message; - } - - protected AbstractFileProcessor(Project project, PsiDirectory dir, boolean subdirs, String title, String message) { - myProject = project; - myModule = null; - directory = dir; - this.subdirs = subdirs; - this.message = message; - this.title = title; - } - protected AbstractFileProcessor(Project project, PsiFile file, String title, String message) { myProject = project; myModule = null; @@ -92,7 +64,7 @@ public abstract class AbstractFileProcessor { this.title = title; } - protected AbstractFileProcessor(Project project, PsiFile[] files, String title, String message, Runnable runnable) { + protected AbstractFileProcessor(Project project, PsiFile[] files, String title, String message) { myProject = project; myModule = null; this.files = files; @@ -102,7 +74,7 @@ public abstract class AbstractFileProcessor { public void run() { if (directory != null) { - process(directory, subdirs); + process(directory, false); } else if (files != null) { process(files); @@ -275,7 +247,7 @@ public abstract class AbstractFileProcessor { for (PsiFile psiFile : files) { vFiles.add(psiFile.getVirtualFile()); } - if (!ReadonlyStatusHandler.getInstance(myProject).ensureFilesWritable(VfsUtil.toVirtualFileArray(vFiles)) + if (!ReadonlyStatusHandler.getInstance(myProject).ensureFilesWritable(VfsUtilCore.toVirtualFileArray(vFiles)) .hasReadonlyFiles()) { if (!files.isEmpty()) { final Runnable[] resultRunnable = new Runnable[1]; @@ -296,15 +268,12 @@ public abstract class AbstractFileProcessor { if (opts != null && FileTypeUtil.isSupportedFile(local)) { files.add(local); } - } if (subdirs) { - PsiDirectory[] dirs = directory.getSubdirectories(); - for (PsiDirectory dir : dirs) { - findFiles(files, dir, subdirs); + for (PsiDirectory dir : directory.getSubdirectories()) { + findFiles(files, dir, true); } - } } diff --git a/plugins/copyright/src/com/maddyhome/idea/copyright/actions/GenerateCopyrightAction.java b/plugins/copyright/src/com/maddyhome/idea/copyright/actions/GenerateCopyrightAction.java index 2a1b9c9df186..c077ccb6b7b1 100644 --- a/plugins/copyright/src/com/maddyhome/idea/copyright/actions/GenerateCopyrightAction.java +++ b/plugins/copyright/src/com/maddyhome/idea/copyright/actions/GenerateCopyrightAction.java @@ -13,9 +13,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package com.maddyhome.idea.copyright.actions; +import com.intellij.copyright.CopyrightManager; import com.intellij.openapi.actionSystem.*; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.module.Module; @@ -24,7 +24,6 @@ import com.intellij.openapi.project.Project; import com.intellij.openapi.ui.Messages; import com.intellij.psi.PsiDocumentManager; import com.intellij.psi.PsiFile; -import com.maddyhome.idea.copyright.CopyrightManager; import com.maddyhome.idea.copyright.ui.CopyrightProjectConfigurable; import com.maddyhome.idea.copyright.util.FileTypeUtil; import org.jetbrains.annotations.Nullable; diff --git a/plugins/copyright/src/com/maddyhome/idea/copyright/actions/UpdateCopyrightAction.java b/plugins/copyright/src/com/maddyhome/idea/copyright/actions/UpdateCopyrightAction.java index 7d096633ab1d..b219fc4c28a6 100644 --- a/plugins/copyright/src/com/maddyhome/idea/copyright/actions/UpdateCopyrightAction.java +++ b/plugins/copyright/src/com/maddyhome/idea/copyright/actions/UpdateCopyrightAction.java @@ -20,6 +20,7 @@ import com.intellij.analysis.AnalysisScope; import com.intellij.analysis.BaseAnalysisAction; import com.intellij.analysis.BaseAnalysisActionDialog; import com.intellij.codeInsight.FileModificationService; +import com.intellij.copyright.CopyrightManager; import com.intellij.ide.util.PropertiesComponent; import com.intellij.openapi.actionSystem.*; import com.intellij.openapi.command.CommandProcessor; @@ -37,7 +38,6 @@ import com.intellij.psi.*; import com.intellij.ui.TitledSeparator; import com.intellij.util.SequentialModalProgressTask; import com.intellij.util.SequentialTask; -import com.maddyhome.idea.copyright.CopyrightManager; import com.maddyhome.idea.copyright.pattern.FileUtil; import com.maddyhome.idea.copyright.util.FileTypeUtil; import org.jetbrains.annotations.NotNull; diff --git a/plugins/copyright/src/com/maddyhome/idea/copyright/actions/UpdateCopyrightProcessor.java b/plugins/copyright/src/com/maddyhome/idea/copyright/actions/UpdateCopyrightProcessor.java index e2fc2cdfb9db..c11ea431faea 100644 --- a/plugins/copyright/src/com/maddyhome/idea/copyright/actions/UpdateCopyrightProcessor.java +++ b/plugins/copyright/src/com/maddyhome/idea/copyright/actions/UpdateCopyrightProcessor.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * 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. @@ -16,6 +16,7 @@ package com.maddyhome.idea.copyright.actions; +import com.intellij.copyright.CopyrightManager; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.module.Module; import com.intellij.openapi.progress.ProgressIndicator; @@ -24,10 +25,8 @@ import com.intellij.openapi.project.Project; import com.intellij.openapi.roots.ProjectRootManager; import com.intellij.openapi.util.EmptyRunnable; import com.intellij.openapi.vfs.VirtualFile; -import com.intellij.psi.PsiDirectory; import com.intellij.psi.PsiFile; import com.intellij.util.IncorrectOperationException; -import com.maddyhome.idea.copyright.CopyrightManager; import com.maddyhome.idea.copyright.CopyrightProfile; import com.maddyhome.idea.copyright.psi.UpdateCopyright; import com.maddyhome.idea.copyright.psi.UpdateCopyrightFactory; @@ -39,18 +38,6 @@ public class UpdateCopyrightProcessor extends AbstractFileProcessor public static final String TITLE = "Update Copyright"; public static final String MESSAGE = "Updating copyrights..."; - public UpdateCopyrightProcessor(Project project, Module module) - { - super(project, module, TITLE, MESSAGE); - setup(project, module); - } - - public UpdateCopyrightProcessor(Project project, Module module, PsiDirectory dir, boolean subdirs) - { - super(project, dir, subdirs, TITLE, MESSAGE); - setup(project, module); - } - public UpdateCopyrightProcessor(Project project, Module module, PsiFile file) { super(project, file, TITLE, MESSAGE); @@ -59,7 +46,7 @@ public class UpdateCopyrightProcessor extends AbstractFileProcessor public UpdateCopyrightProcessor(Project project, Module module, PsiFile[] files) { - super(project, files, TITLE, MESSAGE, null); + super(project, files, TITLE, MESSAGE); setup(project, module); } diff --git a/plugins/copyright/src/com/maddyhome/idea/copyright/pattern/VelocityHelper.java b/plugins/copyright/src/com/maddyhome/idea/copyright/pattern/VelocityHelper.java index d3c5e74a945d..94fe8871cfd9 100644 --- a/plugins/copyright/src/com/maddyhome/idea/copyright/pattern/VelocityHelper.java +++ b/plugins/copyright/src/com/maddyhome/idea/copyright/pattern/VelocityHelper.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * 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. @@ -16,6 +16,7 @@ package com.maddyhome.idea.copyright.pattern; +import com.intellij.copyright.CopyrightManager; import com.intellij.openapi.application.PathManager; import com.intellij.openapi.module.Module; import com.intellij.openapi.project.Project; @@ -23,7 +24,6 @@ import com.intellij.openapi.util.text.StringUtil; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.PsiFile; import com.intellij.psi.util.PsiUtilCore; -import com.maddyhome.idea.copyright.CopyrightManager; import org.apache.commons.collections.ExtendedProperties; import org.apache.velocity.VelocityContext; import org.apache.velocity.app.VelocityEngine; diff --git a/plugins/copyright/src/com/maddyhome/idea/copyright/psi/AbstractUpdateCopyright.java b/plugins/copyright/src/com/maddyhome/idea/copyright/psi/AbstractUpdateCopyright.java index ad21b02c514a..a67b2ff727d3 100644 --- a/plugins/copyright/src/com/maddyhome/idea/copyright/psi/AbstractUpdateCopyright.java +++ b/plugins/copyright/src/com/maddyhome/idea/copyright/psi/AbstractUpdateCopyright.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * 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. @@ -16,13 +16,13 @@ package com.maddyhome.idea.copyright.psi; +import com.intellij.copyright.CopyrightManager; import com.intellij.openapi.fileTypes.FileType; import com.intellij.openapi.module.Module; import com.intellij.openapi.project.Project; import com.intellij.openapi.util.text.StringUtil; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.PsiManager; -import com.maddyhome.idea.copyright.CopyrightManager; import com.maddyhome.idea.copyright.CopyrightProfile; import com.maddyhome.idea.copyright.options.LanguageOptions; import com.maddyhome.idea.copyright.pattern.EntityUtil; diff --git a/plugins/copyright/src/com/maddyhome/idea/copyright/psi/UpdatePsiFileCopyright.java b/plugins/copyright/src/com/maddyhome/idea/copyright/psi/UpdatePsiFileCopyright.java index 02ee5ccb1502..77827b278585 100644 --- a/plugins/copyright/src/com/maddyhome/idea/copyright/psi/UpdatePsiFileCopyright.java +++ b/plugins/copyright/src/com/maddyhome/idea/copyright/psi/UpdatePsiFileCopyright.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2013 JetBrains s.r.o. + * 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. @@ -16,6 +16,7 @@ package com.maddyhome.idea.copyright.psi; +import com.intellij.copyright.CopyrightManager; import com.intellij.lang.Commenter; import com.intellij.lang.LanguageCommenters; import com.intellij.openapi.command.WriteCommandAction; @@ -31,7 +32,6 @@ import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.*; import com.intellij.psi.util.PsiUtilCore; import com.intellij.util.IncorrectOperationException; -import com.maddyhome.idea.copyright.CopyrightManager; import com.maddyhome.idea.copyright.CopyrightProfile; import com.maddyhome.idea.copyright.options.LanguageOptions; import com.maddyhome.idea.copyright.util.FileTypeUtil; diff --git a/plugins/copyright/src/com/maddyhome/idea/copyright/ui/CopyrightConfigurable.java b/plugins/copyright/src/com/maddyhome/idea/copyright/ui/CopyrightConfigurable.java index d14dcdaf88fa..ae0938cc4fd0 100644 --- a/plugins/copyright/src/com/maddyhome/idea/copyright/ui/CopyrightConfigurable.java +++ b/plugins/copyright/src/com/maddyhome/idea/copyright/ui/CopyrightConfigurable.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * 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. @@ -16,6 +16,7 @@ package com.maddyhome.idea.copyright.ui; +import com.intellij.copyright.CopyrightManager; import com.intellij.openapi.components.ServiceManager; import com.intellij.openapi.editor.SpellCheckingEditorCustomizationProvider; import com.intellij.openapi.fileTypes.FileTypes; @@ -28,12 +29,12 @@ import com.intellij.openapi.util.text.StringUtil; import com.intellij.ui.*; import com.intellij.util.DocumentUtil; import com.intellij.util.containers.ContainerUtil; -import com.maddyhome.idea.copyright.CopyrightManager; import com.maddyhome.idea.copyright.CopyrightProfile; import com.maddyhome.idea.copyright.pattern.EntityUtil; import com.maddyhome.idea.copyright.pattern.VelocityHelper; import org.jetbrains.annotations.Nls; import org.jetbrains.annotations.NonNls; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import javax.swing.*; @@ -59,7 +60,7 @@ public class CopyrightConfigurable extends NamedConfigurable { private JTextField myAllowReplaceTextField; private JPanel myEditorPanel; - public CopyrightConfigurable(Project project, CopyrightProfile copyrightProfile, Runnable updater) { + public CopyrightConfigurable(@NotNull Project project, CopyrightProfile copyrightProfile, Runnable updater) { super(true, updater); myProject = project; myCopyrightProfile = copyrightProfile; @@ -130,6 +131,7 @@ public class CopyrightConfigurable extends NamedConfigurable { catch (PatternSyntaxException e) { throw new ConfigurationException("Keyword pattern syntax is incorrect: " + e.getMessage()); } + myCopyrightProfile.setKeyword(keyword); myCopyrightProfile.setAllowReplaceKeyword(myAllowReplaceTextField.getText().trim()); CopyrightManager.getInstance(myProject).replaceCopyright(myDisplayName, myCopyrightProfile); diff --git a/plugins/copyright/src/com/maddyhome/idea/copyright/ui/CopyrightProfilesPanel.java b/plugins/copyright/src/com/maddyhome/idea/copyright/ui/CopyrightProfilesPanel.java index 57bc6e9260b3..17b6d6753f5e 100644 --- a/plugins/copyright/src/com/maddyhome/idea/copyright/ui/CopyrightProfilesPanel.java +++ b/plugins/copyright/src/com/maddyhome/idea/copyright/ui/CopyrightProfilesPanel.java @@ -13,9 +13,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package com.maddyhome.idea.copyright.ui; +import com.intellij.copyright.CopyrightManager; import com.intellij.openapi.actionSystem.AnAction; import com.intellij.openapi.actionSystem.AnActionEvent; import com.intellij.openapi.actionSystem.CommonShortcuts; @@ -37,14 +37,10 @@ import com.intellij.openapi.ui.Messages; import com.intellij.openapi.ui.popup.JBPopupFactory; import com.intellij.openapi.ui.popup.PopupStep; import com.intellij.openapi.ui.popup.util.BaseListPopupStep; -import com.intellij.openapi.util.Condition; import com.intellij.openapi.vfs.VfsUtilCore; -import com.intellij.openapi.vfs.VirtualFile; -import com.intellij.util.Consumer; import com.intellij.util.IconUtil; import com.intellij.util.PlatformIcons; import com.intellij.util.containers.HashMap; -import com.maddyhome.idea.copyright.CopyrightManager; import com.maddyhome.idea.copyright.CopyrightProfile; import com.maddyhome.idea.copyright.options.ExternalOptionHelper; import org.jetbrains.annotations.Nls; @@ -90,15 +86,11 @@ public class CopyrightProfilesPanel extends MasterDetailsComponent implements Se @Override protected void processRemovedItems() { Map profiles = getAllProfiles(); - final List deleted = new ArrayList<>(); - for (CopyrightProfile profile : myManager.getCopyrights()) { + for (CopyrightProfile profile : new ArrayList<>(myManager.getCopyrights())) { if (!profiles.containsValue(profile)) { - deleted.add(profile); + myManager.removeCopyright(profile); } } - for (CopyrightProfile profile : deleted) { - myManager.removeCopyright(profile); - } } @Override @@ -174,10 +166,10 @@ public class CopyrightProfilesPanel extends MasterDetailsComponent implements Se @Override public void actionPerformed(AnActionEvent event) { - final String name = askForProfileName("Create Copyright Profile", ""); - if (name == null) return; - final CopyrightProfile copyrightProfile = new CopyrightProfile(name); - addProfileNode(copyrightProfile); + String name = askForProfileName("Create Copyright Profile", ""); + if (name != null) { + addProfileNode(new CopyrightProfile(name)); + } } }); result.add(new MyDeleteAction()); @@ -188,9 +180,12 @@ public class CopyrightProfilesPanel extends MasterDetailsComponent implements Se @Override public void actionPerformed(AnActionEvent event) { - final String profileName = askForProfileName("Copy Copyright Profile", ""); - if (profileName == null) return; - final CopyrightProfile clone = new CopyrightProfile(); + String profileName = askForProfileName("Copy Copyright Profile", ""); + if (profileName == null) { + return; + } + + CopyrightProfile clone = new CopyrightProfile(); clone.copyFrom((CopyrightProfile)getSelectedObject()); clone.setName(profileName); addProfileNode(clone); @@ -268,7 +263,7 @@ public class CopyrightProfilesPanel extends MasterDetailsComponent implements Se }); } - private void addProfileNode(CopyrightProfile copyrightProfile) { + private void addProfileNode(@NotNull CopyrightProfile copyrightProfile) { final CopyrightConfigurable copyrightConfigurable = new CopyrightConfigurable(myProject, copyrightProfile, TREE_UPDATER); copyrightConfigurable.setModified(true); final MyNode node = new MyNode(copyrightConfigurable); diff --git a/plugins/copyright/src/com/maddyhome/idea/copyright/ui/ProjectSettingsPanel.java b/plugins/copyright/src/com/maddyhome/idea/copyright/ui/ProjectSettingsPanel.java index 541922f92269..e26f64eacc20 100644 --- a/plugins/copyright/src/com/maddyhome/idea/copyright/ui/ProjectSettingsPanel.java +++ b/plugins/copyright/src/com/maddyhome/idea/copyright/ui/ProjectSettingsPanel.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2015 JetBrains s.r.o. + * 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. @@ -15,15 +15,16 @@ */ package com.maddyhome.idea.copyright.ui; +import com.intellij.copyright.CopyrightManager; import com.intellij.ide.DataManager; import com.intellij.ide.util.scopeChooser.PackageSetChooserCombo; import com.intellij.ide.util.scopeChooser.ScopeChooserConfigurable; import com.intellij.openapi.actionSystem.DataContext; import com.intellij.openapi.options.ex.Settings; import com.intellij.openapi.project.Project; +import com.intellij.openapi.ui.ComboBox; import com.intellij.openapi.ui.LabeledComponent; import com.intellij.openapi.util.Comparing; -import com.intellij.packageDependencies.DefaultScopesProvider; import com.intellij.packageDependencies.DependencyValidationManager; import com.intellij.psi.search.scope.packageSet.CustomScopesProviderEx; import com.intellij.psi.search.scope.packageSet.NamedScope; @@ -34,9 +35,7 @@ import com.intellij.ui.ListCellRendererWrapper; import com.intellij.ui.ToolbarDecorator; import com.intellij.ui.components.editors.JBComboBoxTableCellEditorComponent; import com.intellij.ui.table.TableView; -import com.intellij.util.Function; import com.intellij.util.ui.*; -import com.maddyhome.idea.copyright.CopyrightManager; import com.maddyhome.idea.copyright.CopyrightProfile; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -58,7 +57,7 @@ public class ProjectSettingsPanel { private final TableView myScopeMappingTable; private final ListTableModel myScopeMappingModel; - private final JComboBox myProfilesComboBox = new JComboBox(); + private final JComboBox myProfilesComboBox = new ComboBox(); private final HyperlinkLabel myScopesLink = new HyperlinkLabel(); @@ -135,7 +134,7 @@ public class ProjectSettingsPanel { ElementProducer producer = new ElementProducer() { @Override public ScopeSetting createElement() { - return new ScopeSetting(DefaultScopesProvider.getAllScope(), myProfilesModel.getAllProfiles().values().iterator().next()); + return new ScopeSetting(CustomScopesProviderEx.getAllScope(), myProfilesModel.getAllProfiles().values().iterator().next()); } @Override @@ -158,7 +157,7 @@ public class ProjectSettingsPanel { if (defaultCopyright == null) return true; if (!defaultCopyright.equals(selected)) return true; } - final Map map = myManager.getCopyrightsMapping(); + final Map map = myManager.getScopeToCopyright(); if (map.size() != myScopeMappingModel.getItems().size()) return true; final Iterator iterator = map.keySet().iterator(); for (ScopeSetting setting : myScopeMappingModel.getItems()) { @@ -174,22 +173,17 @@ public class ProjectSettingsPanel { } public void apply() { - Collection profiles = new ArrayList<>(myManager.getCopyrights()); - myManager.clearCopyrights(); - for (CopyrightProfile profile : profiles) { - myManager.addCopyright(profile); - } - final List settingList = myScopeMappingModel.getItems(); - for (ScopeSetting scopeSetting : settingList) { + myManager.setDefaultCopyright((CopyrightProfile)myProfilesComboBox.getSelectedItem()); + myManager.clearMappings(); + for (ScopeSetting scopeSetting : myScopeMappingModel.getItems()) { myManager.mapCopyright(scopeSetting.getScope().getName(), scopeSetting.getProfileName()); } - myManager.setDefaultCopyright((CopyrightProfile)myProfilesComboBox.getSelectedItem()); } public void reset() { myProfilesComboBox.setSelectedItem(myManager.getDefaultCopyright()); final List mappings = new ArrayList<>(); - final Map copyrights = myManager.getCopyrightsMapping(); + final Map copyrights = myManager.getScopeToCopyright(); final DependencyValidationManager manager = DependencyValidationManager.getInstance(myProject); final Set scopes2Unmap = new HashSet<>(); for (final String scopeName : copyrights.keySet()) { @@ -207,7 +201,6 @@ public class ProjectSettingsPanel { myScopeMappingModel.setItems(mappings); } - private class ScopeSetting { private NamedScope myScope; private CopyrightProfile myProfile; diff --git a/plugins/copyright/src/com/maddyhome/idea/copyright/ui/TemplateCommentPanel.java b/plugins/copyright/src/com/maddyhome/idea/copyright/ui/TemplateCommentPanel.java index 515afd30f3f0..ba2231a3ab8b 100644 --- a/plugins/copyright/src/com/maddyhome/idea/copyright/ui/TemplateCommentPanel.java +++ b/plugins/copyright/src/com/maddyhome/idea/copyright/ui/TemplateCommentPanel.java @@ -16,6 +16,7 @@ package com.maddyhome.idea.copyright.ui; +import com.intellij.copyright.CopyrightManager; import com.intellij.openapi.editor.colors.EditorColorsManager; import com.intellij.openapi.editor.colors.EditorFontType; import com.intellij.openapi.fileTypes.FileType; @@ -27,7 +28,6 @@ import com.intellij.openapi.project.Project; import com.intellij.ui.DocumentAdapter; import com.intellij.uiDesigner.core.GridConstraints; import com.intellij.util.ui.UIUtil; -import com.maddyhome.idea.copyright.CopyrightManager; import com.maddyhome.idea.copyright.CopyrightProfile; import com.maddyhome.idea.copyright.options.LanguageOptions; import com.maddyhome.idea.copyright.options.Options; @@ -82,10 +82,7 @@ public class TemplateCommentPanel implements SearchableConfigurable { private void updateBox() { boolean enable = true; - if (!cbSeparatorBefore.isSelected()) { - enable = false; - } - else if (!cbSeparatorAfter.isSelected()) { + if (!cbSeparatorBefore.isSelected() || !cbSeparatorAfter.isSelected()) { enable = false; } else { @@ -336,8 +333,8 @@ public class TemplateCommentPanel implements SearchableConfigurable { mySeparatorCharLabel.setEnabled(true); updateBox(); } else { - UIUtil.setEnabled(myCommentTypePanel, enable, true); - UIUtil.setEnabled(myBorderPanel, enable, true); + UIUtil.setEnabled(myCommentTypePanel, false, true); + UIUtil.setEnabled(myBorderPanel, false, true); } } diff --git a/plugins/copyright/src/com/maddyhome/idea/copyright/util/NewFileTracker.java b/plugins/copyright/src/com/maddyhome/idea/copyright/util/NewFileTracker.java index ad68be5d10b6..84a91150c35a 100644 --- a/plugins/copyright/src/com/maddyhome/idea/copyright/util/NewFileTracker.java +++ b/plugins/copyright/src/com/maddyhome/idea/copyright/util/NewFileTracker.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2014 JetBrains s.r.o. + * 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. @@ -24,17 +24,14 @@ import java.util.Collections; import java.util.Set; public class NewFileTracker { - public static NewFileTracker getInstance() { - return instance; - } + private final Set newFiles = Collections.synchronizedSet(new THashSet()); public boolean poll(@NotNull VirtualFile file) { return newFiles.remove(file); } - private NewFileTracker() { - final VirtualFileManager virtualFileManager = VirtualFileManager.getInstance(); - virtualFileManager.addVirtualFileListener(new VirtualFileAdapter() { + public NewFileTracker() { + VirtualFileManager.getInstance().addVirtualFileListener(new VirtualFileAdapter() { @Override public void fileCreated(@NotNull VirtualFileEvent event) { if (event.isFromRefresh()) return; @@ -49,9 +46,6 @@ public class NewFileTracker { }); } - private final Set newFiles = Collections.synchronizedSet(new THashSet()); - private static final NewFileTracker instance = new NewFileTracker(); - public void clear() { newFiles.clear(); }