Package javax.swing.text

Examples of javax.swing.text.Document


     * Given some html, extracts its text.
     */
    public static String extractTextFromHTML(final String html) {
        try {
            final EditorKit kit = new HTMLEditorKit();
            final 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.
            final 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 (final Exception e) {
        }
        return "";
    }
View Full Code Here


        Position pos = mspe.preModify();
        boolean atEnd = mspe.atEnd();

        // add the new text to the end of the current document
        try {
            final Document doc = logPane.getDocument();

            // if there is text to remove, remove it first
            if (removeLen > 0) {
                doc.remove(0, removeLen);
            }

            Position end = doc.getEndPosition();
            doc.insertString(end.getOffset() - 1, str, null);
        } catch (BadLocationException ble) {
            // should never happen
            logger.log(Level.WARNING, "Bad location", ble);
            return;
        }
View Full Code Here

     * new Document is swapped in, uninstall our DocumentFilter from the old
     * Document and install it on the new.
     */
    private class DocumentWatcher implements PropertyChangeListener {
        public void propertyChange(PropertyChangeEvent evt) {
            final Document newDocument = (Document) evt.getNewValue();

            if (!(newDocument instanceof AbstractDocument))
                throwIllegalStateException("The Document behind the JTextField was changed to no longer be an AbstractDocument. It was changed to: " + newDocument);

            // remove our DocumentFilter from the old document
View Full Code Here

        addKeyBinding("CONTROL_K", KeyStroke.getKeyStroke(KeyEvent.VK_K, Event.CTRL_MASK), new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                if(!textComponent.isWritable()) return;

                int start = textComponent.getCaretPosition();
                Document doc = textComponent.getDocument();
                String t;
                try {
                    t = doc.getText(start, doc.getLength()-start);
                } catch (BadLocationException e1) {
                    e1.printStackTrace();
                    return;
                }

                int end = 0;
                while(end<t.length() && t.charAt(end) != '\n' && t.charAt(end) != '\r') {
                    end++;
                }

                try {
                    end = Math.max(1, end);

                    String content = doc.getText(start, end);
                    doc.remove(start, end);

                    // Copy the deleted portion of text into the system clipboard
                    Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
                    cb.setContents(new StringSelection(content), null);
                } catch (BadLocationException e1) {
                    e1.printStackTrace();
                }
            }
        });

        // Ctrl-t swap the two characters before and after the current position
        // Has to create a custom action to handle this one.
        addKeyBinding("CONTROL_T", KeyStroke.getKeyStroke(KeyEvent.VK_T, Event.CTRL_MASK), new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                if(!textComponent.isWritable()) return;

                int p = textComponent.getCaretPosition();
                Document doc = textComponent.getDocument();

                if(p < 1 || p >= doc.getLength())
                    return;

                try {
                    String before = doc.getText(p-1, 1);
                    doc.remove(p-1, 1);
                    doc.insertString(p, before, null);
                    textComponent.setCaretPosition(p);
                } catch (BadLocationException e1) {
                    e1.printStackTrace();
                }
            }
View Full Code Here

        return new DocumentAdapter() {
            protected void textChanged(DocumentEvent e) {
                GenericConnectionDatabaseSettings connectionConfig = getConfiguration();
                connectionConfig.setModified(true);

                Document document = e.getDocument();

                if (document == driverLibraryTextField.getTextField().getDocument()) {
                    updateLibraryTextField();
                }
View Full Code Here

        try {
            isr = new FileInputStream(pathToFile);
            String fileContent = "";
            String fileContentForLanguageDetermination = "";
            RTFEditorKit RTFEditorKit = new RTFEditorKit();
            Document RTFdoc = RTFEditorKit.createDefaultDocument();
            String lang = "";

            RTFEditorKit.read(isr, RTFdoc, 0);
            fileContent = RTFdoc.getText(0, RTFdoc.getLength()).trim();

            fileContentForLanguageDetermination =
                    new String(fileContent.getBytes("ISO-8859-1"), "cp1251");

            if (fileContentForLanguageDetermination.length() < 1000) {
View Full Code Here

   
    // gets the signal name that the caret is sitting on
    public String getSignalNameAtCaret(JTextComponent tc) throws BadLocationException {
        int caret = tc.getCaretPosition();
        int start = caret;
        Document doc = tc.getDocument();
        while (start > 0) {
            char ch = doc.getText(start-1, 1).charAt(0);
            if (isSignalNameChar(ch)) {
                start--;
            }
            else {
                break;
            }
          }
        int end = caret;
        while (end < doc.getLength()) {
            char ch = doc.getText(end, 1).charAt(0);
            if (isSignalNameChar(ch)) {
                end++;
            }
            else {
                break;
            }
        }
        return doc.getText(start, end-start);
    }
View Full Code Here

        add(jPanel3, java.awt.BorderLayout.SOUTH);
    }// </editor-fold>//GEN-END:initComponents

    private void modelListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_modelListValueChanged
        ImportedModel selection = (ImportedModel) modelList.getSelectedValue();
        Document doc = warningTextArea.getDocument();
        try {
            doc.remove(0, doc.getLength());
        } catch (BadLocationException ex) {
            Logger.getLogger(LoaderWarningsPanel.class.getName()).log(Level.SEVERE, null, ex);
        }

        if (selection!=null) {
View Full Code Here

    }//GEN-LAST:event_modelListValueChanged

    private void clearAllBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clearAllBActionPerformed
        ((DefaultListModel)modelList.getModel()).clear();

        Document doc = warningTextArea.getDocument();
        try {
            doc.remove(0, doc.getLength());
        } catch (BadLocationException ex) {
            Logger.getLogger(LoaderWarningsPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
        handler.clearAll();
    }//GEN-LAST:event_clearAllBActionPerformed
View Full Code Here

            return false;

        String sourceRuleText = window.getText().substring(sourceRule.getStartIndex(), sourceRule.getEndIndex()+1);

        try {
            Document doc = window.getTextPane().getDocument();

            int removeStartIndex = sourceRule.getStartIndex();
            int targetInsertionIndex = dropAbove ? targetRule.getStartIndex() : targetRule.getEndIndex();

            // should move at the line after the rule (a comment can be located
            // after the end of the rule but still on the same line)

            // Remove one more character to remove the end of line of the rule
            int removeLength = sourceRule.getLength()+1;
            if(removeStartIndex+removeLength > doc.getLength())
                removeLength--;

            if(sourceRule.getStartIndex()>targetRule.getStartIndex()) {
                doc.remove(removeStartIndex, removeLength);
                doc.insertString(targetInsertionIndex, "\n"+sourceRuleText, null);
                window.setCaretPosition(targetInsertionIndex);
            } else {
                doc.insertString(targetInsertionIndex, "\n"+sourceRuleText, null);
                doc.remove(removeStartIndex, removeLength);
                window.setCaretPosition(targetInsertionIndex-removeLength);
            }
            return true;
        } catch (BadLocationException e) {
            window.consoleTab.println(e);
View Full Code Here

TOP

Related Classes of javax.swing.text.Document

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.