How to make the tree have a loading icon

Answered

How to make the tree have a loading icon, such as this:,I try to pass
this.myTree.setCellRenderer(new MyRenderer());
Call the setIcon method in MyRenderer to set the icon, but the icon is static and cannot be moved, although the icon I set is dynamic

public class MyRenderer extends MouseAdapter implements TreeCellRenderer {
private static final Icon LOADING_NODE_ICON = JBUIScale.scaleIcon(EmptyIcon.create(8, 16));

private final NodeRenderer nodeRenderer;
private final PresentationData presentation;

@NotNull
protected NodeRenderer createRenderer() {
return new NodeRenderer() {
@Override
protected @Nullable ItemPresentation getPresentation(Object node) {
// setIcon(LOADING_NODE_ICON);
return ReadAction.compute(() -> getPresentationInner(node));
}
};
}

@Nullable
ItemPresentation getPresentationInner(Object node) {
if ((node instanceof DatabaseStructure.DbRootGroup)) {
return ((DatabaseStructure.DbRootGroup) node).getPresentation();
}
if ((node instanceof DataSource)) {

return ((DataSource) node).getPresentation();
}
if (node instanceof DataSourceTreeNodeDescriptor) {
return ((DataSourceTreeNodeDescriptor) node).getElement().getPresentation();
} else if (node instanceof DefaultMutableTreeNode) {
Object userObject = ((DefaultMutableTreeNode) node).getUserObject();
if (userObject instanceof DataSourceTreeNodeDescriptor) {
return ((DataSourceTreeNodeDescriptor) userObject).getElement().getPresentation();
}
}
return null;
}

public MyRenderer() {
this.nodeRenderer = this.createRenderer();
this.presentation = new PresentationData();
}

@Override
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
SimpleColoredComponent component = (SimpleColoredComponent) this.nodeRenderer.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
component.setOpaque(false);
component.setIconOpaque(false);
component.setTransparentIconBackground(true);
component.setIconTextGap(JBUIScale.scale(5));
component.setIcon(new AnimatedIcon.Default());
return component;
}
}
0
2 comments

If I use tree.setPaintBusy(true) directly, there will be 2 problems:
The first one: the position of the icon is wrong
The second: I need to set loading for a certain node instead of all

0

Please see com.intellij.execution.testframework.sm.runner.ui.TestTreeRenderer as reference. It uses com.intellij.execution.testframework.sm.runner.ui.TestsPresentationUtil#getIcon to obtain current test node’s icon and also uses new AnimatedIcon.Default() as general “loading” icon.

0

Please sign in to leave a comment.