KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > refactoring > java > ui > JavaRefactoringGlobalAction


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.netbeans.modules.refactoring.java.ui;
20
21 import java.awt.Component JavaDoc;
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.lang.reflect.Method JavaDoc;
25 import java.util.Hashtable JavaDoc;
26 import javax.swing.Action JavaDoc;
27 import javax.swing.Icon JavaDoc;
28 import javax.swing.JMenuItem JavaDoc;
29 import org.openide.awt.Actions;
30 import org.openide.cookies.EditorCookie;
31 import org.openide.loaders.DataObject;
32 import org.openide.nodes.Node;
33 import org.openide.text.CloneableEditorSupport.Pane;
34 import org.openide.util.HelpCtx;
35 import org.openide.util.Lookup;
36 import org.openide.util.actions.CallableSystemAction;
37 import org.openide.util.actions.NodeAction;
38 import org.openide.util.actions.Presenter;
39 import org.openide.util.lookup.AbstractLookup;
40 import org.openide.util.lookup.InstanceContent;
41 import org.openide.windows.TopComponent;
42
43 /**
44  *
45  * JavaRefactoringGlobalAction
46  * This class is copy of RefactoringGlobalAction, which is not in public packages
47  * @author Jan Becicka
48  */

49 public abstract class JavaRefactoringGlobalAction extends NodeAction {
50
51     /** Creates a new JavaRefactoringGlobalActiongGlobalAction */
52     public JavaRefactoringGlobalAction(String JavaDoc name, Icon JavaDoc icon) {
53         setName(name);
54         setIcon(icon);
55     }
56     
57     public final String JavaDoc getName() {
58         return (String JavaDoc) getValue(Action.NAME);
59     }
60     
61     protected void setName(String JavaDoc name) {
62         putValue(Action.NAME, name);
63     }
64     
65     protected void setMnemonic(char m) {
66         putValue(Action.MNEMONIC_KEY, new Integer JavaDoc(m));
67     }
68     
69     private static String JavaDoc trim(String JavaDoc arg) {
70         arg = org.openide.util.Utilities.replaceString(arg, "&", ""); // NOI18N
71
return org.openide.util.Utilities.replaceString(arg, "...", ""); // NOI18N
72
}
73     
74     public org.openide.util.HelpCtx getHelpCtx() {
75         return HelpCtx.DEFAULT_HELP;
76     }
77
78     protected Lookup getLookup(Node[] n) {
79         InstanceContent ic = new InstanceContent();
80         for (Node node:n)
81             ic.add(node);
82         if (n.length>0) {
83             EditorCookie tc = getTextComponent(n[0]);
84             if (tc != null) {
85                 ic.add(tc);
86             }
87         }
88         ic.add(new Hashtable JavaDoc(0));
89         return new AbstractLookup(ic);
90     }
91
92     
93     protected static EditorCookie getTextComponent(Node n) {
94         DataObject dobj = (DataObject) n.getCookie(DataObject.class);
95         if (dobj != null) {
96             EditorCookie ec = (EditorCookie) dobj.getCookie(EditorCookie.class);
97             if (ec != null) {
98                 TopComponent activetc = TopComponent.getRegistry().getActivated();
99                 if (activetc instanceof Pane) {
100                     return ec;
101                 }
102             }
103         }
104         return null;
105     }
106     
107     public abstract void performAction(Lookup context);
108     
109     protected abstract boolean enable(Lookup context);
110     
111     public final void performAction(final Node[] activatedNodes) {
112         performAction(getLookup(activatedNodes));
113     }
114
115     protected boolean enable(Node[] activatedNodes) {
116         return enable(getLookup(activatedNodes));
117     }
118     
119     
120     @Override JavaDoc
121     public Action JavaDoc createContextAwareInstance(Lookup actionContext) {
122         return new ContextAction(actionContext);
123     }
124     
125     public class ContextAction implements Action JavaDoc, Presenter.Menu, Presenter.Popup, Presenter.Toolbar {
126
127         Lookup context;
128
129         public ContextAction(Lookup context) {
130             this.context=context;
131         }
132         
133         public Object JavaDoc getValue(String JavaDoc arg0) {
134             return JavaRefactoringGlobalAction.this.getValue(arg0);
135         }
136         
137         public void putValue(String JavaDoc arg0, Object JavaDoc arg1) {
138             JavaRefactoringGlobalAction.this.putValue(arg0, arg1);
139         }
140         
141         public void setEnabled(boolean arg0) {
142             JavaRefactoringGlobalAction.this.setEnabled(arg0);
143         }
144         
145         public boolean isEnabled() {
146             return enable(context);
147         }
148         
149         public void addPropertyChangeListener(PropertyChangeListener JavaDoc arg0) {
150             JavaRefactoringGlobalAction.this.addPropertyChangeListener(arg0);
151         }
152         
153         public void removePropertyChangeListener(PropertyChangeListener JavaDoc arg0) {
154             JavaRefactoringGlobalAction.this.removePropertyChangeListener(arg0);
155         }
156         
157         public void actionPerformed(ActionEvent JavaDoc arg0) {
158             JavaRefactoringGlobalAction.this.performAction(context);
159         }
160         public JMenuItem JavaDoc getMenuPresenter() {
161             if (isMethodOverridden(JavaRefactoringGlobalAction.this, "getMenuPresenter")) { // NOI18N
162

163                 return JavaRefactoringGlobalAction.this.getMenuPresenter();
164             } else {
165                 return new Actions.MenuItem(this, true);
166             }
167         }
168
169         public JMenuItem JavaDoc getPopupPresenter() {
170             if (isMethodOverridden(JavaRefactoringGlobalAction.this, "getPopupPresenter")) { // NOI18N
171

172                 return JavaRefactoringGlobalAction.this.getPopupPresenter();
173             } else {
174                 return new Actions.MenuItem(this, false);
175             }
176         }
177
178         public Component JavaDoc getToolbarPresenter() {
179             if (isMethodOverridden(JavaRefactoringGlobalAction.this, "getToolbarPresenter")) { // NOI18N
180

181                 return JavaRefactoringGlobalAction.this.getToolbarPresenter();
182             } else {
183                 return new Actions.ToolbarButton(this);
184             }
185         }
186
187         private boolean isMethodOverridden(NodeAction d, String JavaDoc name) {
188             try {
189                 Method JavaDoc m = d.getClass().getMethod(name, new Class JavaDoc[0]);
190
191                 return m.getDeclaringClass() != CallableSystemAction.class;
192             } catch (java.lang.NoSuchMethodException JavaDoc ex) {
193                 ex.printStackTrace();
194                 throw new IllegalStateException JavaDoc("Error searching for method " + name + " in " + d); // NOI18N
195
}
196         }
197     }
198 }
199
Popular Tags