Example usage for javax.swing.text StyledDocument insertString

List of usage examples for javax.swing.text StyledDocument insertString

Introduction

In this page you can find the example usage for javax.swing.text StyledDocument insertString.

Prototype

public void insertString(int offset, String str, AttributeSet a) throws BadLocationException;

Source Link

Document

Inserts a string of content.

Usage

From source file:gda.gui.BatonPanel.java

private void updateMessagePanel(final UserMessage message) {
    SwingUtilities.invokeLater(new Runnable() {

        private void addMessageHeader() throws BadLocationException {
            StyledDocument doc = (StyledDocument) logPanel.getDocument();

            Date date = new Date();
            Format formatter = new SimpleDateFormat("dd MMM HH:mm:ss");
            String newMessage = "\n" + formatter.format(date) + "\t";

            newMessage += "#" + message.getSourceClientNumber();
            newMessage += " " + message.getSourceUsername() + "";

            doc.insertString(doc.getLength(), newMessage, doc.getStyle("bold"));
        }//  w  w w  .j av  a 2 s.  co m

        private void addMessageBody() throws BadLocationException {
            StyledDocument doc = (StyledDocument) logPanel.getDocument();
            String newMessage = "\n    ";
            newMessage += message.getMessage();
            doc.insertString(doc.getLength(), newMessage, doc.getStyle("regular"));
        }

        @Override
        public void run() {
            try {
                addMessageHeader();
                addMessageBody();
                // scroll down to display new message
                getLogPanel().getCaret().setDot(getLogPanel().getText().length());
                saveLog(logPanel.getStyledDocument());
            } catch (BadLocationException e) {
                //
            }
        }
    });
}

From source file:com.xilinx.kintex7.MainScreen.java

private void updateLog(String message, SimpleAttributeSet aset) {
    StyledDocument doc = textArea.getStyledDocument();
    String[] lines = Utils.wrapText(message, 60);
    try {/*w w  w  . j  a v  a 2s .  c  om*/
        for (int i = 0; i < lines.length; ++i)
            doc.insertString(doc.getLength(), "\n" + lines[i], aset);
    } catch (Exception e) {

    }
}

From source file:com.github.alexfalappa.nbspringboot.cfgprops.completion.CfgPropCompletionItem.java

@Override
public void defaultAction(JTextComponent jtc) {
    try {// ww  w.  j  a v  a 2  s .  com
        StyledDocument doc = (StyledDocument) jtc.getDocument();
        // calculate the amount of chars to remove (by default from property start up to caret position)
        int lenToRemove = caretOffset - propStartOffset;
        if (overwrite) {
            // NOTE: the editor removes by itself the word at caret when ctrl + enter is pressed
            // the document state here is different from when the completion was invoked thus we have to
            // find again the offset of the equal sign in the line
            Element lineElement = doc.getParagraphElement(caretOffset);
            String line = doc.getText(lineElement.getStartOffset(),
                    lineElement.getEndOffset() - lineElement.getStartOffset());
            int equalSignIndex = line.indexOf('=');
            if (equalSignIndex >= 0) {
                // from property start to equal sign
                lenToRemove = lineElement.getStartOffset() + equalSignIndex - propStartOffset;
            } else {
                // from property start to end of line (except line terminator)
                lenToRemove = lineElement.getEndOffset() - 1 - propStartOffset;
            }
        }
        // remove characters from the property name start offset
        doc.remove(propStartOffset, lenToRemove);
        doc.insertString(propStartOffset, getText(), null);
        // close the code completion box
        Completion.get().hideAll();
    } catch (BadLocationException ex) {
        Exceptions.printStackTrace(ex);
    }
}

From source file:homenetapp.HomeNetAppGui.java

private void updateTextPane(final String text, final javax.swing.text.Style color) {
    SwingUtilities.invokeLater(new Runnable() {

        public void run() {
            javax.swing.text.StyledDocument doc = consoleTextPane.getStyledDocument();

            try {

                // consoleTextPane.se
                if (color != null) {
                    doc.setLogicalStyle(doc.getLength(), color);
                }//from  w w w .jav  a 2 s  .  c om
                doc.insertString(doc.getLength(), text, null);

                if (doc.getLength() > 10000) {
                    doc.remove(0, text.length());
                }
            } catch (javax.swing.text.BadLocationException e) {
                throw new RuntimeException(e);
            }
            consoleTextPane.setCaretPosition(doc.getLength() - 1);
        }
    });
}

From source file:ome.formats.importer.gui.GuiCommonElements.java

/**
 * Add a text pane to the parent container
 * /*from   w  w w  . j a v a 2 s.c om*/
 * @param container - parent container
 * @param text - text for new Text Pane
 * @param placement - TableLayout placement within the parent container
 * @param debug - turn on/off red debug borders
 * @return new JTextPane
 */
