001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 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.corext.refactoring.reorg;
011:
012: import java.util.ArrayList;
013: import java.util.Arrays;
014: import java.util.HashSet;
015: import java.util.List;
016: import java.util.Set;
017:
018: import org.eclipse.core.runtime.Assert;
019: import org.eclipse.core.runtime.CoreException;
020: import org.eclipse.core.runtime.IProgressMonitor;
021: import org.eclipse.core.runtime.OperationCanceledException;
022: import org.eclipse.core.runtime.SubProgressMonitor;
023:
024: import org.eclipse.core.resources.IResource;
025:
026: import org.eclipse.ltk.core.refactoring.Change;
027: import org.eclipse.ltk.core.refactoring.ChangeDescriptor;
028: import org.eclipse.ltk.core.refactoring.CompositeChange;
029: import org.eclipse.ltk.core.refactoring.RefactoringStatus;
030: import org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext;
031: import org.eclipse.ltk.core.refactoring.participants.CopyProcessor;
032: import org.eclipse.ltk.core.refactoring.participants.RefactoringArguments;
033: import org.eclipse.ltk.core.refactoring.participants.RefactoringParticipant;
034: import org.eclipse.ltk.core.refactoring.participants.ReorgExecutionLog;
035: import org.eclipse.ltk.core.refactoring.participants.SharableParticipants;
036:
037: import org.eclipse.jdt.core.IJavaElement;
038: import org.eclipse.jdt.core.JavaModelException;
039:
040: import org.eclipse.jdt.internal.corext.refactoring.JavaRefactoringArguments;
041: import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
042: import org.eclipse.jdt.internal.corext.refactoring.changes.DynamicValidationStateChange;
043: import org.eclipse.jdt.internal.corext.refactoring.participants.JavaProcessors;
044: import org.eclipse.jdt.internal.corext.refactoring.participants.ResourceProcessors;
045: import org.eclipse.jdt.internal.corext.refactoring.reorg.IReorgPolicy.ICopyPolicy;
046: import org.eclipse.jdt.internal.corext.refactoring.tagging.ICommentProvider;
047: import org.eclipse.jdt.internal.corext.refactoring.tagging.IScriptableRefactoring;
048: import org.eclipse.jdt.internal.corext.util.Resources;
049:
050: import org.eclipse.jdt.ui.refactoring.IRefactoringProcessorIds;
051:
052: public final class JavaCopyProcessor extends CopyProcessor implements
053: IReorgDestinationValidator, IScriptableRefactoring,
054: ICommentProvider {
055:
056: private String fComment;
057:
058: private ICopyPolicy fCopyPolicy;
059:
060: private ReorgExecutionLog fExecutionLog;
061:
062: private INewNameQueries fNewNameQueries;
063:
064: private IReorgQueries fReorgQueries;
065:
066: public JavaCopyProcessor(ICopyPolicy copyPolicy) {
067: fCopyPolicy = copyPolicy;
068: }
069:
070: public boolean canChildrenBeDestinations(
071: IReorgDestination destination) {
072: return fCopyPolicy.canChildrenBeDestinations(destination);
073: }
074:
075: public boolean canElementBeDestination(IReorgDestination destination) {
076: return fCopyPolicy.canElementBeDestination(destination);
077: }
078:
079: public boolean canEnableComment() {
080: return true;
081: }
082:
083: public RefactoringStatus checkFinalConditions(IProgressMonitor pm,
084: CheckConditionsContext context) throws CoreException {
085: Assert.isNotNull(fNewNameQueries, "Missing new name queries"); //$NON-NLS-1$
086: Assert.isNotNull(fReorgQueries, "Missing reorg queries"); //$NON-NLS-1$
087: pm.beginTask("", 2); //$NON-NLS-1$
088: RefactoringStatus result = fCopyPolicy.checkFinalConditions(
089: new SubProgressMonitor(pm, 1), context, fReorgQueries);
090: result.merge(context.check(new SubProgressMonitor(pm, 1)));
091: return result;
092: }
093:
094: public RefactoringStatus checkInitialConditions(IProgressMonitor pm)
095: throws CoreException {
096: RefactoringStatus result = new RefactoringStatus();
097: result.merge(RefactoringStatus.create(Resources
098: .checkInSync(ReorgUtils.getNotNulls(fCopyPolicy
099: .getResources()))));
100: IResource[] javaResources = ReorgUtils.getResources(fCopyPolicy
101: .getJavaElements());
102: result.merge(RefactoringStatus.create(Resources
103: .checkInSync(ReorgUtils.getNotNulls(javaResources))));
104: return result;
105: }
106:
107: public Change createChange(IProgressMonitor pm)
108: throws CoreException {
109: Assert.isNotNull(fNewNameQueries);
110: Assert.isTrue(fCopyPolicy.getJavaElementDestination() == null
111: || fCopyPolicy.getResourceDestination() == null);
112: Assert.isTrue(fCopyPolicy.getJavaElementDestination() != null
113: || fCopyPolicy.getResourceDestination() != null);
114: try {
115: final DynamicValidationStateChange result = new DynamicValidationStateChange(
116: getChangeName()) {
117:
118: public Object getAdapter(Class adapter) {
119: if (ReorgExecutionLog.class.equals(adapter))
120: return fExecutionLog;
121: return super .getAdapter(adapter);
122: }
123:
124: public ChangeDescriptor getDescriptor() {
125: return fCopyPolicy.getDescriptor();
126: }
127:
128: public Change perform(IProgressMonitor pm2)
129: throws CoreException {
130: try {
131: super .perform(pm2);
132: } catch (OperationCanceledException e) {
133: fExecutionLog.markAsCanceled();
134: throw e;
135: }
136: return null;
137: }
138: };
139: Change change = fCopyPolicy.createChange(pm,
140: new MonitoringNewNameQueries(fNewNameQueries,
141: fExecutionLog));
142: if (change instanceof CompositeChange) {
143: CompositeChange subComposite = (CompositeChange) change;
144: result.merge(subComposite);
145: } else {
146: result.add(change);
147: }
148: return result;
149: } finally {
150: pm.done();
151: }
152: }
153:
154: private String[] getAffectedProjectNatures() throws CoreException {
155: String[] jNatures = JavaProcessors
156: .computeAffectedNaturs(fCopyPolicy.getJavaElements());
157: String[] rNatures = ResourceProcessors
158: .computeAffectedNatures(fCopyPolicy.getResources());
159: Set result = new HashSet();
160: result.addAll(Arrays.asList(jNatures));
161: result.addAll(Arrays.asList(rNatures));
162: return (String[]) result.toArray(new String[result.size()]);
163: }
164:
165: private String getChangeName() {
166: return RefactoringCoreMessages.JavaCopyProcessor_changeName;
167: }
168:
169: public String getComment() {
170: return fComment;
171: }
172:
173: public Object getCommonParentForInputElements() {
174: return new ParentChecker(fCopyPolicy.getResources(),
175: fCopyPolicy.getJavaElements()).getCommonParent();
176: }
177:
178: public Object[] getElements() {
179: IJavaElement[] jElements = fCopyPolicy.getJavaElements();
180: IResource[] resources = fCopyPolicy.getResources();
181: List result = new ArrayList(jElements.length + resources.length);
182: result.addAll(Arrays.asList(jElements));
183: result.addAll(Arrays.asList(resources));
184: return result.toArray();
185: }
186:
187: public String getIdentifier() {
188: return IRefactoringProcessorIds.COPY_PROCESSOR;
189: }
190:
191: public IJavaElement[] getJavaElements() {
192: return fCopyPolicy.getJavaElements();
193: }
194:
195: public String getProcessorName() {
196: return RefactoringCoreMessages.JavaCopyProcessor_processorName;
197: }
198:
199: public IResource[] getResources() {
200: return fCopyPolicy.getResources();
201: }
202:
203: public RefactoringStatus initialize(RefactoringArguments arguments) {
204: setReorgQueries(new NullReorgQueries());
205: final RefactoringStatus status = new RefactoringStatus();
206: if (arguments instanceof JavaRefactoringArguments) {
207: final JavaRefactoringArguments extended = (JavaRefactoringArguments) arguments;
208: fCopyPolicy = ReorgPolicyFactory.createCopyPolicy(status,
209: extended);
210: if (fCopyPolicy != null && !status.hasFatalError()) {
211: status.merge(fCopyPolicy.initialize(arguments));
212: if (!status.hasFatalError()) {
213: final ReorgExecutionLog log = ReorgPolicyFactory
214: .loadReorgExecutionLog(extended);
215: if (log != null && !status.hasFatalError())
216: setNewNameQueries(new LoggedNewNameQueries(log));
217: }
218: }
219: } else
220: return RefactoringStatus
221: .createFatalErrorStatus(RefactoringCoreMessages.InitializableRefactoring_inacceptable_arguments);
222: return status;
223: }
224:
225: public boolean isApplicable() throws CoreException {
226: return fCopyPolicy.canEnable();
227: }
228:
229: public RefactoringParticipant[] loadParticipants(
230: RefactoringStatus status,
231: SharableParticipants sharedParticipants)
232: throws CoreException {
233: RefactoringParticipant[] result = fCopyPolicy.loadParticipants(
234: status, this , getAffectedProjectNatures(),
235: sharedParticipants);
236: fExecutionLog = fCopyPolicy.getReorgExecutionLog();
237: return result;
238: }
239:
240: public void setComment(String comment) {
241: fComment = comment;
242: }
243:
244: public RefactoringStatus setDestination(
245: IReorgDestination destination) throws JavaModelException {
246: fCopyPolicy.setDestination(destination);
247: return fCopyPolicy.verifyDestination(destination);
248: }
249:
250: public void setNewNameQueries(INewNameQueries newNameQueries) {
251: Assert.isNotNull(newNameQueries);
252: fNewNameQueries = newNameQueries;
253: }
254:
255: public void setReorgQueries(IReorgQueries queries) {
256: Assert.isNotNull(queries);
257: fReorgQueries = queries;
258: }
259:
260: public int getSaveMode() {
261: return fCopyPolicy.getSaveMode();
262: }
263: }
|