KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ltk > internal > ui > refactoring > model > RefactoringHistoryMergeWizard


1 /*******************************************************************************
2  * Copyright (c) 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ltk.internal.ui.refactoring.model;
12
13 import java.util.HashSet JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.Set JavaDoc;
16
17 import org.eclipse.team.core.diff.IDiff;
18 import org.eclipse.team.core.mapping.IMergeContext;
19
20 import org.eclipse.core.runtime.Assert;
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.core.runtime.IProgressMonitor;
23 import org.eclipse.core.runtime.NullProgressMonitor;
24
25 import org.eclipse.core.resources.IResource;
26 import org.eclipse.core.resources.IResourceChangeEvent;
27 import org.eclipse.core.resources.IResourceChangeListener;
28 import org.eclipse.core.resources.IResourceDelta;
29 import org.eclipse.core.resources.IResourceDeltaVisitor;
30 import org.eclipse.core.resources.ResourcesPlugin;
31
32 import org.eclipse.ltk.core.refactoring.Refactoring;
33 import org.eclipse.ltk.core.refactoring.RefactoringDescriptor;
34 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
35
36 import org.eclipse.ltk.internal.core.refactoring.history.RefactoringHistoryService;
37 import org.eclipse.ltk.internal.ui.refactoring.RefactoringUIPlugin;
38
39 import org.eclipse.ltk.ui.refactoring.history.RefactoringHistoryWizard;
40
41 /**
42  * Partial implementation of a refactoring history merge wizard.
43  * <p>
44  * This refactoring history wizard executes refactorings from a refactoring
45  * history, but uses the time stamps of the refactoring history for the new
46  * local refactoring history entries.
47  * </p>
48  * <p>
49  * Additionally, this refactoring wizard collects all added, removed and
50  * otherwise changed files which happened during the execution of the
51  * refactorings.
52  * </p>
53  *
54  * @since 3.2
55  */

