KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.HashSet JavaDoc;
22 import java.util.Set JavaDoc;
23 import javax.swing.event.ChangeListener JavaDoc;
24 import org.netbeans.api.java.source.CompilationInfo;
25 import org.netbeans.api.java.source.TreePathHandle;
26 import org.netbeans.modules.refactoring.api.AbstractRefactoring;
27 import org.netbeans.modules.refactoring.api.Problem;
28 import org.netbeans.modules.refactoring.java.api.PullUpRefactoring;
29 import org.netbeans.modules.refactoring.spi.ui.CustomRefactoringPanel;
30 import org.netbeans.modules.refactoring.spi.ui.RefactoringUI;
31 import org.openide.util.HelpCtx;
32 import org.openide.util.NbBundle;
33 import org.openide.util.lookup.Lookups;
34
35 /** Refactoring UI object for Pull Up refactoring.
36  *
37  * @author Martin Matula
38  */

39 public class PullUpRefactoringUI implements RefactoringUI {
40     // reference to pull up refactoring this UI object corresponds to
41
private final PullUpRefactoring refactoring;
42     // initially selected members
43
private final Set JavaDoc initialMembers;
44     // UI panel for collecting parameters
45
private PullUpPanel panel;
46     
47     /** Creates a new instance of PullUpRefactoringUI
48      * @param selectedElements Elements the refactoring action was invoked on.
49      */

50     public PullUpRefactoringUI(TreePathHandle[] selectedElements, CompilationInfo info) {
51         initialMembers = new HashSet JavaDoc();
52         // compute source type and members that should be pre-selected from the
53
// set of elements the action was invoked on
54
//TODO:
55
//sourceType = getSourceType(selectedElements, initialMembers);
56
TreePathHandle sourceType = null;
57         // create an instance of pull up refactoring object
58
refactoring = new PullUpRefactoring(Lookups.singleton(sourceType));
59     }
60     
61     // --- IMPLEMENTATION OF RefactoringUI INTERFACE ---------------------------
62

63     public boolean isQuery() {
64         return false;
65     }
66
67     public CustomRefactoringPanel getPanel(ChangeListener JavaDoc parent) {
68         if (panel == null) {
69             panel = new PullUpPanel(refactoring, initialMembers, parent);
70         }
71         return panel;
72     }
73
74     public Problem setParameters() {
75         captureParameters();
76         return refactoring.checkParameters();
77     }
78     
79     public Problem checkParameters() {
80         captureParameters();
81         return refactoring.fastCheckParameters();
82     }
83
84     public AbstractRefactoring getRefactoring() {
85         return refactoring;
86     }
87
88     public String JavaDoc getDescription() {
89         return NbBundle.getMessage(PullUpAction.class, "DSC_PullUp", /*sourceType.getName()*/ "TODO"); // NOI18N
90
}
91
92     public String JavaDoc getName() {
93         return NbBundle.getMessage(PullUpAction.class, "LBL_PullUp"); // NOI18N
94
}
95
96     public boolean hasParameters() {
97         return true;
98     }
99
100     public HelpCtx getHelpCtx() {
101         return new HelpCtx(PullUpRefactoringUI.class.getName());
102     }
103     
104     // --- PRIVATE HELPER METHODS ----------------------------------------------
105

106     /** Gets parameters from the refactoring panel and sets them
107      * to the refactoring object.
108      */

109     private void captureParameters() {
110 //TODO:
111
// refactoring.setTargetType(panel.getTargetType());
112
// refactoring.setMembers(panel.getMembers());
113
}
114     
115 // /** Method that computes the source type and initially selected members from
116
// * elements the refactoring action was invoked on.
117
// * It tries to find a common parent class for the elements to return it as the
118
// * the source type. If not all elements
119
// * have a common parent class, then the class that is a parent class for majority
120
// * of the elements is chosen. The parent JavaClass, Field, Method or MultipartId that
121
// * is part of interface names of a class is taken as the pre-selected member.
122
// * @elements The elements the refactoring was invoked on.
123
// * @initialMembers Should be an empty set that this method will add the members
124
// * that should be selected.
125
// * @return Source type.
126
// */
127
// private static JavaClass getSourceType(Element[] elements, Set initialMembers) {
128
// JavaClass result = null;
129
// // map that will be used to compute final source type and pre-selected members
130
// // maps suggested source type to a set of its suggested pre-selected members
131
// HashMap elementsByClass = new HashMap();
132
//
133
// for (int i = 0; i < elements.length; i++) {
134
// Element element = null;
135
// Element temp = elements[i];
136
// // iterate through the containers of the element (until we get to null
137
// // or a resource)
138
// while (temp != null && !(temp instanceof Resource)) {
139
// if ((temp instanceof JavaClass) || (temp instanceof Field) || (temp instanceof Method)) {
140
// // if the current element is a class, field or a method, exit
141
// // the loop - we have an element that will likely be the member
142
// // to be initially selected
143
// break;
144
// } else if (temp instanceof MultipartId) {
145
// // if the current element is a MultipartId, remember it, but
146
// // go further to find the top-most MultipartId
147
// // if the MultipartId is part of interface names of a class,
148
// // then its direct parent should be JavaClass, so when
149
// // we exit the loop, "element" variable will contain the MultipartId
150
// // and the "temp" variable will contain its parent class.
151
// element = temp;
152
// }
153
// temp = (Element) temp.refImmediateComposite();
154
// }
155
// // if temp is null, the element is not in a resource -> ignore it
156
// if (temp == null) continue;
157
// // if element is a resource, find the primary class in it
158
// // (the class with the same name as the resource)
159
// if (temp instanceof Resource) {
160
// element = null;
161
// String name = ((Resource) temp).getName();
162
// int start = name.lastIndexOf('/') + 1;
163
// int end = name.indexOf('.', start);
164
// if (end < 0) end = name.length();
165
// name = name.substring(start, end);
166
// for (Iterator it = ((Resource) temp).getClassifiers().iterator(); it.hasNext();) {
167
// JavaClass cls = (JavaClass) it.next();
168
// temp = cls;
169
// // if the class of a same name is found, exit the loop
170
// if (name.equals(cls.getSimpleName())) break;
171
// }
172
// // if no class of the same name is found, then the last class in
173
// // the resource is taken as the selected one
174
// }
175
// if (temp instanceof JavaClass) {
176
// // if the found element is a class, check whether element is not null
177
// // and is part of the interface names in class implements clause
178
// if (element == null || !((JavaClass) temp).getInterfaceNames().contains(element)) {
179
// // if not, add the class as a suggested source type (i.e. in place of a key in the map)
180
// addToMap(elementsByClass, (JavaClass) temp, null);
181
// } else {
182
// // if so, the selected element is the interface name
183
// // put the class into element variable (which will later be added as a key
184
// // - i.e. as the suggested source type - into the map) and
185
// // the interface name into the temp variable (which will later be added
186
// // as a value to the map - i.e. as a suggested pre-selected member)
187
// Element cls = temp;
188
// temp = element;
189
// element = cls;
190
// }
191
// }
192
// if (temp instanceof Feature) {
193
// // if the element found is a feature (i.e. either JavaClass, or Field or a Method)
194
// // store its declaring class in element variable (as a suggested source type)
195
// element = ((Feature) temp).getDeclaringClass();
196
// }
197
// // if the thing in the element variable (i.e. the suggested source type)
198
// // is of a correct type (i.e. JavaClass) add the type and the member to the
199
// // map that will be used to compute final source type and pre-selected members
200
// if (element instanceof JavaClass) {
201
// addToMap(elementsByClass, (JavaClass) element, temp);
202
// }
203
// }
204
//
205
// // now go through the map and find the suggested source type corresponding
206
// // to the highest number of the pre-selected members
207
// Set maxMembers = Collections.EMPTY_SET;
208
// for (Iterator it = elementsByClass.entrySet().iterator(); it.hasNext();) {
209
// Map.Entry entry = (Map.Entry) it.next();
210
// Set value = (Set) entry.getValue();
211
// // if the number of members for this source type
212
// // is higher than the last max., take it as max
213
// // note that even when the number is equal, but the set of members contains
214
// // null, it takes a precedence - this is to correctly handle the case when the
215
// // only selected element is an inner class (the map will contain two records:
216
// // 1 - outer->inner, 2 - inner->null). In this case the algorithm chooses the inner class
217
// // to be the source type with no members pre-selected.
218
// if ((maxMembers.size() < value.size()) || ((maxMembers.size() == value.size()) && value.contains(null))) {
219
// maxMembers = value;
220
// result = (JavaClass) entry.getKey();
221
// }
222
// }
223
// initialMembers.addAll(maxMembers);
224
//
225
// return result;
226
// }
227

228 // /** Helper method that simplifies adding members to the map of
229
// * suggested source types to the set of members.
230
// * @param map Map to add a new record to.
231
// * @param parentClass Map key - suggested source type.
232
// * @param member A new value for the key - pre-selected member.
233
// */
234
// private static void addToMap(Map map, JavaClass parentClass, Element member) {
235
// Set value = (Set) map.get(parentClass);
236
// if (value == null) {
237
// value = new HashSet();
238
// map.put(parentClass, value);
239
// }
240
// value.add(member);
241
// }
242
}
Popular Tags