2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
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]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
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.
41
package org.netbeans.modules.refactoring.java.ui;
43
import java.awt.Component;
44
import java.awt.event.ActionEvent;
45
import java.beans.PropertyChangeListener;
46
import java.lang.reflect.Method;
47
import java.util.Hashtable;
48
import javax.swing.Action;
49
import javax.swing.Icon;
50
import javax.swing.JMenuItem;
51
import org.openide.awt.Actions;
52
import org.openide.cookies.EditorCookie;
53
import org.openide.loaders.DataObject;
54
import org.openide.nodes.Node;
55
import org.openide.text.CloneableEditorSupport.Pane;
56
import org.openide.util.HelpCtx;
57
import org.openide.util.Lookup;
58
import org.openide.util.actions.CallableSystemAction;
59
import org.openide.util.actions.NodeAction;
60
import org.openide.util.actions.Presenter;
61
import org.openide.util.lookup.AbstractLookup;
62
import org.openide.util.lookup.InstanceContent;
63
import org.openide.windows.TopComponent;
67
* JavaRefactoringGlobalAction
68
* This class is copy of RefactoringGlobalAction, which is not in public packages
71
public abstract class JavaRefactoringGlobalAction extends NodeAction {
73
/** Creates a new JavaRefactoringGlobalActiongGlobalAction */
74
public JavaRefactoringGlobalAction(String name, Icon icon) {
79
public final String getName() {
80
return (String) getValue(Action.NAME);
83
protected void setName(String name) {
84
putValue(Action.NAME, name);
87
protected void setMnemonic(char m) {
88
putValue(Action.MNEMONIC_KEY, new Integer(m));
91
private static String trim(String arg) {
92
arg = org.openide.util.Utilities.replaceString(arg, "&", ""); // NOI18N
93
return org.openide.util.Utilities.replaceString(arg, "...", ""); // NOI18N
96
public org.openide.util.HelpCtx getHelpCtx() {
97
return HelpCtx.DEFAULT_HELP;
100
protected Lookup getLookup(Node[] n) {
101
InstanceContent ic = new InstanceContent();
105
EditorCookie tc = getTextComponent(n[0]);
110
ic.add(new Hashtable(0));
111
return new AbstractLookup(ic);
115
protected static EditorCookie getTextComponent(Node n) {
116
DataObject dobj = n.getCookie(DataObject.class);
118
EditorCookie ec = dobj.getCookie(EditorCookie.class);
120
TopComponent activetc = TopComponent.getRegistry().getActivated();
121
if (activetc instanceof Pane) {
129
public abstract void performAction(Lookup context);
131
protected abstract boolean enable(Lookup context);
133
public final void performAction(final Node[] activatedNodes) {
134
performAction(getLookup(activatedNodes));
137
protected boolean enable(Node[] activatedNodes) {
138
return enable(getLookup(activatedNodes));
143
public Action createContextAwareInstance(Lookup actionContext) {
144
return new ContextAction(actionContext);
147
public class ContextAction implements Action, Presenter.Menu, Presenter.Popup, Presenter.Toolbar {
151
public ContextAction(Lookup context) {
152
this.context=context;
155
public Object getValue(String arg0) {
156
return JavaRefactoringGlobalAction.this.getValue(arg0);
159
public void putValue(String arg0, Object arg1) {
160
JavaRefactoringGlobalAction.this.putValue(arg0, arg1);
163
public void setEnabled(boolean arg0) {
164
JavaRefactoringGlobalAction.this.setEnabled(arg0);
167
public boolean isEnabled() {
168
return enable(context);
171
public void addPropertyChangeListener(PropertyChangeListener arg0) {
172
JavaRefactoringGlobalAction.this.addPropertyChangeListener(arg0);
175
public void removePropertyChangeListener(PropertyChangeListener arg0) {
176
JavaRefactoringGlobalAction.this.removePropertyChangeListener(arg0);
179
public void actionPerformed(ActionEvent arg0) {
180
JavaRefactoringGlobalAction.this.performAction(context);
182
public JMenuItem getMenuPresenter() {
183
if (isMethodOverridden(JavaRefactoringGlobalAction.this, "getMenuPresenter")) { // NOI18N
185
return JavaRefactoringGlobalAction.this.getMenuPresenter();
187
return new Actions.MenuItem(this, true);
191
public JMenuItem getPopupPresenter() {
192
if (isMethodOverridden(JavaRefactoringGlobalAction.this, "getPopupPresenter")) { // NOI18N
194
return JavaRefactoringGlobalAction.this.getPopupPresenter();
196
return new Actions.MenuItem(this, false);
200
public Component getToolbarPresenter() {
201
if (isMethodOverridden(JavaRefactoringGlobalAction.this, "getToolbarPresenter")) { // NOI18N
203
return JavaRefactoringGlobalAction.this.getToolbarPresenter();
205
return new Actions.ToolbarButton(this);
209
private boolean isMethodOverridden(NodeAction d, String name) {
211
Method m = d.getClass().getMethod(name, new Class[0]);
213
return m.getDeclaringClass() != CallableSystemAction.class;
214
} catch (java.lang.NoSuchMethodException ex) {
215
ex.printStackTrace();
216
throw new IllegalStateException("Error searching for method " + name + " in " + d); // NOI18N