56 public abstract class RefactoringHistoryMergeWizard extends RefactoringHistoryWizard {
57
58     /** Workspace change listener */
59     private class WorkspaceChangeListener implements IResourceChangeListener {
60
61         /**
62          * {@inheritDoc}
63          */

64         public void resourceChanged(final IResourceChangeEvent event) {
65             final IResourceDelta delta= event.getDelta();
66             if (delta != null) {
67                 try {
68                     delta.accept(new IResourceDeltaVisitor() {
69
70                         public final boolean visit(final IResourceDelta current) throws CoreException {
71                             final IResource resource= current.getResource();
72                             if (!resource.isDerived()) {
73                                 if (resource.getType() == IResource.FILE) {
74                                     switch (delta.getKind()) {
75                                         case IResourceDelta.ADDED:
76                                             fAddedFiles.add(resource);
77                                             break;
78                                         case IResourceDelta.REMOVED:
79                                             fRemovedFiles.add(resource);
80                                             break;
81                                         case IResourceDelta.CHANGED:
82                                             fChangedFiles.add(resource);
83                                             break;
84                                     }
85                                 }
86                             }
87                             return true;
88                         }
89                     });
90                 } catch (CoreException exception) {
91                     RefactoringUIPlugin.log(exception);
92                 }
93             }
94         }
95     }
96
97     /** The set of added files */
98     private final Set JavaDoc fAddedFiles= new HashSet JavaDoc();
99
100     /** The set of changed files */
101     private final Set JavaDoc fChangedFiles= new HashSet JavaDoc();
102
103     /** The workspace change listener */
104     private final IResourceChangeListener fListener= new WorkspaceChangeListener();
105
106     /** The set of removed files */
107     private final Set JavaDoc fRemovedFiles= new HashSet JavaDoc();
108
109     /**
110      * Creates a new refactoring history merge wizard.
111      *
112      * @param caption
113      * the caption of the wizard window
114      * @param title
115      * the title of the overview page
116      * @param description
117      * the description of the overview page
118      */

119     protected RefactoringHistoryMergeWizard(final String JavaDoc caption, final String JavaDoc title, final String JavaDoc description) {
120         super(caption, title, description);
121     }
122
123     /**
124      * {@inheritDoc}
125      */

126     protected RefactoringStatus aboutToPerformHistory(final IProgressMonitor monitor) {
127         fAddedFiles.clear();
128         fRemovedFiles.clear();
129         fChangedFiles.clear();
130         ResourcesPlugin.getWorkspace().addResourceChangeListener(fListener, IResourceChangeEvent.POST_CHANGE);
131         return super.aboutToPerformHistory(monitor);
132     }
133
134     /**
135      * {@inheritDoc}
136      */

137     protected RefactoringStatus aboutToPerformRefactoring(final Refactoring refactoring, final RefactoringDescriptor descriptor, final IProgressMonitor monitor) {
138         Assert.isNotNull(descriptor);
139         final long stamp= descriptor.getTimeStamp();
140         if (stamp >= 0)
141             RefactoringHistoryService.getInstance().setOverrideTimeStamp(stamp);
142         return super.aboutToPerformRefactoring(refactoring, descriptor, monitor);
143     }
144
145     /**
146      * Returns the added files.
147      *
148      * @return the added files
149      */

150     public Set JavaDoc getAddedFiles() {
151         return fAddedFiles;
152     }
153
154     /**
155      * Returns the changed files.
156      *
157      * @return the changed files
158      */

159     public Set JavaDoc getChangedFiles() {
160         return fChangedFiles;
161     }
162
163     /**
164      * Returns the removed files.
165      *
166      * @return the removed files
167      */

168     public Set JavaDoc getRemovedFiles() {
169         return fRemovedFiles;
170     }
171
172     /**
173      * {@inheritDoc}
174      */

175     protected RefactoringStatus historyPerformed(final IProgressMonitor monitor) {
176         ResourcesPlugin.getWorkspace().removeResourceChangeListener(fListener);
177         RefactoringHistoryService.getInstance().setOverrideTimeStamp(-1);
178         return super.historyPerformed(monitor);
179     }
180
181     /**
182      * {@inheritDoc}
183      */

184     protected RefactoringStatus refactoringPerformed(final Refactoring refactoring, final IProgressMonitor monitor) {
185         RefactoringHistoryService.getInstance().setOverrideTimeStamp(-1);
186         return super.refactoringPerformed(refactoring, monitor);
187     }
188
189     /**
190      * Resolves the conflicts which have been introduced by the executed
191      * refactorings.
192      *
193      * @param context
194      * the merge context
195      */

196     public void resolveConflicts(final IMergeContext context) {
197         Assert.isNotNull(context);
198         for (final Iterator JavaDoc iterator= fChangedFiles.iterator(); iterator.hasNext();) {
199             final IResource resource= (IResource) iterator.next();
200             final IDiff diff= context.getDiffTree().getDiff(resource);
201             if (diff != null) {
202                 try {
203                     context.markAsMerged(diff, true, new NullProgressMonitor());
204                 } catch (CoreException exception) {
205                     RefactoringUIPlugin.log(exception);
206                 }
207             }
208         }
209         for (final Iterator JavaDoc iterator= fAddedFiles.iterator(); iterator.hasNext();) {
210             final IResource resource= (IResource) iterator.next();
211             final IDiff diff= context.getDiffTree().getDiff(resource);
212             if (diff != null) {
213                 try {
214                     context.markAsMerged(diff, true, new NullProgressMonitor());
215                 } catch (CoreException exception) {
216                     RefactoringUIPlugin.log(exception);
217                 }
218             }
219         }
220         for (final Iterator JavaDoc iterator= fRemovedFiles.iterator(); iterator.hasNext();) {
221             final IResource resource= (IResource) iterator.next();
222             final IDiff diff= context.getDiffTree().getDiff(resource);
223             if (diff != null) {
224                 try {
225                     context.markAsMerged(diff, true, new NullProgressMonitor());
226                 } catch (CoreException exception) {
227                     RefactoringUIPlugin.log(exception);
228                 }
229             }
230         }
231     }
232 }
Popular Tags