001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.refactoring.java.ui;
042:
043: import java.awt.Component;
044: import java.awt.event.ActionEvent;
045: import java.beans.PropertyChangeListener;
046: import java.lang.reflect.Method;
047: import java.util.Hashtable;
048: import javax.swing.Action;
049: import javax.swing.Icon;
050: import javax.swing.JMenuItem;
051: import org.openide.awt.Actions;
052: import org.openide.cookies.EditorCookie;
053: import org.openide.loaders.DataObject;
054: import org.openide.nodes.Node;
055: import org.openide.text.CloneableEditorSupport.Pane;
056: import org.openide.util.HelpCtx;
057: import org.openide.util.Lookup;
058: import org.openide.util.actions.CallableSystemAction;
059: import org.openide.util.actions.NodeAction;
060: import org.openide.util.actions.Presenter;
061: import org.openide.util.lookup.AbstractLookup;
062: import org.openide.util.lookup.InstanceContent;
063: import org.openide.windows.TopComponent;
064:
065: /**
066: *
067: * JavaRefactoringGlobalAction
068: * This class is copy of RefactoringGlobalAction, which is not in public packages
069: * @author Jan Becicka
070: */
071: public abstract class JavaRefactoringGlobalAction extends NodeAction {
072:
073: /** Creates a new JavaRefactoringGlobalActiongGlobalAction */
074: public JavaRefactoringGlobalAction(String name, Icon icon) {
075: setName(name);
076: setIcon(icon);
077: }
078:
079: public final String getName() {
080: return (String) getValue(Action.NAME);
081: }
082:
083: protected void setName(String name) {
084: putValue(Action.NAME, name);
085: }
086:
087: protected void setMnemonic(char m) {
088: putValue(Action.MNEMONIC_KEY, new Integer(m));
089: }
090:
091: private static String trim(String arg) {
092: arg = org.openide.util.Utilities.replaceString(arg, "&", ""); // NOI18N
093: return org.openide.util.Utilities.replaceString(arg, "...", ""); // NOI18N
094: }
095:
096: public org.openide.util.HelpCtx getHelpCtx() {
097: return HelpCtx.DEFAULT_HELP;
098: }
099:
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: protected static EditorCookie getTextComponent(Node n) {
115: DataObject dobj = n.getCookie(DataObject.class);
116: if (dobj != null) {
117: EditorCookie ec = dobj.getCookie(EditorCookie.class);
118: if (ec != null) {
119: TopComponent activetc = TopComponent.getRegistry()
120: .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: @Override
142: public Action createContextAwareInstance(Lookup actionContext) {
143: return new ContextAction(actionContext);
144: }
145:
146: public class ContextAction implements Action, Presenter.Menu,
147: 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(
172: PropertyChangeListener arg0) {
173: JavaRefactoringGlobalAction.this
174: .addPropertyChangeListener(arg0);
175: }
176:
177: public void removePropertyChangeListener(
178: PropertyChangeListener arg0) {
179: JavaRefactoringGlobalAction.this
180: .removePropertyChangeListener(arg0);
181: }
182:
183: public void actionPerformed(ActionEvent arg0) {
184: JavaRefactoringGlobalAction.this .performAction(context);
185: }
186:
187: public JMenuItem getMenuPresenter() {
188: if (isMethodOverridden(JavaRefactoringGlobalAction.this ,
189: "getMenuPresenter")) { // NOI18N
190:
191: return JavaRefactoringGlobalAction.this
192: .getMenuPresenter();
193: } else {
194: return new Actions.MenuItem(this , true);
195: }
196: }
197:
198: public JMenuItem getPopupPresenter() {
199: if (isMethodOverridden(JavaRefactoringGlobalAction.this ,
200: "getPopupPresenter")) { // NOI18N
201:
202: return JavaRefactoringGlobalAction.this
203: .getPopupPresenter();
204: } else {
205: return new Actions.MenuItem(this , false);
206: }
207: }
208:
209: public Component getToolbarPresenter() {
210: if (isMethodOverridden(JavaRefactoringGlobalAction.this ,
211: "getToolbarPresenter")) { // NOI18N
212:
213: return JavaRefactoringGlobalAction.this
214: .getToolbarPresenter();
215: } else {
216: return new Actions.ToolbarButton(this );
217: }
218: }
219:
220: private boolean isMethodOverridden(NodeAction d, String name) {
221: try {
222: Method m = d.getClass().getMethod(name, new Class[0]);
223:
224: return m.getDeclaringClass() != CallableSystemAction.class;
225: } catch (java.lang.NoSuchMethodException ex) {
226: ex.printStackTrace();
227: throw new IllegalStateException(
228: "Error searching for method " + name + " in "
229: + d); // NOI18N
230: }
231: }
232: }
233: }
|