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

package example;

import java.awt.*;
import java.util.Objects;
import java.util.Optional;
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());
    ImageIcon icon = new ImageIcon(getClass().getResource("restore_to_background_color.gif"));

    DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
    DefaultMutableTreeNode s0 = new DefaultMutableTreeNode(new NodeObject("default", icon));
    DefaultMutableTreeNode s1 = new DefaultMutableTreeNode(new NodeObject("setImageObserver", icon));
    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) -> {
          JLabel l = (JLabel) r.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
          Object v = Optional.ofNullable(value)
              .filter(DefaultMutableTreeNode.class::isInstance).map(DefaultMutableTreeNode.class::cast)
              .map(DefaultMutableTreeNode::getUserObject).orElse(null);
          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 l;
        });
      }
    };
    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;
  }
}

@@ -143,35 +143,8 @@
 ts;%0A
-import java.util.Optional;%0A
 impo
@@ -437,95 +437,206 @@
     
-ImageIcon icon = new ImageIcon(getClass().getResource(%22restore_to_background_color.gif%22
+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)
 ));%0A
@@ -1238,27 +1238,21 @@
     
-JLabel l = (JLabel)
+Component c =
  r.g
@@ -1348,149 +1348,112 @@
     
-Object v = Optional.ofNullable(value)%0A              .filter(DefaultMutableTreeNode.class::isInstance).map(DefaultMutableTreeNode.class::cast)
+if (value instanceof DefaultMutableTreeNode && c instanceof JLabel) %7B%0A            JLabel l = (JLabel) c;
 %0A   
@@ -1465,14 +1465,20 @@
     
-  .map
+Object v = (
 (Def
@@ -1496,18 +1496,25 @@
 TreeNode
-::
+) value).
 getUserO
@@ -1522,21 +1522,9 @@
 ject
-).orElse(null
+(
 );%0A 
@@ -1528,24 +1528,26 @@
 ;%0A          
+ 
+ 
 if (v instan
@@ -1561,24 +1561,26 @@
 deObject) %7B%0A
+  
             
@@ -1615,32 +1615,34 @@
  v;%0A            
+ 
+ 
 l.setText(Object
@@ -1672,32 +1672,34 @@
 ));%0A            
+  
 l.setIcon(uo.ico
@@ -1712,16 +1712,18 @@
         
+ 
+ 
 %7D else %7B
@@ -1723,16 +1723,18 @@
  else %7B%0A
+  
         
@@ -1781,32 +1781,34 @@
 ));%0A            
+  
 l.setIcon(null);
@@ -1800,32 +1800,34 @@
 .setIcon(null);%0A
+  
           %7D%0A    
@@ -1818,24 +1818,36 @@
           %7D%0A
+          %7D%0A
           re
@@ -1851,17 +1851,17 @@
  return 
-l
+c
 ;%0A      

argument 'getClass().getResource(...)' might be null