~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to refactoring/java/src/org/netbeans/modules/refactoring/java/ui/JavaRefactoringGlobalAction.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 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-2006 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.netbeans.modules.refactoring.java.ui;
 
42
 
 
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;
 
64
 
 
65
/**
 
66
 * 
 
67
 * JavaRefactoringGlobalAction
 
68
 * This class is copy of RefactoringGlobalAction, which is not in public packages
 
69
 * @author Jan Becicka
 
70
 */
 
71
public abstract class JavaRefactoringGlobalAction extends NodeAction {
 
72
 
 
73
    /** Creates a new JavaRefactoringGlobalActiongGlobalAction */
 
74
    public JavaRefactoringGlobalAction(String name, Icon icon) {
 
75
        setName(name);
 
76
        setIcon(icon);
 
77
    }
 
78
    
 
79
    public final String getName() {
 
80
        return (String) getValue(Action.NAME);
 
81
    }
 
82
    
 
83
    protected void setName(String name) {
 
84
        putValue(Action.NAME, name);
 
85
    }
 
86
    
 
87
    protected void setMnemonic(char m) {
 
88
        putValue(Action.MNEMONIC_KEY, new Integer(m));
 
89
    }
 
90
    
 
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
 
94
    }
 
95
    
 
96
    public org.openide.util.HelpCtx getHelpCtx() {
 
97
        return HelpCtx.DEFAULT_HELP;
 
98
    }
 
99
 
 
100
    protected Lookup getLookup(Node[] n) {
 
101
        InstanceContent ic = new InstanceContent();
 
102
        for (Node node:n)
 
103
            ic.add(node);
 
104
        if (n.length>0) {
 
105
            EditorCookie tc = getTextComponent(n[0]);
 
106
            if (tc != null) {
 
107
                ic.add(tc);
 
108
            }
 
109
        }
 
110
        ic.add(new Hashtable(0));
 
111
        return new AbstractLookup(ic);
 
112
    }
 
113
 
 
114
    
 
115
    protected static EditorCookie getTextComponent(Node n) {
 
116
        DataObject dobj = n.getCookie(DataObject.class);
 
117
        if (dobj != null) {
 
118
            EditorCookie ec = dobj.getCookie(EditorCookie.class);
 
119
            if (ec != null) {
 
120
                TopComponent activetc = TopComponent.getRegistry().getActivated();
 
121
                if (activetc instanceof Pane) {
 
122
                    return ec;
 
123
                }
 
124
            }
 
125
        }
 
126
        return null;
 
127
    }
 
128
    
 
129
    public abstract void performAction(Lookup context);
 
130
    
 
131
    protected abstract boolean enable(Lookup context);
 
132
    
 
133
    public final void performAction(final Node[] activatedNodes) {
 
134
        performAction(getLookup(activatedNodes));
 
135
    }
 
136
 
 
137
    protected boolean enable(Node[] activatedNodes) {
 
138
        return enable(getLookup(activatedNodes));
 
139
    }
 
140
    
 
141
    
 
142
    @Override
 
143
    public Action createContextAwareInstance(Lookup actionContext) {
 
144
        return new ContextAction(actionContext);
 
145
    }
 
146
    
 
147
    public class ContextAction implements Action, Presenter.Menu, Presenter.Popup, Presenter.Toolbar {
 
148
 
 
149
        Lookup context;
 
150
 
 
151
        public ContextAction(Lookup context) {
 
152
            this.context=context;
 
153
        }
 
154
        
 
155
        public Object getValue(String arg0) {
 
156
            return JavaRefactoringGlobalAction.this.getValue(arg0);
 
157
        }
 
158
        
 
159
        public void putValue(String arg0, Object arg1) {
 
160
            JavaRefactoringGlobalAction.this.putValue(arg0, arg1);
 
161
        }
 
162
        
 
163
        public void setEnabled(boolean arg0) {
 
164
            JavaRefactoringGlobalAction.this.setEnabled(arg0);
 
165
        }
 
166
        
 
167
        public boolean isEnabled() {
 
168
            return enable(context);
 
169
        }
 
170
        
 
171
        public void addPropertyChangeListener(PropertyChangeListener arg0) {
 
172
            JavaRefactoringGlobalAction.this.addPropertyChangeListener(arg0);
 
173
        }
 
174
        
 
175
        public void removePropertyChangeListener(PropertyChangeListener arg0) {
 
176
            JavaRefactoringGlobalAction.this.removePropertyChangeListener(arg0);
 
177
        }
 
178
        
 
179
        public void actionPerformed(ActionEvent arg0) {
 
180
            JavaRefactoringGlobalAction.this.performAction(context);
 
181
        }
 
182
        public JMenuItem getMenuPresenter() {
 
183
            if (isMethodOverridden(JavaRefactoringGlobalAction.this, "getMenuPresenter")) { // NOI18N
 
184
 
 
185
                return JavaRefactoringGlobalAction.this.getMenuPresenter();
 
186
            } else {
 
187
                return new Actions.MenuItem(this, true);
 
188
            }
 
189
        }
 
190
 
 
191
        public JMenuItem getPopupPresenter() {
 
192
            if (isMethodOverridden(JavaRefactoringGlobalAction.this, "getPopupPresenter")) { // NOI18N
 
193
 
 
194
                return JavaRefactoringGlobalAction.this.getPopupPresenter();
 
195
            } else {
 
196
                return new Actions.MenuItem(this, false);
 
197
            }
 
198
        }
 
199
 
 
200
        public Component getToolbarPresenter() {
 
201
            if (isMethodOverridden(JavaRefactoringGlobalAction.this, "getToolbarPresenter")) { // NOI18N
 
202
 
 
203
                return JavaRefactoringGlobalAction.this.getToolbarPresenter();
 
204
            } else {
 
205
                return new Actions.ToolbarButton(this);
 
206
            }
 
207
        }
 
208
 
 
209
        private boolean isMethodOverridden(NodeAction d, String name) {
 
210
            try {
 
211
                Method m = d.getClass().getMethod(name, new Class[0]);
 
212
 
 
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
 
217
            }
 
218
        }        
 
219
    }
 
220
}