[UI API] IDEA-307083, IDEA-307615 Clean up SimpleNode.update() mess

Now that doUpdate() implementations only touch the presentation, we
can try to clean up the surrounding mess in SimpleNode.update():

1. Remove equality checks (they were broken anyway, and we do
a similar check in PresentableNodeDescriptor.apply() anyway.

2. Instead of passing values back and forth between fields and
the presentation, we don't mutate fields anymore (this isn't allowed
now anyway), and instead use them as fall back if the presentation
isn't completely filled by doUpdate().

GitOrigin-RevId: aa3d471883c3df14b75f3a9efd1f5b353b67755d
This commit is contained in:
Sergei Tachenov
2022-12-05 14:53:45 +00:00
committed by intellij-monorepo-bot
parent 9646a3d9e9
commit 383a2d7315
2 changed files with 42 additions and 19 deletions
@@ -10,6 +10,7 @@ import com.intellij.util.concurrency.annotations.RequiresBackgroundThread;
import com.intellij.util.concurrency.annotations.RequiresReadLock;
import com.intellij.util.ui.StartupUiUtil;
import com.intellij.util.ui.UIUtil;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -222,13 +223,23 @@ public abstract class PresentableNodeDescriptor<E> extends NodeDescriptor<E> {
}
public @NlsSafe String getName() {
if (!getPresentation().getColoredText().isEmpty()) {
String result = getColoredTextAsPlainText(getPresentation());
if (result != null) {
return result;
}
return myName;
}
@ApiStatus.Internal
@Nullable
protected static String getColoredTextAsPlainText(PresentationData presentation) {
if (!presentation.getColoredText().isEmpty()) {
StringBuilder result = new StringBuilder();
for (ColoredFragment each : getPresentation().getColoredText()) {
for (ColoredFragment each : presentation.getColoredText()) {
result.append(each.getText());
}
return result.toString();
}
return myName;
return null;
}
}
@@ -17,9 +17,6 @@ import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import java.awt.*;
import java.awt.event.InputEvent;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public abstract class SimpleNode extends PresentableNodeDescriptor<Object> implements ComparableObject, LeafState.Supplier {
@@ -79,23 +76,38 @@ public abstract class SimpleNode extends PresentableNodeDescriptor<Object> imple
}
if (newElement == null) return;
Color oldColor = myColor;
String oldName = myName;
Icon oldIcon = getIcon();
List<ColoredFragment> oldFragments = new ArrayList<>(presentation.getColoredText());
myColor = UIUtil.getTreeForeground();
doUpdate(presentation);
myName = getName();
presentation.setPresentableText(myName);
fillFallbackProperties(presentation);
}
presentation.setChanged(!Arrays.equals(new Object[]{getIcon(), myName, oldFragments, myColor},
new Object[]{oldIcon, oldName, oldFragments, oldColor}));
private void fillFallbackProperties(PresentationData presentation) {
fillFallbackText(presentation);
fillFallbackIcon(presentation);
fillFallbackColor(presentation);
}
presentation.setForcedTextForeground(myColor);
presentation.setIcon(getIcon());
private void fillFallbackText(PresentationData presentation) {
var text = getColoredTextAsPlainText(presentation);
if (text == null) {
text = presentation.getPresentableText();
}
if (text == null) {
text = myName;
}
presentation.setPresentableText(text);
}
private void fillFallbackIcon(PresentationData presentation) {
if (presentation.getIcon(false) == null) {
presentation.setIcon(myClosedIcon);
}
}
private void fillFallbackColor(PresentationData presentation) {
if (presentation.getForcedTextForeground() == null) {
presentation.setForcedTextForeground(myColor == null ? UIUtil.getTreeForeground() : myColor);
}
}
/**