001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.corext.refactoring.changes;
011:
012: import org.eclipse.core.runtime.Assert;
013: import org.eclipse.core.runtime.CoreException;
014: import org.eclipse.core.runtime.IPath;
015: import org.eclipse.core.runtime.IProgressMonitor;
016: import org.eclipse.core.runtime.NullProgressMonitor;
017: import org.eclipse.core.runtime.SubProgressMonitor;
018:
019: import org.eclipse.core.resources.IFile;
020: import org.eclipse.core.resources.IFolder;
021: import org.eclipse.core.resources.IResource;
022: import org.eclipse.core.resources.IResourceVisitor;
023: import org.eclipse.core.resources.ResourcesPlugin;
024:
025: import org.eclipse.ui.ide.undo.ResourceDescription;
026:
027: import org.eclipse.ltk.core.refactoring.Change;
028: import org.eclipse.ltk.core.refactoring.RefactoringStatus;
029:
030: import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
031: import org.eclipse.jdt.internal.corext.util.Messages;
032:
033: public class DeleteFolderChange extends AbstractDeleteChange {
034:
035: private final IPath fPath;
036: private final boolean fIsExecuteChange;
037:
038: public DeleteFolderChange(IFolder folder, boolean isExecuteChange) {
039: this (getFolderPath(folder), isExecuteChange);
040: }
041:
042: public DeleteFolderChange(IPath path, boolean isExecuteChange) {
043: fPath = path;
044: fIsExecuteChange = isExecuteChange;
045: }
046:
047: public static IPath getFolderPath(IFolder folder) {
048: return folder.getFullPath().removeFirstSegments(
049: ResourcesPlugin.getWorkspace().getRoot().getFullPath()
050: .segmentCount());
051: }
052:
053: public static IFolder getFolder(IPath path) {
054: return ResourcesPlugin.getWorkspace().getRoot().getFolder(path);
055: }
056:
057: public String getName() {
058: return Messages.format(
059: RefactoringCoreMessages.DeleteFolderChange_0, fPath
060: .lastSegment());
061: }
062:
063: public Object getModifiedElement() {
064: return getFolder(fPath);
065: }
066:
067: public RefactoringStatus isValid(IProgressMonitor pm)
068: throws CoreException {
069: if (fIsExecuteChange) {
070: // no need to do additional checking since the dialog
071: // already prompts the user if there are dirty
072: // or read only files in the folder. The change is
073: // currently not used as a undo/redo change
074: return super .isValid(pm, NONE);
075: } else {
076: return super .isValid(pm, READ_ONLY | DIRTY);
077: }
078: }
079:
080: protected Change doDelete(IProgressMonitor pm) throws CoreException {
081: IFolder folder = getFolder(fPath);
082: Assert.isTrue(folder.exists());
083: pm.beginTask("", 3); //$NON-NLS-1$
084: folder.accept(new IResourceVisitor() {
085: public boolean visit(IResource resource)
086: throws CoreException {
087: if (resource instanceof IFile) {
088: // progress is covered outside.
089: saveFileIfNeeded((IFile) resource,
090: new NullProgressMonitor());
091: }
092: return true;
093: }
094: }, IResource.DEPTH_INFINITE, false);
095: pm.worked(1);
096:
097: ResourceDescription resourceDescription = ResourceDescription
098: .fromResource(folder);
099: folder.delete(false, true, new SubProgressMonitor(pm, 1));
100: resourceDescription.recordStateFromHistory(folder,
101: new SubProgressMonitor(pm, 1));
102: pm.done();
103:
104: return new UndoDeleteResourceChange(resourceDescription);
105: }
106: }
|