PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/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
  1. /*
  2. * UndoManager.java - Buffer undo manager
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 2001 Slava Pestov
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. */
  22. package org.gjt.sp.jedit.buffer;
  23. //{{{ Imports
  24. import java.util.Vector;
  25. import org.gjt.sp.jedit.textarea.JEditTextArea;
  26. import org.gjt.sp.jedit.Buffer;
  27. import org.gjt.sp.util.Log;
  28. //}}}
  29. public class UndoManager
  30. {
  31. //{{{ UndoManager constructor
  32. public UndoManager(Buffer buffer)
  33. {
  34. this.buffer = buffer;
  35. undos = new Vector(100);
  36. } //}}}
  37. //{{{ setLimit() method
  38. public void setLimit(int limit)
  39. {
  40. this.limit = limit;
  41. } //}}}
  42. //{{{ clear() method
  43. public void clear()
  44. {
  45. undos.removeAllElements();
  46. undoPos = undoCount = 0;
  47. } //}}}
  48. //{{{ undo() method
  49. public boolean undo(JEditTextArea textArea)
  50. {
  51. if(insideCompoundEdit())
  52. throw new InternalError("Unbalanced begin/endCompoundEdit()");
  53. if(undoPos == 0)
  54. return false;
  55. else
  56. {
  57. Edit edit = (Edit)undos.elementAt(--undoPos);
  58. int caret = edit.undo();
  59. if(caret != -1)
  60. textArea.setCaretPosition(caret);
  61. return true;
  62. }
  63. } //}}}
  64. //{{{ redo() method
  65. public boolean redo(JEditTextArea textArea)
  66. {
  67. if(insideCompoundEdit())
  68. throw new InternalError("Unbalanced begin/endCompoundEdit()");
  69. if(undoPos == undoCount)
  70. return false;
  71. else
  72. {
  73. Edit edit = (Edit)undos.elementAt(undoPos++);
  74. int caret = edit.redo();
  75. if(caret != -1)
  76. textArea.setCaretPosition(caret);
  77. return true;
  78. }
  79. } //}}}
  80. //{{{ beginCompoundEdit() method
  81. public void beginCompoundEdit()
  82. {
  83. if(compoundEditCount == 0)
  84. compoundEdit = new CompoundEdit();
  85. compoundEditCount++;
  86. } //}}}
  87. //{{{ endCompoundEdit() method
  88. public void endCompoundEdit()
  89. {
  90. if(compoundEditCount == 1)
  91. {
  92. if(compoundEdit.getSize() != 0)
  93. addEdit(compoundEdit);
  94. compoundEdit = null;
  95. }
  96. else if(compoundEditCount == 0)
  97. {
  98. Log.log(Log.WARNING,this,new Exception("Unbalanced begin/endCompoundEdit()"));
  99. return;
  100. }
  101. compoundEditCount--;
  102. } //}}}
  103. //{{{ insideCompoundEdit() method
  104. public boolean insideCompoundEdit()
  105. {
  106. return compoundEditCount != 0;
  107. } //}}}
  108. //{{{ contentInserted() method
  109. public void contentInserted(int offset, int length, String text, boolean clearDirty)
  110. {
  111. Edit toMerge = null;
  112. if(compoundEdit != null)
  113. {
  114. int size = compoundEdit.getSize();
  115. if(size != 0)
  116. toMerge = (Edit)compoundEdit.undos.elementAt(size - 1);
  117. }
  118. else
  119. {
  120. if(undoPos != 0)
  121. toMerge = (Edit)undos.elementAt(undoPos - 1);
  122. }
  123. if(!clearDirty && toMerge instanceof Insert)
  124. {
  125. Insert ins = (Insert)toMerge;
  126. if(ins.offset == offset)
  127. {
  128. ins.str = text.concat(ins.str);
  129. ins.length += length;
  130. return;
  131. }
  132. else if(ins.offset + ins.length == offset)
  133. {
  134. ins.str = ins.str.concat(text);
  135. ins.length += length;
  136. return;
  137. }
  138. }
  139. Insert ins = new Insert(offset,length,text,clearDirty);
  140. if(clearDirty)
  141. {
  142. if(clearDirtyEdit != null)
  143. clearDirtyEdit.clearDirty = false;
  144. clearDirtyEdit = ins;
  145. }
  146. if(compoundEdit != null)
  147. compoundEdit.addEdit(ins);
  148. else
  149. addEdit(ins);
  150. } //}}}
  151. //{{{ contentRemoved() method
  152. public void contentRemoved(int offset, int length, String text, boolean clearDirty)
  153. {
  154. Edit toMerge = null;
  155. if(compoundEdit != null)
  156. {
  157. int size = compoundEdit.getSize();
  158. if(size != 0)
  159. toMerge = (Edit)compoundEdit.undos.elementAt(size - 1);
  160. }
  161. else
  162. {
  163. if(undoPos != 0)
  164. toMerge = (Edit)undos.elementAt(undoPos - 1);
  165. }
  166. if(!clearDirty && toMerge instanceof Remove)
  167. {
  168. Remove rem = (Remove)toMerge;
  169. if(rem.offset == offset)
  170. {
  171. rem.str = rem.str.concat(text);
  172. rem.length += length;
  173. return;
  174. }
  175. else if(rem.offset + rem.length == offset)
  176. {
  177. rem.str = text.concat(rem.str);
  178. rem.length += length;
  179. return;
  180. }
  181. }
  182. Remove rem = new Remove(offset,length,text,clearDirty);
  183. if(clearDirty)
  184. {
  185. if(clearDirtyEdit != null)
  186. clearDirtyEdit.clearDirty = false;
  187. clearDirtyEdit = rem;
  188. }
  189. if(compoundEdit != null)
  190. compoundEdit.addEdit(rem);
  191. else
  192. addEdit(rem);
  193. } //}}}
  194. //{{{ bufferSaved() method
  195. public void bufferSaved()
  196. {
  197. if(clearDirtyEdit != null)
  198. {
  199. clearDirtyEdit.clearDirty = false;
  200. clearDirtyEdit = null;
  201. }
  202. } //}}}
  203. //{{{ Private members
  204. //{{{ Instance variables
  205. private Buffer buffer;
  206. private Vector undos;
  207. private int limit;
  208. private int undoPos;
  209. private int undoCount;
  210. private int compoundEditCount;
  211. private CompoundEdit compoundEdit;
  212. private Edit clearDirtyEdit;
  213. //}}}
  214. //{{{ addEdit() method
  215. private void addEdit(Edit edit)
  216. {
  217. undos.insertElementAt(edit,undoPos++);
  218. if(undos.size() > limit)
  219. {
  220. undos.removeElementAt(0);
  221. undoPos--;
  222. }
  223. undoCount = undoPos;
  224. } //}}}
  225. //}}}
  226. //{{{ Inner classes
  227. //{{{ Edit interface
  228. abstract class Edit
  229. {
  230. //{{{ undo() method
  231. abstract int undo();
  232. //}}}
  233. //{{{ redo() method
  234. abstract int redo();
  235. //}}}
  236. boolean clearDirty;
  237. } //}}}
  238. //{{{ Insert class
  239. class Insert extends Edit
  240. {
  241. //{{{ Insert constructor
  242. Insert(int offset, int length, String str, boolean clearDirty)
  243. {
  244. this.offset = offset;
  245. this.length = length;
  246. this.str = str;
  247. this.clearDirty = clearDirty;
  248. } //}}}
  249. //{{{ undo() method
  250. int undo()
  251. {
  252. buffer.remove(offset,length);
  253. if(clearDirty)
  254. buffer.setDirty(false);
  255. return offset;
  256. } //}}}
  257. //{{{ redo() method
  258. int redo()
  259. {
  260. buffer.insert(offset,str);
  261. return offset + length;
  262. } //}}}
  263. int offset;
  264. int length;
  265. String str;
  266. } //}}}
  267. //{{{ Remove class
  268. class Remove extends Edit
  269. {
  270. //{{{ Remove constructor
  271. Remove(int offset, int length, String str, boolean clearDirty)
  272. {
  273. this.offset = offset;
  274. this.length = length;
  275. this.str = str;
  276. this.clearDirty = clearDirty;
  277. } //}}}
  278. //{{{ undo() method
  279. int undo()
  280. {
  281. buffer.insert(offset,str);
  282. if(clearDirty)
  283. buffer.setDirty(false);
  284. return offset + length;
  285. } //}}}
  286. //{{{ redo() method
  287. int redo()
  288. {
  289. buffer.remove(offset,length);
  290. return offset;
  291. } //}}}
  292. int offset;
  293. int length;
  294. String str;
  295. } //}}}
  296. //{{{ CompoundEdit class
  297. class CompoundEdit extends Edit
  298. {
  299. //{{{ undo() method
  300. public int undo()
  301. {
  302. int retVal = -1;
  303. for(int i = undos.size() - 1; i >= 0; i--)
  304. {
  305. retVal = ((Edit)undos.elementAt(i)).undo();
  306. }
  307. return retVal;
  308. } //}}}
  309. //{{{ redo() method
  310. public int redo()
  311. {
  312. int retVal = -1;
  313. for(int i = 0; i < undos.size(); i++)
  314. {
  315. retVal = ((Edit)undos.elementAt(i)).redo();
  316. }
  317. return retVal;
  318. } //}}}
  319. //{{{ addEdit() method
  320. public void addEdit(Edit edit)
  321. {
  322. undos.addElement(edit);
  323. } //}}}
  324. //{{{ getSize() method
  325. public int getSize()
  326. {
  327. return undos.size();
  328. } //}}}
  329. Vector undos = new Vector();
  330. } //}}}
  331. //}}}
  332. }