Search in sources :

Example 6 with HTMLEditorKit

use of javax.swing.text.html.HTMLEditorKit in project adempiere by adempiere.

the class HTMLRenderer method get.

/**
	 * 	Get View from HTML String
	 *	@param html html string
	 *	@return renderer view
	 */
public static HTMLRenderer get(String html) {
    HTMLEditorKit kit = new HTMLEditorKit();
    HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument();
    try {
        doc.remove(0, doc.getLength());
        Reader r = new StringReader(html);
        kit.read(r, doc, 0);
    } catch (Exception e) {
        log.log(Level.SEVERE, "", e);
    }
    //	Create Renderer
    Element element = doc.getDefaultRootElement();
    ViewFactory factory = kit.getViewFactory();
    //	Y_AXIS is main
    View view = factory.create(element);
    HTMLRenderer renderer = new HTMLRenderer(factory, view);
    renderer.preferenceChanged(null, true, true);
    return renderer;
}
Also used : HTMLDocument(javax.swing.text.html.HTMLDocument) Element(javax.swing.text.Element) ViewFactory(javax.swing.text.ViewFactory) StringReader(java.io.StringReader) Reader(java.io.Reader) StringReader(java.io.StringReader) HTMLEditorKit(javax.swing.text.html.HTMLEditorKit) View(javax.swing.text.View) BadLocationException(javax.swing.text.BadLocationException)

Example 7 with HTMLEditorKit

use of javax.swing.text.html.HTMLEditorKit in project adempiere by adempiere.

the class Worker method run.

/**
	 *  Worker: Read available Online Help Pages
	 */
public void run() {
    if (m_links == null)
        return;
    URL url = null;
    try {
        url = new URL(m_urlString);
    } catch (Exception e) {
        System.err.println("OnlineHelp.Worker.run (url) - " + e);
    }
    if (url == null)
        return;
    //  Read Reference Page
    try {
        URLConnection conn = url.openConnection();
        InputStream is = conn.getInputStream();
        HTMLEditorKit kit = new HTMLEditorKit();
        HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument();
        doc.putProperty("IgnoreCharsetDirective", new Boolean(true));
        kit.read(new InputStreamReader(is), doc, 0);
        //  Get The Links to the Help Pages
        HTMLDocument.Iterator it = doc.getIterator(HTML.Tag.A);
        Object target = null;
        Object href = null;
        while (it != null && it.isValid()) {
            AttributeSet as = it.getAttributes();
            //  key keys
            if (target == null || href == null) {
                Enumeration en = as.getAttributeNames();
                while (en.hasMoreElements()) {
                    Object o = en.nextElement();
                    if (target == null && o.toString().equals("target"))
                        //	javax.swing.text.html.HTML$Attribute
                        target = o;
                    else if (href == null && o.toString().equals("href"))
                        href = o;
                }
            }
            if (target != null && "Online".equals(as.getAttribute(target))) {
                //  Format: /help/<AD_Window_ID>/index.html
                String hrefString = (String) as.getAttribute(href);
                if (hrefString != null) {
                    try {
                        //		System.err.println(hrefString);
                        String AD_Window_ID = hrefString.substring(hrefString.indexOf('/', 1), hrefString.lastIndexOf('/'));
                        m_links.put(AD_Window_ID, hrefString);
                    } catch (Exception e) {
                        System.err.println("OnlineHelp.Worker.run (help) - " + e);
                    }
                }
            }
            it.next();
        }
        is.close();
    } catch (ConnectException e) {
    //	System.err.println("OnlineHelp.Worker.run URL=" + url + " - " + e);
    } catch (UnknownHostException uhe) {
    //	System.err.println("OnlineHelp.Worker.run " + uhe);
    } catch (Exception e) {
        System.err.println("OnlineHelp.Worker.run (e) " + e);
    //	e.printStackTrace();
    } catch (Throwable t) {
        System.err.println("OnlineHelp.Worker.run (t) " + t);
    //	t.printStackTrace();
    }
//	System.out.println("OnlineHelp - Links=" + m_links.size());
}
Also used : Enumeration(java.util.Enumeration) InputStreamReader(java.io.InputStreamReader) UnknownHostException(java.net.UnknownHostException) InputStream(java.io.InputStream) HTMLDocument(javax.swing.text.html.HTMLDocument) HTMLEditorKit(javax.swing.text.html.HTMLEditorKit) URL(java.net.URL) UnknownHostException(java.net.UnknownHostException) ConnectException(java.net.ConnectException) URLConnection(java.net.URLConnection) AttributeSet(javax.swing.text.AttributeSet) ConnectException(java.net.ConnectException)

Example 8 with HTMLEditorKit

use of javax.swing.text.html.HTMLEditorKit in project intellij-community by JetBrains.

the class Messages method configureMessagePaneUi.

