KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.eclipse.core.commands.ExecutionException;
15 import org.eclipse.core.commands.operations.AbstractOperation;
16 import org.eclipse.core.commands.operations.IAdvancedUndoableOperation;
17 import org.eclipse.core.commands.operations.IAdvancedUndoableOperation2;
18 import org.eclipse.core.commands.operations.OperationHistoryEvent;
19 import org.eclipse.core.commands.operations.OperationStatus;
20 import org.eclipse.core.resources.IResource;
21 import org.eclipse.core.resources.IResourceRuleFactory;
22 import org.eclipse.core.resources.IWorkspace;
23 import org.eclipse.core.resources.IWorkspaceRunnable;
24 import org.eclipse.core.resources.ResourcesPlugin;
25 import org.eclipse.core.resources.mapping.IResourceChangeDescriptionFactory;
26 import org.eclipse.core.resources.mapping.ResourceChangeValidator;
27 import org.eclipse.core.runtime.CoreException;
28 import org.eclipse.core.runtime.IAdaptable;
29 import org.eclipse.core.runtime.IProgressMonitor;
30 import org.eclipse.core.runtime.IStatus;
31 import org.eclipse.core.runtime.Status;
32 import org.eclipse.core.runtime.jobs.ISchedulingRule;
33 import org.eclipse.jface.action.Action;
34 import org.eclipse.osgi.util.NLS;
35 import org.eclipse.swt.widgets.Shell;
36 import org.eclipse.ui.PlatformUI;
37 import org.eclipse.ui.ide.IDE;
38 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
39 import org.eclipse.ui.internal.ide.undo.UndoMessages;
40
41 /**
42  * An AbstractWorkspaceOperation represents an undoable operation that affects
43  * the workspace. It handles common workspace operation activities such as
44  * tracking which resources are affected by an operation, prompting the user
45  * when there are possible side effects of operations, building execution
46  * exceptions from core exceptions, etc. Clients may call the public API from a
47  * background thread.
48  *
49  * This class is not intended to be subclassed by clients.
50  *
51  * @since 3.3
52  *
53  */

