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:org.domainmath.gui.MainFrame.java

public void deleteText() {
    RSyntaxTextArea textArea = commandArea;
    boolean beep = true;
    if ((textArea != null) && (textArea.isEditable())) {
        try {//from ww  w .  ja v  a 2  s.co  m
            Document doc = textArea.getDocument();
            Caret caret = textArea.getCaret();
            int dot = caret.getDot();
            int mark = caret.getMark();
            if (dot != mark) {
                doc.remove(Math.min(dot, mark), Math.abs(dot - mark));
                beep = false;
            } else if (dot < doc.getLength()) {
                int delChars = 1;
                if (dot < doc.getLength() - 1) {
                    String dotChars = doc.getText(dot, 2);
                    char c0 = dotChars.charAt(0);
                    char c1 = dotChars.charAt(1);
                    if (c0 >= '\uD800' && c0 <= '\uDBFF' && c1 >= '\uDC00' && c1 <= '\uDFFF') {
                        delChars = 2;
                    }
                }
                doc.remove(dot, delChars);
                beep = false;
            }
        } catch (Exception bl) {
        }
    }

    if (beep) {
        UIManager.getLookAndFeel().provideErrorFeedback(textArea);
    }

    textArea.requestFocusInWindow();
}

From source file:org.nuxeo.ecm.core.convert.plugins.text.extractors.RTF2TextConverter.java

@Override
public BlobHolder convert(BlobHolder blobHolder, Map<String, Serializable> parameters)
        throws ConversionException {
    File f = null;//from  ww w  . j  ava  2  s  .c o m
    try {
        RTFEditorKit rtfParser = new RTFEditorKit();
        Document document = rtfParser.createDefaultDocument();
        rtfParser.read(blobHolder.getBlob().getStream(), document, 0);
        String text = document.getText(0, document.getLength());
        f = Framework.createTempFile("swing-rtf2text", ".txt");
        FileUtils.writeStringToFile(f, text);
        Blob blob;
        try (InputStream in = new FileInputStream(f)) {
            blob = Blobs.createBlob(in, "text/plain");
        }
        return new SimpleCachableBlobHolder(blob);
    } catch (IOException | BadLocationException e) {
        throw new ConversionException("Error during Word2Text conversion", e);
    } finally {
        if (f != null) {
            f.delete();
        }
    }
}

From source file:org.obm.push.tnefconverter.RTFUtils.java

private static String extractRtfText(InputStream stream) throws IOException, BadLocationException {
    RTFEditorKit kit = new RTFEditorKit();
    Document doc = kit.createDefaultDocument();
    kit.read(stream, doc, 0);/* w  ww  . j ava2 s  . c o m*/

    return doc.getText(0, doc.getLength());
}

From source file:org.paxle.desktop.impl.event.MultipleChangesListener.java

