refactor: simplify collection.toArray()
[egit/eclipse.git] / org.eclipse.egit.core / src / org / eclipse / egit / core / op / DeletePathsOperation.java
blob36c8a202f171bfaabf812ea0153c8e79a11009d5
1 /*******************************************************************************
2 * Copyright (C) 2012, Robin Stocker <robin@nibor.org>
3 * Copyright (C) 2015, Stephan Hackstedt <stephan.hackstedt@googlemail.com>
5 * All rights reserved. This program and the accompanying materials
6 * are made available under the terms of the Eclipse Public License 2.0
7 * which accompanies this distribution, and is available at
8 * https://www.eclipse.org/legal/epl-2.0/
10 * SPDX-License-Identifier: EPL-2.0
11 *******************************************************************************/
12 package org.eclipse.egit.core.op;
14 import java.io.File;
15 import java.io.IOException;
16 import java.text.MessageFormat;
17 import java.util.ArrayList;
18 import java.util.Collection;
19 import java.util.List;
20 import java.util.Map;
22 import org.eclipse.core.resources.IResource;
23 import org.eclipse.core.resources.IWorkspace;
24 import org.eclipse.core.resources.IWorkspaceRunnable;
25 import org.eclipse.core.resources.ResourcesPlugin;
26 import org.eclipse.core.runtime.CoreException;
27 import org.eclipse.core.runtime.IPath;
28 import org.eclipse.core.runtime.IProgressMonitor;
29 import org.eclipse.core.runtime.IStatus;
30 import org.eclipse.core.runtime.SubMonitor;
31 import org.eclipse.core.runtime.jobs.ISchedulingRule;
32 import org.eclipse.egit.core.Activator;
33 import org.eclipse.egit.core.internal.CoreText;
34 import org.eclipse.egit.core.internal.indexdiff.IndexDiffCache;
35 import org.eclipse.egit.core.internal.indexdiff.IndexDiffCacheEntry;
36 import org.eclipse.egit.core.internal.job.RuleUtil;
37 import org.eclipse.egit.core.internal.util.ResourceUtil;
38 import org.eclipse.jgit.lib.Repository;
39 import org.eclipse.jgit.util.FileUtils;
41 /**
42 * Operation to delete a collection of (untracked) paths, even it they are
43 * non-workspace resources.
45 public class DeletePathsOperation implements IEGitOperation {
47 private final Collection<IPath> paths;
49 private final ISchedulingRule schedulingRule;
51 /**
52 * @param paths
53 * the files to delete
55 public DeletePathsOperation(final Collection<IPath> paths) {
56 this.paths = paths;
57 schedulingRule = calculateSchedulingRule();
60 @Override
61 public void execute(IProgressMonitor m) throws CoreException {
62 IWorkspaceRunnable action = new IWorkspaceRunnable() {
63 @Override
64 public void run(IProgressMonitor actMonitor) throws CoreException {
65 deletePaths(actMonitor);
68 ResourcesPlugin.getWorkspace().run(action, getSchedulingRule(),
69 IWorkspace.AVOID_UPDATE, m);
72 @Override
73 public ISchedulingRule getSchedulingRule() {
74 return schedulingRule;
77 private void deletePaths(IProgressMonitor monitor) throws CoreException {
78 SubMonitor progress = SubMonitor.convert(monitor,
79 CoreText.DeleteResourcesOperation_deletingResources,
80 paths.size() + 1);
81 boolean errorOccurred = false;
83 boolean refreshAll = false;
84 List<IPath> refreshCachePaths = new ArrayList<IPath>();
86 for (IPath path : paths) {
87 IResource resource = ResourceUtil.getResourceForLocation(path, false);
88 if (resource != null && resource.exists())
89 resource.delete(false, progress.newChild(1));
90 else {
91 File file = path.toFile();
92 if (file.exists()) {
93 try {
94 FileUtils.delete(file, FileUtils.RECURSIVE);
95 } catch (IOException e) {
96 errorOccurred = true;
97 String message = MessageFormat
98 .format(CoreText.DeleteResourcesOperation_deleteFailed,
99 file.getPath());
100 Activator.logError(message, e);
102 refreshCachePaths.add(path);
103 // Selectively refreshing an IndexDiffCacheEntry only works for files,
104 // so refresh all in case of a directory
105 if (file.isDirectory())
106 refreshAll = true;
108 progress.worked(1);
112 if (!refreshCachePaths.isEmpty())
113 refreshIndexDiffCache(refreshCachePaths, refreshAll);
114 progress.worked(1);
116 if (errorOccurred) {
117 IStatus status = Activator.error(
118 CoreText.DeleteResourcesOperation_deleteFailedSeeLog, null);
119 throw new CoreException(status);
123 private ISchedulingRule calculateSchedulingRule() {
124 return RuleUtil.getRuleForContainers(paths);
127 private void refreshIndexDiffCache(List<IPath> refreshCachePaths, boolean refreshAll) {
128 Map<Repository, Collection<String>> resourcesByRepository = ResourceUtil.splitPathsByRepository(refreshCachePaths);
129 for (Map.Entry<Repository, Collection<String>> entry : resourcesByRepository.entrySet()) {
130 Repository repository = entry.getKey();
131 Collection<String> files = entry.getValue();
133 IndexDiffCache cache = Activator.getDefault().getIndexDiffCache();
134 IndexDiffCacheEntry cacheEntry = cache.getIndexDiffCacheEntry(repository);
135 if (cacheEntry != null)
136 if (refreshAll)
137 cacheEntry.refresh();
138 else
139 cacheEntry.refreshFiles(files);