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