Java Code Examples for javax.swing.text.Document#addDocumentListener()

The following examples show how to use javax.swing.text.Document#addDocumentListener() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ElementTreePanel.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Invoked when a property changes. We are only interested in when the
 * Document changes to reset the DocumentListener.
 */
public void propertyChange(PropertyChangeEvent e) {
    if (e.getSource() == getEditor() && e.getPropertyName().equals(
            "document")) {
        Document oldDoc = (Document) e.getOldValue();
        Document newDoc = (Document) e.getNewValue();

        // Reset the DocumentListener
        oldDoc.removeDocumentListener(this);
        newDoc.addDocumentListener(this);

        // Recreate the TreeModel.
        treeModel = new ElementTreeModel(newDoc);
        tree.setModel(treeModel);
    }
}
 
Example 2
Source File: LWTextComponentPeer.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
@Override
public final void setText(final String text) {
    synchronized (getDelegateLock()) {
        // JTextArea.setText() posts two different events (remove & insert).
        // Since we make no differences between text events,
        // the document listener has to be disabled while
        // JTextArea.setText() is called.
        final Document document = getTextComponent().getDocument();
        document.removeDocumentListener(this);
        getTextComponent().setText(text);
        revalidate();
        if (firstChangeSkipped) {
            postEvent(new TextEvent(getTarget(),
                                    TextEvent.TEXT_VALUE_CHANGED));
        }
        document.addDocumentListener(this);
    }
    repaintPeer();
}
 
Example 3
Source File: LWTextComponentPeer.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public final void setText(final String text) {
    synchronized (getDelegateLock()) {
        // JTextArea.setText() posts two different events (remove & insert).
        // Since we make no differences between text events,
        // the document listener has to be disabled while
        // JTextArea.setText() is called.
        final Document document = getTextComponent().getDocument();
        document.removeDocumentListener(this);
        getTextComponent().setText(text);
        revalidate();
        if (firstChangeSkipped) {
            postEvent(new TextEvent(getTarget(),
                                    TextEvent.TEXT_VALUE_CHANGED));
        }
        document.addDocumentListener(this);
    }
    repaintPeer();
}
 
Example 4
Source File: LWTextAreaPeer.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void replaceRange(final String text, final int start,
                         final int end) {
    synchronized (getDelegateLock()) {
        // JTextArea.replaceRange() posts two different events.
        // Since we make no differences between text events,
        // the document listener has to be disabled while
        // JTextArea.replaceRange() is called.
        final Document document = getTextComponent().getDocument();
        document.removeDocumentListener(this);
        getDelegate().getView().replaceRange(text, start, end);
        revalidate();
        postEvent(new TextEvent(getTarget(), TextEvent.TEXT_VALUE_CHANGED));
        document.addDocumentListener(this);
    }
    repaintPeer();
}
 
Example 5
Source File: WeakPositions.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Gets a <code>Position</code> in the document at the given offset. The 
 * position is automatically updated when the document's contents is modified.
 * The <code>Position</code> does not reference the document in anyway that
 * would prevent the document from being garbage collected.
 * 
 * @param doc The document to get a position in.
 * @param offset The initial offset of the position.
 * 
 * @return A <code>Position</code> inside the document.
 * @throws BadLocationException If the offset is not valid for the document.
 */
public static Position get(Document doc, int offset) throws BadLocationException {
    // Check that the offset is valid. This should excercise any rule imposed by
    // the document on its positions.
    doc.createPosition(offset);
    
    synchronized (OGLS) {
        OffsetGapList<WeakP> ogl = OGLS.get(doc);

        if (ogl == null) {
            ogl = new OffsetGapList<WeakPositions.WeakP>();
            OGLS.put(doc, ogl);
            doc.addDocumentListener(WeakListeners.document(documentsTracker, doc));
        }
        
        int index = ogl.findElementIndex(offset);
        WeakP pos = index >= 0 ? ogl.get(index) : null;

        if (pos == null) {
            pos = new WeakP(offset);
            ogl.add(pos);
        }
        
        return pos;
    }
}
 
Example 6
Source File: LWTextComponentPeer.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public final void setText(final String text) {
    synchronized (getDelegateLock()) {
        // JTextArea.setText() posts two different events (remove & insert).
        // Since we make no differences between text events,
        // the document listener has to be disabled while
        // JTextArea.setText() is called.
        final Document document = getTextComponent().getDocument();
        document.removeDocumentListener(this);
        getTextComponent().setText(text);
        revalidate();
        if (firstChangeSkipped) {
            postEvent(new TextEvent(getTarget(),
                                    TextEvent.TEXT_VALUE_CHANGED));
        }
        document.addDocumentListener(this);
    }
    repaintPeer();
}
 
