KickJava   Java API By Example, From Geeks To Geeks.

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


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.List JavaDoc;
15
16 import org.eclipse.compare.structuremergeviewer.IDiffElement;
17 import org.eclipse.core.resources.*;
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IProgressMonitor;
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.core.synchronize.SyncInfo;
24 import org.eclipse.team.core.synchronize.SyncInfoSet;
25 import org.eclipse.team.internal.ccvs.core.CVSException;
26 import org.eclipse.team.internal.ccvs.core.CVSMergeSubscriber;
27 import org.eclipse.team.internal.ccvs.core.CVSSyncInfo;
28 import org.eclipse.team.internal.ccvs.core.CVSTag;
29 import org.eclipse.team.internal.ccvs.core.client.Command;
30 import org.eclipse.team.internal.ccvs.core.client.Update;
31 import org.eclipse.team.internal.ccvs.ui.CVSUIMessages;
32 import org.eclipse.team.internal.ccvs.ui.Policy;
33 import org.eclipse.team.ui.synchronize.ISynchronizePageConfiguration;
34
35 /**
36  * This action performs a "cvs update -j start -j end ..." to merge changes
37  * into the local workspace.
38  */

39 public class MergeUpdateOperation extends SafeUpdateOperation {
40     
41     Subscriber currentSubcriber = null;
42     
43     protected MergeUpdateOperation(ISynchronizePageConfiguration configuration, IDiffElement[] elements, boolean promptBeforeUpdate) {
44         super(configuration, elements, promptBeforeUpdate);
45     }
46     
47     /* (non-Javadoc)
48      * @see org.eclipse.team.internal.ccvs.ui.subscriber.CVSSubscriberAction#getJobName(org.eclipse.team.ui.sync.SyncInfoSet)
49      */

50     protected String JavaDoc getJobName() {
51         SyncInfoSet syncSet = getSyncInfoSet();
52         return NLS.bind(CVSUIMessages.MergeUpdateAction_jobName, new String JavaDoc[] { new Integer JavaDoc(syncSet.size()).toString() });
53     }
54     
55     /* (non-Javadoc)
56      * @see org.eclipse.team.internal.ccvs.ui.subscriber.SafeUpdateOperation#getOverwriteLocalChanges()
57      */

58     protected boolean getOverwriteLocalChanges() {
59         return true;
60     }
61     
62     /* (non-Javadoc)
63      * @see org.eclipse.team.internal.ccvs.ui.subscriber.SafeUpdateOperation#updated(org.eclipse.core.resources.IResource[])
64      */

65     protected void updated(IResource[] resources) throws TeamException {
66         // Mark all succesfully updated resources as merged
67
if(resources.length > 0 && currentSubcriber != null) {
68             ((CVSMergeSubscriber)currentSubcriber).merged(resources);
69         }
70     }
71     
72     /* (non-Javadoc)
73      * @see org.eclipse.team.internal.ccvs.ui.subscriber.SafeUpdateOperation#runUpdateDeletions(org.eclipse.team.core.synchronize.SyncInfo[], org.eclipse.core.runtime.IProgressMonitor)
74      */

75     protected void runUpdateDeletions(SyncInfo[] nodes, IProgressMonitor monitor) throws TeamException {
76         // When merging, update deletions become outgoing deletions so just delete
77
// the files locally without unmanaging (so the sync info is kept to
78
// indicate an outgoing deletion
79
try {
80             monitor.beginTask(null, 100 * nodes.length);
81             for (int i = 0; i < nodes.length; i++) {
82                 IResource resource = nodes[i].getLocal();
83                 if (resource.getType() == IResource.FILE) {
84                     ((IFile)resource).delete(false /* force */, true /* keep local history */, Policy.subMonitorFor(monitor, 100));
85                 }
86             }
87         } catch (CoreException e) {
88             throw CVSException.wrapException(e);
89         } finally {
90             monitor.done();
91         }
92     }
93     
94     /* (non-Javadoc)
95      * @see org.eclipse.team.internal.ccvs.ui.subscriber.SafeUpdateOperation#runSafeUpdate(org.eclipse.team.core.synchronize.SyncInfo[], org.eclipse.core.runtime.IProgressMonitor)
96      */

97     protected void runSafeUpdate(IProject project, SyncInfo[] nodes, IProgressMonitor monitor) throws TeamException {
98         if(nodes.length > 0) {
99             setSubscriber(nodes[0]);
100             CVSTag startTag = ((CVSMergeSubscriber)currentSubcriber).getStartTag();
101             CVSTag endTag = ((CVSMergeSubscriber)currentSubcriber).getEndTag();
102
103             // Incoming additions require different handling then incoming changes and deletions
104
List JavaDoc additions = new ArrayList JavaDoc();
105             List JavaDoc changes = new ArrayList JavaDoc();
106             for (int i = 0; i < nodes.length; i++) {
107                 SyncInfo resource = nodes[i];
108                 int kind = resource.getKind();
109                 if ((kind & SyncInfo.CHANGE_MASK) == SyncInfo.ADDITION) {
110                     additions.add(resource);
111                 } else {
112                     changes.add(resource);
113                 }
114             }
115             
116             try {
117                 monitor.beginTask(null, (additions.size() + changes.size()) * 100);
118                 if (!additions.isEmpty()) {
119                     safeUpdate(
120                         project,
121                         getIResourcesFrom((SyncInfo[]) additions.toArray(new SyncInfo[additions.size()])),
122                         new Command.LocalOption[] {
123                             Command.DO_NOT_RECURSE,
124                             Command.makeArgumentOption(Update.JOIN, endTag.getName())
125                         },
126                         Policy.subMonitorFor(monitor, additions.size() * 100));
127                 }
128                 if (!changes.isEmpty()) {
129                     safeUpdate(
130                         project,
131                         getIResourcesFrom((SyncInfo[]) changes.toArray(new SyncInfo[changes.size()])),
132                         new Command.LocalOption[] {
133                             Command.DO_NOT_RECURSE,
134                             Command.makeArgumentOption(Update.JOIN, startTag.getName()),
135                             Command.makeArgumentOption(Update.JOIN, endTag.getName())
136                         },
137                         Policy.subMonitorFor(monitor, changes.size() * 100));
138                 }
139             } finally {
140                 monitor.done();
141             }
142         }
143     }
144     
145     /**
146      * @param nodes
147      * @throws CVSException
148      */

149     private void setSubscriber(SyncInfo node) throws CVSException {
150         // Assumption that all nodes are from the same subscriber.
151
currentSubcriber = ((CVSSyncInfo)node).getSubscriber();
152         if (!(currentSubcriber instanceof CVSMergeSubscriber)) {
153             throw new CVSException(NLS.bind(CVSUIMessages.MergeUpdateAction_invalidSubscriber, new String JavaDoc[] { currentSubcriber.toString() }));
154         }
155     }
156
157     /* (non-Javadoc)
158      * @see org.eclipse.team.internal.ccvs.ui.subscriber.SafeUpdateOperation#overwriteUpdate(org.eclipse.team.core.synchronize.SyncInfoSet, org.eclipse.core.runtime.IProgressMonitor)
159      */

160     protected void overwriteUpdate(SyncInfoSet set, IProgressMonitor monitor) throws TeamException {
161         SyncInfo[] nodes = set.getSyncInfos();
162         if (nodes.length == 0) return;
163         setSubscriber(nodes[0]);
164         monitor.beginTask(null, 1000 * nodes.length);
165         try {
166             for (int i = 0; i < nodes.length; i++) {
167                 makeRemoteLocal(nodes[i], Policy.subMonitorFor(monitor, 1000));
168             }
169         } finally {
170             monitor.done();
171         }
172     }
173 }
174
Popular Tags