Example usage for javax.swing.text Document getText

List of usage examples for javax.swing.text Document getText

Introduction

In this page you can find the example usage for javax.swing.text Document getText.

Prototype

public String getText(int offset, int length) throws BadLocationException;

Source Link

Document

Fetches the text contained within the given portion of the document.

Usage

From source file:ShowHTMLViews.java

public static void displayView(View view, int indent, Document doc, PrintStream out) {
    String name = view.getClass().getName();
    for (int i = 0; i < indent; i++) {
        out.print("  ");
    }/*from  w  ww.ja  v a  2 s.c o  m*/

    int start = view.getStartOffset();
    int end = view.getEndOffset();
    out.println(name + "; offsets [" + start + ", " + end + "]");
    for (int i = 0; i < indent; i++) {
        out.print("  ");
    }
    HTMLDocDisplay.displayAttributes(view.getAttributes(), indent, out);
    int viewCount = view.getViewCount();
    if (viewCount == 0) {
        int length = Math.min(32, end - start);
        try {
            String txt = doc.getText(start, length);
            for (int i = 0; i < indent + 1; i++) {
                out.print("  ");
            }
            out.println("[" + txt + "]");
        } catch (BadLocationException e) {
        }
    } else {
        for (int i = 0; i < viewCount; i++) {
            displayView(view.getView(i), indent + 1, doc, out);
        }
    }
    out.println("");
}

From source file:MainClass.java

public void remove(DocumentFilter.FilterBypass fb, int offset, int length) throws BadLocationException {

    Document doc = fb.getDocument();
    int currentLength = doc.getLength();
    String currentContent = doc.getText(0, currentLength);
    String before = currentContent.substring(0, offset);
    String after = currentContent.substring(length + offset, currentLength);
    String newValue = before + after;
    currentValue = checkInput(newValue, offset);
    fb.remove(offset, length);//from  w ww .j a  v  a  2s.  co m
}

From source file:MainClass.java

public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attrs)
        throws BadLocationException {

    Document doc = fb.getDocument();
    int currentLength = doc.getLength();
    String currentContent = doc.getText(0, currentLength);
    String before = currentContent.substring(0, offset);
    String after = currentContent.substring(length + offset, currentLength);
    String newValue = before + (text == null ? "" : text) + after;
    currentValue = checkInput(newValue, offset);
    fb.replace(offset, length, text, attrs);
}

From source file:Main.java

public int search(String word) {
    int firstOffset = -1;
    Highlighter highlighter = comp.getHighlighter();
    Highlighter.Highlight[] highlights = highlighter.getHighlights();
    for (int i = 0; i < highlights.length; i++) {
        Highlighter.Highlight h = highlights[i];
        if (h.getPainter() instanceof UnderlineHighlightPainter) {
            highlighter.removeHighlight(h);
        }//  w ww  . j a v a2  s  . co m
    }
    if (word == null || word.equals("")) {
        return -1;
    }
    String content = null;
    try {
        Document d = comp.getDocument();
        content = d.getText(0, d.getLength()).toLowerCase();
    } catch (BadLocationException e) {
        return -1;
    }
    word = word.toLowerCase();
    int lastIndex = 0;
    int wordSize = word.length();
    while ((lastIndex = content.indexOf(word, lastIndex)) != -1) {
        int endIndex = lastIndex + wordSize;
        try {
            highlighter.addHighlight(lastIndex, endIndex, painter);
        } catch (BadLocationException e) {
        }
        if (firstOffset == -1) {
            firstOffset = lastIndex;
        }
        lastIndex = endIndex;
    }
    return firstOffset;
}

From source file:com.ansorgit.plugins.bash.editor.inspections.inspections.FixShebangInspection.java

private void updateShebangLines(DocumentEvent documentEvent) {
    validShebangCommands.clear();//  w  ww .  ja v  a2  s.  co m
    try {
        Document doc = documentEvent.getDocument();
        for (String item : doc.getText(0, doc.getLength()).split("\n")) {
            if (item.trim().length() != 0) {
                validShebangCommands.add(item);
            }
        }
    } catch (BadLocationException e) {
        throw new RuntimeException("Could not save shebang inspection settings input", e);
    }
}

From source file:com.croer.javaorange.diviner.SimpleOrangeTextPane.java

