Example usage for javax.swing.text Document getLength

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

Introduction

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

Prototype

public int getLength();

Source Link

Document

Returns number of characters of content currently in the document.

Usage

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 {/*from  www .  j a va  2  s .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.photovault.swingui.PhotoInfoEditor.java

/**
 DocumentEvent is generated when text field (or to be more exact, its 
 document) is modified./*from   w  ww  .j a va 2 s .c  o m*/
 @param ev The DocumentEvent describing the change.
 */
public void insertUpdate(DocumentEvent ev) {
    Document changedDoc = ev.getDocument();
    PhotoInfoFields changedField = (PhotoInfoFields) changedDoc.getProperty(FIELD);
    Set fieldValues = ctrl.getFieldValues(changedField);
    /* Avoid emptying model when the field has multiple values
       in the model.
    */
    Object value = getField(changedField);
    StringBuffer debugMsg = new StringBuffer();
    debugMsg.append("insertUpdate ").append(changedField).append(": ").append(value);
    debugMsg.append("\nOld values: [");
    boolean first = true;
    for (Object oldValue : fieldValues) {
        if (!first)
            debugMsg.append(", ");
        debugMsg.append(oldValue);
        first = false;
    }
    debugMsg.append("]");
    log.debug(debugMsg.toString());

    if ((fieldValues.size() == 1 && !fieldValues.iterator().next().equals(value))
            || (fieldValues.size() != 1 && changedDoc.getLength() > 0)) {
        ctrl.viewChanged(this, changedField, value);
    }
}

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

/**
 * Given some html, extracts its text./*from w  ww .ja  v  a  2  s . c o 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;//  w  w w  . j av  a2  s .  c  o  m
    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  w w  w  . j  a  va 2s.  com*/
    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:processing.app.EditorTab.java

/**
 * Replace the entire contents of this tab.
 *//*from w  w w .j a v a2 s  .c  om*/
public void setText(String what) {
    // Remove all highlights, since these will all end up at the start of the
    // text otherwise. Preserving them is tricky, so better just remove them.
    textarea.removeAllLineHighlights();
    // Set the caret update policy to NEVER_UPDATE while completely replacing
    // the current text. Normally, the caret tracks inserts and deletions, but
    // replacing the entire text will always make the caret end up at the end,
    // which isn't really useful. With NEVER_UPDATE, the caret will just keep
    // its absolute position (number of characters from the start), which isn't
    // always perfect, but the best we can do without making a diff of the old
    // and new text and some guesswork.
    // Note that we cannot use textarea.setText() here, since that first removes
    // text and then inserts the new text. Even with NEVER_UPDATE, the caret
    // always makes sure to stay valid, so first removing all text makes it
    // reset to 0. Also note that simply saving and restoring the caret position
    // will work, but then the scroll position might change in response to the
    // caret position.
    DefaultCaret caret = (DefaultCaret) textarea.getCaret();
    int policy = caret.getUpdatePolicy();
    caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
    try {
        Document doc = textarea.getDocument();
        int oldLength = doc.getLength();
        // The undo manager already seems to group the insert and remove together
        // automatically, but better be explicit about it.
        textarea.beginAtomicEdit();
        try {
            doc.insertString(oldLength, what, null);
            doc.remove(0, oldLength);
        } catch (BadLocationException e) {
            System.err.println("Unexpected failure replacing text");
        } finally {
            textarea.endAtomicEdit();
        }
    } finally {
        caret.setUpdatePolicy(policy);
    }
}

From source file:qic.ui.ConfigPanel.java

private void saveAndReloadConfig() {
    try {/*from w w  w  . j av a2  s  .  c o  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 {/*w ww . jav a  2 s .co  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   www .  j a  va2s  . co m*/
    // return plaintext
    return document.getText(0, document.getLength());
}

From source file:simplealbum.mvc.autocomplete.JTextPaneX.java

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