// -*- mode:java; encoding:utf-8 -*-
// vim:set fileencoding=utf-8:
// @homepage@

package example;

import java.awt.*;
import java.util.Objects;
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeCellRenderer;
import javax.swing.tree.TreePath;

public final class MainPanel extends JPanel {
  private MainPanel() {
    super(new BorderLayout());
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    String p = "example/restore_to_background_color.gif";
    ImageIcon icon = new ImageIcon(Objects.requireNonNull(cl.getResource(p)));

    DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
    Object uo0 = new NodeObject("default", icon);
    DefaultMutableTreeNode s0 = new DefaultMutableTreeNode(uo0);
    Object uo1 = new NodeObject("setImageObserver", icon);
    DefaultMutableTreeNode s1 = new DefaultMutableTreeNode(uo1);
    root.add(s0);
    root.add(s1);
    JTree tree = new JTree(new DefaultTreeModel(root)) {
      @Override public void updateUI() {
        setCellRenderer(null);
        super.updateUI();
        TreeCellRenderer r = getCellRenderer();
        setCellRenderer((tree, value, selected, expanded, leaf, row, hasFocus) -> {
          Component c = r.getTreeCellRendererComponent(
              tree, value, selected, expanded, leaf, row, hasFocus);
          if (value instanceof DefaultMutableTreeNode && c instanceof JLabel) {
            JLabel l = (JLabel) c;
            Object v = ((DefaultMutableTreeNode) value).getUserObject();
            if (v instanceof NodeObject) {
              NodeObject uo = (NodeObject) v;
              l.setText(Objects.toString(uo.title, ""));
              l.setIcon(uo.icon);
            } else {
              l.setText(Objects.toString(value, ""));
              l.setIcon(null);
            }
          }
          return c;
        });
      }
    };
    TreePath path = new TreePath(s1.getPath());
    // Wastefulness: icon.setImageObserver((ImageObserver) tree);
    icon.setImageObserver((img, infoflags, x, y, w, h) -> {
      if (!tree.isShowing()) {
        return false;
      }
      Rectangle cellRect = tree.getPathBounds(path);
      if ((infoflags & (FRAMEBITS | ALLBITS)) != 0 && Objects.nonNull(cellRect)) {
        tree.repaint(cellRect);
      }
      return (infoflags & (ALLBITS | ABORT)) == 0;
    });
    tree.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    add(new JScrollPane(tree));
    setPreferredSize(new Dimension(320, 240));
  }

  public static void main(String[] args) {
    EventQueue.invokeLater(MainPanel::createAndShowGui);
  }

  private static void createAndShowGui() {
    try {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
      ex.printStackTrace();
      Toolkit.getDefaultToolkit().beep();
    }
    JFrame frame = new JFrame("@title@");
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.getContentPane().add(new MainPanel());
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }
}

class NodeObject {
  public final Icon icon;
  public final String title;

  // protected NodeObject(String title) {
  //   this(title, null);
  // }

  protected NodeObject(String title, Icon icon) {
    this.title = title;
    this.icon = icon;
  }
}

@@ -129,20 +129,84 @@
 ava.
-util.Objects
+awt.image.BufferedImage;%0Aimport java.util.Objects;%0Aimport java.util.Optional
 ;%0Aim
@@ -501,211 +501,83 @@
     
-ClassLoader cl = Thread.currentThread().getContextClassLoader();%0A    String p = %22example/restore_to_background_color.gif%22;%0A    ImageIcon icon = new ImageIcon(Objects.requireNonNull(cl.getResource(p))
+ImageIcon icon = makeImageIcon(%22example/restore_to_background_color.gif%22
 );%0A
-%0A
     
@@ -2495,16 +2495,703 @@
 );%0A  %7D%0A%0A
+  public static ImageIcon makeImageIcon(String path) %7B%0A    ClassLoader cl = Thread.currentThread().getContextClassLoader();%0A    return Optional.ofNullable(cl.getResource(path))%0A        .map(ImageIcon::new)%0A        .orElseGet(() -%3E new ImageIcon(makeMissingImage()));%0A  %7D%0A%0A  private static Image makeMissingImage() %7B%0A    Icon missingIcon = UIManager.getIcon(%22html.missingImage%22);%0A    int iw = missingIcon.getIconWidth();%0A    int ih = missingIcon.getIconHeight();%0A    BufferedImage bi = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);%0A    Graphics2D g2 = bi.createGraphics();%0A    missingIcon.paintIcon(null, g2, (16 - iw) / 2, (16 - ih) / 2);%0A    g2.dispose();%0A    return bi;%0A  %7D%0A%0A
   public

use Optional#orElseGet(...) instead of Objects.requireNonNull(...)