protected void fireText(DocumentEvent e) {
    Document document = e.getDocument();
    String text = "";
    try {/*ww  w.  j  ava  2 s. c o  m*/
        text = document.getText(0, document.getLength());
    } catch (BadLocationException ex) {
        Logger.getLogger(SimpleOrangeTextPane.class.getName()).log(Level.SEVERE, null, ex);
    }
    firePropertyChange("text", null, text);
}

From source file:MainClass.java

public void insertString(DocumentFilter.FilterBypass fb, int offset, String string, AttributeSet attr)
        throws BadLocationException {

    if (string == null) {
        return;/*from w w  w.j  a v a 2s .  c  o  m*/
    } else {
        String newValue;
        Document doc = fb.getDocument();
        int length = doc.getLength();
        if (length == 0) {
            newValue = string;
        } else {
            String currentContent = doc.getText(0, length);
            StringBuffer currentBuffer = new StringBuffer(currentContent);
            currentBuffer.insert(offset, string);
            newValue = currentBuffer.toString();
        }
        currentValue = checkInput(newValue, offset);
        fb.insertString(offset, string, attr);
    }
}

From source file:Main.java

public TestPane() {
    setLayout(new BorderLayout());
    JPanel searchPane = new JPanel();
    searchPane.add(new JLabel("Find: "));

    searchPane.add(findText);/*from   w  w w .j a  va  2  s.co m*/

    add(searchPane, BorderLayout.NORTH);
    add(new JScrollPane(ta));

    try (BufferedReader reader = new BufferedReader(new FileReader(new File("c:/Java_Dev/run.bat")))) {
        ta.read(reader, "Text");
    } catch (Exception e) {
        e.printStackTrace();
    }
    ta.setCaretPosition(0);

    keyTimer = new Timer(250, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            String find = findText.getText();
            Document document = ta.getDocument();
            try {
                for (int index = 0; index + find.length() < document.getLength(); index++) {
                    String match = document.getText(index, find.length());
                    if (find.equals(match)) {
                        DefaultHighlighter.DefaultHighlightPainter highlightPainter = new DefaultHighlighter.DefaultHighlightPainter(
                                Color.YELLOW);
                        ta.getHighlighter().addHighlight(index, index + find.length(), highlightPainter);
                    }
                }
            } catch (BadLocationException exp) {
                exp.printStackTrace();
            }
        }
    });
    keyTimer.setRepeats(false);

    findText.getDocument().addDocumentListener(new DocumentListener() {
        @Override
        public void insertUpdate(DocumentEvent e) {
            keyTimer.restart();
        }

        @Override
        public void removeUpdate(DocumentEvent e) {
            keyTimer.restart();
        }

        @Override
        public void changedUpdate(DocumentEvent e) {
            keyTimer.restart();
        }
    });
}

From source file:dmh.swing.huxley.action.InsertHeadingAction.java

@Override
public int manipulateText(Document document, int start, int end) {
    try {//from w w w  .ja v  a 2  s.com
        final int offset = start;
        final int length = end - start;

        // Extract the selected text.
        String title = StringUtils.trimToEmpty(document.getText(offset, length));
        document.remove(offset, length);

        // Add the header.
        String bar = StringUtils.repeat(token, 40);
        String insertText = "\n " + title + "\n" + bar + "\n";
        document.insertString(offset, insertText, null);

        // Return the caret position.
        return start + ("".equals(title) ? 2 : insertText.length());
    } catch (BadLocationException e) {
        // This indicates a programming error.
        throw new RuntimeException(e);
    }
}

From source file:dmh.swing.huxley.action.WrapTextAction.java

@Override
public int manipulateText(Document document, int start, int end) {
    try {// w w w .j av  a  2s.c  om
        final int offset = start;
        final int length = end - start;

        // Extract the selected text.
        String selectedText = StringUtils.trimToEmpty(document.getText(offset, length));
        document.remove(offset, length);

        // Re-insert the text, wrapped in tokens.
        String insertText = prefixToken + selectedText + suffixToken;
        document.insertString(offset, insertText, null);

        // Return the caret position.
        return start + ("".equals(selectedText) ? 1 : insertText.length());
    } catch (BadLocationException e) {
        // This indicates a programming error.
        throw new RuntimeException(e);
    }
}