PageRenderTime 57ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-1-pre5/org/gjt/sp/jedit/buffer/UndoManager.java

#
Java | 383 lines | 277 code | 48 blank | 58 comment | 61 complexity | 274782df1c14eae0040edadca08c1f35 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, 2002 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.ArrayList;
  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 ArrayList(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.clear();
  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.get(--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.get(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 == 0)
  91. {
  92. Log.log(Log.WARNING,this,new Exception("Unbalanced begin/endCompoundEdit()"));
  93. return;
  94. }
  95. else if(compoundEditCount == 1)
  96. {
  97. switch(compoundEdit.undos.size())
  98. {
  99. case 0:
  100. /* nothing done between begin/end calls */;
  101. break;
  102. case 1:
  103. addEdit((Edit)compoundEdit.undos.get(0));
  104. break;
  105. default:
  106. addEdit(compoundEdit);
  107. }
  108. compoundEdit = null;
  109. }
  110. compoundEditCount--;
  111. } //}}}
  112. //{{{ insideCompoundEdit() method
  113. public boolean insideCompoundEdit()
  114. {
  115. return compoundEditCount != 0;
  116. } //}}}
  117. //{{{ contentInserted() method
  118. public void contentInserted(int offset, int length, String text, boolean clearDirty)
  119. {
  120. Edit toMerge = getLastEdit();
  121. if(!clearDirty && toMerge instanceof Insert)
  122. {
  123. Insert ins = (Insert)toMerge;
  124. if(ins.offset == offset)
  125. {
  126. ins.str = text.concat(ins.str);
  127. ins.length += length;
  128. return;
  129. }
  130. else if(ins.offset + ins.length == offset)
  131. {
  132. ins.str = ins.str.concat(text);
  133. ins.length += length;
  134. return;
  135. }
  136. }
  137. Insert ins = new Insert(offset,length,text,clearDirty);
  138. if(clearDirty)
  139. {
  140. if(clearDirtyEdit != null)
  141. clearDirtyEdit.clearDirty = false;
  142. clearDirtyEdit = ins;
  143. }
  144. if(compoundEdit != null)
  145. compoundEdit.undos.add(ins);
  146. else
  147. addEdit(ins);
  148. } //}}}
  149. //{{{ contentRemoved() method
  150. public void contentRemoved(int offset, int length, String text, boolean clearDirty)
  151. {
  152. Edit toMerge = getLastEdit();
  153. if(!clearDirty && toMerge instanceof Remove)
  154. {
  155. Remove rem = (Remove)toMerge;
  156. if(rem.offset == offset)
  157. {
  158. rem.str = rem.str.concat(text);
  159. rem.length += length;
  160. return;
  161. }
  162. else if(offset + length == rem.offset)
  163. {
  164. rem.str = text.concat(rem.str);
  165. rem.length += length;
  166. rem.offset = offset;
  167. return;
  168. }
  169. }
  170. Remove rem = new Remove(offset,length,text,clearDirty);
  171. if(clearDirty)
  172. {
  173. if(clearDirtyEdit != null)
  174. clearDirtyEdit.clearDirty = false;
  175. clearDirtyEdit = rem;
  176. }
  177. if(compoundEdit != null)
  178. compoundEdit.undos.add(rem);
  179. else
  180. addEdit(rem);
  181. } //}}}
  182. //{{{ bufferSaved() method
  183. public void bufferSaved()
  184. {
  185. if(clearDirtyEdit != null)
  186. {
  187. clearDirtyEdit.clearDirty = false;
  188. clearDirtyEdit = null;
  189. }
  190. } //}}}
  191. //{{{ Private members
  192. //{{{ Instance variables
  193. private Buffer buffer;
  194. private ArrayList undos;
  195. private int limit;
  196. private int undoPos;
  197. private int undoCount;
  198. private int compoundEditCount;
  199. private CompoundEdit compoundEdit;
  200. private Edit clearDirtyEdit;
  201. //}}}
  202. //{{{ addEdit() method
  203. private void addEdit(Edit edit)
  204. {
  205. undos.add(undoPos++,edit);
  206. if(undos.size() > limit)
  207. {
  208. undos.remove(0);
  209. undoPos--;
  210. }
  211. undoCount = undoPos;
  212. } //}}}
  213. //{{{ getLastEdit() method
  214. private Edit getLastEdit()
  215. {
  216. if(compoundEdit != null)
  217. {
  218. int size = compoundEdit.undos.size();
  219. if(size != 0)
  220. return (Edit)compoundEdit.undos.get(size - 1);
  221. else
  222. return null;
  223. }
  224. else if(undoCount != 0 && undoPos != 0)
  225. {
  226. Edit e = (Edit)undos.get(undoPos - 1);
  227. if(e instanceof CompoundEdit)
  228. {
  229. CompoundEdit c = (CompoundEdit)e;
  230. return (Edit)c.undos.get(c.undos.size() - 1);
  231. }
  232. }
  233. if(undoPos != 0)
  234. return (Edit)undos.get(undoPos - 1);
  235. else
  236. return null;
  237. } //}}}
  238. //}}}
  239. //{{{ Inner classes
  240. //{{{ Edit interface
  241. abstract class Edit
  242. {
  243. //{{{ undo() method
  244. abstract int undo();
  245. //}}}
  246. //{{{ redo() method
  247. abstract int redo();
  248. //}}}
  249. boolean clearDirty;
  250. } //}}}
  251. //{{{ Insert class
  252. class Insert extends Edit
  253. {
  254. //{{{ Insert constructor
  255. Insert(int offset, int length, String str, boolean clearDirty)
  256. {
  257. this.offset = offset;
  258. this.length = length;
  259. this.str = str;
  260. this.clearDirty = clearDirty;
  261. } //}}}
  262. //{{{ undo() method
  263. int undo()
  264. {
  265. buffer.remove(offset,length);
  266. if(clearDirty)
  267. buffer.setDirty(false);
  268. return offset;
  269. } //}}}
  270. //{{{ redo() method
  271. int redo()
  272. {
  273. buffer.insert(offset,str);
  274. return offset + length;
  275. } //}}}
  276. int offset;
  277. int length;
  278. String str;
  279. } //}}}
  280. //{{{ Remove class
  281. class Remove extends Edit
  282. {
  283. //{{{ Remove constructor
  284. Remove(int offset, int length, String str, boolean clearDirty)
  285. {
  286. this.offset = offset;
  287. this.length = length;
  288. this.str = str;
  289. this.clearDirty = clearDirty;
  290. } //}}}
  291. //{{{ undo() method
  292. int undo()
  293. {
  294. buffer.insert(offset,str);
  295. if(clearDirty)
  296. buffer.setDirty(false);
  297. return offset + length;
  298. } //}}}
  299. //{{{ redo() method
  300. int redo()
  301. {
  302. buffer.remove(offset,length);
  303. return offset;
  304. } //}}}
  305. int offset;
  306. int length;
  307. String str;
  308. } //}}}
  309. //{{{ CompoundEdit class
  310. class CompoundEdit extends Edit
  311. {
  312. //{{{ undo() method
  313. public int undo()
  314. {
  315. int retVal = -1;
  316. for(int i = undos.size() - 1; i >= 0; i--)
  317. {
  318. retVal = ((Edit)undos.get(i)).undo();
  319. }
  320. return retVal;
  321. } //}}}
  322. //{{{ redo() method
  323. public int redo()
  324. {
  325. int retVal = -1;
  326. for(int i = 0; i < undos.size(); i++)
  327. {
  328. retVal = ((Edit)undos.get(i)).redo();
  329. }
  330. return retVal;
  331. } //}}}
  332. ArrayList undos = new ArrayList();
  333. } //}}}
  334. //}}}
  335. }