mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
toolbar decorator button comparator
This commit is contained in:
@@ -25,10 +25,7 @@ import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @author Konstantin Bulenkov
|
||||
@@ -88,7 +85,7 @@ class CommonActionsPanel extends JPanel {
|
||||
private final AnActionButton[] myActions;
|
||||
|
||||
CommonActionsPanel(ListenerFactory factory, @Nullable JComponent contextComponent, boolean isHorizontal,
|
||||
@Nullable AnActionButton[] additionalActions,
|
||||
@Nullable AnActionButton[] additionalActions, @Nullable Comparator<AnActionButton> buttonComparator,
|
||||
String addName, String removeName, String moveUpName, String moveDownName, String editName,
|
||||
Buttons... buttons) {
|
||||
super(new BorderLayout());
|
||||
@@ -117,6 +114,9 @@ class CommonActionsPanel extends JPanel {
|
||||
for (AnActionButton action : actions) {
|
||||
action.setContextComponent(contextComponent);
|
||||
}
|
||||
if (buttonComparator != null) {
|
||||
Arrays.sort(myActions, buttonComparator);
|
||||
}
|
||||
final ActionToolbar toolbar = ActionManager.getInstance().createActionToolbar(ActionPlaces.UNKNOWN,
|
||||
new DefaultActionGroup(myActions),
|
||||
isHorizontal);
|
||||
|
||||
@@ -31,6 +31,7 @@ import javax.swing.border.Border;
|
||||
import javax.swing.border.LineBorder;
|
||||
import java.awt.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
@@ -72,6 +73,7 @@ public abstract class ToolbarDecorator implements DataProvider, CommonActionsPan
|
||||
private String myMoveDownName;
|
||||
private Dimension myPreferredSize;
|
||||
private CommonActionsPanel myPanel;
|
||||
private Comparator<AnActionButton> myButtonComparator;
|
||||
|
||||
protected abstract JComponent getComponent();
|
||||
|
||||
@@ -138,6 +140,30 @@ public abstract class ToolbarDecorator implements DataProvider, CommonActionsPan
|
||||
return this;
|
||||
}
|
||||
|
||||
public ToolbarDecorator setButtonComparator(Comparator<AnActionButton> buttonComparator) {
|
||||
myButtonComparator = buttonComparator;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ToolbarDecorator setButtonComparator(String...actionNames) {
|
||||
final List<String> names = Arrays.asList(actionNames);
|
||||
myButtonComparator = new Comparator<AnActionButton>() {
|
||||
@Override
|
||||
public int compare(AnActionButton o1, AnActionButton o2) {
|
||||
final String t1 = o1.getTemplatePresentation().getText();
|
||||
final String t2 = o2.getTemplatePresentation().getText();
|
||||
if (t1 == null || t2 == null) return 0;
|
||||
|
||||
final int ind1 = names.indexOf(t1);
|
||||
final int ind2 = names.indexOf(t2);
|
||||
if (ind1 == -1 && ind2 >= 0) return 1;
|
||||
if (ind2 == -1 && ind1 >= 0) return -1;
|
||||
return ind1 - ind2;
|
||||
}
|
||||
};
|
||||
return this;
|
||||
}
|
||||
|
||||
public ToolbarDecorator setLineBorder(int top, int left, int bottom, int right) {
|
||||
return setToolbarBorder(new CustomLineBorder(top, left, bottom, right));
|
||||
}
|
||||
@@ -218,6 +244,7 @@ public abstract class ToolbarDecorator implements DataProvider, CommonActionsPan
|
||||
myPanel = new CommonActionsPanel(this, contextComponent,
|
||||
myToolbarPosition == ActionToolbarPosition.TOP || myToolbarPosition == ActionToolbarPosition.BOTTOM,
|
||||
myExtraActions.toArray(new AnActionButton[myExtraActions.size()]),
|
||||
myButtonComparator,
|
||||
myAddName, myRemoveName, myMoveUpName, myMoveDownName, myEditName,
|
||||
buttons);
|
||||
myPanel.setBorder(myBorder);
|
||||
|
||||
Reference in New Issue
Block a user