]>
Commit | Line | Data |
---|---|---|
1b2798f6 EK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2000, 2011 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.fix; | |
12 | ||
13 | import java.util.Map; | |
14 | ||
15 | import org.eclipse.core.runtime.CoreException; | |
16 | import org.eclipse.core.runtime.IProgressMonitor; | |
17 | ||
18 | import org.eclipse.ltk.core.refactoring.RefactoringStatus; | |
19 | ||
20 | import org.eclipse.jdt.core.ICompilationUnit; | |
21 | import org.eclipse.jdt.core.IJavaProject; | |
22 | import org.eclipse.jdt.core.dom.CompilationUnit; | |
23 | ||
24 | import org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings; | |
25 | import org.eclipse.jdt.internal.corext.fix.CleanUpConstants; | |
26 | import org.eclipse.jdt.internal.corext.fix.ImportsFix; | |
27 | ||
28 | import org.eclipse.jdt.ui.cleanup.CleanUpContext; | |
29 | import org.eclipse.jdt.ui.cleanup.CleanUpRequirements; | |
30 | import org.eclipse.jdt.ui.cleanup.ICleanUpFix; | |
31 | ||
32 | import org.eclipse.jdt.internal.ui.preferences.JavaPreferencesSettings; | |
33 | ||
34 | public class ImportsCleanUp extends AbstractCleanUp { | |
35 | ||
36 | private CodeGenerationSettings fCodeGeneratorSettings; | |
37 | private RefactoringStatus fStatus; | |
38 | ||
39 | public ImportsCleanUp(Map<String, String> options) { | |
40 | super(options); | |
41 | } | |
42 | ||
43 | public ImportsCleanUp() { | |
44 | super(); | |
45 | } | |
46 | ||
47 | /** | |
48 | * {@inheritDoc} | |
49 | */ | |
50 | @Override | |
51 | public CleanUpRequirements getRequirements() { | |
52 | boolean isOrganizeImports= isEnabled(CleanUpConstants.ORGANIZE_IMPORTS); | |
53 | return new CleanUpRequirements(isOrganizeImports, isOrganizeImports, false, null); | |
54 | } | |
55 | ||
56 | /** | |
57 | * {@inheritDoc} | |
58 | */ | |
59 | @Override | |
60 | public ICleanUpFix createFix(CleanUpContext context) throws CoreException { | |
61 | CompilationUnit compilationUnit= context.getAST(); | |
62 | if (compilationUnit == null) | |
63 | return null; | |
64 | ||
65 | return ImportsFix.createCleanUp(compilationUnit, fCodeGeneratorSettings, | |
66 | isEnabled(CleanUpConstants.ORGANIZE_IMPORTS), fStatus); | |
67 | } | |
68 | ||
69 | /** | |
70 | * {@inheritDoc} | |
71 | */ | |
72 | @Override | |
73 | public RefactoringStatus checkPreConditions(IJavaProject project, ICompilationUnit[] compilationUnits, IProgressMonitor monitor) throws CoreException { | |
74 | ||
75 | if (isEnabled(CleanUpConstants.ORGANIZE_IMPORTS)) { | |
76 | fCodeGeneratorSettings= JavaPreferencesSettings.getCodeGenerationSettings(project); | |
77 | fStatus= new RefactoringStatus(); | |
78 | } | |
79 | ||
80 | return super.checkPreConditions(project, compilationUnits, monitor); | |
81 | } | |
82 | ||
83 | /** | |
84 | * {@inheritDoc} | |
85 | */ | |
86 | @Override | |
87 | public RefactoringStatus checkPostConditions(IProgressMonitor monitor) throws CoreException { | |
88 | fCodeGeneratorSettings= null; | |
89 | try { | |
90 | if (fStatus == null || fStatus.isOK()) { | |
91 | return super.checkPostConditions(monitor); | |
92 | } else { | |
93 | return fStatus; | |
94 | } | |
95 | } finally { | |
96 | fStatus= null; | |
97 | } | |
98 | } | |
99 | ||
100 | /** | |
101 | * {@inheritDoc} | |
102 | */ | |
103 | @Override | |
104 | public String[] getStepDescriptions() { | |
105 | if (isEnabled(CleanUpConstants.ORGANIZE_IMPORTS)) | |
106 | return new String[] {MultiFixMessages.ImportsCleanUp_OrganizeImports_Description}; | |
107 | ||
108 | return null; | |
109 | } | |
110 | ||
111 | /** | |
112 | * {@inheritDoc} | |
113 | */ | |
114 | @Override | |
115 | public String getPreview() { | |
116 | StringBuffer buf= new StringBuffer(); | |
117 | ||
118 | if (isEnabled(CleanUpConstants.ORGANIZE_IMPORTS)) { | |
119 | buf.append("import org.model.Engine;\n"); //$NON-NLS-1$ | |
120 | } else { | |
121 | buf.append("import org.model.*;\n"); //$NON-NLS-1$ | |
122 | } | |
123 | ||
124 | return buf.toString(); | |
125 | } | |
126 | ||
127 | } |