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