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.alex73.skarynka.scan.ui.book.PagePopupMenu.java

String showPageNumberDialog() {
    StringBuilder result = new StringBuilder();
    PageNumber dialog = new PageNumber(DataStorage.mainFrame, true);
    dialog.setTitle(Messages.getString("PAGE_NUMBER_TITLE", Book2.simplifyPageNumber(startSelection)));
    dialog.errorLabel.setText(" ");
    dialog.renameButton.addActionListener(new ActionListener() {
        @Override// w ww  . j a v  a 2  s .  com
        public void actionPerformed(ActionEvent e) {
            result.append(dialog.txtNumber.getText().trim());
            dialog.dispose();
        }
    });

    dialog.txtNumber.getDocument().addDocumentListener(new DocumentListener() {
        @Override
        public void removeUpdate(DocumentEvent e) {
            check(e.getDocument());
        }

        @Override
        public void insertUpdate(DocumentEvent e) {
            check(e.getDocument());
        }

        @Override
        public void changedUpdate(DocumentEvent e) {
            check(e.getDocument());
        }

        void check(Document d) {
            dialog.errorLabel.setText(" ");
            try {
                String newText = d.getText(0, d.getLength()).trim();
                newText = Book2.formatPageNumber(newText);
                if (StringUtils.isEmpty(newText)) {
                    dialog.renameButton.setEnabled(false);
                    dialog.errorLabel.setText(Messages.getString("PAGE_NUMBER_ERROR_WRONG"));
                } else {
                    Book2.PageInfo pi = book.getPageInfo(newText);
                    if (pi != null) {
                        dialog.renameButton.setEnabled(false);
                        dialog.errorLabel.setText(Messages.getString("PAGE_NUMBER_ERROR_EXIST",
                                Book2.simplifyPageNumber(newText)));
                    } else {
                        dialog.renameButton.setEnabled(true);
                    }
                }
            } catch (BadLocationException ex) {
                dialog.renameButton.setEnabled(false);
            }
        }
    });

    dialog.setLocationRelativeTo(DataStorage.mainFrame);
    dialog.setVisible(true);
    return result.toString();
}

From source file:org.apache.syncope.ide.netbeans.view.ResourceExplorerTopComponent.java

private void saveContent() {
    try {//  ww  w . j  av  a2s.com
        JTextComponent ed = EditorRegistry.lastFocusedComponent();
        Document document = ed.getDocument();
        String content = document.getText(0, document.getLength());
        String path = (String) document.getProperty(Document.TitleProperty);
        String[] temp = path.split(File.separator);
        String name = temp[temp.length - 1];
        String templateType = temp[temp.length - 2];
        temp = name.split("\\.");
        String format = temp[1];
        String key = temp[0];

        if (templateType.equals("Mail")) {
            if (format.equals("txt")) {
                mailTemplateManagerService.setFormat(key, MailTemplateFormat.TEXT,
                        IOUtils.toInputStream(content, encodingPattern));
            } else {
                mailTemplateManagerService.setFormat(key, MailTemplateFormat.HTML,
                        IOUtils.toInputStream(content, encodingPattern));
            }
        } else if (format.equals("html")) {
            reportTemplateManagerService.setFormat(key, ReportTemplateFormat.HTML,
                    IOUtils.toInputStream(content, encodingPattern));
        } else if (format.equals("fo")) {
            reportTemplateManagerService.setFormat(key, ReportTemplateFormat.FO,
                    IOUtils.toInputStream(content, encodingPattern));
        } else {
            reportTemplateManagerService.setFormat(key, ReportTemplateFormat.CSV,
                    IOUtils.toInputStream(content, encodingPattern));
        }
    } catch (BadLocationException e) {
        Exceptions.printStackTrace(e);
    }
}

From source file:org.astrojournal.logging.JTextPaneAppender.java

