Added
Link Here
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 1997-2009 Sun Microsystems, Inc. All rights reserved. |
5 |
* |
6 |
* The contents of this file are subject to the terms of either the GNU |
7 |
* General Public License Version 2 only ("GPL") or the Common |
8 |
* Development and Distribution License("CDDL") (collectively, the |
9 |
* "License"). You may not use this file except in compliance with the |
10 |
* License. You can obtain a copy of the License at |
11 |
* http://www.netbeans.org/cddl-gplv2.html |
12 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
13 |
* specific language governing permissions and limitations under the |
14 |
* License. When distributing the software, include this License Header |
15 |
* Notice in each file and include the License file at |
16 |
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this |
17 |
* particular file as subject to the "Classpath" exception as provided |
18 |
* by Sun in the GPL Version 2 section of the License file that |
19 |
* accompanied this code. If applicable, add the following below the |
20 |
* License Header, with the fields enclosed by brackets [] replaced by |
21 |
* your own identifying information: |
22 |
* "Portions Copyrighted [year] [name of copyright owner]" |
23 |
* |
24 |
* Contributor(s): |
25 |
* |
26 |
* The Original Software is NetBeans. The Initial Developer of the Original |
27 |
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2008 Sun |
28 |
* Microsystems, Inc. All Rights Reserved. |
29 |
* |
30 |
* If you wish your version of this file to be governed by only the CDDL |
31 |
* or only the GPL Version 2, indicate your decision by adding |
32 |
* "[Contributor] elects to include this software in this distribution |
33 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
34 |
* single choice of license, a recipient has the option to distribute |
35 |
* your version of this file under either the CDDL, the GPL Version 2 or |
36 |
* to extend the choice of license to its licensees as provided above. |
37 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
38 |
* Version 2 license, then the option applies only if the new code is |
39 |
* made subject to such option by the copyright holder. |
40 |
*/ |
41 |
package org.openide.actions; |
42 |
|
43 |
import java.awt.EventQueue; |
44 |
import java.awt.event.ActionEvent; |
45 |
import javax.swing.Action; |
46 |
import org.openide.awt.UndoRedo; |
47 |
import org.openide.util.HelpCtx; |
48 |
import org.openide.util.LookupEvent; |
49 |
import org.openide.util.NbBundle; |
50 |
import org.openide.windows.TopComponent; |
51 |
import org.openide.windows.TopComponent.Registry; |
52 |
import org.openide.windows.WindowManager; |
53 |
|
54 |
import java.beans.*; |
55 |
import java.util.Map; |
56 |
import java.util.logging.Logger; |
57 |
import java.util.logging.Level; |
58 |
import javax.swing.AbstractAction; |
59 |
|
60 |
import javax.swing.UIManager; |
61 |
import javax.swing.event.*; |
62 |
import javax.swing.undo.*; |
63 |
import org.openide.util.ContextAwareAction; |
64 |
import org.openide.util.Exceptions; |
65 |
import org.openide.util.ImageUtilities; |
66 |
import org.openide.util.Lookup; |
67 |
import org.openide.util.LookupListener; |
68 |
import org.openide.util.Utilities; |
69 |
import org.openide.util.WeakListeners; |
70 |
|
71 |
|
72 |
/** Context aware undo and redo actions. |
73 |
* |
74 |
* @author Jaroslav Tulach |
75 |
*/ |
76 |
final class UndoRedoAction extends AbstractAction |
77 |
implements ContextAwareAction, PropertyChangeListener, ChangeListener, LookupListener, Runnable, HelpCtx.Provider { |
78 |
private static final Logger LOG = Logger.getLogger(UndoRedoAction.class.getName()); |
79 |
/** last edit */ |
80 |
private UndoRedo last = UndoRedo.NONE; |
81 |
private final boolean doUndo; |
82 |
private final Lookup.Result<UndoRedo.Provider> result; |
83 |
private final boolean fallback; |
84 |
private PropertyChangeListener weakPCL; |
85 |
private ChangeListener weakCL; |
86 |
private LookupListener weakLL; |
87 |
|
88 |
|
89 |
UndoRedoAction(Lookup context, boolean doUndo, boolean fallback) { |
90 |
this.doUndo = doUndo; |
91 |
this.fallback = fallback; |
92 |
this.result = context.lookupResult(UndoRedo.Provider.class); |
93 |
} |
94 |
|
95 |
public static Action create(Map<?,?> map) { |
96 |
if (Boolean.TRUE.equals(map.get("redo"))) { // NOI18N |
97 |
return new UndoRedoAction(Utilities.actionsGlobalContext(), false, true); |
98 |
} |
99 |
if (Boolean.TRUE.equals(map.get("undo"))) { // NOI18N |
100 |
return new UndoRedoAction(Utilities.actionsGlobalContext(), true, true); |
101 |
} |
102 |
throw new IllegalStateException(); |
103 |
} |
104 |
|
105 |
|
106 |
@Override |
107 |
public boolean isEnabled() { |
108 |
initializeUndoRedo(); |
109 |
return super.isEnabled(); |
110 |
} |
111 |
|
112 |
void initializeUndoRedo() { |
113 |
assert EventQueue.isDispatchThread(); |
114 |
if (weakLL != null) { |
115 |
return; |
116 |
} |
117 |
String res; |
118 |
if (doUndo) { |
119 |
res = "org/openide/resources/actions/undo.gif"; // NOI18N |
120 |
} else { |
121 |
res = "org/openide/resources/actions/redo.gif"; // NOI18N |
122 |
} |
123 |
putValue("iconBase", res); // NOI18N |
124 |
putValue(SMALL_ICON, ImageUtilities.loadImageIcon(res, true)); |
125 |
if (fallback) { |
126 |
Registry r = WindowManager.getDefault().getRegistry(); |
127 |
weakPCL = WeakListeners.propertyChange(this, r); |
128 |
r.addPropertyChangeListener(weakPCL); |
129 |
} |
130 |
weakCL = WeakListeners.change(this, null); |
131 |
weakLL = WeakListeners.create(LookupListener.class, this, result); |
132 |
result.addLookupListener(weakLL); |
133 |
last = UndoRedo.NONE; |
134 |
|
135 |
run(); |
136 |
} |
137 |
|
138 |
@Override |
139 |
public void run() { |
140 |
if (!EventQueue.isDispatchThread()) { |
141 |
EventQueue.invokeLater(this); |
142 |
return; |
143 |
} |
144 |
|
145 |
UndoRedo ur = getUndoRedo(); |
146 |
last.removeChangeListener(weakCL); |
147 |
|
148 |
if (doUndo) { |
149 |
setEnabled(ur.canUndo()); |
150 |
} else { |
151 |
setEnabled(ur.canRedo()); |
152 |
} |
153 |
putValue(NAME, getName()); |
154 |
|
155 |
last = ur; |
156 |
last.addChangeListener(weakCL); |
157 |
} |
158 |
|
159 |
private UndoRedo getUndoRedo() { |
160 |
assert EventQueue.isDispatchThread(); |
161 |
for (UndoRedo.Provider provider : result.allInstances()) { |
162 |
UndoRedo ur = provider.getUndoRedo(); |
163 |
if (ur != null) { |
164 |
return ur; |
165 |
} |
166 |
} |
167 |
|
168 |
if (fallback) { |
169 |
TopComponent el = WindowManager.getDefault().getRegistry().getActivated(); |
170 |
if (el != null) { |
171 |
UndoRedo ur = el.getUndoRedo(); |
172 |
if (ur != null) { |
173 |
return ur; |
174 |
} |
175 |
} |
176 |
} |
177 |
return UndoRedo.NONE; |
178 |
} |
179 |
|
180 |
private String getName() { |
181 |
assert EventQueue.isDispatchThread(); |
182 |
//#40823 related. AbstractUndoableEdit prepends "Undo/Redo" strings before the custom text, |
183 |
// resulting in repetitive text in UndoAction/RedoAction. attempt to remove the AbstractUndoableEdit text |
184 |
// keeping our text because it has mnemonics. |
185 |
String undo = getUndoRedo().getUndoPresentationName(); |
186 |
LOG.log (Level.FINE, "getUndoRedo().getUndoPresentationName() returns {0}", undo); |
187 |
|
188 |
if ((undo != null) && (getDefaultSwingText() != null) && undo.startsWith(getDefaultSwingText())) { |
189 |
undo = undo.substring(getDefaultSwingText().length()).trim(); |
190 |
} |
191 |
|
192 |
LOG.log (Level.FINE, "Name adapted by SWING_DEFAULT_LABEL is {0}", undo); |
193 |
String presentationName = null; |
194 |
if (undo == null || undo.trim ().length () == 0) { |
195 |
presentationName = NbBundle.getMessage(UndoRedoAction.class, doUndo ? "UndoSimple" : "RedoSimple"); |
196 |
} else { |
197 |
presentationName = NbBundle.getMessage(UndoRedoAction.class, doUndo ? "UndoWithParameter" : "UndoSimple", undo); |
198 |
} |
199 |
|
200 |
LOG.log (Level.FINE, "Result name is {0}", presentationName); |
201 |
|
202 |
return presentationName; |
203 |
} |
204 |
|
205 |
@Override |
206 |
public HelpCtx getHelpCtx() { |
207 |
return new HelpCtx(UndoRedoAction.class); |
208 |
} |
209 |
|
210 |
@Override |
211 |
public void actionPerformed(ActionEvent ev) { |
212 |
UndoRedo undoRedo = getUndoRedo(); |
213 |
if (doUndo) try { |
214 |
if (undoRedo.canUndo()) { |
215 |
undoRedo.undo(); |
216 |
} |
217 |
} catch (CannotUndoException ex) { |
218 |
Exceptions.printStackTrace(ex); |
219 |
} else try { |
220 |
if (undoRedo.canRedo()) { |
221 |
undoRedo.redo(); |
222 |
} |
223 |
} catch (CannotRedoException ex) { |
224 |
Exceptions.printStackTrace(ex); |
225 |
} |
226 |
run(); |
227 |
} |
228 |
|
229 |
@Override |
230 |
public void propertyChange(PropertyChangeEvent ev) { |
231 |
if (TopComponent.Registry.PROP_ACTIVATED.equals(ev.getPropertyName())) { |
232 |
run(); |
233 |
} |
234 |
} |
235 |
|
236 |
@Override |
237 |
public void stateChanged(ChangeEvent ev) { |
238 |
run(); |
239 |
} |
240 |
|
241 |
@Override |
242 |
public void resultChanged(LookupEvent ev) { |
243 |
run(); |
244 |
} |
245 |
|
246 |
@Override |
247 |
public Action createContextAwareInstance(Lookup actionContext) { |
248 |
return new UndoRedoAction(actionContext, doUndo, false); |
249 |
} |
250 |
|
251 |
private String getDefaultSwingText() { |
252 |
return doUndo ? UIManager.getString("AbstractUndoableEdit.undoText") : //NOI18N |
253 |
UIManager.getString("AbstractUndoableEdit.redoText"); //NOI18N |
254 |
} |
255 |
} |