KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > languages > javascript > refactoring > JSRefactoringsFactory


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.languages.javascript.refactoring;
21
22 import java.util.Iterator JavaDoc;
23 import javax.swing.text.BadLocationException JavaDoc;
24 import javax.swing.text.Document JavaDoc;
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 /**
53  *
54  * @author Daniel Prusa
55  */

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 JavaDoc 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 JavaDoc doc = (Document JavaDoc)lookup.lookup(Document JavaDoc.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 JavaDoc iter = decl.getUsages().iterator(); iter.hasNext(); ) {
127                 elements.add(refactoring, new UsageElement(dobj, ((Usage)iter.next()).getASTItem()));
128             }
129             return null;
130         }
131
132     } // JSWhereUsedQueryPlugin
133

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 JavaDoc getText() {
147             return item instanceof ASTToken ? ((ASTToken)item).getIdentifier() : ((ASTNode) item).getNT();
148         }
149
150         public String JavaDoc getDisplayText() {
151             return getText();
152         }
153
154         public void performChange() {
155         }
156
157         public Object JavaDoc 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     } // UsageElement
176

177     // ..........................................................................
178

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 JavaDoc doc = (Document JavaDoc)lookup.lookup(Document JavaDoc.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 JavaDoc newName = refactoring.getNewName();
203             String JavaDoc 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 JavaDoc doc = decl.getDocument();
226             String JavaDoc newName = refactoring.getNewName();
227             elements.add(refactoring, new RenameElement(dobj, doc, decl.getASTItem(), newName));
228             for (Iterator JavaDoc 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     } // JSRenameRefactoringPlugin
235

236     static class RenameElement extends SimpleRefactoringElementImpl {
237
238         private DataObject dobj;
239         private Document JavaDoc doc;
240         private ASTItem item;
241         private String JavaDoc newName;
242         private PositionBounds bounds = null;
243     
244         public RenameElement(DataObject dobj, Document JavaDoc doc, ASTItem item, String JavaDoc newName) {
245             this.dobj = dobj;
246             this.doc = doc;
247             this.item = item;
248             this.newName = newName;
249             getPosition();
250         }
251         
252         public String JavaDoc getText() {
253             return item instanceof ASTToken ? ((ASTToken)item).getIdentifier() : ((ASTNode) item).getNT();
254         }
255
256         public String JavaDoc 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 JavaDoc e) {
267                 ErrorManager.getDefault().notify(e);
268             }
269         }
270
271         public Object JavaDoc 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     } // RenameElement
290

291     private static String JavaDoc getString(String JavaDoc id) {
292         return NbBundle.getMessage(JSRefactoringsFactory.class, id);
293     }
294     
295 }
296
Popular Tags