KickJava   Java API By Example, From Geeks To Geeks.

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


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.PushDownRefactoring;
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
36 /** Refactoring UI object for Push Down refactoring.
37  *
38  * @author Pavel Flaska
39  */

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

51     public PushDownRefactoringUI(TreePathHandle[] selectedElements, CompilationInfo info) {
52         initialMembers = new HashSet JavaDoc();
53         // compute source type and members that should be pre-selected from the
54
// set of elements the action was invoked on
55

56         //TODO:
57
//sourceType = getSourceType(selectedElements, initialMembers);
58
TreePathHandle sourceType=null;
59         // create an instance of pull up refactoring object
60
refactoring = new PushDownRefactoring(Lookups.singleton(sourceType));
61     }
62     
63     // --- IMPLEMENTATION OF RefactoringUI INTERFACE ---------------------------
64

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

108     /** Gets parameters from the refactoring panel and sets them
109      * to the refactoring object.
110      */

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