mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
refactor to extract renderer from GotoActionModel
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2000-2014 JetBrains s.r.o.
|
||||
* Copyright 2000-2015 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.
|
||||
@@ -41,6 +41,7 @@ import com.intellij.ui.components.JBLabel;
|
||||
import com.intellij.ui.components.OnOffButton;
|
||||
import com.intellij.ui.speedSearch.SpeedSearchUtil;
|
||||
import com.intellij.util.ArrayUtil;
|
||||
import com.intellij.util.Function;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import com.intellij.util.containers.ContainerUtilRt;
|
||||
import com.intellij.util.ui.EmptyIcon;
|
||||
@@ -144,7 +145,7 @@ public class GotoActionModel implements ChooseByNameModel, CustomMatcherModel, C
|
||||
@NotNull public final Comparable value;
|
||||
@NotNull final String pattern;
|
||||
|
||||
MatchedValue(@NotNull Comparable value, @NotNull String pattern) {
|
||||
public MatchedValue(@NotNull Comparable value, @NotNull String pattern) {
|
||||
this.value = value;
|
||||
this.pattern = pattern;
|
||||
}
|
||||
@@ -208,121 +209,12 @@ public class GotoActionModel implements ChooseByNameModel, CustomMatcherModel, C
|
||||
|
||||
@Override
|
||||
public ListCellRenderer getListCellRenderer() {
|
||||
return new DefaultListCellRenderer() {
|
||||
return new GotoActionListCellRenderer(new Function<OptionDescription, String>() {
|
||||
@Override
|
||||
public Component getListCellRendererComponent(@NotNull final JList list,
|
||||
final Object matchedValue,
|
||||
final int index, final boolean isSelected, final boolean cellHasFocus) {
|
||||
final JPanel panel = new JPanel(new BorderLayout());
|
||||
panel.setBorder(IdeBorderFactory.createEmptyBorder(2));
|
||||
panel.setOpaque(true);
|
||||
Color bg = UIUtil.getListBackground(isSelected);
|
||||
panel.setBackground(bg);
|
||||
|
||||
if (matchedValue instanceof String) { //...
|
||||
final JBLabel label = new JBLabel((String)matchedValue);
|
||||
label.setIcon(EMPTY_ICON);
|
||||
panel.add(label, BorderLayout.WEST);
|
||||
return panel;
|
||||
}
|
||||
|
||||
Color groupFg = isSelected ? UIUtil.getListSelectionForeground() : UIUtil.getLabelDisabledForeground();
|
||||
|
||||
final Object value = ((MatchedValue) matchedValue).value;
|
||||
String pattern = ((MatchedValue)matchedValue).pattern;
|
||||
|
||||
SimpleColoredComponent nameComponent = new SimpleColoredComponent();
|
||||
nameComponent.setBackground(bg);
|
||||
panel.add(nameComponent, BorderLayout.CENTER);
|
||||
|
||||
if (value instanceof ActionWrapper) {
|
||||
final ActionWrapper actionWithParentGroup = (ActionWrapper)value;
|
||||
final AnAction anAction = actionWithParentGroup.getAction();
|
||||
final Presentation presentation = anAction.getTemplatePresentation();
|
||||
boolean toggle = anAction instanceof ToggleAction;
|
||||
String groupName = actionWithParentGroup.getAction() instanceof ApplyIntentionAction ? null : actionWithParentGroup.getGroupName();
|
||||
final Color fg = defaultActionForeground(isSelected, actionWithParentGroup.getPresentation());
|
||||
panel.add(createIconLabel(presentation.getIcon()), BorderLayout.WEST);
|
||||
appendWithColoredMatches(nameComponent, getName(presentation.getText(), groupName, toggle), pattern, fg, isSelected);
|
||||
|
||||
final Shortcut shortcut = preferKeyboardShortcut(KeymapManager.getInstance().getActiveKeymap().getShortcuts(getActionId(anAction)));
|
||||
if (shortcut != null) {
|
||||
nameComponent.append(" (" + KeymapUtil.getShortcutText(shortcut) + ")", new SimpleTextAttributes(STYLE_PLAIN, groupFg));
|
||||
}
|
||||
|
||||
if (toggle) {
|
||||
final OnOffButton button = new OnOffButton();
|
||||
AnActionEvent event = new AnActionEvent(null, ((ActionWrapper)value).myDataContext,
|
||||
ActionPlaces.UNKNOWN, new Presentation(), ActionManager.getInstance(),
|
||||
0);
|
||||
button.setSelected(((ToggleAction)anAction).isSelected(event));
|
||||
panel.add(button, BorderLayout.EAST);
|
||||
panel.setBorder(IdeBorderFactory.createEmptyBorder());
|
||||
}
|
||||
else {
|
||||
if (groupName != null) {
|
||||
final JLabel groupLabel = new JLabel(groupName);
|
||||
groupLabel.setBackground(bg);
|
||||
groupLabel.setForeground(groupFg);
|
||||
panel.add(groupLabel, BorderLayout.EAST);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (value instanceof OptionDescription) {
|
||||
if (!isSelected && !(value instanceof BooleanOptionDescription)) {
|
||||
Color descriptorBg = UIUtil.isUnderDarcula() ? ColorUtil.brighter(UIUtil.getListBackground(), 1) : LightColors.SLIGHTLY_GRAY;
|
||||
panel.setBackground(descriptorBg);
|
||||
nameComponent.setBackground(descriptorBg);
|
||||
}
|
||||
String hit = ((OptionDescription)value).getHit();
|
||||
if (hit == null) {
|
||||
hit = ((OptionDescription)value).getOption();
|
||||
}
|
||||
hit = StringUtil.unescapeXml(hit);
|
||||
hit = hit.replace(" ", " "); // avoid extra spaces from mnemonics and xml conversion
|
||||
String fullHit = hit;
|
||||
hit = StringUtil.first(hit, 45, true);
|
||||
|
||||
final Color fg = UIUtil.getListForeground(isSelected);
|
||||
|
||||
appendWithColoredMatches(nameComponent, hit.trim(), pattern, fg, isSelected);
|
||||
|
||||
panel.add(new JLabel(EMPTY_ICON), BorderLayout.WEST);
|
||||
panel.setToolTipText(fullHit);
|
||||
|
||||
if (value instanceof BooleanOptionDescription) {
|
||||
final OnOffButton button = new OnOffButton();
|
||||
button.setSelected(((BooleanOptionDescription)value).isOptionEnabled());
|
||||
panel.add(button, BorderLayout.EAST);
|
||||
panel.setBorder(IdeBorderFactory.createEmptyBorder());
|
||||
}
|
||||
else {
|
||||
final JLabel settingsLabel = new JLabel(getGroupName((OptionDescription)value));
|
||||
settingsLabel.setForeground(groupFg);
|
||||
settingsLabel.setBackground(bg);
|
||||
panel.add(settingsLabel, BorderLayout.EAST);
|
||||
}
|
||||
}
|
||||
return panel;
|
||||
public String fun(OptionDescription description) {
|
||||
return getGroupName(description);
|
||||
}
|
||||
|
||||
public String getName(String text, String groupName, boolean toggle) {
|
||||
return toggle && StringUtil.isNotEmpty(groupName)? groupName + ": "+ text : text;
|
||||
}
|
||||
|
||||
private void appendWithColoredMatches(SimpleColoredComponent nameComponent, String name, String pattern, Color fg, boolean selected) {
|
||||
final SimpleTextAttributes plain = new SimpleTextAttributes(STYLE_PLAIN, fg);
|
||||
final SimpleTextAttributes highlighted = new SimpleTextAttributes(null, fg, null, STYLE_SEARCH_MATCH);
|
||||
List<TextRange> fragments = ContainerUtil.newArrayList();
|
||||
if (selected) {
|
||||
int matchStart = StringUtil.indexOfIgnoreCase(name, pattern, 0);
|
||||
if (matchStart >= 0) {
|
||||
fragments.add(TextRange.from(matchStart, pattern.length()));
|
||||
}
|
||||
}
|
||||
SpeedSearchUtil.appendColoredFragments(nameComponent, name, fragments, plain, highlighted);
|
||||
}
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
protected String getActionId(@NotNull final AnAction anAction) {
|
||||
@@ -721,4 +613,130 @@ public class GotoActionModel implements ChooseByNameModel, CustomMatcherModel, C
|
||||
return myAction.getTemplatePresentation().getText().hashCode();
|
||||
}
|
||||
}
|
||||
|
||||
public static class GotoActionListCellRenderer extends DefaultListCellRenderer {
|
||||
private final Function<OptionDescription, String> myGroupNamer;
|
||||
|
||||
public GotoActionListCellRenderer(Function<OptionDescription, String> groupNamer) {
|
||||
myGroupNamer = groupNamer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Component getListCellRendererComponent(@NotNull final JList list,
|
||||
final Object matchedValue,
|
||||
final int index, final boolean isSelected, final boolean cellHasFocus) {
|
||||
final JPanel panel = new JPanel(new BorderLayout());
|
||||
panel.setBorder(IdeBorderFactory.createEmptyBorder(2));
|
||||
panel.setOpaque(true);
|
||||
Color bg = UIUtil.getListBackground(isSelected);
|
||||
panel.setBackground(bg);
|
||||
|
||||
if (matchedValue instanceof String) { //...
|
||||
final JBLabel label = new JBLabel((String)matchedValue);
|
||||
label.setIcon(EMPTY_ICON);
|
||||
panel.add(label, BorderLayout.WEST);
|
||||
return panel;
|
||||
}
|
||||
|
||||
Color groupFg = isSelected ? UIUtil.getListSelectionForeground() : UIUtil.getLabelDisabledForeground();
|
||||
|
||||
final Object value = ((MatchedValue) matchedValue).value;
|
||||
String pattern = ((MatchedValue)matchedValue).pattern;
|
||||
|
||||
SimpleColoredComponent nameComponent = new SimpleColoredComponent();
|
||||
nameComponent.setBackground(bg);
|
||||
panel.add(nameComponent, BorderLayout.CENTER);
|
||||
|
||||
if (value instanceof ActionWrapper) {
|
||||
final ActionWrapper actionWithParentGroup = (ActionWrapper)value;
|
||||
final AnAction anAction = actionWithParentGroup.getAction();
|
||||
final Presentation presentation = anAction.getTemplatePresentation();
|
||||
boolean toggle = anAction instanceof ToggleAction;
|
||||
String groupName = actionWithParentGroup.getAction() instanceof ApplyIntentionAction ? null : actionWithParentGroup.getGroupName();
|
||||
final Color fg = defaultActionForeground(isSelected, actionWithParentGroup.getPresentation());
|
||||
panel.add(createIconLabel(presentation.getIcon()), BorderLayout.WEST);
|
||||
appendWithColoredMatches(nameComponent, getName(presentation.getText(), groupName, toggle), pattern, fg, isSelected);
|
||||
|
||||
final Shortcut shortcut = preferKeyboardShortcut(KeymapManager.getInstance().getActiveKeymap().getShortcuts(ActionManager.getInstance().getId(anAction)));
|
||||
if (shortcut != null) {
|
||||
nameComponent.append(" (" + KeymapUtil.getShortcutText(shortcut) + ")", new SimpleTextAttributes(STYLE_PLAIN, groupFg));
|
||||
}
|
||||
|
||||
if (toggle) {
|
||||
final OnOffButton button = new OnOffButton();
|
||||
AnActionEvent event = new AnActionEvent(null, ((ActionWrapper)value).myDataContext,
|
||||
ActionPlaces.UNKNOWN, new Presentation(), ActionManager.getInstance(),
|
||||
0);
|
||||
button.setSelected(((ToggleAction)anAction).isSelected(event));
|
||||
panel.add(button, BorderLayout.EAST);
|
||||
panel.setBorder(IdeBorderFactory.createEmptyBorder());
|
||||
}
|
||||
else {
|
||||
if (groupName != null) {
|
||||
final JLabel groupLabel = new JLabel(groupName);
|
||||
groupLabel.setBackground(bg);
|
||||
groupLabel.setForeground(groupFg);
|
||||
panel.add(groupLabel, BorderLayout.EAST);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (value instanceof OptionDescription) {
|
||||
if (!isSelected && !(value instanceof BooleanOptionDescription)) {
|
||||
Color descriptorBg = UIUtil.isUnderDarcula() ? ColorUtil.brighter(UIUtil.getListBackground(), 1) : LightColors.SLIGHTLY_GRAY;
|
||||
panel.setBackground(descriptorBg);
|
||||
nameComponent.setBackground(descriptorBg);
|
||||
}
|
||||
String hit = ((OptionDescription)value).getHit();
|
||||
if (hit == null) {
|
||||
hit = ((OptionDescription)value).getOption();
|
||||
}
|
||||
hit = StringUtil.unescapeXml(hit);
|
||||
hit = hit.replace(" ", " "); // avoid extra spaces from mnemonics and xml conversion
|
||||
String fullHit = hit;
|
||||
hit = StringUtil.first(hit, 45, true);
|
||||
|
||||
final Color fg = UIUtil.getListForeground(isSelected);
|
||||
|
||||
appendWithColoredMatches(nameComponent, hit.trim(), pattern, fg, isSelected);
|
||||
|
||||
panel.add(new JLabel(EMPTY_ICON), BorderLayout.WEST);
|
||||
panel.setToolTipText(fullHit);
|
||||
|
||||
if (value instanceof BooleanOptionDescription) {
|
||||
final OnOffButton button = new OnOffButton();
|
||||
button.setSelected(((BooleanOptionDescription)value).isOptionEnabled());
|
||||
panel.add(button, BorderLayout.EAST);
|
||||
panel.setBorder(IdeBorderFactory.createEmptyBorder());
|
||||
}
|
||||
else {
|
||||
final JLabel settingsLabel = new JLabel(myGroupNamer.fun((OptionDescription)value));
|
||||
settingsLabel.setForeground(groupFg);
|
||||
settingsLabel.setBackground(bg);
|
||||
panel.add(settingsLabel, BorderLayout.EAST);
|
||||
}
|
||||
}
|
||||
return panel;
|
||||
}
|
||||
|
||||
public String getName(String text, String groupName, boolean toggle) {
|
||||
return toggle && StringUtil.isNotEmpty(groupName)? groupName + ": "+ text : text;
|
||||
}
|
||||
|
||||
private static void appendWithColoredMatches(SimpleColoredComponent nameComponent,
|
||||
String name,
|
||||
String pattern,
|
||||
Color fg,
|
||||
boolean selected) {
|
||||
final SimpleTextAttributes plain = new SimpleTextAttributes(STYLE_PLAIN, fg);
|
||||
final SimpleTextAttributes highlighted = new SimpleTextAttributes(null, fg, null, STYLE_SEARCH_MATCH);
|
||||
List<TextRange> fragments = ContainerUtil.newArrayList();
|
||||
if (selected) {
|
||||
int matchStart = StringUtil.indexOfIgnoreCase(name, pattern, 0);
|
||||
if (matchStart >= 0) {
|
||||
fragments.add(TextRange.from(matchStart, pattern.length()));
|
||||
}
|
||||
}
|
||||
SpeedSearchUtil.appendColoredFragments(nameComponent, name, fragments, plain, highlighted);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user