KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > core > mapping > ResourceMappingMerger


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.team.core.mapping;
12
13 import java.util.HashSet JavaDoc;
14 import java.util.Set JavaDoc;
15
16 import org.eclipse.core.resources.IProject;
17 import org.eclipse.core.resources.mapping.*;
18 import org.eclipse.core.runtime.*;
19 import org.eclipse.core.runtime.jobs.ISchedulingRule;
20 import org.eclipse.core.runtime.jobs.MultiRule;
21 import org.eclipse.team.core.diff.IDiff;
22 import org.eclipse.team.core.mapping.provider.MergeStatus;
23
24 /**
25  * Abstract implementation of {@link IResourceMappingMerger}. This merger
26  * delegates the merge of all resources covered by the mappings of the
27  * model provider returned from {@link #getModelProvider()} back to the
28  * merge context. Subclasses should override the {@link #merge(IMergeContext, IProgressMonitor)}
29  * method in order to change this behavior.
30  *
31  * <p>
32  * Clients may subclass this class.
33  *
34  * @see IResourceMappingMerger
35  *
36  * @since 3.2
37  */

38 public abstract class ResourceMappingMerger implements IResourceMappingMerger {
39
40     /* (non-Javadoc)
41      * @see org.eclipse.team.core.mapping.IResourceMappingMerger#validateMerge(org.eclipse.team.core.mapping.IMergeContext, org.eclipse.core.runtime.IProgressMonitor)
42      */

43     public IStatus validateMerge(IMergeContext mergeContext, IProgressMonitor monitor) {
44         return Status.OK_STATUS;
45     }
46     
47     /**
48      * Return the model provider associated with this merger.
49      * @return Return the model provider associated with this merger.
50      */

51     protected abstract ModelProvider getModelProvider();
52     
53     /**
54      * Return the scheduling rule required to merge all the
55      * changes in the context for the model provider of this merger.
56      * By default, return a rule that covers all the projects for the mappings
57      * that belong to the model provider of this merger.
58      * @param context the context that contains the changes to be merged
59      * @return the scheduling rule required by this merger to merge all
60      * the changes in the given context belonging to the merger's
61      * model provider.
62      * @see org.eclipse.team.core.mapping.IResourceMappingMerger#getMergeRule(org.eclipse.team.core.mapping.IMergeContext)
63      */

64     public ISchedulingRule getMergeRule(IMergeContext context) {
65         ResourceMapping[] mappings = context.getScope().getMappings(getModelProvider().getId());
66         ISchedulingRule rule = null;
67         for (int i = 0; i < mappings.length; i++) {
68             ResourceMapping mapping = mappings[i];
69             IProject[] mappingProjects = mapping.getProjects();
70             for (int j = 0; j < mappingProjects.length; j++) {
71                 IProject project = mappingProjects[j];
72                 if (rule == null) {
73                     rule = project;
74                 } else {
75                     rule = MultiRule.combine(rule, project);
76                 }
77             }
78         }
79         return rule;
80     }
81     
82     /**
83      * A default implementation of merge that attempts to merge all the mappings
84      * in the context.
85      * @param mergeContext the context
86      * @param monitor a progress monitor
87      * @return a status indicating whether the merge was successful
88      * @throws CoreException if an error occurred
89      * @see org.eclipse.team.core.mapping.IResourceMappingMerger#merge(org.eclipse.team.core.mapping.IMergeContext, org.eclipse.core.runtime.IProgressMonitor)
90      */

91     public IStatus merge(IMergeContext mergeContext, IProgressMonitor monitor) throws CoreException {
92         IDiff[] deltas = getSetToMerge(mergeContext);
93         IStatus status = mergeContext.merge(deltas, false /* don't force */, monitor);
94         return covertFilesToMappings(status, mergeContext);
95     }
96
97     private IDiff[] getSetToMerge(IMergeContext mergeContext) {
98         ResourceMapping[] mappings = mergeContext.getScope().getMappings(getModelProvider().getDescriptor().getId());
99         Set JavaDoc result = new HashSet JavaDoc();
100         for (int i = 0; i < mappings.length; i++) {
101             ResourceMapping mapping = mappings[i];
102             ResourceTraversal[] traversals = mergeContext.getScope().getTraversals(mapping);
103             IDiff[] deltas = mergeContext.getDiffTree().getDiffs(traversals);
104             for (int j = 0; j < deltas.length; j++) {
105                 IDiff delta = deltas[j];
106                 result.add(delta);
107             }
108         }
109         return (IDiff[]) result.toArray(new IDiff[result.size()]);
110     }
111
112     private IStatus covertFilesToMappings(IStatus status, IMergeContext mergeContext) {
113         if (status.getCode() == IMergeStatus.CONFLICTS) {
114             // In general, we can't say which mapping failed so return them all
115
return new MergeStatus(status.getPlugin(), status.getMessage(), mergeContext.getScope().getMappings(getModelProvider().getDescriptor().getId()));
116         }
117         return status;
118     }
119 }
120
Popular Tags