KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > actions > UndoAction


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

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 JavaDoc;
31 import java.util.logging.Level JavaDoc;
32
33 import javax.swing.SwingUtilities JavaDoc;
34 import javax.swing.UIManager JavaDoc;
35 import javax.swing.event.*;
36 import javax.swing.undo.*;
37 import org.openide.util.Exceptions;
38
39
40 /** Undo an edit.
41 *
42 * @see UndoRedo
43 * @author Ian Formanek, Jaroslav Tulach
44 */

45 public class UndoAction extends CallableSystemAction {
46     /** initialized listener */
47     private static Listener listener;
48
49     /** last edit */
50     private static UndoRedo last = UndoRedo.NONE;
51     private static String JavaDoc SWING_DEFAULT_LABEL = UIManager.getString("AbstractUndoableEdit.undoText"); //NOI18N
52
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     /** Initializes the object.
62     */

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     /** Update status of action.
80     */

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 JavaDoc() {
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     /** Finds current undo/redo.
108     */

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 JavaDoc getName() {
116         //#40823 related. AbstractUndoableEdit prepends "Undo/Redo" strings before the custom text,
117
// resulting in repetitive text in UndoAction/RedoAction. attempt to remove the AbstractUndoableEdit text
118
// keeping our text because it has mnemonics.
119
String JavaDoc 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 JavaDoc 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 JavaDoc iconResource() {
140         return "org/openide/resources/actions/undo.gif"; // NOI18N
141
}
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     /** Listener on changes of selected workspace element and
162     * its changes.
163     */

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