📄 workspaceaction.java
字号:
/**
* Returns whether the given resource is a descendent of any of the resources
* in the given list.
*
* @param resources the list of resources (element type: <code>IResource</code>)
* @param child the resource to check
* @return <code>true</code> if <code>child</code> is a descendent of any of the
* elements of <code>resources</code>
*/
boolean isDescendent(List resources, IResource child) {
IResource parent = child.getParent();
return parent != null
&& (resources.contains(parent) || isDescendent(resources,
parent));
}
/**
* Performs pruning on the given list of resources, as described in
* <code>shouldPerformResourcePruning</code>.
*
* @param resourceCollection the list of resources (element type:
* <code>IResource</code>)
* @return the list of resources (element type: <code>IResource</code>)
* after pruning.
* @see #shouldPerformResourcePruning
*/
List pruneResources(List resourceCollection) {
List prunedList = new ArrayList(resourceCollection);
Iterator elementsEnum = prunedList.iterator();
while (elementsEnum.hasNext()) {
IResource currentResource = (IResource) elementsEnum.next();
if (isDescendent(prunedList, currentResource)) {
elementsEnum.remove(); //Removes currentResource
}
}
return prunedList;
}
/**
* Records the core exception to be displayed to the user
* once the action is finished.
*
* @param error a <code>CoreException</code>
*/
MultiStatus recordError(MultiStatus errors, CoreException error) {
if (errors == null) {
errors = new MultiStatus(IDEWorkbenchPlugin.IDE_WORKBENCH,
IStatus.ERROR, getProblemsMessage(), null);
}
errors.merge(error.getStatus());
return errors;
}
/**
* The <code>CoreWrapperAction</code> implementation of this <code>IAction</code>
* method uses a <code>ProgressMonitorDialog</code> to run the operation. The
* operation calls <code>execute</code> (which, in turn, calls
* <code>invokeOperation</code>). Afterwards, any <code>CoreException</code>s
* encountered while running the operation are reported to the user via a
* problems dialog.
* <p>
* Subclasses may extend this method.
* </p>
*/
public void run() {
final IStatus[] errorStatus = new IStatus[1];
try {
WorkspaceModifyOperation op = new WorkspaceModifyOperation() {
public void execute(IProgressMonitor monitor) {
errorStatus[0] = WorkspaceAction.this.execute(getActionResources(), monitor);
}
};
new ProgressMonitorJobsDialog(shell).run(true, true, op);
} catch (InterruptedException e) {
return;
} catch (InvocationTargetException e) {
// we catch CoreException in execute(), but unexpected runtime exceptions or errors may still occur
String msg = NLS.bind(IDEWorkbenchMessages.WorkspaceAction_logTitle, getClass().getName(), e.getTargetException());
IDEWorkbenchPlugin.log(msg, StatusUtil.newStatus(IStatus.ERROR,
msg, e.getTargetException()));
displayError(e.getTargetException().getMessage());
}
// If errors occurred, open an Error dialog & build a multi status error for it
if (errorStatus[0] != null && !errorStatus[0].isOK()) {
ErrorDialog.openError(shell, getProblemsTitle(), null, // no special message
errorStatus[0]);
}
}
/**
* Returns whether this action should attempt to optimize the resources being
* operated on. This kind of pruning makes sense when the operation has depth
* infinity semantics (when the operation is applied explicitly to a resource
* then it is also applied implicitly to all the resource's descendents).
* <p>
* The <code>WorkspaceAction</code> implementation of this method returns
* <code>true</code>. Subclasses should reimplement to return <code>false</code>
* if pruning is not required.
* </p>
*
* @return <code>true</code> if pruning should be performed,
* and <code>false</code> if pruning is not desired
*
* @since 3.1
*/
protected boolean shouldPerformResourcePruning() {
return true;
}
/**
* The <code>WorkspaceAction</code> implementation of this
* <code>SelectionListenerAction</code> method ensures that this action is
* disabled if any of the selected resources are inaccessible. Subclasses may
* extend to react to selection changes; however, if the super method returns
* <code>false</code>, the overriding method should also return <code>false</code>.
*/
protected boolean updateSelection(IStructuredSelection selection) {
if (!super.updateSelection(selection) || selection.isEmpty()) {
return false;
}
for (Iterator i = getSelectedResources().iterator(); i.hasNext();) {
IResource r = (IResource) i.next();
if (!r.isAccessible()) {
return false;
}
}
return true;
}
/**
* Returns the elements that the action is to be performed on.
* By default return the selected resources.
* <p>
* Subclasses may override this method.
*
* @return list of resource elements (element type: <code>IResource</code>)
*/
protected List getActionResources() {
return getSelectedResources();
}
/**
* Run the action in the background rather than with the
* progress dialog.
* @param rule The rule to apply to the background job or
* <code>null</code> if there isn't one.
*/
public void runInBackground(ISchedulingRule rule) {
runInBackground(rule, (Object []) null);
}
/**
* Run the action in the background rather than with the
* progress dialog.
* @param rule The rule to apply to the background job or
* <code>null</code> if there isn't one.
* @param jobFamily a single family that the job should
* belong to or <code>null</code> if none.
*
* @since 3.1
*/
public void runInBackground(ISchedulingRule rule, Object jobFamily) {
if (jobFamily == null) {
runInBackground(rule, (Object []) null);
} else {
runInBackground(rule, new Object[] {jobFamily});
}
}
/**
* Run the action in the background rather than with the
* progress dialog.
* @param rule The rule to apply to the background job or
* <code>null</code> if there isn't one.
* @param jobFamilies the families the job should belong
* to or <code>null</code> if none.
*
* @since 3.1
*/
public void runInBackground(ISchedulingRule rule, final Object [] jobFamilies) {
//obtain a copy of the selected resources before the job is forked
final List resources = new ArrayList(getActionResources());
Job job = new WorkspaceJob(removeMnemonics(getText())) {
/* (non-Javadoc)
* @see org.eclipse.core.runtime.jobs.Job#belongsTo(java.lang.Object)
*/
public boolean belongsTo(Object family) {
if (jobFamilies == null || family == null) {
return false;
}
for (int i = 0; i < jobFamilies.length; i++) {
if (family.equals(jobFamilies[i])) {
return true;
}
}
return false;
}
/* (non-Javadoc)
* @see org.eclipse.core.resources.WorkspaceJob#runInWorkspace(org.eclipse.core.runtime.IProgressMonitor)
*/
public IStatus runInWorkspace(IProgressMonitor monitor) {
return WorkspaceAction.this.execute(resources, monitor);
}
};
if (rule != null) {
job.setRule(rule);
}
job.setUser(true);
job.schedule();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -