KickJava   Java API By Example, From Geeks To Geeks.

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


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

41
42 public class XJUndoEngine {
43
44     protected XJMainMenuBar mainMenuBar;
45     protected Map JavaDoc<JTextPane,XJUndo> undos = new HashMap JavaDoc<JTextPane, XJUndo>();
46
47     public XJUndoEngine() {
48     }
49
50     public void setMainMenuBar(XJMainMenuBar mainMenuBar) {
51         this.mainMenuBar = mainMenuBar;
52     }
53
54     public void registerUndo(XJUndo undo, JTextPane component) {
55         undo.bindTo(component);
56         component.addFocusListener(new EditorFocusListener());
57         undos.put(component, undo);
58     }
59
60     public XJUndo getCurrentUndo() {
61         // Use the permanent focus owner because on Windows/Linux, an opened menu become
62
// the current focus owner (non-permanent).
63
return undos.get(KeyboardFocusManager.getCurrentKeyboardFocusManager().getPermanentFocusOwner());
64     }
65
66     public XJUndo getUndo(Object JavaDoc object) {
67         return undos.get(object);
68     }
69
70     public void updateUndoRedo(Object JavaDoc source) {
71         XJUndo undo = getUndo(source);
72         updateUndoRedo(undo);
73     }
74
75     public void updateUndoRedo(XJUndo undo) {
76         if(mainMenuBar != null)
77             mainMenuBar.menuUndoRedoItemState(getCurrentUndo());
78     }
79
80     public void undoStateDidChange(XJUndo undo) {
81         updateUndoRedo(undo);
82     }
83
84     protected class EditorFocusListener implements FocusListener JavaDoc {
85
86         public void focusGained(FocusEvent JavaDoc event) {
87             updateUndoRedo(event.getSource());
88         }
89
90         public void focusLost(FocusEvent JavaDoc event) {
91             // Update the menu only if the event is not temporary. Temporary
92
// focus lost can be, for example, when opening a menu on Windows/Linux.
93
if(!event.isTemporary())
94                 updateUndoRedo(null);
95         }
96     }
97
98 }
99
Popular Tags