001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.ui.fix;
011:
012: import java.util.ArrayList;
013: import java.util.Hashtable;
014: import java.util.List;
015: import java.util.Map;
016:
017: import org.eclipse.core.runtime.CoreException;
018:
019: import org.eclipse.jdt.core.ICompilationUnit;
020: import org.eclipse.jdt.core.JavaCore;
021: import org.eclipse.jdt.core.compiler.IProblem;
022: import org.eclipse.jdt.core.dom.CompilationUnit;
023:
024: import org.eclipse.jdt.internal.corext.fix.CleanUpConstants;
025: import org.eclipse.jdt.internal.corext.fix.IFix;
026: import org.eclipse.jdt.internal.corext.fix.StringFix;
027:
028: import org.eclipse.jdt.ui.text.java.IProblemLocation;
029:
030: /**
031: * Create fixes which can solve problems in connection with Strings
032: * @see org.eclipse.jdt.internal.corext.fix.StringFix
033: *
034: */
035: public class StringCleanUp extends AbstractMultiFix {
036:
037: public StringCleanUp(Map options) {
038: super (options);
039: }
040:
041: public StringCleanUp() {
042: super ();
043: }
044:
045: /**
046: * {@inheritDoc}
047: */
048: public CleanUpRequirements getRequirements() {
049: return new CleanUpRequirements(requireAST(), false,
050: getRequiredOptions());
051: }
052:
053: private boolean requireAST() {
054: return isEnabled(CleanUpConstants.ADD_MISSING_NLS_TAGS)
055: || isEnabled(CleanUpConstants.REMOVE_UNNECESSARY_NLS_TAGS);
056: }
057:
058: /**
059: * {@inheritDoc}
060: */
061: protected IFix createFix(CompilationUnit compilationUnit)
062: throws CoreException {
063: if (compilationUnit == null)
064: return null;
065:
066: return StringFix
067: .createCleanUp(
068: compilationUnit,
069: isEnabled(CleanUpConstants.ADD_MISSING_NLS_TAGS),
070: isEnabled(CleanUpConstants.REMOVE_UNNECESSARY_NLS_TAGS));
071: }
072:
073: /**
074: * {@inheritDoc}
075: */
076: protected IFix createFix(CompilationUnit compilationUnit,
077: IProblemLocation[] problems) throws CoreException {
078: if (compilationUnit == null)
079: return null;
080:
081: return StringFix
082: .createCleanUp(
083: compilationUnit,
084: problems,
085: isEnabled(CleanUpConstants.ADD_MISSING_NLS_TAGS),
086: isEnabled(CleanUpConstants.REMOVE_UNNECESSARY_NLS_TAGS));
087: }
088:
089: private Map getRequiredOptions() {
090: Map result = new Hashtable();
091:
092: if (isEnabled(CleanUpConstants.ADD_MISSING_NLS_TAGS)
093: || isEnabled(CleanUpConstants.REMOVE_UNNECESSARY_NLS_TAGS))
094: result.put(JavaCore.COMPILER_PB_NON_NLS_STRING_LITERAL,
095: JavaCore.WARNING);
096:
097: return result;
098: }
099:
100: /**
101: * {@inheritDoc}
102: */
103: public String[] getDescriptions() {
104: List result = new ArrayList();
105: if (isEnabled(CleanUpConstants.ADD_MISSING_NLS_TAGS))
106: result
107: .add(MultiFixMessages.StringMultiFix_AddMissingNonNls_description);
108: if (isEnabled(CleanUpConstants.REMOVE_UNNECESSARY_NLS_TAGS))
109: result
110: .add(MultiFixMessages.StringMultiFix_RemoveUnnecessaryNonNls_description);
111: return (String[]) result.toArray(new String[result.size()]);
112: }
113:
114: /**
115: * {@inheritDoc}
116: */
117: public String getPreview() {
118: StringBuffer buf = new StringBuffer();
119:
120: if (isEnabled(CleanUpConstants.REMOVE_UNNECESSARY_NLS_TAGS)) {
121: buf.append("public String s;"); //$NON-NLS-1$
122: } else {
123: buf.append("public String s; //$NON-NLS-1$"); //$NON-NLS-1$
124: }
125:
126: return buf.toString();
127: }
128:
129: /**
130: * {@inheritDoc}
131: */
132: public boolean canFix(ICompilationUnit compilationUnit,
133: IProblemLocation problem) {
134: if (problem.getProblemId() == IProblem.UnnecessaryNLSTag)
135: return isEnabled(CleanUpConstants.REMOVE_UNNECESSARY_NLS_TAGS);
136:
137: if (problem.getProblemId() == IProblem.NonExternalizedStringLiteral)
138: return isEnabled(CleanUpConstants.ADD_MISSING_NLS_TAGS);
139:
140: return false;
141: }
142:
143: /**
144: * {@inheritDoc}
145: */
146: public int computeNumberOfFixes(CompilationUnit compilationUnit) {
147: int result = 0;
148: IProblem[] problems = compilationUnit.getProblems();
149: if (isEnabled(CleanUpConstants.ADD_MISSING_NLS_TAGS))
150: result += getNumberOfProblems(problems,
151: IProblem.NonExternalizedStringLiteral);
152:
153: if (isEnabled(CleanUpConstants.REMOVE_UNNECESSARY_NLS_TAGS))
154: result += getNumberOfProblems(problems,
155: IProblem.UnnecessaryNLSTag);
156:
157: return result;
158: }
159: }
|