KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > ui > mapping > SynchronizationOperation


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.ui.mapping;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14
15 import org.eclipse.core.resources.mapping.ResourceTraversal;
16 import org.eclipse.core.runtime.*;
17 import org.eclipse.core.runtime.jobs.*;
18 import org.eclipse.team.core.diff.IDiff;
19 import org.eclipse.team.core.mapping.*;
20 import org.eclipse.team.internal.ui.*;
21 import org.eclipse.team.ui.TeamOperation;
22 import org.eclipse.team.ui.synchronize.ISynchronizePageConfiguration;
23 import org.eclipse.team.ui.synchronize.ISynchronizePageSite;
24 import org.eclipse.ui.IWorkbenchPart;
25
26 /**
27  * This operation class can be used by model providers when performing
28  * merge operations triggered from a synchronize participant page
29  * associated with a synchronization or merge context.
30  * <p>
31  * This class may be subclasses by clients.
32  *
33  * @see ISynchronizationContext
34  * @see IMergeContext
35  *
36  * @since 3.2
37  */

38 public abstract class SynchronizationOperation extends TeamOperation {
39
40     private final ISynchronizePageConfiguration configuration;
41     private final Object JavaDoc[] elements;
42
43     /*
44      * Helper method for extracting the part safely from a configuration
45      */

46     private static IWorkbenchPart getPart(ISynchronizePageConfiguration configuration) {
47         if (configuration != null) {
48             ISynchronizePageSite site = configuration.getSite();
49             if (site != null) {
50                 return site.getPart();
51             }
52         }
53         return null;
54     }
55     
56     /**
57      * Create a synchronize operation that operations on the given elements
58      * @param configuration the configuration for the page the operation is associated with
59      * @param elements the elements to be operated on
60      */

61     protected SynchronizationOperation(ISynchronizePageConfiguration configuration, Object JavaDoc[] elements) {
62         super(getPart(configuration), configuration.getRunnableContext());
63         this.configuration = configuration;
64         this.elements = elements;
65     }
66
67     /**
68      * Return the configuration for the page from which this
69      * operation was launched.
70      * @return the configuration for the page from which this
71      * operation was launched
72      */

73     public ISynchronizePageConfiguration getConfiguration() {
74         return configuration;
75     }
76
77     /**
78      * Return the synchronization context associated with this action.
79      * @return the synchronization context associated with this action
80      */

81     protected ISynchronizationContext getContext() {
82         return (ISynchronizationContext)getConfiguration().getProperty(ITeamContentProviderManager.P_SYNCHRONIZATION_CONTEXT);
83     }
84     
85     /**
86      * Return the model elements that are the target of this operation.
87      * @return the model elements that are the target of this operation
88      */

89     public Object JavaDoc[] getElements() {
90         return elements;
91     }
92     
93     /**
94      * Make <code>shouldRun</code> public so the result
95      * can be used to provide handler enablement
96      */

97     public boolean shouldRun() {
98         return super.shouldRun();
99     }
100     
101     /**
102      * Return the saveable that this operation will write its results
103      * to or <code>null</code> if the operation does not buffer
104      * its results. By default, <code>null</code> is returned but
105      * subclasses may override.
106      * @return the saveable that this operation will write its results
107      * to or <code>null</code>
108      */

109     public SaveableComparison getSaveable() {
110         return null;
111     }
112     
113     /* (non-Javadoc)
114      * @see org.eclipse.jface.operation.IRunnableWithProgress#run(org.eclipse.core.runtime.IProgressMonitor)
115      */

116     public final void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
117         try {
118             monitor.beginTask(null, 100);
119             setContextBusy(Policy.subMonitorFor(monitor, 5));
120             execute(Policy.subMonitorFor(monitor, 90));
121         } finally {
122             clearContextBusy(Policy.subMonitorFor(monitor, 5));
123             monitor.done();
124         }
125     }
126
127     private void clearContextBusy(final IProgressMonitor monitor) {
128         // Add a job change listener to the job manager that will clear the busy
129
// when there are no more jobs related to the context running
130
final IJobManager jobManager = Platform.getJobManager();
131         final IJobChangeListener listener = new JobChangeAdapter() {
132             public void done(IJobChangeEvent event) {
133                 Job[] jobs = jobManager.find(getContext());
134                 if (jobs.length == 0) {
135                     final IResourceDiffTree diffTree = getContext().getDiffTree();
136                     diffTree.clearBusy(null);
137                     jobManager.removeJobChangeListener(this);
138                 }
139             }
140         };
141         jobManager.addJobChangeListener(listener);
142     }
143
144     private void setContextBusy(final IProgressMonitor monitor) {
145         try {
146             // TODO: This may miss setting some diffs (i.e. those that don't exist locally)
147
ResourceTraversal[] traversals = Utils.getTraversals(getElements());
148             final IResourceDiffTree diffTree = getContext().getDiffTree();
149             IDiff[] diffs = diffTree.getDiffs(traversals);
150             diffTree.setBusy(diffs, monitor);
151         } catch (CoreException e) {
152             TeamUIPlugin.log(e);
153         }
154     }
155
156     /**
157      * Execute the operation. Subclasses should implement the operations behavior in the
158      * execute method. Clients should call either {@link #run()} or {@link #run(IProgressMonitor)}
159      * to invoke the operation.
160      * @param monitor a progress monitor
161      * @throws InvocationTargetException
162      * @throws InterruptedException
163      */

164     protected abstract void execute(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc;
165
166 }
167
Popular Tags