KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > fix > ConvertLoopCleanUp


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15 import java.util.Map JavaDoc;
16
17 import org.eclipse.core.runtime.CoreException;
18
19 import org.eclipse.jdt.core.ICompilationUnit;
20 import org.eclipse.jdt.core.dom.CompilationUnit;
21
22 import org.eclipse.jdt.internal.corext.fix.CleanUpConstants;
23 import org.eclipse.jdt.internal.corext.fix.ConvertLoopFix;
24 import org.eclipse.jdt.internal.corext.fix.IFix;
25
26 import org.eclipse.jdt.ui.text.java.IProblemLocation;
27
28 public class ConvertLoopCleanUp extends AbstractCleanUp {
29     
30     public ConvertLoopCleanUp(Map JavaDoc options) {
31         super(options);
32     }
33     
34     public ConvertLoopCleanUp() {
35         super();
36     }
37     
38     /**
39      * {@inheritDoc}
40      */

41     public boolean requireAST(ICompilationUnit unit) throws CoreException {
42         return isEnabled(CleanUpConstants.CONTROL_STATMENTS_CONVERT_FOR_LOOP_TO_ENHANCED);
43     }
44     
45     /**
46      * {@inheritDoc}
47      */

48     public IFix createFix(CompilationUnit compilationUnit) throws CoreException {
49         if (compilationUnit == null)
50             return null;
51         
52         boolean convertForLoops= isEnabled(CleanUpConstants.CONTROL_STATMENTS_CONVERT_FOR_LOOP_TO_ENHANCED);
53         
54         return ConvertLoopFix.createCleanUp(compilationUnit,
55                 convertForLoops, convertForLoops,
56                 isEnabled(CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL) && isEnabled(CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL_LOCAL_VARIABLES));
57     }
58     
59     /**
60      * {@inheritDoc}
61      */

62     public IFix createFix(CompilationUnit compilationUnit, IProblemLocation[] problems) throws CoreException {
63         //No warnings generated by the compiler
64
return null;
65     }
66     
67     /**
68      * {@inheritDoc}
69      */

70     public Map JavaDoc getRequiredOptions() {
71         return null;
72     }
73     
74     /**
75      * {@inheritDoc}
76      */

77     public String JavaDoc[] getDescriptions() {
78         List JavaDoc result= new ArrayList JavaDoc();
79         
80         if (isEnabled(CleanUpConstants.CONTROL_STATMENTS_CONVERT_FOR_LOOP_TO_ENHANCED))
81             result.add(MultiFixMessages.Java50CleanUp_ConvertToEnhancedForLoop_description);
82         
83         return (String JavaDoc[])result.toArray(new String JavaDoc[result.size()]);
84     }
85     
86     public String JavaDoc getPreview() {
87         StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
88         
89         if (isEnabled(CleanUpConstants.CONTROL_STATMENTS_CONVERT_FOR_LOOP_TO_ENHANCED)) {
90             buf.append("for (int element : ids) {\n"); //$NON-NLS-1$
91
buf.append(" double value= element / 2; \n"); //$NON-NLS-1$
92
buf.append(" System.out.println(value);\n"); //$NON-NLS-1$
93
buf.append("}\n"); //$NON-NLS-1$
94
} else {
95             buf.append("for (int i = 0; i < ids.length; i++) {\n"); //$NON-NLS-1$
96
buf.append(" double value= ids[i] / 2; \n"); //$NON-NLS-1$
97
buf.append(" System.out.println(value);\n"); //$NON-NLS-1$
98
buf.append("}\n"); //$NON-NLS-1$
99
}
100         
101         return buf.toString();
102     }
103     
104     /**
105      * {@inheritDoc}
106      */

107     public boolean canFix(CompilationUnit compilationUnit, IProblemLocation problem) throws CoreException {
108         return false;
109     }
110     
111     /**
112      * {@inheritDoc}
113      */

114     public int maximalNumberOfFixes(CompilationUnit compilationUnit) {
115         return -1;
116     }
117 }
118
Popular Tags