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 ; 8 import java.awt.event.FocusListener ; 9 import java.util.HashMap ; 10 import java.util.Map ; 11 41 42 public class XJUndoEngine { 43 44 protected XJMainMenuBar mainMenuBar; 45 protected Map <JTextPane,XJUndo> undos = new HashMap <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 return undos.get(KeyboardFocusManager.getCurrentKeyboardFocusManager().getPermanentFocusOwner()); 64 } 65 66 public XJUndo getUndo(Object object) { 67 return undos.get(object); 68 } 69 70 public void updateUndoRedo(Object 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 { 85 86 public void focusGained(FocusEvent event) { 87 updateUndoRedo(event.getSource()); 88 } 89 90 public void focusLost(FocusEvent event) { 91 if(!event.isTemporary()) 94 updateUndoRedo(null); 95 } 96 } 97 98 } 99 | Popular Tags |