54 public abstract class AbstractWorkspaceOperation extends AbstractOperation
55         implements IAdvancedUndoableOperation, IAdvancedUndoableOperation2 {
56
57     private static String JavaDoc ELLIPSIS = "..."; //$NON-NLS-1$
58

59     protected static int EXECUTE = 1;
60
61     protected static int UNDO = 2;
62
63     protected static int REDO = 3;
64
65     protected IResource[] resources;
66
67     private boolean isValid = true;
68
69     /*
70      * Specifies whether any user prompting is appropriate while computing
71      * status.
72      */

73     protected boolean quietCompute = false;
74
75     String JavaDoc[] modelProviderIds;
76
77     /**
78      * Create an AbstractWorkspaceOperation with the specified name.
79      *
80      * @param name
81      * the name used to describe the operation
82      */

83     AbstractWorkspaceOperation(String JavaDoc name) {
84         // Many operation names are based on the triggering action's name, so
85
// we strip out the any mnemonics that may be embedded in the name.
86
super(Action.removeMnemonics(name));
87
88         // For the same reason, check for an ellipsis and strip out
89
String JavaDoc label = this.getLabel();
90         if (label.endsWith(ELLIPSIS)) {
91             this.setLabel(label
92                     .substring(0, label.length() - ELLIPSIS.length()));
93         }
94     }
95
96     /**
97      * Set the ids of any model providers for the resources involved.
98      *
99      * @param ids
100      * the array of String model provider ids that provide models
101      * associated with the resources involved in this operation
102      */

103     public void setModelProviderIds(String JavaDoc[] ids) {
104         modelProviderIds = ids;
105     }
106
107     /**
108      * Set the resources which are affected by this operation
109      *
110      * @param resources
111      * an array of resources
112      */

113     protected void setTargetResources(IResource[] resources) {
114         this.resources = resources;
115     }
116
117     /**
118      * Return the workspace manipulated by this operation.
119      *
120      * @return the IWorkspace used by this operation.
121      */

122     protected IWorkspace getWorkspace() {
123         return ResourcesPlugin.getWorkspace();
124     }
125
126     /**
127      * Return the workspace rule factory associated with this operation.
128      *
129      * @return the IResourceRuleFactory associated with this operation.
130      */

131     protected IResourceRuleFactory getWorkspaceRuleFactory() {
132         return getWorkspace().getRuleFactory();
133     }
134
135     /**
136      * Mark this operation invalid due to some external change. May be used by
137      * subclasses.
138      *
139      */

140     protected void markInvalid() {
141         isValid = false;
142     }
143
144     /*
145      * (non-Javadoc)
146      *
147      * This implementation checks a validity flag.
148      *
149      * @see org.eclipse.core.commands.operations.IUndoableOperation#canExecute()
150      */

151     public boolean canExecute() {
152         return isValid();
153     }
154
155     /*
156      * (non-Javadoc)
157      *
158      * This implementation checks a validity flag.
159      *
160      * @see org.eclipse.core.commands.operations.IUndoableOperation#canUndo()
161      */

162     public boolean canUndo() {
163         return isValid();
164     }
165
166     /*
167      * (non-Javadoc)
168      *
169      * This implementation checks a validity flag.
170      *
171      * @see org.eclipse.core.commands.operations.IUndoableOperation#canRedo()
172      */

173     public boolean canRedo() {
174         return isValid();
175     }
176
177     /**
178      * Execute the specified operation. This implementation executes the
179      * operation in a workspace runnable and catches any CoreExceptions
180      * resulting from the operation. Unhandled CoreExceptions are propagated as
181      * ExecutionExceptions.
182      *
183      * @param monitor
184      * the progress monitor to use for the operation
185      * @param uiInfo
186      * the IAdaptable (or <code>null</code>) provided by the
187      * caller in order to supply UI information for prompting the
188      * user if necessary. When this parameter is not
189      * <code>null</code>, it contains an adapter for the
190      * org.eclipse.swt.widgets.Shell.class
191      * @return the IStatus of the execution. The status severity should be set
192      * to <code>OK</code> if the operation was successful, and
193      * <code>ERROR</code> if it was not. Any other status is assumed
194      * to represent an incompletion of the execution.
195      * @throws ExecutionException
196      * if an exception occurred during execution.
197      *
198      * @see org.eclipse.core.commands.operations.IUndoableOperation#execute(org.eclipse.core.runtime.IProgressMonitor,
199      * org.eclipse.core.runtime.IAdaptable)
200      */

201     public IStatus execute(IProgressMonitor monitor, final IAdaptable uiInfo)
202             throws ExecutionException {
203         try {
204             getWorkspace().run(new IWorkspaceRunnable() {
205                 public void run(IProgressMonitor monitor) throws CoreException {
206                     doExecute(monitor, uiInfo);
207                 }
208             }, getExecuteSchedulingRule(), IWorkspace.AVOID_UPDATE, monitor);
209         } catch (final CoreException e) {
210             throw new ExecutionException(NLS.bind(
211                     UndoMessages.AbstractWorkspaceOperation_ExecuteErrorTitle,
212                     getLabel()), e);
213         }
214         isValid = true;
215         return Status.OK_STATUS;
216     }
217
218     /**
219      * Redo the specified operation. This implementation redoes the operation in
220      * a workspace runnable and catches any CoreExceptions resulting from the
221      * operation. Unhandled CoreExceptions are propagated as
222      * ExecutionExceptions.
223      *
224      * @param monitor
225      * the progress monitor to use for the operation
226      * @param uiInfo
227      * the IAdaptable (or <code>null</code>) provided by the
228      * caller in order to supply UI information for prompting the
229      * user if necessary. When this parameter is not
230      * <code>null</code>, it contains an adapter for the
231      * org.eclipse.swt.widgets.Shell.class
232      * @return the IStatus of the redo. The status severity should be set to
233      * <code>OK</code> if the operation was successful, and
234      * <code>ERROR</code> if it was not. Any other status is assumed
235      * to represent an incompletion of the redo.
236      * @throws ExecutionException
237      * if an exception occurred during execution.
238      * @see org.eclipse.core.commands.operations.IUndoableOperation#redo(org.eclipse.core.runtime.IProgressMonitor,
239      * org.eclipse.core.runtime.IAdaptable)
240      */

241     public IStatus redo(IProgressMonitor monitor, final IAdaptable uiInfo)
242             throws ExecutionException {
243         try {
244             getWorkspace().run(new IWorkspaceRunnable() {
245                 public void run(IProgressMonitor monitor) throws CoreException {
246                     doExecute(monitor, uiInfo);
247                 }
248             }, getRedoSchedulingRule(), IWorkspace.AVOID_UPDATE, monitor);
249         } catch (final CoreException e) {
250             throw new ExecutionException(NLS.bind(
251                     UndoMessages.AbstractWorkspaceOperation_RedoErrorTitle,
252                     getLabel()), e);
253
254         }
255         isValid = true;
256         return Status.OK_STATUS;
257     }
258
259     /**
260      * Undo the specified operation. This implementation undoes the operation in
261      * a workspace runnable and catches any CoreExceptions resulting from the
262      * operation. Unhandled CoreExceptions are propagated as
263      * ExecutionExceptions.
264      *
265      * @param monitor
266      * the progress monitor to use for the operation
267      * @param uiInfo
268      * the IAdaptable (or <code>null</code>) provided by the
269      * caller in order to supply UI information for prompting the
270      * user if necessary. When this parameter is not
271      * <code>null</code>, it contains an adapter for the
272      * org.eclipse.swt.widgets.Shell.class
273      * @return the IStatus of the undo. The status severity should be set to
274      * <code>OK</code> if the operation was successful, and
275      * <code>ERROR</code> if it was not. Any other status is assumed
276      * to represent an incompletion of the undo. *
277      * @throws ExecutionException
278      * if an exception occurred during execution.
279      * @see org.eclipse.core.commands.operations.IUndoableOperation#undo(org.eclipse.core.runtime.IProgressMonitor,
280      * org.eclipse.core.runtime.IAdaptable)
281      */

282     public IStatus undo(IProgressMonitor monitor, final IAdaptable uiInfo)
283             throws ExecutionException {
284         try {
285             getWorkspace().run(new IWorkspaceRunnable() {
286                 public void run(IProgressMonitor monitor) throws CoreException {
287                     doUndo(monitor, uiInfo);
288                 }
289             }, getUndoSchedulingRule(), IWorkspace.AVOID_UPDATE, monitor);
290         } catch (final CoreException e) {
291             throw new ExecutionException(NLS.bind(
292                     UndoMessages.AbstractWorkspaceOperation_UndoErrorTitle,
293                     getLabel()), e);
294
295         }
296         isValid = true;
297         return Status.OK_STATUS;
298     }
299
300     /**
301      * Perform the specific work involved in undoing this operation.
302      *
303      * @param monitor
304      * the progress monitor to use for the operation
305      * @param uiInfo
306      * the IAdaptable (or <code>null</code>) provided by the
307      * caller in order to supply UI information for prompting the
308      * user if necessary. When this parameter is not
309      * <code>null</code>, it contains an adapter for the
310      * org.eclipse.swt.widgets.Shell.class
311      * @throws CoreException
312      * propagates any CoreExceptions thrown from the resources API
313      */

314     protected abstract void doUndo(IProgressMonitor monitor, IAdaptable uiInfo)
315             throws CoreException;
316
317     /**
318      * Perform the specific work involved in executing this operation.
319      *
320      * @param monitor
321      * the progress monitor to use for the operation
322      * @param uiInfo
323      * the IAdaptable (or <code>null</code>) provided by the
324      * caller in order to supply UI information for prompting the
325      * user if necessary. When this parameter is not
326      * <code>null</code>, it contains an adapter for the
327      * org.eclipse.swt.widgets.Shell.class
328      * @throws CoreException
329      * propagates any CoreExceptions thrown from the resources API
330      *
331      */

332     protected abstract void doExecute(IProgressMonitor monitor,
333             IAdaptable uiInfo) throws CoreException;
334
335     /**
336      * Return whether the proposed operation is valid. The default
337      * implementation simply checks to see if the flag has been marked as
338      * invalid, relying on subclasses to mark the flag invalid when appropriate.
339      *
340      * @return the validity flag
341      */

342     protected boolean isValid() {
343         return isValid;
344     }
345
346     /*
347      * (non-Javadoc)
348      *
349      * @see org.eclipse.core.commands.operations.IAdvancedUndoableOperation#aboutToNotify(org.eclipse.core.commands.operations.OperationHistoryEvent)
350      */

351     public void aboutToNotify(OperationHistoryEvent event) {
352         // do nothing
353
}
354
355     /*
356      * (non-Javadoc)
357      *
358      * @see org.eclipse.core.commands.operations.IAdvancedUndoableOperation#getAffectedObjects()
359      */

360     public Object JavaDoc[] getAffectedObjects() {
361         return resources;
362     }
363
364     /**
365      * Return a status indicating the projected outcome of executing the
366      * receiver. This method is not called by the operation history, but instead
367      * is used by clients (such as implementers of
368      * {@link org.eclipse.core.commands.operations.IOperationApprover2}) who
369      * wish to perform advanced validation of an operation before attempting to
370      * execute it.
371      *
372      * If an ERROR status is returned, the operation will not proceed and the
373      * user notified if deemed necessary by the caller. The validity flag on the
374      * operation should be marked as invalid. If an OK status is returned, the
375      * operation will proceed. The caller must interpret any other returned
376      * status severity, and may choose to prompt the user as to how to proceed.
377      *
378      * If there are multiple conditions that result in an ambiguous status
379      * severity, it is best for the implementor of this method to consult the
380      * user as to how to proceed for each one, and return an OK or ERROR status
381      * that accurately reflects the user's wishes, or to return a multi-status
382      * that accurately describes all of the issues at hand, so that the caller
383      * may potentially consult the user. (Note that the user should not be
384      * consulted at all if a client has called {@link #setQuietCompute(boolean)}
385      * with a value of <code>true</code>.)
386      *
387      * This implementation computes the validity of execution by computing the
388      * resource delta that would be generated on execution, and checking whether
389      * any registered model providers are affected by the operation.
390      *
391      * @param monitor
392      * the progress monitor to be used for computing the status
393      * @return the status indicating the projected outcome of executing the
394      * receiver
395      *
396      * @see org.eclipse.core.commands.operations.IAdvancedUndoableOperation#computeUndoableStatus(org.eclipse.core.runtime.IProgressMonitor)
397      * @see #setQuietCompute(boolean)
398      */

399     public IStatus computeExecutionStatus(IProgressMonitor monitor) {
400         IStatus status = Status.OK_STATUS;
401
402         // If we are not to prompt the user, nothing to do.
403
if (quietCompute) {
404             return status;
405         }
406
407         IResourceChangeDescriptionFactory factory = ResourceChangeValidator
408                 .getValidator().createDeltaFactory();
409         if (updateResourceChangeDescriptionFactory(factory, EXECUTE)) {
410             boolean proceed = IDE
411                     .promptToConfirm(
412                             getShell(null),
413                             UndoMessages.AbstractWorkspaceOperation_SideEffectsWarningTitle,
414                             NLS
415                                     .bind(
416                                             UndoMessages.AbstractWorkspaceOperation_ExecuteSideEffectsWarningMessage,
417                                             getLabel()), factory.getDelta(),
418                             modelProviderIds, true /* syncExec */);
419             if (!proceed) {
420                 status = Status.CANCEL_STATUS;
421             }
422         }
423         return status;
424
425     }
426
427     /**
428      * Return a status indicating the projected outcome of undoing the receiver.
429      * This method is not called by the operation history, but instead is used
430      * by clients (such as implementers of
431      * {@link org.eclipse.core.commands.operations.IOperationApprover2}) who
432      * wish to perform advanced validation of an operation before attempting to
433      * undo it.
434      *
435      * If an ERROR status is returned, the undo will not proceed and the user
436      * notified if deemed necessary by the caller. The validity flag on the
437      * operation should be marked as invalid. If an OK status is returned, the
438      * undo will proceed. The caller must interpret any other returned status
439      * severity, and may choose to prompt the user as to how to proceed.
440      *
441      * If there are multiple conditions that result in an ambiguous status
442      * severity, it is best for the implementor of this method to consult the
443      * user as to how to proceed for each one, and return an OK or ERROR status
444      * that accurately reflects the user's wishes, or to return a multi-status
445      * that accurately describes all of the issues at hand, so that the caller
446      * may potentially consult the user. (Note that the user should not be
447      * consulted at all if a client has called {@link #setQuietCompute(boolean)}
448      * with a value of <code>true</code>.)
449      *
450      * This implementation computes the validity of undo by computing the
451      * resource delta that would be generated on undo, and checking whether any
452      * registered model providers are affected by the operation.
453      *
454      * @param monitor
455      * the progress monitor to be used for computing the status
456      * @return the status indicating the projected outcome of undoing the
457      * receiver
458      *
459      * @see org.eclipse.core.commands.operations.IAdvancedUndoableOperation#computeUndoableStatus(org.eclipse.core.runtime.IProgressMonitor)
460      * @see #setQuietCompute(boolean)
461      */

462     public IStatus computeUndoableStatus(IProgressMonitor monitor) {
463         IStatus status = Status.OK_STATUS;
464         // If we are not to prompt the user, nothing to do.
465
if (quietCompute) {
466             return status;
467         }
468
469         IResourceChangeDescriptionFactory factory = ResourceChangeValidator
470                 .getValidator().createDeltaFactory();
471         if (updateResourceChangeDescriptionFactory(factory, UNDO)) {
472             boolean proceed = IDE
473                     .promptToConfirm(
474                             getShell(null),
475                             UndoMessages.AbstractWorkspaceOperation_SideEffectsWarningTitle,
476                             NLS
477                                     .bind(
478                                             UndoMessages.AbstractWorkspaceOperation_UndoSideEffectsWarningMessage,
479                                             getLabel()), factory.getDelta(),
480                             modelProviderIds, true /* syncExec */);
481             if (!proceed) {
482                 status = Status.CANCEL_STATUS;
483             }
484         }
485         return status;
486
487     }
488
489     /**
490      * Return a status indicating the projected outcome of redoing the receiver.
491      * This method is not called by the operation history, but instead is used
492      * by clients (such as implementers of
493      * {@link org.eclipse.core.commands.operations.IOperationApprover2}) who
494      * wish to perform advanced validation of an operation before attempting to
495      * redo it.
496      *
497      * If an ERROR status is returned, the redo will not proceed and the user
498      * notified if deemed necessary by the caller. The validity flag on the
499      * operation should be marked as invalid. If an OK status is returned, the
500      * redo will proceed. The caller must interpret any other returned status
501      * severity, and may choose to prompt the user as to how to proceed.
502      *
503      * If there are multiple conditions that result in an ambiguous status
504      * severity, it is best for the implementor of this method to consult the
505      * user as to how to proceed for each one, and return an OK or ERROR status
506      * that accurately reflects the user's wishes, or to return a multi-status
507      * that accurately describes all of the issues at hand, so that the caller
508      * may potentially consult the user. (Note that the user should not be
509      * consulted at all if a client has called {@link #setQuietCompute(boolean)}
510      * with a value of <code>true</code>.)
511      *
512      * This implementation computes the validity of redo by computing the
513      * resource delta that would be generated on redo, and checking whether any
514      * registered model providers are affected by the operation.
515      *
516      * @param monitor
517      * the progress monitor to be used for computing the status
518      * @return the status indicating the projected outcome of redoing the
519      * receiver
520      *
521      * @see org.eclipse.core.commands.operations.IAdvancedUndoableOperation#computeUndoableStatus(org.eclipse.core.runtime.IProgressMonitor)
522      * @see #setQuietCompute(boolean)
523      */

524     public IStatus computeRedoableStatus(IProgressMonitor monitor) {
525         IStatus status = Status.OK_STATUS;
526         // If we are not to prompt the user, nothing to do.
527
if (quietCompute) {
528             return status;
529         }
530
531         IResourceChangeDescriptionFactory factory = ResourceChangeValidator
532                 .getValidator().createDeltaFactory();
533         if (updateResourceChangeDescriptionFactory(factory, REDO)) {
534             boolean proceed = IDE
535                     .promptToConfirm(
536                             getShell(null),
537                             UndoMessages.AbstractWorkspaceOperation_SideEffectsWarningTitle,
538                             NLS
539                                     .bind(
540                                             UndoMessages.AbstractWorkspaceOperation_RedoSideEffectsWarningMessage,
541                                             getLabel()), factory.getDelta(),
542                             modelProviderIds, true /* syncExec */);
543             if (!proceed) {
544                 status = Status.CANCEL_STATUS;
545             }
546         }
547         return status;
548     }
549
550     /**
551      * Update the provided resource change description factory so it can
552      * generate a resource delta describing the result of an undo or redo.
553      * Return a boolean indicating whether any update was done. The default
554      * implementation does not update the factory. Subclasses are expected to
555      * override this method to more specifically describe their modifications to
556      * the workspace.
557      *
558      * @param factory
559      * the factory to update
560      * @param operation
561      * an integer indicating whether the change is part of an
562      * execute, undo, or redo
563      * @return a boolean indicating whether the factory was updated.
564      */

565     protected boolean updateResourceChangeDescriptionFactory(
566             IResourceChangeDescriptionFactory factory, int operation) {
567         return false;
568     }
569
570     /**
571      * Return an error status describing an invalid operation using the provided
572      * message.
573      *
574      * @param message
575      * the message to be used in the status, or <code>null</code>
576      * if a generic message should be used
577      * @return the error status
578      */

579     protected IStatus getErrorStatus(String JavaDoc message) {
580         String JavaDoc statusMessage = message;
581         if (statusMessage == null) {
582             statusMessage = NLS
583                     .bind(
584                             UndoMessages.AbstractWorkspaceOperation_ErrorInvalidMessage,
585                             getLabel());
586         }
587         return new Status(IStatus.ERROR, IDEWorkbenchPlugin.IDE_WORKBENCH,
588                 OperationStatus.OPERATION_INVALID, statusMessage, null);
589     }
590
591     /**
592      * Return a warning status describing the warning state of an operation
593      * using the provided message and code.
594      *
595      * @param message
596      * the message to be used in the status, or <code>null</code>
597      * if a generic message should be used
598      * @param code
599      * the integer code to be assigned to the status
600      * @return the warning status
601      */

602     protected IStatus getWarningStatus(String JavaDoc message, int code) {
603         String JavaDoc statusMessage = message;
604         if (statusMessage == null) {
605             statusMessage = NLS
606                     .bind(
607                             UndoMessages.AbstractWorkspaceOperation_GenericWarningMessage,
608                             getLabel());
609         }
610         return new Status(IStatus.WARNING, IDEWorkbenchPlugin.IDE_WORKBENCH,
611                 code, statusMessage, null);
612     }
613
614     /**
615      * Return whether the resources known by this operation currently exist.
616      *
617      * @return <code>true</code> if there are existing resources and
618      * <code>false</code> if there are no known resources or any one
619      * of them does not exist
620      */

621     protected boolean resourcesExist() {
622         if (resources == null || resources.length == 0) {
623             return false;
624         }
625         for (int i = 0; i < resources.length; i++) {
626             if (!resources[i].exists()) {
627                 return false;
628             }
629         }
630         return true;
631     }
632
633     /**
634      * Return whether the resources known by this operation contain any
635      * projects.
636      *
637      * @return <code>true</code> if there is one or more projects known by
638      * this operation and false if there are no projects.
639      */

640     protected boolean resourcesIncludesProjects() {
641         if (resources == null || resources.length == 0) {
642             return false;
643         }
644         for (int i = 0; i < resources.length; i++) {
645             if (resources[i].getType() == IResource.PROJECT) {
646                 return true;
647             }
648         }
649         return false;
650     }
651
652     /**
653      * Return a scheduling rule appropriate for executing this operation.
654      *
655      * The default implementation is to return a rule that locks out the entire
656      * workspace. Subclasses are encouraged to provide more specific rules that
657      * affect only their resources.
658      *
659      * @return the scheduling rule to use when executing this operation, or
660      * <code>null</code> if there are no scheduling restrictions for
661      * this operation.
662      *
663      * @see IWorkspace#run(IWorkspaceRunnable, ISchedulingRule, int,
664      * IProgressMonitor)
665      */

666     protected ISchedulingRule getExecuteSchedulingRule() {
667         return getWorkspace().getRoot();
668     }
669
670     /**
671      * Return a scheduling rule appropriate for undoing this operation.
672      *
673      * The default implementation is to return a rule that locks out the entire
674      * workspace. Subclasses are encouraged to provide more specific rules that
675      * affect only their resources.
676      *
677      * @return the scheduling rule to use when undoing this operation, or
678      * <code>null</code> if there are no scheduling restrictions for
679      * this operation.
680      *
681      * @see IWorkspace#run(IWorkspaceRunnable, ISchedulingRule, int,
682      * IProgressMonitor)
683      */

684     protected ISchedulingRule getUndoSchedulingRule() {
685         return getWorkspace().getRoot();
686     }
687
688     /**
689      * Return a scheduling rule appropriate for redoing this operation.
690      *
691      * The default implementation considers the redo scheduling rule the same as
692      * the original execution scheduling rule.
693      *
694      * @return the scheduling rule to use when redoing this operation, or
695      * <code>null</code> if there are no scheduling restrictions for
696      * this operation.
697      *
698      * @see IWorkspace#run(IWorkspaceRunnable, ISchedulingRule, int,
699      * IProgressMonitor)
700      */

701     protected ISchedulingRule getRedoSchedulingRule() {
702         return getExecuteSchedulingRule();
703     }
704
705     /*
706      * (non-Javadoc)
707      *
708      * @see org.eclipse.core.commands.operations.IAdvancedUndoableOperation2#setQuietCompute(boolean)
709      */

710     public void setQuietCompute(boolean quiet) {
711         quietCompute = quiet;
712     }
713
714     /*
715      * @see java.lang.Object#toString()
716      */

717     public String JavaDoc toString() {
718         StringBuffer JavaDoc text = new StringBuffer JavaDoc(super.toString());
719         text.append("\n"); //$NON-NLS-1$
720
text.append(this.getClass().getName());
721         appendDescriptiveText(text);
722         return text.toString();
723     }
724
725     /**
726      * Append any descriptive text to the specified string buffer to be shown in
727      * the receiver's {@link #toString()} text.
728      * <p>Note that this method is not intend to be subclassed by clients.
729      *
730      * @param text
731      * the StringBuffer on which to append the text
732      */

733     protected void appendDescriptiveText(StringBuffer JavaDoc text) {
734         text.append(" resources: "); //$NON-NLS-1$
735
text.append(resources);
736         text.append('\'');
737     }
738
739     /**
740      * Return the shell described by the specified adaptable, or the active
741      * shell if no shell has been specified in the adaptable.
742      *
743      * @param uiInfo
744      * the IAdaptable (or <code>null</code>) provided by the
745      * caller in order to supply UI information for prompting the
746      * user if necessary. When this parameter is not
747      * <code>null</code>, it contains an adapter for the
748      * org.eclipse.swt.widgets.Shell.class
749      *
750      * @return the shell specified in the adaptable, or the active shell if no
751      * shell has been specified
752      *
753      */

754     protected Shell getShell(IAdaptable uiInfo) {
755         if (uiInfo != null) {
756             Shell shell = (Shell) uiInfo.getAdapter(Shell.class);
757             if (shell != null) {
758                 return shell;
759             }
760         }
761         return PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
762     }
763
764     /*
765      * (non-Javadoc)
766      *
767      * @see org.eclipse.core.commands.operations.IAdvancedUndoableOperation2#runInBackground()
768      */

769     public boolean runInBackground() {
770         return true;
771     }
772 }
773
Popular Tags