|
Removed
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 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 |
|
| 12 |
package org.eclipse.jdt.internal.corext.refactoring.changes.undo; |
| 13 |
|
| 14 |
import java.net.URI; |
| 15 |
|
| 16 |
import org.eclipse.core.resources.IContainer; |
| 17 |
import org.eclipse.core.resources.IFile; |
| 18 |
import org.eclipse.core.resources.IFolder; |
| 19 |
import org.eclipse.core.resources.IResource; |
| 20 |
import org.eclipse.core.resources.IWorkspaceRoot; |
| 21 |
import org.eclipse.core.resources.ResourcesPlugin; |
| 22 |
import org.eclipse.core.runtime.Assert; |
| 23 |
import org.eclipse.core.runtime.CoreException; |
| 24 |
import org.eclipse.core.runtime.IPath; |
| 25 |
import org.eclipse.core.runtime.IProgressMonitor; |
| 26 |
import org.eclipse.core.runtime.Path; |
| 27 |
import org.eclipse.core.runtime.SubProgressMonitor; |
| 28 |
|
| 29 |
/** |
| 30 |
* ContainerDescription is a lightweight description that describes a container |
| 31 |
* to be created. |
| 32 |
* |
| 33 |
* This class is not intended to be instantiated or used by clients. |
| 34 |
* |
| 35 |
* @since 3.3 |
| 36 |
* |
| 37 |
*/ |
| 38 |
public abstract class ContainerDescription extends ResourceDescription { |
| 39 |
|
| 40 |
String name; |
| 41 |
|
| 42 |
URI location; |
| 43 |
|
| 44 |
String defaultCharSet; |
| 45 |
|
| 46 |
ResourceDescription[] members; |
| 47 |
|
| 48 |
/** |
| 49 |
* Create a container description from the specified container handle that |
| 50 |
* can be used to create the container. The returned ContainerDescription |
| 51 |
* should represent any non-existing parents in addition to the specified |
| 52 |
* container. |
| 53 |
* |
| 54 |
* @param container |
| 55 |
* the handle of the container to be described |
| 56 |
* @return a container description describing the container and any |
| 57 |
* non-existing parents. |
| 58 |
*/ |
| 59 |
|
| 60 |
public static ContainerDescription fromContainer(IContainer container) { |
| 61 |
IPath fullPath = container.getFullPath(); |
| 62 |
ContainerDescription firstCreatedParent = null; |
| 63 |
ContainerDescription currentContainerDescription = null; |
| 64 |
|
| 65 |
// Does the container exist already? If so, then the parent exists and |
| 66 |
// we use the normal creation constructor. |
| 67 |
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); |
| 68 |
IContainer currentContainer = (IContainer) root.findMember(fullPath); |
| 69 |
if (currentContainer != null) { |
| 70 |
return (ContainerDescription) ResourceDescription |
| 71 |
.fromResource(container); |
| 72 |
} |
| 73 |
|
| 74 |
// Create container descriptions for any uncreated parents in the given |
| 75 |
// path. |
| 76 |
currentContainer = root; |
| 77 |
for (int i = 0; i < fullPath.segmentCount(); i++) { |
| 78 |
String currentSegment = fullPath.segment(i); |
| 79 |
IResource resource = currentContainer.findMember(currentSegment); |
| 80 |
if (resource != null) { |
| 81 |
// parent already exists, no need to create a description for it |
| 82 |
currentContainer = (IContainer) resource; |
| 83 |
} else { |
| 84 |
if (i == 0) { |
| 85 |
// parent does not exist and it is a project |
| 86 |
firstCreatedParent = new ProjectDescription(root |
| 87 |
.getProject(currentSegment)); |
| 88 |
currentContainerDescription = firstCreatedParent; |
| 89 |
} else { |
| 90 |
IFolder folderHandle = currentContainer.getFolder(new Path( |
| 91 |
currentSegment)); |
| 92 |
ContainerDescription currentFolder = new FolderDescription( |
| 93 |
folderHandle); |
| 94 |
currentContainer = folderHandle; |
| 95 |
if (currentContainerDescription != null) { |
| 96 |
currentContainerDescription.addMember(currentFolder); |
| 97 |
} |
| 98 |
currentContainerDescription = currentFolder; |
| 99 |
if (firstCreatedParent == null) { |
| 100 |
firstCreatedParent = currentFolder; |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
return firstCreatedParent; |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Create a ContainerDescription with no state. |
| 110 |
*/ |
| 111 |
public ContainerDescription() { |
| 112 |
|
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Create a ContainerDescription from the specified container handle. |
| 117 |
* Typically used when the container handle represents a resource that |
| 118 |
* actually exists, although it will not fail if the resource is |
| 119 |
* non-existent. |
| 120 |
* |
| 121 |
* @param container |
| 122 |
* the container to be described |
| 123 |
*/ |
| 124 |
public ContainerDescription(IContainer container) { |
| 125 |
super(container); |
| 126 |
this.name = container.getName(); |
| 127 |
if (container.isLinked()) { |
| 128 |
this.location = container.getLocationURI(); |
| 129 |
} |
| 130 |
try { |
| 131 |
if (container.isAccessible()) { |
| 132 |
defaultCharSet = container.getDefaultCharset(false); |
| 133 |
IResource[] resourceMembers = container.members(); |
| 134 |
members = new ResourceDescription[resourceMembers.length]; |
| 135 |
for (int i = 0; i < resourceMembers.length; i++) { |
| 136 |
members[i] = ResourceDescription |
| 137 |
.fromResource(resourceMembers[i]); |
| 138 |
} |
| 139 |
} |
| 140 |
} catch (CoreException e) { |
| 141 |
// Eat this exception because it only occurs when the resource |
| 142 |
// does not exist and we have already checked this. |
| 143 |
// We do not want to throw exceptions on the simple constructor, as |
| 144 |
// no one has actually tried to do anything yet. |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Create any child resources known by this container description. |
| 150 |
* |
| 151 |
* @param parentHandle |
| 152 |
* the handle of the created parent |
| 153 |
* @param monitor |
| 154 |
* the progress monitor to be used |
| 155 |
* @param ticks |
| 156 |
* the number of ticks allocated for creating children |
| 157 |
* @throws CoreException |
| 158 |
*/ |
| 159 |
protected void createChildResources(IContainer parentHandle, |
| 160 |
IProgressMonitor monitor, int ticks) throws CoreException { |
| 161 |
|
| 162 |
// restore any children |
| 163 |
if (members != null && members.length > 0) { |
| 164 |
for (int i = 0; i < members.length; i++) { |
| 165 |
members[i].parent = parentHandle; |
| 166 |
members[i].createResource(new SubProgressMonitor(monitor, ticks |
| 167 |
/ members.length)); |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
/* |
| 173 |
* (non-Javadoc) |
| 174 |
* |
| 175 |
* @see org.eclipse.ui.internal.ide.undo.ResourceDescription#recordStateFromHistory(org.eclipse.core.resources.IResource, |
| 176 |
* org.eclipse.core.runtime.IProgressMonitor) |
| 177 |
*/ |
| 178 |
public void recordStateFromHistory(IResource resource, |
| 179 |
IProgressMonitor monitor) throws CoreException { |
| 180 |
monitor.beginTask( |
| 181 |
UndoMessages.FolderDescription_SavingUndoInfoProgress, 100); |
| 182 |
for (int i = 0; i < members.length; i++) { |
| 183 |
if (members[i] instanceof FileDescription) { |
| 184 |
IPath path = resource.getFullPath().append( |
| 185 |
((FileDescription) members[i]).name); |
| 186 |
IFile fileHandle = resource.getWorkspace().getRoot().getFile( |
| 187 |
path); |
| 188 |
members[i].recordStateFromHistory(fileHandle, |
| 189 |
new SubProgressMonitor(monitor, 100 / members.length)); |
| 190 |
} else if (members[i] instanceof FolderDescription) { |
| 191 |
IPath path = resource.getFullPath().append( |
| 192 |
((FolderDescription) members[i]).name); |
| 193 |
IFolder folderHandle = resource.getWorkspace().getRoot() |
| 194 |
.getFolder(path); |
| 195 |
members[i].recordStateFromHistory(folderHandle, |
| 196 |
new SubProgressMonitor(monitor, 100 / members.length)); |
| 197 |
} |
| 198 |
} |
| 199 |
monitor.done(); |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* Return the name of the container described by this ContainerDescription. |
| 204 |
* |
| 205 |
* @return the name of the container. |
| 206 |
*/ |
| 207 |
public String getName() { |
| 208 |
return name; |
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* Return the first folder found that has no child folders. |
| 213 |
* |
| 214 |
* @return the container description for the first child in the receiver |
| 215 |
* that is a leaf, or this container if there are no children. |
| 216 |
*/ |
| 217 |
public ContainerDescription getFirstLeafFolder() { |
| 218 |
// If there are no members, this is a leaf |
| 219 |
if (members == null || members.length == 0) { |
| 220 |
return this; |
| 221 |
} |
| 222 |
// Traverse the members and find the first potential leaf |
| 223 |
for (int i = 0; i < members.length; i++) { |
| 224 |
if (members[i] instanceof ContainerDescription) { |
| 225 |
return ((ContainerDescription) members[i]).getFirstLeafFolder(); |
| 226 |
} |
| 227 |
} |
| 228 |
// No child folders were found, this is a leaf |
| 229 |
return this; |
| 230 |
} |
| 231 |
|
| 232 |
/** |
| 233 |
* Add the specified resource description as a member of this resource |
| 234 |
* description |
| 235 |
* |
| 236 |
* @param member |
| 237 |
* the resource description considered a member of this |
| 238 |
* container. |
| 239 |
*/ |
| 240 |
public void addMember(ResourceDescription member) { |
| 241 |
if (members == null) { |
| 242 |
members = new ResourceDescription[] { member }; |
| 243 |
} else { |
| 244 |
ResourceDescription[] expandedMembers = new ResourceDescription[members.length + 1]; |
| 245 |
System.arraycopy(members, 0, expandedMembers, 0, members.length); |
| 246 |
expandedMembers[members.length] = member; |
| 247 |
members = expandedMembers; |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
/* |
| 252 |
* (non-Javadoc) |
| 253 |
* |
| 254 |
* @see org.eclipse.ui.internal.ide.undo.ResourceDescription#restoreResourceAttributes(org.eclipse.core.resources.IResource) |
| 255 |
*/ |
| 256 |
protected void restoreResourceAttributes(IResource resource) |
| 257 |
throws CoreException { |
| 258 |
super.restoreResourceAttributes(resource); |
| 259 |
Assert.isLegal(resource instanceof IContainer); |
| 260 |
IContainer container = (IContainer) resource; |
| 261 |
if (defaultCharSet != null) { |
| 262 |
container.setDefaultCharset(defaultCharSet, null); |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
/** |
| 267 |
* Set the location to which this container is linked. |
| 268 |
* |
| 269 |
* @param location |
| 270 |
* the location URI, or <code>null</code> if there is no link |
| 271 |
*/ |
| 272 |
public void setLocation(URI location) { |
| 273 |
this.location = location; |
| 274 |
} |
| 275 |
|
| 276 |
/* |
| 277 |
* (non-Javadoc) |
| 278 |
* |
| 279 |
* @see org.eclipse.ui.internal.ide.undo.ResourceDescription#verifyExistence(boolean) |
| 280 |
*/ |
| 281 |
public boolean verifyExistence(boolean checkMembers) { |
| 282 |
boolean existence = super.verifyExistence(checkMembers); |
| 283 |
if (existence) { |
| 284 |
if (checkMembers) { |
| 285 |
// restore any children |
| 286 |
if (members != null && members.length > 0) { |
| 287 |
for (int i = 0; i < members.length; i++) { |
| 288 |
if (!members[i].verifyExistence(checkMembers)) { |
| 289 |
return false; |
| 290 |
} |
| 291 |
} |
| 292 |
} |
| 293 |
} |
| 294 |
return true; |
| 295 |
} |
| 296 |
return false; |
| 297 |
} |
| 298 |
} |