KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.swing.JEditorPane JavaDoc;
23 import javax.swing.text.Document JavaDoc;
24 import org.netbeans.modules.languages.javascript.*;
25 import java.util.Collection JavaDoc;
26 import javax.swing.text.JTextComponent JavaDoc;
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 /**
44  *
45  * @author Daniel Prusa
46  */

47 public class RefactoringActionsProvider extends ActionsImplementationProvider {
48     
49     private static final String JavaDoc JS_MIME_TYPE = "text/javascript"; // NOI18N
50

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 JavaDoc[] objs = getASTPathAndDocument(lookup);
62         ASTPath path = (ASTPath)objs[0];
63         Document JavaDoc doc = (Document JavaDoc)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 JavaDoc[] objs = getASTPathAndDocument(lookup);
72         ASTPath path = (ASTPath)objs[0];
73         Document JavaDoc doc = (Document JavaDoc)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 JavaDoc[] getASTPathAndDocument(Lookup lookup) {
86         EditorCookie ec = lookup.lookup(EditorCookie.class);
87         JTextComponent JavaDoc 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 JavaDoc[] {node.findPath(position), doc};
97     }
98     
99     private static boolean canRefactor(Lookup lookup) {
100         Collection JavaDoc<? 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 JavaDoc[] panes = ec.getOpenedPanes();
112             // check if it is called from editor
113
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 JavaDoc 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