KickJava   Java API By Example, From Geeks To Geeks.

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


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.ui.synchronize;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.compare.structuremergeviewer.IDiffElement;
17 import org.eclipse.core.runtime.jobs.IJobChangeEvent;
18 import org.eclipse.jface.operation.IRunnableContext;
19 import org.eclipse.team.core.synchronize.SyncInfo;
20 import org.eclipse.team.core.synchronize.SyncInfoSet;
21 import org.eclipse.team.internal.ui.synchronize.SyncInfoModelElement;
22 import org.eclipse.team.ui.TeamOperation;
23 import org.eclipse.ui.IWorkbenchPart;
24
25 /**
26  * A specialized team operation that operates on
27  * {@link org.eclipse.team.ui.synchronize.ISynchronizeModelElement}elements. If
28  * the operation is run in the background the elements the operation is created
29  * with will be updated to show that they are busy while the operation is
30  * running and will be marked un-busy after the operation completes.
31  *
32  * @see SyncInfoSet
33  * @see SynchronizeModelAction
34  * @since 3.0
35  */

36 public abstract class SynchronizeModelOperation extends TeamOperation {
37     
38     private IDiffElement[] elements;
39     
40     /*
41      * Helper method for extracting the part safely from a configuration
42      */

43     private static IWorkbenchPart getPart(ISynchronizePageConfiguration configuration) {
44         if (configuration != null) {
45             ISynchronizePageSite site = configuration.getSite();
46             if (site != null) {
47                 return site.getPart();
48             }
49         }
50         return null;
51     }
52     
53     /*
54      * Helper method for extracting the runnable context safely from a configuration
55      */

56     private static IRunnableContext getRunnableContext(ISynchronizePageConfiguration configuration) {
57         if (configuration != null) {
58             return configuration.getRunnableContext();
59         }
60         return null;
61     }
62     
63     /**
64      * Create an operation that will operate on the given diff elements.
65      *
66      * @param configuration the participant configuration in which this
67      * operation is run
68      * @param elements the model elements this operation will run with
69      */

70     protected SynchronizeModelOperation(ISynchronizePageConfiguration configuration, IDiffElement[] elements) {
71         super(getPart(configuration), getRunnableContext(configuration));
72         this.elements = elements;
73     }
74
75     /**
76      * Returns a sync info set that contains the {@link SyncInfo}for the
77      * elements of this operations.
78      *
79      * @return the sync info set that contains the elements this operation is
80      * operating on.
81      */

82     protected SyncInfoSet getSyncInfoSet() {
83         return makeSyncInfoSetFromSelection(getSyncInfos());
84     }
85
86     /* (non-Javadoc)
87      * @see org.eclipse.core.runtime.jobs.JobChangeAdapter#scheduled(org.eclipse.core.runtime.jobs.IJobChangeEvent)
88      */

89     public void scheduled(IJobChangeEvent event) {
90         super.scheduled(event);
91         markBusy(elements, true);
92     }
93     
94     /* (non-Javadoc)
95      * @see org.eclipse.core.runtime.jobs.JobChangeAdapter#done(org.eclipse.core.runtime.jobs.IJobChangeEvent)
96      */

97     public void done(IJobChangeEvent event) {
98         markBusy(elements, false);
99         super.done(event);
100     }
101     
102     private void markBusy(IDiffElement[] elements, boolean isBusy) {
103         for (int i = 0; i < elements.length; i++) {
104             IDiffElement element = elements[i];
105             if (element instanceof ISynchronizeModelElement) {
106                 ((ISynchronizeModelElement)element).setPropertyToRoot(ISynchronizeModelElement.BUSY_PROPERTY, isBusy);
107             }
108         }
109     }
110     
111     /*
112      * Return the selected SyncInfo for which this action is enabled.
113      *
114      * @return the selected SyncInfo for which this action is enabled.
115      */

116     private SyncInfo[] getSyncInfos() {
117         List JavaDoc filtered = new ArrayList JavaDoc();
118         for (int i = 0; i < elements.length; i++) {
119             IDiffElement e = elements[i];
120             if (e instanceof SyncInfoModelElement) {
121                 filtered.add(((SyncInfoModelElement)e).getSyncInfo());
122             }
123         }
124         return (SyncInfo[]) filtered.toArray(new SyncInfo[filtered.size()]);
125     }
126     
127     /*
128      * Return a sync info set that contains the given sync info
129      */

130     private SyncInfoSet makeSyncInfoSetFromSelection(SyncInfo[] infos) {
131         return new SyncInfoSet(infos);
132     }
133 }
134
Popular Tags