From ca119acb5bf8a398cb27399f19c9551d6c87b0da Mon Sep 17 00:00:00 2001 From: Sergey Malenkov Date: Mon, 26 Mar 2018 22:41:41 +0300 Subject: [PATCH] IDEA-188727 SelectInManager: utility method to find SelectInTarged for ToolWindow --- .../actions/SelectInProjectViewAction.java | 31 +++--------- .../src/com/intellij/ide/SelectInManager.java | 50 +++++++++---------- 2 files changed, 30 insertions(+), 51 deletions(-) diff --git a/platform/lang-impl/src/com/intellij/ide/actions/SelectInProjectViewAction.java b/platform/lang-impl/src/com/intellij/ide/actions/SelectInProjectViewAction.java index 193ce662951c..8e10063f9c82 100644 --- a/platform/lang-impl/src/com/intellij/ide/actions/SelectInProjectViewAction.java +++ b/platform/lang-impl/src/com/intellij/ide/actions/SelectInProjectViewAction.java @@ -16,45 +16,28 @@ import static com.intellij.openapi.wm.ToolWindowId.PROJECT_VIEW; * @author Konstantin Bulenkov */ public class SelectInProjectViewAction extends DumbAwareAction { + private static PsiDocumentManager getDocumentManager(Project project) { + return project == null || project.isDisposed() ? null : PsiDocumentManager.getInstance(project); + } + @Override public void beforeActionPerformedUpdate(@NotNull AnActionEvent event) { - PsiDocumentManager manager = getPsiDocumentManager(event.getProject()); + PsiDocumentManager manager = getDocumentManager(event.getProject()); if (manager != null) manager.commitAllDocuments(); super.beforeActionPerformedUpdate(event); } @Override public void update(@NotNull AnActionEvent event) { - SelectInTarget target = findSelectInProjectView(event.getProject()); + SelectInTarget target = SelectInManager.findSelectInTarget(PROJECT_VIEW, event.getProject()); SelectInContext context = target == null ? null : SelectInContextImpl.createContext(event); event.getPresentation().setEnabled(context != null && target.canSelect(context)); } @Override public void actionPerformed(@NotNull AnActionEvent event) { - SelectInTarget target = findSelectInProjectView(event.getProject()); + SelectInTarget target = SelectInManager.findSelectInTarget(PROJECT_VIEW, event.getProject()); SelectInContext context = target == null ? null : SelectInContextImpl.createContext(event); if (context != null) target.selectIn(context, true); } - - private static PsiDocumentManager getPsiDocumentManager(Project project) { - return project == null || project.isDisposed() ? null : PsiDocumentManager.getInstance(project); - } - - private static SelectInManager getSelectInManager(Project project) { - return project == null || project.isDisposed() ? null : SelectInManager.getInstance(project); - } - - private static SelectInTarget findSelectInProjectView(Project project) { - SelectInManager manager = getSelectInManager(project); - SelectInTarget[] targets = manager == null ? null : manager.getTargets(); - if (targets != null) { - for (SelectInTarget target : targets) { - if (target != null && PROJECT_VIEW.equals(target.getToolWindowId())) { - return target; - } - } - } - return null; - } } diff --git a/platform/platform-api/src/com/intellij/ide/SelectInManager.java b/platform/platform-api/src/com/intellij/ide/SelectInManager.java index 435c0477ab53..74f2f20b1888 100644 --- a/platform/platform-api/src/com/intellij/ide/SelectInManager.java +++ b/platform/platform-api/src/com/intellij/ide/SelectInManager.java @@ -1,18 +1,4 @@ -/* - * Copyright 2000-2009 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. - */ +// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.ide; import com.intellij.openapi.components.ServiceManager; @@ -20,10 +6,11 @@ import com.intellij.openapi.extensions.Extensions; import com.intellij.openapi.project.DumbAware; import com.intellij.openapi.project.DumbService; import com.intellij.openapi.project.Project; -import com.intellij.util.containers.ContainerUtil; import org.jetbrains.annotations.NonNls; +import org.jetbrains.annotations.NotNull; import java.util.*; +import java.util.stream.Stream; public class SelectInManager { private final Project myProject; @@ -42,7 +29,7 @@ public class SelectInManager { } /** - * "Select In" targets should be registered as extension points ({@link com.intellij.ide.SelectInTarget#EP_NAME}). + * "Select In" targets should be registered as extension points ({@link SelectInTarget#EP_NAME}). */ @Deprecated public void addTarget(SelectInTarget target) { @@ -55,15 +42,11 @@ public class SelectInManager { public SelectInTarget[] getTargets() { checkLoadExtensions(); - SelectInTarget[] targets = myTargets.toArray(new SelectInTarget[0]); - Arrays.sort(targets, new SelectInTargetComparator()); - + Stream stream = myTargets.stream(); if (DumbService.getInstance(myProject).isDumb()) { - final List awareList = (List)ContainerUtil.findAll(targets, DumbAware.class); - return awareList.toArray(new SelectInTarget[0]); + stream = stream.filter(target -> target instanceof DumbAware); } - - return targets; + return stream.sorted(SelectInTargetComparator.INSTANCE).toArray(SelectInTarget[]::new); } private void checkLoadExtensions() { @@ -77,11 +60,24 @@ public class SelectInManager { return ServiceManager.getService(project, SelectInManager.class); } + public static SelectInTarget findSelectInTarget(@NotNull String id, Project project) { + SelectInManager manager = project == null || project.isDisposed() ? null : SelectInManager.getInstance(project); + SelectInTarget[] targets = manager == null ? null : manager.getTargets(); + if (targets != null) { + for (SelectInTarget target : targets) { + if (target != null && Objects.equals(id, target.getToolWindowId())) { + return target; + } + } + } + return null; + } + public static class SelectInTargetComparator implements Comparator { + public static final Comparator INSTANCE = new SelectInTargetComparator(); + public int compare(final SelectInTarget o1, final SelectInTarget o2) { - if (o1.getWeight() < o2.getWeight()) return -1; - if (o1.getWeight() > o2.getWeight()) return 1; - return 0; + return Float.compare(o1.getWeight(), o2.getWeight()); } } }