Search in sources :

Example 31 with SimpleAttributeSet

use of javax.swing.text.SimpleAttributeSet in project android by JetBrains.

the class TextWidget method drawText.

protected void drawText(ViewTransform transform, Graphics2D g, int x, int y) {
    int tx = transform.getSwingX(x);
    int ty = transform.getSwingY(y);
    int h = transform.getSwingDimension(mWidget.getDrawHeight());
    int w = transform.getSwingDimension(mWidget.getDrawWidth());
    int horizontalPadding = transform.getSwingDimension(mHorizontalPadding + mHorizontalMargin);
    int verticalPadding = transform.getSwingDimension(mVerticalPadding + mVerticalMargin);
    int originalSize = mFont.getSize();
    int scaleSize = transform.getSwingDimension(originalSize);
    g.setFont(mFont.deriveFont((float) scaleSize));
    FontMetrics fontMetrics = g.getFontMetrics();
    Color color = mTextColor.getColor();
    if (mWidget.getVisibility() == ConstraintWidget.INVISIBLE) {
        color = new Color(color.getRed(), color.getGreen(), color.getBlue(), 100);
    }
    g.setColor(color);
    String string = getText();
    if (mToUpperCase) {
        string = string.toUpperCase();
    }
    int ftx = 0;
    int fty = 0;
    int stringWidth = fontMetrics.stringWidth(string);
    if (stringWidth > w && !mSingleLine) {
        // if it is multi lined text use a swing text pane to do the wrap
        mTextPane.setText(string);
        mTextPane.setForeground(color);
        mTextPane.setSize(w, h);
        mTextPane.setFont(mFont.deriveFont((float) scaleSize * 0.88f));
        StyledDocument doc = mTextPane.getStyledDocument();
        SimpleAttributeSet attributeSet = new SimpleAttributeSet();
        switch(mAlignmentX) {
            case TEXT_ALIGNMENT_VIEW_START:
                StyleConstants.setAlignment(attributeSet, StyleConstants.ALIGN_LEFT);
                break;
            case TEXT_ALIGNMENT_CENTER:
                StyleConstants.setAlignment(attributeSet, StyleConstants.ALIGN_CENTER);
                break;
            case TEXT_ALIGNMENT_VIEW_END:
                StyleConstants.setAlignment(attributeSet, StyleConstants.ALIGN_RIGHT);
                break;
        }
        switch(mAlignmentY) {
            case TEXT_ALIGNMENT_VIEW_START:
                mTextPane.setAlignmentY(JTextArea.TOP_ALIGNMENT);
                break;
            case TEXT_ALIGNMENT_CENTER:
                mTextPane.setAlignmentY(JTextArea.CENTER_ALIGNMENT);
                break;
            case TEXT_ALIGNMENT_VIEW_END:
                mTextPane.setAlignmentY(JTextArea.BOTTOM_ALIGNMENT);
                break;
        }
        doc.setParagraphAttributes(0, doc.getLength(), attributeSet, false);
        g.translate(tx, ty);
        Shape clip = g.getClip();
        g.clipRect(0, 0, w, h);
        mTextPane.paint(g);
        g.setClip(clip);
        g.translate(-tx, -ty);
    } else {
        switch(mAlignmentX) {
            case TEXT_ALIGNMENT_VIEW_START:
                {
                    ftx = tx + horizontalPadding;
                }
                break;
            case TEXT_ALIGNMENT_CENTER:
                {
                    int paddx = (w - stringWidth) / 2;
                    ftx = tx + paddx;
                }
                break;
            case TEXT_ALIGNMENT_VIEW_END:
                {
                    int padd = w - stringWidth + horizontalPadding;
                    ftx = tx + padd;
                }
                break;
        }
        switch(mAlignmentY) {
            case TEXT_ALIGNMENT_VIEW_START:
                {
                    fty = ty + fontMetrics.getAscent() + fontMetrics.getMaxDescent() + verticalPadding;
                }
                break;
            case TEXT_ALIGNMENT_CENTER:
                {
                    fty = ty + fontMetrics.getAscent() + (h - fontMetrics.getAscent()) / 2;
                }
                break;
            case TEXT_ALIGNMENT_VIEW_END:
                {
                    fty = ty + h - fontMetrics.getMaxDescent() - verticalPadding;
                }
                break;
        }
        Shape clip = g.getClip();
        g.clipRect(tx, ty, w, h);
        g.drawString(string, ftx, fty);
        g.setClip(clip);
    }
}
Also used : SimpleAttributeSet(javax.swing.text.SimpleAttributeSet) StyledDocument(javax.swing.text.StyledDocument) AttributedString(java.text.AttributedString)

Example 32 with SimpleAttributeSet

use of javax.swing.text.SimpleAttributeSet in project android by JetBrains.

the class DrawTextRegion method paint.

