public StyledHTMLEditorPane() { this.setContentType("text/html"); this.document = (HTMLDocument) this.getDocument(); this.setDocument(document); Constants.loadSimpleStyle(document.getStyleSheet()); }
/** * Sets the font used for HTML displays to the specified font. Components that display HTML do not * necessarily honor font properties, since the HTML document can override these values. Calling * {@code setHtmlFont} after the data is set will force the HTML display to use the font specified * to this method. * * @param doc the HTML document to update * @param font the font to use * @throws NullPointerException if any parameter is {@code null} */ public static void setHtmlFont(HTMLDocument doc, Font font) { String stylesheet = String.format(STYLESHEET, font.getName(), font.getSize(), font.getName(), font.getSize()); try { doc.getStyleSheet().loadRules(new StringReader(stylesheet), null); } catch (IOException e) { // this should never happen with our sheet throw new IllegalStateException(e); } }
/** * Sets the default font for an HTML document (e.g., in a tool tip displaying HTML). This is here * because when rendering HTML, {@code setFont()} is not honored. * * @param doc The document to modify. * @param font The font to use. * @param fg The default foreground color. */ public static void setFont(HTMLDocument doc, Font font, Color fg) { doc.getStyleSheet() .addRule( "body { font-family: " + font.getFamily() + "; font-size: " + font.getSize() + "pt" + "; color: " + getHexString(fg) + "; }"); }
/** * Tweaks a <code>JEditorPane</code> so it can be used to render the content in a focusable * pseudo-tool tip. It is assumed that the editor pane is using an <code>HTMLDocument</code>. * * @param textArea The editor pane to tweak. */ public static void tweakTipEditorPane(JEditorPane textArea) { // Jump through a few hoops to get things looking nice in Nimbus boolean isNimbus = isNimbusLookAndFeel(); if (isNimbus) { Color selBG = textArea.getSelectionColor(); Color selFG = textArea.getSelectedTextColor(); textArea.setUI(new javax.swing.plaf.basic.BasicEditorPaneUI()); textArea.setSelectedTextColor(selFG); textArea.setSelectionColor(selBG); } textArea.setEditable(false); // Required for links to work! textArea.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); // Make selection visible even though we are not (initially) focusable. textArea.getCaret().setSelectionVisible(true); // Set the foreground color. Important because when rendering HTML, // default foreground becomes black, which may not match all LAF's // (e.g. Substance). Color fg = UIManager.getColor("Label.foreground"); if (fg == null || (isNimbus && isDerivedColor(fg))) { fg = SystemColor.textText; } textArea.setForeground(fg); // Make it use the "tool tip" background color. textArea.setBackground(TipUtil.getToolTipBackground()); // Force JEditorPane to use a certain font even in HTML. // All standard LookAndFeels, even Nimbus (!), define Label.font. Font font = UIManager.getFont("Label.font"); if (font == null) { // Try to make a sensible default font = new Font("SansSerif", Font.PLAIN, 12); } HTMLDocument doc = (HTMLDocument) textArea.getDocument(); setFont(doc, font, fg); // Always add link foreground rule. Unfortunately these CSS rules // stack each time the LaF is changed (how can we overwrite them // without clearing out the important "standard" ones?). Color linkFG = RSyntaxUtilities.getHyperlinkForeground(); doc.getStyleSheet().addRule("a { color: " + getHexString(linkFG) + "; }"); }
/** * Modify the current base font size * * @param s the size to add to the current size */ public void modifyFont(final int s) { try { currentFontSize = Math.min(Math.max(0, currentFontSize + s), 6); HTMLDocument doc = (HTMLDocument) accessibleHtml.getDocument(); StyleContext.NamedStyle style = (StyleContext.NamedStyle) doc.getStyleSheet().getStyle("body"); MutableAttributeSet attr = (MutableAttributeSet) style.getResolveParent(); if (StyleConstants.getFontSize(attr) != fontSizes[currentFontSize]) { ConfigManager.setHelpFontSize(currentFontSize); StyleConstants.setFontSize(attr, fontSizes[currentFontSize]); accessibleHtml.setVisible(false); style.setResolveParent(attr); accessibleHtml.setVisible(true); } } catch (NullPointerException e) { // Can occur if the user is changing quickly the document SwingUtilities.invokeLater( () -> { modifyFont(s); }); } }
protected StyleSheet getStyleSheet() { HTMLDocument doc = (HTMLDocument) getDocument(); return doc.getStyleSheet(); }