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.Map;
013:
014: import org.eclipse.core.runtime.CoreException;
015: import org.eclipse.core.runtime.IProgressMonitor;
016:
017: import org.eclipse.ltk.core.refactoring.RefactoringStatus;
018:
019: import org.eclipse.jdt.core.ICompilationUnit;
020: import org.eclipse.jdt.core.IJavaProject;
021: import org.eclipse.jdt.core.dom.CompilationUnit;
022:
023: import org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings;
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.ImportsFix;
027:
028: import org.eclipse.jdt.internal.ui.preferences.JavaPreferencesSettings;
029:
030: public class ImportsCleanUp extends AbstractCleanUp {
031:
032: private CodeGenerationSettings fCodeGeneratorSettings;
033: private RefactoringStatus fStatus;
034:
035: public ImportsCleanUp(Map options) {
036: super (options);
037: }
038:
039: public ImportsCleanUp() {
040: super ();
041: }
042:
043: /**
044: * {@inheritDoc}
045: */
046: public CleanUpRequirements getRequirements() {
047: boolean isOrganizeImports = isEnabled(CleanUpConstants.ORGANIZE_IMPORTS);
048: return new CleanUpRequirements(isOrganizeImports,
049: isOrganizeImports, null);
050: }
051:
052: /**
053: * {@inheritDoc}
054: */
055: public IFix createFix(CleanUpContext context) throws CoreException {
056: CompilationUnit compilationUnit = context.getAST();
057: if (compilationUnit == null)
058: return null;
059:
060: return ImportsFix.createCleanUp(compilationUnit,
061: fCodeGeneratorSettings,
062: isEnabled(CleanUpConstants.ORGANIZE_IMPORTS), fStatus);
063: }
064:
065: /**
066: * {@inheritDoc}
067: */
068: public RefactoringStatus checkPreConditions(IJavaProject project,
069: ICompilationUnit[] compilationUnits,
070: IProgressMonitor monitor) throws CoreException {
071:
072: if (isEnabled(CleanUpConstants.ORGANIZE_IMPORTS)) {
073: fCodeGeneratorSettings = JavaPreferencesSettings
074: .getCodeGenerationSettings(project);
075: fStatus = new RefactoringStatus();
076: }
077:
078: return super .checkPreConditions(project, compilationUnits,
079: monitor);
080: }
081:
082: /**
083: * {@inheritDoc}
084: */
085: public RefactoringStatus checkPostConditions(
086: IProgressMonitor monitor) throws CoreException {
087: fCodeGeneratorSettings = null;
088: if (fStatus == null || fStatus.isOK()) {
089: return super .checkPostConditions(monitor);
090: } else {
091: return fStatus;
092: }
093: }
094:
095: /**
096: * {@inheritDoc}
097: */
098: public String[] getDescriptions() {
099: if (isEnabled(CleanUpConstants.ORGANIZE_IMPORTS))
100: return new String[] { MultiFixMessages.ImportsCleanUp_OrganizeImports_Description };
101:
102: return null;
103: }
104:
105: /**
106: * {@inheritDoc}
107: */
108: public String getPreview() {
109: StringBuffer buf = new StringBuffer();
110:
111: if (isEnabled(CleanUpConstants.ORGANIZE_IMPORTS)) {
112: buf.append("import org.model.Engine;\n"); //$NON-NLS-1$
113: } else {
114: buf.append("import org.model.*;\n"); //$NON-NLS-1$
115: }
116:
117: return buf.toString();
118: }
119:
120: }
|