]>
Commit | Line | Data |
---|---|---|
1b2798f6 EK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2000, 2011 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.jdt.internal.corext.refactoring.changes; | |
12 | ||
13 | import org.eclipse.core.runtime.Assert; | |
14 | import org.eclipse.core.runtime.CoreException; | |
15 | import org.eclipse.core.runtime.IPath; | |
16 | import org.eclipse.core.runtime.IProgressMonitor; | |
17 | import org.eclipse.core.runtime.OperationCanceledException; | |
18 | import org.eclipse.core.runtime.SubProgressMonitor; | |
19 | ||
20 | import org.eclipse.core.resources.IContainer; | |
21 | import org.eclipse.core.resources.IFile; | |
22 | import org.eclipse.core.resources.IFolder; | |
23 | import org.eclipse.core.resources.IProject; | |
24 | import org.eclipse.core.resources.IResource; | |
25 | ||
26 | import org.eclipse.ltk.core.refactoring.Change; | |
27 | import org.eclipse.ltk.core.refactoring.participants.ReorgExecutionLog; | |
28 | import org.eclipse.ltk.core.refactoring.resource.ResourceChange; | |
29 | ||
30 | import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages; | |
31 | import org.eclipse.jdt.internal.corext.refactoring.reorg.INewNameQuery; | |
32 | import org.eclipse.jdt.internal.corext.refactoring.reorg.ReorgUtils; | |
33 | import org.eclipse.jdt.internal.corext.util.Messages; | |
34 | ||
35 | import org.eclipse.jdt.internal.ui.viewsupport.BasicElementLabels; | |
36 | ||
37 | public class CopyResourceChange extends ResourceChange { | |
38 | ||
39 | private final INewNameQuery fNewNameQuery; | |
40 | private final IResource fSource; | |
41 | private final IContainer fTarget; | |
42 | ||
43 | ||
44 | public CopyResourceChange(IResource res, IContainer dest, INewNameQuery newNameQuery) { | |
45 | Assert.isTrue(res instanceof IFile || res instanceof IFolder); | |
46 | Assert.isTrue(dest instanceof IProject || dest instanceof IFolder); | |
47 | ||
48 | fNewNameQuery= newNameQuery; | |
49 | fSource= res; | |
50 | fTarget= dest; | |
51 | ||
52 | // Copy resource change isn't undoable and isn't used | |
53 | // as a redo/undo change right now. | |
54 | setValidationMethod(SAVE_IF_DIRTY); | |
55 | } | |
56 | ||
57 | /* (non-Javadoc) | |
58 | * @see org.eclipse.ltk.core.refactoring.Change#getName() | |
59 | */ | |
60 | @Override | |
61 | public String getName() { | |
62 | return Messages.format(RefactoringCoreMessages.CopyResourceString_copy, new String[] { BasicElementLabels.getPathLabel(getResource().getFullPath(), false), BasicElementLabels.getResourceName(getDestination()) }); | |
63 | } | |
64 | ||
65 | /* non java-doc | |
66 | * @see IChange#perform(ChangeContext, IProgressMonitor) | |
67 | */ | |
68 | @Override | |
69 | public final Change perform(IProgressMonitor pm) throws CoreException, OperationCanceledException { | |
70 | try{ | |
71 | pm.beginTask(getName(), 2); | |
72 | ||
73 | String newName= getNewResourceName(); | |
74 | IResource resource= getResource(); | |
75 | boolean performReorg= deleteIfAlreadyExists(new SubProgressMonitor(pm, 1), newName); | |
76 | if (!performReorg) | |
77 | return null; | |
78 | ||
79 | getResource().copy(getDestinationPath(newName), getReorgFlags(), new SubProgressMonitor(pm, 1)); | |
80 | ||
81 | markAsExecuted(resource); | |
82 | return null; | |
83 | } finally { | |
84 | pm.done(); | |
85 | } | |
86 | } | |
87 | ||
88 | private IPath getDestinationPath(String newName) { | |
89 | return getDestination().getFullPath().append(newName); | |
90 | } | |
91 | ||
92 | /** | |
93 | * returns false if source and destination are the same (in workspace or on disk) | |
94 | * in such case, no action should be performed | |
95 | * @param pm the progress monitor | |
96 | * @param newName the new name | |
97 | * @return returns <code>true</code> if the resource already exists | |
98 | * @throws CoreException thrown when teh resource cannpt be accessed | |
99 | */ | |
100 | private boolean deleteIfAlreadyExists(IProgressMonitor pm, String newName) throws CoreException { | |
101 | pm.beginTask("", 1); //$NON-NLS-1$ | |
102 | IResource current= getDestination().findMember(newName); | |
103 | if (current == null) | |
104 | return true; | |
105 | if (! current.exists()) | |
106 | return true; | |
107 | ||
108 | IResource resource= getResource(); | |
109 | Assert.isNotNull(resource); | |
110 | ||
111 | if (ReorgUtils.areEqualInWorkspaceOrOnDisk(resource, current)) | |
112 | return false; | |
113 | ||
114 | if (current instanceof IFile) | |
115 | ((IFile)current).delete(false, true, new SubProgressMonitor(pm, 1)); | |
116 | else if (current instanceof IFolder) | |
117 | ((IFolder)current).delete(false, true, new SubProgressMonitor(pm, 1)); | |
118 | else | |
119 | Assert.isTrue(false); | |
120 | ||
121 | return true; | |
122 | } | |
123 | ||
124 | ||
125 | private String getNewResourceName() throws OperationCanceledException { | |
126 | if (fNewNameQuery == null) | |
127 | return getResource().getName(); | |
128 | String name= fNewNameQuery.getNewName(); | |
129 | if (name == null) | |
130 | return getResource().getName(); | |
131 | return name; | |
132 | } | |
133 | ||
134 | @Override | |
135 | protected IResource getModifiedResource() { | |
136 | return getResource(); | |
137 | } | |
138 | ||
139 | private IResource getResource() { | |
140 | return fSource; | |
141 | } | |
142 | ||
143 | private IContainer getDestination() { | |
144 | return fTarget; | |
145 | } | |
146 | ||
147 | private int getReorgFlags() { | |
148 | return IResource.KEEP_HISTORY | IResource.SHALLOW; | |
149 | } | |
150 | ||
151 | private void markAsExecuted(IResource resource) { | |
152 | ReorgExecutionLog log= (ReorgExecutionLog)getAdapter(ReorgExecutionLog.class); | |
153 | if (log != null) { | |
154 | log.markAsProcessed(resource); | |
155 | } | |
156 | } | |
157 | } | |
158 |