diff --git a/platform/platform-api/src/com/intellij/openapi/actionSystem/DefaultActionGroup.java b/platform/platform-api/src/com/intellij/openapi/actionSystem/DefaultActionGroup.java index e703d986cb3c..c706725aa36d 100644 --- a/platform/platform-api/src/com/intellij/openapi/actionSystem/DefaultActionGroup.java +++ b/platform/platform-api/src/com/intellij/openapi/actionSystem/DefaultActionGroup.java @@ -9,10 +9,7 @@ import com.intellij.util.containers.ContainerUtil; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.Arrays; -import java.util.Collection; -import java.util.HashSet; -import java.util.List; +import java.util.*; /** * A default implementation of {@link ActionGroup}. Provides the ability @@ -33,7 +30,8 @@ import java.util.List; */ public class DefaultActionGroup extends ActionGroup { private static final Logger LOG = Logger.getInstance("#com.intellij.openapi.actionSystem.DefaultActionGroup"); - private static final String CANT_ADD_ITSELF = "Cannot add a group to itself"; + private static final String CANT_ADD_ITSELF = "Cannot add a group to itself: "; + private static final String CANT_ADD_ACTION_TWICE = "Cannot add an action twice: "; /** * Contains instances of AnAction */ @@ -76,11 +74,16 @@ public class DefaultActionGroup extends ActionGroup { private void addActions(@NotNull List actions) { HashSet actionSet = new HashSet<>(); + List uniqueActions = new ArrayList<>(actions.size()); for (AnAction action : actions) { - if (action == this) throw new IllegalArgumentException(CANT_ADD_ITSELF); - if (!(action instanceof Separator) && !actionSet.add(action)) throw new ActionDuplicationException(action); + if (action == this) throw new IllegalArgumentException(CANT_ADD_ITSELF + action); + if (!(action instanceof Separator) && !actionSet.add(action)) { + LOG.error(CANT_ADD_ACTION_TWICE + action); + continue; + } + uniqueActions.add(action); } - mySortedChildren.addAll(actions); + mySortedChildren.addAll(uniqueActions); } /** @@ -134,13 +137,11 @@ public class DefaultActionGroup extends ActionGroup { @NotNull public final ActionInGroup addAction(@NotNull AnAction action, @NotNull Constraints constraint, @NotNull ActionManager actionManager) { - if (action == this) throw new IllegalArgumentException(CANT_ADD_ITSELF); + if (action == this) throw new IllegalArgumentException(CANT_ADD_ITSELF + action); // Check that action isn't already registered - if (!(action instanceof Separator)) { - if (mySortedChildren.contains(action)) throw new ActionDuplicationException(action); - for (Pair pair : myPairs) { - if (action.equals(pair.first)) throw new ActionDuplicationException(action); - } + if (!(action instanceof Separator) && containsAction(action)) { + LOG.error(CANT_ADD_ACTION_TWICE + action); + return new ActionInGroup(this, action); } constraint = (Constraints)constraint.clone(); @@ -163,6 +164,14 @@ public class DefaultActionGroup extends ActionGroup { return new ActionInGroup(this, action); } + private boolean containsAction(@NotNull AnAction action) { + if (mySortedChildren.contains(action)) return true; + for (Pair pair : myPairs) { + if (action.equals(pair.first)) return true; + } + return false; + } + private void actionAdded(@NotNull AnAction addedAction, @NotNull ActionManager actionManager) { String addedActionId = addedAction instanceof ActionStub ? ((ActionStub)addedAction).getId() : actionManager.getId(addedAction); if (addedActionId == null) { @@ -406,12 +415,6 @@ public class DefaultActionGroup extends ActionGroup { add(Separator.create(separatorText)); } - private static class ActionDuplicationException extends IllegalArgumentException { - ActionDuplicationException(@NotNull AnAction action) { - super("cannot add an action twice: " + action); - } - } - /** * Creates an action group with specified template text. It is necessary to redefine template text if group contains * user specific data such as Project name, file name, etc