/jEdit/tags/jedit-4-0-pre5/org/gjt/sp/jedit/buffer/UndoManager.java
Java | 379 lines | 273 code | 47 blank | 59 comment | 63 complexity | 575fc29236447883a2fc163bfb03c279 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
- /*
- * UndoManager.java - Buffer undo manager
- * :tabSize=8:indentSize=8:noTabs=false:
- * :folding=explicit:collapseFolds=1:
- *
- * Copyright (C) 2001 Slava Pestov
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- */
- package org.gjt.sp.jedit.buffer;
- //{{{ Imports
- import java.util.Vector;
- import org.gjt.sp.jedit.textarea.JEditTextArea;
- import org.gjt.sp.jedit.Buffer;
- import org.gjt.sp.util.Log;
- //}}}
- public class UndoManager
- {
- //{{{ UndoManager constructor
- public UndoManager(Buffer buffer)
- {
- this.buffer = buffer;
- undos = new Vector(100);
- } //}}}
- //{{{ setLimit() method
- public void setLimit(int limit)
- {
- this.limit = limit;
- } //}}}
- //{{{ clear() method
- public void clear()
- {
- undos.removeAllElements();
- undoPos = undoCount = 0;
- } //}}}
- //{{{ undo() method
- public boolean undo(JEditTextArea textArea)
- {
- if(insideCompoundEdit())
- throw new InternalError("Unbalanced begin/endCompoundEdit()");
- if(undoPos == 0)
- return false;
- else
- {
- Edit edit = (Edit)undos.elementAt(--undoPos);
- int caret = edit.undo();
- if(caret != -1)
- textArea.setCaretPosition(caret);
- return true;
- }
- } //}}}
- //{{{ redo() method
- public boolean redo(JEditTextArea textArea)
- {
- if(insideCompoundEdit())
- throw new InternalError("Unbalanced begin/endCompoundEdit()");
- if(undoPos == undoCount)
- return false;
- else
- {
- Edit edit = (Edit)undos.elementAt(undoPos++);
- int caret = edit.redo();
- if(caret != -1)
- textArea.setCaretPosition(caret);
- return true;
- }
- } //}}}
- //{{{ beginCompoundEdit() method
- public void beginCompoundEdit()
- {
- if(compoundEditCount == 0)
- compoundEdit = new CompoundEdit();
- compoundEditCount++;
- } //}}}
- //{{{ endCompoundEdit() method
- public void endCompoundEdit()
- {
- if(compoundEditCount == 1)
- {
- if(compoundEdit.getSize() != 0)
- addEdit(compoundEdit);
- compoundEdit = null;
- }
- else if(compoundEditCount == 0)
- {
- Log.log(Log.WARNING,this,new Exception("Unbalanced begin/endCompoundEdit()"));
- return;
- }
- compoundEditCount--;
- } //}}}
- //{{{ insideCompoundEdit() method
- public boolean insideCompoundEdit()
- {
- return compoundEditCount != 0;
- } //}}}
- //{{{ contentInserted() method
- public void contentInserted(int offset, int length, String text, boolean clearDirty)
- {
- Edit toMerge = null;
- if(compoundEdit != null)
- {
- int size = compoundEdit.getSize();
- if(size != 0)
- toMerge = (Edit)compoundEdit.undos.elementAt(size - 1);
- }
- else
- {
- if(undoPos != 0)
- toMerge = (Edit)undos.elementAt(undoPos - 1);
- }
- if(!clearDirty && toMerge instanceof Insert)
- {
- Insert ins = (Insert)toMerge;
- if(ins.offset == offset)
- {
- ins.str = text.concat(ins.str);
- ins.length += length;
- return;
- }
- else if(ins.offset + ins.length == offset)
- {
- ins.str = ins.str.concat(text);
- ins.length += length;
- return;
- }
- }
- Insert ins = new Insert(offset,length,text,clearDirty);
- if(clearDirty)
- {
- if(clearDirtyEdit != null)
- clearDirtyEdit.clearDirty = false;
- clearDirtyEdit = ins;
- }
- if(compoundEdit != null)
- compoundEdit.addEdit(ins);
- else
- addEdit(ins);
- } //}}}
- //{{{ contentRemoved() method
- public void contentRemoved(int offset, int length, String text, boolean clearDirty)
- {
- Edit toMerge = null;
- if(compoundEdit != null)
- {
- int size = compoundEdit.getSize();
- if(size != 0)
- toMerge = (Edit)compoundEdit.undos.elementAt(size - 1);
- }
- else
- {
- if(undoPos != 0)
- toMerge = (Edit)undos.elementAt(undoPos - 1);
- }
- if(!clearDirty && toMerge instanceof Remove)
- {
- Remove rem = (Remove)toMerge;
- if(rem.offset == offset)
- {
- rem.str = rem.str.concat(text);
- rem.length += length;
- return;
- }
- else if(rem.offset + rem.length == offset)
- {
- rem.str = text.concat(rem.str);
- rem.length += length;
- return;
- }
- }
- Remove rem = new Remove(offset,length,text,clearDirty);
- if(clearDirty)
- {
- if(clearDirtyEdit != null)
- clearDirtyEdit.clearDirty = false;
- clearDirtyEdit = rem;
- }
- if(compoundEdit != null)
- compoundEdit.addEdit(rem);
- else
- addEdit(rem);
- } //}}}
- //{{{ bufferSaved() method
- public void bufferSaved()
- {
- if(clearDirtyEdit != null)
- {
- clearDirtyEdit.clearDirty = false;
- clearDirtyEdit = null;
- }
- } //}}}
- //{{{ Private members
- //{{{ Instance variables
- private Buffer buffer;
- private Vector undos;
- private int limit;
- private int undoPos;
- private int undoCount;
- private int compoundEditCount;
- private CompoundEdit compoundEdit;
- private Edit clearDirtyEdit;
- //}}}
- //{{{ addEdit() method
- private void addEdit(Edit edit)
- {
- undos.insertElementAt(edit,undoPos++);
- if(undos.size() > limit)
- {
- undos.removeElementAt(0);
- undoPos--;
- }
- undoCount = undoPos;
- } //}}}
- //}}}
- //{{{ Inner classes
- //{{{ Edit interface
- abstract class Edit
- {
- //{{{ undo() method
- abstract int undo();
- //}}}
- //{{{ redo() method
- abstract int redo();
- //}}}
- boolean clearDirty;
- } //}}}
- //{{{ Insert class
- class Insert extends Edit
- {
- //{{{ Insert constructor
- Insert(int offset, int length, String str, boolean clearDirty)
- {
- this.offset = offset;
- this.length = length;
- this.str = str;
- this.clearDirty = clearDirty;
- } //}}}
- //{{{ undo() method
- int undo()
- {
- buffer.remove(offset,length);
- if(clearDirty)
- buffer.setDirty(false);
- return offset;
- } //}}}
- //{{{ redo() method
- int redo()
- {
- buffer.insert(offset,str);
- return offset + length;
- } //}}}
- int offset;
- int length;
- String str;
- } //}}}
- //{{{ Remove class
- class Remove extends Edit
- {
- //{{{ Remove constructor
- Remove(int offset, int length, String str, boolean clearDirty)
- {
- this.offset = offset;
- this.length = length;
- this.str = str;
- this.clearDirty = clearDirty;
- } //}}}
- //{{{ undo() method
- int undo()
- {
- buffer.insert(offset,str);
- if(clearDirty)
- buffer.setDirty(false);
- return offset + length;
- } //}}}
- //{{{ redo() method
- int redo()
- {
- buffer.remove(offset,length);
- return offset;
- } //}}}
- int offset;
- int length;
- String str;
- } //}}}
- //{{{ CompoundEdit class
- class CompoundEdit extends Edit
- {
- //{{{ undo() method
- public int undo()
- {
- int retVal = -1;
- for(int i = undos.size() - 1; i >= 0; i--)
- {
- retVal = ((Edit)undos.elementAt(i)).undo();
- }
- return retVal;
- } //}}}
- //{{{ redo() method
- public int redo()
- {
- int retVal = -1;
- for(int i = 0; i < undos.size(); i++)
- {
- retVal = ((Edit)undos.elementAt(i)).redo();
- }
- return retVal;
- } //}}}
- //{{{ addEdit() method
- public void addEdit(Edit edit)
- {
- undos.addElement(edit);
- } //}}}
- //{{{ getSize() method
- public int getSize()
- {
- return undos.size();
- } //}}}
- Vector undos = new Vector();
- } //}}}
- //}}}
- }