001: /*
002: * UndoRemoveLine.java
003: *
004: * Copyright (C) 2002-2003 Peter Graves
005: * $Id: UndoRemoveLine.java,v 1.4 2003/06/11 15:06:42 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j;
023:
024: import javax.swing.undo.AbstractUndoableEdit;
025: import javax.swing.undo.UndoableEdit;
026:
027: public final class UndoRemoveLine extends AbstractUndoableEdit
028: implements Constants, UndoableEdit {
029: private final boolean insertBefore;
030: private final PreState preState;
031: private PostState postState;
032:
033: public UndoRemoveLine(Editor editor, boolean insertBefore) {
034: this .insertBefore = insertBefore;
035: preState = new PreState(editor);
036: }
037:
038: public void undo() {
039: super .undo();
040: final Editor editor = Editor.currentEditor();
041: final Buffer buffer = editor.getBuffer();
042: postState = new PostState(editor);
043: preState.restoreState(editor);
044: editor.setUpdateFlag(REFRAME);
045:
046: if (postState.modificationCount != preState.modificationCount) {
047: // Buffer was changed.
048: buffer.invalidate();
049: Sidebar
050: .setUpdateFlagInAllFrames(SIDEBAR_MODIFIED_BUFFER_COUNT
051: | SIDEBAR_REPAINT_BUFFER_LIST);
052: Sidebar.repaintBufferListInAllFrames();
053: }
054: buffer.repaint();
055: }
056:
057: public void redo() {
058: super .redo();
059: final Editor editor = Editor.currentEditor();
060: final Buffer buffer = editor.getBuffer();
061: postState.restoreState(editor);
062: editor.setUpdateFlag(REFRAME);
063:
064: if (postState.modificationCount != preState.modificationCount) {
065: // Buffer was changed.
066: buffer.invalidate();
067: Sidebar
068: .setUpdateFlagInAllFrames(SIDEBAR_MODIFIED_BUFFER_COUNT
069: | SIDEBAR_REPAINT_BUFFER_LIST);
070: Sidebar.repaintBufferListInAllFrames();
071: }
072: buffer.repaint();
073: }
074:
075: private class PreState {
076: final int dotLineNumber;
077: final int dotOffset;
078: final int absCaretCol;
079: final int modificationCount;
080: final boolean modified;
081: final Line line;
082:
083: PreState(Editor editor) {
084: final Line dotLine = editor.getDotLine();
085: dotLineNumber = dotLine.lineNumber();
086: dotOffset = editor.getDotOffset();
087: Debug.assertTrue(dotOffset == 0);
088: absCaretCol = editor.getAbsoluteCaretCol();
089: final Buffer buffer = editor.getBuffer();
090: modificationCount = buffer.getModCount();
091: modified = buffer.isModified();
092:
093: final Line toBeRemoved = insertBefore ? dotLine.previous()
094: : dotLine.next();
095: Debug.assertTrue(toBeRemoved != null);
096: line = toBeRemoved.copy();
097: }
098:
099: // Undo remove line.
100: void restoreState(Editor editor) {
101: final Buffer buffer = editor.getBuffer();
102: Line before, after;
103: if (insertBefore) {
104: before = editor.getDotLine().previous();
105: after = editor.getDotLine();
106: } else {
107: before = editor.getDotLine();
108: after = editor.getDotLine().next();
109: }
110: line.setPrevious(before);
111: if (before != null)
112: before.setNext(line);
113: else
114: buffer.setFirstLine(line);
115: line.setNext(after);
116: if (after != null)
117: after.setPrevious(line);
118: // Markers!!
119:
120: buffer.setModCount(modificationCount);
121: buffer.needsRenumbering = true;
122: buffer.renumber();
123:
124: final Display display = editor.getDisplay();
125: display.setCaretCol(absCaretCol - display.getShift());
126: }
127: }
128:
129: private class PostState {
130: final int dotLineNumber;
131: final int dotOffset;
132: final int absCaretCol;
133: final int modificationCount;
134: final boolean modified;
135:
136: PostState(Editor editor) {
137: final Line dotLine = editor.getDotLine();
138: dotLineNumber = dotLine.lineNumber();
139: dotOffset = editor.getDotOffset();
140: absCaretCol = editor.getAbsoluteCaretCol();
141: final Buffer buffer = editor.getBuffer();
142: modificationCount = buffer.getModCount();
143: modified = buffer.isModified();
144: }
145:
146: // Redo remove line.
147: void restoreState(Editor editor) {
148: final Buffer buffer = editor.getBuffer();
149:
150: Line remove;
151: if (insertBefore) {
152: // Remove line before dot.
153: remove = editor.getDotLine().previous();
154: } else {
155: // Remove line after dot.
156: remove = editor.getDotLine().next();
157: }
158: final Line before = remove.previous();
159: final Line after = remove.next();
160:
161: if (before != null)
162: before.setNext(after);
163: else
164: buffer.setFirstLine(after);
165: if (after != null)
166: after.setPrevious(before);
167: // Markers!!
168:
169: buffer.setModCount(modificationCount);
170: buffer.needsRenumbering = true;
171: buffer.renumber();
172:
173: editor.setDot(dotLineNumber, dotOffset);
174: final Display display = editor.getDisplay();
175: display.setCaretCol(absCaretCol - display.getShift());
176:
177: for (EditorIterator it = new EditorIterator(); it.hasNext();) {
178: Editor ed = it.nextEditor();
179: if (ed.getTopLine() == remove)
180: ed.setTopLine(editor.getDotLine());
181: }
182: }
183: }
184: }
|