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: package org.netbeans.modules.refactoring.java.ui;
042:
043: import java.text.MessageFormat;
044: import java.util.HashSet;
045: import java.util.ResourceBundle;
046: import java.util.Set;
047: import javax.lang.model.element.Element;
048: import javax.lang.model.element.ElementKind;
049: import javax.swing.event.ChangeListener;
050: import org.netbeans.api.java.classpath.ClassPath;
051: import org.netbeans.api.java.project.JavaProjectConstants;
052: import org.netbeans.api.java.source.ClasspathInfo;
053: import org.netbeans.api.java.source.CompilationInfo;
054: import org.netbeans.api.java.source.TreePathHandle;
055: import org.netbeans.api.java.source.UiUtils;
056: import org.netbeans.api.project.FileOwnerQuery;
057: import org.netbeans.api.project.Project;
058: import org.netbeans.api.project.ProjectUtils;
059: import org.netbeans.api.project.SourceGroup;
060: import org.netbeans.api.project.Sources;
061: import org.netbeans.modules.refactoring.api.AbstractRefactoring;
062: import org.netbeans.modules.refactoring.api.WhereUsedQuery;
063: import org.netbeans.modules.refactoring.java.RetoucheUtils;
064: import org.netbeans.modules.refactoring.java.api.WhereUsedQueryConstants;
065: import org.netbeans.modules.refactoring.spi.ui.CustomRefactoringPanel;
066: import org.netbeans.modules.refactoring.spi.ui.RefactoringUI;
067: import org.netbeans.spi.java.classpath.support.ClassPathSupport;
068: import org.openide.filesystems.FileObject;
069: import org.openide.util.HelpCtx;
070: import org.openide.util.NbBundle;
071: import org.openide.util.lookup.Lookups;
072:
073: /**
074: *
075: * @author Martin Matula, Jan Becicka
076: */
077: public class WhereUsedQueryUI implements RefactoringUI {
078: private WhereUsedQuery query = null;
079: private final String name;
080: private WhereUsedPanel panel;
081: private final TreePathHandle element;
082: private ElementKind kind;
083: private AbstractRefactoring delegate;
084:
085: public WhereUsedQueryUI(TreePathHandle handle, CompilationInfo info) {
086: this .query = new WhereUsedQuery(Lookups.singleton(handle));
087: this .query.getContext().add(info.getClasspathInfo());
088: this .element = handle;
089: Element el = handle.resolveElement(info);
090: if (el != null) {
091: name = UiUtils.getHeader(el, info, UiUtils.PrintPart.NAME);
092: kind = el.getKind();
093: } else {
094: name = ""; //NOI18N
095: kind = ElementKind.OTHER;
096: }
097: }
098:
099: public WhereUsedQueryUI(TreePathHandle jmiObject, String name,
100: AbstractRefactoring delegate) {
101: this .delegate = delegate;
102: //this.query = new JavaWhereUsedQuery(jmiObject);
103: //this.query.getContext().add(info.getClasspathInfo());
104: this .element = jmiObject;
105: //Element el = jmiObject.resolveElement(info);
106: //name = UiUtils.getHeader(el, info, UiUtils.PrintPart.NAME);
107: //kind = el.getKind();
108: this .name = name;
109: }
110:
111: public boolean isQuery() {
112: return true;
113: }
114:
115: public CustomRefactoringPanel getPanel(ChangeListener parent) {
116: if (panel == null) {
117: panel = new WhereUsedPanel(name, element, parent);
118: }
119: return panel;
120: }
121:
122: public org.netbeans.modules.refactoring.api.Problem setParameters() {
123: query.putValue(query.SEARCH_IN_COMMENTS, panel
124: .isSearchInComments());
125: if (panel.getScope() == WhereUsedPanel.Scope.ALL) {
126: if (kind == ElementKind.METHOD
127: && panel.isMethodFromBaseClass()) {
128: TreePathHandle basem = panel.getBaseMethod();
129: if (basem != null
130: && (basem.getFileObject() == null || basem
131: .getFileObject().getNameExt().endsWith(
132: "class"))) { //NOI18N
133: query.getContext().add(
134: RetoucheUtils.getClasspathInfoFor(element,
135: basem));
136: } else {
137: query.getContext().add(
138: RetoucheUtils.getClasspathInfoFor(basem));
139: }
140: } else {
141: query.getContext().add(
142: RetoucheUtils.getClasspathInfoFor(element));
143: }
144: } else {
145: ClasspathInfo info = query.getContext().lookup(
146: ClasspathInfo.class);
147: Project p = FileOwnerQuery
148: .getOwner(element.getFileObject());
149: Sources sources = ProjectUtils.getSources(p);
150: Set<FileObject> roots = new HashSet();
151: for (SourceGroup sg : sources
152: .getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA)) {
153: roots.add(sg.getRootFolder());
154: }
155: ClassPath rcp = ClassPathSupport.createClassPath(roots
156: .toArray(new FileObject[roots.size()]));
157: info = ClasspathInfo.create(info
158: .getClassPath(ClasspathInfo.PathKind.BOOT), info
159: .getClassPath(ClasspathInfo.PathKind.COMPILE), rcp);
160: query.getContext().add(info);
161: }
162: if (kind == ElementKind.METHOD) {
163: setForMethod();
164: return query.checkParameters();
165: } else if (kind.isClass() || kind.isInterface()) {
166: setForClass();
167: return query.checkParameters();
168: } else
169: return null;
170: }
171:
172: private void setForMethod() {
173: if (panel.isMethodFromBaseClass()) {
174: query.setRefactoringSource(Lookups.singleton(panel
175: .getBaseMethod()));
176: } else {
177: query.setRefactoringSource(Lookups.singleton(element));
178: }
179: query.putValue(WhereUsedQueryConstants.FIND_OVERRIDING_METHODS,
180: panel.isMethodOverriders());
181: query.putValue(query.FIND_REFERENCES, panel
182: .isMethodFindUsages());
183: }
184:
185: private void setForClass() {
186: query.putValue(WhereUsedQueryConstants.FIND_SUBCLASSES, panel
187: .isClassSubTypes());
188: query.putValue(WhereUsedQueryConstants.FIND_DIRECT_SUBCLASSES,
189: panel.isClassSubTypesDirectOnly());
190: query
191: .putValue(query.FIND_REFERENCES, panel
192: .isClassFindUsages());
193: }
194:
195: public org.netbeans.modules.refactoring.api.Problem checkParameters() {
196: if (kind == ElementKind.METHOD) {
197: setForMethod();
198: return query.fastCheckParameters();
199: } else if (kind.isClass() || kind.isInterface()) {
200: setForClass();
201: return query.fastCheckParameters();
202: } else
203: return null;
204: }
205:
206: public org.netbeans.modules.refactoring.api.AbstractRefactoring getRefactoring() {
207: return query != null ? query : delegate;
208: }
209:
210: public String getDescription() {
211: if (panel != null) {
212: if ((kind == ElementKind.INTERFACE)
213: || (kind == ElementKind.CLASS)) {
214: if (!panel.isClassFindUsages())
215: if (!panel.isClassSubTypesDirectOnly()) {
216: return getString(
217: "DSC_WhereUsedFindAllSubTypes", name);
218: } else {
219: return getString(
220: "DSC_WhereUsedFindDirectSubTypes", name);
221: }
222: } else {
223: if (kind == ElementKind.METHOD) {
224: String description = null;
225: if (panel.isMethodFindUsages()) {
226: description = getString("DSC_FindUsages");
227: }
228:
229: if (panel.isMethodOverriders()) {
230: if (description != null) {
231: description += " " + getString("DSC_And")
232: + " ";
233: } else {
234: description = "";
235: }
236: description += getString("DSC_WhereUsedMethodOverriders");
237: }
238:
239: description += " "
240: + getString("DSC_WhereUsedOf", panel
241: .getMethodDeclaringClass()
242: + '.' + name); //NOI18N
243: return description;
244: }
245: }
246: }
247: return getString("DSC_WhereUsed", name);
248: }
249:
250: private ResourceBundle bundle;
251:
252: private String getString(String key) {
253: if (bundle == null) {
254: bundle = NbBundle.getBundle(WhereUsedQueryUI.class);
255: }
256: return bundle.getString(key);
257: }
258:
259: private String getString(String key, String value) {
260: return new MessageFormat(getString(key))
261: .format(new Object[] { value });
262: }
263:
264: public String getName() {
265: return new MessageFormat(NbBundle.getMessage(
266: WhereUsedPanel.class, "LBL_UsagesOf"))
267: .format(new Object[] { name });
268: }
269:
270: public boolean hasParameters() {
271: return true;
272: }
273:
274: public HelpCtx getHelpCtx() {
275: return new HelpCtx(WhereUsedQueryUI.class);
276: }
277:
278: }
|