1 11 package org.eclipse.ui.internal.wizards.datatransfer; 12 13 import java.io.File ; 14 import java.io.IOException ; 15 import java.util.ArrayList ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 19 import org.eclipse.core.resources.IContainer; 20 import org.eclipse.core.resources.IFile; 21 import org.eclipse.core.resources.IResource; 22 import org.eclipse.core.runtime.CoreException; 23 import org.eclipse.core.runtime.IPath; 24 import org.eclipse.core.runtime.IProgressMonitor; 25 import org.eclipse.core.runtime.IStatus; 26 import org.eclipse.core.runtime.MultiStatus; 27 import org.eclipse.core.runtime.Path; 28 import org.eclipse.core.runtime.Status; 29 import org.eclipse.jface.operation.IRunnableWithProgress; 30 import org.eclipse.jface.operation.ModalContext; 31 import org.eclipse.osgi.util.NLS; 32 import org.eclipse.ui.PlatformUI; 33 import org.eclipse.ui.dialogs.IOverwriteQuery; 34 35 36 39 public class FileSystemExportOperation implements IRunnableWithProgress { 40 private IPath path; 41 42 private IProgressMonitor monitor; 43 44 private FileSystemExporter exporter = new FileSystemExporter(); 45 46 private List resourcesToExport; 47 48 private IOverwriteQuery overwriteCallback; 49 50 private IResource resource; 51 52 private List errorTable = new ArrayList (1); 53 54 private static final int OVERWRITE_NOT_SET = 0; 56 57 private static final int OVERWRITE_NONE = 1; 58 59 private static final int OVERWRITE_ALL = 2; 60 61 private int overwriteState = OVERWRITE_NOT_SET; 62 63 private boolean createLeadupStructure = true; 64 65 private boolean createContainerDirectories = true; 66 67 71 public FileSystemExportOperation(IResource res, String destinationPath, 72 IOverwriteQuery overwriteImplementor) { 73 super(); 74 resource = res; 75 path = new Path(destinationPath); 76 overwriteCallback = overwriteImplementor; 77 } 78 79 84 public FileSystemExportOperation(IResource res, List resources, 85 String destinationPath, IOverwriteQuery overwriteImplementor) { 86 this(res, destinationPath, overwriteImplementor); 87 resourcesToExport = resources; 88 } 89 90 97 protected int countChildrenOf(IResource parentResource) 98 throws CoreException { 99 if (parentResource.getType() == IResource.FILE) { 100 return 1; 101 } 102 103 int count = 0; 104 if (parentResource.isAccessible()) { 105 IResource[] children = ((IContainer) parentResource).members(); 106 for (int i = 0; i < children.length; i++) { 107 count += countChildrenOf(children[i]); 108 } 109 } 110 111 return count; 112 } 113 114 120 protected int countSelectedResources() throws CoreException { 121 int result = 0; 122 Iterator resources = resourcesToExport.iterator(); 123 124 while (resources.hasNext()) { 125 result += countChildrenOf((IResource) resources.next()); 126 } 127 128 return result; 129 } 130 131 137 protected void createLeadupDirectoriesFor(IResource childResource) { 138 IPath resourcePath = childResource.getFullPath().removeLastSegments(1); 139 140 for (int i = 0; i < resourcePath.segmentCount(); i++) { 141 path = path.append(resourcePath.segment(i)); 142 exporter.createFolder(path); 143 } 144 } 145 146 149 protected void exportAllResources() throws InterruptedException { 150 if (resource.getType() == IResource.FILE) { 151 exportFile((IFile) resource, path); 152 } else { 153 try { 154 exportChildren(((IContainer) resource).members(), path); 155 } catch (CoreException e) { 156 errorTable.add(e.getStatus()); 160 } 161 } 162 } 163 164 170 protected void exportChildren(IResource[] children, IPath currentPath) 171 throws InterruptedException { 172 for (int i = 0; i < children.length; i++) { 173 IResource child = children[i]; 174 if (!child.isAccessible()) { 175 continue; 176 } 177 178 if (child.getType() == IResource.FILE) { 179 exportFile((IFile) child, currentPath); 180 } else { 181 IPath destination = currentPath.append(child.getName()); 182 exporter.createFolder(destination); 183 try { 184 exportChildren(((IContainer) child).members(), destination); 185 } catch (CoreException e) { 186 errorTable.add(e.getStatus()); 192 } 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, 213 0, NLS.bind(DataTransferMessages.DataTransfer_cannotOverwrite, targetFile.getAbsolutePath()), 214 null)); 215 monitor.worked(1); 216 return; 217 } 218 219 if (overwriteState == OVERWRITE_NONE) { 220 return; 221 } 222 223 if (overwriteState != OVERWRITE_ALL) { 224 String overwriteAnswer = overwriteCallback 225 .queryOverwrite(properPathString); 226 227 if (overwriteAnswer.equals(IOverwriteQuery.CANCEL)) { 228 throw new InterruptedException (); 229 } 230 231 if (overwriteAnswer.equals(IOverwriteQuery.NO)) { 232 monitor.worked(1); 233 return; 234 } 235 236 if (overwriteAnswer.equals(IOverwriteQuery.NO_ALL)) { 237 monitor.worked(1); 238 overwriteState = OVERWRITE_NONE; 239 return; 240 } 241 242 if (overwriteAnswer.equals(IOverwriteQuery.ALL)) { 243 overwriteState = OVERWRITE_ALL; 244 } 245 } 246 } 247 248 try { 249 exporter.write(file, fullPath); 250 } catch (IOException e) { 251 errorTable.add(new Status(IStatus.ERROR, PlatformUI.PLUGIN_ID, 0, 252 NLS.bind(DataTransferMessages.DataTransfer_errorExporting, fullPath, e.getMessage()), e)); 253 } catch (CoreException e) { 254 errorTable.add(new Status(IStatus.ERROR, PlatformUI.PLUGIN_ID, 0, 255 NLS.bind(DataTransferMessages.DataTransfer_errorExporting, fullPath, e.getMessage()), e)); 256 } 257 258 monitor.worked(1); 259 ModalContext.checkCanceled(monitor); 260 } 261 262 266 protected void exportSpecifiedResources() throws InterruptedException { 267 Iterator resources = resourcesToExport.iterator(); 268 IPath initPath = (IPath) path.clone(); 269 270 while (resources.hasNext()) { 271 IResource currentResource = (IResource) resources.next(); 272 if (!currentResource.isAccessible()) { 273 continue; 274 } 275 276 path = initPath; 277 278 if (resource == null) { 279 if (createLeadupStructure) { 283 createLeadupDirectoriesFor(currentResource); 284 } 285 286 } else { 287 IPath containersToCreate = currentResource.getFullPath() 290 .removeFirstSegments( 291 resource.getFullPath().segmentCount()) 292 .removeLastSegments(1); 293 294 for (int i = 0; i < containersToCreate.segmentCount(); i++) { 295 path = path.append(containersToCreate.segment(i)); 296 exporter.createFolder(path); 297 } 298 } 299 300 if (currentResource.getType() == IResource.FILE) { 301 exportFile((IFile) currentResource, path); 302 } else { 303 if (createContainerDirectories) { 304 path = path.append(currentResource.getName()); 305 exporter.createFolder(path); 306 } 307 308 try { 309 exportChildren(((IContainer) currentResource).members(), 310 path); 311 } catch (CoreException e) { 312 errorTable.add(e.getStatus()); 315 } 316 } 317 } 318 } 319 320 328 public IStatus getStatus() { 329 IStatus[] errors = new IStatus[errorTable.size()]; 330 errorTable.toArray(errors); 331 return new MultiStatus( 332 PlatformUI.PLUGIN_ID, 333 IStatus.OK, 334 errors, 335 DataTransferMessages.FileSystemExportOperation_problemsExporting, 336 null); 337 } 338 339 347 protected boolean isDescendent(List resources, IResource child) { 348 if (child.getType() == IResource.PROJECT) { 349 return false; 350 } 351 352 IResource parent = child.getParent(); 353 if (resources.contains(parent)) { 354 return true; 355 } 356 357 return isDescendent(resources, parent); 358 } 359 360 364 public void run(IProgressMonitor progressMonitor) 365 throws InterruptedException { 366 this.monitor = progressMonitor; 367 368 if (resource != null) { 369 if (createLeadupStructure) { 370 createLeadupDirectoriesFor(resource); 371 } 372 373 if (createContainerDirectories 374 && resource.getType() != IResource.FILE) { 375 path = path.append(resource.getName()); 377 exporter.createFolder(path); 378 } 379 } 380 381 try { 382 int totalWork = IProgressMonitor.UNKNOWN; 383 try { 384 if (resourcesToExport == null) { 385 totalWork = countChildrenOf(resource); 386 } else { 387 totalWork = countSelectedResources(); 388 } 389 } catch (CoreException e) { 390 errorTable.add(e.getStatus()); 392 } 393 monitor.beginTask(DataTransferMessages.DataTransfer_exportingTitle, totalWork); 394 if (resourcesToExport == null) { 395 exportAllResources(); 396 } else { 397 exportSpecifiedResources(); 398 } 399 } finally { 400 monitor.done(); 401 } 402 } 403 404 410 public void setCreateContainerDirectories(boolean value) { 411 createContainerDirectories = value; 412 } 413 414 420 public void setCreateLeadupStructure(boolean value) { 421 createLeadupStructure = value; 422 } 423 424 431 public void setOverwriteFiles(boolean value) { 432 if (value) { 433 overwriteState = OVERWRITE_ALL; 434 } 435 } 436 } 437 | Popular Tags |