1 19 20 package org.netbeans.modules.languages.javascript.refactoring; 21 22 import java.util.Iterator ; 23 import javax.swing.text.BadLocationException ; 24 import javax.swing.text.Document ; 25 import javax.swing.text.Position.Bias; 26 import org.netbeans.api.languages.ASTItem; 27 import org.netbeans.api.languages.ASTNode; 28 import org.netbeans.api.languages.ASTPath; 29 import org.netbeans.api.languages.ASTToken; 30 import org.netbeans.modules.languages.javascript.Semantic; 31 import org.netbeans.modules.languages.javascript.Semantic.Declaration; 32 import org.netbeans.modules.languages.javascript.Semantic.Info; 33 import org.netbeans.modules.languages.javascript.Semantic.Usage; 34 import org.netbeans.modules.refactoring.api.AbstractRefactoring; 35 import org.netbeans.modules.refactoring.api.Problem; 36 import org.netbeans.modules.refactoring.api.RenameRefactoring; 37 import org.netbeans.modules.refactoring.api.WhereUsedQuery; 38 import org.netbeans.modules.refactoring.spi.RefactoringElementsBag; 39 import org.netbeans.modules.refactoring.spi.RefactoringPlugin; 40 import org.netbeans.modules.refactoring.spi.RefactoringPluginFactory; 41 import org.netbeans.modules.refactoring.spi.SimpleRefactoringElementImpl; 42 import org.openide.ErrorManager; 43 import org.openide.filesystems.FileObject; 44 import org.openide.loaders.DataObject; 45 import org.openide.loaders.DataObjectNotFoundException; 46 import org.openide.text.CloneableEditorSupport; 47 import org.openide.text.PositionBounds; 48 import org.openide.text.PositionRef; 49 import org.openide.util.Lookup; 50 import org.openide.util.NbBundle; 51 52 56 public class JSRefactoringsFactory implements RefactoringPluginFactory { 57 58 public RefactoringPlugin createInstance(AbstractRefactoring refactoring) { 59 Lookup lookup = refactoring.getRefactoringSource(); 60 ASTPath path = lookup.lookup(ASTPath.class); 61 if (path == null) { 62 return null; 63 } 64 if (refactoring instanceof WhereUsedQuery) { 65 return new JSWhereUsedQueryPlugin((WhereUsedQuery) refactoring); 66 } else if (refactoring instanceof RenameRefactoring) { 67 return new JSRenameRefactoringPlugin((RenameRefactoring) refactoring); 68 } 69 return null; 70 } 71 72 public static CloneableEditorSupport findCloneableEditorSupport(DataObject dob) { 73 Object obj = dob.getCookie(org.openide.cookies.OpenCookie.class); 74 if (obj instanceof CloneableEditorSupport) { 75 return (CloneableEditorSupport)obj; 76 } 77 obj = dob.getCookie(org.openide.cookies.EditorCookie.class); 78 if (obj instanceof CloneableEditorSupport) { 79 return (CloneableEditorSupport)obj; 80 } 81 return null; 82 } 83 84 public static class JSWhereUsedQueryPlugin implements RefactoringPlugin { 85 86 private WhereUsedQuery refactoring; 87 private Declaration decl; 88 89 public JSWhereUsedQueryPlugin(WhereUsedQuery refactoring) { 90 this.refactoring = refactoring; 91 } 92 93 public Problem preCheck() { 94 Lookup lookup = refactoring.getRefactoringSource(); 95 ASTPath path = (ASTPath)lookup.lookup(ASTPath.class); 96 Document doc = (Document )lookup.lookup(Document .class); 97 ASTItem leaf = path.getLeaf(); 98 Info info = Semantic.getInfo(doc); 99 decl = info.getItem(leaf); 100 if (decl == null) { 101 return new Problem(true, getString("LBL_CannotFindUsages")); 102 } 103 return null; 104 } 105 106 public Problem checkParameters() { 107 return null; 108 } 109 110 public Problem fastCheckParameters() { 111 return null; 112 } 113 114 public void cancelRequest() { 115 } 116 117 public Problem prepare(RefactoringElementsBag elements) { 118 DataObject dobj = null; 119 try { 120 dobj = DataObject.find(decl.getFileObject()); 121 } catch (DataObjectNotFoundException e) { 122 ErrorManager.getDefault().notify(e); 123 return new Problem(true, getString("LBL_CannotFindUsages")); 124 } 125 elements.add(refactoring, new UsageElement(dobj, decl.getASTItem())); 126 for (Iterator iter = decl.getUsages().iterator(); iter.hasNext(); ) { 127 elements.add(refactoring, new UsageElement(dobj, ((Usage)iter.next()).getASTItem())); 128 } 129 return null; 130 } 131 132 } 134 static class UsageElement extends SimpleRefactoringElementImpl { 135 136 private DataObject dobj; 137 private ASTItem item; 138 private PositionBounds bounds = null; 139 140 public UsageElement(DataObject dobj, ASTItem item) { 141 this.item = item; 142 this.dobj = dobj; 143 getPosition(); 144 } 145 146 public String getText() { 147 return item instanceof ASTToken ? ((ASTToken)item).getIdentifier() : ((ASTNode) item).getNT(); 148 } 149 150 public String getDisplayText() { 151 return getText(); 152 } 153 154 public void performChange() { 155 } 156 157 public Object getComposite() { 158 return getParentFile(); 159 } 160 161 public FileObject getParentFile() { 162 return dobj.getPrimaryFile(); 163 } 164 165 public PositionBounds getPosition() { 166 if (bounds == null) { 167 CloneableEditorSupport ces = findCloneableEditorSupport(dobj); 168 int offset = item.getOffset(); 169 PositionRef ref1 = ces.createPositionRef(offset, Bias.Forward); 170 PositionRef ref2 = ces.createPositionRef(offset + item.getLength(), Bias.Forward); 171 bounds = new PositionBounds(ref1, ref2); 172 } 173 return bounds; 174 } 175 } 177 179 public static class JSRenameRefactoringPlugin implements RefactoringPlugin { 180 181 private RenameRefactoring refactoring; 182 private Declaration decl; 183 184 public JSRenameRefactoringPlugin(RenameRefactoring refactoring) { 185 this.refactoring = refactoring; 186 } 187 188 public Problem preCheck() { 189 Lookup lookup = refactoring.getRefactoringSource(); 190 ASTPath path = (ASTPath)lookup.lookup(ASTPath.class); 191 Document doc = (Document )lookup.lookup(Document .class); 192 ASTItem leaf = path.getLeaf(); 193 Info info = Semantic.getInfo(doc); 194 decl = info.getItem(leaf); 195 if (decl == null) { 196 return new Problem(true, getString("LBL_CannotRename")); 197 } 198 return null; 199 } 200 201 public Problem checkParameters() { 202 String newName = refactoring.getNewName(); 203 String oldName = decl != null ? ((ASTToken)decl.getASTItem()).getIdentifier() : null; 204 if (newName.equals(oldName)) { 205 return new Problem(true, getString("LBL_NameNotChanged")); 206 } 207 return null; 208 } 209 210 public Problem fastCheckParameters() { 211 return null; 212 } 213 214 public void cancelRequest() { 215 } 216 217 public Problem prepare(RefactoringElementsBag elements) { 218 DataObject dobj = null; 219 try { 220 dobj = DataObject.find(decl.getFileObject()); 221 } catch (DataObjectNotFoundException e) { 222 ErrorManager.getDefault().notify(e); 223 return new Problem(true, getString("LBL_CannotRename")); 224 } 225 Document doc = decl.getDocument(); 226 String newName = refactoring.getNewName(); 227 elements.add(refactoring, new RenameElement(dobj, doc, decl.getASTItem(), newName)); 228 for (Iterator iter = decl.getUsages().iterator(); iter.hasNext(); ) { 229 elements.add(refactoring, new RenameElement(dobj, doc, ((Usage)iter.next()).getASTItem(), newName)); 230 } 231 return null; 232 } 233 234 } 236 static class RenameElement extends SimpleRefactoringElementImpl { 237 238 private DataObject dobj; 239 private Document doc; 240 private ASTItem item; 241 private String newName; 242 private PositionBounds bounds = null; 243 244 public RenameElement(DataObject dobj, Document doc, ASTItem item, String newName) { 245 this.dobj = dobj; 246 this.doc = doc; 247 this.item = item; 248 this.newName = newName; 249 getPosition(); 250 } 251 252 public String getText() { 253 return item instanceof ASTToken ? ((ASTToken)item).getIdentifier() : ((ASTNode) item).getNT(); 254 } 255 256 public String getDisplayText() { 257 return getText(); 258 } 259 260 public void performChange() { 261 int offset = bounds.getBegin().getOffset(); 262 try { 263 doc.remove(offset, item.getLength()); 264 doc.insertString(offset, newName, null); 265 } 266 catch (BadLocationException e) { 267 ErrorManager.getDefault().notify(e); 268 } 269 } 270 271 public Object getComposite() { 272 return getParentFile(); 273 } 274 275 public FileObject getParentFile() { 276 return dobj.getPrimaryFile(); 277 } 278 279 public PositionBounds getPosition() { 280 if (bounds == null) { 281 CloneableEditorSupport ces = findCloneableEditorSupport(dobj); 282 int offset = item.getOffset(); 283 PositionRef ref1 = ces.createPositionRef(offset, Bias.Forward); 284 PositionRef ref2 = ces.createPositionRef(offset + item.getLength(), Bias.Forward); 285 bounds = new PositionBounds(ref1, ref2); 286 } 287 return bounds; 288 } 289 } 291 private static String getString(String id) { 292 return NbBundle.getMessage(JSRefactoringsFactory.class, id); 293 } 294 295 } 296 | Popular Tags |