use of javax.swing.undo.UndoableEdit in project groovy by apache.
the class TextUndoManager method undo.
public void undo() throws javax.swing.undo.CannotUndoException {
compoundEdit.end();
UndoableEdit edit = editToBeUndone();
if (((StructuredEdit) editToBeUndone()).editedTime() == firstModified) {
firstModified = 0;
} else if (firstModified == 0) {
firstModified = ((StructuredEdit) editToBeUndone()).editedTime();
}
boolean redoable = canRedo();
boolean changed = hasChanged();
super.undo();
firePropertyChangeEvent(UndoManager.RedoName, redoable, canRedo());
}
use of javax.swing.undo.UndoableEdit in project groovy by apache.
the class TextUndoManager method undoableEditHappened.
public void undoableEditHappened(UndoableEditEvent uee) {
UndoableEdit edit = uee.getEdit();
boolean undoable = canUndo();
long editTime = System.currentTimeMillis();
if (firstModified == 0 || editTime - compoundEdit.editedTime() > 700) {
compoundEdit.end();
compoundEdit = new StructuredEdit();
}
compoundEdit.addEdit(edit);
firstModified = firstModified == 0 ? compoundEdit.editedTime() : firstModified;
if (lastEdit() != compoundEdit) {
boolean changed = hasChanged();
addEdit(compoundEdit);
firePropertyChangeEvent(UndoManager.UndoName, undoable, canUndo());
}
}
use of javax.swing.undo.UndoableEdit in project jdk8u_jdk by JetBrains.
the class DefaultStyledDocument method removeElementImpl.
private void removeElementImpl(Element elem) {
if (elem.getDocument() != this) {
throw new IllegalArgumentException("element doesn't belong to document");
}
BranchElement parent = (BranchElement) elem.getParentElement();
if (parent == null) {
throw new IllegalArgumentException("can't remove the root element");
}
int startOffset = elem.getStartOffset();
int removeFrom = startOffset;
int endOffset = elem.getEndOffset();
int removeTo = endOffset;
int lastEndOffset = getLength() + 1;
Content content = getContent();
boolean atEnd = false;
boolean isComposedText = Utilities.isComposedTextElement(elem);
if (endOffset >= lastEndOffset) {
// element includes the last "\n" character, needs special handling
if (startOffset <= 0) {
throw new IllegalArgumentException("can't remove the whole content");
}
// last "\n" must not be removed
removeTo = lastEndOffset - 1;
try {
if (content.getString(startOffset - 1, 1).charAt(0) == '\n') {
// preceding leaf ends with "\n", remove it
removeFrom--;
}
} catch (BadLocationException ble) {
// can't happen
throw new IllegalStateException(ble);
}
atEnd = true;
}
int length = removeTo - removeFrom;
DefaultDocumentEvent dde = new DefaultDocumentEvent(removeFrom, length, DefaultDocumentEvent.EventType.REMOVE);
UndoableEdit ue = null;
// do not leave empty branch elements
while (parent.getElementCount() == 1) {
elem = parent;
parent = (BranchElement) parent.getParentElement();
if (parent == null) {
// shouldn't happen
throw new IllegalStateException("invalid element structure");
}
}
Element[] removed = { elem };
Element[] added = {};
int index = parent.getElementIndex(startOffset);
parent.replace(index, 1, added);
dde.addEdit(new ElementEdit(parent, index, removed, added));
if (length > 0) {
try {
ue = content.remove(removeFrom, length);
if (ue != null) {
dde.addEdit(ue);
}
} catch (BadLocationException ble) {
// can only happen if the element structure is severely broken
throw new IllegalStateException(ble);
}
lastEndOffset -= length;
}
if (atEnd) {
// preceding leaf element should be extended to cover orphaned "\n"
Element prevLeaf = parent.getElement(parent.getElementCount() - 1);
while ((prevLeaf != null) && !prevLeaf.isLeaf()) {
prevLeaf = prevLeaf.getElement(prevLeaf.getElementCount() - 1);
}
if (prevLeaf == null) {
// shouldn't happen
throw new IllegalStateException("invalid element structure");
}
int prevStartOffset = prevLeaf.getStartOffset();
BranchElement prevParent = (BranchElement) prevLeaf.getParentElement();
int prevIndex = prevParent.getElementIndex(prevStartOffset);
Element newElem;
newElem = createLeafElement(prevParent, prevLeaf.getAttributes(), prevStartOffset, lastEndOffset);
Element[] prevRemoved = { prevLeaf };
Element[] prevAdded = { newElem };
prevParent.replace(prevIndex, 1, prevAdded);
dde.addEdit(new ElementEdit(prevParent, prevIndex, prevRemoved, prevAdded));
}
postRemoveUpdate(dde);
dde.end();
fireRemoveUpdate(dde);
if (!(isComposedText && (ue != null))) {
// do not fire UndoabeEdit event for composed text edit (unsupported)
fireUndoableEditUpdate(new UndoableEditEvent(this, dde));
}
}
use of javax.swing.undo.UndoableEdit in project cayenne by apache.
the class CayenneUndoManager method redo.
@Override
public void redo() throws CannotRedoException {
UndoableEdit e = editToBeRedone();
if (e instanceof TextCompoundEdit) {
TextCompoundEdit edit = (TextCompoundEdit) e;
edit.watchCaretPosition();
super.redo();
edit.stopWatchingCaretPosition();
} else {
super.redo();
}
updateUI();
}
use of javax.swing.undo.UndoableEdit in project cayenne by apache.
the class CayenneUndoManager method undo.
@Override
public void undo() throws CannotUndoException {
UndoableEdit e = editToBeUndone();
if (e instanceof TextCompoundEdit) {
TextCompoundEdit edit = (TextCompoundEdit) e;
edit.watchCaretPosition();
super.undo();
edit.stopWatchingCaretPosition();
} else {
super.undo();
}
updateUI();
}
Aggregations