KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > UndoInsertString


1 /*
2  * UndoInsertString.java
3  *
4  * Copyright (C) 2002-2003 Peter Graves
5  * $Id: UndoInsertString.java,v 1.3 2003/06/11 13:54:39 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j;
23
24 import javax.swing.undo.AbstractUndoableEdit JavaDoc;
25 import javax.swing.undo.UndoableEdit JavaDoc;
26
27 public class UndoInsertString extends AbstractUndoableEdit JavaDoc
28     implements Constants, UndoableEdit JavaDoc
29 {
30     private final Buffer buffer;
31     private final PreState preState;
32     private PostState postState;
33     private LineSequence lines;
34
35     public UndoInsertString(Editor editor)
36     {
37         buffer = editor.getBuffer();
38         preState = new PreState(editor);
39         postState = new PostState(editor);
40     }
41
42     public void undo()
43     {
44         super.undo();
45         final Editor editor = Editor.currentEditor();
46         Debug.assertTrue(editor.getBuffer() == buffer);
47         postState = new PostState(editor);
48         preState.restoreState(editor);
49         update(editor);
50     }
51
52     public void redo()
53     {
54         super.redo();
55         final Editor editor = Editor.currentEditor();
56         Debug.assertTrue(editor.getBuffer() == buffer);
57         postState.restoreState(editor);
58         update(editor);
59     }
60
61     private void update(Editor editor)
62     {
63         editor.setUpdateFlag(REFRAME);
64         if (postState.modificationCount != preState.modificationCount) {
65             // Buffer was changed.
66
buffer.invalidate();
67             Sidebar.setUpdateFlagInAllFrames(SIDEBAR_MODIFIED_BUFFER_COUNT |
68                 SIDEBAR_REPAINT_BUFFER_LIST);
69             Sidebar.repaintBufferListInAllFrames();
70         }
71     }
72
73     private class PreState
74     {
75         final int dotLineNumber;
76         final int dotOffset;
77         final int markLineNumber;
78         final int markOffset;
79         final int absCaretCol;
80         final boolean isColumnSelection;
81         final int modificationCount;
82         final boolean modified;
83         final Line line;
84
85         PreState(Editor editor)
86         {
87             dotLineNumber = editor.getDotLine().lineNumber();;
88             dotOffset = editor.getDotOffset();
89             Position mark = editor.getMark();
90             if (mark != null) {
91                 markLineNumber = mark.lineNumber();
92                 markOffset = mark.getOffset();
93             } else {
94                 markLineNumber = -1;
95                 markOffset = -1;
96             }
97             absCaretCol = editor.getAbsoluteCaretCol();
98             isColumnSelection = editor.isColumnSelection();
99             modificationCount = buffer.getModCount();
100             modified = buffer.isModified();
101             line = editor.getDotLine().copy();
102         }
103
104         // Undo.
105
void restoreState(Editor editor)
106         {
107             final Line first = buffer.getLine(dotLineNumber);
108             final Line last = editor.getDotLine();
109             final Line after = last.next();
110             for (Line ln = first; ln != after; ln = ln.next())
111                 editor.adjustMarkers(ln);
112             if (first == last) {
113                 lines = new LineSequence(first);
114                 first.copy(line);
115                 Editor.updateInAllEditors(first);
116             } else {
117                 final Line before = first.previous();
118                 lines = new LineSequence(first, last);
119                 final Line restored = line.copy();
120                 restored.setPrevious(before);
121                 if (before != null)
122                     before.setNext(restored);
123                 else
124                     buffer.setFirstLine(restored);
125                 restored.setNext(after);
126                 if (after != null)
127                     after.setPrevious(restored);
128                 buffer.needsRenumbering = true;
129                 buffer.renumber();
130                 buffer.repaint();
131             }
132             buffer.setModCount(modificationCount);
133             editor.setDot(dotLineNumber, dotOffset);
134             editor.setMark(null);
135             final Display display = editor.getDisplay();
136             display.setCaretCol(absCaretCol - display.getShift());
137         }
138     }
139
140     private class PostState
141     {
142         final int dotLineNumber;
143         final int dotOffset;
144         final int markLineNumber;
145         final int markOffset;
146         final int absCaretCol;
147         final boolean isColumnSelection;
148         final int modificationCount;
149         final boolean modified;
150
151         PostState(Editor editor)
152         {
153             dotLineNumber = editor.getDotLine().lineNumber();;
154             dotOffset = editor.getDotOffset();
155             Position mark = editor.getMark();
156             if (mark != null) {
157                 markLineNumber = mark.lineNumber();
158                 markOffset = mark.getOffset();
159             } else {
160                 markLineNumber = -1;
161                 markOffset = -1;
162             }
163             absCaretCol = editor.getAbsoluteCaretCol();
164             isColumnSelection = editor.isColumnSelection();
165             modificationCount = editor.getBuffer().getModCount();
166             modified = editor.getBuffer().isModified();
167         }
168
169         // Redo.
170
void restoreState(Editor editor)
171         {
172             final Line dotLine = editor.getDotLine();
173             if (lines.size() == 1) {
174                 dotLine.copy(lines.getFirstLine());
175                 Editor.updateInAllEditors(dotLine);
176             } else {
177                 final Line before = dotLine.previous();
178                 final Line after = dotLine.next();
179                 lines.getFirstLine().setPrevious(before);
180                 if (before != null)
181                     before.setNext(lines.getFirstLine());
182                 else
183                     buffer.setFirstLine(lines.getFirstLine());
184                 lines.getLastLine().setNext(after);
185                 if (after != null)
186                     after.setPrevious(lines.getLastLine());
187                 buffer.needsRenumbering = true;
188                 buffer.renumber();
189                 buffer.repaint();
190             }
191             buffer.setModCount(modificationCount);
192             editor.setDot(dotLineNumber, dotOffset);
193             editor.setMark(null);
194             final Display display = editor.getDisplay();
195             display.setCaretCol(absCaretCol - display.getShift());
196         }
197     }
198 }
199
Popular Tags