Search in sources :

Example 26 with StyledDocument

use of javax.swing.text.StyledDocument in project java-swing-tips by aterai.

the class BottomInsetViewFactory method setDummyText.

private static void setDummyText(JTextPane textPane, MutableAttributeSet attr) {
    textPane.setText("12341234\n1234 567890 5555 66666 77777\n88 999999 ");
    try {
        StyledDocument doc = textPane.getStyledDocument();
        doc.insertString(doc.getLength(), "134500698\n", attr);
    } catch (BadLocationException ex) {
        // should never happen
        RuntimeException wrap = new StringIndexOutOfBoundsException(ex.offsetRequested());
        wrap.initCause(ex);
        throw wrap;
    }
// StyledDocument doc = new DefaultStyledDocument();
// MutableAttributeSet a = new SimpleAttributeSet();
// StyleConstants.setLineSpacing(a, .5f);
// doc.setParagraphAttributes(0, doc.getLength() - 1, a, false);
// textPane.setStyledDocument(doc);
}
Also used : StyledDocument(javax.swing.text.StyledDocument) BadLocationException(javax.swing.text.BadLocationException)

Example 27 with StyledDocument

use of javax.swing.text.StyledDocument in project groovy by apache.

the class ConsoleSupport method addStylesToDocument.

protected void addStylesToDocument(JTextPane outputArea) {
    StyledDocument doc = outputArea.getStyledDocument();
    Style def = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);
    Style regular = doc.addStyle("regular", def);
    StyleConstants.setFontFamily(def, "Monospaced");
    promptStyle = doc.addStyle("prompt", regular);
    StyleConstants.setForeground(promptStyle, Color.BLUE);
    commandStyle = doc.addStyle("command", regular);
    StyleConstants.setForeground(commandStyle, Color.MAGENTA);
    outputStyle = doc.addStyle("output", regular);
    StyleConstants.setBold(outputStyle, true);
}
Also used : StyledDocument(javax.swing.text.StyledDocument) Style(javax.swing.text.Style)

Example 28 with StyledDocument

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

the class TutorialStep method initStepNumber.

/**
   * Create and add the step number indicator. Note that this is a custom
   * display that surrounds the number with a circle thus has some tricky
   * display characteristics. It's unclear if a form can be leveraged for this.
   */
private void initStepNumber() {
    // Get standard label font.
    Font font = new JLabel().getFont();
    JTextPane stepNumber = new JTextPane();
    stepNumber.setEditable(false);
    stepNumber.setText(String.valueOf(myIndex));
    Font boldFont = new Font(font.getFontName(), Font.BOLD, 11);
    stepNumber.setFont(boldFont);
    stepNumber.setOpaque(false);
    stepNumber.setForeground(NUMBER_COLOR);
    stepNumber.setBorder(new NumberBorder());
    Dimension size = new Dimension(21, 21);
    stepNumber.setSize(size);
    stepNumber.setPreferredSize(size);
    stepNumber.setMinimumSize(size);
    stepNumber.setMaximumSize(size);
    StyledDocument doc = stepNumber.getStyledDocument();
    SimpleAttributeSet center = new SimpleAttributeSet();
    StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);
    doc.setParagraphAttributes(0, doc.getLength(), center, false);
    GridBagConstraints c = new GridBagConstraints();
    c.gridx = 0;
    c.gridy = 0;
    c.weightx = 0;
    c.fill = GridBagConstraints.NONE;
    c.anchor = GridBagConstraints.CENTER;
    c.insets = JBUI.insets(5);
    add(stepNumber, c);
}
Also used : SimpleAttributeSet(javax.swing.text.SimpleAttributeSet) StyledDocument(javax.swing.text.StyledDocument)

Example 29 with StyledDocument

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

the class AndroidThemePreviewPanel method createErrorPanel.

private void createErrorPanel() {
    myErrorLabel = new JTextPane() {

        @Override
        public Dimension getMaximumSize() {
            // Necessary to vertically center it inside a Box.
            return super.getPreferredSize();
        }
    };
    myErrorLabel.setOpaque(false);
    myErrorPanel = new Box(BoxLayout.PAGE_AXIS);
    myErrorPanel.add(Box.createVerticalGlue());
    myErrorPanel.add(myErrorLabel);
    myErrorPanel.add(Box.createVerticalGlue());
    myErrorPanel.setOpaque(false);
    StyledDocument document = myErrorLabel.getStyledDocument();
    SimpleAttributeSet attributes = new SimpleAttributeSet();
    StyleConstants.setAlignment(attributes, StyleConstants.ALIGN_CENTER);
    document.setParagraphAttributes(0, document.getLength(), attributes, false);
}
Also used : SimpleAttributeSet(javax.swing.text.SimpleAttributeSet) StyledDocument(javax.swing.text.StyledDocument)

Example 30 with StyledDocument

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

Aggregations

StyledDocument (javax.swing.text.StyledDocument)63 BadLocationException (javax.swing.text.BadLocationException)29 Style (javax.swing.text.Style)18 SimpleAttributeSet (javax.swing.text.SimpleAttributeSet)12 Point (java.awt.Point)7 JTextPane (javax.swing.JTextPane)7 DefaultStyledDocument (javax.swing.text.DefaultStyledDocument)7 ArrayList (java.util.ArrayList)4 JLabel (javax.swing.JLabel)4 Test (org.junit.jupiter.api.Test)4 Font (java.awt.Font)3 LogRecord (java.util.logging.LogRecord)3 Matcher (java.util.regex.Matcher)3 ImageIcon (javax.swing.ImageIcon)3 JPanel (javax.swing.JPanel)3 PersistentArrayMap (clojure.lang.PersistentArrayMap)2 BorderLayout (java.awt.BorderLayout)2 Dimension (java.awt.Dimension)2 Rectangle (java.awt.Rectangle)2 IOException (java.io.IOException)2