introduce ColoredItem interface

GitOrigin-RevId: 1a27db56d5046eb2e0b0f3241b354078088f80c6
This commit is contained in:
Gregory.Shrago
2019-07-02 06:52:16 +03:00
committed by intellij-monorepo-bot
parent 58ab6cce3a
commit fc44f9b724
3 changed files with 37 additions and 5 deletions
@@ -1,8 +1,11 @@
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.ui.components;
import com.intellij.openapi.util.ColoredItem;
import com.intellij.ui.list.ListCellBackgroundSupplier;
import com.intellij.util.ui.UIUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import javax.swing.plaf.ComponentUI;
@@ -36,11 +39,7 @@ public final class WideSelectionListUI extends BasicListUI {
boolean selected = selectionModel.isSelectedIndex(row);
boolean focused = row == leadSelectionIndex && list.hasFocus();
Object value = model.getElementAt(row);
Color background = null;
if (list instanceof ListCellBackgroundSupplier) {
//noinspection unchecked
background = ((ListCellBackgroundSupplier<Object>)list).getCellBackground(value, row);
}
Color background = getBackground(list, value, row);
if (background != null) {
g.setColor(background);
g.fillRect(rowBounds.x, rowBounds.y, rowBounds.width, rowBounds.height);
@@ -57,6 +56,20 @@ public final class WideSelectionListUI extends BasicListUI {
super.paintCell(g, row, rowBounds, renderer, model, selectionModel, leadSelectionIndex);
}
@Nullable
private static Color getBackground(@NotNull JList list, @Nullable Object value, int row) {
if (value instanceof ColoredItem) {
Color background = ((ColoredItem)value).getColor();
if (background != null) return background;
}
if (list instanceof ListCellBackgroundSupplier) {
//noinspection unchecked
Color background = ((ListCellBackgroundSupplier<Object>)list).getCellBackground(value, row);
if (background != null) return background;
}
return null;
}
private static void paintRenderer(Graphics g, int x, int y, int width, int height, Component owner, Component renderer) {
g.clipRect(0, y, owner.getWidth(), height);
paintBackground(g, y, height, owner, renderer);
@@ -2,6 +2,7 @@
package com.intellij.ui.tree.ui;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.ColoredItem;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.SystemInfo;
import com.intellij.ui.components.JBScrollPane;
@@ -66,6 +67,10 @@ public final class DefaultTreeUI extends BasicTreeUI {
return UIUtil.getTreeSelectionBackground(tree.hasFocus() || Boolean.TRUE.equals(property));
}
Object node = TreeUtil.getLastUserObject(path);
if (node instanceof ColoredItem) {
Color background = ((ColoredItem)node).getColor();
if (background != null) return background;
}
if (node instanceof TreeNodeBackgroundSupplier) {
TreeNodeBackgroundSupplier supplier = (TreeNodeBackgroundSupplier)node;
Color background = supplier.getNodeBackground(row);
@@ -0,0 +1,14 @@
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.openapi.util;
import org.jetbrains.annotations.Nullable;
import java.awt.*;
/**
* @author gregsh
*/
public interface ColoredItem {
@Nullable
Color getColor();
}