1 19 package org.openide.actions; 20 21 import org.openide.awt.UndoRedo; 22 import org.openide.util.HelpCtx; 23 import org.openide.util.NbBundle; 24 import org.openide.util.actions.CallableSystemAction; 25 import org.openide.windows.TopComponent; 26 import org.openide.windows.TopComponent.Registry; 27 import org.openide.windows.WindowManager; 28 29 import java.beans.*; 30 import java.util.logging.Logger ; 31 import java.util.logging.Level ; 32 33 import javax.swing.SwingUtilities ; 34 import javax.swing.UIManager ; 35 import javax.swing.event.*; 36 import javax.swing.undo.*; 37 import org.openide.util.Exceptions; 38 39 40 45 public class UndoAction extends CallableSystemAction { 46 47 private static Listener listener; 48 49 50 private static UndoRedo last = UndoRedo.NONE; 51 private static String SWING_DEFAULT_LABEL = UIManager.getString("AbstractUndoableEdit.undoText"); private static UndoAction undoAction = null; 53 private static RedoAction redoAction = null; 54 55 public boolean isEnabled() { 56 initializeUndoRedo(); 57 58 return super.isEnabled(); 59 } 60 61 63 static synchronized void initializeUndoRedo() { 64 if (listener != null) { 65 return; 66 } 67 68 listener = new Listener(); 69 70 Registry r = WindowManager.getDefault().getRegistry(); 71 72 r.addPropertyChangeListener(org.openide.util.WeakListeners.propertyChange(listener, r)); 73 last = getUndoRedo(); 74 last.addChangeListener(listener); 75 76 updateStatus(); 77 } 78 79 81 static synchronized void updateStatus() { 82 if (undoAction == null) { 83 undoAction = (UndoAction) findObject(UndoAction.class, false); 84 } 85 86 if (redoAction == null) { 87 redoAction = (RedoAction) findObject(RedoAction.class, false); 88 } 89 90 SwingUtilities.invokeLater( 91 new Runnable () { 92 public void run() { 93 UndoRedo ur = getUndoRedo(); 94 95 if (undoAction != null) { 96 undoAction.setEnabled(ur.canUndo()); 97 } 98 99 if (redoAction != null) { 100 redoAction.setEnabled(ur.canRedo()); 101 } 102 } 103 } 104 ); 105 } 106 107 109 static UndoRedo getUndoRedo() { 110 TopComponent el = WindowManager.getDefault().getRegistry().getActivated(); 111 112 return (el == null) ? UndoRedo.NONE : el.getUndoRedo(); 113 } 114 115 public String getName() { 116 String undo = getUndoRedo().getUndoPresentationName(); 120 Logger.getLogger (UndoAction.class.getName ()).log (Level.FINE, "getUndoRedo().getUndoPresentationName() returns " + undo); 121 Logger.getLogger (UndoAction.class.getName ()).log (Level.FINE, "SWING_DEFAULT_LABEL is " + SWING_DEFAULT_LABEL); 122 123 if ((undo != null) && (SWING_DEFAULT_LABEL != null) && undo.startsWith(SWING_DEFAULT_LABEL)) { 124 undo = undo.substring(SWING_DEFAULT_LABEL.length()).trim(); 125 } 126 127 Logger.getLogger (UndoAction.class.getName ()).log (Level.FINE, "Name adapted by SWING_DEFAULT_LABEL is " + undo); 128 String presentationName = NbBundle.getMessage(UndoAction.class, "Undo", undo); 129 130 Logger.getLogger (UndoAction.class.getName ()).log (Level.FINE, "Result name is " + presentationName); 131 132 return presentationName; 133 } 134 135 public HelpCtx getHelpCtx() { 136 return new HelpCtx(UndoAction.class); 137 } 138 139 protected String iconResource() { 140 return "org/openide/resources/actions/undo.gif"; } 142 143 public void performAction() { 144 try { 145 UndoRedo undoRedo = getUndoRedo(); 146 147 if (undoRedo.canUndo()) { 148 undoRedo.undo(); 149 } 150 } catch (CannotUndoException ex) { 151 Exceptions.printStackTrace(ex); 152 } 153 154 updateStatus(); 155 } 156 157 protected boolean asynchronous() { 158 return false; 159 } 160 161 164 private static final class Listener implements PropertyChangeListener, ChangeListener { 165 Listener() { 166 } 167 168 public void propertyChange(PropertyChangeEvent ev) { 169 updateStatus(); 170 last.removeChangeListener(this); 171 last = getUndoRedo(); 172 last.addChangeListener(this); 173 } 174 175 public void stateChanged(ChangeEvent ev) { 176 updateStatus(); 177 } 178 } 179 } 180 | Popular Tags |