KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * UndoDeleteLineSeparator.java
3  *
4  * Copyright (C) 2002-2003 Peter Graves
5  * $Id: UndoDeleteLineSeparator.java,v 1.4 2003/06/11 15:05:59 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 final class UndoDeleteLineSeparator extends AbstractUndoableEdit JavaDoc
28     implements Constants, UndoableEdit JavaDoc
29 {
30     private PreState preState;
31     private PostState postState;
32
33     public UndoDeleteLineSeparator(Editor editor)
34     {
35         preState = new PreState(editor);
36     }
37
38     public void undo()
39     {
40         super.undo();
41         final Editor editor = Editor.currentEditor();
42         final Buffer buffer = editor.getBuffer();
43         postState = new PostState(editor);
44         preState.restoreState(editor);
45         editor.setUpdateFlag(REFRAME);
46
47         if (postState.modificationCount != preState.modificationCount) {
48             // Buffer was changed.
49
buffer.invalidate();
50             Sidebar.setUpdateFlagInAllFrames(SIDEBAR_MODIFIED_BUFFER_COUNT |
51                 SIDEBAR_REPAINT_BUFFER_LIST);
52             Sidebar.repaintBufferListInAllFrames();
53         }
54     }
55
56     public void redo()
57     {
58         super.redo();
59         final Editor editor = Editor.currentEditor();
60         final Buffer buffer = editor.getBuffer();
61         postState.restoreState(editor);
62         editor.setUpdateFlag(REFRAME);
63
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 static class PreState
74     {
75         final int dotLineNumber;
76         final int dotOffset;
77         final int absCaretCol;
78         final int modificationCount;
79         final boolean modified;
80         final Line first;
81         final Line second;
82
83         PreState(Editor editor)
84         {
85             final Line dotLine = editor.getDotLine();
86             dotLineNumber = dotLine.lineNumber();
87             dotOffset = editor.getDotOffset();
88             absCaretCol = editor.getAbsoluteCaretCol();
89             final Buffer buffer = editor.getBuffer();
90             modificationCount = buffer.getModCount();
91             modified = buffer.isModified();
92             first = dotLine.copy();
93             second = dotLine.next().copy();
94         }
95
96         // Undo.
97
void restoreState(Editor editor)
98         {
99             final Buffer buffer = editor.getBuffer();
100
101             Line before = editor.getDotLine().previous();
102             Line after = editor.getDotLine().next();
103             first.setPrevious(before);
104             if (before != null)
105                 before.setNext(first);
106             else
107                 buffer.setFirstLine(first);
108             first.setNext(second);
109             second.setPrevious(first);
110             second.setNext(after);
111             if (after != null)
112                 after.setPrevious(second);
113             // Markers!!
114

115             buffer.setModCount(modificationCount);
116
117             buffer.needsRenumbering = true;
118             buffer.renumber();
119
120             editor.setDot(first, dotOffset);
121             final Display display = editor.getDisplay();
122             display.setCaretCol(absCaretCol - display.getShift());
123             display.setUpdateFlag(REPAINT);
124         }
125     }
126
127     private static class PostState
128     {
129         final int dotLineNumber;
130         final int dotOffset;
131         final int absCaretCol;
132         final int modificationCount;
133         final boolean modified;
134         final Line line;
135
136         PostState(Editor editor)
137         {
138             final Line dotLine = editor.getDotLine();
139             dotLineNumber = dotLine.lineNumber();
140             dotOffset = editor.getDotOffset();
141             absCaretCol = editor.getAbsoluteCaretCol();
142             final Buffer buffer = editor.getBuffer();
143             modificationCount = buffer.getModCount();
144             modified = buffer.isModified();
145             line = dotLine.copy();
146         }
147
148         // Redo.
149
void restoreState(Editor editor)
150         {
151             final Buffer buffer = editor.getBuffer();
152
153             final Line before = editor.getDotLine().previous();
154             final Line after = editor.getDotLine().next().next();
155             final Line restored = line.copy();
156             restored.setPrevious(before);
157             if (before != null)
158                 before.setNext(restored);
159             else
160                 buffer.setFirstLine(restored);
161             restored.setNext(after);
162             if (after != null)
163                 after.setPrevious(restored);
164             // Markers!!
165

166             buffer.setModCount(modificationCount);
167
168             buffer.needsRenumbering = true;
169             buffer.renumber();
170
171             editor.setDot(restored, dotOffset);
172             final Display display = editor.getDisplay();
173             display.setCaretCol(absCaretCol - display.getShift());
174             display.setUpdateFlag(REPAINT);
175         }
176     }
177 }
178
Popular Tags