|
| 1 | +/* |
| 2 | + * Copyright © Magento, Inc. All rights reserved. |
| 3 | + * See COPYING.txt for license details. |
| 4 | + */ |
| 5 | + |
| 6 | +package com.magento.idea.magento2plugin.actions.groups; |
| 7 | + |
| 8 | +import com.intellij.ide.actions.NonEmptyActionGroup; |
| 9 | +import com.intellij.openapi.actionSystem.AnAction; |
| 10 | +import com.intellij.openapi.actionSystem.AnActionEvent; |
| 11 | +import java.util.Arrays; |
| 12 | +import java.util.Comparator; |
| 13 | +import java.util.LinkedList; |
| 14 | +import java.util.List; |
| 15 | +import org.jetbrains.annotations.NotNull; |
| 16 | + |
| 17 | +public class ContextActionsGroup extends NonEmptyActionGroup { |
| 18 | + |
| 19 | + public ContextActionsGroup() { |
| 20 | + super(); |
| 21 | + } |
| 22 | + |
| 23 | + @Override |
| 24 | + public void update(final @NotNull AnActionEvent event) { |
| 25 | + if (getChildrenCount() > 0) { |
| 26 | + final AnAction[] actions = getChildren(event); |
| 27 | + final List<AnAction> originalActionList = new LinkedList<>(Arrays.asList(actions)); |
| 28 | + final List<AnAction> sortedActionList = new LinkedList<>(Arrays.asList(actions)); |
| 29 | + sortedActionList.sort(new ContextActionsComparator()); |
| 30 | + |
| 31 | + if (!originalActionList.equals(sortedActionList)) { |
| 32 | + removeAll(); |
| 33 | + addAll(sortedActionList.toArray(new AnAction[0])); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + super.update(event); |
| 38 | + } |
| 39 | + |
| 40 | + private static class ContextActionsComparator implements Comparator<AnAction> { |
| 41 | + |
| 42 | + @Override |
| 43 | + public int compare(final AnAction action1, final AnAction action2) { |
| 44 | + if (action1.getTemplateText() == null || action2.getTemplateText() == null) { |
| 45 | + return 0; |
| 46 | + } |
| 47 | + |
| 48 | + return action1.getTemplateText().compareTo(action2.getTemplateText()); |
| 49 | + } |
| 50 | + } |
| 51 | +} |
0 commit comments