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