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

package example;

import java.awt.*;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ColorConvertOp;
import java.util.Objects;
import javax.swing.*;

public final class MainPanel extends JPanel {
  private static final String HTML_TEXT = "<html>Html <font color='red'>label</font><br/> Test";

  private MainPanel() {
    super(new BorderLayout());
    JPanel p = new JPanel(new GridLayout(2, 3));

    JLabel label0 = new JLabel("Default JLabel");
    p.add(initTitledBorder("JLabel", label0));

    JLabel label1 = new JLabel(HTML_TEXT);
    p.add(initTitledBorder("JLabel+Html", label1));

    JLabel label2 = new JLabel(HTML_TEXT) {
      // JLabel with html tag can not be disabled or setForegroud?!
      // https://community.oracle.com/thread/1377943
      @Override public void setEnabled(boolean b) {
        super.setEnabled(b);
        setForeground(b ? UIManager.getColor("Label.foreground") : UIManager.getColor("Label.disabledForeground"));
      }
    };
    p.add(initTitledBorder("JLabel+Html+", label2));

    JLabel label3 = new DisabledHtmlLabel(HTML_TEXT);
    p.add(initTitledBorder("JLabel+Html++", label3));

    JEditorPane editor1 = new JEditorPane("text/html", HTML_TEXT);
    editor1.setOpaque(false);
    editor1.setEditable(false);
    p.add(initTitledBorder("JEditorPane", editor1));

    JEditorPane editor2 = new JEditorPane("text/html", HTML_TEXT);
    editor2.setOpaque(false);
    editor2.setEditable(false);
    editor2.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE);
    editor2.setFont(UIManager.getFont("Label.font"));
    p.add(initTitledBorder("JEditorPane+", editor2));

    JCheckBox check = new JCheckBox("setEnabled", true);
    check.addActionListener(e -> {
      boolean f = ((JCheckBox) e.getSource()).isSelected();
      for (Component c : p.getComponents()) {
        c.setEnabled(f);
      }
    });

    add(check, BorderLayout.NORTH);
    add(p);
    setPreferredSize(new Dimension(320, 240));
  }

  private static Component initTitledBorder(String title, JComponent c) {
    c.setBorder(BorderFactory.createTitledBorder(title));
    return c;
  }

  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 DisabledHtmlLabel extends JLabel {
  private static final ColorConvertOp GRAY_CCO = new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null);
  private transient BufferedImage shadow;

  protected DisabledHtmlLabel(String text) {
    super(text);
  }

  @Override public void setEnabled(boolean b) {
    setForeground(b ? UIManager.getColor("Label.foreground")
            : UIManager.getColor("Label.disabledForeground"));
    if (!b) {
      BufferedImage source = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
      Graphics2D g2 = source.createGraphics();
      g2.setPaint(new Color(0x0, true));
      g2.fillRect(0, 0, getWidth(), getHeight());
      // print(g2);
      paint(g2);
      g2.dispose();
      shadow = GRAY_CCO.filter(source, null);
    }
    super.setEnabled(b);
  }

  @Override protected void paintComponent(Graphics g) {
    if (!isEnabled() && Objects.nonNull(shadow)) {
      g.drawImage(shadow, 0, 0, this);
    } else {
      super.paintComponent(g);
    }
  }
}

@@ -826,12 +826,13 @@
 grou
+n
 d?!%0A
-
     
@@ -919,33 +919,39 @@
 Enabled(boolean 
-b
+enabled
 ) %7B%0A        supe
@@ -959,25 +959,31 @@
 .setEnabled(
-b
+enabled
 );%0A        s
@@ -985,45 +985,31 @@
     
-setForeground(b ? UIManager.getColor(
+String key = enabled ? 
 %22Lab
@@ -1026,31 +1026,11 @@
 und%22
-)
  : 
-UIManager.getColor(
 %22Lab
@@ -1043,32 +1043,78 @@
 abledForeground%22
+;%0A        setForeground(UIManager.getColor(key
 ));%0A      %7D%0A    
@@ -3272,21 +3272,48 @@
 boolean 
-b
+enabled
 ) %7B%0A
+    if (enabled) %7B%0A  
     setF
@@ -3326,12 +3326,8 @@
 und(
-b ? 
 UIMa
@@ -3364,23 +3364,44 @@
 nd%22)
+);
 %0A    
-        : 
+%7D else %7B%0A      setForeground(
 UIMa
@@ -3449,22 +3449,8 @@
 ));%0A
-    if (!b) %7B%0A
     
@@ -3815,17 +3815,23 @@
 Enabled(
-b
+enabled
 );%0A  %7D%0A%0A

fix typo setForegroud should be setForeground