KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.awt.Component JavaDoc;
21 import java.awt.Dimension JavaDoc;
22 import java.awt.event.ItemEvent JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collection JavaDoc;
26 import javax.swing.JPanel JavaDoc;
27 import javax.swing.SwingUtilities JavaDoc;
28 import org.netbeans.api.java.source.CancellableTask;
29 import org.netbeans.api.java.source.CompilationController;
30 import org.netbeans.api.java.source.JavaSource;
31 import org.netbeans.api.java.source.JavaSource.Phase;
32 import org.netbeans.api.java.source.TreePathHandle;
33 import org.netbeans.modules.refactoring.api.SafeDeleteRefactoring;
34 import org.netbeans.modules.refactoring.spi.ui.CustomRefactoringPanel;
35 import org.openide.filesystems.FileObject;
36 import org.openide.util.NbBundle;
37 import org.netbeans.modules.refactoring.java.RefactoringModule;
38 import org.openide.util.NbBundle;
39
40
41 /**
42  * Subclass of CustomRefactoringPanel representing the
43  * Safe Delete refactoring UI
44  * @author Bharath Ravikumar
45  */

46 public class SafeDeletePanel extends JPanel JavaDoc implements CustomRefactoringPanel {
47     
48     private final transient Collection JavaDoc elements;
49     private final transient SafeDeleteRefactoring refactoring;
50     
51     /**
52      * Creates new form RenamePanelName
53      * @param refactoring The SafeDelete refactoring used by this panel
54      * @param selectedElements A Collection of selected elements
55      */

56     public SafeDeletePanel(SafeDeleteRefactoring refactoring, Collection JavaDoc selectedElements) {
57         setName(NbBundle.getMessage(SafeDeletePanel.class,"LBL_SafeDel")); // NOI18N
58
this.elements = selectedElements;
59         this.refactoring = refactoring;
60         initComponents();
61     }
62     
63     private boolean initialized = false;
64     private String JavaDoc methodDeclaringClass = null;
65     
66     String JavaDoc getMethodDeclaringClass() {
67         return methodDeclaringClass;
68     }
69     /**
70      * Initialization method. Creates appropriate labels in the panel.
71      */

72     public void initialize() {
73         //This is needed since the checkBox is gets disabled on a
74
//repeated invocation of SafeDelete follwing removal of references
75
//to the element
76
searchInComments.setEnabled(true);
77         
78         if (initialized) return;
79         
80         final ArrayList JavaDoc<String JavaDoc> names = new ArrayList JavaDoc();
81         //TODO: should be improved
82
for (Object JavaDoc o:refactoring.getRefactoringSource().lookupAll(Object JavaDoc.class)) {
83             if (o instanceof FileObject)
84                 names.add(((FileObject)o).getName());
85             else if (o instanceof TreePathHandle) {
86                 final TreePathHandle handle=(TreePathHandle) o;
87                 //"really easy" way how to get name of the method
88
//from handle
89
JavaSource s = JavaSource.forFileObject(handle.getFileObject());
90                 try {
91                 s.runUserActionTask(new CancellableTask<CompilationController>() {
92                     public void cancel() {
93                     }
94
95                     public void run(CompilationController parameter) throws Exception JavaDoc {
96                         parameter.toPhase(Phase.RESOLVED);
97                         names.add(handle.resolveElement(parameter).getSimpleName().toString());
98                     }
99                 }, true);
100                 } catch (IOException JavaDoc ioe) {
101                     throw (RuntimeException JavaDoc) new RuntimeException JavaDoc().initCause(ioe);
102                 }
103                        
104             }
105         }
106 // final String labelText;
107
// if(elements.size() > 1){
108
// Iterator elementIterator = elements.iterator();
109
// Object elementToDelete = null;
110
// //Fix for bug 61742
111
// //One needs to peek into the list & check if there
112
// //is a resource which is being deleted. If so, only the resource's
113
// //name will be shown in the UI. Otherwise, the names of the multiple
114
// //features being deleted will be shown in the dialog.
115
// //I wish we didn't have to do this. This sure is avoidable. :-(
116
// while(elementIterator.hasNext()){
117
// Object localElementToDelete = elementIterator.next();
118
// if(localElementToDelete instanceof Resource){
119
// elementToDelete = localElementToDelete;
120
// break;
121
// }
122
// }
123
// if(elementToDelete != null){
124
// //You just have to display the resource file's name in the dialog now.
125
// String elementName = getElementName((Element)elementToDelete);
126
// labelText = NbBundle.getMessage(SafeDeletePanel.class, "LBL_SafeDelHeader",elementName);// NOI18N
127
// } else{
128
// //Display only the first two feature elements in the dialog for now.
129
// elementIterator = elements.iterator();
130
// Element firstElement = (Element) elementIterator.next();
131
// String firstElementName = getElementName(firstElement);
132
// String secondElementName = getElementName((Element) elementIterator.next());
133
// labelText = NbBundle.getMessage(SafeDeletePanel.class, "LBL_SafeDel_Elements",firstElementName, secondElementName);// NOI18N
134
// }
135
// }//End if elements size > 1
136
// else{
137
// Element element = (Element) elements.iterator().next();
138
// labelText = getCustomString(element);
139
// }
140

141         SwingUtilities.invokeLater(new Runnable JavaDoc() {
142             public void run() {
143                 if (names.size()==1) {
144                     label.setText(NbBundle.getMessage(SafeDeletePanel.class, "LBL_SafeDelHeader", names.get(0)));
145                 } else {
146                     label.setText(NbBundle.getMessage(SafeDeletePanel.class, "LBL_SafeDel_Elements", names.get(0), names.get(1)));
147                 }
148                 validate();
149             }
150         });
151         initialized = true;
152     }
153     
154     public void requestFocus() {
155         super.requestFocus();
156     }
157     
158     /** This method is called from within the constructor to
159      * initialize the form.
160      * WARNING: Do NOT modify this code. The content of this method is
161      * always regenerated by the Form Editor.
162      */

163     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
164
private void initComponents() {
165         buttonGroup = new javax.swing.ButtonGroup JavaDoc();
166         jPanel3 = new javax.swing.JPanel JavaDoc();
167         label = new javax.swing.JLabel JavaDoc();
168         searchInComments = new javax.swing.JCheckBox JavaDoc();
169
170         setLayout(new java.awt.BorderLayout JavaDoc());
171
172         jPanel3.setLayout(new java.awt.BorderLayout JavaDoc());
173
174         jPanel3.add(label, java.awt.BorderLayout.NORTH);
175
176         searchInComments.setSelected(((Boolean JavaDoc) RefactoringModule.getOption("searchInComments.whereUsed", Boolean.FALSE)).booleanValue());
177         java.util.ResourceBundle JavaDoc bundle = java.util.ResourceBundle.getBundle("org/netbeans/modules/refactoring/java/ui/Bundle"); // NOI18N
178
org.openide.awt.Mnemonics.setLocalizedText(searchInComments, bundle.getString("LBL_SafeDelInComents")); // NOI18N
179
searchInComments.setMargin(new java.awt.Insets JavaDoc(10, 14, 2, 2));
180         searchInComments.addItemListener(new java.awt.event.ItemListener JavaDoc() {
181             public void itemStateChanged(java.awt.event.ItemEvent JavaDoc evt) {
182                 searchInCommentsItemStateChanged(evt);
183             }
184         });
185
186         jPanel3.add(searchInComments, java.awt.BorderLayout.SOUTH);
187         searchInComments.getAccessibleContext().setAccessibleDescription(searchInComments.getText());
188
189         add(jPanel3, java.awt.BorderLayout.NORTH);
190
191     }// </editor-fold>//GEN-END:initComponents
192

193     private void searchInCommentsItemStateChanged(java.awt.event.ItemEvent JavaDoc evt) {//GEN-FIRST:event_searchInCommentsItemStateChanged
194
// used for change default value for deleteInComments check-box.
195
// The value is persisted and then used as default in next IDE run.
196
Boolean JavaDoc b = evt.getStateChange() == ItemEvent.SELECTED ? Boolean.TRUE : Boolean.FALSE;
197         RefactoringModule.setOption("searchInComments.whereUsed", b);
198         refactoring.setCheckInComments(b.booleanValue());
199     }//GEN-LAST:event_searchInCommentsItemStateChanged
200

201     // Variables declaration - do not modify//GEN-BEGIN:variables
202
private javax.swing.ButtonGroup JavaDoc buttonGroup;
203     private javax.swing.JPanel JavaDoc jPanel3;
204     private javax.swing.JLabel JavaDoc label;
205     private javax.swing.JCheckBox JavaDoc searchInComments;
206     // End of variables declaration//GEN-END:variables
207

208     
209     public Dimension JavaDoc getPreferredSize() {
210         Dimension JavaDoc orig = super.getPreferredSize();
211         return new Dimension JavaDoc(orig.width + 30 , orig.height + 30);
212     }
213     
214     /**
215      * Indicates whether the element usage must be checked in comments
216      * before deleting each element.
217      * @return Returns the isSelected() attribute of the
218      * underlying check box that controls search in comments
219      */

220     public boolean isSearchInComments() {
221         return searchInComments.isSelected();
222     }
223     
224     public Component JavaDoc getComponent() {
225         return this;
226     }
227
228     
229 //--public utility methods--
230
//
231
// //This method has been made public so that another class might be able to reuse this.
232
// //This should be moved to a common utility class.
233
// /**
234
// * Returns the formatted string corresponding to the declaration
235
// * of a CallableFeature(a {@link org.netbeans.jmi.javamodel.Method}
236
// * or a {@link org.netbeans.jmi.javamodel.Constructor})
237
// * Copied from {@link org.netbeans.modules.refactoring.ui.WhereUsedPanel}
238
// */
239
// public String getHeader(CallableFeature call) {
240
// if (((CallableFeatureImpl) call).getParser() == null) {
241
// if (call instanceof Method) {
242
// return ((Method) call).getName();
243
// } else if (call instanceof Constructor) {
244
// return getSimpleName(call.getDeclaringClass());
245
// }
246
// return "";
247
// }
248
// int s = ((MetadataElement) call).getPartStartOffset(ElementPartKindEnum.HEADER);
249
// int element = ((MetadataElement) call).getPartEndOffset(ElementPartKindEnum.HEADER);
250
// String result = call.getResource().getSourceText().substring(s,element);
251
// if (result.length() > 50) {
252
// result = result.substring(0,49) + "..."; // NOI18N
253
// }
254
// return CheckUtils.htmlize(result);
255
// }
256
//
257
// //This method has been made public so that another class might be able to reuse this.
258
// //This should be moved to a common utility class.
259
// /**
260
// * Returns the SimpleName for a class, accounting for
261
// * anonymous classes as well.
262
// * Copied from {@link org.netbeans.modules.refactoring.ui.WhereUsedPanel}
263
// */
264
// public String getSimpleName(ClassDefinition clazz) {
265
// if (clazz instanceof JavaClass) {
266
// return ((JavaClass) clazz).getSimpleName();
267
// } else {
268
// return NbBundle.getMessage(SafeDeleteUI.class, "LBL_AnonymousClass"); // NOI18N
269
// }
270
// }
271
//
272
//
273
////--private helper methods--
274
// /**
275
// * Returns a string constructed by passing the parameter to
276
// * <CODE>NbBundle.getMessage</CODE> with the Bundle key LBL_SafeDelHeader
277
// */
278
// private String getCustomString(Object refElement) {
279
// //This check for elementreference may not be needed, now that safe delete action
280
// //extracts the referred element. TODO:remove this check later.
281
// if(refElement instanceof ElementReference){
282
// NamedElement referredElement = ((ElementReference) refElement).getElement();
283
// return NbBundle.getMessage(SafeDeleteUI.class,"LBL_SafeDelRefWarning",referredElement.getName());//NOI18N
284
// } else{
285
// if(refElement instanceof Method)
286
// return NbBundle.getMessage(SafeDeleteUI.class,"LBL_SafeDelMethod",
287
// getHeader((CallableFeature)refElement),
288
// getSimpleName(((CallableFeature) refElement).getDeclaringClass())); // NOI18N
289
// else if (refElement instanceof Constructor) {
290
// return NbBundle.getMessage(SafeDeleteUI.class,"LBL_SafeDelConstructor",
291
// getHeader((CallableFeature)refElement),
292
// getSimpleName(((CallableFeature) refElement).getDeclaringClass())); // NOI18N
293
// } else if (refElement instanceof Field) {
294
// return NbBundle.getMessage(SafeDeleteUI.class,"LBL_SafeDelField",
295
// ((Field)refElement).getName(),
296
// getSimpleName(((Field) refElement).getDeclaringClass())); // NOI18N
297
// } else if (refElement instanceof Variable) {
298
// return NbBundle.getMessage(SafeDeleteUI.class,"LBL_SafeDelVariable",((Variable)refElement).getName());//NOI18N
299
// }
300
// else if(refElement instanceof NamedElement){
301
// //Handle any named element that is not of any of the above types
302
// String typeName = ((NamedElement) refElement).getName();
303
// return NbBundle.getMessage(SafeDeleteUI.class,"LBL_SafeDelHeader",typeName);//NOI18N
304
// }
305
// else
306
// return NbBundle.getMessage(SafeDeleteUI.class,"LBL_SafeDelHeader",refElement);//NOI18N
307
// }
308
// }
309
//
310
// private String getElementName(Element element) {
311
// if(element instanceof Resource){
312
// FileObject fileObject = JavaModel.getFileObject((Resource) element);
313
// return fileObject.getNameExt();
314
// } else
315
// return element.toString();
316
// }
317
//
318
}
319
320
Popular Tags