KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ltk > ui > refactoring > model > AbstractSynchronizationContentProvider


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.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.diff.IDiffVisitor;
19 import org.eclipse.team.core.diff.IThreeWayDiff;
20 import org.eclipse.team.core.diff.ITwoWayDiff;
21 import org.eclipse.team.core.history.IFileRevision;
22 import org.eclipse.team.core.mapping.IResourceDiff;
23 import org.eclipse.team.core.mapping.IResourceDiffTree;
24 import org.eclipse.team.core.mapping.ISynchronizationContext;
25 import org.eclipse.team.ui.mapping.SynchronizationContentProvider;
26
27 import org.eclipse.core.runtime.Assert;
28 import org.eclipse.core.runtime.IProgressMonitor;
29 import org.eclipse.core.runtime.NullProgressMonitor;
30 import org.eclipse.core.runtime.SubProgressMonitor;
31
32 import org.eclipse.core.resources.IProject;
33 import org.eclipse.core.resources.IResource;
34
35 import org.eclipse.ltk.core.refactoring.RefactoringDescriptor;
36 import org.eclipse.ltk.core.refactoring.RefactoringDescriptorProxy;
37 import org.eclipse.ltk.core.refactoring.history.RefactoringHistory;
38
39 import org.eclipse.ltk.internal.core.refactoring.history.RefactoringDescriptorProxyAdapter;
40 import org.eclipse.ltk.internal.core.refactoring.history.RefactoringHistoryImplementation;
41 import org.eclipse.ltk.internal.core.refactoring.history.RefactoringHistoryService;
42 import org.eclipse.ltk.internal.ui.refactoring.RefactoringUIMessages;
43 import org.eclipse.ltk.internal.ui.refactoring.model.RefactoringDescriptorDiff;
44 import org.eclipse.ltk.internal.ui.refactoring.model.RefactoringDescriptorSynchronizationProxy;
45
46 /**
47  * Partial implementation of a refactoring-aware synchronization content
48  * provider.
49  * <p>
50  * This class provides a method
51  * {@link #getRefactorings(ISynchronizationContext, IProject, IProgressMonitor)}
52  * which may be used in subclasses to render refactorings in team
53  * synchronization views.
54  * </p>
55  * <p>
56  * Note: this class is designed to be extended by clients. Programming language
57  * implementers who need refactoring support in a synchronization content
58  * provider used in team synchronization views may use this class as a basis for
59  * refactoring-aware synchronization content providers.
60  * </p>
61  *
62  * @see org.eclipse.team.ui.mapping.SynchronizationContentProvider
63  *
64  * @since 3.2
65  */

