Search in sources :

Example 21 with HTMLDocument

use of javax.swing.text.html.HTMLDocument in project processing by processing.

the class DetailPanel method setSelectionStyle.

static void setSelectionStyle(JTextPane textPane, boolean selected) {
    Document doc = textPane.getDocument();
    if (doc instanceof HTMLDocument) {
        HTMLDocument html = (HTMLDocument) doc;
        StyleSheet styleSheet = html.getStyleSheet();
        if (selected) {
            styleSheet.addRule("a { text-decoration:underline } ");
        } else {
            styleSheet.addRule("a { text-decoration:none }");
        }
    }
}
Also used : StyleSheet(javax.swing.text.html.StyleSheet) HTMLDocument(javax.swing.text.html.HTMLDocument) HTMLDocument(javax.swing.text.html.HTMLDocument) Document(javax.swing.text.Document)

Example 22 with HTMLDocument

use of javax.swing.text.html.HTMLDocument in project OpenAM by OpenRock.

the class TableJPanel method appendMessage.

private void appendMessage(String colorString, String s) {
    HTMLEditorKit kit = (HTMLEditorKit) messagejEditorPane.getEditorKit();
    HTMLDocument doc = (HTMLDocument) messagejEditorPane.getDocument();
    try {
        String newS = s.replaceAll("<", "&lt;");
        newS = newS.replaceAll(">", "&gt;");
        newS = newS.replaceAll("\n", "<br>");
        kit.insertHTML(doc, messagejEditorPane.getCaretPosition(), "<font color=" + colorString + ">" + newS, 0, 0, HTML.Tag.FONT);
        messagejEditorPane.setCaretPosition(doc.getLength() - 1);
    } catch (Exception ex) {
        //ex.printStackTrace();
        System.out.println("Exception in TableJPanel :" + ex.getMessage());
    }
}
Also used : HTMLDocument(javax.swing.text.html.HTMLDocument) HTMLEditorKit(javax.swing.text.html.HTMLEditorKit)

Example 23 with HTMLDocument

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

the class UnusedDeclarationPresentation method getCustomPreviewPanel.

@Override
public JComponent getCustomPreviewPanel(RefEntity entity) {
    final Project project = entity.getRefManager().getProject();
    JEditorPane htmlView = new JEditorPane() {

        @Override
        public String getToolTipText(MouseEvent evt) {
            int pos = viewToModel(evt.getPoint());
            if (pos >= 0) {
                HTMLDocument hdoc = (HTMLDocument) getDocument();
                javax.swing.text.Element e = hdoc.getCharacterElement(pos);
                AttributeSet a = e.getAttributes();
                SimpleAttributeSet value = (SimpleAttributeSet) a.getAttribute(HTML.Tag.A);
                if (value != null) {
                    String objectPackage = (String) value.getAttribute("qualifiedname");
                    if (objectPackage != null) {
                        return objectPackage;
                    }
                }
            }
            return null;
        }
    };
    htmlView.setContentType(UIUtil.HTML_MIME);
    htmlView.setEditable(false);
    htmlView.setOpaque(false);
    htmlView.setBackground(UIUtil.getLabelBackground());
    htmlView.addHyperlinkListener(new HyperlinkAdapter() {

        @Override
        protected void hyperlinkActivated(HyperlinkEvent e) {
            URL url = e.getURL();
            if (url == null) {
                return;
            }
            @NonNls String ref = url.getRef();
            int offset = Integer.parseInt(ref);
            String fileURL = url.toExternalForm();
            fileURL = fileURL.substring(0, fileURL.indexOf('#'));
            VirtualFile vFile = VirtualFileManager.getInstance().findFileByUrl(fileURL);
            if (vFile == null) {
                vFile = VfsUtil.findFileByURL(url);
            }
            if (vFile != null) {
                final OpenFileDescriptor descriptor = new OpenFileDescriptor(project, vFile, offset);
                FileEditorManager.getInstance(project).openTextEditor(descriptor, true);
            }
        }
    });
    final StyleSheet css = ((HTMLEditorKit) htmlView.getEditorKit()).getStyleSheet();
    css.addRule("p.problem-description-group {text-indent: " + JBUI.scale(9) + "px;font-weight:bold;}");
    css.addRule("div.problem-description {margin-left: " + JBUI.scale(9) + "px;}");
    css.addRule("ul {margin-left:" + JBUI.scale(10) + "px;text-indent: 0}");
    css.addRule("code {font-family:" + UIUtil.getLabelFont().getFamily() + "}");
    final StringBuffer buf = new StringBuffer();
    getComposer().compose(buf, entity, false);
    final String text = buf.toString();
    SingleInspectionProfilePanel.readHTML(htmlView, SingleInspectionProfilePanel.toHTML(htmlView, text, false));
    return ScrollPaneFactory.createScrollPane(htmlView, true);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) MouseEvent(java.awt.event.MouseEvent) HyperlinkEvent(javax.swing.event.HyperlinkEvent) HTMLDocument(javax.swing.text.html.HTMLDocument) HTMLEditorKit(javax.swing.text.html.HTMLEditorKit) URL(java.net.URL) SimpleAttributeSet(javax.swing.text.SimpleAttributeSet) Project(com.intellij.openapi.project.Project) StyleSheet(javax.swing.text.html.StyleSheet) AttributeSet(javax.swing.text.AttributeSet) SimpleAttributeSet(javax.swing.text.SimpleAttributeSet) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) HyperlinkAdapter(com.intellij.ui.HyperlinkAdapter)

