KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > mappings > ModelMergeParticipant


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.team.internal.ccvs.ui.mappings;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.core.resources.IResource;
17 import org.eclipse.core.resources.ResourcesPlugin;
18 import org.eclipse.core.resources.mapping.ResourceMapping;
19 import org.eclipse.core.runtime.*;
20 import org.eclipse.osgi.util.NLS;
21 import org.eclipse.team.core.TeamException;
22 import org.eclipse.team.core.mapping.ISynchronizationScope;
23 import org.eclipse.team.core.mapping.ISynchronizationScopeManager;
24 import org.eclipse.team.core.mapping.provider.MergeContext;
25 import org.eclipse.team.core.subscribers.SubscriberScopeManager;
26 import org.eclipse.team.internal.ccvs.core.*;
27 import org.eclipse.team.internal.ccvs.ui.CVSUIMessages;
28 import org.eclipse.team.internal.ccvs.ui.CVSUIPlugin;
29 import org.eclipse.team.internal.ccvs.ui.actions.ShowAnnotationAction;
30 import org.eclipse.team.internal.ccvs.ui.actions.ShowResourceInHistoryAction;
31 import org.eclipse.team.internal.ccvs.ui.subscriber.CVSActionDelegateWrapper;
32 import org.eclipse.team.internal.ui.Utils;
33 import org.eclipse.team.ui.TeamUI;
34 import org.eclipse.team.ui.synchronize.*;
35 import org.eclipse.ui.IMemento;
36 import org.eclipse.ui.PartInitException;
37
38 public class ModelMergeParticipant extends CVSModelSynchronizeParticipant {
39
40     public static final String JavaDoc VIEWER_ID = "org.eclipse.team.cvs.ui.mergeSynchronization"; //$NON-NLS-1$
41

42     public static final String JavaDoc CONTEXT_MENU_CONTRIBUTION_GROUP_1 = "otherActions1"; //$NON-NLS-1$
43

44     public static final String JavaDoc ID = "org.eclipse.team.cvs.ui.modelMergeParticipant"; //$NON-NLS-1$
45

46     private final static String JavaDoc CTX_SUBSCRIBER = "mergeSubscriber"; //$NON-NLS-1$
47
private final static String JavaDoc CTX_ROOT = "root"; //$NON-NLS-1$
48
private final static String JavaDoc CTX_ROOT_PATH = "root_resource"; //$NON-NLS-1$
49
private final static String JavaDoc CTX_START_TAG = "start_tag"; //$NON-NLS-1$
50
private final static String JavaDoc CTX_START_TAG_TYPE = "start_tag_type"; //$NON-NLS-1$
51
private final static String JavaDoc CTX_END_TAG = "end_tag"; //$NON-NLS-1$
52
private final static String JavaDoc CTX_END_TAG_TYPE = "end_tag_type"; //$NON-NLS-1$
53

54     public class MergeActionGroup extends ModelSynchronizeParticipantActionGroup {
55         public void initialize(ISynchronizePageConfiguration configuration) {
56             super.initialize(configuration);
57             if (!configuration.getSite().isModal()) {
58                 appendToGroup(
59                     ISynchronizePageConfiguration.P_CONTEXT_MENU,
60                     CONTEXT_MENU_CONTRIBUTION_GROUP_1,
61                     new CVSActionDelegateWrapper(new ShowAnnotationAction(), configuration));
62                 appendToGroup(
63                     ISynchronizePageConfiguration.P_CONTEXT_MENU,
64                     CONTEXT_MENU_CONTRIBUTION_GROUP_1,
65                     new CVSActionDelegateWrapper(new ShowResourceInHistoryAction(), configuration));
66             }
67         }
68     }
69
70     private CVSMergeSubscriber subscriber;
71     
72     public ModelMergeParticipant() {
73     }
74
75     public ModelMergeParticipant(MergeSubscriberContext context) {
76         super(context);
77         subscriber = getSubscriber();
78         initialize();
79     }
80     
81     protected ModelSynchronizeParticipantActionGroup createMergeActionGroup() {
82         return new MergeActionGroup();
83     }
84
85     /* (non-Javadoc)
86      * @see org.eclipse.team.ui.operations.ModelSynchronizeParticipant#initializeConfiguration(org.eclipse.team.ui.synchronize.ISynchronizePageConfiguration)
87      */

88     protected void initializeConfiguration(ISynchronizePageConfiguration configuration) {
89         configuration.setProperty(ISynchronizePageConfiguration.P_VIEWER_ID, VIEWER_ID);
90         super.initializeConfiguration(configuration);
91         configuration.setSupportedModes(ISynchronizePageConfiguration.INCOMING_MODE | ISynchronizePageConfiguration.CONFLICTING_MODE);
92         configuration.setMode(ISynchronizePageConfiguration.INCOMING_MODE);
93     }
94     
95     private void initialize() {
96         try {
97             ISynchronizeParticipantDescriptor descriptor = TeamUI.getSynchronizeManager().getParticipantDescriptor(ID);
98             setInitializationData(descriptor);
99             CVSMergeSubscriber s = (CVSMergeSubscriber)getSubscriber();
100             setSecondaryId(s.getId().getLocalName());
101         } catch (CoreException e) {
102             CVSUIPlugin.log(e);
103         }
104     }
105
106     private CVSMergeSubscriber getSubscriber() {
107         return (CVSMergeSubscriber)((MergeSubscriberContext)getContext()).getSubscriber();
108     }
109     
110     public void init(String JavaDoc secondaryId, IMemento memento) throws PartInitException {
111         if(memento != null) {
112             ISynchronizeParticipantDescriptor descriptor = TeamUI.getSynchronizeManager().getParticipantDescriptor(ID);
113             String JavaDoc qualifier = descriptor.getId();
114             String JavaDoc localname = secondaryId;
115             if(qualifier == null || localname == null) {
116                 throw new PartInitException(CVSUIMessages.MergeSynchronizeParticipant_8);
117             }
118             try {
119                 subscriber = read(new QualifiedName(qualifier, localname), memento.getChild(CTX_SUBSCRIBER));
120             } catch (CVSException e) {
121                 throw new PartInitException(CVSUIMessages.MergeSynchronizeParticipant_9, e);
122             }
123         }
124         try {
125             super.init(secondaryId, memento);
126         } catch (PartInitException e) {
127             subscriber.cancel();
128             throw e;
129         }
130     }
131     
132     public void saveState(IMemento memento) {
133         super.saveState(memento);
134         write(subscriber, memento.createChild(CTX_SUBSCRIBER));
135     }
136     
137     private CVSMergeSubscriber read(QualifiedName id, IMemento memento) throws CVSException {
138         CVSTag start = new CVSTag(memento.getString(CTX_START_TAG), memento.getInteger(CTX_START_TAG_TYPE).intValue());
139         CVSTag end = new CVSTag(memento.getString(CTX_END_TAG), memento.getInteger(CTX_END_TAG_TYPE).intValue());
140         
141         IMemento[] rootNodes = memento.getChildren(CTX_ROOT);
142         if(rootNodes == null || rootNodes.length == 0) {
143             throw new CVSException(NLS.bind(CVSUIMessages.MergeSynchronizeParticipant_10, new String JavaDoc[] { id.toString() }));
144         }
145         
146         List JavaDoc resources = new ArrayList JavaDoc();
147         for (int i = 0; i < rootNodes.length; i++) {
148             IMemento rootNode = rootNodes[i];
149             IPath path = new Path(rootNode.getString(CTX_ROOT_PATH));
150             IResource resource = ResourcesPlugin.getWorkspace().getRoot().findMember(path, true /* include phantoms */);
151             if(resource != null) {
152                 resources.add(resource);
153             } else {
154                 // log that a resource previously in the merge set is no longer in the workspace
155
CVSProviderPlugin.log(IStatus.INFO, NLS.bind(CVSUIMessages.MergeSynchronizeParticipant_11, new String JavaDoc[] { path.toString() }), null);
156             }
157         }
158         if(resources.isEmpty()) {
159             throw new CVSException(NLS.bind(CVSUIMessages.MergeSynchronizeParticipant_12, new String JavaDoc[] { id.toString() }));
160         }
161         IResource[] roots = (IResource[]) resources.toArray(new IResource[resources.size()]);
162         return new CVSMergeSubscriber(id, roots, start, end);
163     }
164     
165     private void write(CVSMergeSubscriber s, IMemento memento) {
166         // start and end tags
167
CVSTag start = s.getStartTag();
168         CVSTag end = s.getEndTag();
169         memento.putString(CTX_START_TAG, start.getName());
170         memento.putInteger(CTX_START_TAG_TYPE, start.getType());
171         memento.putString(CTX_END_TAG, end.getName());
172         memento.putInteger(CTX_END_TAG_TYPE, end.getType());
173         
174         // resource roots
175
IResource[] roots = s.roots();
176         for (int i = 0; i < roots.length; i++) {
177             IResource resource = roots[i];
178             IMemento rootNode = memento.createChild(CTX_ROOT);
179             rootNode.putString(CTX_ROOT_PATH, resource.getFullPath().toString());
180         }
181     }
182     
183     protected String JavaDoc getShortTaskName() {
184         return CVSUIMessages.Participant_merging;
185     }
186     
187     public void dispose() {
188         if(TeamUI.getSynchronizeManager().get(getId(), getSecondaryId()) != null) {
189             // If the participant is managed by the synchronize manager then we
190
// we don't want to flush the synchronizer cache.
191
((MergeSubscriberContext)getContext()).setCancelSubscriber(false);
192         }
193         super.dispose();
194     }
195     
196     protected ISynchronizationScopeManager createScopeManager(ResourceMapping[] mappings) {
197         return new SubscriberScopeManager(subscriber.getName(),
198                 mappings, subscriber, true);
199     }
200     
201     protected MergeContext restoreContext(ISynchronizationScopeManager manager) throws CoreException {
202         return MergeSubscriberContext.createContext(manager, subscriber);
203     }
204     
205     /* (non-Javadoc)
206      * @see org.eclipse.team.ui.synchronize.ISynchronizeParticipant#getName()
207      */

208     public String JavaDoc getName() {
209         return NLS.bind(CVSUIMessages.CompareParticipant_0, new String JavaDoc[] { ((CVSMergeSubscriber)getSubscriber()).getName(), Utils.getScopeDescription(getContext().getScope()) });
210     }
211     
212     /*
213      * Returns a merge participant that exist and is configured with the given set of resources, start, and end tags.
214      */

215     public static ModelMergeParticipant getMatchingParticipant(ResourceMapping[] mappings, CVSTag startTag, CVSTag endTag) {
216         ISynchronizeParticipantReference[] refs = TeamUI.getSynchronizeManager().getSynchronizeParticipants();
217         for (int i = 0; i < refs.length; i++) {
218             ISynchronizeParticipantReference reference = refs[i];
219             if (reference.getId().equals(ID)) {
220                 ModelMergeParticipant p;
221                 try {
222                     p = (ModelMergeParticipant) reference.getParticipant();
223                 } catch (TeamException e) {
224                     continue;
225                 }
226                 ISynchronizationScope scope = p.getContext().getScope().asInputScope();
227                 ResourceMapping[] roots = scope.getMappings();
228                 if (roots.length == mappings.length) {
229                     boolean match = true;
230                     for (int j = 0; j < mappings.length; j++) {
231                         
232                         ResourceMapping mapping = mappings[j];
233                         if (scope.getTraversals(mapping) == null) {
234                             // The mapping is not in the scope so the participants don't match
235
match = false;
236                             break;
237                         }
238                     }
239                     if (match && p.getSubscriber().getStartTag().equals(startTag) && p.getSubscriber().getEndTag().equals(endTag)) {
240                         return p;
241                     }
242                 }
243             }
244         }
245         return null;
246     }
247 }
248
Popular Tags