Search in sources :

Example 1 with HTML

use of javax.swing.text.html.HTML in project camel by apache.

the class JavadocParser method startTag.

@Override
protected void startTag(TagElement tag) throws ChangedCharSetException {
    super.startTag(tag);
    final HTML.Tag htmlTag = tag.getHTMLTag();
    if (htmlTag != null) {
        if (HTML.Tag.A.equals(htmlTag)) {
            final SimpleAttributeSet attributes = getAttributes();
            final Object name = attributes.getAttribute(HTML.Attribute.NAME);
            if (name != null) {
                final String nameAttr = (String) name;
                if (parserState == ParserState.INIT && ("method_summary".equals(nameAttr) || "method.summary".equals(nameAttr))) {
                    parserState = ParserState.METHOD_SUMMARY;
                } else if (parserState == ParserState.METHOD) {
                    if (methodWithTypes == null) {
                        final String hrefAttr = (String) attributes.getAttribute(HTML.Attribute.HREF);
                        if (hrefAttr != null && hrefAttr.contains(hrefPattern)) {
                            // unescape HTML
                            String methodSignature = hrefAttr.substring(hrefAttr.indexOf('#') + 1);
                            final int firstHyphen = methodSignature.indexOf('-');
                            if (firstHyphen != -1) {
                                final int lastHyphen = methodSignature.lastIndexOf('-');
                                methodSignature = methodSignature.substring(0, firstHyphen) + "(" + methodSignature.substring(firstHyphen + 1, lastHyphen) + ")";
                                methodSignature = methodSignature.replaceAll("-", ",");
                            }
                            // support varargs
                            if (methodSignature.contains("...)")) {
                                methodSignature = methodSignature.replaceAll("\\.\\.\\.\\)", "[])");
                            }
                            // map Java8 array types
                            if (methodSignature.contains(":A")) {
                                methodSignature = methodSignature.replaceAll(":A", "[]");
                            }
                            methodWithTypes = unescapeHtml(methodSignature);
                        }
                    } else {
                        final String title = (String) attributes.getAttribute(HTML.Attribute.TITLE);
                        if (title != null) {
                            // append package name to type name text
                            methodTextBuilder.append(title.substring(title.lastIndexOf(' '))).append('.');
                        }
                    }
                }
            }
        } else if (parserState == ParserState.METHOD_SUMMARY && HTML.Tag.CODE.equals(htmlTag)) {
            parserState = ParserState.METHOD;
        }
    }
}
Also used : SimpleAttributeSet(javax.swing.text.SimpleAttributeSet) HTML(javax.swing.text.html.HTML)

Example 2 with HTML

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

the class IdeTooltipManager method initPane.

public static JEditorPane initPane(@NonNls Html html, final HintHint hintHint, @Nullable final JLayeredPane layeredPane) {
    final Ref<Dimension> prefSize = new Ref<>(null);
    @NonNls String text = HintUtil.prepareHintText(html, hintHint);
    final boolean[] prefSizeWasComputed = { false };
    final JEditorPane pane = new JEditorPane() {

        @Override
        public Dimension getPreferredSize() {
            if (!isShowing() && layeredPane != null) {
                AppUIUtil.targetToDevice(this, layeredPane);
            }
            if (!prefSizeWasComputed[0] && hintHint.isAwtTooltip()) {
                JLayeredPane lp = layeredPane;
                if (lp == null) {
                    JRootPane rootPane = UIUtil.getRootPane(this);
                    if (rootPane != null) {
                        lp = rootPane.getLayeredPane();
                    }
                }
                Dimension size;
                if (lp != null) {
                    size = lp.getSize();
                    prefSizeWasComputed[0] = true;
                } else {
                    size = ScreenUtil.getScreenRectangle(0, 0).getSize();
                }
                int fitWidth = (int) (size.width * 0.8);
                Dimension prefSizeOriginal = super.getPreferredSize();
                if (prefSizeOriginal.width > fitWidth) {
                    setSize(new Dimension(fitWidth, Integer.MAX_VALUE));
                    Dimension fixedWidthSize = super.getPreferredSize();
                    Dimension minSize = super.getMinimumSize();
                    prefSize.set(new Dimension(fitWidth > minSize.width ? fitWidth : minSize.width, fixedWidthSize.height));
                } else {
                    prefSize.set(new Dimension(prefSizeOriginal));
                }
            }
            Dimension s = prefSize.get() != null ? new Dimension(prefSize.get()) : super.getPreferredSize();
            Border b = getBorder();
            if (b != null) {
                JBInsets.addTo(s, b.getBorderInsets(this));
            }
            return s;
        }

        @Override
        public void setPreferredSize(Dimension preferredSize) {
            super.setPreferredSize(preferredSize);
            prefSize.set(preferredSize);
        }
    };
    final HTMLEditorKit.HTMLFactory factory = new HTMLEditorKit.HTMLFactory() {

        @Override
        public View create(Element elem) {
            AttributeSet attrs = elem.getAttributes();
            Object elementName = attrs.getAttribute(AbstractDocument.ElementNameAttribute);
            Object o = elementName != null ? null : attrs.getAttribute(StyleConstants.NameAttribute);
            if (o instanceof HTML.Tag) {
                HTML.Tag kind = (HTML.Tag) o;
                if (kind == HTML.Tag.HR) {
                    return new CustomHrView(elem, hintHint.getTextForeground());
                }
            }
            return super.create(elem);
        }
    };
    HTMLEditorKit kit = new HTMLEditorKit() {

        @Override
        public ViewFactory getViewFactory() {
            return factory;
        }
    };
    pane.setEditorKit(kit);
    pane.setText(text);
    pane.setCaretPosition(0);
    pane.setEditable(false);
    if (hintHint.isOwnBorderAllowed()) {
        setBorder(pane);
        setColors(pane);
    } else {
        pane.setBorder(null);
    }
    if (!hintHint.isAwtTooltip()) {
        prefSizeWasComputed[0] = true;
    }
    final boolean opaque = hintHint.isOpaqueAllowed();
    pane.setOpaque(opaque);
    if (UIUtil.isUnderNimbusLookAndFeel() && !opaque) {
        pane.setBackground(UIUtil.TRANSPARENT_COLOR);
    } else {
        pane.setBackground(hintHint.getTextBackground());
    }
    return pane;
}
Also used : NonNls(org.jetbrains.annotations.NonNls) HTML(javax.swing.text.html.HTML) HTMLEditorKit(javax.swing.text.html.HTMLEditorKit) RelativePoint(com.intellij.ui.awt.RelativePoint) Ref(com.intellij.openapi.util.Ref) Border(javax.swing.border.Border)

Aggregations

HTML (javax.swing.text.html.HTML)2 Ref (com.intellij.openapi.util.Ref)1 RelativePoint (com.intellij.ui.awt.RelativePoint)1 Border (javax.swing.border.Border)1 SimpleAttributeSet (javax.swing.text.SimpleAttributeSet)1 HTMLEditorKit (javax.swing.text.html.HTMLEditorKit)1 NonNls (org.jetbrains.annotations.NonNls)1