1 19 20 package org.netbeans.modules.languages.javascript.refactoring; 21 22 import javax.swing.JEditorPane ; 23 import javax.swing.text.Document ; 24 import org.netbeans.modules.languages.javascript.*; 25 import java.util.Collection ; 26 import javax.swing.text.JTextComponent ; 27 import org.netbeans.api.languages.ASTNode; 28 import org.netbeans.api.languages.ASTPath; 29 import org.netbeans.api.languages.ParseException; 30 import org.netbeans.api.languages.ParserManager; 31 import org.netbeans.modules.editor.NbEditorDocument; 32 import org.netbeans.modules.refactoring.spi.ui.ActionsImplementationProvider; 33 import org.netbeans.modules.refactoring.spi.ui.RefactoringUI; 34 import org.netbeans.modules.refactoring.spi.ui.UI; 35 import org.openide.cookies.EditorCookie; 36 import org.openide.filesystems.FileObject; 37 import org.openide.loaders.DataObject; 38 import org.openide.util.Lookup; 39 import org.openide.nodes.Node; 40 import org.openide.text.CloneableEditorSupport; 41 import org.openide.windows.TopComponent; 42 43 47 public class RefactoringActionsProvider extends ActionsImplementationProvider { 48 49 private static final String JS_MIME_TYPE = "text/javascript"; 51 public boolean canFindUsages(Lookup lookup) { 52 return canRefactor(lookup); 53 } 54 55 public boolean canRename(Lookup lookup) { 56 return canRefactor(lookup); 57 } 58 59 public void doFindUsages(Lookup lookup) { 60 FileObject fobj = getFileObject(lookup); 61 Object [] objs = getASTPathAndDocument(lookup); 62 ASTPath path = (ASTPath)objs[0]; 63 Document doc = (Document )objs[1]; 64 TopComponent activetc = TopComponent.getRegistry().getActivated(); 65 RefactoringUI ui = new WhereUsedQueryUI(path, fobj, doc); 66 UI.openRefactoringUI(ui, activetc); 67 } 68 69 public void doRename(Lookup lookup) { 70 FileObject fobj = getFileObject(lookup); 71 Object [] objs = getASTPathAndDocument(lookup); 72 ASTPath path = (ASTPath)objs[0]; 73 Document doc = (Document )objs[1]; 74 TopComponent activetc = TopComponent.getRegistry().getActivated(); 75 RefactoringUI ui = new RenameRefactoringUI(path, fobj, doc); 76 UI.openRefactoringUI(ui, activetc); 77 } 78 79 private static FileObject getFileObject(Lookup lookup) { 80 Node n = (Node)lookup.lookup(Node.class); 81 DataObject dob = n.getCookie(DataObject.class); 82 return dob.getPrimaryFile(); 83 } 84 85 private static Object [] getASTPathAndDocument(Lookup lookup) { 86 EditorCookie ec = lookup.lookup(EditorCookie.class); 87 JTextComponent textComp = ec.getOpenedPanes()[0]; 88 NbEditorDocument doc = (NbEditorDocument)textComp.getDocument(); 89 int position = textComp.getCaretPosition(); 90 ASTNode node = null; 91 try { 92 node = ParserManager.get(doc).getAST(); 93 } catch (ParseException e) { 94 return null; 95 } 96 return new Object [] {node.findPath(position), doc}; 97 } 98 99 private static boolean canRefactor(Lookup lookup) { 100 Collection <? extends Node> nodes = lookup.lookupAll(Node.class); 101 if (nodes.size() != 1) { 102 return false; 103 } 104 Node n = nodes.iterator().next(); 105 DataObject dob = n.getCookie(DataObject.class); 106 if ((dob != null) && (JS_MIME_TYPE.equals(dob.getPrimaryFile().getMIMEType()))) { 107 EditorCookie ec = lookup.lookup(EditorCookie.class); 108 if (ec == null) { 109 return false; 110 } 111 JEditorPane [] panes = ec.getOpenedPanes(); 112 TopComponent activetc = TopComponent.getRegistry().getActivated(); 114 if (!(activetc instanceof CloneableEditorSupport.Pane)) { 115 return false; 116 } 117 if (panes == null || panes.length == 0) { 118 return false; 119 } 120 JTextComponent textComp = panes[0]; 121 NbEditorDocument doc = (NbEditorDocument)textComp.getDocument(); 122 ASTNode node = null; 123 try { 124 node = ParserManager.get(doc).getAST(); 125 } catch (ParseException e) { 126 } 127 return node != null; 128 } 129 return false; 130 } 131 132 } 133 | Popular Tags |