1 11 package org.eclipse.team.internal.ccvs.ui.subscriber; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.util.ArrayList ; 15 import java.util.HashMap ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 import java.util.Map ; 19 20 import org.eclipse.compare.structuremergeviewer.IDiffElement; 21 import org.eclipse.core.resources.IContainer; 22 import org.eclipse.core.resources.IFile; 23 import org.eclipse.core.resources.IProject; 24 import org.eclipse.core.resources.IResource; 25 import org.eclipse.core.resources.ResourcesPlugin; 26 import org.eclipse.core.runtime.CoreException; 27 import org.eclipse.core.runtime.IProgressMonitor; 28 import org.eclipse.core.runtime.IStatus; 29 import org.eclipse.jface.window.Window; 30 import org.eclipse.osgi.util.NLS; 31 import org.eclipse.team.internal.ccvs.core.CVSStatus; 32 import org.eclipse.team.core.TeamException; 33 import org.eclipse.team.core.synchronize.SyncInfo; 34 import org.eclipse.team.core.synchronize.SyncInfoSet; 35 import org.eclipse.team.core.variants.IResourceVariant; 36 import org.eclipse.team.internal.ccvs.core.*; 37 import org.eclipse.team.internal.ccvs.core.client.PruneFolderVisitor; 38 import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot; 39 import org.eclipse.team.internal.ccvs.core.resources.EclipseSynchronizer; 40 import org.eclipse.team.internal.ccvs.ui.*; 41 import org.eclipse.team.internal.ccvs.ui.Policy; 42 import org.eclipse.team.internal.ui.TeamUIPlugin; 43 import org.eclipse.team.ui.synchronize.ISynchronizePageConfiguration; 44 import org.eclipse.team.ui.synchronize.SynchronizeModelOperation; 45 46 public abstract class CVSSubscriberOperation extends SynchronizeModelOperation { 47 48 protected CVSSubscriberOperation(ISynchronizePageConfiguration configuration, IDiffElement[] elements) { 49 super(configuration, elements); 50 } 51 52 55 public void run(IProgressMonitor monitor) throws InvocationTargetException , InterruptedException { 56 final Map projectSyncInfos = getProjectSyncInfoSetMap(); 58 monitor.beginTask(null, projectSyncInfos.size() * 100); 59 for (Iterator iter = projectSyncInfos.keySet().iterator(); iter.hasNext(); ) { 60 final IProject project = (IProject) iter.next(); 61 run(projectSyncInfos, project, monitor); 62 } 63 monitor.done(); 64 } 65 66 74 protected void run(final Map projectSyncInfos, final IProject project, IProgressMonitor monitor) throws InvocationTargetException { 75 try { 76 EclipseSynchronizer.getInstance().run( 79 project, 80 new ICVSRunnable() { 81 public void run(IProgressMonitor monitor) throws CVSException { 82 try { 83 CVSSubscriberOperation.this.runWithProjectRule(project, (SyncInfoSet)projectSyncInfos.get(project), monitor); 84 } catch (TeamException e) { 85 throw CVSException.wrapException(e); 86 } 87 } 88 }, Policy.subMonitorFor(monitor, 100)); 89 } catch (TeamException e) { 90 throw new InvocationTargetException (e); 91 } 92 } 93 94 102 protected abstract void runWithProjectRule(IProject project, SyncInfoSet set, IProgressMonitor monitor) throws TeamException; 103 104 108 protected boolean isOutOfSync(SyncInfo resource) { 109 if (resource == null) return false; 110 return (!(resource.getKind() == 0) || 111 (! resource.getLocal().exists() && resource.getRemote() != null)); 112 } 113 114 protected void makeInSync(SyncInfo[] folders, IProgressMonitor monitor) throws TeamException { 115 monitor.beginTask(null, folders.length); 120 for (int i = 0; i < folders.length; i++) { 121 SyncInfo resource = folders[i]; 122 makeInSync(resource); 123 monitor.worked(1); 124 } 125 monitor.done(); 126 } 127 128 protected boolean makeInSync(SyncInfo info) throws TeamException { 129 if (isOutOfSync(info)) { 130 SyncInfo parent = getParent(info); 131 if (parent == null) { 132 if (info.getLocal().getType() == IResource.ROOT) { 133 return true; 135 } else { 136 CVSUIPlugin.log(IStatus.WARNING, NLS.bind(CVSUIMessages.CVSSubscriberAction_0, new String [] { info.getLocal().getFullPath().toString() }), null); 138 return false; 139 } 140 } else { 141 if (!makeInSync(parent)) { 142 return false; 144 } 145 } 146 if (info instanceof CVSSyncInfo) { 147 CVSSyncInfo cvsInfo= (CVSSyncInfo) info; 148 IStatus status = cvsInfo.makeInSync(); 149 if (status.getSeverity() == IStatus.ERROR) { 150 logError(status); 151 return false; 152 } 153 return true; 154 } 155 return false; 156 } else { 157 return true; 158 } 159 } 160 161 protected void makeOutgoing(SyncInfo[] folders, IProgressMonitor monitor) throws TeamException { 162 monitor.beginTask(null, 100 * folders.length); 167 for (int i = 0; i < folders.length; i++) { 168 SyncInfo info = folders[i]; 169 makeOutgoing(info, Policy.subMonitorFor(monitor, 100)); 170 } 171 monitor.done(); 172 } 173 174 private void makeOutgoing(SyncInfo info, IProgressMonitor monitor) throws TeamException { 175 if (info == null) return; 176 if (info instanceof CVSSyncInfo) { 177 CVSSyncInfo cvsInfo= (CVSSyncInfo) info; 178 IStatus status = cvsInfo.makeOutgoing(monitor); 179 if (status.getSeverity() == IStatus.ERROR) { 180 logError(status); 181 } 182 } 183 } 184 185 189 protected void logError(IStatus status) { 190 CVSUIPlugin.log(status); 191 } 192 193 198 protected void handle(Exception t) { 199 CVSUIPlugin.openError(getShell(), getErrorTitle(), null, t, CVSUIPlugin.PERFORM_SYNC_EXEC | CVSUIPlugin.LOG_NONTEAM_EXCEPTIONS); 200 } 201 202 206 protected String getErrorTitle() { 207 return null; 208 } 209 210 protected boolean canRunAsJob() { 211 return true; 212 } 213 214 protected void pruneEmptyParents(SyncInfo[] nodes) throws CVSException { 215 if (!CVSProviderPlugin.getPlugin().getPruneEmptyDirectories()) return; 218 ICVSResource[] cvsResources = new ICVSResource[nodes.length]; 219 for (int i = 0; i < cvsResources.length; i++) { 220 cvsResources[i] = CVSWorkspaceRoot.getCVSResourceFor(nodes[i].getLocal()); 221 } 222 new PruneFolderVisitor().visit( 223 CVSWorkspaceRoot.getCVSFolderFor(ResourcesPlugin.getWorkspace().getRoot()), 224 cvsResources); 225 } 226 227 public CVSSyncInfo getCVSSyncInfo(SyncInfo info) { 228 if (info instanceof CVSSyncInfo) { 229 return (CVSSyncInfo)info; 230 } 231 return null; 232 } 233 234 protected SyncInfo getParent(SyncInfo info) throws TeamException { 235 return ((CVSSyncInfo)info).getSubscriber().getSyncInfo(info.getLocal().getParent()); 236 } 237 238 protected IResource[] getIResourcesFrom(SyncInfo[] nodes) { 239 List resources = new ArrayList (nodes.length); 240 for (int i = 0; i < nodes.length; i++) { 241 resources.add(nodes[i].getLocal()); 242 } 243 return (IResource[]) resources.toArray(new IResource[resources.size()]); 244 } 245 246 252 protected boolean promptForOverwrite(final SyncInfoSet syncSet) { 253 final int[] result = new int[] {Window.CANCEL}; 254 TeamUIPlugin.getStandardDisplay().syncExec(new Runnable () { 255 public void run() { 256 UpdateDialog dialog = new UpdateDialog(getShell(), syncSet); 257 result[0] = dialog.open(); 258 } 259 }); 260 return (result[0] == UpdateDialog.YES); 261 } 262 263 268 protected void makeRemoteLocal(SyncInfo info, IProgressMonitor monitor) throws TeamException { 269 IResourceVariant remote = info.getRemote(); 270 IResource local = info.getLocal(); 271 try { 272 if(remote==null) { 273 if (local.exists()) { 274 local.delete(IResource.KEEP_HISTORY, monitor); 275 } 276 } else { 277 if(remote.isContainer()) { 278 ensureContainerExists(info); 279 } else { 280 monitor.beginTask(null, 200); 281 try { 282 IFile localFile = (IFile)local; 283 if(local.exists()) { 284 localFile.setContents(remote.getStorage(Policy.subMonitorFor(monitor, 100)).getContents(), false , true , Policy.subMonitorFor(monitor, 100)); 285 } else { 286 ensureContainerExists(getParent(info)); 287 localFile.create(remote.getStorage(Policy.subMonitorFor(monitor, 100)).getContents(), false , Policy.subMonitorFor(monitor, 100)); 288 } 289 } finally { 290 monitor.done(); 291 } 292 } 293 } 294 } catch(CoreException e) { 295 IStatus status = new CVSStatus(IStatus.ERROR, CVSStatus.ERROR, CVSUIMessages.UpdateMergeActionProblems_merging_remote_resources_into_workspace_1,e, local); 296 throw new CVSException(status); 297 } 298 } 299 300 private boolean ensureContainerExists(SyncInfo info) throws TeamException { 301 IResource local = info.getLocal(); 302 if (!local.exists()) { 304 if (!ensureContainerExists(getParent(info))) { 305 return false; 306 } 307 } 308 if (isOutOfSync(info)) { 310 if (info instanceof CVSSyncInfo) { 311 CVSSyncInfo cvsInfo = (CVSSyncInfo)info; 312 IStatus status = cvsInfo.makeInSync(); 313 if (status.getSeverity() == IStatus.ERROR) { 314 logError(status); 315 return false; 316 } 317 } 318 } 319 ICVSFolder cvsFolder = CVSWorkspaceRoot.getCVSFolderFor((IContainer)local); 321 if (!cvsFolder.exists()) { 322 cvsFolder.mkdir(); 323 } 324 return true; 325 } 326 327 330 private Map getProjectSyncInfoSetMap() { 331 Map map = new HashMap (); 332 SyncInfoSet all = getSyncInfoSet(); 333 SyncInfo[] infos = all.getSyncInfos(); 334 for (int i = 0; i < infos.length; i++) { 335 SyncInfo info = infos[i]; 336 IProject project = info.getLocal().getProject(); 337 SyncInfoSet set = (SyncInfoSet)map.get(project); 338 if (set == null) { 339 set = new SyncInfoSet(); 340 map.put(project, set); 341 } 342 set.add(info); 343 } 344 return map; 345 } 346 } 347 | Popular Tags |