KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > refactoring > java > ui > WhereUsedQueryUI


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 package org.netbeans.modules.refactoring.java.ui;
20
21 import java.text.MessageFormat JavaDoc;
22 import java.util.ResourceBundle JavaDoc;
23 import javax.lang.model.element.Element;
24 import javax.lang.model.element.ElementKind;
25 import javax.swing.event.ChangeListener JavaDoc;
26 import org.netbeans.api.java.source.CompilationInfo;
27 import org.netbeans.api.java.source.TreePathHandle;
28 import org.netbeans.api.java.source.UiUtils;
29 import org.netbeans.modules.refactoring.api.AbstractRefactoring;
30 import org.netbeans.modules.refactoring.api.WhereUsedQuery;
31 import org.netbeans.modules.refactoring.java.api.WhereUsedQueryConstants;
32 import org.netbeans.modules.refactoring.spi.ui.CustomRefactoringPanel;
33 import org.netbeans.modules.refactoring.spi.ui.RefactoringUI;
34 import org.openide.util.HelpCtx;
35 import org.openide.util.NbBundle;
36 import org.openide.util.lookup.Lookups;
37
38 /**
39  *
40  * @author Martin Matula, Jan Becicka
41  */

42 public class WhereUsedQueryUI implements RefactoringUI {
43     private WhereUsedQuery query = null;
44     private final String JavaDoc name;
45     private WhereUsedPanel panel;
46     private final TreePathHandle element;
47     private ElementKind kind;
48     private AbstractRefactoring delegate;
49
50     public WhereUsedQueryUI(TreePathHandle jmiObject, CompilationInfo info) {
51         this.query = new WhereUsedQuery(Lookups.singleton(jmiObject));
52         this.query.getContext().add(info.getClasspathInfo());
53         this.element = jmiObject;
54         Element el = jmiObject.resolveElement(info);
55         name = UiUtils.getHeader(el, info, UiUtils.PrintPart.NAME);
56         kind = el.getKind();
57     }
58     
59     public WhereUsedQueryUI(TreePathHandle jmiObject, String JavaDoc name, AbstractRefactoring delegate) {
60         this.delegate = delegate;
61         //this.query = new JavaWhereUsedQuery(jmiObject);
62
//this.query.getContext().add(info.getClasspathInfo());
63
this.element = jmiObject;
64         //Element el = jmiObject.resolveElement(info);
65
//name = UiUtils.getHeader(el, info, UiUtils.PrintPart.NAME);
66
//kind = el.getKind();
67
this.name = name;
68     }
69     
70
71     public boolean isQuery() {
72         return true;
73     }
74
75     public CustomRefactoringPanel getPanel(ChangeListener JavaDoc parent) {
76         if (panel == null) {
77             panel = new WhereUsedPanel(name, element, parent);
78         }
79         return panel;
80     }
81
82     public org.netbeans.modules.refactoring.api.Problem setParameters() {
83         query.putValue(query.SEARCH_IN_COMMENTS,panel.isSearchInComments());
84         if (kind == ElementKind.METHOD) {
85             setForMethod();
86             return query.checkParameters();
87         } else if (kind.isClass() || kind.isInterface()) {
88             setForClass();
89             return query.checkParameters();
90         } else
91             return null;
92     }
93     
94     private void setForMethod() {
95         if (panel.isMethodFromBaseClass()) {
96             query.setRefactoringSource(Lookups.singleton(panel.getBaseMethod()));
97         } else {
98             query.setRefactoringSource(Lookups.singleton(element));
99         }
100         query.putValue(WhereUsedQueryConstants.FIND_OVERRIDING_METHODS,panel.isMethodOverriders());
101         query.putValue(query.FIND_REFERENCES,panel.isMethodFindUsages());
102     }
103     
104     private void setForClass() {
105         query.putValue(WhereUsedQueryConstants.FIND_SUBCLASSES,panel.isClassSubTypes());
106         query.putValue(WhereUsedQueryConstants.FIND_DIRECT_SUBCLASSES,panel.isClassSubTypesDirectOnly());
107         query.putValue(query.FIND_REFERENCES,panel.isClassFindUsages());
108     }
109     
110     public org.netbeans.modules.refactoring.api.Problem checkParameters() {
111         if (kind == ElementKind.METHOD) {
112             setForMethod();
113             return query.fastCheckParameters();
114         } else if (kind.isClass() || kind.isInterface()) {
115             setForClass();
116             return query.fastCheckParameters();
117         } else
118             return null;
119     }
120
121     public org.netbeans.modules.refactoring.api.AbstractRefactoring getRefactoring() {
122         return query!=null?query:delegate;
123     }
124
125     public String JavaDoc getDescription() {
126         if (panel!=null) {
127             if ((kind == ElementKind.INTERFACE) || (kind == ElementKind.CLASS)) {
128                 if (!panel.isClassFindUsages())
129                     if (!panel.isClassSubTypesDirectOnly()) {
130                     return getString("DSC_WhereUsedFindAllSubTypes", name);
131                     } else {
132                     return getString("DSC_WhereUsedFindDirectSubTypes", name);
133                     }
134             } else {
135                 if (kind == ElementKind.METHOD) {
136                     String JavaDoc description = null;
137                     if (panel.isMethodFindUsages()) {
138                         description = getString("DSC_FindUsages");
139                     }
140                     
141                     if (panel.isMethodOverriders()) {
142                         if (description != null) {
143                             description += " " + getString("DSC_And") + " ";
144                         } else {
145                             description = "";
146                         }
147                         description += getString("DSC_WhereUsedMethodOverriders");
148                     }
149                     
150                     description += " " + getString("DSC_WhereUsedOf", panel.getMethodDeclaringClass() + '.' + name); //NOI18N
151
return description;
152                 }
153             }
154         }
155         return getString("DSC_WhereUsed", name);
156     }
157     
158     private ResourceBundle JavaDoc bundle;
159     private String JavaDoc getString(String JavaDoc key) {
160         if (bundle == null) {
161             bundle = NbBundle.getBundle(WhereUsedQueryUI.class);
162         }
163         return bundle.getString(key);
164     }
165     
166     private String JavaDoc getString(String JavaDoc key, String JavaDoc value) {
167         return new MessageFormat JavaDoc(getString(key)).format (new Object JavaDoc[] {value});
168     }
169
170
171     public String JavaDoc getName() {
172         return new MessageFormat JavaDoc(NbBundle.getMessage(WhereUsedPanel.class, "LBL_WhereUsed")).format (
173                     new Object JavaDoc[] {name}
174                 );
175     }
176     
177     public boolean hasParameters() {
178         return true;
179     }
180
181     public HelpCtx getHelpCtx() {
182         return new HelpCtx(WhereUsedQueryUI.class);
183     }
184     
185 }
186
Popular Tags