@NotNull
public static JTextPane configureMessagePaneUi(@NotNull JTextPane messageComponent, @Nullable String message, @Nullable UIUtil.FontSize fontSize) {
    UIUtil.FontSize fixedFontSize = fontSize == null ? UIUtil.FontSize.NORMAL : fontSize;
    messageComponent.setFont(UIUtil.getLabelFont(fixedFontSize));
    if (BasicHTML.isHTMLString(message)) {
        HTMLEditorKit editorKit = UIUtil.getHTMLEditorKit();
        Font font = UIUtil.getLabelFont(fixedFontSize);
        editorKit.getStyleSheet().addRule(UIUtil.displayPropertiesToCSS(font, UIUtil.getLabelForeground()));
        messageComponent.setEditorKit(editorKit);
    }
    messageComponent.setText(message);
    messageComponent.setEditable(false);
    if (messageComponent.getCaret() != null) {
        messageComponent.setCaretPosition(0);
    }
    if (UIUtil.isUnderNimbusLookAndFeel()) {
        messageComponent.setOpaque(false);
        messageComponent.setBackground(UIUtil.TRANSPARENT_COLOR);
    } else {
        messageComponent.setBackground(UIUtil.getOptionPaneBackground());
    }
    messageComponent.setForeground(UIUtil.getLabelForeground());
    return messageComponent;
}
Also used : HTMLEditorKit(javax.swing.text.html.HTMLEditorKit) UIUtil(com.intellij.util.ui.UIUtil)

Example 9 with HTMLEditorKit

use of javax.swing.text.html.HTMLEditorKit in project intellij-community by JetBrains.

the class JBLabel method updateStyle.

private void updateStyle(@NotNull JEditorPane pane) {
    EditorKit kit = pane.getEditorKit();
    if (kit instanceof HTMLEditorKit) {
        StyleSheet css = ((HTMLEditorKit) kit).getStyleSheet();
        css.addRule("body, p {" + "color:#" + ColorUtil.toHex(getForeground()) + ";" + "font-family:" + getFont().getFamily() + ";" + "font-size:" + getFont().getSize() + "pt;" + "white-space:nowrap;}");
    }
}
Also used : EditorKit(javax.swing.text.EditorKit) HTMLEditorKit(javax.swing.text.html.HTMLEditorKit) StyleSheet(javax.swing.text.html.StyleSheet) HTMLEditorKit(javax.swing.text.html.HTMLEditorKit)

Example 10 with HTMLEditorKit

use of javax.swing.text.html.HTMLEditorKit in project sling by apache.

the class WebloaderJob method getDocumentUrlsFromGoogle.

private URL[] getDocumentUrlsFromGoogle(String currentFiletype, int start) throws IOException, BadLocationException {
    final List urls = new ArrayList();
    String query = webQuery + " filetype:" + currentFiletype;
    final URL google = new URL("http://www.google.com/search?q=" + URLEncoder.encode(query, "UTF-8") + "&start=" + start);
    log.debug("Querying {}", google.toString());
    statusInfo = "Querying " + google.toString();
    statusDetails = "";
    URLConnection con = google.openConnection();
    con.setRequestProperty("User-Agent", "");
    InputStream in = con.getInputStream();
    try {
        HTMLEditorKit kit = new HTMLEditorKit();
        HTMLDocument doc = new HTMLDocument();
        doc.putProperty("IgnoreCharsetDirective", Boolean.TRUE);
        kit.read(new InputStreamReader(in, "UTF-8"), doc, 0);
        HTMLDocument.Iterator it = doc.getIterator(HTML.Tag.A);
        while (it.isValid()) {
            if (it.getAttributes() != null) {
                String href = (String) it.getAttributes().getAttribute(HTML.Attribute.HREF);
                if (href != null && href.endsWith("." + currentFiletype)) {
                    URL url = new URL(new URL("http", "www.google.com", "dummy"), href);
                    if (url.getHost().indexOf("google") == -1) {
                        log.debug("Got document URL from google: {}", url);
                        statusDetails = "Got URL " + url;
                        urls.add(url);
                    }
                }
            }
            it.next();
        }
    } finally {
        in.close();
    }
    return (URL[]) urls.toArray(new URL[urls.size()]);
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) HTMLDocument(javax.swing.text.html.HTMLDocument) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) HTMLEditorKit(javax.swing.text.html.HTMLEditorKit) URL(java.net.URL) URLConnection(java.net.URLConnection)

Aggregations

HTMLEditorKit (javax.swing.text.html.HTMLEditorKit)47 HTMLDocument (javax.swing.text.html.HTMLDocument)18 JEditorPane (javax.swing.JEditorPane)10 StyleSheet (javax.swing.text.html.StyleSheet)9 IOException (java.io.IOException)8 JScrollPane (javax.swing.JScrollPane)8 Dimension (java.awt.Dimension)7 URL (java.net.URL)7 Document (javax.swing.text.Document)6 MouseEvent (java.awt.event.MouseEvent)5 StringReader (java.io.StringReader)5 HyperlinkEvent (javax.swing.event.HyperlinkEvent)5 ActionListener (java.awt.event.ActionListener)4 HyperlinkListener (javax.swing.event.HyperlinkListener)4 BorderLayout (java.awt.BorderLayout)3 ActionEvent (java.awt.event.ActionEvent)3 InputStream (java.io.InputStream)3 BadLocationException (javax.swing.text.BadLocationException)3 MouseAdapter (java.awt.event.MouseAdapter)2 InputStreamReader (java.io.InputStreamReader)2