def replace(self, offset, length, text, attrs): if length == 0: super(BeautifierPanel.CustomUndoPlainDocument, self).replace(offset, length, text, attrs) else: self.compoundEdit = CompoundEdit() super(BeautifierPanel.CustomUndoPlainDocument, self).fireUndoableEditUpdate( UndoableEditEvent(self, self.compoundEdit)) super(BeautifierPanel.CustomUndoPlainDocument, self).replace(offset, length, text, attrs) self.compoundEdit.end() self.compoundEdit = None
def force_start_compound(self): """ Wraps list of interactions with the text area that'll cause several significant edit events that we want to put together in a compound edit. """ empty_compound = CompoundEdit() if not self.must_compound: self.must_compound = True if not self.current_compound.equals(empty_compound): self.current_compound.end() self.undo_manager.addEdit(self.current_compound) self.current_compound = CompoundEdit()
def undoableEditHappened(self, event): edit = event.getEdit() edit_type = str(edit.getType()) # If significant INSERT/REMOVE event happen, end and add current # edit compound to undo_manager and start a new one. if ((edit_type == "INSERT" or edit_type == "REMOVE") and not self.must_compound): # Explicitly end compound edits so their inProgress flag goes # to false. Note undo() only undoes compound edits when they # are not in progress. self.current_compound.end() self.current_compound = CompoundEdit() self.undo_manager.addEdit(self.current_compound) # Always add current edit to current compound self.current_compound.addEdit(edit)
class AtfUndoableEditListener(UndoableEditListener): ''' Overrides the undoableEditHappened functionality to group INSERT/REMOVE edit events with their associated CHANGE events (these correspond to highlighting only at the moment). TODO: Make compounds save whole words so undoing is not so much of a pain for the user. ''' def __init__(self, undo_manager): self.undo_manager = undo_manager self.current_compound = CompoundEdit() self.must_compound = False def force_start_compound(self): """ Wraps list of interactions with the text area that'll cause several significant edit events that we want to put together in a compound edit. """ empty_compound = CompoundEdit() if not self.must_compound: self.must_compound = True if not self.current_compound.equals(empty_compound): self.current_compound.end() self.undo_manager.addEdit(self.current_compound) self.current_compound = CompoundEdit() def force_stop_compound(self): self.current_compound.end() self.undo_manager.addEdit(self.current_compound) self.must_compound = False def undoableEditHappened(self, event): edit = event.getEdit() edit_type = str(edit.getType()) # If significant INSERT/REMOVE event happen, end and add current # edit compound to undo_manager and start a new one. if ((edit_type == "INSERT" or edit_type == "REMOVE") and not self.must_compound): # Explicitly end compound edits so their inProgress flag goes # to false. Note undo() only undoes compound edits when they # are not in progress. self.current_compound.end() self.current_compound = CompoundEdit() self.undo_manager.addEdit(self.current_compound) # Always add current edit to current compound self.current_compound.addEdit(edit)
class CustomUndoPlainDocument(PlainDocument): # Code from: https://stackoverflow.com/questions/24433089/jtextarea-settext-undomanager compoundEdit = CompoundEdit() def fireUndoableEditUpdate(self, e): if self.compoundEdit == None: super(BeautifierPanel.CustomUndoPlainDocument, self).fireUndoableEditUpdate(e) else: self.compoundEdit.addEdit(e.getEdit()) def replace(self, offset, length, text, attrs): if length == 0: super(BeautifierPanel.CustomUndoPlainDocument, self).replace(offset, length, text, attrs) else: self.compoundEdit = CompoundEdit() super(BeautifierPanel.CustomUndoPlainDocument, self).fireUndoableEditUpdate( UndoableEditEvent(self, self.compoundEdit)) super(BeautifierPanel.CustomUndoPlainDocument, self).replace(offset, length, text, attrs) self.compoundEdit.end() self.compoundEdit = None
def __init__(self, undo_manager): self.undo_manager = undo_manager self.current_compound = CompoundEdit() self.must_compound = False