1 19 package org.netbeans.modules.xml.refactoring.ui.j.spi.ui; 20 21 import java.awt.event.ActionEvent ; 22 import javax.swing.Action ; 23 import javax.swing.Icon ; 24 import javax.swing.JEditorPane ; 25 import javax.swing.text.JTextComponent ; 26 import javax.swing.text.TextAction ; 27 import org.openide.cookies.EditorCookie; 34 import org.openide.loaders.DataObject; 35 import org.openide.nodes.Node; 36 import org.openide.text.CloneableEditorSupport; 37 import org.openide.util.HelpCtx; 38 import org.openide.util.actions.NodeAction; 39 import org.openide.windows.TopComponent; 40 41 46 public abstract class AbstractRefactoringAction extends NodeAction implements RefactoringAction, Runnable { 47 private Node[] activatedNodes; 48 private int caretPosition; 49 private int startSelectionPosition; 50 private int endSelectionPosition; 51 private static DelegateTextAction delegate = new DelegateTextAction(); 52 53 54 public AbstractRefactoringAction(String name, Icon icon) { 55 setName(name); 56 setIcon(icon); 57 } 58 59 public final String getName() { 60 return (String ) getValue(Action.NAME); 61 } 62 63 protected void setName(String name) { 64 putValue(Action.NAME, name); 65 } 66 67 protected void setMnemonic(char m) { 68 putValue(Action.MNEMONIC_KEY, new Integer (m)); 69 } 70 71 public final void performAction(final Node[] n) { 72 new Runnable () { 73 public void run() { 74 processContext(n, delegate.getTextComponent()); 75 AbstractRefactoringAction.this.run(); 76 } 77 }; 78 } 80 81 private boolean invokedExternally = false; 82 92 private static String trim(String arg) { 93 arg = org.openide.util.Utilities.replaceString(arg, "&", ""); return org.openide.util.Utilities.replaceString(arg, "...", ""); } 96 97 protected abstract boolean enabled(Node[] activatedNodes); 98 99 108 110 114 public final void processContext(Node[] activatedNodes, JTextComponent textComponent) { 115 this.activatedNodes = activatedNodes; 116 117 setCaretPosition(textComponent); 118 boolean e = enabled(activatedNodes); 119 setEnabled(e); 121 } 123 124 private void setCaretPosition(JTextComponent textComponent) { 125 caretPosition = -1; 126 if (activatedNodes == null || textComponent == null || invokedExternally) 127 return; 128 DataObject dobj = (DataObject) activatedNodes[0].getCookie(DataObject.class); 130 if (dobj != null) { 131 EditorCookie ec = (EditorCookie) dobj.getCookie(EditorCookie.class); 132 if (ec != null) { 133 TopComponent activetc = TopComponent.getRegistry().getActivated(); 134 if (activetc instanceof CloneableEditorSupport.Pane) { 135 JEditorPane pane=((CloneableEditorSupport.Pane)activetc).getEditorPane(); 136 137 if (textComponent.equals(pane)) { 138 caretPosition = textComponent.getCaretPosition(); 139 startSelectionPosition = textComponent.getSelectionStart(); 140 endSelectionPosition = textComponent.getSelectionEnd(); 141 142 if (startSelectionPosition != endSelectionPosition && endSelectionPosition == caretPosition) { 144 caretPosition--; 145 } 146 } 147 } 148 } 149 } 150 } 151 152 public final void run() { 153 } 190 191 public boolean enable(Node[]a) { 192 return enabled(a); 193 } 194 195 public org.openide.util.HelpCtx getHelpCtx() { 196 return HelpCtx.DEFAULT_HELP; 197 } 198 199 protected void firePropertyChange(String name, Object oldValue, Object newValue) { 200 if (PROP_ENABLED.equals(name)) { 201 if (newValue != null) { 202 super.firePropertyChange(name, oldValue, newValue); 203 } 204 } else { 205 super.firePropertyChange(name, oldValue, newValue); 206 } 207 } 208 209 protected boolean asynchronous() { 210 return false; 211 } 212 213 217 private static final class DelegateTextAction extends TextAction { 218 public static final long serialVersionUID = 1L; 219 220 public DelegateTextAction() { 221 super(""); 222 } 223 224 public void actionPerformed(ActionEvent a) { 225 } 226 227 public JTextComponent getTextComponent() { 228 return getFocusedComponent(); 229 } 230 }; 231 232 } 233 | Popular Tags |