/*
* Copyright (c) 2012 Sam Harwell, Tunnel Vision Laboratories LLC
* All rights reserved.
*
* The source code of this document is proprietary work, and is not licensed for
* distribution. For information about licensing, contact Sam Harwell at:
* sam@tunnelvisionlabs.com
*/
package org.antlr.netbeans.editor.navigation;
import java.awt.Color;
import java.awt.Insets;
import java.awt.Rectangle;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JEditorPane;
import javax.swing.SwingUtilities;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.EditorKit;
import javax.swing.text.html.HTMLEditorKit;
/**
* HTML documentation view.
* Javadoc content is displayed in JEditorPane pane using HTMLEditorKit.
*
* @author Martin Roskanin
* @since 03/2002
*/
class HTMLDocView extends JEditorPane {
// -J-Dorg.antlr.netbeans.editor.navigation.HTMLDocView.level=FINE
private static final Logger LOGGER = Logger.getLogger(HTMLDocView.class.getName());
private HTMLEditorKit htmlKit;
/** Creates a new instance of HTMLJavaDocView */
public HTMLDocView(Color bgColor) {
setEditable(false);
setFocusable(true);
setBackground(bgColor);
setMargin(new Insets(0,3,3,3));
}
/** Sets the javadoc content as HTML document */
public void setContent(final String content, final String reference) {
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run(){
Reader in = new StringReader("<HTML><BODY>"+content+"</BODY></HTML>");//NOI18N
try{
setContentType("text/html"); //NOI18N
Document doc = getDocument();
doc.remove(0, doc.getLength());
getEditorKit().read(in, getDocument(), 0); //!!! still too expensive to be called from AWT
setCaretPosition(0);
if (reference != null) {
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run(){
scrollToReference(reference);
}
});
} else {
scrollRectToVisible(new Rectangle(0,0,0,0));
}
} catch (IOException | BadLocationException ioe) {
LOGGER.log(Level.WARNING, "An exception occurred while setting HTML content.", ioe);
}
}
});
}
@Override
protected EditorKit createDefaultEditorKit() {
// it is extremelly slow to init it
if (htmlKit == null){
htmlKit= new HTMLEditorKit ();
setEditorKit(htmlKit);
// override the Swing default CSS to make the HTMLEditorKit use the
// same font as the rest of the UI.
// XXX the style sheet is shared by all HTMLEditorKits. We must
// detect if it has been tweaked by ourselves or someone else
// (template description for example) and avoid doing the same
// thing again
if (htmlKit.getStyleSheet().getStyleSheets() != null)
return htmlKit;
javax.swing.text.html.StyleSheet css = new javax.swing.text.html.StyleSheet();
java.awt.Font f = getFont();
css.addRule(new StringBuffer("body { font-size: ").append(f.getSize()) // NOI18N
.append("; font-family: ").append(f.getName()).append("; }").toString()); // NOI18N
css.addStyleSheet(htmlKit.getStyleSheet());
htmlKit.setStyleSheet(css);
}
return htmlKit;
}
}