ui: do not throw an exception on duplicated actions in group

This is an error we can recover from.
IDEA-207311
This commit is contained in:
Aleksey Pivovarov
2019-02-19 14:06:44 +03:00
parent 42f169adce
commit 5bc3e369c4
@@ -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<? extends AnAction> actions) {
HashSet<Object> actionSet = new HashSet<>();
List<AnAction> 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<AnAction, Constraints> 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<AnAction, Constraints> 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