1 11 12 package org.eclipse.ui.internal.ide.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 import org.eclipse.ui.ide.undo.ResourceDescription; 29 30 39 public abstract class ContainerDescription extends AbstractResourceDescription { 40 41 String name; 42 43 URI location; 44 45 String defaultCharSet; 46 47 AbstractResourceDescription[] members; 48 49 60 61 public static ContainerDescription fromContainer(IContainer container) { 62 IPath fullPath = container.getFullPath(); 63 ContainerDescription firstCreatedParent = null; 64 ContainerDescription currentContainerDescription = null; 65 66 IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); 69 IContainer currentContainer = (IContainer) root.findMember(fullPath); 70 if (currentContainer != null) { 71 return (ContainerDescription) ResourceDescription 72 .fromResource(container); 73 } 74 75 currentContainer = root; 78 for (int i = 0; i < fullPath.segmentCount(); i++) { 79 String currentSegment = fullPath.segment(i); 80 IResource resource = currentContainer.findMember(currentSegment); 81 if (resource != null) { 82 currentContainer = (IContainer) resource; 84 } else { 85 if (i == 0) { 86 firstCreatedParent = new ProjectDescription(root 88 .getProject(currentSegment)); 89 currentContainerDescription = firstCreatedParent; 90 } else { 91 IFolder folderHandle = currentContainer.getFolder(new Path( 92 currentSegment)); 93 ContainerDescription currentFolder = new FolderDescription( 94 folderHandle); 95 currentContainer = folderHandle; 96 if (currentContainerDescription != null) { 97 currentContainerDescription.addMember(currentFolder); 98 } 99 currentContainerDescription = currentFolder; 100 if (firstCreatedParent == null) { 101 firstCreatedParent = currentFolder; 102 } 103 } 104 } 105 } 106 return firstCreatedParent; 107 } 108 109 112 public ContainerDescription() { 113 114 } 115 116 125 public ContainerDescription(IContainer container) { 126 super(container); 127 this.name = container.getName(); 128 if (container.isLinked()) { 129 this.location = container.getLocationURI(); 130 } 131 try { 132 if (container.isAccessible()) { 133 defaultCharSet = container.getDefaultCharset(false); 134 IResource[] resourceMembers = container.members(); 135 members = new AbstractResourceDescription[resourceMembers.length]; 136 for (int i = 0; i < resourceMembers.length; i++) { 137 members[i] = (AbstractResourceDescription) ResourceDescription 138 .fromResource(resourceMembers[i]); 139 } 140 } 141 } catch (CoreException e) { 142 } 147 } 148 149 160 protected void createChildResources(IContainer parentHandle, 161 IProgressMonitor monitor, int ticks) throws CoreException { 162 163 if (members != null && members.length > 0) { 165 for (int i = 0; i < members.length; i++) { 166 members[i].parent = parentHandle; 167 members[i].createResource(new SubProgressMonitor(monitor, ticks 168 / members.length)); 169 } 170 } 171 } 172 173 179 public void recordStateFromHistory(IResource resource, 180 IProgressMonitor monitor) throws CoreException { 181 monitor.beginTask( 182 UndoMessages.FolderDescription_SavingUndoInfoProgress, 100); 183 if (members != null) { 184 for (int i = 0; i < members.length; i++) { 185 if (members[i] instanceof FileDescription) { 186 IPath path = resource.getFullPath().append( 187 ((FileDescription) members[i]).name); 188 IFile fileHandle = resource.getWorkspace().getRoot().getFile( 189 path); 190 members[i].recordStateFromHistory(fileHandle, 191 new SubProgressMonitor(monitor, 100 / members.length)); 192 } else if (members[i] instanceof FolderDescription) { 193 IPath path = resource.getFullPath().append( 194 ((FolderDescription) members[i]).name); 195 IFolder folderHandle = resource.getWorkspace().getRoot() 196 .getFolder(path); 197 members[i].recordStateFromHistory(folderHandle, 198 new SubProgressMonitor(monitor, 100 / members.length)); 199 } 200 } 201 } 202 monitor.done(); 203 } 204 205 210 public String getName() { 211 return name; 212 } 213 214 220 public ContainerDescription getFirstLeafFolder() { 221 if (members == null || members.length == 0) { 223 return this; 224 } 225 for (int i = 0; i < members.length; i++) { 227 if (members[i] instanceof ContainerDescription) { 228 return ((ContainerDescription) members[i]).getFirstLeafFolder(); 229 } 230 } 231 return this; 233 } 234 235 243 public void addMember(AbstractResourceDescription member) { 244 if (members == null) { 245 members = new AbstractResourceDescription[] { member }; 246 } else { 247 AbstractResourceDescription[] expandedMembers = new AbstractResourceDescription[members.length + 1]; 248 System.arraycopy(members, 0, expandedMembers, 0, members.length); 249 expandedMembers[members.length] = member; 250 members = expandedMembers; 251 } 252 } 253 254 259 protected void restoreResourceAttributes(IResource resource) 260 throws CoreException { 261 super.restoreResourceAttributes(resource); 262 Assert.isLegal(resource instanceof IContainer); 263 IContainer container = (IContainer) resource; 264 if (defaultCharSet != null) { 265 container.setDefaultCharset(defaultCharSet, null); 266 } 267 } 268 269 275 public void setLocation(URI location) { 276 this.location = location; 277 } 278 279 284 public boolean verifyExistence(boolean checkMembers) { 285 boolean existence = super.verifyExistence(checkMembers); 286 if (existence) { 287 if (checkMembers) { 288 if (members != null && members.length > 0) { 290 for (int i = 0; i < members.length; i++) { 291 if (!members[i].verifyExistence(checkMembers)) { 292 return false; 293 } 294 } 295 } 296 } 297 return true; 298 } 299 return false; 300 } 301 } 302 | Popular Tags |