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.spi.impl;
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 javax.swing.Action;
048: import javax.swing.Icon;
049: import javax.swing.JMenuItem;
050: import org.openide.awt.Actions;
051: import org.openide.cookies.EditorCookie;
052: import org.openide.loaders.DataObject;
053: import org.openide.nodes.Node;
054: import org.openide.text.CloneableEditorSupport.Pane;
055: import org.openide.util.HelpCtx;
056: import org.openide.util.Lookup;
057: import org.openide.util.actions.CallableSystemAction;
058: import org.openide.util.actions.NodeAction;
059: import org.openide.util.actions.Presenter;
060: import org.openide.util.lookup.AbstractLookup;
061: import org.openide.util.lookup.InstanceContent;
062: import org.openide.windows.TopComponent;
063:
064: /**
065: * This action should be in public package probably.
066: * There is copy of this action: JavaRefactoringGlobal action
067: * @author Jan Becicka
068: */
069: public abstract class RefactoringGlobalAction extends NodeAction {
070:
071: /** Creates a new instance of RefactoringGlobalAction */
072: public RefactoringGlobalAction(String name, Icon icon) {
073: setName(name);
074: setIcon(icon);
075: }
076:
077: public final String getName() {
078: return (String) getValue(Action.NAME);
079: }
080:
081: protected void setName(String name) {
082: putValue(Action.NAME, name);
083: }
084:
085: protected void setMnemonic(char m) {
086: putValue(Action.MNEMONIC_KEY, new Integer(m));
087: }
088:
089: private static String trim(String arg) {
090: arg = org.openide.util.Utilities.replaceString(arg, "&", ""); // NOI18N
091: return org.openide.util.Utilities.replaceString(arg, "...", ""); // NOI18N
092: }
093:
094: public org.openide.util.HelpCtx getHelpCtx() {
095: return HelpCtx.DEFAULT_HELP;
096: }
097:
098: protected Lookup getLookup(Node[] n) {
099: InstanceContent ic = new InstanceContent();
100: for (Node node : n)
101: ic.add(node);
102: if (n.length > 0) {
103: EditorCookie tc = getTextComponent(n[0]);
104: if (tc != null) {
105: ic.add(tc);
106: }
107: }
108: return new AbstractLookup(ic);
109: }
110:
111: protected static EditorCookie getTextComponent(Node n) {
112: DataObject dobj = (DataObject) n.getCookie(DataObject.class);
113: if (dobj != null) {
114: EditorCookie ec = (EditorCookie) dobj
115: .getCookie(EditorCookie.class);
116: if (ec != null) {
117: TopComponent activetc = TopComponent.getRegistry()
118: .getActivated();
119: if (activetc instanceof Pane) {
120: return ec;
121: }
122: }
123: }
124: return null;
125: }
126:
127: public abstract void performAction(Lookup context);
128:
129: protected abstract boolean enable(Lookup context);
130:
131: public final void performAction(final Node[] activatedNodes) {
132: performAction(getLookup(activatedNodes));
133: }
134:
135: protected boolean enable(Node[] activatedNodes) {
136: return enable(getLookup(activatedNodes));
137: }
138:
139: @Override
140: public Action createContextAwareInstance(Lookup actionContext) {
141: return new ContextAction(actionContext);
142: }
143:
144: public class ContextAction implements Action, Presenter.Menu,
145: Presenter.Popup, Presenter.Toolbar {
146:
147: Lookup context;
148:
149: public ContextAction(Lookup context) {
150: this .context = context;
151: }
152:
153: public Object getValue(String arg0) {
154: return RefactoringGlobalAction.this .getValue(arg0);
155: }
156:
157: public void putValue(String arg0, Object arg1) {
158: RefactoringGlobalAction.this .putValue(arg0, arg1);
159: }
160:
161: public void setEnabled(boolean arg0) {
162: RefactoringGlobalAction.this .setEnabled(arg0);
163: }
164:
165: public boolean isEnabled() {
166: return enable(context);
167: }
168:
169: public void addPropertyChangeListener(
170: PropertyChangeListener arg0) {
171: RefactoringGlobalAction.this
172: .addPropertyChangeListener(arg0);
173: }
174:
175: public void removePropertyChangeListener(
176: PropertyChangeListener arg0) {
177: RefactoringGlobalAction.this
178: .removePropertyChangeListener(arg0);
179: }
180:
181: public void actionPerformed(ActionEvent arg0) {
182: RefactoringGlobalAction.this .performAction(context);
183: }
184:
185: public JMenuItem getMenuPresenter() {
186: if (isMethodOverridden(RefactoringGlobalAction.this ,
187: "getMenuPresenter")) { // NOI18N
188:
189: return RefactoringGlobalAction.this .getMenuPresenter();
190: } else {
191: return new Actions.MenuItem(this , true);
192: }
193: }
194:
195: public JMenuItem getPopupPresenter() {
196: if (isMethodOverridden(RefactoringGlobalAction.this ,
197: "getPopupPresenter")) { // NOI18N
198:
199: return RefactoringGlobalAction.this .getPopupPresenter();
200: } else {
201: return new Actions.MenuItem(this , false);
202: }
203: }
204:
205: public Component getToolbarPresenter() {
206: if (isMethodOverridden(RefactoringGlobalAction.this ,
207: "getToolbarPresenter")) { // NOI18N
208:
209: return RefactoringGlobalAction.this
210: .getToolbarPresenter();
211: } else {
212: return new Actions.ToolbarButton(this );
213: }
214: }
215:
216: private boolean isMethodOverridden(NodeAction d, String name) {
217: try {
218: Method m = d.getClass().getMethod(name, new Class[0]);
219:
220: return m.getDeclaringClass() != CallableSystemAction.class;
221: } catch (java.lang.NoSuchMethodException ex) {
222: ex.printStackTrace();
223: throw new IllegalStateException(
224: "Error searching for method " + name + " in "
225: + d); // NOI18N
226: }
227: }
228: }
229: }
|