finally IDEA-64374 Speed search not working in Find Usages toolwindow

This commit is contained in:
Gregory.Shrago
2013-10-22 22:15:34 +04:00
parent dfa0b15073
commit efb4101728
6 changed files with 71 additions and 27 deletions
@@ -16,8 +16,10 @@
package com.intellij.ui;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.util.containers.Convertor;
import javax.swing.*;
import javax.swing.tree.TreePath;
/**
* @author yole
@@ -44,6 +46,8 @@ public abstract class TreeUIHelper {
public abstract void installTreeSpeedSearch(JTree tree);
public abstract void installListSpeedSearch(JList list);
public abstract void installTreeSpeedSearch(JTree tree, Convertor<TreePath, String> convertor, boolean canExpand);
public abstract void installListSpeedSearch(JList list, Convertor<Object, String> convertor);
public abstract void installEditSourceOnEnterKeyHandler(JTree tree);
@@ -16,19 +16,30 @@
package com.intellij.ui;
import com.intellij.util.Function;
import com.intellij.util.containers.Convertor;
import javax.swing.*;
public class ListSpeedSearch extends SpeedSearchBase<JList> {
private Function<Object, String> myElementTextDelegate;
private final Convertor<Object, String> myToStringConvertor;
public ListSpeedSearch(JList list) {
super(list);
myToStringConvertor = null;
}
public ListSpeedSearch(final JList component, final Function<Object, String> elementTextDelegate) {
public ListSpeedSearch(final JList component, final Convertor<Object, String> convertor) {
super(component);
myElementTextDelegate = elementTextDelegate;
myToStringConvertor = convertor;
}
public ListSpeedSearch(final JList component, final Function<Object, String> convertor) {
this(component, new Convertor<Object, String>() {
@Override
public String convert(Object o) {
return convertor.fun(o);
}
});
}
protected void selectElement(Object element, String selectedText) {
@@ -58,8 +69,8 @@ public class ListSpeedSearch extends SpeedSearchBase<JList> {
}
protected String getElementText(Object element) {
if (myElementTextDelegate != null) {
return myElementTextDelegate.fun(element);
if (myToStringConvertor != null) {
return myToStringConvertor.convert(element);
}
return element == null ? null : element.toString();
}
@@ -22,7 +22,6 @@ import com.intellij.util.containers.Convertor;
import com.intellij.util.ui.tree.TreeUtil;
import javax.swing.*;
import javax.swing.text.Position;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
@@ -53,14 +52,8 @@ public class TreeSpeedSearch extends SpeedSearchBase<JTree> {
}
};
/**
* @deprecated You should use {@link TreeSpeedSearch#TreeSpeedSearch(JTree)}
* @see Tree#getNextMatch(String, int, Position.Bias)
*/
public TreeSpeedSearch(JTree tree, Convertor<TreePath, String> toStringConvertor) {
super(tree);
myToStringConvertor = toStringConvertor;
setComparator(new SpeedSearchComparator(false, true));
this(tree, toStringConvertor, false);
}
public TreeSpeedSearch(JTree tree) {
@@ -72,6 +65,10 @@ public class TreeSpeedSearch extends SpeedSearchBase<JTree> {
}
public TreeSpeedSearch(Tree tree, Convertor<TreePath, String> toString, boolean canExpand) {
this((JTree)tree, toString, canExpand);
}
public TreeSpeedSearch(JTree tree, Convertor<TreePath, String> toString, boolean canExpand) {
super(tree);
setComparator(new SpeedSearchComparator(false, true));
myToStringConvertor = toString;
@@ -20,8 +20,10 @@ import com.intellij.ui.table.JBTable;
import com.intellij.ui.treeStructure.Tree;
import com.intellij.util.EditSourceOnDoubleClickHandler;
import com.intellij.util.EditSourceOnEnterKeyHandler;
import com.intellij.util.containers.Convertor;
import javax.swing.*;
import javax.swing.tree.TreePath;
/**
* @author yole
@@ -51,10 +53,18 @@ public class TreeUIHelperImpl extends TreeUIHelper {
new TreeSpeedSearch(tree);
}
public void installListSpeedSearch(final JList list) {
public void installTreeSpeedSearch(JTree tree, Convertor<TreePath, String> convertor, boolean canExpand) {
new TreeSpeedSearch(tree, convertor, canExpand);
}
public void installListSpeedSearch(JList list) {
new ListSpeedSearch(list);
}
public void installListSpeedSearch(JList list, Convertor<Object, String> convertor) {
new ListSpeedSearch(list, convertor);
}
public void installEditSourceOnEnterKeyHandler(final JTree tree) {
EditSourceOnEnterKeyHandler.install(tree);
}
@@ -57,6 +57,7 @@ import com.intellij.util.Consumer;
import com.intellij.util.EditSourceOnDoubleClickHandler;
import com.intellij.util.Processor;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.containers.Convertor;
import com.intellij.util.containers.TransferToEDTQueue;
import com.intellij.util.messages.MessageBusConnection;
import com.intellij.util.ui.DialogUtil;
@@ -438,7 +439,22 @@ public class UsageViewImpl implements UsageView, UsageModelTracker.UsageModelTra
}
});
//TODO: install speed search. Not in openapi though. It makes sense to create a common TreeEnchancer service.
TreeUIHelper.getInstance().installTreeSpeedSearch(myTree, new Convertor<TreePath, String>() {
@Override
public String convert(TreePath o) {
Object value = o.getLastPathComponent();
TreeCellRenderer renderer = myTree.getCellRenderer();
if (renderer instanceof ColoredTreeCellRenderer) {
ColoredTreeCellRenderer coloredRenderer = (ColoredTreeCellRenderer)renderer;
coloredRenderer.clear();
coloredRenderer.customizeCellRenderer(myTree, value, false, false, false, 0, false);
return coloredRenderer.toString();
}
else {
return value == null? null : value.toString();
}
}
}, true);
}
@NotNull
@@ -22,10 +22,12 @@ import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vcs.FileStatus;
import com.intellij.ui.ColoredTreeCellRenderer;
import com.intellij.ui.SimpleTextAttributes;
import com.intellij.ui.speedSearch.SpeedSearchUtil;
import com.intellij.usageView.UsageTreeColors;
import com.intellij.usageView.UsageTreeColorsScheme;
import com.intellij.usageView.UsageViewBundle;
import com.intellij.usages.*;
import org.jetbrains.annotations.NotNull;
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
@@ -55,7 +57,7 @@ class UsageViewTreeCellRenderer extends ColoredTreeCellRenderer {
if (value instanceof Node && value != tree.getModel().getRoot()) {
Node node = (Node)value;
if (!node.isValid()) {
append(UsageViewBundle.message("node.invalid") + " ", ourInvalidAttributes);
doAppend(UsageViewBundle.message("node.invalid") + " ", ourInvalidAttributes);
}
if (myPresentation.isShowReadOnlyStatusAsRed() && node.isReadOnly()) {
showAsReadOnly = true;
@@ -69,61 +71,65 @@ class UsageViewTreeCellRenderer extends ColoredTreeCellRenderer {
if (userObject instanceof UsageTarget) {
UsageTarget usageTarget = (UsageTarget)userObject;
if (!usageTarget.isValid()) {
append(UsageViewBundle.message("node.invalid"), ourInvalidAttributes);
doAppend(UsageViewBundle.message("node.invalid"), ourInvalidAttributes);
return;
}
final ItemPresentation presentation = usageTarget.getPresentation();
LOG.assertTrue(presentation != null);
if (showAsReadOnly) {
append(UsageViewBundle.message("node.readonly") + " ", ourReadOnlyAttributes);
doAppend(UsageViewBundle.message("node.readonly") + " ", ourReadOnlyAttributes);
}
final String text = presentation.getPresentableText();
append(text != null? text : "", SimpleTextAttributes.REGULAR_ATTRIBUTES);
doAppend(text != null? text : "", SimpleTextAttributes.REGULAR_ATTRIBUTES);
setIcon(presentation.getIcon(expanded));
}
else if (treeNode instanceof GroupNode) {
GroupNode node = (GroupNode)treeNode;
if (node.isRoot()) {
append(StringUtil.capitalize(myPresentation.getUsagesWord()), patchAttrs(node, SimpleTextAttributes.REGULAR_BOLD_ATTRIBUTES));
doAppend(StringUtil.capitalize(myPresentation.getUsagesWord()), patchAttrs(node, SimpleTextAttributes.REGULAR_BOLD_ATTRIBUTES));
}
else {
append(node.getGroup().getText(myView),
doAppend(node.getGroup().getText(myView),
patchAttrs(node, showAsReadOnly ? ourReadOnlyAttributes : SimpleTextAttributes.REGULAR_ATTRIBUTES));
setIcon(node.getGroup().getIcon(expanded));
}
int count = node.getRecursiveUsageCount();
append(" (" + StringUtil.pluralize(count + " " + myPresentation.getUsagesWord(), count) + ")", patchAttrs(node, myNumberOfUsagesAttribute));
doAppend(" (" + StringUtil.pluralize(count + " " + myPresentation.getUsagesWord(), count) + ")", patchAttrs(node, myNumberOfUsagesAttribute));
}
else if (treeNode instanceof UsageNode) {
UsageNode node = (UsageNode)treeNode;
setIcon(node.getUsage().getPresentation().getIcon());
if (showAsReadOnly) {
append(UsageViewBundle.message("node.readonly") + " ", patchAttrs(node, ourReadOnlyAttributes));
doAppend(UsageViewBundle.message("node.readonly") + " ", patchAttrs(node, ourReadOnlyAttributes));
}
if (node.isValid()) {
TextChunk[] text = node.getUsage().getPresentation().getText();
for (TextChunk textChunk : text) {
SimpleTextAttributes simples = textChunk.getSimpleAttributesIgnoreBackground();
append(textChunk.getText(), patchAttrs(node, simples));
doAppend(textChunk.getText(), patchAttrs(node, simples));
}
}
}
else if (userObject instanceof String) {
append((String)userObject, SimpleTextAttributes.REGULAR_BOLD_ATTRIBUTES);
doAppend((String)userObject, SimpleTextAttributes.REGULAR_BOLD_ATTRIBUTES);
}
else {
append(userObject == null ? "" : userObject.toString(), SimpleTextAttributes.REGULAR_ATTRIBUTES);
doAppend(userObject == null ? "" : userObject.toString(), SimpleTextAttributes.REGULAR_ATTRIBUTES);
}
}
else {
append(value.toString(), SimpleTextAttributes.REGULAR_ATTRIBUTES);
doAppend(value.toString(), SimpleTextAttributes.REGULAR_ATTRIBUTES);
}
}
private void doAppend(@NotNull String fragment, @NotNull SimpleTextAttributes attributes) {
SpeedSearchUtil.appendFragmentsForSpeedSearch(myTree, fragment, attributes, mySelected, this);
}
private static SimpleTextAttributes patchAttrs(Node node, SimpleTextAttributes original) {
if (node.isExcluded()) {
original = new SimpleTextAttributes(original.getStyle() | SimpleTextAttributes.STYLE_STRIKEOUT, original.getFgColor(), original.getWaveColor());