@Override
public void paint(Graphics2D g, SceneContext sceneContext) {
    int tx = x;
    int ty = y;
    int h = height;
    int w = width;
    if (!sceneContext.getColorSet().drawBackground()) {
        return;
    }
    super.paint(g, sceneContext);
    ColorSet colorSet = sceneContext.getColorSet();
    int horizontalPadding = mHorizontalPadding + mHorizontalMargin;
    int verticalPadding = mVerticalPadding + mVerticalMargin;
    g.setFont(mFont);
    FontMetrics fontMetrics = g.getFontMetrics();
    Color color = colorSet.getFrames();
    g.setColor(color);
    String string = mText;
    if (mToUpperCase) {
        string = string.toUpperCase();
    }
    int ftx = 0;
    int fty = 0;
    int stringWidth = fontMetrics.stringWidth(string);
    if (stringWidth > w && !mSingleLine) {
        // if it is multi lined text use a swing text pane to do the wrap
        mTextPane.setText(string);
        mTextPane.setForeground(color);
        mTextPane.setSize(w, h);
        mTextPane.setFont(mFont.deriveFont((float) mFont.getSize() * 0.88f));
        StyledDocument doc = mTextPane.getStyledDocument();
        SimpleAttributeSet attributeSet = new SimpleAttributeSet();
        switch(mAlignmentX) {
            case TEXT_ALIGNMENT_VIEW_START:
                StyleConstants.setAlignment(attributeSet, StyleConstants.ALIGN_LEFT);
                break;
            case TEXT_ALIGNMENT_CENTER:
                StyleConstants.setAlignment(attributeSet, StyleConstants.ALIGN_CENTER);
                break;
            case TEXT_ALIGNMENT_VIEW_END:
                StyleConstants.setAlignment(attributeSet, StyleConstants.ALIGN_RIGHT);
                break;
        }
        switch(mAlignmentY) {
            case TEXT_ALIGNMENT_VIEW_START:
                mTextPane.setAlignmentY(JTextArea.TOP_ALIGNMENT);
                break;
            case TEXT_ALIGNMENT_CENTER:
                mTextPane.setAlignmentY(JTextArea.CENTER_ALIGNMENT);
                break;
            case TEXT_ALIGNMENT_VIEW_END:
                mTextPane.setAlignmentY(JTextArea.BOTTOM_ALIGNMENT);
                break;
        }
        doc.setParagraphAttributes(0, doc.getLength(), attributeSet, false);
        g.translate(tx, ty);
        Shape clip = g.getClip();
        g.clipRect(0, 0, w, h);
        mTextPane.paint(g);
        g.setClip(clip);
        g.translate(-tx, -ty);
    } else {
        switch(mAlignmentX) {
            case TEXT_ALIGNMENT_VIEW_START:
                {
                    ftx = tx + horizontalPadding;
                }
                break;
            case TEXT_ALIGNMENT_CENTER:
                {
                    int paddx = (w - stringWidth) / 2;
                    ftx = tx + paddx;
                }
                break;
            case TEXT_ALIGNMENT_VIEW_END:
                {
                    int padd = w - stringWidth + horizontalPadding;
                    ftx = tx + padd;
                }
                break;
        }
        fty = myBaseLineOffset + ty;
        Shape clip = g.getClip();
        g.clipRect(tx, ty, w, h);
        g.drawString(string, ftx, fty);
        g.setClip(clip);
    }
}
Also used : ColorSet(com.android.tools.sherpa.drawing.ColorSet) SimpleAttributeSet(javax.swing.text.SimpleAttributeSet) StyledDocument(javax.swing.text.StyledDocument)

Example 33 with SimpleAttributeSet

use of javax.swing.text.SimpleAttributeSet in project android by JetBrains.

the class DeviceSelectionPopup method initMessage.

private void initMessage() {
    myMessage.setEditable(false);
    myMessage.setFont(promptLabel.getFont());
    myMessage.setBackground(myPanel.getBackground());
    StyledDocument doc = myMessage.getStyledDocument();
    SimpleAttributeSet center = new SimpleAttributeSet();
    StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);
    doc.setParagraphAttributes(0, doc.getLength(), center, false);
    if (myNoMatchingDevice) {
        myMessage.setText(NO_DEVICE_MESSAGE);
        myMessage.setForeground(ERROR_COLOR);
    } else {
        myMessage.setText(PROMPT_HELP_TEXT);
        myMessage.setForeground(MESSAGE_COLOR);
    }
}
Also used : SimpleAttributeSet(javax.swing.text.SimpleAttributeSet) StyledDocument(javax.swing.text.StyledDocument)

Example 34 with SimpleAttributeSet

use of javax.swing.text.SimpleAttributeSet 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 35 with SimpleAttributeSet

use of javax.swing.text.SimpleAttributeSet 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)

Aggregations

SimpleAttributeSet (javax.swing.text.SimpleAttributeSet)58 StyledDocument (javax.swing.text.StyledDocument)11 BadLocationException (javax.swing.text.BadLocationException)8 MutableAttributeSet (javax.swing.text.MutableAttributeSet)7 HTMLDocument (javax.swing.text.html.HTMLDocument)5 Color (java.awt.Color)3 AttributeSet (javax.swing.text.AttributeSet)3 View (javax.swing.text.View)3 HTML (javax.swing.text.html.HTML)3 User (lib.pircbot.User)3 Font (java.awt.Font)2 PrintWriter (java.io.PrintWriter)2 StringWriter (java.io.StringWriter)2 URL (java.net.URL)2 Matcher (java.util.regex.Matcher)2 Pattern (java.util.regex.Pattern)2 Document (javax.swing.text.Document)2 Test (org.junit.Test)2 TaskCallbackStatus (cbit.vcell.mapping.TaskCallbackMessage.TaskCallbackStatus)1 ColorSet (com.android.tools.sherpa.drawing.ColorSet)1