001: /*
002: * UndoDeleteRegion.java
003: *
004: * Copyright (C) 2002-2003 Peter Graves
005: * $Id: UndoDeleteRegion.java,v 1.4 2003/06/11 15:07:24 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 class UndoDeleteRegion extends AbstractUndoableEdit implements
028: Constants, UndoableEdit {
029: private final PreState preState;
030: private PostState postState;
031: private final LineSequence lines;
032:
033: public UndoDeleteRegion(Editor editor, Region r) {
034: Debug.assertTrue(!editor.isColumnSelection());
035: preState = new PreState(editor);
036: lines = new LineSequence(r.getBeginLine(), r.getEndLine());
037: }
038:
039: public void undo() {
040: super .undo();
041: final Editor editor = Editor.currentEditor();
042: final Buffer buffer = editor.getBuffer();
043: postState = new PostState(editor);
044: preState.restoreState(editor);
045: editor.setUpdateFlag(REFRAME);
046:
047: if (postState.modificationCount != preState.modificationCount) {
048: // Buffer was changed.
049: buffer.invalidate();
050: Sidebar
051: .setUpdateFlagInAllFrames(SIDEBAR_MODIFIED_BUFFER_COUNT
052: | SIDEBAR_REPAINT_BUFFER_LIST);
053: Sidebar.repaintBufferListInAllFrames();
054: }
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: }
073:
074: private class PreState {
075: final int dotLineNumber;
076: final int dotOffset;
077: final int markLineNumber;
078: final int markOffset;
079: final int absCaretCol;
080: final int modificationCount;
081: final boolean modified;
082:
083: PreState(Editor editor) {
084: final Line dotLine = editor.getDotLine();
085: dotLineNumber = dotLine.lineNumber();
086: dotOffset = editor.getDotOffset();
087: absCaretCol = editor.getAbsoluteCaretCol();
088: Position mark = editor.getMark();
089: if (mark != null) {
090: markLineNumber = mark.lineNumber();
091: markOffset = mark.getOffset();
092: } else {
093: markLineNumber = -1;
094: markOffset = -1;
095: }
096: final Buffer buffer = editor.getBuffer();
097: modificationCount = buffer.getModCount();
098: modified = buffer.isModified();
099: }
100:
101: // Undo.
102: void restoreState(Editor editor) {
103: final Buffer buffer = editor.getBuffer();
104:
105: final Line dotLine = editor.getDotLine();
106: final Line before = dotLine.previous();
107: final Line after = dotLine.next();
108:
109: editor.adjustMarkers(dotLine);
110:
111: Line first = lines.getFirstLine();
112: Line last = lines.getLastLine();
113: first.setPrevious(before);
114: if (before != null)
115: before.setNext(first);
116: else
117: buffer.setFirstLine(first);
118: last.setNext(after);
119: if (after != null)
120: after.setPrevious(last);
121:
122: buffer.setModCount(modificationCount);
123:
124: buffer.needsRenumbering = true;
125: buffer.renumber();
126:
127: editor.setDot(dotLineNumber, dotOffset);
128: editor.setMark(markLineNumber, markOffset);
129: final Display display = editor.getDisplay();
130: display.setCaretCol(absCaretCol - display.getShift());
131: display.setUpdateFlag(REPAINT);
132: }
133: }
134:
135: private class PostState {
136: final int dotLineNumber;
137: final int dotOffset;
138: final int absCaretCol;
139: final int modificationCount;
140: final boolean modified;
141: final Line line;
142:
143: PostState(Editor editor) {
144: final Line dotLine = editor.getDotLine();
145: dotLineNumber = dotLine.lineNumber();
146: dotOffset = editor.getDotOffset();
147: absCaretCol = editor.getAbsoluteCaretCol();
148: final Buffer buffer = editor.getBuffer();
149: modificationCount = buffer.getModCount();
150: modified = buffer.isModified();
151: line = dotLine.copy();
152: }
153:
154: // Redo the deletion.
155: void restoreState(Editor editor) {
156: final Buffer buffer = editor.getBuffer();
157:
158: final Line before = editor.getDotLine().previous();
159: Line after = editor.getDotLine();
160: for (int i = 0; i < lines.size(); i++) {
161: after = after.next();
162: if (after == null)
163: break;
164: }
165:
166: final Line restored = line.copy();
167: restored.setPrevious(before);
168: if (before != null)
169: before.setNext(restored);
170: else
171: buffer.setFirstLine(restored);
172: restored.setNext(after);
173: if (after != null)
174: after.setPrevious(restored);
175: // Markers!!
176:
177: buffer.setModCount(modificationCount);
178:
179: buffer.needsRenumbering = true;
180: buffer.renumber();
181:
182: editor.setDot(restored, dotOffset);
183: editor.setMark(null);
184: final Display display = editor.getDisplay();
185: display.setCaretCol(absCaretCol - display.getShift());
186: display.setUpdateFlag(REPAINT);
187: }
188: }
189: }
|