From c69719b357c292c2ac856346dc55d8bce723f239 Mon Sep 17 00:00:00 2001 From: "Gregory.Shrago" Date: Fri, 17 Jan 2014 18:43:51 +0400 Subject: [PATCH] RecentProjectManager -> applicationService --- .../intellij/ide/RecentProjectsManager.java | 5 +- .../ide/RecentDirectoryProjectsManagerEx.java | 5 +- .../ide/RecentDirectoryProjectsManager.java | 5 +- .../ide/RecentProjectsManagerBase.java | 203 +++++++++--------- .../project/impl/ProjectManagerImpl.java | 9 +- .../src/META-INF/PlatformLangPlugin.xml | 3 + .../src/META-INF/PlatformPlugin.xml | 3 + .../src/componentSets/PlatformComponents.xml | 6 +- .../componentSets/PlatformLangComponents.xml | 4 - resources/src/idea/RichPlatformPlugin.xml | 10 +- 10 files changed, 129 insertions(+), 124 deletions(-) diff --git a/java/idea-ui/src/com/intellij/ide/RecentProjectsManager.java b/java/idea-ui/src/com/intellij/ide/RecentProjectsManager.java index 0e3079558437..6ac07343357a 100644 --- a/java/idea-ui/src/com/intellij/ide/RecentProjectsManager.java +++ b/java/idea-ui/src/com/intellij/ide/RecentProjectsManager.java @@ -21,7 +21,6 @@ import com.intellij.openapi.components.State; import com.intellij.openapi.components.Storage; import com.intellij.openapi.components.StoragePathMacros; import com.intellij.openapi.project.Project; -import com.intellij.openapi.project.ProjectManager; import com.intellij.util.messages.MessageBus; import org.jetbrains.annotations.NotNull; @@ -34,8 +33,8 @@ import org.jetbrains.annotations.NotNull; )} ) public class RecentProjectsManager extends RecentProjectsManagerBase { - public RecentProjectsManager(final ProjectManager projectManager, final MessageBus messageBus) { - super(projectManager, messageBus); + public RecentProjectsManager(MessageBus messageBus) { + super(messageBus); } protected String getProjectPath(@NotNull Project project) { diff --git a/platform/lang-impl/src/com/intellij/ide/RecentDirectoryProjectsManagerEx.java b/platform/lang-impl/src/com/intellij/ide/RecentDirectoryProjectsManagerEx.java index 7a2bd3f81eed..21207519fb6b 100644 --- a/platform/lang-impl/src/com/intellij/ide/RecentDirectoryProjectsManagerEx.java +++ b/platform/lang-impl/src/com/intellij/ide/RecentDirectoryProjectsManagerEx.java @@ -20,7 +20,6 @@ import com.intellij.openapi.components.State; import com.intellij.openapi.components.Storage; import com.intellij.openapi.components.StoragePathMacros; import com.intellij.openapi.project.Project; -import com.intellij.openapi.project.ProjectManager; import com.intellij.platform.ModuleAttachProcessor; import com.intellij.util.messages.MessageBus; import org.jetbrains.annotations.NotNull; @@ -37,8 +36,8 @@ import org.jetbrains.annotations.NotNull; )} ) public class RecentDirectoryProjectsManagerEx extends RecentDirectoryProjectsManager { - public RecentDirectoryProjectsManagerEx(ProjectManager projectManager, MessageBus messageBus) { - super(projectManager, messageBus); + public RecentDirectoryProjectsManagerEx(MessageBus messageBus) { + super(messageBus); } @NotNull diff --git a/platform/platform-impl/src/com/intellij/ide/RecentDirectoryProjectsManager.java b/platform/platform-impl/src/com/intellij/ide/RecentDirectoryProjectsManager.java index bdae0170338d..4189b6adf98b 100644 --- a/platform/platform-impl/src/com/intellij/ide/RecentDirectoryProjectsManager.java +++ b/platform/platform-impl/src/com/intellij/ide/RecentDirectoryProjectsManager.java @@ -20,7 +20,6 @@ import com.intellij.openapi.components.State; import com.intellij.openapi.components.Storage; import com.intellij.openapi.components.StoragePathMacros; import com.intellij.openapi.project.Project; -import com.intellij.openapi.project.ProjectManager; import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.vfs.LocalFileSystem; import com.intellij.openapi.vfs.VirtualFile; @@ -42,8 +41,8 @@ import org.jetbrains.annotations.Nullable; )} ) public class RecentDirectoryProjectsManager extends RecentProjectsManagerBase { - public RecentDirectoryProjectsManager(ProjectManager projectManager, MessageBus messageBus) { - super(projectManager, messageBus); + public RecentDirectoryProjectsManager(MessageBus messageBus) { + super(messageBus); } @Nullable diff --git a/platform/platform-impl/src/com/intellij/ide/RecentProjectsManagerBase.java b/platform/platform-impl/src/com/intellij/ide/RecentProjectsManagerBase.java index 3c46db280b43..e77e289f1740 100644 --- a/platform/platform-impl/src/com/intellij/ide/RecentProjectsManagerBase.java +++ b/platform/platform-impl/src/com/intellij/ide/RecentProjectsManagerBase.java @@ -21,9 +21,11 @@ import com.intellij.openapi.actionSystem.CommonDataKeys; import com.intellij.openapi.actionSystem.Separator; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.components.PersistentStateComponent; +import com.intellij.openapi.components.ServiceManager; +import com.intellij.openapi.project.DumbAwareAction; import com.intellij.openapi.project.Project; import com.intellij.openapi.project.ProjectManager; -import com.intellij.openapi.project.ProjectManagerAdapter; +import com.intellij.openapi.project.ProjectManagerListener; import com.intellij.openapi.project.impl.ProjectImpl; import com.intellij.openapi.ui.Messages; import com.intellij.openapi.util.Ref; @@ -33,6 +35,7 @@ import com.intellij.openapi.util.registry.Registry; import com.intellij.openapi.util.text.StringUtil; import com.intellij.openapi.wm.impl.SystemDock; import com.intellij.openapi.wm.impl.welcomeScreen.WelcomeFrame; +import com.intellij.util.ArrayUtil; import com.intellij.util.containers.ContainerUtil; import com.intellij.util.messages.MessageBus; import org.jetbrains.annotations.NotNull; @@ -44,16 +47,35 @@ import java.util.*; /** * @author yole */ -public abstract class RecentProjectsManagerBase implements PersistentStateComponent { +public abstract class RecentProjectsManagerBase implements ProjectManagerListener, PersistentStateComponent { public static RecentProjectsManagerBase getInstance() { - return ApplicationManager.getApplication().getComponent(RecentProjectsManagerBase.class); + return ServiceManager.getService(RecentProjectsManagerBase.class); } public static class State { - public List recentPaths = new ArrayList(); - public List openPaths = new ArrayList(); - public Map names = new HashMap(); + public List recentPaths = ContainerUtil.newArrayList(); + public List openPaths = ContainerUtil.newArrayList(); + public Map names = ContainerUtil.newLinkedHashMap(); public String lastPath; + + void validateRecentProjects() { + //noinspection StatementWithEmptyBody + while (recentPaths.remove(null)) ; + Collection displayNames = names.values(); + //noinspection StatementWithEmptyBody + while (displayNames.remove("")) ; + + while (recentPaths.size() > Registry.intValue("ide.max.recent.projects")) { + int index = recentPaths.size() - 1; + names.remove(recentPaths.get(index)); + recentPaths.remove(index); + } + } + + void removePath(String path) { + recentPaths.remove(path); + names.remove(path); + } } private final Object myStateLock = new Object(); @@ -61,14 +83,13 @@ public abstract class RecentProjectsManagerBase implements PersistentStateCompon private final Map myNameCache = Collections.synchronizedMap(new HashMap()); - public RecentProjectsManagerBase(ProjectManager projectManager, MessageBus messageBus) { - projectManager.addProjectManagerListener(new MyProjectManagerListener()); + protected RecentProjectsManagerBase(MessageBus messageBus) { messageBus.connect().subscribe(AppLifecycleListener.TOPIC, new MyAppLifecycleListener()); } public State getState() { synchronized (myStateLock) { - validateRecentProjects(); + myState.validateRecentProjects(); return myState; } } @@ -88,37 +109,16 @@ public abstract class RecentProjectsManagerBase implements PersistentStateCompon } } - private void validateRecentProjects() { - synchronized (myStateLock) { - for (Iterator i = myState.recentPaths.iterator(); i.hasNext();) { - String s = (String)i.next(); - - if (s == null) { - i.remove(); - } - } - while (myState.recentPaths.size() > Registry.intValue("ide.max.recent.projects")) { - final int index = myState.recentPaths.size() - 1; - myState.names.remove(myState.recentPaths.get(index)); - myState.recentPaths.remove(index); - } - } - } - public void removePath(final String path) { if (path == null) return; synchronized (myStateLock) { if (SystemInfo.isFileSystemCaseSensitive) { - myState.recentPaths.remove(path); - myState.names.remove(path); + myState.removePath(path); } else { - Iterator i = myState.recentPaths.iterator(); - while (i.hasNext()) { - String p = i.next(); + for (String p : ArrayUtil.toStringArray(myState.recentPaths)) { if (path.equalsIgnoreCase(p)) { - myState.names.remove(p); - i.remove(); + myState.removePath(path); } } } @@ -137,13 +137,16 @@ public abstract class RecentProjectsManagerBase implements PersistentStateCompon if (openProjects.length == 0) { myState.lastPath = null; myState.openPaths = Collections.emptyList(); - } else { + } + else { myState.lastPath = getProjectPath(openProjects[openProjects.length - 1]); - myState.openPaths = new ArrayList(); + myState.openPaths = ContainerUtil.newArrayList(); for (Project openProject : openProjects) { - final String path = getProjectPath(openProject); - ContainerUtil.addIfNotNull(myState.openPaths, path); - myState.names.put(path, getProjectDisplayName(openProject)); + String path = getProjectPath(openProject); + if (path != null) { + myState.openPaths.add(path); + myState.names.put(path, getProjectDisplayName(openProject)); + } } } } @@ -157,17 +160,11 @@ public abstract class RecentProjectsManagerBase implements PersistentStateCompon private Set getDuplicateProjectNames(Set openedPaths, Set recentPaths) { Set names = ContainerUtil.newHashSet(); Set duplicates = ContainerUtil.newHashSet(); - for (String path : openedPaths) { + for (String path : ContainerUtil.concat(openedPaths, recentPaths)) { if (!names.add(getProjectName(path))) { duplicates.add(path); } } - for (String path : recentPaths) { - if (!names.add(getProjectName(path))) { - duplicates.add(path); - } - } - return duplicates; } @@ -177,17 +174,17 @@ public abstract class RecentProjectsManagerBase implements PersistentStateCompon * @return */ public AnAction[] getRecentProjectsActions(boolean addClearListItem) { - validateRecentProjects(); + final Set paths; + synchronized (myStateLock) { + myState.validateRecentProjects(); + paths = ContainerUtil.newLinkedHashSet(myState.recentPaths); + } final Set openedPaths = ContainerUtil.newHashSet(); for (Project openProject : ProjectManager.getInstance().getOpenProjects()) { ContainerUtil.addIfNotNull(openedPaths, getProjectPath(openProject)); } - final LinkedHashSet paths; - synchronized (myStateLock) { - paths = ContainerUtil.newLinkedHashSet(myState.recentPaths); - } paths.remove(null); paths.removeAll(openedPaths); @@ -220,7 +217,7 @@ public abstract class RecentProjectsManagerBase implements PersistentStateCompon list.add(action); } if (addClearListItem) { - AnAction clearListAction = new AnAction(IdeBundle.message("action.clear.list")) { + AnAction clearListAction = new DumbAwareAction(IdeBundle.message("action.clear.list")) { public void actionPerformed(AnActionEvent e) { final int rc = Messages.showOkCancelDialog(e.getData(CommonDataKeys.PROJECT), "Would you like to clear the list of recent projects?", @@ -261,34 +258,35 @@ public abstract class RecentProjectsManagerBase implements PersistentStateCompon return file.exists() && (!file.isDirectory() || new File(file, Project.DIRECTORY_STORE_FOLDER).exists()); } - private class MyProjectManagerListener extends ProjectManagerAdapter { - public void projectOpened(final Project project) { - if (ApplicationManager.getApplication().isHeadlessEnvironment()) return; - String path = getProjectPath(project); + public void projectOpened(final Project project) { + String path = getProjectPath(project); + if (path != null) { + markPathRecent(path); + } + SystemDock.updateMenu(); + } + + @Override + public final boolean canCloseProject(Project project) { + return true; + } + + @Override + public void projectClosing(Project project) { + synchronized (myStateLock) { + myState.names.put(getProjectPath(project), getProjectDisplayName(project)); + } + } + + public void projectClosed(final Project project) { + Project[] openProjects = ProjectManager.getInstance().getOpenProjects(); + if (openProjects.length > 0) { + String path = getProjectPath(openProjects[openProjects.length - 1]); if (path != null) { markPathRecent(path); } - SystemDock.updateMenu(); - } - - @Override - public void projectClosing(Project project) { - synchronized (myStateLock) { - myState.names.put(getProjectPath(project), getProjectDisplayName(project)); - } - } - - public void projectClosed(final Project project) { - if (ApplicationManager.getApplication().isHeadlessEnvironment()) return; - Project[] openProjects = ProjectManager.getInstance().getOpenProjects(); - if (openProjects.length > 0) { - String path = getProjectPath(openProjects[openProjects.length - 1]); - if (path != null) { - markPathRecent(path); - } - } - SystemDock.updateMenu(); } + SystemDock.updateMenu(); } @NotNull @@ -330,35 +328,46 @@ public abstract class RecentProjectsManagerBase implements PersistentStateCompon } } + protected boolean willReopenProjectOnStart() { + return GeneralSettings.getInstance().isReopenLastProject() && getLastProjectPath() != null; + } + + protected void doReopenLastProject() { + GeneralSettings generalSettings = GeneralSettings.getInstance(); + if (generalSettings.isReopenLastProject()) { + Collection openPaths; + synchronized (myStateLock) { + openPaths = ContainerUtil.newLinkedHashSet(myState.openPaths); + } + if (!openPaths.isEmpty()) { + for (String openPath : openPaths) { + if (isValidProjectPath(openPath)) { + doOpenProject(openPath, null, true); + } + } + } + else { + String lastProjectPath = getLastProjectPath(); + if (lastProjectPath != null) { + if (isValidProjectPath(lastProjectPath)) doOpenProject(lastProjectPath, null, false); + } + } + } + } + private class MyAppLifecycleListener extends AppLifecycleListener.Adapter { public void appFrameCreated(final String[] commandLineArgs, @NotNull final Ref willOpenProject) { - if (GeneralSettings.getInstance().isReopenLastProject() && getLastProjectPath() != null) { + if (!ApplicationManager.getApplication().isHeadlessEnvironment()) { + ProjectManager.getInstance().addProjectManagerListener(RecentProjectsManagerBase.this); + } + if (willReopenProjectOnStart()) { willOpenProject.set(Boolean.TRUE); } } - public void appStarting(final Project projectFromCommandLine) { + public void appStarting(Project projectFromCommandLine) { if (projectFromCommandLine != null) return; - GeneralSettings generalSettings = GeneralSettings.getInstance(); - if (generalSettings.isReopenLastProject()) { - List openPaths; - synchronized (myStateLock) { - openPaths = ContainerUtil.newArrayList(myState.openPaths); - } - if (!openPaths.isEmpty()) { - for (String openPath : openPaths) { - if (isValidProjectPath(openPath)) { - doOpenProject(openPath, null, true); - } - } - } - else { - String lastProjectPath = getLastProjectPath(); - if (lastProjectPath != null) { - if (isValidProjectPath(lastProjectPath)) doOpenProject(lastProjectPath, null, false); - } - } - } + doReopenLastProject(); } public void projectFrameClosed() { diff --git a/platform/platform-impl/src/com/intellij/openapi/project/impl/ProjectManagerImpl.java b/platform/platform-impl/src/com/intellij/openapi/project/impl/ProjectManagerImpl.java index ccc5db7f17f7..d32c188fdf0e 100644 --- a/platform/platform-impl/src/com/intellij/openapi/project/impl/ProjectManagerImpl.java +++ b/platform/platform-impl/src/com/intellij/openapi/project/impl/ProjectManagerImpl.java @@ -19,6 +19,7 @@ import com.intellij.CommonBundle; import com.intellij.conversion.ConversionResult; import com.intellij.conversion.ConversionService; import com.intellij.ide.AppLifecycleListener; +import com.intellij.ide.RecentProjectsManagerBase; import com.intellij.ide.impl.ProjectUtil; import com.intellij.ide.startup.impl.StartupManagerImpl; import com.intellij.notification.NotificationsManager; @@ -58,6 +59,7 @@ import com.intellij.openapi.vfs.impl.local.LocalFileSystemImpl; import com.intellij.openapi.wm.impl.welcomeScreen.WelcomeFrame; import com.intellij.util.Alarm; import com.intellij.util.ArrayUtil; +import com.intellij.util.ObjectUtils; import com.intellij.util.TimeoutUtil; import com.intellij.util.containers.ContainerUtil; import com.intellij.util.containers.HashMap; @@ -118,7 +120,10 @@ public class ProjectManagerImpl extends ProjectManagerEx implements NamedJDOMExt return array; } - public ProjectManagerImpl(VirtualFileManager virtualFileManager, ProgressManager progressManager) { + /** @noinspection UnusedParameters*/ + public ProjectManagerImpl(VirtualFileManager virtualFileManager, + RecentProjectsManagerBase recentProjectsManager, + ProgressManager progressManager) { myProgressManager = progressManager; Application app = ApplicationManager.getApplication(); MessageBus messageBus = app.getMessageBus(); @@ -291,7 +296,7 @@ public class ProjectManagerImpl extends ProjectManagerEx implements NamedJDOMExt @NotNull String filePath, boolean isDefault, boolean isOptimiseTestLoadSpeed) { - return isDefault ? new DefaultProject(this, "", isOptimiseTestLoadSpeed, projectName) + return isDefault ? new DefaultProject(this, "", isOptimiseTestLoadSpeed, ObjectUtils.assertNotNull(projectName)) : new ProjectImpl(this, new File(filePath).getAbsolutePath(), isOptimiseTestLoadSpeed, projectName); } diff --git a/platform/platform-resources/src/META-INF/PlatformLangPlugin.xml b/platform/platform-resources/src/META-INF/PlatformLangPlugin.xml index 426575e32eca..943f3f568d68 100644 --- a/platform/platform-resources/src/META-INF/PlatformLangPlugin.xml +++ b/platform/platform-resources/src/META-INF/PlatformLangPlugin.xml @@ -22,6 +22,9 @@ + + + + diff --git a/platform/platform-resources/src/componentSets/PlatformComponents.xml b/platform/platform-resources/src/componentSets/PlatformComponents.xml index 29037ab0a7e3..8aa305c4c939 100644 --- a/platform/platform-resources/src/componentSets/PlatformComponents.xml +++ b/platform/platform-resources/src/componentSets/PlatformComponents.xml @@ -15,10 +15,6 @@ com.intellij.openapi.project.impl.ProjectStoreClassProvider com.intellij.openapi.components.impl.stores.PlatformProjectStoreClassProvider - - com.intellij.ide.RecentProjectsManagerBase - com.intellij.ide.RecentDirectoryProjectsManager - @@ -53,7 +49,7 @@ com.intellij.platform.PlatformVcsDetector - + diff --git a/platform/platform-resources/src/componentSets/PlatformLangComponents.xml b/platform/platform-resources/src/componentSets/PlatformLangComponents.xml index 7e4cbdc677aa..dc70e3968008 100644 --- a/platform/platform-resources/src/componentSets/PlatformLangComponents.xml +++ b/platform/platform-resources/src/componentSets/PlatformLangComponents.xml @@ -15,10 +15,6 @@ com.intellij.openapi.project.impl.ProjectStoreClassProvider com.intellij.openapi.components.impl.stores.PlatformLangProjectStoreClassProvider - - com.intellij.ide.RecentProjectsManagerBase - com.intellij.ide.RecentDirectoryProjectsManagerEx - diff --git a/resources/src/idea/RichPlatformPlugin.xml b/resources/src/idea/RichPlatformPlugin.xml index ae4c6b6491e0..9313e52e266a 100644 --- a/resources/src/idea/RichPlatformPlugin.xml +++ b/resources/src/idea/RichPlatformPlugin.xml @@ -10,13 +10,6 @@ - - - com.intellij.ide.RecentProjectsManagerBase - com.intellij.ide.RecentProjectsManager - - - com.intellij.framework.detection.impl.FrameworkDetectionManager @@ -133,6 +126,9 @@ + +