From 00f2fb76480c9545f893396eb3f0a352caa18960 Mon Sep 17 00:00:00 2001 From: Alexey Kudravtsev Date: Fri, 14 Jun 2013 13:57:41 +0400 Subject: [PATCH] decouple InspectionProjectProfileManager from status bar --- .../profile/DefaultProjectProfileManager.java | 16 +- .../profile/ProfileChangeAdapter.java | 6 +- .../daemon/impl/DaemonListeners.java | 36 ++- .../daemon/impl/HectorComponent.java | 3 +- .../ide/actions/TogglePopupHintsAction.java | 4 +- .../InspectionProfileManagerImpl.java | 9 +- .../InspectionProjectProfileManagerImpl.java | 284 ------------------ 7 files changed, 62 insertions(+), 296 deletions(-) delete mode 100644 platform/lang-impl/src/com/intellij/profile/codeInspection/InspectionProjectProfileManagerImpl.java diff --git a/platform/analysis-api/src/com/intellij/profile/DefaultProjectProfileManager.java b/platform/analysis-api/src/com/intellij/profile/DefaultProjectProfileManager.java index 1ccdade0cb78..e08009d56fdb 100644 --- a/platform/analysis-api/src/com/intellij/profile/DefaultProjectProfileManager.java +++ b/platform/analysis-api/src/com/intellij/profile/DefaultProjectProfileManager.java @@ -127,8 +127,8 @@ public abstract class DefaultProjectProfileManager extends ProjectProfileManager public synchronized void writeExternal(Element element) throws WriteExternalException { final List sortedProfiles = new ArrayList(myProfiles.keySet()); - Element profiles = null; Collections.sort(sortedProfiles); + Element profiles = null; for (String profile : sortedProfiles) { final Profile projectProfile = myProfiles.get(profile); if (projectProfile != null) { @@ -227,7 +227,7 @@ public abstract class DefaultProjectProfileManager extends ProjectProfileManager return profile; } - public void addProfilesListener(final ProfileChangeAdapter profilesListener, Disposable parent) { + public void addProfilesListener(@NotNull final ProfileChangeAdapter profilesListener, @NotNull Disposable parent) { myProfilesListener.add(profilesListener); Disposer.register(parent, new Disposable() { @Override @@ -237,7 +237,7 @@ public abstract class DefaultProjectProfileManager extends ProjectProfileManager }); } - public void removeProfilesListener(ProfileChangeAdapter profilesListener) { + public void removeProfilesListener(@NotNull ProfileChangeAdapter profilesListener) { myProfilesListener.remove(profilesListener); } @@ -299,4 +299,14 @@ public abstract class DefaultProjectProfileManager extends ProjectProfileManager } } + protected void fireProfilesInitialized() { + for (ProfileChangeAdapter profileChangeAdapter : myProfilesListener) { + profileChangeAdapter.profilesInitialized(); + } + } + protected void fireProfilesShutdown() { + for (ProfileChangeAdapter profileChangeAdapter : myProfilesListener) { + profileChangeAdapter.profilesShutdown(); + } + } } diff --git a/platform/analysis-api/src/com/intellij/profile/ProfileChangeAdapter.java b/platform/analysis-api/src/com/intellij/profile/ProfileChangeAdapter.java index 540ef02ec3e9..7e77e3de44ac 100644 --- a/platform/analysis-api/src/com/intellij/profile/ProfileChangeAdapter.java +++ b/platform/analysis-api/src/com/intellij/profile/ProfileChangeAdapter.java @@ -15,11 +15,15 @@ */ package com.intellij.profile; +import org.jetbrains.annotations.NotNull; + /** * User: anna * Date: 20-Nov-2005 */ public abstract class ProfileChangeAdapter { public void profileChanged(Profile profile){} - public void profileActivated(Profile oldProfile, Profile profile){} + public void profileActivated(@NotNull Profile oldProfile, Profile profile){} + public void profilesInitialized() {} + public void profilesShutdown(){} } diff --git a/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/DaemonListeners.java b/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/DaemonListeners.java index fd04257d7b91..da15ba64e73c 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/DaemonListeners.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/DaemonListeners.java @@ -19,6 +19,7 @@ package com.intellij.codeInsight.daemon.impl; import com.intellij.ProjectTopics; import com.intellij.codeHighlighting.Pass; import com.intellij.codeInsight.daemon.DaemonCodeAnalyzer; +import com.intellij.codeInsight.daemon.impl.analysis.HighlightingSettingsPerFile; import com.intellij.codeInsight.hint.TooltipController; import com.intellij.ide.PowerSaveMode; import com.intellij.ide.todo.TodoConfiguration; @@ -59,6 +60,9 @@ import com.intellij.openapi.vfs.VirtualFile; import com.intellij.openapi.vfs.VirtualFileAdapter; import com.intellij.openapi.vfs.VirtualFileManager; import com.intellij.openapi.vfs.VirtualFilePropertyEvent; +import com.intellij.openapi.wm.WindowManager; +import com.intellij.openapi.wm.ex.StatusBarEx; +import com.intellij.openapi.wm.impl.status.TogglePopupHintsPanel; import com.intellij.packageDependencies.DependencyValidationManager; import com.intellij.profile.Profile; import com.intellij.profile.ProfileChangeAdapter; @@ -504,9 +508,38 @@ public class DaemonListeners implements Disposable { } @Override - public void profileActivated(Profile oldProfile, Profile profile) { + public void profileActivated(@NotNull Profile oldProfile, Profile profile) { stopDaemonAndRestartAllFiles(); } + + @Override + public void profilesInitialized() { + inspectionProfilesInitialized(); + } + + @Override + public void profilesShutdown() { + HighlightingSettingsPerFile.getInstance(myProject).cleanProfileSettings(); + } + } + + private TogglePopupHintsPanel myTogglePopupHintsPanel; + private void inspectionProfilesInitialized() { + UIUtil.invokeLaterIfNeeded(new Runnable() { + @Override + public void run() { + if (myProject.isDisposed()) return; + StatusBarEx statusBar = (StatusBarEx)WindowManager.getInstance().getStatusBar(myProject); + myTogglePopupHintsPanel = new TogglePopupHintsPanel(myProject); + statusBar.addWidget(myTogglePopupHintsPanel, myProject); + + stopDaemonAndRestartAllFiles(); + } + }); + } + + public void updateStatusBar() { + if (myTogglePopupHintsPanel != null) myTogglePopupHintsPanel.updateStatus(); } private class MyAnActionListener implements AnActionListener { @@ -594,5 +627,4 @@ public class DaemonListeners implements Disposable { myDaemonEventPublisher.daemonCancelEventOccurred(); myDaemonCodeAnalyzer.restart(); } - } diff --git a/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/HectorComponent.java b/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/HectorComponent.java index 1d3e2ccdd3b7..2e6f6cf9ecd8 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/HectorComponent.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/HectorComponent.java @@ -36,7 +36,6 @@ import com.intellij.openapi.ui.popup.JBPopupFactory; import com.intellij.openapi.util.Computable; import com.intellij.openapi.util.Disposer; import com.intellij.openapi.vfs.VirtualFile; -import com.intellij.profile.codeInspection.InspectionProjectProfileManagerImpl; import com.intellij.profile.codeInspection.ui.ErrorsConfigurable; import com.intellij.psi.FileViewProvider; import com.intellij.psi.PsiElement; @@ -272,7 +271,7 @@ public class HectorComponent extends JPanel { } } forceDaemonRestart(); - InspectionProjectProfileManagerImpl.getInstanceImpl(myFile.getProject()).updateStatusBar(); + DaemonListeners.getInstance(myFile.getProject()).updateStatusBar(); } } diff --git a/platform/lang-impl/src/com/intellij/ide/actions/TogglePopupHintsAction.java b/platform/lang-impl/src/com/intellij/ide/actions/TogglePopupHintsAction.java index 3026460ae69f..db63dc84c272 100644 --- a/platform/lang-impl/src/com/intellij/ide/actions/TogglePopupHintsAction.java +++ b/platform/lang-impl/src/com/intellij/ide/actions/TogglePopupHintsAction.java @@ -20,6 +20,7 @@ package com.intellij.ide.actions; import com.intellij.codeInsight.daemon.DaemonCodeAnalyzer; +import com.intellij.codeInsight.daemon.impl.DaemonListeners; import com.intellij.openapi.actionSystem.AnAction; import com.intellij.openapi.actionSystem.AnActionEvent; import com.intellij.openapi.actionSystem.DataContext; @@ -28,7 +29,6 @@ import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.fileEditor.FileEditorManager; import com.intellij.openapi.project.Project; import com.intellij.openapi.vfs.VirtualFile; -import com.intellij.profile.codeInspection.InspectionProjectProfileManagerImpl; import com.intellij.psi.PsiFile; import com.intellij.psi.PsiManager; @@ -63,6 +63,6 @@ public class TogglePopupHintsAction extends AnAction{ LOG.assertTrue(project!=null); DaemonCodeAnalyzer codeAnalyzer = DaemonCodeAnalyzer.getInstance(project); codeAnalyzer.setImportHintsEnabled(psiFile,!codeAnalyzer.isImportHintsEnabled(psiFile)); - InspectionProjectProfileManagerImpl.getInstanceImpl(project).updateStatusBar(); + DaemonListeners.getInstance(project).updateStatusBar(); } } diff --git a/platform/lang-impl/src/com/intellij/profile/codeInspection/InspectionProfileManagerImpl.java b/platform/lang-impl/src/com/intellij/profile/codeInspection/InspectionProfileManagerImpl.java index 2ff5d88615ed..2445dee7728d 100644 --- a/platform/lang-impl/src/com/intellij/profile/codeInspection/InspectionProfileManagerImpl.java +++ b/platform/lang-impl/src/com/intellij/profile/codeInspection/InspectionProfileManagerImpl.java @@ -17,6 +17,7 @@ package com.intellij.profile.codeInspection; import com.intellij.codeHighlighting.HighlightDisplayLevel; import com.intellij.codeInsight.daemon.InspectionProfileConvertor; +import com.intellij.codeInsight.daemon.impl.DaemonListeners; import com.intellij.codeInsight.daemon.impl.HighlightInfoType; import com.intellij.codeInsight.daemon.impl.SeveritiesProvider; import com.intellij.codeInsight.daemon.impl.SeverityRegistrar; @@ -127,6 +128,7 @@ public class InspectionProfileManagerImpl extends InspectionProfileManager imple mySchemesManager = schemesManagerFactory.createSchemesManager(FILE_SPEC, processor, RoamingType.PER_USER); } + @NotNull private static InspectionProfileImpl createSampleProfile() { return new InspectionProfileImpl("Default"); } @@ -317,6 +319,7 @@ public class InspectionProfileManagerImpl extends InspectionProfileManager imple } } + @NotNull @Override public Profile getRootProfile() { Profile current = mySchemesManager.getCurrentScheme(); @@ -335,7 +338,7 @@ public class InspectionProfileManagerImpl extends InspectionProfileManager imple } @Override - public void addProfile(final Profile profile) { + public void addProfile(@NotNull final Profile profile) { mySchemesManager.addNewScheme(profile, true); } @@ -363,7 +366,9 @@ public class InspectionProfileManagerImpl extends InspectionProfileManager imple UIUtil.invokeLaterIfNeeded(new Runnable() { @Override public void run() { - InspectionProjectProfileManagerImpl.getInstanceImpl(project).updateStatusBar(); + if (!project.isDisposed()) { + DaemonListeners.getInstance(project).updateStatusBar(); + } } }); } diff --git a/platform/lang-impl/src/com/intellij/profile/codeInspection/InspectionProjectProfileManagerImpl.java b/platform/lang-impl/src/com/intellij/profile/codeInspection/InspectionProjectProfileManagerImpl.java deleted file mode 100644 index bd144ad2ead8..000000000000 --- a/platform/lang-impl/src/com/intellij/profile/codeInspection/InspectionProjectProfileManagerImpl.java +++ /dev/null @@ -1,284 +0,0 @@ -/* - * Copyright 2000-2013 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.profile.codeInspection; - -import com.intellij.codeInsight.daemon.DaemonCodeAnalyzer; -import com.intellij.codeInsight.daemon.impl.SeverityRegistrar; -import com.intellij.codeInsight.daemon.impl.analysis.HighlightingSettingsPerFile; -import com.intellij.codeInspection.InspectionProfile; -import com.intellij.codeInspection.ex.InspectionProfileImpl; -import com.intellij.codeInspection.ex.InspectionProfileWrapper; -import com.intellij.openapi.Disposable; -import com.intellij.openapi.application.Application; -import com.intellij.openapi.application.ApplicationManager; -import com.intellij.openapi.components.*; -import com.intellij.openapi.project.DumbAwareRunnable; -import com.intellij.openapi.project.Project; -import com.intellij.openapi.startup.StartupManager; -import com.intellij.openapi.util.Disposer; -import com.intellij.openapi.util.InvalidDataException; -import com.intellij.openapi.util.WriteExternalException; -import com.intellij.openapi.wm.WindowManager; -import com.intellij.openapi.wm.ex.StatusBarEx; -import com.intellij.openapi.wm.impl.status.TogglePopupHintsPanel; -import com.intellij.packageDependencies.DependencyValidationManager; -import com.intellij.profile.Profile; -import com.intellij.psi.search.scope.packageSet.NamedScopeManager; -import com.intellij.psi.search.scope.packageSet.NamedScopesHolder; -import com.intellij.util.ui.UIUtil; -import org.jdom.Element; -import org.jetbrains.annotations.NonNls; -import org.jetbrains.annotations.NotNull; - -import java.util.HashSet; -import java.util.Map; -import java.util.Set; -import java.util.concurrent.ConcurrentHashMap; - -/** - * User: anna - * Date: 30-Nov-2005 - */ -@State( - name = "InspectionProjectProfileManager", - storages = { - @Storage( - file = StoragePathMacros.PROJECT_FILE - ) - ,@Storage( file = StoragePathMacros.PROJECT_CONFIG_DIR + "/inspectionProfiles/", scheme = StorageScheme.DIRECTORY_BASED, stateSplitter = InspectionProjectProfileManagerImpl.ProfileStateSplitter.class) - } -) -public class InspectionProjectProfileManagerImpl extends InspectionProjectProfileManager implements SeverityProvider, ProjectComponent, PersistentStateComponent { - private final Map myName2Profile = new ConcurrentHashMap(); - private final SeverityRegistrar mySeverityRegistrar; - private final NamedScopeManager myLocalScopesHolder; - private TogglePopupHintsPanel myTogglePopupHintsPanel; - private NamedScopesHolder.ScopeListener myScopeListener; - - public InspectionProjectProfileManagerImpl(final Project project, - InspectionProfileManager inspectionProfileManager, - DependencyValidationManager holder, - NamedScopeManager localScopesHolder) { - super(project, inspectionProfileManager, holder); - myLocalScopesHolder = localScopesHolder; - mySeverityRegistrar = new SeverityRegistrar(); - } - - public static InspectionProjectProfileManagerImpl getInstanceImpl(Project project){ - return (InspectionProjectProfileManagerImpl)project.getComponent(InspectionProjectProfileManager.class); - } - - @Override - public String getProfileName() { - return getInspectionProfile().getName(); - } - - @Override - public Element getState() { - try { - final Element e = new Element("settings"); - writeExternal(e); - return e; - } - catch (WriteExternalException e1) { - LOG.error(e1); - return null; - } - } - - @Override - public void loadState(Element state) { - try { - readExternal(state); - } - catch (InvalidDataException e) { - LOG.error(e); - } - } - - @Override - public boolean isProfileLoaded() { - return myName2Profile.containsKey(getInspectionProfile().getName()); - } - - @NotNull - public synchronized InspectionProfileWrapper getProfileWrapper(){ - final InspectionProfile profile = getInspectionProfile(); - final String profileName = profile.getName(); - if (!myName2Profile.containsKey(profileName)){ - initProfileWrapper(profile); - } - return myName2Profile.get(profileName); - } - - public InspectionProfileWrapper getProfileWrapper(final String profileName){ - return myName2Profile.get(profileName); - } - - @Override - public void updateProfile(@NotNull Profile profile) { - super.updateProfile(profile); - initProfileWrapper(profile); - } - - @Override - public void deleteProfile(String name) { - super.deleteProfile(name); - final InspectionProfileWrapper profileWrapper = myName2Profile.remove(name); - if (profileWrapper != null) { - profileWrapper.cleanup(myProject); - } - } - - @Override - @NotNull - @NonNls - public String getComponentName() { - return "InspectionProjectProfileManager"; - } - - @Override - public void initComponent() { - } - - @Override - public void disposeComponent() { - } - - @Override - public void projectOpened() { - StatusBarEx statusBar = (StatusBarEx)WindowManager.getInstance().getStatusBar(myProject); - myTogglePopupHintsPanel = new TogglePopupHintsPanel(myProject); - statusBar.addWidget(myTogglePopupHintsPanel, myProject); - StartupManager.getInstance(myProject).registerPostStartupActivity(new DumbAwareRunnable() { - @Override - public void run() { - final Set profiles = new HashSet(); - profiles.add(getProjectProfileImpl()); - profiles.addAll(getProfiles()); - profiles.addAll(InspectionProfileManager.getInstance().getProfiles()); - final Application app = ApplicationManager.getApplication(); - Runnable initInspectionProfilesRunnable = new Runnable() { - @Override - public void run() { - for (Profile profile : profiles) { - initProfileWrapper(profile); - } - //restart daemon when profiles are ready - ApplicationManager.getApplication().invokeLater(new Runnable() { - @Override - public void run() { - DaemonCodeAnalyzer.getInstance(myProject).restart(); - } - }, myProject.getDisposed()); - } - }; - if (app.isUnitTestMode() || app.isHeadlessEnvironment()) { - initInspectionProfilesRunnable.run(); - UIUtil.dispatchAllInvocationEvents(); //do not restart daemon in the middle of the test - } else { - app.executeOnPooledThread(initInspectionProfilesRunnable); - } - myScopeListener = new NamedScopesHolder.ScopeListener() { - @Override - public void scopesChanged() { - for (Profile profile : getProfiles()) { - ((InspectionProfile)profile).scopesChanged(); - } - } - }; - myHolder.addScopeListener(myScopeListener); - myLocalScopesHolder.addScopeListener(myScopeListener); - Disposer.register(myProject, new Disposable() { - @Override - public void dispose() { - myHolder.removeScopeListener(myScopeListener); - myLocalScopesHolder.removeScopeListener(myScopeListener); - } - }); - } - }); - } - - @Override - public void initProfileWrapper(@NotNull Profile profile) { - final InspectionProfileWrapper wrapper = new InspectionProfileWrapper((InspectionProfile)profile); - wrapper.init(myProject); - myName2Profile.put(profile.getName(), wrapper); - } - - @Override - public void projectClosed() { - - final Application app = ApplicationManager.getApplication(); - Runnable cleanupInspectionProfilesRunnable = new Runnable() { - @Override - public void run() { - for (InspectionProfileWrapper wrapper : myName2Profile.values()) { - wrapper.cleanup(myProject); - } - } - }; - if (app.isUnitTestMode() || app.isHeadlessEnvironment()) { - cleanupInspectionProfilesRunnable.run(); - } - else { - app.executeOnPooledThread(cleanupInspectionProfilesRunnable); - } - HighlightingSettingsPerFile.getInstance(myProject).cleanProfileSettings(); - } - - @NotNull - @Override - public SeverityRegistrar getSeverityRegistrar() { - return mySeverityRegistrar; - } - - @NotNull - @Override - public SeverityRegistrar getOwnSeverityRegistrar() { - return mySeverityRegistrar; - } - - @Override - public void readExternal(final Element element) throws InvalidDataException { - mySeverityRegistrar.readExternal(element); - super.readExternal(element); - } - - @Override - public void writeExternal(final Element element) throws WriteExternalException { - super.writeExternal(element); - mySeverityRegistrar.writeExternal(element); - } - - public void updateStatusBar() { - if (myTogglePopupHintsPanel != null) myTogglePopupHintsPanel.updateStatus(); - } - - @Override - public Profile getProfile(@NotNull final String name) { - return getProfile(name, true); - } - - @Override - public void convert(Element element) throws InvalidDataException { - super.convert(element); - if (PROJECT_PROFILE != null) { - ((InspectionProfileImpl)getProjectProfileImpl()).convert(element); - } - } -}