KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > xjlib > appkit > undo > XJUndo


1 /*
2
3 [The "BSD licence"]
4 Copyright (c) 2005 Jean Bovet
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions
9 are met:
10
11 1. Redistributions of source code must retain the above copyright
12 notice, this list of conditions and the following disclaimer.
13 2. Redistributions in binary form must reproduce the above copyright
14 notice, this list of conditions and the following disclaimer in the
15 documentation and/or other materials provided with the distribution.
16 3. The name of the author may not be used to endorse or promote products
17 derived from this software without specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 */

31
32 package org.antlr.xjlib.appkit.undo;
33
34 import javax.swing.*;
35 import javax.swing.event.UndoableEditEvent JavaDoc;
36 import javax.swing.event.UndoableEditListener JavaDoc;
37 import javax.swing.undo.CompoundEdit JavaDoc;
38 import javax.swing.undo.UndoManager JavaDoc;
39 import javax.swing.undo.UndoableEdit JavaDoc;
40 import java.util.Stack JavaDoc;
41
42 public class XJUndo {
43
44     protected XJUndoEngine engine;
45     protected XJUndoDelegate delegate;
46
47     protected UndoManager JavaDoc undoManager = new UndoManager JavaDoc();
48     protected XJUndoAction undoAction;
49     protected XJRedoAction redoAction;
50
51     protected Stack JavaDoc<CustomCompoundEdit> groupEditEvent = new Stack JavaDoc<CustomCompoundEdit>();
52
53     protected int enable = 0;
54
55     public XJUndo(XJUndoEngine engine, XJUndoDelegate delegate) {
56         this.engine = engine;
57         this.delegate = delegate;
58
59         undoAction = new XJUndoAction(undoManager);
60         redoAction = new XJRedoAction(undoManager);
61
62         undoAction.setRedoAction(redoAction);
63         redoAction.setUndoAction(undoAction);
64     }
65
66     public void bindTo(JTextPane textPane) {
67         textPane.getDocument().addUndoableEditListener(new TextPaneUndoableEditListener());
68     }
69
70     public void performUndo() {
71         if(delegate != null)
72             delegate.undoManagerWillUndo(false);
73
74         try {
75             undoAction.actionPerformed(null);
76             fireUndoStateDidChange();
77         } finally {
78             if(delegate != null)
79                 delegate.undoManagerDidUndo(false);
80         }
81     }
82
83     public void performRedo() {
84         if(delegate != null)
85             delegate.undoManagerWillUndo(true);
86
87         try {
88             redoAction.actionPerformed(null);
89             fireUndoStateDidChange();
90         } finally {
91             if(delegate != null)
92                 delegate.undoManagerDidUndo(true);
93         }
94     }
95
96     public void beginUndoGroup(String JavaDoc name) {
97         groupEditEvent.push(new CustomCompoundEdit(name));
98     }
99
100     public void endUndoGroup() {
101         CustomCompoundEdit edit = groupEditEvent.pop();
102         edit.end();
103         addEditEvent(edit);
104     }
105
106     public CompoundEdit JavaDoc getUndoGroup() {
107         if(groupEditEvent.isEmpty())
108             return null;
109         else
110             return (CompoundEdit JavaDoc)groupEditEvent.peek();
111     }
112
113     public void enableUndo() {
114         enable--;
115     }
116
117     public void disableUndo() {
118         enable++;
119     }
120
121     public boolean isEnabled() {
122         return enable == 0;
123     }
124
125     public boolean canUndo() {
126         return undoManager.canUndo();
127     }
128
129     public boolean canRedo() {
130         return undoManager.canRedo();
131     }
132
133     public void addEditEvent(UndoableEdit JavaDoc edit) {
134         undoManager.addEdit(edit);
135         undoAction.updateUndoState();
136         redoAction.updateRedoState();
137         fireUndoStateDidChange();
138     }
139
140     protected void fireUndoStateDidChange() {
141         engine.undoStateDidChange(this);
142     }
143
144     protected class TextPaneUndoableEditListener implements UndoableEditListener JavaDoc {
145         public void undoableEditHappened(UndoableEditEvent JavaDoc e) {
146             if(!isEnabled())
147                 return;
148
149             CompoundEdit JavaDoc groupEdit = getUndoGroup();
150             if(groupEdit == null)
151                 addEditEvent(e.getEdit());
152             else
153                 groupEdit.addEdit(e.getEdit());
154         }
155     }
156
157     protected static class CustomCompoundEdit extends CompoundEdit JavaDoc {
158
159         protected String JavaDoc name;
160
161         public CustomCompoundEdit(String JavaDoc name) {
162             this.name = name;
163         }
164
165         public String JavaDoc getUndoPresentationName() {
166             return "Undo "+name;
167         }
168
169         public String JavaDoc getRedoPresentationName() {
170             return "Redo "+name;
171         }
172
173     }
174 }
175
Popular Tags