Example 7
Source File: LWTextAreaPeer.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void replaceRange(final String text, final int start,
                         final int end) {
    synchronized (getDelegateLock()) {
        // JTextArea.replaceRange() posts two different events.
        // Since we make no differences between text events,
        // the document listener has to be disabled while
        // JTextArea.replaceRange() is called.
        final Document document = getTextComponent().getDocument();
        document.removeDocumentListener(this);
        getDelegate().getView().replaceRange(text, start, end);
        revalidate();
        postEvent(new TextEvent(getTarget(), TextEvent.TEXT_VALUE_CHANGED));
        document.addDocumentListener(this);
    }
    repaintPeer();
}
 
Example 8
Source File: ElementTreePanel.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Invoked when a property changes. We are only interested in when the
 * Document changes to reset the DocumentListener.
 */
public void propertyChange(PropertyChangeEvent e) {
    if (e.getSource() == getEditor() && e.getPropertyName().equals(
            "document")) {
        Document oldDoc = (Document) e.getOldValue();
        Document newDoc = (Document) e.getNewValue();

        // Reset the DocumentListener
        oldDoc.removeDocumentListener(this);
        newDoc.addDocumentListener(this);

        // Recreate the TreeModel.
        treeModel = new ElementTreeModel(newDoc);
        tree.setModel(treeModel);
    }
}
 
Example 9
Source File: LWTextComponentPeer.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
@Override
public final void setText(final String text) {
    synchronized (getDelegateLock()) {
        // JTextArea.setText() posts two different events (remove & insert).
        // Since we make no differences between text events,
        // the document listener has to be disabled while
        // JTextArea.setText() is called.
        final Document document = getTextComponent().getDocument();
        document.removeDocumentListener(this);
        getTextComponent().setText(text);
        revalidate();
        if (firstChangeSkipped) {
            postEvent(new TextEvent(getTarget(),
                                    TextEvent.TEXT_VALUE_CHANGED));
        }
        document.addDocumentListener(this);
    }
    repaintPeer();
}
 
Example 10
Source File: ElementTreePanel.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Invoked when a property changes. We are only interested in when the
 * Document changes to reset the DocumentListener.
 */
public void propertyChange(PropertyChangeEvent e) {
    if (e.getSource() == getEditor() && e.getPropertyName().equals(
            "document")) {
        Document oldDoc = (Document) e.getOldValue();
        Document newDoc = (Document) e.getNewValue();

        // Reset the DocumentListener
        oldDoc.removeDocumentListener(this);
        newDoc.addDocumentListener(this);

        // Recreate the TreeModel.
        treeModel = new ElementTreeModel(newDoc);
        tree.setModel(treeModel);
    }
}
 
Example 11
Source File: JLabelTextComponent.java    From blog with Apache License 2.0 5 votes vote down vote up
public final void setDocument(Document document) {
	if (this.document != null) {
		this.document.removeDocumentListener(documentListenerAdapter);
	}
	this.document = document;
	if (document != null) {
		document.addDocumentListener(documentListenerAdapter);
	}
}
 
Example 12
Source File: ElementTreePanel.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Resets the JTextComponent to <code>editor</code>. This will update
 * the tree accordingly.
 */
public void setEditor(JTextComponent editor) {
    if (this.editor == editor) {
        return;
    }

    if (this.editor != null) {
        Document oldDoc = this.editor.getDocument();

        oldDoc.removeDocumentListener(this);
        this.editor.removePropertyChangeListener(this);
        this.editor.removeCaretListener(this);
    }
    this.editor = editor;
    if (editor == null) {
        treeModel = null;
        tree.setModel(null);
    } else {
        Document newDoc = editor.getDocument();

        newDoc.addDocumentListener(this);
        editor.addPropertyChangeListener(this);
        editor.addCaretListener(this);
        treeModel = new ElementTreeModel(newDoc);
        tree.setModel(treeModel);
    }
}
 
