KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > refactoring > spi > impl > RefactoringGlobalAction


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.spi.impl;
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  * This action should be in public package probably.
45  * There is copy of this action: JavaRefactoringGlobal action
46  * @author Jan Becicka
47  */

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

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

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

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