Search in sources :

Example 6 with StyledDocument

use of javax.swing.text.StyledDocument in project JMRI by JMRI.

the class JTextPaneAppender method append.

/**
     * @see
     * org.apache.log4j.AppenderSkeleton#append(org.apache.log4j.spi.LoggingEvent)
     */
@Override
public void append(LoggingEvent event) {
    if (myTextPane == null) {
        LogLog.warn("TextPane is not initialized");
        return;
    }
    // if myTextPane == null
    String text = this.layout.format(event);
    String[] stackTrace = event.getThrowableStrRep();
    if (stackTrace != null) {
        StringBuffer sb = new StringBuffer(text);
        for (int i = 0; i < stackTrace.length; i++) {
            sb.append("    ").append(stackTrace[i]).append("\n");
        }
        // for i
        text = sb.toString();
    }
    StyledDocument myDoc = myTextPane.getStyledDocument();
    try {
        myDoc.insertString(myDoc.getLength(), text, myAttributeSet.get(event.getLevel().toString()));
    } catch (BadLocationException badex) {
        // can't log this, as it would be recursive error
        System.err.println(badex);
    }
    myTextPane.setCaretPosition(myDoc.getLength());
}
Also used : StyledDocument(javax.swing.text.StyledDocument) BadLocationException(javax.swing.text.BadLocationException)

Example 7 with StyledDocument

use of javax.swing.text.StyledDocument in project binnavi by google.

the class BaseTypeTableCellRenderer method renderText.

public static String renderText(final TypeInstance instance) {
    final StyledDocument document = new DefaultStyledDocument();
    generateDocument(instance, false, document);
    try {
        return document.getText(0, document.getLength());
    } catch (final BadLocationException exception) {
        CUtilityFunctions.logException(exception);
    }
    return "";
}
Also used : DefaultStyledDocument(javax.swing.text.DefaultStyledDocument) StyledDocument(javax.swing.text.StyledDocument) DefaultStyledDocument(javax.swing.text.DefaultStyledDocument) BadLocationException(javax.swing.text.BadLocationException)

Example 8 with StyledDocument

use of javax.swing.text.StyledDocument in project groovy-core by groovy.

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 9 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 10 with StyledDocument

use of javax.swing.text.StyledDocument in project enclojure by EricThorsen.

the class ClojureCodeCompletionItem method doSubstitute.