66 public abstract class AbstractSynchronizationContentProvider extends SynchronizationContentProvider {
67
68     /**
69      * Returns the refactorings for the specified project which are not in sync.
70      * <p>
71      * This method fetches refactoring information for all refactorings which
72      * are not in sync for a project (e.g. have not yet been checked into the
73      * repository, or are pending refactorings to execute on the local
74      * workspace).
75      * </p>
76      *
77      * @param context
78      * the synchronization context to use
79      * @param project
80      * the project to compute its refactorings
81      * @param monitor
82      * the progress monitor to use, or <code>null</code> if no
83      * progress monitoring or cancelation is desired
84      * @return the refactoring history representing the refactorings
85      */

86     protected RefactoringHistory getRefactorings(final ISynchronizationContext context, final IProject project, IProgressMonitor monitor) {
87         Assert.isNotNull(context);
88         Assert.isNotNull(project);
89         if (monitor == null)
90             monitor= new NullProgressMonitor();
91         try {
92             monitor.beginTask(RefactoringUIMessages.RefactoringModelMerger_retrieving_refactorings, IProgressMonitor.UNKNOWN);
93             final IProgressMonitor finalMonitor= monitor;
94             final Set JavaDoc result= new HashSet JavaDoc();
95             final IResourceDiffTree tree= context.getDiffTree();
96             tree.accept(project.getFolder(RefactoringHistoryService.NAME_HISTORY_FOLDER).getFullPath(), new IDiffVisitor() {
97
98                 public final boolean visit(final IDiff diff) {
99                     if (diff instanceof IThreeWayDiff) {
100                         final IThreeWayDiff threeWay= (IThreeWayDiff) diff;
101                         final Set JavaDoc localDescriptors= new HashSet JavaDoc();
102                         final Set JavaDoc remoteDescriptors= new HashSet JavaDoc();
103                         final ITwoWayDiff localDiff= threeWay.getLocalChange();
104                         if (localDiff instanceof IResourceDiff && localDiff.getKind() != IDiff.NO_CHANGE) {
105                             final IResourceDiff resourceDiff= (IResourceDiff) localDiff;
106                             final IFileRevision revision= resourceDiff.getAfterState();
107                             if (revision != null) {
108                                 final String JavaDoc name= revision.getName();
109                                 if (name.equalsIgnoreCase(RefactoringHistoryService.NAME_HISTORY_FILE))
110                                     AbstractResourceMappingMerger.getRefactoringDescriptors(revision, localDescriptors, new SubProgressMonitor(finalMonitor, 1, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL));
111                             }
112                         }
113                         final ITwoWayDiff remoteDiff= threeWay.getLocalChange();
114                         if (remoteDiff instanceof IResourceDiff && remoteDiff.getKind() != IDiff.NO_CHANGE) {
115                             final IResourceDiff resourceDiff= (IResourceDiff) remoteDiff;
116                             final IFileRevision revision= resourceDiff.getAfterState();
117                             if (revision != null) {
118                                 final String JavaDoc name= revision.getName();
119                                 if (name.equalsIgnoreCase(RefactoringHistoryService.NAME_HISTORY_FILE))
120                                     AbstractResourceMappingMerger.getRefactoringDescriptors(revision, remoteDescriptors, new SubProgressMonitor(finalMonitor, 1, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL));
121                             }
122                         }
123                         final Set JavaDoc local= new HashSet JavaDoc(localDescriptors);
124                         local.removeAll(remoteDescriptors);
125                         for (final Iterator JavaDoc iterator= local.iterator(); iterator.hasNext();) {
126                             final RefactoringDescriptor descriptor= (RefactoringDescriptor) iterator.next();
127                             result.add(new RefactoringDescriptorSynchronizationProxy(new RefactoringDescriptorProxyAdapter(descriptor), project.getName(), IThreeWayDiff.OUTGOING));
128                         }
129                         final Set JavaDoc remote= new HashSet JavaDoc(remoteDescriptors);
130                         remote.removeAll(localDescriptors);
131                         for (final Iterator JavaDoc iterator= remote.iterator(); iterator.hasNext();) {
132                             final RefactoringDescriptor descriptor= (RefactoringDescriptor) iterator.next();
133                             result.add(new RefactoringDescriptorSynchronizationProxy(new RefactoringDescriptorProxyAdapter(descriptor), project.getName(), IThreeWayDiff.INCOMING));
134                         }
135                     }
136                     return true;
137                 }
138             }, IResource.DEPTH_INFINITE);
139
140             for (final Iterator JavaDoc iterator= result.iterator(); iterator.hasNext();) {
141                 final RefactoringDescriptorSynchronizationProxy proxy= (RefactoringDescriptorSynchronizationProxy) iterator.next();
142                 if (!isVisible(new RefactoringDescriptorDiff(proxy, IDiff.CHANGE, proxy.getDirection())))
143                     result.remove(proxy);
144             }
145
146             return new RefactoringHistoryImplementation((RefactoringDescriptorProxy[]) result.toArray(new RefactoringDescriptorProxy[result.size()]));
147         } finally {
148             monitor.done();
149         }
150     }
151 }
152
Popular Tags