KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > subscriber > MergeSynchronizeParticipant


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.internal.ccvs.ui.subscriber;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Arrays JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.resources.IResource;
18 import org.eclipse.core.resources.ResourcesPlugin;
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.subscribers.Subscriber;
23 import org.eclipse.team.internal.ccvs.core.*;
24 import org.eclipse.team.internal.ccvs.ui.CVSUIMessages;
25 import org.eclipse.team.internal.ccvs.ui.CVSUIPlugin;
26 import org.eclipse.team.internal.ccvs.ui.actions.ShowAnnotationAction;
27 import org.eclipse.team.internal.ccvs.ui.actions.ShowResourceInHistoryAction;
28 import org.eclipse.team.internal.ui.Utils;
29 import org.eclipse.team.ui.TeamUI;
30 import org.eclipse.team.ui.synchronize.*;
31 import org.eclipse.ui.IMemento;
32 import org.eclipse.ui.PartInitException;
33
34 public class MergeSynchronizeParticipant extends CVSParticipant {
35     
36     /**
37      * The id of a workspace action group to which additions actions can
38      * be added.
39      */

40     public static final String JavaDoc TOOLBAR_CONTRIBUTION_GROUP = "toolbar_group"; //$NON-NLS-1$
41

42     public static final String JavaDoc CONTEXT_MENU_CONTRIBUTION_GROUP = "context_group_1"; //$NON-NLS-1$
43
public static final String JavaDoc NON_MODAL_CONTEXT_MENU_CONTRIBUTION_GROUP = "context_group_2"; //$NON-NLS-1$
44

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

52     /**
53      * Actions for the merge particpant's toolbar
54      */

55     public class MergeParticipantActionContribution extends SynchronizePageActionGroup {
56         private MergeUpdateAction updateAction;
57         public void initialize(ISynchronizePageConfiguration configuration) {
58             super.initialize(configuration);
59             
60             updateAction = new MergeUpdateAction(
61                     configuration,
62                     getVisibleRootsSelectionProvider(),
63                     "WorkspaceToolbarUpdateAction."); //$NON-NLS-1$
64
updateAction.setPromptBeforeUpdate(true);
65             appendToGroup(
66                     ISynchronizePageConfiguration.P_TOOLBAR_MENU,
67                     TOOLBAR_CONTRIBUTION_GROUP,
68                     updateAction);
69             
70             appendToGroup(
71                     ISynchronizePageConfiguration.P_CONTEXT_MENU,
72                     CONTEXT_MENU_CONTRIBUTION_GROUP,
73                     new MergeUpdateAction(configuration));
74             appendToGroup(
75                     ISynchronizePageConfiguration.P_CONTEXT_MENU,
76                     CONTEXT_MENU_CONTRIBUTION_GROUP,
77                     new ConfirmMergedAction(configuration));
78             
79             if (!configuration.getSite().isModal()) {
80                 appendToGroup(
81                     ISynchronizePageConfiguration.P_CONTEXT_MENU,
82                     NON_MODAL_CONTEXT_MENU_CONTRIBUTION_GROUP,
83                     new CVSActionDelegateWrapper(new ShowAnnotationAction(), configuration));
84                 appendToGroup(
85                     ISynchronizePageConfiguration.P_CONTEXT_MENU,
86                     NON_MODAL_CONTEXT_MENU_CONTRIBUTION_GROUP,
87                     new CVSActionDelegateWrapper(new ShowResourceInHistoryAction(), configuration));
88             }
89
90         }
91     }
92     
93     public MergeSynchronizeParticipant() {
94         super();
95     }
96     
97     public MergeSynchronizeParticipant(CVSMergeSubscriber subscriber) {
98         setSubscriber(subscriber);
99     }
100     
101     /* (non-Javadoc)
102      * @see org.eclipse.team.ui.sync.SubscriberParticipant#setSubscriber(org.eclipse.team.core.subscribers.TeamSubscriber)
103      */

104     public void setSubscriber(Subscriber subscriber) {
105         super.setSubscriber(subscriber);
106         try {
107             ISynchronizeParticipantDescriptor descriptor = TeamUI.getSynchronizeManager().getParticipantDescriptor(CVSMergeSubscriber.ID);
108             setInitializationData(descriptor);
109             CVSMergeSubscriber s = (CVSMergeSubscriber)getSubscriber();
110             setSecondaryId(s.getId().getLocalName());
111         } catch (CoreException e) {
112             CVSUIPlugin.log(e);
113         }
114     }
115     
116     /* (non-Javadoc)
117      * @see org.eclipse.team.ui.synchronize.ISynchronizeParticipant#init(org.eclipse.ui.IMemento)
118      */

119     public void init(String JavaDoc secondayId, IMemento memento) throws PartInitException {
120         super.init(secondayId, memento);
121         if(memento != null) {
122             ISynchronizeParticipantDescriptor descriptor = TeamUI.getSynchronizeManager().getParticipantDescriptor(CVSMergeSubscriber.ID);
123             String JavaDoc qualifier = descriptor.getId();
124             String JavaDoc localname = secondayId;
125             if(qualifier == null || localname == null) {
126                 throw new PartInitException(CVSUIMessages.MergeSynchronizeParticipant_8);
127             }
128             try {
129                 setSubscriber(read(new QualifiedName(qualifier, localname), memento));
130             } catch (CVSException e) {
131                 throw new PartInitException(CVSUIMessages.MergeSynchronizeParticipant_9, e);
132             }
133         }
134     }
135     
136     /* (non-Javadoc)
137      * @see org.eclipse.team.ui.synchronize.ISynchronizeParticipant#init(org.eclipse.ui.IMemento)
138      */

139     public void saveState(IMemento memento) {
140         super.saveState(memento);
141         CVSMergeSubscriber s = (CVSMergeSubscriber)getSubscriber();
142         write(s, memento);
143     }
144         
145     /* (non-Javadoc)
146      * @see org.eclipse.team.ui.sync.AbstractSynchronizeParticipant#dispose()
147      */

148     public void dispose() {
149         super.dispose();
150         if(TeamUI.getSynchronizeManager().get(getId(), getSecondaryId()) == null) {
151             // If the participant isn't managed by the synchronize manager then we
152
// must ensure that the state cached in the synchronizer is flushed.
153
flushStateCache();
154         }
155     }
156     
157     /* (non-Javadoc)
158      * @see org.eclipse.team.ui.synchronize.ISynchronizeParticipant#getName()
159      */

160     public String JavaDoc getName() {
161         return NLS.bind(CVSUIMessages.CompareParticipant_0, new String JavaDoc[] { ((CVSMergeSubscriber)getSubscriber()).getName(), Utils.convertSelection(getSubscriber().roots()) });
162     }
163     
164     /*
165      * Returns the start tag for this merge participant. The start tag is actually stored with the subscriber.
166      */

167     protected CVSTag getStartTag() {
168         return ((CVSMergeSubscriber)getSubscriber()).getStartTag();
169     }
170     
171     /*
172      * Returns the end tag for this merge participant. The end tag is actually stored with the subscriber.
173      */

174     protected CVSTag getEndTag() {
175         return ((CVSMergeSubscriber)getSubscriber()).getEndTag();
176     }
177     
178     /*
179      * Returns a merge participant that exist and is configured with the given set of resources, start, and end tags.
180      */

181     public static MergeSynchronizeParticipant getMatchingParticipant(IResource[] resources, CVSTag startTag, CVSTag endTag) {
182         ISynchronizeParticipantReference[] refs = TeamUI.getSynchronizeManager().getSynchronizeParticipants();
183         for (int i = 0; i < refs.length; i++) {
184             ISynchronizeParticipantReference reference = refs[i];
185             if (reference.getId().equals(CVSMergeSubscriber.ID)) {
186                 MergeSynchronizeParticipant p;
187                 try {
188                     p = (MergeSynchronizeParticipant) reference.getParticipant();
189                 } catch (TeamException e) {
190                     continue;
191                 }
192                 IResource[] roots = p.getResources();
193                 Arrays.sort(resources, Utils.resourceComparator);
194                 Arrays.sort(roots, Utils.resourceComparator);
195                 if (Arrays.equals(resources, roots) && p.getStartTag().equals(startTag) && p.getEndTag().equals(endTag)) {
196                     return p;
197                 }
198             }
199         }
200         return null;
201     }
202     
203     private void write(CVSMergeSubscriber s, IMemento memento) {
204         // start and end tags
205
CVSTag start = s.getStartTag();
206         CVSTag end = s.getEndTag();
207         memento.putString(CTX_START_TAG, start.getName());
208         memento.putInteger(CTX_START_TAG_TYPE, start.getType());
209         memento.putString(CTX_END_TAG, end.getName());
210         memento.putInteger(CTX_END_TAG_TYPE, end.getType());
211         
212         // resource roots
213
IResource[] roots = s.roots();
214         for (int i = 0; i < roots.length; i++) {
215             IResource resource = roots[i];
216             IMemento rootNode = memento.createChild(CTX_ROOT);
217             rootNode.putString(CTX_ROOT_PATH, resource.getFullPath().toString());
218         }
219     }
220     
221     private CVSMergeSubscriber read(QualifiedName id, IMemento memento) throws CVSException {
222         CVSTag start = new CVSTag(memento.getString(CTX_START_TAG), memento.getInteger(CTX_START_TAG_TYPE).intValue()); //
223
CVSTag end = new CVSTag(memento.getString(CTX_END_TAG), memento.getInteger(CTX_END_TAG_TYPE).intValue()); //
224

225         IMemento[] rootNodes = memento.getChildren(CTX_ROOT);
226         if(rootNodes == null || rootNodes.length == 0) {
227             throw new CVSException(NLS.bind(CVSUIMessages.MergeSynchronizeParticipant_10, new String JavaDoc[] { id.toString() }));
228         }
229         
230         List JavaDoc resources = new ArrayList JavaDoc();
231         for (int i = 0; i < rootNodes.length; i++) {
232             IMemento rootNode = rootNodes[i];
233             IPath path = new Path(rootNode.getString(CTX_ROOT_PATH));
234             IResource resource = ResourcesPlugin.getWorkspace().getRoot().findMember(path, true /* include phantoms */);
235             if(resource != null) {
236                 resources.add(resource);
237             } else {
238                 // log that a resource previously in the merge set is no longer in the workspace
239
CVSProviderPlugin.log(IStatus.INFO, NLS.bind(CVSUIMessages.MergeSynchronizeParticipant_11, new String JavaDoc[] { path.toString() }), null);
240             }
241         }
242         if(resources.isEmpty()) {
243             throw new CVSException(NLS.bind(CVSUIMessages.MergeSynchronizeParticipant_12, new String JavaDoc[] { id.toString() }));
244         }
245         IResource[] roots = (IResource[]) resources.toArray(new IResource[resources.size()]);
246         return new CVSMergeSubscriber(id, roots, start, end);
247     }
248     
249     private void flushStateCache() {
250         ((CVSMergeSubscriber)getSubscriber()).cancel();
251     }
252
253     /* (non-Javadoc)
254      * @see org.eclipse.team.ui.synchronize.subscribers.SubscriberParticipant#initializeConfiguration(org.eclipse.team.ui.synchronize.ISynchronizePageConfiguration)
255      */

256     protected void initializeConfiguration(ISynchronizePageConfiguration configuration) {
257         super.initializeConfiguration(configuration);
258         configuration.addMenuGroup(ISynchronizePageConfiguration.P_TOOLBAR_MENU, TOOLBAR_CONTRIBUTION_GROUP);
259         configuration.addMenuGroup(
260                 ISynchronizePageConfiguration.P_CONTEXT_MENU,
261                 CONTEXT_MENU_CONTRIBUTION_GROUP);
262         configuration.addMenuGroup(
263                 ISynchronizePageConfiguration.P_CONTEXT_MENU,
264                 NON_MODAL_CONTEXT_MENU_CONTRIBUTION_GROUP);
265         configuration.setSupportedModes(ISynchronizePageConfiguration.INCOMING_MODE | ISynchronizePageConfiguration.CONFLICTING_MODE);
266         configuration.setMode(ISynchronizePageConfiguration.INCOMING_MODE);
267         configuration.addActionContribution(new MergeParticipantActionContribution());
268     }
269     
270     /* (non-Javadoc)
271      * @see org.eclipse.team.ui.synchronize.SubscriberParticipant#getLongTaskName()
272      */

273     protected String JavaDoc getLongTaskName() {
274         return getName();
275     }
276     
277     /* (non-Javadoc)
278      * @see org.eclipse.team.ui.synchronize.SubscriberParticipant#getShortTaskName()
279      */

280     protected String JavaDoc getShortTaskName() {
281         return CVSUIMessages.Participant_merging;
282     }
283     
284     protected CVSChangeSetCapability createChangeSetCapability() {
285         // See bug 84561 for a description of the problems with Merge Change Sets
286
return null;
287     }
288 }
289
Popular Tags