下面列出了javax.swing.text.Document#removeDocumentListener ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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();
}
@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();
}
@Override
public void propertyChange(PropertyChangeEvent evt) {
FileObject closedFile = null;
if (!EditorCookie.Observable.PROP_DOCUMENT.equals(evt.getPropertyName())) {
return;
}
synchronized (this) {
Document old = (Document)evt.getOldValue();
if (old != null && docWL != null) {
old.removeDocumentListener(docWL);
docWL = null;
}
Document nue = (Document)evt.getNewValue();
if (nue != null) {
nue.addDocumentListener(docWL = WeakListeners.document(this, nue));
} else {
closedFile = extractFileObject(old);
}
}
if (closedFile != null) {
invalidate(closedFile);
}
}
@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();
}
@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);
getTextComponent().replaceRange(text, start, end);
revalidate();
postEvent(new TextEvent(getTarget(), TextEvent.TEXT_VALUE_CHANGED));
document.addDocumentListener(this);
}
repaintPeer();
}
@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();
}
@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();
}
@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();
}
@Override
public void setLayout(final LayoutManager mgr) {
if (mgr instanceof DocumentListener) {
Document doc = getDocument();
if (doc != null) doc.removeDocumentListener((DocumentListener) mgr);
}
}
@Override
public void setLayout(final LayoutManager mgr) {
if (mgr instanceof DocumentListener) {
Document doc = getDocument();
if (doc != null) doc.removeDocumentListener((DocumentListener) mgr);
}
}
public void run() {
Document document = getDocument();
DocumentListener listener = document == null ? null :
(DocumentListener) document.getProperty(PROPERTY_MODIFICATION_LISTENER);
if (listener != null) {
document.removeDocumentListener(listener);
}
try {
reloadModel();
} finally {
if (listener != null) {
document.addDocumentListener(listener);
}
}
}
/**
* 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);
}
}
@Override
protected void setDocument (Document doc) {
if (doc == null) {
Document d = getDocument();
if (d != null) {
d.removeDocumentListener(this);
}
textView.setDocument (new PlainDocument());
return;
}
textView.setEditorKit(new OutputEditorKit(isWrapped(), textView,
editorKitListener));
super.setDocument(doc);
updateKeyBindings();
}
/**
* 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);
}
}
private void remove(Document document, EditHistory history, int offset, int length) throws Exception {
try {
document.addDocumentListener(history);
document.remove(offset, length);
} finally {
document.removeDocumentListener(history);
}
}
public void setSwingDocument(final Document document) {
final Document prevDocument = this.textArea.getDocument();
final DocumentListener listener = this.getDocumentListener();
if (prevDocument != null) {
prevDocument.removeDocumentListener(listener);
}
document.addDocumentListener(listener);
this.textArea.setDocument(document);
}
/**
* 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);
}
}
/** Resolving problems when editor was modified and closed
* (issue 57483)
*/
protected void notifyClosed() {
mvtc = null;
if (topComponentsListener != null) {
TopComponent.getRegistry().removePropertyChangeListener(topComponentsListener);
topComponentsListener = null;
}
Document document = getDocument();
if (document!=null) document.removeDocumentListener(docListener);
super.notifyClosed();
}
private void insert(Document document, EditHistory history, int offset, String string) throws Exception {
try {
document.addDocumentListener(history);
document.insertString(offset, string, null);
} finally {
document.removeDocumentListener(history);
}
}
/**
* Remove document listener that was previously added to the document
* with given priority or use default {@link Document#removeDocumentListener(DocumentListener)}
* if the given document is not listener priority aware.
*
* @param doc document from which the listener should be removed.
* @param listener document listener to remove.
* @param priority priority with which the listener should be removed.
* It should correspond to the priority with which the listener
* was added originally.
*/
public static void removeDocumentListener(Document doc, DocumentListener listener,
DocumentListenerPriority priority) {
if (!removePriorityDocumentListener(doc, listener, priority))
doc.removeDocumentListener(listener);
}