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: * Alex Blewitt - https://bugs.eclipse.org/bugs/show_bug.cgi?id=168954
011: *******************************************************************************/package org.eclipse.jdt.internal.ui.fix;
012:
013: import com.ibm.icu.text.MessageFormat;
014:
015: import java.util.HashSet;
016: import java.util.Iterator;
017: import java.util.Map;
018:
019: import org.eclipse.core.runtime.CoreException;
020: import org.eclipse.core.runtime.IProgressMonitor;
021: import org.eclipse.core.runtime.NullProgressMonitor;
022:
023: import org.eclipse.core.resources.IFile;
024: import org.eclipse.core.resources.IMarker;
025: import org.eclipse.core.resources.IResource;
026:
027: import org.eclipse.debug.core.model.IBreakpoint;
028:
029: import org.eclipse.ltk.core.refactoring.RefactoringStatus;
030:
031: import org.eclipse.jdt.core.ICompilationUnit;
032: import org.eclipse.jdt.core.dom.CompilationUnit;
033:
034: import org.eclipse.jdt.internal.corext.fix.CleanUpConstants;
035: import org.eclipse.jdt.internal.corext.fix.IFix;
036: import org.eclipse.jdt.internal.corext.fix.SortMembersFix;
037:
038: public class SortMembersCleanUp extends AbstractCleanUp {
039:
040: private HashSet fTouchedFiles;
041:
042: public SortMembersCleanUp() {
043: super ();
044: }
045:
046: public SortMembersCleanUp(Map options) {
047: super (options);
048: }
049:
050: /**
051: * {@inheritDoc}
052: */
053: public IFix createFix(CleanUpContext context) throws CoreException {
054: CompilationUnit compilationUnit = context.getAST();
055: if (compilationUnit == null)
056: return null;
057:
058: boolean sortMembers = isEnabled(CleanUpConstants.SORT_MEMBERS);
059: IFix fix = SortMembersFix
060: .createCleanUp(
061: compilationUnit,
062: sortMembers,
063: sortMembers
064: && isEnabled(CleanUpConstants.SORT_MEMBERS_ALL));
065: if (fix != null) {
066: if (fTouchedFiles == null) {
067: fTouchedFiles = new HashSet();
068: }
069: fTouchedFiles.add(((ICompilationUnit) compilationUnit
070: .getJavaElement()).getResource());
071: }
072: return fix;
073: }
074:
075: /**
076: * {@inheritDoc}
077: */
078: public RefactoringStatus checkPostConditions(
079: IProgressMonitor monitor) throws CoreException {
080: if (fTouchedFiles == null) {
081: return super .checkPostConditions(monitor);
082: } else {
083: if (monitor == null)
084: monitor = new NullProgressMonitor();
085:
086: monitor.beginTask("", fTouchedFiles.size()); //$NON-NLS-1$
087:
088: try {
089: RefactoringStatus result = new RefactoringStatus();
090: for (Iterator iterator = fTouchedFiles.iterator(); iterator
091: .hasNext();) {
092: IFile file = (IFile) iterator.next();
093: if (containsRelevantMarkers(file)) {
094: String fileLocation = file
095: .getProjectRelativePath().toOSString();
096: String projectName = file.getProject()
097: .getName();
098: result
099: .addWarning(MessageFormat
100: .format(
101: MultiFixMessages.SortMembersCleanUp_RemoveMarkersWarning0,
102: new Object[] {
103: fileLocation,
104: projectName }));
105: }
106:
107: monitor.worked(1);
108: }
109:
110: return result;
111: } finally {
112: monitor.done();
113: fTouchedFiles = null;
114: }
115:
116: }
117: }
118:
119: /**
120: * {@inheritDoc}
121: */
122: public String[] getDescriptions() {
123: if (isEnabled(CleanUpConstants.SORT_MEMBERS)) {
124: if (isEnabled(CleanUpConstants.SORT_MEMBERS_ALL)) {
125: return new String[] { MultiFixMessages.SortMembersCleanUp_AllMembers_description };
126: } else {
127: return new String[] { MultiFixMessages.SortMembersCleanUp_Excluding_description };
128: }
129: }
130: return null;
131: }
132:
133: /**
134: * {@inheritDoc}
135: */
136: public String getPreview() {
137: StringBuffer buf = new StringBuffer();
138:
139: buf.append("class SortExample {\n"); //$NON-NLS-1$
140:
141: if ((isEnabled(CleanUpConstants.SORT_MEMBERS) && isEnabled(CleanUpConstants.SORT_MEMBERS_ALL))) {
142: buf.append(" private String bar;\n"); //$NON-NLS-1$
143: buf.append(" private String foo;\n"); //$NON-NLS-1$
144: } else {
145: buf.append(" private String foo;\n"); //$NON-NLS-1$
146: buf.append(" private String bar;\n"); //$NON-NLS-1$
147: }
148:
149: if (isEnabled(CleanUpConstants.SORT_MEMBERS)) {
150: buf.append(" private void bar() {}\n"); //$NON-NLS-1$
151: buf.append(" private void foo() {}\n"); //$NON-NLS-1$
152: } else {
153: buf.append(" private void foo() {}\n"); //$NON-NLS-1$
154: buf.append(" private void bar() {}\n"); //$NON-NLS-1$
155: }
156:
157: buf.append("}\n"); //$NON-NLS-1$
158:
159: return buf.toString();
160: }
161:
162: /**
163: * {@inheritDoc}
164: */
165: public CleanUpRequirements getRequirements() {
166: return new CleanUpRequirements(
167: isEnabled(CleanUpConstants.SORT_MEMBERS), false, null);
168: }
169:
170: private static boolean containsRelevantMarkers(IFile file)
171: throws CoreException {
172: IMarker[] bookmarks = file.findMarkers(IMarker.BOOKMARK, true,
173: IResource.DEPTH_INFINITE);
174: if (bookmarks.length != 0)
175: return true;
176:
177: IMarker[] tasks = file.findMarkers(IMarker.TASK, true,
178: IResource.DEPTH_INFINITE);
179: if (tasks.length != 0)
180: return true;
181:
182: IMarker[] breakpoints = file.findMarkers(
183: IBreakpoint.BREAKPOINT_MARKER, true,
184: IResource.DEPTH_INFINITE);
185: if (breakpoints.length != 0)
186: return true;
187:
188: return false;
189: }
190:
191: }
|