public static synchronized JTextPane addTextPane(Container container, String text, String placement,
        boolean debug) {
    StyleContext context = new StyleContext();
    StyledDocument document = new DefaultStyledDocument(context);

    Style style = context.getStyle(StyleContext.DEFAULT_STYLE);
    StyleConstants.setAlignment(style, StyleConstants.ALIGN_LEFT);

    try {
        document.insertString(document.getLength(), text, null);
    } catch (BadLocationException e) {
        log.error("BadLocationException inserting text to document.");
    }

    JTextPane textPane = new JTextPane(document);
    textPane.setOpaque(false);
    textPane.setEditable(false);
    textPane.setFocusable(false);
    container.add(textPane, placement);

    if (debug == true)
        textPane.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createLineBorder(Color.red),
                textPane.getBorder()));

    return textPane;
}

From source file:ome.formats.importer.gui.GuiCommonElements.java

/**
 * A version of addTextPane that lets you add a style and context
 * /*ww  w  . jav a  2s  . co m*/
 * @param container - parent container
 * @param text - text for new Text Pane
 * @param placement - TableLayout placement within the parent container
 * @param context - context for new text pane
 * @param style - style to apply to new text pane
 * @param debug - turn on/off red debug borders
 * @return new JTextPane
 */
public static synchronized JTextPane addTextPane(Container container, String text, String placement,
        StyleContext context, Style style, boolean debug) {
    StyledDocument document = new DefaultStyledDocument(context);

    try {
        document.insertString(document.getLength(), text, style);
    } catch (BadLocationException e) {
        log.error("BadLocationException inserting text to document.");
    }

    JTextPane textPane = new JTextPane(document);
    textPane.setOpaque(false);
    textPane.setEditable(false);
    textPane.setFocusable(false);
    container.add(textPane, placement);

    if (debug == true)
        textPane.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createLineBorder(Color.red),
                textPane.getBorder()));

    return textPane;
}

From source file:ome.formats.importer.gui.GuiImporter.java

/**
 * This method appends data to the output window
 * //from   www . j  av a2  s .  co m
 * @param s - text to append
 */
public void appendToOutput(String s) {
    try {
        StyledDocument doc = (StyledDocument) outputTextPane.getDocument();
        Style style = doc.addStyle("StyleName", null);
        StyleConstants.setForeground(style, Color.black);
        StyleConstants.setFontFamily(style, "SansSerif");
        StyleConstants.setFontSize(style, 12);
        StyleConstants.setBold(style, false);

        doc.insertString(doc.getLength(), s, style);

        //trim the document size so it doesn't grow to big
        int maxChars = 200000;
        if (doc.getLength() > maxChars)
            doc.remove(0, doc.getLength() - maxChars);

        //outputTextPane.setDocument(doc);
    } catch (BadLocationException e) {
    }
}

From source file:ome.formats.importer.gui.GuiImporter.java

/**
 * This method appends data to the output window.
 * /*from  www .  j av a 2 s  . c om*/
 * @param s - string to append
 */
public void appendToDebug(String s) {
    log.debug(s);
    try {
        StyledDocument doc = (StyledDocument) debugTextPane.getDocument();

        Style style = doc.addStyle("StyleName", null);
        StyleConstants.setForeground(style, Color.black);
        StyleConstants.setFontFamily(style, "SansSerif");
        StyleConstants.setFontSize(style, 12);
        StyleConstants.setBold(style, false);

        doc.insertString(doc.getLength(), s, style);

        //trim the document size so it doesn't grow to big
        int maxChars = 200000;
        if (doc.getLength() > maxChars)
            doc.remove(0, doc.getLength() - maxChars);

        //debugTextPane.setDocument(doc);
    } catch (BadLocationException e) {
    }
}

From source file:org.dodjsoft.tail.TailFilePanel.java

@Override
public void handle(final String string) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override/*from  www . j  a  v  a  2  s . c  om*/
        public void run() {
            StyledDocument document = (StyledDocument) tailTextPanel.getDocument();
            try {
                document.insertString(document.getLength(), string + "\n", null);
            } catch (BadLocationException ex) {
                Exceptions.printStackTrace(ex);
            }
            tailTextPanel.setCaretPosition(document.getLength());
        }
    });
}

From source file:org.drugis.addis.gui.wizard.AddStudyWizard.java

private static JComponent buildTip(String tip) {
    JTextPane area = new JTextPane();
    StyledDocument doc = area.getStyledDocument();
    addStylesToDoc(doc);/*  ww w  .j av a2s  .  c o m*/

    area.setBackground(new Color(255, 180, 180));

    try {
        doc.insertString(0, "x", doc.getStyle("tip"));
        doc.insertString(doc.getLength(), " Tip: \n", doc.getStyle("bold"));
        doc.insertString(doc.getLength(), tip, doc.getStyle("regular"));
    } catch (BadLocationException e) {
        e.printStackTrace();
    }

    area.setEditable(false);

    JScrollPane pane = new JScrollPane(area);
    pane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
    pane.setPreferredSize(TextComponentFactory.textPaneDimension(area, 270, 70));

    pane.setWheelScrollingEnabled(true);
    pane.getVerticalScrollBar().setValue(0);

    return pane;
}