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

package example;

import java.awt.*;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Optional;
import javax.swing.*;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.StyleSheet;

public final class MainPanel extends JPanel {
  private MainPanel() {
    super(new BorderLayout());
    Font font = makeFont(getClass().getResource("Burnstown Dam.ttf")).orElseGet(this::getFont);
    GraphicsEnvironment.getLocalGraphicsEnvironment().registerFont(font);

    JPanel p = new JPanel(new GridLayout(0, 1));
    JLabel label = new JLabel();
    label.setFont(font.deriveFont(Font.PLAIN, 24));
    label.setText("1: setFont(font.deriveFont(Font.PLAIN, 24))");
    p.add(label);
    JLabel label2 = new JLabel();
    label2.setText("<html><font size='+3' face='Burnstown Dam'>2: html, font, size,+3</font>");
    p.add(label2);

    StyleSheet styleSheet = new StyleSheet();
    styleSheet.addRule("body {font-size: 24pt; font-family: Burnstown Dam;}");
    // styleSheet.addRule(
    //     ".highlight {color: red; background: green; font-family: Burnstown Dam.ttf; }");
    HTMLEditorKit htmlEditorKit = new HTMLEditorKit();
    htmlEditorKit.setStyleSheet(styleSheet);
    JEditorPane editor = new JEditorPane();
    editor.setEditorKit(htmlEditorKit);
    editor.setText(makeTestHtml());
    JEditorPane editor2 = new JEditorPane();
    editor2.setFont(font);
    editor2.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE);
    editor2.setText("4: putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE)");
    JPanel p2 = new JPanel(new GridLayout(0, 1));
    p2.add(new JScrollPane(editor));
    p2.add(new JScrollPane(editor2));

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

  private static Optional<Font> makeFont(URL url) {
    try (InputStream is = url.openStream()) {
      return Optional.of(Font.createFont(Font.TRUETYPE_FONT, is).deriveFont(12f));
    } catch (IOException | FontFormatException ex) {
      ex.printStackTrace();
      return Optional.empty();
    }
  }

  private static String makeTestHtml() {
    return String.join("\n",
        "<html><body>",
        "<div>3: StyleSheet, body{font-size:24pt; font-family:Burnstown Dam;}</div>",
        "</body></html>"
    );
  }

  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);
  }
}

@@ -173,29 +173,8 @@
 am;%0A
-import java.net.URL;%0A
 impo
@@ -196,16 +196,16 @@
 tional;%0A
+
 import j
@@ -413,72 +413,403 @@
     
-Font font = makeFont(getClass().getResource(%22Burnstown Dam.ttf%22)
+String path = %22example/Burnstown Dam.ttf%22;%0A    ClassLoader cl = Thread.currentThread().getContextClassLoader();%0A    Font font = Optional.ofNullable(cl.getResource(path)).map(url -%3E %7B%0A      try (InputStream s = url.openStream()) %7B%0A        return Font.createFont(Font.TRUETYPE_FONT, s).deriveFont(12f);%0A      %7D catch (IOException %7C FontFormatException ex) %7B%0A        return getFont();%0A      %7D%0A    %7D
 ).or
@@ -2218,312 +2218,8 @@
  %7D%0A%0A
-  private static Optional%3CFont%3E makeFont(URL url) %7B%0A    try (InputStream is = url.openStream()) %7B%0A      return Optional.of(Font.createFont(Font.TRUETYPE_FONT, is).deriveFont(12f));%0A    %7D catch (IOException %7C FontFormatException ex) %7B%0A      ex.printStackTrace();%0A      return Optional.empty();%0A    %7D%0A  %7D%0A%0A
   pr

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