private void doSubstitute(final JTextComponent component, final String toAdd, final int backOffset) {
    final StyledDocument doc = (StyledDocument) component.getDocument();
    class AtomicChange implements Runnable {

        public void run() {
            int caretOffset = component.getCaretPosition();
            String cname = component.getClass().getName();
            Boolean _isReplPanel = false;
            if (cname.equals("javax.swing.JEditorPane"))
                _isReplPanel = true;
            String value = getText();
            String javaList = "";
            if (toAdd != null) {
                value += toAdd;
            }
            try {
                String c = component.getText(caretOffset - 1, 1);
                if (//java package containing the Class at the end
                !_isClojure && _isNamespaceOrPkg) {
                    value = setValueForClass(value, component);
                    javaList = getJavaImportListStr(value);
                    if ((javaList.contains("(")) && (!_isReplPanel)) {
                        int messageret = (JOptionPane.showConfirmDialog(component, "Do you want to add the import " + value + " to your ns imports?", "add import ?", 0));
                        if (messageret == 0) {
                            PersistentArrayMap entry = addImportList(component, javaList);
                            //component.setCaretPosition(caretOffset + javaList.length());
                            value = getClassPart(value);
                            if (entry != null) {
                                String origNS = (String) entry.get(Keyword.intern(Symbol.create("orignodestr")));
                                String newNS = (String) entry.get(Keyword.intern(Symbol.create("newnodestr")));
                                int insertOffset = newNS.length() - origNS.length();
                                _carretOffset = _carretOffset + insertOffset;
                                _dotOffset = _dotOffset + insertOffset;
                            }
                        }
                    }
                }
                if (!c.equals("/") && (_layout != backSlashAfterParen) && (_layout != backSlashNoParen)) {
                    switch(_layout) {
                        case LowercaseAfterParen:
                            doc.remove(_dotOffset, _carretOffset - _dotOffset);
                            break;
                        case UppercaseAfterParen:
                            doc.remove(_dotOffset, _carretOffset - _dotOffset);
                            break;
                        case UppercaseAfterParenWithDot:
                            if (//java method
                            _isMethodOrFunction && (!_isClojure)) {
                                if (_isConstructor) {
                                    value = "";
                                } else if (Character.isLetter(value.charAt(0))) {
                                    {
                                        doc.remove(_dotOffset, _carretOffset - _dotOffset);
                                        value = "." + value;
                                    }
                                }
                            } else
                                doc.remove(_dotOffset, _carretOffset - _dotOffset);
                            break;
                        case LowercaseAfterParenWithDot:
                            if (//java method
                            _isMethodOrFunction && (!_isClojure)) {
                                if (_isConstructor) {
                                    value = "";
                                } else if (Character.isLetter(value.charAt(0))) {
                                    {
                                        doc.remove(_dotOffset, _carretOffset - _dotOffset);
                                        value = "." + value;
                                    }
                                }
                            } else
                                doc.remove(_dotOffset, _carretOffset - _dotOffset);
                            break;
                        case backSlashAfterParen:
                            doc.remove(_dotOffset, _carretOffset - _dotOffset);
                            break;
                        case FirstDotAfterParen:
                            doc.remove(_dotOffset, _carretOffset - _dotOffset);
                            break;
                        case FirstDotNoParen:
                            doc.remove(_dotOffset, _carretOffset - _dotOffset);
                            break;
                        case UppercaseNoParen:
                            doc.remove(_dotOffset, _carretOffset - _dotOffset);
                            break;
                        case UppercaseNoParenWithDot:
                            if (//java method
                            _isMethodOrFunction && (!_isClojure)) {
                                if (_isConstructor) {
                                    value = "";
                                } else if (Character.isLetter(value.charAt(0))) {
                                    {
                                        doc.remove(_dotOffset, _carretOffset - _dotOffset);
                                        value = "." + value;
                                    }
                                }
                            } else
                                doc.remove(_dotOffset, _carretOffset - _dotOffset);
                            break;
                        case LowercaseNoParenWithDot:
                            if (//java method
                            _isMethodOrFunction && (!_isClojure)) {
                                if (_isConstructor) {
                                    value = "";
                                } else if (Character.isLetter(value.charAt(0))) {
                                    {
                                        doc.remove(_dotOffset, _carretOffset - _dotOffset);
                                        value = "." + value;
                                    }
                                }
                            } else
                                doc.remove(_dotOffset, _carretOffset - _dotOffset);
                            break;
                        case LowercaseNoParen:
                            doc.remove(_dotOffset, _carretOffset - _dotOffset);
                            break;
                    }
                    doc.insertString(_dotOffset, value, null);
                } else {
                    if (!c.equals("/")) {
                        int backSlashOffset = indexOfBackSlash(component);
                        if (backSlashOffset == -1)
                            backSlashOffset = caretOffset;
                        if (_isStatic || _layout == backSlashAfterParen || _layout == backSlashNoParen) {
                            if (_isClojure && _isMethodOrFunction) {
                                if (isInCurrentNamespace(_fullclassname, component)) {
                                    doc.remove(_dotOffset, _carretOffset - _dotOffset);
                                    doc.insertString(_dotOffset, setValue(value), null);
                                } else {
                                    doc.remove(backSlashOffset + 1, caretOffset - backSlashOffset - 1);
                                    doc.insertString(backSlashOffset + 1, value, null);
                                }
                            } else {
                                doc.remove(backSlashOffset + 1, caretOffset - backSlashOffset - 1);
                                doc.insertString(backSlashOffset + 1, value, null);
                            }
                        } else {
                            doc.remove(_dotOffset, _carretOffset - _dotOffset);
                            doc.insertString(_dotOffset, setValue(value), null);
                        }
                    } else if (_isStatic || _layout == backSlashAfterParen || _layout == backSlashNoParen)
                        if (_isClojure && _isMethodOrFunction) {
                            if (isInCurrentNamespace(_fullclassname, component)) {
                                doc.remove(_dotOffset, _carretOffset - _dotOffset);
                                doc.insertString(_dotOffset, setValue(value), null);
                            } else
                                doc.insertString(_carretOffset, value, null);
                        } else
                            doc.insertString(_carretOffset, value, null);
                    else {
                        doc.remove(_dotOffset, _carretOffset - _dotOffset);
                        doc.insertString(_dotOffset, setValue(value), null);
                    }
                }
                component.setCaretPosition(component.getCaretPosition() - backOffset);
            } catch (BadLocationException e) {
                LOG.log(Level.FINEST, e.getMessage());
            }
        }
    }
    AtomicChange change = new AtomicChange();
    try {
        NbDocument.runAtomicAsUser(doc, change);
    } catch (BadLocationException ex) {
        LOG.log(Level.FINEST, ex.getMessage());
    }
}
Also used : PersistentArrayMap(clojure.lang.PersistentArrayMap) StyledDocument(javax.swing.text.StyledDocument) BadLocationException(javax.swing.text.BadLocationException)

Aggregations

StyledDocument (javax.swing.text.StyledDocument)17 BadLocationException (javax.swing.text.BadLocationException)6 SimpleAttributeSet (javax.swing.text.SimpleAttributeSet)6 Style (javax.swing.text.Style)4 PersistentArrayMap (clojure.lang.PersistentArrayMap)2 Point (java.awt.Point)2 ColorSet (com.android.tools.sherpa.drawing.ColorSet)1 ExtendedHTMLDocument (gmgen.gui.ExtendedHTMLDocument)1 BorderLayout (java.awt.BorderLayout)1 AttributedString (java.text.AttributedString)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 Hashtable (java.util.Hashtable)1 Iterator (java.util.Iterator)1 Entry (java.util.Map.Entry)1 JPanel (javax.swing.JPanel)1 JSplitPane (javax.swing.JSplitPane)1 JTabbedPane (javax.swing.JTabbedPane)1 JTable (javax.swing.JTable)1 JTextPane (javax.swing.JTextPane)1