Removed
Link Here
|
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2000, 2006 IBM Corporation and others. |
3 |
* All rights reserved. This program and the accompanying materials |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
5 |
* which accompanies this distribution, and is available at |
6 |
* http://www.eclipse.org/legal/epl-v10.html |
7 |
* |
8 |
* Contributors: |
9 |
* IBM Corporation - initial API and implementation |
10 |
*******************************************************************************/ |
11 |
package org.eclipse.jdt.internal.ui.actions; |
12 |
|
13 |
import java.lang.reflect.InvocationTargetException; |
14 |
import java.util.Arrays; |
15 |
import java.util.Collection; |
16 |
import java.util.HashSet; |
17 |
|
18 |
import org.eclipse.core.runtime.CoreException; |
19 |
import org.eclipse.core.runtime.IStatus; |
20 |
import org.eclipse.core.runtime.MultiStatus; |
21 |
import org.eclipse.core.runtime.Status; |
22 |
import org.eclipse.core.runtime.preferences.DefaultScope; |
23 |
import org.eclipse.core.runtime.preferences.IEclipsePreferences; |
24 |
import org.eclipse.core.runtime.preferences.InstanceScope; |
25 |
import org.eclipse.core.runtime.preferences.IEclipsePreferences.IPreferenceChangeListener; |
26 |
import org.eclipse.core.runtime.preferences.IEclipsePreferences.PreferenceChangeEvent; |
27 |
|
28 |
import org.eclipse.jface.dialogs.ErrorDialog; |
29 |
import org.eclipse.jface.dialogs.MessageDialog; |
30 |
import org.eclipse.jface.viewers.IStructuredSelection; |
31 |
|
32 |
import org.eclipse.jface.text.ITextSelection; |
33 |
|
34 |
import org.eclipse.ui.IWorkbenchSite; |
35 |
|
36 |
import org.eclipse.jdt.core.ICompilationUnit; |
37 |
import org.eclipse.jdt.core.IJavaElement; |
38 |
import org.eclipse.jdt.core.IJavaProject; |
39 |
import org.eclipse.jdt.core.IPackageFragment; |
40 |
import org.eclipse.jdt.core.IPackageFragmentRoot; |
41 |
import org.eclipse.jdt.core.JavaModelException; |
42 |
|
43 |
import org.eclipse.jdt.internal.corext.fix.CleanUpConstants; |
44 |
import org.eclipse.jdt.internal.corext.refactoring.RefactoringExecutionStarter; |
45 |
import org.eclipse.jdt.internal.corext.util.JavaModelUtil; |
46 |
import org.eclipse.jdt.internal.corext.util.Messages; |
47 |
|
48 |
import org.eclipse.jdt.ui.IWorkingCopyManager; |
49 |
import org.eclipse.jdt.ui.JavaUI; |
50 |
import org.eclipse.jdt.ui.actions.SelectionDispatchAction; |
51 |
|
52 |
import org.eclipse.jdt.internal.ui.JavaPlugin; |
53 |
import org.eclipse.jdt.internal.ui.browsing.LogicalPackage; |
54 |
import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor; |
55 |
import org.eclipse.jdt.internal.ui.util.ElementValidator; |
56 |
|
57 |
public class CleanUpAction extends SelectionDispatchAction { |
58 |
|
59 |
private JavaEditor fEditor; |
60 |
private IPreferenceChangeListener fPreferenceChangeListener; |
61 |
|
62 |
public CleanUpAction(IWorkbenchSite site) { |
63 |
super(site); |
64 |
setToolTipText(ActionMessages.CleanUpAction_tooltip); |
65 |
setDescription(ActionMessages.CleanUpAction_description); |
66 |
|
67 |
fPreferenceChangeListener= new IPreferenceChangeListener() { |
68 |
public void preferenceChange(PreferenceChangeEvent event) { |
69 |
if (event.getKey().equals(CleanUpConstants.SHOW_CLEAN_UP_WIZARD)) { |
70 |
updateActionLabel(); |
71 |
} |
72 |
} |
73 |
}; |
74 |
new InstanceScope().getNode(JavaUI.ID_PLUGIN).addPreferenceChangeListener(fPreferenceChangeListener); |
75 |
|
76 |
updateActionLabel(); |
77 |
|
78 |
// PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.ORGANIZE_IMPORTS_ACTION); |
79 |
} |
80 |
|
81 |
/** |
82 |
* Note: This constructor is for internal use only. Clients should not call this constructor. |
83 |
* @param editor the Java editor |
84 |
*/ |
85 |
public CleanUpAction(JavaEditor editor) { |
86 |
this(editor.getEditorSite()); |
87 |
fEditor= editor; |
88 |
setEnabled(getCompilationUnit(fEditor) != null); |
89 |
} |
90 |
|
91 |
/* (non-Javadoc) |
92 |
* Method declared on SelectionDispatchAction. |
93 |
*/ |
94 |
public void run(ITextSelection selection) { |
95 |
ICompilationUnit cu= getCompilationUnit(fEditor); |
96 |
if (cu != null) { |
97 |
run(cu); |
98 |
} |
99 |
} |
100 |
|
101 |
/* (non-Javadoc) |
102 |
* Method declared on SelectionDispatchAction. |
103 |
*/ |
104 |
public void run(IStructuredSelection selection) { |
105 |
ICompilationUnit[] cus= getCompilationUnits(selection); |
106 |
if (cus.length == 0) { |
107 |
MessageDialog.openInformation(getShell(), ActionMessages.CleanUpAction_EmptySelection_title, ActionMessages.CleanUpAction_EmptySelection_description); |
108 |
} else if (cus.length == 1) { |
109 |
run(cus[0]); |
110 |
} else { |
111 |
runOnMultiple(cus); |
112 |
} |
113 |
} |
114 |
|
115 |
/* (non-Javadoc) |
116 |
* Method declared on SelectionDispatchAction. |
117 |
*/ |
118 |
public void selectionChanged(ITextSelection selection) { |
119 |
setEnabled(getCompilationUnit(fEditor) != null); |
120 |
} |
121 |
|
122 |
/* (non-Javadoc) |
123 |
* Method declared on SelectionDispatchAction. |
124 |
*/ |
125 |
public void selectionChanged(IStructuredSelection selection) { |
126 |
setEnabled(isEnabled(selection)); |
127 |
} |
128 |
|
129 |
private boolean isEnabled(IStructuredSelection selection) { |
130 |
Object[] selected= selection.toArray(); |
131 |
for (int i= 0; i < selected.length; i++) { |
132 |
try { |
133 |
if (selected[i] instanceof IJavaElement) { |
134 |
IJavaElement elem= (IJavaElement) selected[i]; |
135 |
if (elem.exists()) { |
136 |
switch (elem.getElementType()) { |
137 |
case IJavaElement.TYPE: |
138 |
return elem.getParent().getElementType() == IJavaElement.COMPILATION_UNIT; // for browsing perspective |
139 |
case IJavaElement.COMPILATION_UNIT: |
140 |
return true; |
141 |
case IJavaElement.IMPORT_CONTAINER: |
142 |
return true; |
143 |
case IJavaElement.PACKAGE_FRAGMENT: |
144 |
case IJavaElement.PACKAGE_FRAGMENT_ROOT: |
145 |
IPackageFragmentRoot root= (IPackageFragmentRoot) elem.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT); |
146 |
return (root.getKind() == IPackageFragmentRoot.K_SOURCE); |
147 |
case IJavaElement.JAVA_PROJECT: |
148 |
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=65638 |
149 |
return true; |
150 |
} |
151 |
} |
152 |
} else if (selected[i] instanceof LogicalPackage) { |
153 |
return true; |
154 |
} |
155 |
} catch (JavaModelException e) { |
156 |
if (!e.isDoesNotExist()) { |
157 |
JavaPlugin.log(e); |
158 |
} |
159 |
} |
160 |
} |
161 |
return false; |
162 |
} |
163 |
|
164 |
/** |
165 |
* Note: This method is for internal use only. Clients should not call this method. |
166 |
* @param cu The compilation unit to process |
167 |
*/ |
168 |
public void run(ICompilationUnit cu) { |
169 |
if (!ElementValidator.check(cu, getShell(), ActionMessages.CleanUpAction_ErrorDialogTitle, fEditor != null)) |
170 |
return; |
171 |
if (!ActionUtil.isEditable(fEditor, getShell(), cu)) |
172 |
return; |
173 |
|
174 |
try { |
175 |
RefactoringExecutionStarter.startCleanupRefactoring(new ICompilationUnit[] {cu}, showWizard(), getShell()); |
176 |
} catch (InvocationTargetException e) { |
177 |
JavaPlugin.log(e); |
178 |
if (e.getCause() instanceof CoreException) |
179 |
showUnexpectedError((CoreException)e.getCause()); |
180 |
} catch (JavaModelException e) { |
181 |
showUnexpectedError(e); |
182 |
} |
183 |
return; |
184 |
} |
185 |
|
186 |
/** |
187 |
* Perform on multiple compilation units. No editors are opened. |
188 |
* @param cus The compilation units to run on |
189 |
*/ |
190 |
public void runOnMultiple(final ICompilationUnit[] cus) { |
191 |
String message= ActionMessages.CleanUpAction_MultiStateErrorTitle; |
192 |
final MultiStatus status= new MultiStatus(JavaUI.ID_PLUGIN, IStatus.OK, message, null); |
193 |
|
194 |
for (int i= 0; i < cus.length; i++) { |
195 |
testOnBuildPath(cus[i], status); |
196 |
} |
197 |
|
198 |
if (!status.isOK()) { |
199 |
String title= ActionMessages.CleanUpAction_ErrorDialogTitle; |
200 |
ErrorDialog.openError(getShell(), title, null, status); |
201 |
return; |
202 |
} |
203 |
|
204 |
try { |
205 |
RefactoringExecutionStarter.startCleanupRefactoring(cus, showWizard(), getShell()); |
206 |
} catch (InvocationTargetException e) { |
207 |
JavaPlugin.log(e); |
208 |
if (e.getCause() instanceof CoreException) |
209 |
showUnexpectedError((CoreException)e.getCause()); |
210 |
} catch (JavaModelException e) { |
211 |
showUnexpectedError(e); |
212 |
} |
213 |
return; |
214 |
} |
215 |
|
216 |
private void showUnexpectedError(CoreException e) { |
217 |
String message2= Messages.format(ActionMessages.CleanUpAction_UnexpectedErrorMessage, e.getStatus().getMessage()); |
218 |
IStatus status= new Status(IStatus.ERROR, JavaUI.ID_PLUGIN, IStatus.ERROR, message2, null); |
219 |
String title= ActionMessages.CleanUpAction_ErrorDialogTitle; |
220 |
ErrorDialog.openError(getShell(), title, null, status); |
221 |
} |
222 |
|
223 |
private boolean testOnBuildPath(ICompilationUnit cu, MultiStatus status) { |
224 |
IJavaProject project= cu.getJavaProject(); |
225 |
if (!project.isOnClasspath(cu)) { |
226 |
String cuLocation= cu.getPath().makeRelative().toString(); |
227 |
String message= Messages.format(ActionMessages.CleanUpAction_CUNotOnBuildpathMessage, cuLocation); |
228 |
status.add(new Status(IStatus.INFO, JavaUI.ID_PLUGIN, IStatus.ERROR, message, null)); |
229 |
return false; |
230 |
} |
231 |
return true; |
232 |
} |
233 |
|
234 |
private ICompilationUnit[] getCompilationUnits(IStructuredSelection selection) { |
235 |
HashSet result= new HashSet(); |
236 |
Object[] selected= selection.toArray(); |
237 |
for (int i= 0; i < selected.length; i++) { |
238 |
try { |
239 |
if (selected[i] instanceof IJavaElement) { |
240 |
IJavaElement elem= (IJavaElement) selected[i]; |
241 |
if (elem.exists()) { |
242 |
|
243 |
switch (elem.getElementType()) { |
244 |
case IJavaElement.TYPE: |
245 |
if (elem.getParent().getElementType() == IJavaElement.COMPILATION_UNIT) { |
246 |
result.add(elem.getParent()); |
247 |
} |
248 |
break; |
249 |
case IJavaElement.COMPILATION_UNIT: |
250 |
result.add(elem); |
251 |
break; |
252 |
case IJavaElement.IMPORT_CONTAINER: |
253 |
result.add(elem.getParent()); |
254 |
break; |
255 |
case IJavaElement.PACKAGE_FRAGMENT: |
256 |
collectCompilationUnits((IPackageFragment) elem, result); |
257 |
break; |
258 |
case IJavaElement.PACKAGE_FRAGMENT_ROOT: |
259 |
collectCompilationUnits((IPackageFragmentRoot) elem, result); |
260 |
break; |
261 |
case IJavaElement.JAVA_PROJECT: |
262 |
IPackageFragmentRoot[] roots= ((IJavaProject) elem).getPackageFragmentRoots(); |
263 |
for (int k= 0; k < roots.length; k++) { |
264 |
collectCompilationUnits(roots[k], result); |
265 |
} |
266 |
break; |
267 |
} |
268 |
} |
269 |
} else if (selected[i] instanceof LogicalPackage) { |
270 |
IPackageFragment[] packageFragments= ((LogicalPackage)selected[i]).getFragments(); |
271 |
for (int k= 0; k < packageFragments.length; k++) { |
272 |
IPackageFragment pack= packageFragments[k]; |
273 |
if (pack.exists()) { |
274 |
collectCompilationUnits(pack, result); |
275 |
} |
276 |
} |
277 |
} |
278 |
} catch (JavaModelException e) { |
279 |
if (JavaModelUtil.isExceptionToBeLogged(e)) |
280 |
JavaPlugin.log(e); |
281 |
} |
282 |
} |
283 |
return (ICompilationUnit[]) result.toArray(new ICompilationUnit[result.size()]); |
284 |
} |
285 |
|
286 |
private void collectCompilationUnits(IPackageFragment pack, Collection result) throws JavaModelException { |
287 |
result.addAll(Arrays.asList(pack.getCompilationUnits())); |
288 |
} |
289 |
|
290 |
private void collectCompilationUnits(IPackageFragmentRoot root, Collection result) throws JavaModelException { |
291 |
if (root.getKind() == IPackageFragmentRoot.K_SOURCE) { |
292 |
IJavaElement[] children= root.getChildren(); |
293 |
for (int i= 0; i < children.length; i++) { |
294 |
collectCompilationUnits((IPackageFragment) children[i], result); |
295 |
} |
296 |
} |
297 |
} |
298 |
|
299 |
private static ICompilationUnit getCompilationUnit(JavaEditor editor) { |
300 |
IWorkingCopyManager manager= JavaPlugin.getDefault().getWorkingCopyManager(); |
301 |
ICompilationUnit cu= manager.getWorkingCopy(editor.getEditorInput()); |
302 |
return cu; |
303 |
} |
304 |
|
305 |
private boolean showWizard() { |
306 |
InstanceScope instanceScope= new InstanceScope(); |
307 |
IEclipsePreferences instanceNode= instanceScope.getNode(JavaUI.ID_PLUGIN); |
308 |
if (instanceNode.get(CleanUpConstants.SHOW_CLEAN_UP_WIZARD, null) != null) |
309 |
return instanceNode.getBoolean(CleanUpConstants.SHOW_CLEAN_UP_WIZARD, true); |
310 |
|
311 |
DefaultScope defaultScope= new DefaultScope(); |
312 |
IEclipsePreferences defaultNode= defaultScope.getNode(JavaUI.ID_PLUGIN); |
313 |
return defaultNode.getBoolean(CleanUpConstants.SHOW_CLEAN_UP_WIZARD, true); |
314 |
} |
315 |
|
316 |
private void updateActionLabel() { |
317 |
if (showWizard()) { |
318 |
setText(ActionMessages.CleanUpAction_labelWizard); |
319 |
} else { |
320 |
setText(ActionMessages.CleanUpAction_label); |
321 |
} |
322 |
} |
323 |
|
324 |
public void dispose() { |
325 |
if (fPreferenceChangeListener != null) { |
326 |
new InstanceScope().getNode(JavaUI.ID_PLUGIN).removePreferenceChangeListener(fPreferenceChangeListener); |
327 |
fPreferenceChangeListener= null; |
328 |
} |
329 |
} |
330 |
} |