KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * UndoInsertLineSeparator.java
3  *
4  * Copyright (C) 2002-2003 Peter Graves
5  * $Id: UndoInsertLineSeparator.java,v 1.4 2003/06/11 15:07:58 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 UndoInsertLineSeparator extends AbstractUndoableEdit JavaDoc
28     implements Constants, UndoableEdit JavaDoc
29 {
30     private PreState preState;
31     private PostState postState;
32
33     public UndoInsertLineSeparator(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         buffer.repaint();
55     }
56
57     public void redo()
58     {
59         super.redo();
60         final Editor editor = Editor.currentEditor();
61         final Buffer buffer = editor.getBuffer();
62         postState.restoreState(editor);
63         editor.setUpdateFlag(REFRAME);
64
65         if (postState.modificationCount != preState.modificationCount) {
66             // Buffer was changed.
67
buffer.invalidate();
68             Sidebar.setUpdateFlagInAllFrames(SIDEBAR_MODIFIED_BUFFER_COUNT |
69                 SIDEBAR_REPAINT_BUFFER_LIST);
70             Sidebar.repaintBufferListInAllFrames();
71         }
72         buffer.repaint();
73     }
74
75     private static class PreState
76     {
77         final int dotLineNumber;
78         final int dotOffset;
79         final int absCaretCol;
80         final int modificationCount;
81         final boolean modified;
82         final Line line;
83
84         PreState(Editor editor)
85         {
86             dotLineNumber = editor.getDotLine().lineNumber();
87             dotOffset = editor.getDotOffset();
88             absCaretCol = editor.getAbsoluteCaretCol();
89             final Buffer buffer = editor.getBuffer();
90             modificationCount = buffer.getModCount();
91             modified = buffer.isModified();
92             line = editor.getDotLine().copy();
93         }
94
95         // Undo.
96
void restoreState(Editor editor)
97         {
98             final Buffer buffer = editor.getBuffer();
99
100             editor.setDot(dotLineNumber, 0);
101             final Line before = editor.getDotLine().previous();
102             final Line after = editor.getDotLine().next().next();
103             final Line restored = line.copy();
104             restored.setPrevious(before);
105             if (before != null)
106                 before.setNext(restored);
107             else
108                 buffer.setFirstLine(restored);
109             restored.setNext(after);
110             if (after != null)
111                 after.setPrevious(restored);
112             // Markers!!
113

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

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