Search in sources :

Example 31 with StyledDocument

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

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

use of javax.swing.text.StyledDocument in project pcgen by PCGen.

the class NotesView method handleBackspace.

//methods dealing with Key Events
private void handleBackspace() {
    // TODO: This sucks, clean it up
    Element elem;
    int pos = editor.getCaretPosition();
    StyledDocument htmlDoc = (ExtendedHTMLDocument) editor.getStyledDocument();
    try {
        if (pos > 0) {
            if ((editor.getSelectedText()) != null) {
                ExtendedHTMLEditorKit.delete(editor);
                return;
            }
            int sOffset = htmlDoc.getParagraphElement(pos).getStartOffset();
            if (sOffset == editor.getSelectionStart()) {
                if (ExtendedHTMLEditorKit.checkParentsTag(htmlDoc.getParagraphElement(editor.getCaretPosition()), HTML.Tag.LI)) {
                    elem = ExtendedHTMLEditorKit.getListItemParent(htmlDoc.getCharacterElement(editor.getCaretPosition()));
                    boolean content = false;
                    int so = elem.getStartOffset();
                    int eo = elem.getEndOffset();
                    if ((so + 1) < eo) {
                        char[] temp = editor.getText(so, eo - so).toCharArray();
                        for (char aTemp : temp) {
                            if (!Character.isWhitespace(aTemp)) {
                                content = true;
                            }
                        }
                    }
                    if (!content) {
                        elem.getParentElement();
                        ExtendedHTMLEditorKit.removeTag(editor, elem, true);
                        editor.setCaretPosition(sOffset - 1);
                        return;
                    }
                    editor.setCaretPosition(editor.getCaretPosition() - 1);
                    editor.moveCaretPosition(editor.getCaretPosition() - 2);
                    editor.replaceSelection("");
                    return;
                }
            }
            editor.replaceSelection("");
        }
    } catch (BadLocationException ble) {
        Logging.errorPrint(ble.getMessage(), ble);
    }
}
Also used : ExtendedHTMLDocument(gmgen.gui.ExtendedHTMLDocument) Element(javax.swing.text.Element) StyledDocument(javax.swing.text.StyledDocument) Point(java.awt.Point) BadLocationException(javax.swing.text.BadLocationException)

Example 34 with StyledDocument

use of javax.swing.text.StyledDocument in project litiengine by gurkenlabs.

the class ConsoleLogHandler method publish.

@Override
public void publish(final LogRecord record) {
    StyledDocument doc = textPane.getStyledDocument();
    SimpleAttributeSet keyWord = new SimpleAttributeSet();
    StyleConstants.setForeground(keyWord, getColor(record.getLevel()));
    StyleConstants.setBold(keyWord, true);
    StyleConstants.setFontSize(keyWord, 12);
    StyleConstants.setFontFamily(keyWord, CONSOLE_FONT);
    SimpleAttributeSet text = new SimpleAttributeSet();
    StyleConstants.setForeground(text, getColor(record.getLevel()));
    StyleConstants.setFontFamily(text, CONSOLE_FONT);
    try {
        doc.insertString(doc.getLength(), String.format("%1$-10s", record.getLevel()), keyWord);
        if (record.getParameters() != null) {
            doc.insertString(doc.getLength(), MessageFormat.format(record.getMessage(), record.getParameters()), text);
        } else {
            doc.insertString(doc.getLength(), record.getMessage(), text);
        }
        doc.insertString(doc.getLength(), "\n", text);
    } catch (BadLocationException e) {
    }
    textPane.setCaretPosition(doc.getLength());
}
Also used : SimpleAttributeSet(javax.swing.text.SimpleAttributeSet) StyledDocument(javax.swing.text.StyledDocument) BadLocationException(javax.swing.text.BadLocationException)

Example 35 with StyledDocument

use of javax.swing.text.StyledDocument in project elki by elki-project.

the class LogPane method publish.

/**
 * Publish a log record to the logging pane.
 *
 * @param record
 *        Log record
 * @throws Exception
 */
protected synchronized void publish(LogRecord record) throws BadLocationException {
    // choose an appropriate formatter
    final Formatter fmt;
    final Style style;
    // always format progress messages using the progress formatter.
    if (record.getLevel().intValue() >= Level.WARNING.intValue()) {
        // format errors using the error formatter
        fmt = errformat;
        style = errStyle;
    } else if (record.getLevel().intValue() <= Level.FINE.intValue()) {
        // format debug statements using the debug formatter.
        fmt = debugformat;
        style = dbgStyle;
    } else {
        // default to the message formatter.
        fmt = msgformat;
        style = msgStyle;
    }
    // format
    final String m;
    m = fmt.format(record);
    StyledDocument doc = getStyledDocument();
    if (record instanceof ProgressLogRecord) {
        if (lastNewlinePos < doc.getLength()) {
            doc.remove(lastNewlinePos, doc.getLength() - lastNewlinePos);
        }
    } else {
        // insert a newline, if we didn't see one yet.
        if (lastNewlinePos < doc.getLength()) {
            doc.insertString(doc.getLength(), "\n", style);
            lastNewlinePos = doc.getLength();
        }
    }
    int tail = tailingNonNewline(m, 0, m.length());
    int headlen = m.length() - tail;
    if (headlen > 0) {
        String pre = m.substring(0, headlen);
        doc.insertString(doc.getLength(), pre, style);
    }
    lastNewlinePos = doc.getLength();
    if (tail > 0) {
        String post = m.substring(m.length() - tail);
        doc.insertString(lastNewlinePos, post, style);
    }
}
Also used : Formatter(java.util.logging.Formatter) ErrorFormatter(de.lmu.ifi.dbs.elki.logging.ErrorFormatter) MessageFormatter(de.lmu.ifi.dbs.elki.logging.MessageFormatter) StyledDocument(javax.swing.text.StyledDocument) Style(javax.swing.text.Style) ProgressLogRecord(de.lmu.ifi.dbs.elki.logging.progress.ProgressLogRecord)

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