KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > UndoMove


1 /*
2  * UndoMove.java
3  *
4  * Copyright (C) 2002-2003 Peter Graves
5  * $Id: UndoMove.java,v 1.2 2003/08/01 17:23:39 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j;
23
24 import javax.swing.undo.AbstractUndoableEdit JavaDoc;
25
26 public class UndoMove extends AbstractUndoableEdit JavaDoc implements Constants
27 {
28     private final State preState;
29     private State postState;
30
31     public UndoMove(Editor editor)
32     {
33         preState = new State(editor);
34     }
35
36     public void undo()
37     {
38         super.undo();
39         final Editor editor = Editor.currentEditor();
40         postState = new State(editor);
41         preState.restoreState(editor);
42         editor.setUpdateFlag(REFRAME);
43     }
44
45     public void redo()
46     {
47         super.redo();
48         final Editor editor = Editor.currentEditor();
49         postState.restoreState(editor);
50         editor.setUpdateFlag(REFRAME);
51     }
52
53     private static class State
54     {
55         final int dotLineNumber;
56         final int dotOffset;
57         final int markLineNumber;
58         final int markOffset;
59         final int absCaretCol;
60         final boolean isColumnSelection;
61
62         State(Editor editor)
63         {
64             dotLineNumber = editor.getDotLine().lineNumber();;
65             dotOffset = editor.getDotOffset();
66             Position mark = editor.getMark();
67             if (mark != null) {
68                 markLineNumber = mark.lineNumber();
69                 markOffset = mark.getOffset();
70             } else {
71                 markLineNumber = -1;
72                 markOffset = -1;
73             }
74             absCaretCol = editor.getAbsoluteCaretCol();
75             isColumnSelection = editor.isColumnSelection();
76         }
77
78         void restoreState(Editor editor)
79         {
80             boolean wasMarked = editor.getMark() != null;
81             editor.updateDotLine();
82             editor.setDot(dotLineNumber, dotOffset);
83             if (markLineNumber >= 0) {
84                 editor.setMark(markLineNumber, markOffset);
85                 editor.setColumnSelection(isColumnSelection);
86             } else
87                 editor.setMark(null);
88             editor.updateDotLine();
89             final Display display = editor.getDisplay();
90             display.setCaretCol(absCaretCol - display.getShift());
91             if (wasMarked || editor.getMark() != null)
92                 display.setUpdateFlag(REPAINT);
93         }
94     }
95 }
96
Popular Tags