Example 24 with HTMLDocument

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

the class EditableNotificationMessageElement method updateStyle.

protected void updateStyle(@NotNull JEditorPane editorPane, @Nullable JTree tree, Object value, boolean selected, boolean hasFocus) {
    super.updateStyle(editorPane, tree, value, selected, hasFocus);
    final HTMLDocument htmlDocument = (HTMLDocument) editorPane.getDocument();
    final Style linkStyle = htmlDocument.getStyleSheet().getStyle(LINK_STYLE);
    StyleConstants.setForeground(linkStyle, IdeTooltipManager.getInstance().getLinkForeground(false));
    StyleConstants.setItalic(linkStyle, true);
    HTMLDocument.Iterator iterator = htmlDocument.getIterator(HTML.Tag.A);
    while (iterator.isValid()) {
        boolean disabledLink = false;
        final AttributeSet attributes = iterator.getAttributes();
        if (attributes instanceof SimpleAttributeSet) {
            final Object attribute = attributes.getAttribute(HTML.Attribute.HREF);
            if (attribute instanceof String && disabledLinks.containsKey(attribute)) {
                disabledLink = true;
                //TODO [Vlad] add support for disabled link text update
                ////final String linkText = disabledLinks.get(attribute);
                //if (linkText != null) {
                //}
                ((SimpleAttributeSet) attributes).removeAttribute(HTML.Attribute.HREF);
            }
            if (attribute == null) {
                disabledLink = true;
            }
        }
        if (!disabledLink) {
            htmlDocument.setCharacterAttributes(iterator.getStartOffset(), iterator.getEndOffset() - iterator.getStartOffset(), linkStyle, false);
        }
        iterator.next();
    }
}
Also used : SimpleAttributeSet(javax.swing.text.SimpleAttributeSet) SimpleAttributeSet(javax.swing.text.SimpleAttributeSet) AttributeSet(javax.swing.text.AttributeSet) HTMLDocument(javax.swing.text.html.HTMLDocument) Style(javax.swing.text.Style)

Example 25 with HTMLDocument

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

the class SwingHelper method scrollToReference.

public static boolean scrollToReference(JEditorPane view, String reference) {
    reference = StringUtil.trimStart(reference, "#");
    List<String> toCheck = Arrays.asList("a", "h1", "h2", "h3", "h4");
    Document document = view.getDocument();
    if (document instanceof HTMLDocument) {
        List<Element> list = new ArrayList<>();
        for (Element root : document.getRootElements()) {
            getAllElements(root, list, toCheck);
        }
        for (Element element : list) {
            AttributeSet attributes = element.getAttributes();
            String nm = (String) attributes.getAttribute(HTML.Attribute.NAME);
            if (nm == null)
                nm = (String) attributes.getAttribute(HTML.Attribute.ID);
            if ((nm != null) && nm.equals(reference)) {
                try {
                    int pos = element.getStartOffset();
                    Rectangle r = view.modelToView(pos);
                    if (r != null) {
                        Rectangle vis = view.getVisibleRect();
                        r.y -= 5;
                        r.height = vis.height;
                        view.scrollRectToVisible(r);
                        return true;
                    }
                } catch (BadLocationException ex) {
                //ignore
                }
            }
        }
    }
    return false;
}
Also used : AttributeSet(javax.swing.text.AttributeSet) HTMLDocument(javax.swing.text.html.HTMLDocument) Element(javax.swing.text.Element) HTMLDocument(javax.swing.text.html.HTMLDocument) Document(javax.swing.text.Document) BadLocationException(javax.swing.text.BadLocationException)

Aggregations

HTMLDocument (javax.swing.text.html.HTMLDocument)30 HTMLEditorKit (javax.swing.text.html.HTMLEditorKit)10 Element (javax.swing.text.Element)6 StyleSheet (javax.swing.text.html.StyleSheet)6 URL (java.net.URL)5 AttributeSet (javax.swing.text.AttributeSet)5 BadLocationException (javax.swing.text.BadLocationException)5 IOException (java.io.IOException)4 Document (javax.swing.text.Document)4 Style (javax.swing.text.Style)3 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 StringReader (java.io.StringReader)2 URLConnection (java.net.URLConnection)2 ArrayList (java.util.ArrayList)2 HyperlinkEvent (javax.swing.event.HyperlinkEvent)2 SimpleAttributeSet (javax.swing.text.SimpleAttributeSet)2 HTMLFrameHyperlinkEvent (javax.swing.text.html.HTMLFrameHyperlinkEvent)2 NlModel (com.android.tools.idea.uibuilder.model.NlModel)1 DataContext (com.intellij.openapi.actionSystem.DataContext)1