@Override
public void append(final LogEvent event) {
    final String message = new String(this.getLayout().toByteArray(event));
    try {//from  www  . ja va  2 s. c  om
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    if (jTextPane != null) {
                        try {
                            Document doc = jTextPane.getDocument();
                            if (event.getLevel().equals(Level.DEBUG)) {
                                doc.insertString(doc.getLength(), message, styleSmall);
                            } else if (event.getLevel().equals(Level.INFO)) {
                                if (message.startsWith(
                                        AJMetaInfo.NAME.getInfo() + " " + AJMetaInfo.VERSION.getInfo())) {
                                    doc.insertString(doc.getLength(), message, styleSmallItalic);
                                } else if (message.indexOf("Testing pdflatex") != -1) {
                                    String[] lines = message.split("\n");
                                    doc.insertString(doc.getLength(), lines[0], styleBold);
                                    for (int i = 1; i < lines.length; i++) {
                                        doc.insertString(doc.getLength(), lines[i] + "\n", styleSmallItalic);
                                    }
                                    doc.insertString(doc.getLength(), "\n\n", styleRegular);
                                } else if (StringUtils.countMatches(message, "\n") > 1) {
                                    doc.insertString(doc.getLength(), message, styleSmallItalic);
                                } else if (!message.startsWith("\t")) {
                                    doc.insertString(doc.getLength(), message, styleBold);
                                } else {
                                    doc.insertString(doc.getLength(), message, styleRegular);
                                }
                            } else if (event.getLevel().equals(Level.WARN)) {
                                doc.insertString(doc.getLength(), message, styleBlue);
                            } else {
                                doc.insertString(doc.getLength(), message, styleRed);
                            }
                        } catch (BadLocationException e) {
                            LOGGER.error(e, e);
                        }
                    }
                } catch (final Throwable t) {
                    LOGGER.error("Unable to append log to text pane: " + t.getMessage()
                            + ". Please see help menu for reporting this issue.", t);
                }
            }
        });
    } catch (final IllegalStateException e) {
        LOGGER.error("Unable to append log to text pane: " + e.getMessage()
                + ". Please see help menu for reporting this issue.", e);
    }
}

From source file:org.domainmath.gui.MainFrame.java

public void deleteText() {
    RSyntaxTextArea textArea = commandArea;
    boolean beep = true;
    if ((textArea != null) && (textArea.isEditable())) {
        try {//from w w  w  . j a v  a 2 s . c  o  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   w ww . j  a v a 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);/*from  w w w .  ja  v  a 2  s . co  m*/

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

From source file:org.omegat.gui.scripting.ScriptingWindow.java

/**
 * Print log text to the Scripting Window's console area. A trailing line break will be added
 * automatically.//w  w w. j a  v a  2 s  . c  o m
 */
private void logResultToWindow(String s) {
    Document doc = m_txtResult.getDocument();
    try {
        doc.insertString(doc.getLength(), s + "\n", null);
    } catch (BadLocationException e1) {
        /* empty */
    }
}

From source file:org.opendatakit.appengine.updater.UpdaterWindow.java

public synchronized void displayOutput(StreamType type, String actionName, String line) {
    Document doc = editorArea.getDocument();

    String str = actionName + ((type == StreamType.ERR) ? "!:  " : " :  ") + line + "\r\n";

    try {/*from  ww  w  .j  av a 2s .co m*/
        doc.insertString(doc.getLength(), str, null);
    } catch (BadLocationException e) {
        e.printStackTrace();
    }
}

From source file:org.opendatakit.briefcase.ui.ScrollingStatusListDialog.java

private void appendToDocument(JTextComponent component, String msg) {
    Document doc = component.getDocument();
    try {/*from  w  w  w .j a va 2  s .  c om*/
        doc.insertString(doc.getLength(), "\n" + msg, null);
    } catch (BadLocationException e) {
        LOG.error("Insertion failed: " + e.getMessage());
    }
}

From source file:org.opendatakit.briefcase.ui.ScrollingStatusListDialog.java

@EventSubscriber(eventClass = FormStatusEvent.class)
public void onFormStatusEvent(FormStatusEvent event) {
    // Since there can be multiple FormStatusEvent's published concurrently,
    // we have to check if the event is meant for this dialog instance.
    if (isShowing() && event.getStatus().getFormDefinition().equals(form)) {
        try {/*from  w  ww  .ja v a 2 s . c  o  m*/
            Document doc = editorArea.getDocument();
            String latestStatus = "\n" + event.getStatusString();
            doc.insertString(doc.getLength(), latestStatus, null);
        } catch (BadLocationException e) {
            LOG.warn("failed to update history", e);
        }
    }
}