package com.karateca.ddescriber.dialog;

import com.intellij.openapi.util.IconLoader;
import com.karateca.ddescriber.model.TestFindResult;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import java.awt.*;

/**
 * @author Andres Dominguez.
 */
public class CustomTreeCellRenderer extends DefaultTreeCellRenderer {

  private static final Color GREEN_BG_COLOR = new Color(182, 232, 172);
  private final Color defaultColor = getBackgroundNonSelectionColor();
  private final Icon itIcon = IconLoader.findIcon("/icons/it-icon.png");
  private final Icon descIcon = IconLoader.findIcon("/icons/desc-icon.png");
  private final boolean showFileName;

  public CustomTreeCellRenderer(boolean showFileName) {
    this.showFileName = showFileName;
  }

  @Override
  public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
    Component component = super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);

    DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;

    // Ignore non test nodes.
    if (!(node.getUserObject() instanceof TestFindResult)) {
      return component;
    }

    TestFindResult findResult = (TestFindResult) node.getUserObject();

    Color color = findResult.isMarkedForRun() ? GREEN_BG_COLOR : defaultColor;
    setBackgroundNonSelectionColor(color);

    // Set the icon depending on the type.
    setIcon(findResult.isDescribe() ? descIcon : itIcon);

    String name = findResult.toString();
    if (showFileName && findResult.isTopDescribe()) {
      name = String.format("[%s] - %s", findResult.getVirtualFile().getName(), name);
    }
    setText(name);

    return component;
  }
}

@@ -1706,18 +1706,24 @@
 at(%22
-%5B
 %25s
-%5D
  - 
+%5B
 %25s
-%22
+%5D%22, name
 , fi
@@ -1757,22 +1757,16 @@
 etName()
-, name
 );%0A    %7D

move file name to the end