|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2026 Felix Schmid |
| 3 | + * |
| 4 | + * This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Public License 2.0 |
| 6 | + * which accompanies this distribution, and is available at |
| 7 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: EPL-2.0 |
| 10 | + * |
| 11 | + * Contributors: |
| 12 | + * Felix Schmid - initial API and implementation and/or initial documentation |
| 13 | + *******************************************************************************/ |
| 14 | +package org.eclipse.ltk.core.refactoring.resource; |
| 15 | + |
| 16 | +import java.net.URI; |
| 17 | +import java.text.MessageFormat; |
| 18 | + |
| 19 | +import org.eclipse.core.runtime.Assert; |
| 20 | +import org.eclipse.core.runtime.CoreException; |
| 21 | +import org.eclipse.core.runtime.IPath; |
| 22 | +import org.eclipse.core.runtime.IProgressMonitor; |
| 23 | +import org.eclipse.core.runtime.OperationCanceledException; |
| 24 | +import org.eclipse.core.runtime.SubMonitor; |
| 25 | + |
| 26 | +import org.eclipse.core.resources.IContainer; |
| 27 | +import org.eclipse.core.resources.IFile; |
| 28 | +import org.eclipse.core.resources.IFolder; |
| 29 | +import org.eclipse.core.resources.IResource; |
| 30 | + |
| 31 | +import org.eclipse.ltk.core.refactoring.Change; |
| 32 | +import org.eclipse.ltk.core.refactoring.ChangeDescriptor; |
| 33 | +import org.eclipse.ltk.core.refactoring.CompositeChange; |
| 34 | +import org.eclipse.ltk.core.refactoring.RefactoringStatus; |
| 35 | +import org.eclipse.ltk.core.refactoring.participants.ReorgExecutionLog; |
| 36 | +import org.eclipse.ltk.internal.core.refactoring.BasicElementLabels; |
| 37 | +import org.eclipse.ltk.internal.core.refactoring.RefactoringCoreMessages; |
| 38 | + |
| 39 | +/** |
| 40 | + * {@link Change} that copies a resource. |
| 41 | + * |
| 42 | + * @since 3.16 |
| 43 | + */ |
| 44 | +public class CopyResourceChange extends ResourceChange { |
| 45 | + |
| 46 | + private ChangeDescriptor descriptor; |
| 47 | + |
| 48 | + private final IResource origin; |
| 49 | + |
| 50 | + private final ReorgExecutionLog log; |
| 51 | + |
| 52 | + private final IContainer destination; |
| 53 | + |
| 54 | + public CopyResourceChange(final IResource origin, final ReorgExecutionLog log, final IContainer destination) { |
| 55 | + Assert.isTrue(origin instanceof IFile || origin instanceof IFolder); |
| 56 | + this.origin= origin; |
| 57 | + this.log= log; |
| 58 | + this.destination= destination; |
| 59 | + setValidationMethod(VALIDATE_NOT_DIRTY); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public String getName() { |
| 64 | + return MessageFormat.format(RefactoringCoreMessages.CopyResourceChange_name, |
| 65 | + BasicElementLabels.getPathLabel(origin.getFullPath(), false), |
| 66 | + BasicElementLabels.getResourceName(destination)); |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public final Change perform(final IProgressMonitor pm) throws CoreException, OperationCanceledException { |
| 71 | + CompositeChange undoChanges= new CompositeChange( |
| 72 | + RefactoringCoreMessages.CopyResourceChange_undo_composite_name); |
| 73 | + copyMerge(origin, destination, undoChanges, pm); |
| 74 | + return undoChanges; |
| 75 | + } |
| 76 | + |
| 77 | + private void copyMerge(IResource res, IContainer dest, CompositeChange undoChanges, |
| 78 | + IProgressMonitor pm) throws CoreException { |
| 79 | + SubMonitor monitor= SubMonitor.convert(pm, getName(), 1); |
| 80 | + |
| 81 | + if (res instanceof IFile) { // copy and overwrite normal files |
| 82 | + performResourceCopy(res, dest, undoChanges, monitor.split(1)); |
| 83 | + } else if (res instanceof IFolder folder) { |
| 84 | + String newName= log.getNewName(res); |
| 85 | + if (newName == null) { |
| 86 | + newName= res.getName(); |
| 87 | + } |
| 88 | + final IResource resAtDest= dest.findMember(newName); |
| 89 | + |
| 90 | + if (!resourceExists(resAtDest) || !(resAtDest instanceof IFolder folderAtDest) || |
| 91 | + !homogeneousResources(folder, resAtDest)) { // copy non-existing or non-homogeneous folders |
| 92 | + performResourceCopy(res, dest, undoChanges, monitor.split(1)); |
| 93 | + } else { // merge copy members of existing folders |
| 94 | + monitor.setWorkRemaining(folder.members().length); |
| 95 | + for (IResource member : folder.members()) { |
| 96 | + copyMerge(member, folderAtDest, undoChanges, monitor.split(1)); |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + private void performResourceCopy(IResource res, IContainer dest, CompositeChange undoChanges, |
| 103 | + IProgressMonitor pm) throws CoreException { |
| 104 | + SubMonitor subMonitor= SubMonitor.convert(pm, getName(), 2); |
| 105 | + String newName= log.getNewName(res); |
| 106 | + if (newName == null) { |
| 107 | + newName= res.getName(); |
| 108 | + } |
| 109 | + final IResource resAtDest= dest.findMember(newName); |
| 110 | + |
| 111 | + if (resourceExists(resAtDest) && areEqualInWorkspaceOrOnDisk(res, resAtDest)) { |
| 112 | + log.markAsProcessed(res); |
| 113 | + return; |
| 114 | + } |
| 115 | + |
| 116 | + final Change undoOverwrite= deleteIfAlreadyExists(resAtDest, subMonitor.newChild(1)); |
| 117 | + final IPath copyTo= dest.getFullPath().append(newName); |
| 118 | + res.copy(copyTo, getReorgFlags(), subMonitor.newChild(1)); |
| 119 | + log.markAsProcessed(res); |
| 120 | + |
| 121 | + undoChanges.add(new DeleteResourceChange(copyTo, false)); |
| 122 | + if (undoOverwrite != null) { |
| 123 | + undoChanges.add(undoOverwrite); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + protected IResource getModifiedResource() { |
| 129 | + return origin; |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * deletes a resource if it exists and returns a <code>Change</code> to undo the deletion |
| 134 | + * |
| 135 | + * @param resource the resource to delete |
| 136 | + * @param pm the progress monitor |
| 137 | + * @return returns an undo <code>Change</code> or <code>null</code> if nothing was deleted |
| 138 | + * @throws CoreException thrown when the resource cannot be accessed |
| 139 | + */ |
| 140 | + private Change deleteIfAlreadyExists(final IResource resource, final IProgressMonitor pm) throws CoreException { |
| 141 | + if (!resourceExists(resource)) { |
| 142 | + pm.done(); |
| 143 | + return null; |
| 144 | + } |
| 145 | + SubMonitor subMonitor= SubMonitor.convert(pm, |
| 146 | + RefactoringCoreMessages.MoveResourceChange_progress_delete_destination, 3); |
| 147 | + DeleteResourceChange deleteChange= new DeleteResourceChange(resource.getFullPath(), true); |
| 148 | + deleteChange.initializeValidationData(subMonitor.newChild(1)); |
| 149 | + RefactoringStatus deleteStatus= deleteChange.isValid(subMonitor.newChild(1)); |
| 150 | + if (!deleteStatus.hasFatalError()) { |
| 151 | + return deleteChange.perform(subMonitor.newChild(1)); |
| 152 | + } |
| 153 | + return null; |
| 154 | + } |
| 155 | + |
| 156 | + private static boolean areEqualInWorkspaceOrOnDisk(final IResource r1, final IResource r2) { |
| 157 | + if (r1 == null || r2 == null) { |
| 158 | + return false; |
| 159 | + } |
| 160 | + if (r1.equals(r2)) { |
| 161 | + return true; |
| 162 | + } |
| 163 | + final URI r1Location= r1.getLocationURI(); |
| 164 | + final URI r2Location= r2.getLocationURI(); |
| 165 | + if (r1Location == null || r2Location == null) { |
| 166 | + return false; |
| 167 | + } |
| 168 | + return r1Location.equals(r2Location); |
| 169 | + } |
| 170 | + |
| 171 | + private static boolean resourceExists(final IResource resource) { |
| 172 | + return resource != null && resource.exists(); |
| 173 | + } |
| 174 | + |
| 175 | + private static boolean homogeneousResources(IResource res1, IResource res2) { |
| 176 | + boolean res1Linked= res1.isLinked(); |
| 177 | + boolean res2Linked= res2.isLinked(); |
| 178 | + return res1Linked && res2Linked || !res1Linked && !res2Linked; |
| 179 | + } |
| 180 | + |
| 181 | + private static int getReorgFlags() { |
| 182 | + return IResource.KEEP_HISTORY | IResource.SHALLOW; |
| 183 | + } |
| 184 | + |
| 185 | + @Override |
| 186 | + public ChangeDescriptor getDescriptor() { |
| 187 | + return descriptor; |
| 188 | + } |
| 189 | + |
| 190 | + public void setDescriptor(ChangeDescriptor descriptor) { |
| 191 | + this.descriptor= descriptor; |
| 192 | + } |
| 193 | +} |
0 commit comments