KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > ui > synchronize > ModelParticipantMergeOperation


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.ui.synchronize;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14
15 import org.eclipse.compare.CompareConfiguration;
16 import org.eclipse.compare.CompareUI;
17 import org.eclipse.core.resources.mapping.RemoteResourceMappingContext;
18 import org.eclipse.core.runtime.*;
19 import org.eclipse.core.runtime.jobs.*;
20 import org.eclipse.team.core.mapping.ISynchronizationContext;
21 import org.eclipse.team.core.mapping.ISynchronizationScopeManager;
22 import org.eclipse.team.core.mapping.provider.SynchronizationContext;
23 import org.eclipse.team.internal.ui.TeamUIMessages;
24 import org.eclipse.team.ui.TeamUI;
25 import org.eclipse.ui.IWorkbenchPart;
26 import org.eclipse.ui.progress.IWorkbenchSiteProgressService;
27 import org.eclipse.ui.progress.WorkbenchJob;
28
29 /**
30  * A model merge operation that uses a participant to preview the changes
31  * in either a dialog or the Synchronize view.
32  *
33  * @since 3.2
34  */

35 public abstract class ModelParticipantMergeOperation extends ModelMergeOperation {
36     
37     /**
38      * Status code that can be returned from the {@link #performMerge(IProgressMonitor)}
39      * method to indicate that a subclass would like to force a preview of the merge.
40      * The message of such a status should be ignored.
41      */

42     public static final int REQUEST_PREVIEW = 1024;
43
44     private ModelSynchronizeParticipant participant;
45     private boolean ownsParticipant = true;
46
47     private boolean sentToSyncView;
48     
49     private static final Object JavaDoc PARTICIPANT_MERGE_FAMILY = new Object JavaDoc();
50     
51     /**
52      * Create a merge participant operation for the scope of the given manager.
53      * @param part the workbench part from which the merge was launched or <code>null</code>
54      * @param manager the scope manager
55      */

56     protected ModelParticipantMergeOperation(IWorkbenchPart part, ISynchronizationScopeManager manager) {
57         super(part, manager);
58     }
59
60     /* (non-Javadoc)
61      * @see org.eclipse.team.ui.operations.ModelMergeOperation#initializeContext(org.eclipse.core.runtime.IProgressMonitor)
62      */

63     protected void initializeContext(IProgressMonitor monitor) throws CoreException {
64         if (participant == null) {
65             participant = createParticipant();
66             if (isPreviewRequested() && !isPreviewInDialog()) {
67                 // Put the participant into the sync view right away since a preview is requested
68
handlePreviewRequest();
69                 sentToSyncView = true;
70             }
71             participant.getContext().refresh(getScope().getTraversals(),
72                     RemoteResourceMappingContext.FILE_CONTENTS_REQUIRED, monitor);
73             // Only wait if we are not going to preview or we are previewing in a dialog
74
if (!sentToSyncView)
75                 try {
76                     Job.getJobManager().join(participant.getContext(), monitor);
77                 } catch (InterruptedException JavaDoc e) {
78                     // Ignore
79
}
80         }
81     }
82     
83     /* (non-Javadoc)
84      * @see org.eclipse.team.ui.operations.ModelMergeOperation#execute(org.eclipse.core.runtime.IProgressMonitor)
85      */

86     protected void execute(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
87         try {
88             super.execute(monitor);
89         } finally {
90             if (ownsParticipant && participant != null)
91                 participant.dispose();
92         }
93     }
94     
95     /* (non-Javadoc)
96      * @see org.eclipse.team.ui.mapping.ModelMergeOperation#executeMerge(org.eclipse.core.runtime.IProgressMonitor)
97      */

98     protected void executeMerge(IProgressMonitor monitor) throws CoreException {
99         if (!sentToSyncView)
100             super.executeMerge(monitor);
101     }
102     
103     /* (non-Javadoc)
104      * @see org.eclipse.team.ui.operations.ModelMergeOperation#handlePreviewRequest()
105      */

106     protected void handlePreviewRequest() {
107         Job job = new WorkbenchJob(getJobName()) {
108             public IStatus runInUIThread(IProgressMonitor monitor) {
109                 if (isPreviewInDialog()) {
110                     CompareConfiguration cc = new CompareConfiguration();
111                     ISynchronizePageConfiguration pageConfiguration = participant.createPageConfiguration();
112                     // Restrict preview page to only support incoming and conflict modes
113
if (pageConfiguration.getComparisonType() == ISynchronizePageConfiguration.THREE_WAY) {
114                         pageConfiguration.setSupportedModes(ISynchronizePageConfiguration.INCOMING_MODE | ISynchronizePageConfiguration.CONFLICTING_MODE);
115                         pageConfiguration.setMode(ISynchronizePageConfiguration.INCOMING_MODE);
116                     }
117                     ParticipantPageCompareEditorInput input = new ParticipantPageCompareEditorInput(cc, pageConfiguration, participant);
118                     CompareUI.openCompareDialog(input);
119                 } else {
120                     ISynchronizeManager mgr = TeamUI.getSynchronizeManager();
121                     ISynchronizeView view = mgr.showSynchronizeViewInActivePage();
122                     mgr.addSynchronizeParticipants(new ISynchronizeParticipant[] {participant});
123                     view.display(participant);
124                     Object JavaDoc adapted = view.getSite().getAdapter(IWorkbenchSiteProgressService.class);
125                     if (adapted instanceof IWorkbenchSiteProgressService) {
126                         IWorkbenchSiteProgressService siteProgress = (IWorkbenchSiteProgressService) adapted;
127                         siteProgress.showBusyForFamily(PARTICIPANT_MERGE_FAMILY);
128                     }
129                 }
130                 return Status.OK_STATUS;
131             }
132         };
133         job.addJobChangeListener(new JobChangeAdapter() {
134             public void done(IJobChangeEvent event) {
135                 // Ensure that the participant is disposed it it didn't go to the sync view
136
if (TeamUI.getSynchronizeManager().get(participant.getId(), participant.getSecondaryId()) == null)
137                     participant.dispose();
138             }
139             
140         });
141         ownsParticipant = false;
142         job.schedule();
143     }
144
145     public boolean belongsTo(Object JavaDoc family) {
146         if (family == PARTICIPANT_MERGE_FAMILY) {
147             return true;
148         }
149         if (participant != null && participant == family)
150             return true;
151         return super.belongsTo(family);
152     }
153     
154     /**
155      * Return whether previews should occur in a dialog or in the synchronize view.
156      * @return whether previews should occur in a dialog or in the synchronize view
157      */

158     protected boolean isPreviewInDialog() {
159         return true;
160     }
161
162     /* (non-Javadoc)
163      * @see org.eclipse.team.ui.operations.ResourceMappingOperation#getContext()
164      */

165     protected ISynchronizationContext getContext() {
166         if (participant != null)
167             return participant.getContext();
168         return null;
169     }
170     
171     /* (non-Javadoc)
172      * @see org.eclipse.team.ui.operations.ResourceMappingOperation#getPreviewRequestMessage()
173      */

174     protected String JavaDoc getPreviewRequestMessage() {
175         if (!isPreviewRequested()) {
176             return TeamUIMessages.ResourceMappingMergeOperation_4;
177         }
178         return super.getPreviewRequestMessage();
179     }
180
181     /**
182      * Create the synchronize participant to be used by this operation
183      * to preview changes. By default, a {@link ModelSynchronizeParticipant}
184      * is created using the scope manager ({@link #getScopeManager()}) context
185      * from ({@link #createMergeContext()}) and job name ({@link #getJobName()})
186      * of this operation. Subclasses may override this method.
187      * <p>
188      * Once created, it is the responsibility of the participant to dispose of the
189      * synchronization context when it is no longer needed.
190      * @return a newly created synchronize participant to be used by this operation
191      */

192     protected ModelSynchronizeParticipant createParticipant() {
193         return ModelSynchronizeParticipant.createParticipant(createMergeContext(), getJobName());
194     }
195
196     /**
197      * Create a merge context for use by this operation. This method
198      * is not long running so the operation should not refresh the
199      * context or perform other long running operations in this thread.
200      * However the context may start initializing in another thread as long
201      * as the job used to perform the initialization belongs to the
202      * family that matches the context.
203      * @return a merge context for use by this operation
204      */

205     protected abstract SynchronizationContext createMergeContext();
206
207 }
208
Popular Tags