KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > ide > undo > DeleteResourcesOperation


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 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.ui.ide.undo;
13
14 import java.util.ArrayList JavaDoc;
15
16 import org.eclipse.core.resources.IResource;
17 import org.eclipse.core.resources.mapping.IResourceChangeDescriptionFactory;
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IAdaptable;
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.core.runtime.IStatus;
22 import org.eclipse.core.runtime.Status;
23 import org.eclipse.core.runtime.jobs.ISchedulingRule;
24
25 /**
26  * A DeleteResourcesOperation represents an undoable operation for deleting one
27  * or more resources in the workspace. Clients may call the public API from a
28  * background thread.
29  *
30  * This class is intended to be instantiated and used by clients. It is not
31  * intended to be subclassed by clients.
32  *
33  * @since 3.3
34  *
35  */

36 public class DeleteResourcesOperation extends AbstractResourcesOperation {
37
38     // Whether to delete project content
39
private boolean deleteContent = false;
40
41     /**
42      * Create a DeleteResourcesOperation
43      *
44      * @param resources
45      * the resources to be deleted
46      * @param label
47      * the label of the operation
48      * @param deleteContent
49      * whether or not we are deleting content for projects
50      */

51     public DeleteResourcesOperation(IResource[] resources, String JavaDoc label,
52             boolean deleteContent) {
53         super(resources, label);
54         this.deleteContent = deleteContent;
55     }
56
57     /*
58      * (non-Javadoc)
59      *
60      * Map execution to resource deletion.
61      *
62      * @see org.eclipse.ui.ide.undo.AbstractWorkspaceOperation#doExecute(org.eclipse.core.runtime.IProgressMonitor,
63      * org.eclipse.core.runtime.IAdaptable)
64      */

65     protected void doExecute(IProgressMonitor monitor, IAdaptable uiInfo)
66             throws CoreException {
67         delete(monitor, uiInfo, deleteContent);
68     }
69
70     /*
71      * (non-Javadoc)
72      *
73      * Map undo to resource recreation.
74      *
75      * @see org.eclipse.ui.ide.undo.AbstractWorkspaceOperation#doUndo(org.eclipse.core.runtime.IProgressMonitor,
76      * org.eclipse.core.runtime.IAdaptable)
77      */

78     protected void doUndo(IProgressMonitor monitor, IAdaptable uiInfo)
79             throws CoreException {
80         recreate(monitor, uiInfo);
81     }
82
83     /*
84      * (non-Javadoc)
85      *
86      * @see org.eclipse.ui.ide.undo.AbstractWorkspaceOperation#updateResourceChangeDescriptionFactory(org.eclipse.core.resources.mapping.IResourceChangeDescriptionFactory,
87      * int)
88      */

89     protected boolean updateResourceChangeDescriptionFactory(
90             IResourceChangeDescriptionFactory factory, int operation) {
91         boolean modified = false;
92         if (operation == UNDO) {
93             for (int i = 0; i < resourceDescriptions.length; i++) {
94                 IResource resource = resourceDescriptions[i]
95                         .createResourceHandle();
96                 factory.create(resource);
97                 modified = true;
98             }
99         } else {
100             for (int i = 0; i < resources.length; i++) {
101                 IResource resource = resources[i];
102                 factory.delete(resource);
103                 modified = true;
104             }
105         }
106         return modified;
107     }
108
109     /*
110      * (non-Javadoc)
111      *
112      * @see org.eclipse.ui.ide.undo.AbstractWorkspaceOperation#getExecuteSchedulingRule()
113      */

114     protected ISchedulingRule getExecuteSchedulingRule() {
115         return super.computeDeleteSchedulingRule();
116     }
117
118     /*
119      * (non-Javadoc)
120      *
121      * @see org.eclipse.ui.ide.undo.AbstractWorkspaceOperation#getUndoSchedulingRule()
122      */

123     protected ISchedulingRule getUndoSchedulingRule() {
124         return super.computeCreateSchedulingRule();
125     }
126
127     /*
128      * (non-Javadoc)
129      *
130      * Map execution status to deletion status. Provide an extra warning if
131      * project content is to be deleted.
132      *
133      * @see org.eclipse.ui.ide.undo.AbstractWorkspaceOperation#computeExecutionStatus(org.eclipse.core.runtime.IProgressMonitor)
134      */

135     public IStatus computeExecutionStatus(IProgressMonitor monitor) {
136         IStatus status = super.computeExecutionStatus(monitor);
137         if (status.isOK()) {
138             status = computeDeleteStatus();
139         }
140         return status;
141     }
142
143     /*
144      * (non-Javadoc)
145      *
146      * Map undo status to resource creation status.
147      *
148      * @see org.eclipse.ui.ide.undo.AbstractWorkspaceOperation#computeUndoableStatus(org.eclipse.core.runtime.IProgressMonitor)
149      */

150     public IStatus computeUndoableStatus(IProgressMonitor monitor) {
151         IStatus status = super.computeUndoableStatus(monitor);
152         if (status.isOK()) {
153             // Recreating should not allow overwriting anything that is there,
154
// because we have no way to restore it.
155
// See https://bugs.eclipse.org/bugs/show_bug.cgi?id=162655
156
status = computeCreateStatus(false);
157         }
158         return status;
159     }
160
161     /*
162      * (non-Javadoc)
163      *
164      * Map redo status to resource deletion status.
165      *
166      * @see org.eclipse.ui.ide.undo.AbstractWorkspaceOperation#computeRedoableStatus(org.eclipse.core.runtime.IProgressMonitor)
167      */

168     public IStatus computeRedoableStatus(IProgressMonitor monitor) {
169         IStatus status = super.computeRedoableStatus(monitor);
170         if (status.isOK()) {
171             status = computeDeleteStatus();
172         }
173         return status;
174     }
175
176     /*
177      * (non-Javadoc)
178      *
179      * @see org.eclipse.ui.ide.undo.AbstractWorkspaceOperation#appendDescriptiveText(java.lang.StringBuffer)
180      */

181     protected void appendDescriptiveText(StringBuffer JavaDoc text) {
182         super.appendDescriptiveText(text);
183         text.append(" deleteContent: "); //$NON-NLS-1$
184
text.append(deleteContent);
185         text.append('\'');
186     }
187     
188     /*
189      * Overridden so that projects whose contents are not to be deleted
190      * will not be checked. A better solution would be to add API to
191      * ReadOnlyStateChecker to specify whether project children should
192      * be checked, but it is too late to do that now.
193      * See https://bugs.eclipse.org/bugs/show_bug.cgi?id=180758
194      */

195     IStatus checkReadOnlyResources(IResource[] resourcesToCheck) {
196         // If we aren't deleting content of projects, don't bother
197
// checking the read only status of projects or their children.
198
// Clients currently do not mix and match projects and non-projects
199
// in a DeleteResourcesOperation. However, this is not specified
200
// in the API, so assume that there could be mixes.
201
if (!deleteContent) {
202             ArrayList JavaDoc nonProjectResourcesToCheck = new ArrayList JavaDoc();
203             for (int i=0; i<resourcesToCheck.length; i++) {
204                 if (resourcesToCheck[i].getType() != IResource.PROJECT) {
205                     nonProjectResourcesToCheck.add(resourcesToCheck[i]);
206                 }
207             }
208             if (nonProjectResourcesToCheck.isEmpty()) {
209                 return Status.OK_STATUS;
210             }
211             return super.checkReadOnlyResources((IResource[])nonProjectResourcesToCheck
212                     .toArray(new IResource [nonProjectResourcesToCheck.size()]));
213         }
214         // We are deleting project content, so do it the normal way
215
return super.checkReadOnlyResources(resourcesToCheck);
216     }
217 }
218
Popular Tags