Example 13
Source File: FixedWidthTextArea.java    From pumpernickel with MIT License 5 votes vote down vote up
@Override
public void setDocument(Document d) {
	Document oldDocument = getDocument();
	if (oldDocument != null)
		oldDocument.removeDocumentListener(docListener);
	super.setDocument(d);
	d.addDocumentListener(docListener);
	cachedSize = null;
}
 
Example 14
Source File: ElementTreePanel.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Resets the JTextComponent to <code>editor</code>. This will update
 * the tree accordingly.
 */
public void setEditor(JTextComponent editor) {
    if (this.editor == editor) {
        return;
    }

    if (this.editor != null) {
        Document oldDoc = this.editor.getDocument();

        oldDoc.removeDocumentListener(this);
        this.editor.removePropertyChangeListener(this);
        this.editor.removeCaretListener(this);
    }
    this.editor = editor;
    if (editor == null) {
        treeModel = null;
        tree.setModel(null);
    } else {
        Document newDoc = editor.getDocument();

        newDoc.addDocumentListener(this);
        editor.addPropertyChangeListener(this);
        editor.addCaretListener(this);
        treeModel = new ElementTreeModel(newDoc);
        tree.setModel(treeModel);
    }
}
 
Example 15
Source File: ElementTreePanel.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Resets the JTextComponent to <code>editor</code>. This will update
 * the tree accordingly.
 */
public void setEditor(JTextComponent editor) {
    if (this.editor == editor) {
        return;
    }

    if (this.editor != null) {
        Document oldDoc = this.editor.getDocument();

        oldDoc.removeDocumentListener(this);
        this.editor.removePropertyChangeListener(this);
        this.editor.removeCaretListener(this);
    }
    this.editor = editor;
    if (editor == null) {
        treeModel = null;
        tree.setModel(null);
    } else {
        Document newDoc = editor.getDocument();

        newDoc.addDocumentListener(this);
        editor.addPropertyChangeListener(this);
        editor.addCaretListener(this);
        treeModel = new ElementTreeModel(newDoc);
        tree.setModel(treeModel);
    }
}
 
Example 16
Source File: ElementTreePanel.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Resets the JTextComponent to <code>editor</code>. This will update
 * the tree accordingly.
 */
public void setEditor(JTextComponent editor) {
    if (this.editor == editor) {
        return;
    }

    if (this.editor != null) {
        Document oldDoc = this.editor.getDocument();

        oldDoc.removeDocumentListener(this);
        this.editor.removePropertyChangeListener(this);
        this.editor.removeCaretListener(this);
    }
    this.editor = editor;
    if (editor == null) {
        treeModel = null;
        tree.setModel(null);
    } else {
        Document newDoc = editor.getDocument();

        newDoc.addDocumentListener(this);
        editor.addPropertyChangeListener(this);
        editor.addCaretListener(this);
        treeModel = new ElementTreeModel(newDoc);
        tree.setModel(treeModel);
    }
}
 
Example 17
Source File: FileSearchAction.java    From netbeans with Apache License 2.0 4 votes vote down vote up
FileDescriptorConvertor(@NonNull final Document doc) {
    doc.addDocumentListener(this);
}
 
Example 18
Source File: WholeNumberField.java    From beast-mcmc with GNU Lesser General Public License v2.1 4 votes vote down vote up
protected Document createDefaultModel() {
    Document doc = new WholeNumberFieldDocument();
    doc.addDocumentListener(this);
    return doc;
}
 
Example 19
Source File: RealNumberField.java    From beast-mcmc with GNU Lesser General Public License v2.1 4 votes vote down vote up
protected Document createDefaultModel() {
    Document doc = new RealNumberField.RealNumberFieldDocument();
    doc.addDocumentListener(this);
    return doc;
}
 
Example 20
Source File: DocumentUtilities.java    From netbeans with Apache License 2.0 2 votes vote down vote up
/**
 * Add document listener to document with given priority
 * or default to using regular {@link Document#addDocumentListener(DocumentListener)}
 * if the given document is not listener priority aware.
 * 
 * @param doc document to which the listener should be added.
 * @param listener document listener to add.
 * @param priority priority with which the listener should be added.
 *  If the document does not support document listeners ordering
 *  then the listener is added in a regular way by using
 *  {@link javax.swing.text.Document#addDocumentListener(
 *  javax.swing.event.DocumentListener)} method.
 */
public static void addDocumentListener(Document doc, DocumentListener listener,
DocumentListenerPriority priority) {
    if (!addPriorityDocumentListener(doc, listener, priority))
        doc.addDocumentListener(listener);
}