下面列出了javax.swing.event.DocumentEvent#getLength() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public synchronized void insertUpdate(DocumentEvent e) {
if (inSync)
return ;
//check for modifications outside the first region:
if (e.getOffset() < region.getFirstRegionStartOffset() || (e.getOffset() + e.getLength()) > region.getFirstRegionEndOffset()) {
release(true);
return;
}
inSync = true;
region.sync();
span = region.getFirstRegionLength();
ui.updateInput(getRegionText(0));
inSync = false;
requestRepaint();
}
@Override
public void insertUpdate(DocumentEvent evt) {
documentChanged(evt);
int offset = evt.getOffset();
int length = evt.getLength();
int newStart;
int newEnd;
if (selectionStart > offset || (selectionStart == selectionEnd && selectionStart == offset)) {
newStart = selectionStart + length;
} else {
newStart = selectionStart;
}
if (selectionEnd >= offset) {
newEnd = selectionEnd + length;
} else {
newEnd = selectionEnd;
}
select(newStart, newEnd);
}
public void insertUpdate(DocumentEvent e) {
int changeStart = e.getOffset();
int changeEnd = e.getOffset() + e.getLength();
if (isAffectedByChange(changeStart, changeEnd)) {
fireHighlightsChange(changeStart, changeEnd);
}
}
public void changedUpdate(DocumentEvent e, Shape a, ViewFactory f) {
super.changedUpdate(e, a, f);
int pos = e.getOffset();
if (pos <= getStartOffset() && (pos + e.getLength()) >=
getEndOffset()) {
RowView.this.setPropertiesFromAttributes();
}
}
public void changedUpdate(DocumentEvent changes, Shape a, ViewFactory f) {
super.changedUpdate(changes, a, f);
int pos = changes.getOffset();
if (pos <= getStartOffset() && (pos + changes.getLength()) >=
getEndOffset()) {
setPropertiesFromAttributes();
}
}
public void changedUpdate(DocumentEvent e, Shape a, ViewFactory f) {
super.changedUpdate(e, a, f);
int pos = e.getOffset();
if (pos <= getStartOffset() && (pos + e.getLength()) >=
getEndOffset()) {
RowView.this.setPropertiesFromAttributes();
}
}
public void changedUpdate(DocumentEvent e, Shape a, ViewFactory f) {
super.changedUpdate(e, a, f);
int pos = e.getOffset();
if (pos <= getStartOffset() && (pos + e.getLength()) >=
getEndOffset()) {
RowView.this.setPropertiesFromAttributes();
}
}
private void documentChanged(DocumentEvent documentEvent) {
if(!documentDirty) {
fireSourceChanged();
}
//indicate that the synchronization between document content and the element may be broken
//not used by the model logic itself but by the "resortElements" method.
documentDirty = true;
try {
//test whether a new text was inserted before or after the root element boundaries (positions)
if(getRootElement().getStartOffset() > 0 || getRootElement().getEndOffset() < getDocument().getLength()) {
getRootElement().setStartPosition(0);
getRootElement().setEndPosition(getDocument().getLength());
}
//TODO: here we have to decide whether the document change affects
//the model and how.
int change_offset = documentEvent.getOffset();
int change_length = documentEvent.getLength();
int type = documentEvent.getType().equals(EventType.REMOVE) ? DocumentChange.REMOVE : DocumentChange.INSERT;
DocumentChange dchi = new DocumentChange(getDocument().createPosition(change_offset), change_length, type);
documentChanges.add(dchi);
if(debug) System.out.println(dchi);
}catch(BadLocationException e) {
e.printStackTrace();
}
requestModelUpdate(false);
}
public void changedUpdate(DocumentEvent e, Shape a, ViewFactory f) {
super.changedUpdate(e, a, f);
int pos = e.getOffset();
if (pos <= getStartOffset() && (pos + e.getLength()) >=
getEndOffset()) {
RowView.this.setPropertiesFromAttributes();
}
}
public void changedUpdate(DocumentEvent changes, Shape a, ViewFactory f) {
super.changedUpdate(changes, a, f);
int pos = changes.getOffset();
if (pos <= getStartOffset() && (pos + changes.getLength()) >=
getEndOffset()) {
setPropertiesFromAttributes();
}
}
public void changedUpdate(DocumentEvent changes, Shape a, ViewFactory f) {
super.changedUpdate(changes, a, f);
int pos = changes.getOffset();
if (pos <= getStartOffset() && (pos + changes.getLength()) >=
getEndOffset()) {
setPropertiesFromAttributes();
}
}
public void insertUpdate(DocumentEvent e) {
int offset = e.getOffset();
int length = e.getLength();
int newDot = dot;
short changed = 0;
if (newDot >= offset) {
newDot += length;
changed |= 1;
}
int newMark = mark;
if (newMark >= offset) {
newMark += length;
changed |= 2;
}
if (changed != 0) {
if (newMark == newDot) {
setDot(newDot);
ensureValidPosition();
} else {
setDot(newMark);
if (getDot() == newMark) {
moveDot(newDot);
}
ensureValidPosition();
}
}
}
public final void insertUpdate(DocumentEvent e) {
//Ensure it is consumed
e.getLength();
documentChanged();
if (e.getOffset() + e.getLength() >= getCaretPos() && (locked || !(e instanceof OutputDocument.DO))) {
//#119985 only move caret when not in editable section
OutputDocument doc = (OutputDocument)e.getDocument();
if (! (e instanceof OutputDocument.DO) && getCaretPos() >= doc.getOutputLength()) {
return ;
}
getCaret().setDot(e.getOffset() + e.getLength());
}
}
public void changedUpdate(DocumentEvent changes, Shape a, ViewFactory f) {
super.changedUpdate(changes, a, f);
int pos = changes.getOffset();
if (pos <= getStartOffset() && (pos + changes.getLength()) >=
getEndOffset()) {
setPropertiesFromAttributes();
}
}
public void changedUpdate(DocumentEvent changes, Shape a, ViewFactory f) {
super.changedUpdate(changes, a, f);
int pos = changes.getOffset();
if (pos <= getStartOffset() && (pos + changes.getLength()) >=
getEndOffset()) {
setPropertiesFromAttributes();
}
}
public void changedUpdate(DocumentEvent e, Shape a, ViewFactory f) {
super.changedUpdate(e, a, f);
int pos = e.getOffset();
if (pos <= getStartOffset() && (pos + e.getLength()) >=
getEndOffset()) {
RowView.this.setPropertiesFromAttributes();
}
}
public void insertUpdate(DocumentEvent de) {
lastUpdate = de.getOffset() + de.getLength();
}
public void insertUpdate(DocumentEvent evt, UndoableEdit edit, AttributeSet attr) {
int insertOffset = evt.getOffset();
int insertEndOffset = insertOffset + evt.getLength();
CharSequence text = DocumentUtilities.getText(doc);
if (insertOffset > 0) { // [Swing] marks (and elements) at offset zero do not move up
insertOffset--;
}
try {
int index = -1; // Index of the elements modification - computed lazily
List<LineElement> addedLines = null; // Collected added lines
LineElement removedLine = null; // Removed line element
Position lastAddedLineEndPos = null;
for (int offset = insertOffset; offset < insertEndOffset; offset++) {
if (text.charAt(offset) == '\n') {
if (index == -1) { // Not computed yet
index = getElementIndex(offset);
removedLine = (LineElement)getElement(index);
lastAddedLineEndPos = removedLine.getStartPosition();
addedLines = new ArrayList<LineElement>(2);
}
Position lineEndPos = doc.createPosition(offset + 1);
addedLines.add(new LineElement(this, lastAddedLineEndPos, lineEndPos));
lastAddedLineEndPos = lineEndPos;
}
}
if (index != -1) { // Some lines were added
// If the text was inserted at the line boundary i.e. right after existing '\n'
// and the ending char of the inserted text was not '\n' (otherwise
// it would be a "clean" line insert) then there must be two line elements
// removed.
Position removedLineEndPos = removedLine.getEndPosition();
int removedLineEndOffset = removedLineEndPos.getOffset();
Element[] removed; // removed line elements
int lastAddedLineEndOffset = lastAddedLineEndPos.getOffset();
if (insertEndOffset == removedLineEndOffset
&& lastAddedLineEndOffset != removedLineEndOffset
// && index + 1 < getElementCount()
) {
LineElement removedLine2 = (LineElement)getElement(index + 1);
removed = new Element[] { removedLine, removedLine2 };
removedLineEndPos = removedLine2.getEndPosition();
removedLineEndOffset = removedLineEndPos.getOffset();
} else { // just one line removed
removed = new Element[] { removedLine };
}
if (lastAddedLineEndOffset < removedLineEndOffset) {
addedLines.add(new LineElement(this, lastAddedLineEndPos, removedLineEndPos));
}
Element[] added = new Element[addedLines.size()];
addedLines.toArray(added);
edit.addEdit(new Edit(index, removed, added));
replace(index, removed.length, added);
}
} catch (BadLocationException e) {
throw new IllegalStateException(e.toString());
}
}
@Override
public void removeUpdate(DocumentEvent ev) {
if(mode == PROGRAMMATIC || mode == INSERT) {
return;
}
int pos = ev.getOffset();
if(ev.getLength() != 1 || (pos < start || pos > end)) {
// cancel any autocompletion if the user cut some text
// or delete outside brackets when in POPUP mode
cleanup();
return;
}
if(mode == POPUP_TYPES) {
end = pos;
String type = getText().substring(start, end);
if(!type.matches("[a-zA-Z0-9]+")) {
return;
}
for(int i = 0; i < queryList.getModel().getSize(); i++) {
if(startsWithIgnoreCase(
queryList.getModel().getElementAt(i), type)) {
queryPopupWindow.setVisible(true);
queryList.setSelectedIndex((i));
queryList.ensureIndexIsVisible(i);
break;
}
}
} else if(mode == POPUP_FEATURES) {
end = pos;
String feature = getText().substring(start, end);
if(!feature.matches("[a-zA-Z0-9]+")) {
return;
}
for(int i = 0; i < queryList.getModel().getSize(); i++) {
if(startsWithIgnoreCase(
queryList.getModel().getElementAt(i), feature)) {
queryPopupWindow.setVisible(true);
queryList.setSelectedIndex((i));
queryList.ensureIndexIsVisible(i);
break;
}
}
}
}
public void insertUpdate(DocumentEvent ev) {
if (ev.getLength() != 1) {
return;
}
int pos = ev.getOffset();
String content = null;
try {
content = textArea.getText(0, pos + 1);
} catch (BadLocationException e) {
e.printStackTrace();
}
// Find where the word starts
int w;
for (w = pos; w >= 0; w--) {
if (!Character.isLetter(content.charAt(w))) {
break;
}
}
if (pos - w < 2) {
// Too few chars
return;
}
String prefix = content.substring(w + 1).toLowerCase();
int n = Collections.binarySearch(words, prefix);
if (n < 0 && -n <= words.size()) {
String match = words.get(-n - 1);
if (match.startsWith(prefix)) {
// A completion is found
String completion = match.substring(pos - w);
// We cannot modify Document from within notification,
// so we submit a task that does the change later
SwingUtilities.invokeLater(new CompletionTask(completion, pos + 1));
}
} else {
// Nothing found
mode = Mode.INSERT;
}
}