001: /*
002: * UndoFold.java
003: *
004: * Copyright (C) 2002 Peter Graves
005: * $Id: UndoFold.java,v 1.1.1.1 2002/09/24 16:09:33 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 UndoFold extends AbstractUndoableEdit implements
028: Constants, UndoableEdit {
029: private final State preState;
030: private State postState;
031:
032: public UndoFold(Editor editor) {
033: preState = new State(editor);
034: }
035:
036: public void undo() {
037: super .undo();
038: final Editor editor = Editor.currentEditor();
039: postState = new State(editor);
040: preState.restoreState(editor);
041: editor.setUpdateFlag(REFRAME);
042: }
043:
044: public void redo() {
045: super .redo();
046: final Editor editor = Editor.currentEditor();
047: postState.restoreState(editor);
048: editor.setUpdateFlag(REFRAME);
049: }
050:
051: private static class State {
052: final int dotLineNumber;
053: final int dotOffset;
054: final int markLineNumber;
055: final int markOffset;
056: final int absCaretCol;
057: final boolean isColumnSelection;
058: final HiddenLines hiddenLines;
059:
060: State(Editor editor) {
061: dotLineNumber = editor.getDotLine().lineNumber();
062: ;
063: dotOffset = editor.getDotOffset();
064: Position mark = editor.getMark();
065: if (mark != null) {
066: markLineNumber = mark.lineNumber();
067: markOffset = mark.getOffset();
068: } else {
069: markLineNumber = -1;
070: markOffset = -1;
071: }
072: absCaretCol = editor.getAbsoluteCaretCol();
073: isColumnSelection = editor.isColumnSelection();
074: hiddenLines = new HiddenLines(editor);
075: }
076:
077: void restoreState(Editor editor) {
078: // Remember the top line in the current edit window (try not to
079: // reframe unnecessarily).
080: final Display display = editor.getDisplay();
081: int topLineNumber = display.getTopLineNumber();
082:
083: hiddenLines.restore();
084:
085: // Use same top line if possible.
086: Line topLine = display.getTopLine();
087:
088: if (topLine.isHidden()) {
089: Line line = topLine.previousVisible();
090: if (line == null)
091: line = topLine.nextVisible();
092: editor.setTopLine(line);
093: }
094: editor.setDot(dotLineNumber, dotOffset);
095: if (markLineNumber >= 0) {
096: editor.setMark(markLineNumber, markOffset);
097: editor.setColumnSelection(isColumnSelection);
098: } else
099: editor.setMark(null);
100: display.setCaretCol(absCaretCol - display.getShift());
101: display.setUpdateFlag(REPAINT);
102:
103: // Update any other windows displaying this buffer.
104: if (Editor.getEditorCount() > 1) {
105: for (EditorIterator it = new EditorIterator(); it
106: .hasNext();) {
107: Editor ed = it.nextEditor();
108: if (ed.getBuffer() == editor.getBuffer()) {
109: // Make sure dot is visible.
110: if (ed.getDot().isHidden()) {
111: Line line = ed.getDotLine()
112: .previousVisible();
113: if (line != null) {
114: ed.setDot(line, 0);
115: ed.moveCaretToDotCol();
116: }
117: }
118: ed.getDisplay().repaint();
119: }
120: }
121: }
122: }
123: }
124: }
|