private void setState(final Object comp, final long when, final boolean init) {
    if (comp instanceof Document) {
        final Document doc = (Document) comp;
        try {/*  w ww .  j  a  va 2s.  c  o  m*/
            setState(doc, doc.getText(0, doc.getLength()), when, init);
        } catch (BadLocationException e) {
            e.printStackTrace();
        }
    } else {
        final Class<?> clazz = comp.getClass();
        final int eidx = getEntryIndex(clazz);
        if (eidx < 0)
            throw new RuntimeException(
                    "component '" + comp.getClass().getName() + "' not supported for monitoring");
        try {
            setState(comp, clazz.getMethod(GET_VALUES[eidx]).invoke(comp), when, init);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:org.python.pydev.core.docutils.StringUtils.java

/**
 * Given some html, extracts its text./*from  w ww.  ja  v a2 s  .  co m*/
 */
public static String extractTextFromHTML(String html) {
    try {
        EditorKit kit = new HTMLEditorKit();
        Document doc = kit.createDefaultDocument();

        // The Document class does not yet handle charset's properly.
        doc.putProperty("IgnoreCharsetDirective", Boolean.TRUE);

        // Create a reader on the HTML content.
        Reader rd = new StringReader(html);

        // Parse the HTML.
        kit.read(rd, doc, 0);

        //  The HTML text is now stored in the document
        return doc.getText(0, doc.getLength());
    } catch (Exception e) {
    }
    return "";
}

From source file:org.silverpeas.core.index.indexing.parser.rtfParser.RtfParser.java

public void outPutContent(Writer out, String path, String encoding) throws IOException {

    FileInputStream in = null;/*from  ww  w.  java2 s .c  om*/
    try {
        in = new FileInputStream(path);
        byte[] buffer = new byte[in.available()];
        in.read(buffer, 0, in.available());

        // RTF always uses ASCII, so we don't need to care about the encoding
        String input = new String(buffer);

        String result = null;
        try {
            // use build in RTF parser from Swing API
            RTFEditorKit rtfEditor = new RTFEditorKit();
            Document doc = rtfEditor.createDefaultDocument();
            rtfEditor.read(new StringReader(input), doc, 0);

            result = doc.getText(0, doc.getLength());
        } catch (Exception e) {
            SilverTrace.warn("indexing", "RtfParser.outPutContent()", "", e);
        }
        out.write(result);
    } finally {
        IOUtils.closeQuietly(in);
    }
}

From source file:org.silverpeas.search.indexEngine.parser.rtfParser.RtfParser.java

public void outPutContent(Writer out, String path, String encoding) throws IOException {

    FileInputStream in = null;/*from  ww w .  ja va 2s. c  om*/
    try {
        in = new FileInputStream(path);
        byte[] buffer = new byte[in.available()];
        in.read(buffer, 0, in.available());

        // RTF always uses ASCII, so we don't need to care about the encoding
        String input = new String(buffer);

        // workaround to remove RTF keywords that cause a NPE in Java 1.4
        // this is a known bug in Java 1.4 that was fixed in 1.5
        // please see http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5042109 for
        // the official bug report
        // input = TS_REMOVE_PATTERN.matcher(input).replaceAll("");

        String result = null;
        try {
            // use build in RTF parser from Swing API
            RTFEditorKit rtfEditor = new RTFEditorKit();
            Document doc = rtfEditor.createDefaultDocument();
            rtfEditor.read(new StringReader(input), doc, 0);

            result = doc.getText(0, doc.getLength());
        } catch (Exception e) {
            SilverTrace.warn("indexEngine", "RtfParser.outPutContent()", "", e);
        }

        SilverTrace.debug("indexEngine", "RtfParser.outPutContent", "root.MSG_GEN_EXIT_METHOD", result);

        out.write(result);
    } finally {
        IOUtils.closeQuietly(in);
    }
}

From source file:qic.ui.ConfigPanel.java

private void saveAndReloadConfig() {
    try {/*from w  w w . j  a  va2 s  . co m*/
        // directly calling getText() from TextArea is causing bad values, probably swing quirks
        Document document = textArea.getDocument();
        Util.overwriteFile(CONFIG_PROPERTIES_FILENAME, document.getText(0, document.getLength()));
        Config.loadConfig();
    } catch (BadLocationException | IOException e) {
        logger.error("Error while saving to " + CONFIG_PROPERTIES_FILENAME);
        showError(e);
    }
}

From source file:qic.ui.GuildPanel.java

void save() {
    try {//from   www.j a va  2s  .  c  o m
        // directly calling getText() from TextArea is causing bad values, probably swing quirks
        Document document = textArea.getDocument();
        String content = document.getText(0, document.getLength());
        Util.overwriteFile(GUILD_LIST_FILENAME, content);
    } catch (BadLocationException | IOException e) {
        logger.error("Error while saving to " + GUILD_LIST_FILENAME);
        showError(e);
    }
}

From source file:sernet.gs.ui.rcp.gsimport.TransferData.java

public static String convertRtf(String notizText) throws IOException, BadLocationException {
    StringReader reader = new StringReader(notizText);
    RTFEditorKit kit = new RTFEditorKit();
    Document document = kit.createDefaultDocument();
    kit.read(reader, document, 0);//from w  w w.j  a  va  2 s.  c o m
    // return plaintext
    return document.getText(0, document.getLength());
}