1 11 package org.eclipse.ui.wizards.datatransfer; 12 13 import java.io.File ; 14 import java.io.IOException ; 15 import java.util.*; 16 17 import org.eclipse.core.resources.*; 18 import org.eclipse.core.runtime.*; 19 import org.eclipse.jface.operation.IRunnableWithProgress; 20 import org.eclipse.jface.operation.ModalContext; 21 import org.eclipse.ui.PlatformUI; 22 import org.eclipse.ui.dialogs.IOverwriteQuery; 23 24 27 28 class FileSystemExportOperation implements IRunnableWithProgress { 29 private IPath path; 30 private IProgressMonitor monitor; 31 private FileSystemExporter exporter = new FileSystemExporter(); 32 private List resourcesToExport; 33 private IOverwriteQuery overwriteCallback; 34 private IResource resource; 35 private List errorTable = new ArrayList(1); 36 37 private static final int OVERWRITE_NOT_SET = 0; 39 private static final int OVERWRITE_NONE = 1; 40 private static final int OVERWRITE_ALL = 2; 41 private int overwriteState = OVERWRITE_NOT_SET; 42 43 private boolean createLeadupStructure = true; 44 private boolean createContainerDirectories = true; 45 49 public FileSystemExportOperation( 50 List resources, 51 String destinationPath, 52 IOverwriteQuery overwriteImplementor) { 53 super(); 54 55 Iterator elementsEnum = resources.iterator(); 57 while (elementsEnum.hasNext()) { 58 IResource currentResource = (IResource) elementsEnum.next(); 59 if (isDescendent(resources, currentResource)) 60 elementsEnum.remove(); } 62 63 resourcesToExport = resources; 64 path = new Path(destinationPath); 65 overwriteCallback = overwriteImplementor; 66 } 67 71 public FileSystemExportOperation( 72 IResource res, 73 String destinationPath, 74 IOverwriteQuery overwriteImplementor) { 75 super(); 76 resource = res; 77 path = new Path(destinationPath); 78 overwriteCallback = overwriteImplementor; 79 } 80 85 public FileSystemExportOperation( 86 IResource res, 87 List resources, 88 String destinationPath, 89 IOverwriteQuery overwriteImplementor) { 90 this(res, destinationPath, overwriteImplementor); 91 resourcesToExport = resources; 92 } 93 96 protected void addError(String message, Throwable e) { 97 errorTable.add( 98 new Status(IStatus.ERROR, PlatformUI.PLUGIN_ID, 0, message, e)); 99 } 100 107 protected int countChildrenOf(IResource parentResource) throws CoreException { 108 if (parentResource.getType() == IResource.FILE) 109 return 1; 110 111 int count = 0; 112 if (parentResource.isAccessible()) { 113 IResource[] children = ((IContainer) parentResource).members(); 114 for (int i = 0; i < children.length; i++) 115 count += countChildrenOf(children[i]); 116 } 117 118 return count; 119 } 120 126 protected int countSelectedResources() throws CoreException { 127 int result = 0; 128 Iterator resources = resourcesToExport.iterator(); 129 130 while (resources.hasNext()) 131 result += countChildrenOf((IResource) resources.next()); 132 133 return result; 134 } 135 141 protected void createLeadupDirectoriesFor(IResource childResource) { 142 IPath resourcePath = childResource.getFullPath().removeLastSegments(1); 143 144 for (int i = 0; i < resourcePath.segmentCount(); i++) { 145 path = path.append(resourcePath.segment(i)); 146 exporter.createFolder(path); 147 } 148 } 149 152 protected void exportAllResources() throws InterruptedException { 153 if (resource.getType() == IResource.FILE) 154 exportFile((IFile) resource, path); 155 else { 156 try { 157 exportChildren(((IContainer) resource).members(), path); 158 } catch (CoreException e) { 159 errorTable.add(e); 163 } 164 } 165 } 166 172 protected void exportChildren(IResource[] children, IPath currentPath) 173 throws InterruptedException { 174 for (int i = 0; i < children.length; i++) { 175 IResource child = children[i]; 176 if (!child.isAccessible()) 177 continue; 178 179 if (child.getType() == IResource.FILE) 180 exportFile((IFile) child, currentPath); 181 else { 182 IPath destination = currentPath.append(child.getName()); 183 exporter.createFolder(destination); 184 try { 185 exportChildren(((IContainer) child).members(), destination); 186 } catch (CoreException e) { 187 errorTable.add(e.getStatus()); 193 } 194 } 195 } 196 } 197 203 protected void exportFile(IFile file, IPath location) 204 throws InterruptedException { 205 IPath fullPath = location.append(file.getName()); 206 monitor.subTask(file.getFullPath().toString()); 207 String properPathString = fullPath.toOSString(); 208 File targetFile = new File (properPathString); 209 210 if (targetFile.exists()) { 211 if (!targetFile.canWrite()) { 212 errorTable.add(new Status(IStatus.ERROR, PlatformUI.PLUGIN_ID, 0, DataTransferMessages.format("DataTransfer.cannotOverwrite", new Object [] { targetFile.getAbsolutePath()}), null)); 214 monitor.worked(1); 215 return; 216 } 217 218 if(overwriteState == OVERWRITE_NONE) 219 return; 220 221 if (overwriteState != OVERWRITE_ALL) { 222 String overwriteAnswer = 223 overwriteCallback.queryOverwrite(properPathString); 224 225 if (overwriteAnswer.equals(IOverwriteQuery.CANCEL)) 226 throw new InterruptedException (); 227 228 if (overwriteAnswer.equals(IOverwriteQuery.NO)) { 229 monitor.worked(1); 230 return; 231 } 232 233 if (overwriteAnswer.equals(IOverwriteQuery.NO_ALL)) { 234 monitor.worked(1); 235 overwriteState = OVERWRITE_NONE; 236 return; 237 } 238 239 if (overwriteAnswer.equals(IOverwriteQuery.ALL)) 240 overwriteState = OVERWRITE_ALL; 241 } 242 } 243 244 try { 245 exporter.write(file, fullPath); 246 } catch (IOException e) { 247 errorTable.add(new Status(IStatus.ERROR, PlatformUI.PLUGIN_ID, 0, DataTransferMessages.format("DataTransfer.errorExporting", new Object [] { fullPath, e.getMessage()}), e)); 249 } catch (CoreException e) { 250 errorTable.add(new Status(IStatus.ERROR, PlatformUI.PLUGIN_ID, 0, DataTransferMessages.format("DataTransfer.errorExporting", new Object [] { fullPath, e.getMessage()}), e)); 252 } 253 254 monitor.worked(1); 255 ModalContext.checkCanceled(monitor); 256 } 257 261 protected void exportSpecifiedResources() throws InterruptedException { 262 Iterator resources = resourcesToExport.iterator(); 263 IPath initPath = (IPath) path.clone(); 264 265 while (resources.hasNext()) { 266 IResource currentResource = (IResource) resources.next(); 267 if (!currentResource.isAccessible()) 268 continue; 269 270 path = initPath; 271 272 if (resource == null) { 273 if (createLeadupStructure) 277 createLeadupDirectoriesFor(currentResource); 278 279 } else { 280 IPath containersToCreate = 283 currentResource 284 .getFullPath() 285 .removeFirstSegments( 286 resource.getFullPath().segmentCount()) 287 .removeLastSegments(1); 288 289 for (int i = 0; i < containersToCreate.segmentCount(); i++) { 290 path = path.append(containersToCreate.segment(i)); 291 exporter.createFolder(path); 292 } 293 } 294 295 if (currentResource.getType() == IResource.FILE) 296 exportFile((IFile) currentResource, path); 297 else { 298 if (createContainerDirectories) { 299 path = path.append(currentResource.getName()); 300 exporter.createFolder(path); 301 } 302 303 try { 304 exportChildren( 305 ((IContainer) currentResource).members(), 306 path); 307 } catch (CoreException e) { 308 errorTable.add(e.getStatus()); 311 } 312 } 313 } 314 } 315 323 public IStatus getStatus() { 324 IStatus[] errors = new IStatus[errorTable.size()]; 325 errorTable.toArray(errors); 326 return new MultiStatus(PlatformUI.PLUGIN_ID, IStatus.OK, errors, DataTransferMessages.getString("FileSystemExportOperation.problemsExporting"), null); 328 } 329 337 protected boolean isDescendent(List resources, IResource child) { 338 if (child.getType() == IResource.PROJECT) 339 return false; 340 341 IResource parent = child.getParent(); 342 if (resources.contains(parent)) 343 return true; 344 345 return isDescendent(resources, parent); 346 } 347 351 public void run(IProgressMonitor progressMonitor) throws InterruptedException { 352 this.monitor = progressMonitor; 353 354 if (resource != null) { 355 if (createLeadupStructure) 356 createLeadupDirectoriesFor(resource); 357 358 if (createContainerDirectories 359 && resource.getType() != IResource.FILE) { 360 path = path.append(resource.getName()); 362 exporter.createFolder(path); 363 } 364 } 365 366 try { 367 int totalWork = IProgressMonitor.UNKNOWN; 368 try { 369 if (resourcesToExport == null) 370 totalWork = countChildrenOf(resource); 371 else 372 totalWork = countSelectedResources(); 373 } catch (CoreException e) { 374 errorTable.add(e.getStatus()); 376 } 377 monitor.beginTask(DataTransferMessages.getString("DataTransfer.exportingTitle"), totalWork); if (resourcesToExport == null) { 379 exportAllResources(); 380 } else { 381 exportSpecifiedResources(); 382 } 383 } finally { 384 monitor.done(); 385 } 386 } 387 393 public void setCreateContainerDirectories(boolean value) { 394 createContainerDirectories = value; 395 } 396 402 public void setCreateLeadupStructure(boolean value) { 403 createLeadupStructure = value; 404 } 405 412 public void setOverwriteFiles(boolean value) { 413 if(value) 414 overwriteState = OVERWRITE_ALL; 415 } 416 } 417 | Popular Tags |