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:
023: import org.eclipse.core.resources.IResource;
024:
025: import org.eclipse.ltk.core.refactoring.Change;
026: import org.eclipse.ltk.core.refactoring.ChangeDescriptor;
027: import org.eclipse.ltk.core.refactoring.CompositeChange;
028: import org.eclipse.ltk.core.refactoring.RefactoringStatus;
029: import org.eclipse.ltk.core.refactoring.TextEditBasedChange;
030: import org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext;
031: import org.eclipse.ltk.core.refactoring.participants.MoveProcessor;
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.SharableParticipants;
035:
036: import org.eclipse.jdt.core.IJavaElement;
037: import org.eclipse.jdt.core.JavaModelException;
038:
039: import org.eclipse.jdt.internal.corext.refactoring.JavaRefactoringArguments;
040: import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
041: import org.eclipse.jdt.internal.corext.refactoring.changes.DynamicValidationStateChange;
042: import org.eclipse.jdt.internal.corext.refactoring.participants.JavaProcessors;
043: import org.eclipse.jdt.internal.corext.refactoring.participants.ResourceProcessors;
044: import org.eclipse.jdt.internal.corext.refactoring.reorg.IReorgPolicy.IMovePolicy;
045: import org.eclipse.jdt.internal.corext.refactoring.tagging.ICommentProvider;
046: import org.eclipse.jdt.internal.corext.refactoring.tagging.IQualifiedNameUpdating;
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 JavaMoveProcessor extends MoveProcessor implements
053: IScriptableRefactoring, ICommentProvider,
054: IQualifiedNameUpdating, IReorgDestinationValidator {
055:
056: private String fComment;
057:
058: private ICreateTargetQueries fCreateTargetQueries;
059:
060: private IMovePolicy fMovePolicy;
061:
062: private IReorgQueries fReorgQueries;
063:
064: private boolean fWasCanceled;
065:
066: public JavaMoveProcessor(IMovePolicy policy) {
067: fMovePolicy = policy;
068: }
069:
070: public boolean canChildrenBeDestinations(
071: IReorgDestination destination) {
072: return fMovePolicy.canChildrenBeDestinations(destination);
073: }
074:
075: public boolean canElementBeDestination(IReorgDestination destination) {
076: return fMovePolicy.canElementBeDestination(destination);
077: }
078:
079: public boolean canEnableComment() {
080: return true;
081: }
082:
083: public boolean canEnableQualifiedNameUpdating() {
084: return fMovePolicy.canEnableQualifiedNameUpdating();
085: }
086:
087: public boolean canUpdateQualifiedNames() {
088: return fMovePolicy.canUpdateQualifiedNames();
089: }
090:
091: public boolean canUpdateReferences() {
092: return fMovePolicy.canUpdateReferences();
093: }
094:
095: public RefactoringStatus checkFinalConditions(IProgressMonitor pm,
096: CheckConditionsContext context) throws CoreException {
097: try {
098: Assert.isNotNull(fReorgQueries);
099: fWasCanceled = false;
100: return fMovePolicy.checkFinalConditions(pm, context,
101: fReorgQueries);
102: } catch (OperationCanceledException e) {
103: fWasCanceled = true;
104: throw e;
105: }
106: }
107:
108: public RefactoringStatus checkInitialConditions(IProgressMonitor pm)
109: throws CoreException {
110: pm.beginTask("", 1); //$NON-NLS-1$
111: try {
112: RefactoringStatus result = new RefactoringStatus();
113: result.merge(RefactoringStatus.create(Resources
114: .checkInSync(ReorgUtils.getNotNulls(fMovePolicy
115: .getResources()))));
116: IResource[] javaResources = ReorgUtils
117: .getResources(fMovePolicy.getJavaElements());
118: result.merge(RefactoringStatus
119: .create(Resources.checkInSync(ReorgUtils
120: .getNotNulls(javaResources))));
121: return result;
122: } finally {
123: pm.done();
124: }
125: }
126:
127: public Change createChange(IProgressMonitor pm)
128: throws CoreException {
129: Assert.isTrue(fMovePolicy.getJavaElementDestination() == null
130: || fMovePolicy.getResourceDestination() == null);
131: Assert.isTrue(fMovePolicy.getJavaElementDestination() != null
132: || fMovePolicy.getResourceDestination() != null);
133: try {
134: final DynamicValidationStateChange result = new DynamicValidationStateChange(
135: RefactoringCoreMessages.JavaMoveProcessor_change_name) {
136:
137: public ChangeDescriptor getDescriptor() {
138: return fMovePolicy.getDescriptor();
139: }
140:
141: public Change perform(IProgressMonitor pm2)
142: throws CoreException {
143: Change change = super .perform(pm2);
144: Change[] changes = getChildren();
145: for (int index = 0; index < changes.length; index++) {
146: if (!(changes[index] instanceof TextEditBasedChange))
147: return null;
148: }
149: return change;
150: }
151: };
152: CreateTargetExecutionLog log = null;
153: if (fCreateTargetQueries instanceof MonitoringCreateTargetQueries) {
154: final MonitoringCreateTargetQueries queries = (MonitoringCreateTargetQueries) fCreateTargetQueries;
155: final ICreateTargetQueries delegate = queries
156: .getDelegate();
157: if (delegate instanceof LoggedCreateTargetQueries)
158: log = queries.getCreateTargetExecutionLog();
159: }
160: if (log != null) {
161: final Object[] selected = log.getSelectedElements();
162: for (int index = 0; index < selected.length; index++) {
163: result.add(new LoggedCreateTargetChange(
164: selected[index], fCreateTargetQueries));
165: }
166: }
167: Change change = fMovePolicy.createChange(pm);
168: if (change instanceof CompositeChange) {
169: CompositeChange subComposite = (CompositeChange) change;
170: result.merge(subComposite);
171: } else {
172: result.add(change);
173: }
174: return result;
175: } finally {
176: pm.done();
177: }
178: }
179:
180: private String[] getAffectedProjectNatures() throws CoreException {
181: String[] jNatures = JavaProcessors
182: .computeAffectedNaturs(fMovePolicy.getJavaElements());
183: String[] rNatures = ResourceProcessors
184: .computeAffectedNatures(fMovePolicy.getResources());
185: Set result = new HashSet();
186: result.addAll(Arrays.asList(jNatures));
187: result.addAll(Arrays.asList(rNatures));
188: return (String[]) result.toArray(new String[result.size()]);
189: }
190:
191: public String getComment() {
192: return fComment;
193: }
194:
195: public Object getCommonParentForInputElements() {
196: return new ParentChecker(fMovePolicy.getResources(),
197: fMovePolicy.getJavaElements()).getCommonParent();
198: }
199:
200: public ICreateTargetQuery getCreateTargetQuery() {
201: return fMovePolicy.getCreateTargetQuery(fCreateTargetQueries);
202: }
203:
204: protected Object getDestination() {
205: IJavaElement je = fMovePolicy.getJavaElementDestination();
206: if (je != null)
207: return je;
208: return fMovePolicy.getResourceDestination();
209: }
210:
211: public Object[] getElements() {
212: List result = new ArrayList();
213: result.addAll(Arrays.asList(fMovePolicy.getJavaElements()));
214: result.addAll(Arrays.asList(fMovePolicy.getResources()));
215: return result.toArray();
216: }
217:
218: public String getFilePatterns() {
219: return fMovePolicy.getFilePatterns();
220: }
221:
222: public String getIdentifier() {
223: return IRefactoringProcessorIds.MOVE_PROCESSOR;
224: }
225:
226: public IJavaElement[] getJavaElements() {
227: return fMovePolicy.getJavaElements();
228: }
229:
230: public String getProcessorName() {
231: return RefactoringCoreMessages.MoveRefactoring_0;
232: }
233:
234: public IResource[] getResources() {
235: return fMovePolicy.getResources();
236: }
237:
238: public boolean getUpdateQualifiedNames() {
239: return fMovePolicy.getUpdateQualifiedNames();
240: }
241:
242: public boolean getUpdateReferences() {
243: if (!canUpdateReferences())
244: return false;
245: return fMovePolicy.getUpdateReferences();
246: }
247:
248: public boolean hasAllInputSet() {
249: return fMovePolicy.hasAllInputSet();
250: }
251:
252: public boolean hasDestinationSet() {
253: return fMovePolicy.getJavaElementDestination() != null
254: || fMovePolicy.getResourceDestination() != null;
255: }
256:
257: public RefactoringStatus initialize(RefactoringArguments arguments) {
258: setReorgQueries(new NullReorgQueries());
259: final RefactoringStatus status = new RefactoringStatus();
260: if (arguments instanceof JavaRefactoringArguments) {
261: final JavaRefactoringArguments extended = (JavaRefactoringArguments) arguments;
262: fMovePolicy = ReorgPolicyFactory.createMovePolicy(status,
263: arguments);
264: if (fMovePolicy != null && !status.hasFatalError()) {
265: final CreateTargetExecutionLog log = ReorgPolicyFactory
266: .loadCreateTargetExecutionLog(extended);
267: if (log != null && !status.hasFatalError()) {
268: fMovePolicy.setDestinationCheck(false);
269: fCreateTargetQueries = new MonitoringCreateTargetQueries(
270: new LoggedCreateTargetQueries(log), log);
271: }
272: status.merge(fMovePolicy.initialize(arguments));
273: }
274: } else
275: return RefactoringStatus
276: .createFatalErrorStatus(RefactoringCoreMessages.InitializableRefactoring_inacceptable_arguments);
277: return status;
278: }
279:
280: public boolean isApplicable() throws CoreException {
281: return fMovePolicy.canEnable();
282: }
283:
284: public boolean isTextualMove() {
285: return fMovePolicy.isTextualMove();
286: }
287:
288: public RefactoringParticipant[] loadParticipants(
289: RefactoringStatus status, SharableParticipants shared)
290: throws CoreException {
291: return fMovePolicy.loadParticipants(status, this ,
292: getAffectedProjectNatures(), shared);
293: }
294:
295: public Change postCreateChange(Change[] participantChanges,
296: IProgressMonitor pm) throws CoreException {
297: return fMovePolicy.postCreateChange(participantChanges, pm);
298: }
299:
300: public void setComment(String comment) {
301: fComment = comment;
302: }
303:
304: public void setCreateTargetQueries(ICreateTargetQueries queries) {
305: Assert.isNotNull(queries);
306: fCreateTargetQueries = new MonitoringCreateTargetQueries(
307: queries, fMovePolicy.getCreateTargetExecutionLog());
308: }
309:
310: public RefactoringStatus setDestination(
311: IReorgDestination destination) throws JavaModelException {
312: fMovePolicy.setDestination(destination);
313: return fMovePolicy.verifyDestination(destination);
314: }
315:
316: public void setFilePatterns(String patterns) {
317: fMovePolicy.setFilePatterns(patterns);
318: }
319:
320: public void setReorgQueries(IReorgQueries queries) {
321: Assert.isNotNull(queries);
322: fReorgQueries = queries;
323: }
324:
325: public void setUpdateQualifiedNames(boolean update) {
326: fMovePolicy.setUpdateQualifiedNames(update);
327: }
328:
329: public void setUpdateReferences(boolean update) {
330: fMovePolicy.setUpdateReferences(update);
331: }
332:
333: public boolean wasCanceled() {
334: return fWasCanceled;
335: }
336:
337: public int getSaveMode() {
338: return fMovePolicy.getSaveMode();
339: }
340: }
|