1 19 package org.netbeans.modules.refactoring.spi.impl; 20 21 import java.awt.Component ; 22 import java.awt.event.ActionEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.lang.reflect.Method ; 25 import java.util.Hashtable ; 26 import javax.swing.Action ; 27 import javax.swing.Icon ; 28 import javax.swing.JMenuItem ; 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 48 public abstract class RefactoringGlobalAction extends NodeAction { 49 50 51 public RefactoringGlobalAction(String name, Icon icon) { 52 setName(name); 53 setIcon(icon); 54 } 55 56 public final String getName() { 57 return (String ) getValue(Action.NAME); 58 } 59 60 protected void setName(String name) { 61 putValue(Action.NAME, name); 62 } 63 64 protected void setMnemonic(char m) { 65 putValue(Action.MNEMONIC_KEY, new Integer (m)); 66 } 67 68 private static String trim(String arg) { 69 arg = org.openide.util.Utilities.replaceString(arg, "&", ""); return org.openide.util.Utilities.replaceString(arg, "...", ""); } 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 (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 120 public Action createContextAwareInstance(Lookup actionContext) { 121 return new ContextAction(actionContext); 122 } 123 124 public class ContextAction implements Action , 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 getValue(String arg0) { 133 return RefactoringGlobalAction.this.getValue(arg0); 134 } 135 136 public void putValue(String arg0, Object 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 arg0) { 149 RefactoringGlobalAction.this.addPropertyChangeListener(arg0); 150 } 151 152 public void removePropertyChangeListener(PropertyChangeListener arg0) { 153 RefactoringGlobalAction.this.removePropertyChangeListener(arg0); 154 } 155 156 public void actionPerformed(ActionEvent arg0) { 157 RefactoringGlobalAction.this.performAction(context); 158 } 159 public JMenuItem getMenuPresenter() { 160 if (isMethodOverridden(RefactoringGlobalAction.this, "getMenuPresenter")) { 162 return RefactoringGlobalAction.this.getMenuPresenter(); 163 } else { 164 return new Actions.MenuItem(this, true); 165 } 166 } 167 168 public JMenuItem getPopupPresenter() { 169 if (isMethodOverridden(RefactoringGlobalAction.this, "getPopupPresenter")) { 171 return RefactoringGlobalAction.this.getPopupPresenter(); 172 } else { 173 return new Actions.MenuItem(this, false); 174 } 175 } 176 177 public Component getToolbarPresenter() { 178 if (isMethodOverridden(RefactoringGlobalAction.this, "getToolbarPresenter")) { 180 return RefactoringGlobalAction.this.getToolbarPresenter(); 181 } else { 182 return new Actions.ToolbarButton(this); 183 } 184 } 185 186 private boolean isMethodOverridden(NodeAction d, String name) { 187 try { 188 Method m = d.getClass().getMethod(name, new Class [0]); 189 190 return m.getDeclaringClass() != CallableSystemAction.class; 191 } catch (java.lang.NoSuchMethodException ex) { 192 ex.printStackTrace(); 193 throw new IllegalStateException ("Error searching for method " + name + " in " + d); } 195 } 196 } 197 } 198 | Popular Tags |