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