KickJava   Java API By Example, From Geeks To Geeks.

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


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.Hashtable JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.Map JavaDoc;
17
18 import org.eclipse.core.runtime.CoreException;
19
20 import org.eclipse.jdt.core.ICompilationUnit;
21 import org.eclipse.jdt.core.JavaCore;
22 import org.eclipse.jdt.core.compiler.IProblem;
23 import org.eclipse.jdt.core.dom.CompilationUnit;
24
25 import org.eclipse.jdt.internal.corext.fix.CleanUpConstants;
26 import org.eclipse.jdt.internal.corext.fix.IFix;
27 import org.eclipse.jdt.internal.corext.fix.StringFix;
28
29 import org.eclipse.jdt.ui.text.java.IProblemLocation;
30
31 /**
32  * Create fixes which can solve problems in connection with Strings
33  * @see org.eclipse.jdt.internal.corext.fix.StringFix
34  *
35  */

36 public class StringCleanUp extends AbstractCleanUp {
37     
38     public StringCleanUp(Map JavaDoc options) {
39         super(options);
40     }
41     
42     public StringCleanUp() {
43         super();
44     }
45     
46     /**
47      * {@inheritDoc}
48      */

49     public boolean requireAST(ICompilationUnit unit) throws CoreException {
50         return isEnabled(CleanUpConstants.ADD_MISSING_NLS_TAGS) ||
51                isEnabled(CleanUpConstants.REMOVE_UNNECESSARY_NLS_TAGS);
52     }
53
54     public IFix createFix(CompilationUnit compilationUnit) throws CoreException {
55         if (compilationUnit == null)
56             return null;
57
58         return StringFix.createCleanUp(compilationUnit,
59                 isEnabled(CleanUpConstants.ADD_MISSING_NLS_TAGS),
60                 isEnabled(CleanUpConstants.REMOVE_UNNECESSARY_NLS_TAGS));
61     }
62     
63     /**
64      * {@inheritDoc}
65      */

66     public IFix createFix(CompilationUnit compilationUnit, IProblemLocation[] problems) throws CoreException {
67         if (compilationUnit == null)
68             return null;
69         
70         return StringFix.createCleanUp(compilationUnit, problems,
71                 isEnabled(CleanUpConstants.ADD_MISSING_NLS_TAGS),
72                 isEnabled(CleanUpConstants.REMOVE_UNNECESSARY_NLS_TAGS));
73     }
74
75     public Map JavaDoc getRequiredOptions() {
76         Map JavaDoc result= new Hashtable JavaDoc();
77         
78         if (isEnabled(CleanUpConstants.ADD_MISSING_NLS_TAGS) || isEnabled(CleanUpConstants.REMOVE_UNNECESSARY_NLS_TAGS))
79             result.put(JavaCore.COMPILER_PB_NON_NLS_STRING_LITERAL, JavaCore.WARNING);
80         
81         return result;
82     }
83
84     /**
85      * {@inheritDoc}
86      */

87     public String JavaDoc[] getDescriptions() {
88         List JavaDoc result= new ArrayList JavaDoc();
89         if (isEnabled(CleanUpConstants.ADD_MISSING_NLS_TAGS))
90             result.add(MultiFixMessages.StringMultiFix_AddMissingNonNls_description);
91         if (isEnabled(CleanUpConstants.REMOVE_UNNECESSARY_NLS_TAGS))
92             result.add(MultiFixMessages.StringMultiFix_RemoveUnnecessaryNonNls_description);
93         return (String JavaDoc[])result.toArray(new String JavaDoc[result.size()]);
94     }
95     
96     /**
97      * {@inheritDoc}
98      */

99     public String JavaDoc getPreview() {
100         StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
101         
102         if (isEnabled(CleanUpConstants.REMOVE_UNNECESSARY_NLS_TAGS)) {
103             buf.append("public String s;"); //$NON-NLS-1$
104
} else {
105             buf.append("public String s; //$NON-NLS-1$"); //$NON-NLS-1$
106
}
107         
108         return buf.toString();
109     }
110
111     /**
112      * {@inheritDoc}
113      * @throws CoreException
114      */

115     public boolean canFix(CompilationUnit compilationUnit, IProblemLocation problem) throws CoreException {
116         return StringFix.createFix(compilationUnit, problem, isEnabled(CleanUpConstants.REMOVE_UNNECESSARY_NLS_TAGS), isEnabled(CleanUpConstants.ADD_MISSING_NLS_TAGS)) != null;
117     }
118
119     /**
120      * {@inheritDoc}
121      */

122     public int maximalNumberOfFixes(CompilationUnit compilationUnit) {
123         int result= 0;
124         IProblem[] problems= compilationUnit.getProblems();
125         if (isEnabled(CleanUpConstants.ADD_MISSING_NLS_TAGS))
126             result+= getNumberOfProblems(problems, IProblem.NonExternalizedStringLiteral);
127         
128         if (isEnabled(CleanUpConstants.REMOVE_UNNECESSARY_NLS_TAGS))
129             result+= getNumberOfProblems(problems, IProblem.UnnecessaryNLSTag);
130         
131         return result;
132     }
133 }
134
Popular Tags