001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.languages.features.refactoring;
043:
044: import javax.swing.JEditorPane;
045: import javax.swing.text.Document;
046: import java.util.Collection;
047: import javax.swing.text.JTextComponent;
048:
049: import org.netbeans.api.languages.ASTNode;
050: import org.netbeans.api.languages.ASTPath;
051: import org.netbeans.api.languages.ParseException;
052: import org.netbeans.api.languages.ParserManager;
053: import org.netbeans.modules.editor.NbEditorDocument;
054: import org.netbeans.modules.languages.ParserManagerImpl;
055: import org.netbeans.modules.refactoring.spi.ui.ActionsImplementationProvider;
056: import org.netbeans.modules.refactoring.spi.ui.RefactoringUI;
057: import org.netbeans.modules.refactoring.spi.ui.UI;
058: import org.openide.cookies.EditorCookie;
059: import org.openide.filesystems.FileObject;
060: import org.openide.loaders.DataObject;
061: import org.openide.util.Lookup;
062: import org.openide.nodes.Node;
063: import org.openide.text.CloneableEditorSupport;
064: import org.openide.windows.TopComponent;
065:
066: /**
067: *
068: * @author Daniel Prusa
069: */
070: public class RefactoringActionsProvider extends
071: ActionsImplementationProvider {
072:
073: private static final String JS_MIME_TYPE = "text/javascript"; // NOI18N
074:
075: public boolean canFindUsages(Lookup lookup) {
076: return canRefactor(lookup);
077: }
078:
079: public boolean canRename(Lookup lookup) {
080: return canRefactor(lookup);
081: }
082:
083: public void doFindUsages(Lookup lookup) {
084: FileObject fobj = getFileObject(lookup);
085: Object[] objs = getASTPathAndDocument(lookup);
086: ASTPath path = (ASTPath) objs[0];
087: Document doc = (Document) objs[1];
088: TopComponent activetc = TopComponent.getRegistry()
089: .getActivated();
090: RefactoringUI ui = new WhereUsedQueryUI(path, fobj, doc);
091: UI.openRefactoringUI(ui, activetc);
092: }
093:
094: public void doRename(Lookup lookup) {
095: FileObject fobj = getFileObject(lookup);
096: Object[] objs = getASTPathAndDocument(lookup);
097: ASTPath path = (ASTPath) objs[0];
098: Document doc = (Document) objs[1];
099: TopComponent activetc = TopComponent.getRegistry()
100: .getActivated();
101: RefactoringUI ui = new RenameRefactoringUI(path, fobj, doc);
102: UI.openRefactoringUI(ui, activetc);
103: }
104:
105: private static FileObject getFileObject(Lookup lookup) {
106: Node n = (Node) lookup.lookup(Node.class);
107: DataObject dob = n.getCookie(DataObject.class);
108: return dob.getPrimaryFile();
109: }
110:
111: private static Object[] getASTPathAndDocument(Lookup lookup) {
112: EditorCookie ec = lookup.lookup(EditorCookie.class);
113: JTextComponent textComp = ec.getOpenedPanes()[0];
114: NbEditorDocument doc = (NbEditorDocument) textComp
115: .getDocument();
116: String selectedText = textComp.getSelectedText();
117: ASTNode node = ParserManagerImpl.getImpl(doc).getAST();
118: int position = 0;
119: if (selectedText != null) {
120: position = textComp.getSelectionStart();
121: for (int x = 0; x < selectedText.length(); x++) {
122: if (Character.isWhitespace(selectedText.charAt(x))) {
123: position++;
124: } else {
125: break;
126: }
127: }
128: } else {
129: position = textComp.getCaretPosition();
130: }
131: return new Object[] { node.findPath(position), doc };
132: }
133:
134: private static boolean canRefactor(Lookup lookup) {
135: // Disabled - this module only seems to handle JavaScript, not
136: // Schliemann filetypes in general, and JavaScript is handled by
137: // the javascript.refactoring module now
138: return false;
139: /*
140: Collection<? extends Node> nodes = lookup.lookupAll(Node.class);
141: if (nodes.size() != 1) {
142: return false;
143: }
144: Node n = nodes.iterator().next();
145: DataObject dob = n.getCookie(DataObject.class);
146: if ((dob != null) && (JS_MIME_TYPE.equals(dob.getPrimaryFile().getMIMEType()))) {
147: EditorCookie ec = lookup.lookup(EditorCookie.class);
148: if (ec == null) {
149: return false;
150: }
151: JEditorPane[] panes = ec.getOpenedPanes();
152: // check if it is called from editor
153: TopComponent activetc = TopComponent.getRegistry().getActivated();
154: if (!(activetc instanceof CloneableEditorSupport.Pane)) {
155: return false;
156: }
157: if (panes == null || panes.length == 0) {
158: return false;
159: }
160: JTextComponent textComp = panes[0];
161: Document doc = textComp.getDocument();
162: if (!(doc instanceof NbEditorDocument)) {
163: return false;
164: }
165: ASTNode node = null;
166: try {
167: node = ParserManager.get((NbEditorDocument)doc).getAST();
168: } catch (ParseException e) {
169: }
170: return node != null;
171: }
172: return false;
173: */
174: }
175:
176: }
|