JFormDesigner / FlatLaf

FlatLaf - Swing Look and Feel (with Darcula/IntelliJ themes support)
https://www.formdev.com/flatlaf/
Apache License 2.0
3.43k stars 272 forks source link

JTextPane HTML issue #485

Closed mlaggner closed 2 years ago

mlaggner commented 2 years ago

I have a weird issue with a JTextPane and do not know why this happens.

My JTextPane is reusable and should switch between raw text and HTML (raw text for normal content and HTML if there are links in the content). When I switched to HTML (setContentType("text/html")) and want to switch back (setContentType("text/plain")) the newlines in the JTextPane are gone:

image

When I set HTML mode for everything (constructor) and I set an empty text (setText("")), all no more text is shown in this JTextPane until I restart my application:

image

Do I miss anything here in my setup?

 public ReadOnlyTextPaneHTML() {
    super();
    setOpaque(false);
    setBorder(null);
    setEditable(false);
    setFocusable(false);

    addHyperlinkListener(e -> {
      if (HyperlinkEvent.EventType.ACTIVATED.equals(e.getEventType())) {
        try {
          TmmUIHelper.browseUrl(e.getURL().toURI().toString());
        }
        catch (Exception ex) {
          // ex.printStackTrace();
        }
      }
    });
  }

  @Override
  public void setText(String t) {
    if (t == null || t.isEmpty()) {
      setContentType("text/plain");
      super.setText(t);
    }
    else {
      if (t.startsWith("<html>")) {
        // already HTML? just set correct content type - no replacement done here
        setContentType("text/html");
        super.setText(t);
      }
      else {
        // performance: just do a quick contains, before doing all the fancy stuff
        if (t.contains("http") || t.contains("www")) {
          // remove all existing href tags, to not reHTMLify existing ones
          t = Jsoup.clean(t, "", Whitelist.simpleText(), NO_PRETTYPRINT);

          // with space around, so a line concatenating has a whitespace delimiter
          t = t.replaceAll("\\n", " <br/> ");
          t = t.replaceAll("(?:https|http)://([^\\s]+)", "<a href=\"$0\">$1</a>");
          // whitespace before WWW to not include former style!!!
          t = t.replaceAll("(?:^|\\s)(www\\.[^\\s]+)", " <a href=\"https://$1\">$1</a>");

          setContentType("text/html");
          super.setText("<html>" + t + "</html>");
        }
        else {
          // no HTML links found to upgrade
          setContentType("text/plain");
          super.setText(t);
        }
      }
    }
  }
DevCharly commented 2 years ago

When I set HTML mode for everything (constructor) and I set an empty text (setText("")), ...

Maybe it works when you set it to an empty HTML text "<html></html>".

DevCharly commented 2 years ago

Have you tried whether this works in other Lafs? E.g. Metal, Windows, Nimbus...

mlaggner commented 2 years ago

just tried that on metal an

looks like this is no FlatLaf issue at all - just need to find a "good solution" which works :/ sorry for bothering you