Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSergey Prigogin2014-07-30 22:58:06 +0000
committerSergey Prigogin2014-07-30 23:47:55 +0000
commit31f37f5b63cc75cbf8d39b14ab05019607c462b6 (patch)
tree1ad467aa4da30f6ee9c89f3bc55e2515c05f32a9 /build/org.eclipse.cdt.managedbuilder.ui/src/org/eclipse/cdt
parent6a94e17ca785a27daf909dbddc6c401be9c38d50 (diff)
downloadorg.eclipse.cdt-31f37f5b63cc75cbf8d39b14ab05019607c462b6.tar.gz
org.eclipse.cdt-31f37f5b63cc75cbf8d39b14ab05019607c462b6.tar.xz
org.eclipse.cdt-31f37f5b63cc75cbf8d39b14ab05019607c462b6.zip
Proper enablement/disablement of Clean/Build Selected Files commands.
Diffstat (limited to 'build/org.eclipse.cdt.managedbuilder.ui/src/org/eclipse/cdt')
-rw-r--r--build/org.eclipse.cdt.managedbuilder.ui/src/org/eclipse/cdt/managedbuilder/internal/ui/actions/BuildFilesAction.java381
-rw-r--r--build/org.eclipse.cdt.managedbuilder.ui/src/org/eclipse/cdt/managedbuilder/internal/ui/actions/BuildFilesHandler.java186
-rw-r--r--build/org.eclipse.cdt.managedbuilder.ui/src/org/eclipse/cdt/managedbuilder/internal/ui/actions/CleanFilesAction.java315
-rw-r--r--build/org.eclipse.cdt.managedbuilder.ui/src/org/eclipse/cdt/managedbuilder/internal/ui/actions/CleanFilesHandler.java145
4 files changed, 331 insertions, 696 deletions
diff --git a/build/org.eclipse.cdt.managedbuilder.ui/src/org/eclipse/cdt/managedbuilder/internal/ui/actions/BuildFilesAction.java b/build/org.eclipse.cdt.managedbuilder.ui/src/org/eclipse/cdt/managedbuilder/internal/ui/actions/BuildFilesAction.java
deleted file mode 100644
index 6983bcc43c6..00000000000
--- a/build/org.eclipse.cdt.managedbuilder.ui/src/org/eclipse/cdt/managedbuilder/internal/ui/actions/BuildFilesAction.java
+++ /dev/null
@@ -1,381 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2006, 2010 Texas Instruments Incorporated and others.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * Texas Instruments - initial API and implementation
- *******************************************************************************/
-package org.eclipse.cdt.managedbuilder.internal.ui.actions;
-
-import java.util.Iterator;
-import java.util.LinkedList;
-import java.util.List;
-
-import org.eclipse.cdt.core.model.ITranslationUnit;
-import org.eclipse.cdt.managedbuilder.core.IManagedBuildInfo;
-import org.eclipse.cdt.managedbuilder.core.ManagedBuildManager;
-import org.eclipse.cdt.managedbuilder.internal.core.GeneratedMakefileBuilder;
-import org.eclipse.cdt.managedbuilder.internal.core.ManagedMakeMessages;
-import org.eclipse.cdt.managedbuilder.makegen.IManagedBuilderMakefileGenerator;
-import org.eclipse.core.resources.ICommand;
-import org.eclipse.core.resources.IFile;
-import org.eclipse.core.resources.IProject;
-import org.eclipse.core.resources.IResource;
-import org.eclipse.core.resources.ResourcesPlugin;
-import org.eclipse.core.runtime.CoreException;
-import org.eclipse.core.runtime.IAdaptable;
-import org.eclipse.core.runtime.IProgressMonitor;
-import org.eclipse.core.runtime.IStatus;
-import org.eclipse.core.runtime.NullProgressMonitor;
-import org.eclipse.core.runtime.Preferences;
-import org.eclipse.core.runtime.jobs.Job;
-import org.eclipse.jface.action.IAction;
-import org.eclipse.jface.viewers.ISelection;
-import org.eclipse.jface.viewers.IStructuredSelection;
-import org.eclipse.ui.ISelectionService;
-import org.eclipse.ui.IWorkbenchWindow;
-import org.eclipse.ui.IWorkbenchWindowActionDelegate;
-import org.eclipse.ui.PlatformUI;
-import org.eclipse.ui.actions.ActionDelegate;
-import org.eclipse.ui.internal.ide.actions.BuildUtilities;
-
-/**
- * Action to build selected files.
- */
-public class BuildFilesAction extends ActionDelegate implements
- IWorkbenchWindowActionDelegate {
-
- /**
- * The workbench window; or <code>null</code> if this action has been
- * <code>dispose</code>d.
- */
- private IWorkbenchWindow workbenchWindow = null;
-
- private IAction action = null;
-
- /**
- *
- */
- public BuildFilesAction() {
- this(PlatformUI.getWorkbench().getActiveWorkbenchWindow());
- }
-
- /**
- * Creates an instance of this action, for use in the given window.
- */
- public BuildFilesAction(IWorkbenchWindow window) {
- if (window == null) {
- throw new IllegalArgumentException();
- }
- this.workbenchWindow = window;
- }
-
- /*
- * (non-Javadoc)
- *
- * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#dispose()
- */
- @Override
- public void dispose() {
- // TODO Auto-generated method stub
-
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.ui.actions.ActionDelegate#init(org.eclipse.jface.action.IAction)
- */
- @Override
- public void init(IAction action) {
- this.action = action;
- update();
- }
-
- /*
- * (non-Javadoc)
- *
- * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#init(org.eclipse.ui.IWorkbenchWindow)
- */
- @Override
- public void init(IWorkbenchWindow window) {
- workbenchWindow = window;
-
- }
-
- /**
- * Helper method that converts an object to the <code>IFile</code>
- * interface. The conversion is a bare cast operation (if the object is
- * instance of <code>IFile</code>, or an adaptation (if the object is
- * instance of <code>IAdaptable</code>).
- *
- * @param object
- * the object to be cast to <code>IFile</code>
- * @return a reference to an IFile corresponding to the object provided, or
- * null if it is not possible to convert the provided object to
- * <code>IFile</code>.
- */
- private IFile convertToIFile(Object object) {
-
- if (object instanceof IFile) {
- return (IFile) object;
- }
-
- if (object instanceof IAdaptable) {
- IAdaptable adaptable = (IAdaptable) object;
- IFile file = (IFile) adaptable.getAdapter(IFile.class);
-
- if (file != null) {
- return file;
- }
- }
-
- // this *should* be redundant now that I've made CElement adaptable to IFile but we'll leave
- // it just to be safe
- if (object instanceof ITranslationUnit) {
- IResource resource = ((ITranslationUnit) object).getResource();
-
- // should be safe to cast to IFile (can't really have a non-file
- // translation unit), but check anyway
- if (resource instanceof IFile) {
- return (IFile) resource;
- }
-
- }
-
- return null;
- }
-
- /**
- * Returns a list of resources currently selected.
- * "Buildable" means buildable by MBS.
- *
- * @return a list of resources
- */
- private List<IFile> getSelectedBuildableFiles() {
-
- List<IFile> files = new LinkedList<IFile>();
-
- ISelectionService selectionService = workbenchWindow
- .getSelectionService();
- ISelection selection = selectionService.getSelection();
-
- if (selection instanceof IStructuredSelection) {
- IStructuredSelection structuredSelection = (IStructuredSelection) selection;
- for (Iterator<?> elements = structuredSelection.iterator(); elements
- .hasNext();) {
- IFile file = convertToIFile(elements.next());
- if (file != null) {
- // we only add files that we can actually build
- IManagedBuildInfo buildInfo = ManagedBuildManager
- .getBuildInfo(file.getProject());
-
- if ((buildInfo != null) && buildInfo.isValid()
- && buildInfo
- .buildsFileType(file.getFileExtension())) {
- files.add(file);
- }
- }
- }
-
- // since we don't allow building folders, there can be no
- // redundancies
- // eliminateRedundancies(resources);
- }
-
- return files;
- }
-
- private static final class BuildFilesJob extends Job {
- private final List<IFile> files;
-
- BuildFilesJob(List<IFile> filesToBuild) {
- super(ManagedMakeMessages.getResourceString("BuildFilesAction.buildingSelectedFiles")); //$NON-NLS-1$
- files = filesToBuild;
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
- */
- @Override
- protected IStatus run(IProgressMonitor monitor) {
- GeneratedMakefileBuilder builder = new GeneratedMakefileBuilder();
- return builder.invokeInternalBuilder(files, monitor);
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.core.runtime.jobs.Job#belongsTo(java.lang.Object)
- */
- @Override
- public boolean belongsTo(Object family) {
- return ResourcesPlugin.FAMILY_MANUAL_BUILD == family;
- }
-
- }
-
- /*
- * (non-Javadoc)
- *
- * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
- */
- @Override
- public void run(IAction action) {
- List<IFile> selectedFiles = getSelectedBuildableFiles();
- Job buildFilesJob = new BuildFilesJob(selectedFiles);
-
- List<IProject> projects = getProjectsToBuild(selectedFiles);
- if (projects != null && !projects.isEmpty()) {
- // Save all resources prior to doing build
- BuildUtilities.saveEditors(projects);
- }
-
- buildFilesJob.schedule();
-
- }
-
- private boolean shouldBeEnabled() {
-
- // fix for bug 139663
- // if build automatically is turned on, then this menu should be turned off as
- // it will trigger the auto build
- Preferences preferences = ResourcesPlugin.getPlugin().getPluginPreferences();
-
- if(preferences.getBoolean(ResourcesPlugin.PREF_AUTO_BUILDING))
- {
- // auto building is on... do not enable the menu
- return false;
- }
-
- ISelectionService selectionService = workbenchWindow
- .getSelectionService();
- ISelection selection = selectionService.getSelection();
-
- if (selection instanceof IStructuredSelection) {
- IStructuredSelection structuredSelection = (IStructuredSelection) selection;
-
- if (structuredSelection.size() <= 0) {
- return false;
- }
-
- for (Iterator<?> elements = structuredSelection.iterator(); elements
- .hasNext();) {
- IFile file = convertToIFile(elements.next());
- if (file != null) {
- // we only add files that we can actually build
- if (!ManagedBuildManager.manages(file.getProject())) {
- return false;
- }
-
- IManagedBuildInfo buildInfo = ManagedBuildManager
- .getBuildInfo(file.getProject());
-
- if (buildInfo == null || !buildInfo.isValid()
- || buildInfo.getDefaultConfiguration() == null
- || !buildInfo.getDefaultConfiguration().isManagedBuildOn()) {
- return false;
- }
-
- IManagedBuilderMakefileGenerator buildfileGenerator = ManagedBuildManager
- .getBuildfileGenerator(buildInfo
- .getDefaultConfiguration());
-
- if (buildfileGenerator == null) {
- return false;
- }
-
- // make sure build file generator is initialized
- buildfileGenerator.initialize(file.getProject(), buildInfo,
- new NullProgressMonitor());
-
- // if we have no build info or we can't build the file, then
- // disable the action
- if (!buildInfo.buildsFileType(file.getFileExtension())
- || buildfileGenerator.isGeneratedResource(file)) {
-
- return false;
-
- }
- }
-
- else {
- return false;
- }
- }
- return true;
- }
-
- return false;
-
- }
-
- /*
- * Updates the enablement state for the action based upon the selection. If
- * the selection corresponds to files buildable by MBS, then the action will
- * be enabled.
- */
- private void update() {
- if (action != null) {
- action.setEnabled(shouldBeEnabled());
- }
- }
-
- /*
- * (non-Javadoc)
- *
- * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction,
- * org.eclipse.jface.viewers.ISelection)
- */
- @Override
- public void selectionChanged(IAction action, ISelection selection) {
- // update state
- update();
- }
-
-
- /*
- * Returns the projects to build.
- * This contains the set of projects which have builders, across all selected resources.
- */
- private List<IProject> getProjectsToBuild(List<IFile> selectedFiles) {
- List<IProject> projectsToBuild = new LinkedList<IProject>();
- for (Iterator<IFile> i = selectedFiles.iterator(); i.hasNext();) {
- IFile file = i.next();
- IProject project = file.getProject();
- if (project != null) {
- if (!projectsToBuild.contains(project)) {
- if (hasBuilder(project)) {
- projectsToBuild.add(project);
- }
- }
- }
- }
-
- return projectsToBuild;
- }
-
- /*
- * Returns whether there are builders configured on the given project.
- *
- * @return <code>true</code> if it has builders,
- * <code>false</code> if not, or if this couldn't be determined
- */
- private boolean hasBuilder(IProject project) {
- if (!project.isAccessible())
- return false;
- try {
- ICommand[] commands = project.getDescription().getBuildSpec();
- if (commands.length > 0) {
- return true;
- }
- } catch (CoreException e) {
- // this method is called when selection changes, so
- // just fall through if it fails.
- // this shouldn't happen anyway, since the list of selected resources
- // has already been checked for accessibility before this is called
- }
- return false;
- }
-
-
-}
diff --git a/build/org.eclipse.cdt.managedbuilder.ui/src/org/eclipse/cdt/managedbuilder/internal/ui/actions/BuildFilesHandler.java b/build/org.eclipse.cdt.managedbuilder.ui/src/org/eclipse/cdt/managedbuilder/internal/ui/actions/BuildFilesHandler.java
new file mode 100644
index 00000000000..7e7b87583e4
--- /dev/null
+++ b/build/org.eclipse.cdt.managedbuilder.ui/src/org/eclipse/cdt/managedbuilder/internal/ui/actions/BuildFilesHandler.java
@@ -0,0 +1,186 @@
+/*******************************************************************************
+ * Copyright (c) 2006, 2014 Texas Instruments Incorporated and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Texas Instruments - initial API and implementation
+ * Sergey Prigogin (Google)
+ *******************************************************************************/
+package org.eclipse.cdt.managedbuilder.internal.ui.actions;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+
+import org.eclipse.cdt.internal.core.AdapterUtil;
+import org.eclipse.cdt.internal.ui.util.AbstractResourceActionHandler;
+import org.eclipse.cdt.managedbuilder.core.IManagedBuildInfo;
+import org.eclipse.cdt.managedbuilder.core.ManagedBuildManager;
+import org.eclipse.cdt.managedbuilder.internal.core.GeneratedMakefileBuilder;
+import org.eclipse.cdt.managedbuilder.internal.core.ManagedMakeMessages;
+import org.eclipse.cdt.managedbuilder.makegen.IManagedBuilderMakefileGenerator;
+import org.eclipse.core.commands.ExecutionEvent;
+import org.eclipse.core.commands.ExecutionException;
+import org.eclipse.core.resources.ICommand;
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.core.runtime.Platform;
+import org.eclipse.core.runtime.jobs.Job;
+import org.eclipse.core.runtime.preferences.IPreferencesService;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.ui.internal.ide.actions.BuildUtilities;
+
+public class BuildFilesHandler extends AbstractResourceActionHandler {
+ @Override
+ public Object execute(ExecutionEvent event) throws ExecutionException {
+ List<IFile> selectedFiles = getSelectedFiles(event);
+ if (selectedFiles.isEmpty())
+ return null;
+
+ Job buildFilesJob = new BuildFilesJob(selectedFiles);
+
+ Collection<IProject> projects = getProjectsToBuild(selectedFiles);
+ BuildUtilities.saveEditors(projects); // Save all resources prior to doing build.
+ buildFilesJob.schedule();
+ return null;
+ }
+
+ @Override
+ public void setEnabled(Object evaluationContext) {
+ super.setEnabled(evaluationContext);
+ setBaseEnabled(shouldBeEnabled());
+ }
+
+ private boolean shouldBeEnabled() {
+ // Fix for bug 139663.
+ // If build automatically is turned on, then this command should be turned off as
+ // it will trigger the auto build.
+ IPreferencesService preferences = Platform.getPreferencesService();
+
+ if (preferences.getBoolean(ResourcesPlugin.PI_RESOURCES, ResourcesPlugin.PREF_AUTO_BUILDING,
+ false, null)) {
+ // Auto building is on - do not enable the command.
+ return false;
+ }
+
+ IStructuredSelection selection = getSelection();
+ if (selection.isEmpty())
+ return false;
+
+ for (Iterator<?> elements = selection.iterator(); elements.hasNext();) {
+ IFile file = AdapterUtil.adapt(elements.next(), IFile.class);
+ if (file == null)
+ return false;
+
+ // We only add files that we can actually build.
+ if (!ManagedBuildManager.manages(file.getProject()))
+ return false;
+
+ IManagedBuildInfo buildInfo = ManagedBuildManager.getBuildInfo(file.getProject());
+
+ if (buildInfo == null || !buildInfo.isValid()
+ || buildInfo.getDefaultConfiguration() == null
+ || !buildInfo.getDefaultConfiguration().isManagedBuildOn()) {
+ return false;
+ }
+
+ IManagedBuilderMakefileGenerator buildfileGenerator =
+ ManagedBuildManager.getBuildfileGenerator(buildInfo.getDefaultConfiguration());
+
+ if (buildfileGenerator == null)
+ return false;
+
+ // Make sure build file generator is initialized.
+ buildfileGenerator.initialize(file.getProject(), buildInfo, new NullProgressMonitor());
+
+ // If we have no build info or we can't build the file, then disable the command.
+ if (!buildInfo.buildsFileType(file.getFileExtension())
+ || buildfileGenerator.isGeneratedResource(file)) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ private List<IFile> getSelectedFiles(ExecutionEvent event) throws ExecutionException {
+ Collection<IResource> selectedResources = getSelectedResources(event);
+ List<IFile> files = new ArrayList<>(selectedResources.size());
+ for (IResource resource : selectedResources) {
+ if (resource instanceof IFile)
+ files.add((IFile) resource);
+ }
+ return files;
+ }
+
+ /*
+ * Returns the projects to build. These are the projects which have builders,
+ * across all selected resources.
+ */
+ private Collection<IProject> getProjectsToBuild(List<IFile> selectedFiles) {
+ Set<IProject> projectsToBuild = new HashSet<IProject>();
+ for (IFile file : selectedFiles) {
+ IProject project = file.getProject();
+ if (!projectsToBuild.contains(project)) {
+ if (hasBuilder(project)) {
+ projectsToBuild.add(project);
+ }
+ }
+ }
+
+ return projectsToBuild;
+ }
+
+ /*
+ * Checks whether there are builders configured on the given project.
+ *
+ * @return {@code true} if it has builders, {@code false} if not, or if this couldn't be
+ * determined
+ */
+ private boolean hasBuilder(IProject project) {
+ if (!project.isAccessible())
+ return false;
+
+ try {
+ ICommand[] commands = project.getDescription().getBuildSpec();
+ if (commands.length > 0)
+ return true;
+ } catch (CoreException e) {
+ // This method is called when selection changes, so just fall through if it
+ // fails. This shouldn't happen anyway, since the list of selected resources
+ // has already been checked for accessibility before this is called.
+ }
+ return false;
+ }
+
+ private static class BuildFilesJob extends Job {
+ private final List<IFile> files;
+
+ BuildFilesJob(List<IFile> filesToBuild) {
+ super(ManagedMakeMessages.getResourceString("BuildFilesHandler.buildingSelectedFiles")); //$NON-NLS-1$
+ files = filesToBuild;
+ }
+
+ @Override
+ protected IStatus run(IProgressMonitor monitor) {
+ GeneratedMakefileBuilder builder = new GeneratedMakefileBuilder();
+ return builder.invokeInternalBuilder(files, monitor);
+ }
+
+ @Override
+ public boolean belongsTo(Object family) {
+ return ResourcesPlugin.FAMILY_MANUAL_BUILD == family;
+ }
+ }
+}
diff --git a/build/org.eclipse.cdt.managedbuilder.ui/src/org/eclipse/cdt/managedbuilder/internal/ui/actions/CleanFilesAction.java b/build/org.eclipse.cdt.managedbuilder.ui/src/org/eclipse/cdt/managedbuilder/internal/ui/actions/CleanFilesAction.java
deleted file mode 100644
index 91d33ec1d8b..00000000000
--- a/build/org.eclipse.cdt.managedbuilder.ui/src/org/eclipse/cdt/managedbuilder/internal/ui/actions/CleanFilesAction.java
+++ /dev/null
@@ -1,315 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2006, 2010 Texas Instruments Incorporated and others.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * Texas Instruments - initial API and implementation
- *******************************************************************************/
-package org.eclipse.cdt.managedbuilder.internal.ui.actions;
-
-import java.util.Iterator;
-import java.util.LinkedList;
-import java.util.List;
-
-import org.eclipse.cdt.core.model.ITranslationUnit;
-import org.eclipse.cdt.managedbuilder.core.IManagedBuildInfo;
-import org.eclipse.cdt.managedbuilder.core.ManagedBuildManager;
-import org.eclipse.cdt.managedbuilder.internal.core.GeneratedMakefileBuilder;
-import org.eclipse.cdt.managedbuilder.internal.core.ManagedMakeMessages;
-import org.eclipse.core.resources.IFile;
-import org.eclipse.core.resources.IResource;
-import org.eclipse.core.resources.ResourcesPlugin;
-import org.eclipse.core.runtime.IAdaptable;
-import org.eclipse.core.runtime.IProgressMonitor;
-import org.eclipse.core.runtime.IStatus;
-import org.eclipse.core.runtime.Preferences;
-import org.eclipse.core.runtime.Status;
-import org.eclipse.core.runtime.jobs.Job;
-import org.eclipse.jface.action.IAction;
-import org.eclipse.jface.viewers.ISelection;
-import org.eclipse.jface.viewers.IStructuredSelection;
-import org.eclipse.ui.ISelectionService;
-import org.eclipse.ui.IWorkbenchWindow;
-import org.eclipse.ui.IWorkbenchWindowActionDelegate;
-import org.eclipse.ui.PlatformUI;
-import org.eclipse.ui.actions.ActionDelegate;
-
-/**
- * Action to clean build output for selected files
- */
-public class CleanFilesAction extends ActionDelegate implements
- IWorkbenchWindowActionDelegate {
- /**
- * The workbench window; or <code>null</code> if this action has been
- * <code>dispose</code>d.
- */
- private IWorkbenchWindow workbenchWindow = null;
-
- private IAction action = null;
-
- /**
- *
- */
- public CleanFilesAction() {
- this(PlatformUI.getWorkbench().getActiveWorkbenchWindow());
- }
-
- /**
- * Creates an instance of this action, for use in the given window.
- */
- public CleanFilesAction(IWorkbenchWindow window) {
- if (window == null) {
- throw new IllegalArgumentException();
- }
- this.workbenchWindow = window;
- }
-
- /*
- * (non-Javadoc)
- *
- * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#dispose()
- */
- @Override
- public void dispose() {
- // TODO Auto-generated method stub
-
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.ui.actions.ActionDelegate#init(org.eclipse.jface.action.IAction)
- */
- @Override
- public void init(IAction action) {
- this.action = action;
- update();
- }
-
- /*
- * (non-Javadoc)
- *
- * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#init(org.eclipse.ui.IWorkbenchWindow)
- */
- @Override
- public void init(IWorkbenchWindow window) {
- workbenchWindow = window;
-
- }
-
- /**
- * Helper method that converts an object to the <code>IFile</code>
- * interface. The conversion is a bare cast operation (if the object is
- * instance of <code>IFile</code>, or an adaptation (if the object is
- * instance of <code>IAdaptable</code>).
- *
- * @param object
- * the object to be cast to <code>IFile</code>
- * @return a reference to an IFile corresponding to the object provided, or
- * null if it is not possible to convert the provided object to
- * <code>IFile</code>.
- */
- private IFile convertToIFile(Object object) {
-
- if (object instanceof IFile) {
- return (IFile) object;
- }
-
- if (object instanceof IAdaptable) {
- IAdaptable adaptable = (IAdaptable) object;
- IFile file = (IFile) adaptable.getAdapter(IFile.class);
-
- if (file != null) {
- return file;
- }
- }
-
- // this *should* be redundant now that I've made CElement adaptable to IFile but we'll leave
- // it just to be safe
- if (object instanceof ITranslationUnit) {
- IResource resource = ((ITranslationUnit) object).getResource();
-
- // should be safe to cast to IFile (can't really have a non-file
- // translation unit), but check anyway
- if (resource instanceof IFile) {
- return (IFile) resource;
- }
-
- }
-
- return null;
- }
-
- /**
- * Returns a list of buildable resources currently selected.
- * "Buildable" means buildable by MBS.
- *
- * @return a list of resources
- */
- private List<IFile> getSelectedBuildableFiles() {
-
- List<IFile> files = new LinkedList<IFile>();
-
- ISelectionService selectionService = workbenchWindow
- .getSelectionService();
- ISelection selection = selectionService.getSelection();
-
- if (selection instanceof IStructuredSelection) {
- IStructuredSelection structuredSelection = (IStructuredSelection) selection;
- for (Iterator<?> elements = structuredSelection.iterator(); elements
- .hasNext();) {
- IFile file = convertToIFile(elements.next());
- if (file != null) {
- // we only add files that we can actually build
- IManagedBuildInfo buildInfo = ManagedBuildManager
- .getBuildInfo(file.getProject());
-
- if (buildInfo.buildsFileType(file.getFileExtension())) {
- files.add(file);
- }
- }
- }
-
- // since we don't allow building folders, there can be no
- // redundancies
- // eliminateRedundancies(resources);
- }
-
- return files;
- }
-
- private static final class CleanFilesJob extends Job {
- private final List<IFile> files;
-
- private CleanFilesJob(List<IFile> filesToBuild) {
- super(ManagedMakeMessages.getResourceString("CleanFilesAction.cleaningFiles")); //$NON-NLS-1$
- files = filesToBuild;
- }
-
- /*
- * (non-Javadoc)
- *
- * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
- */
- @Override
- protected IStatus run(IProgressMonitor monitor) {
- synchronized (getClass()) {
- if (monitor.isCanceled()) {
- return Status.CANCEL_STATUS;
- }
- Job[] buildJobs = Job.getJobManager().find(
- ResourcesPlugin.FAMILY_MANUAL_BUILD);
- for (int i = 0; i < buildJobs.length; i++) {
- Job curr = buildJobs[i];
- if ((curr != this) && (curr instanceof CleanFilesJob)) {
- curr.cancel(); // cancel all other build jobs of our
- // kind
-
- }
- }
- }
-
- GeneratedMakefileBuilder builder = new GeneratedMakefileBuilder();
- return builder.cleanFiles(files, monitor);
- }
-
- @Override
- public boolean belongsTo(Object family) {
- return ResourcesPlugin.FAMILY_MANUAL_BUILD == family;
- }
- }
-
- /*
- * (non-Javadoc)
- *
- * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
- */
- @Override
- public void run(IAction action) {
- List<IFile> selectedFiles = getSelectedBuildableFiles();
- CleanFilesJob job = new CleanFilesJob(selectedFiles);
- job.schedule();
- }
-
- private boolean shouldBeEnabled() {
-
-
- // fix for Bugzilla 139663
- // if build automatically is turned on, then this menu should be turned off as
- // it will trigger the auto build
- Preferences preferences = ResourcesPlugin.getPlugin().getPluginPreferences();
-
- if(preferences.getBoolean(ResourcesPlugin.PREF_AUTO_BUILDING))
- {
- // auto building is on... do not enable the menu
- return false;
- }
-
- ISelectionService selectionService = workbenchWindow
- .getSelectionService();
- ISelection selection = selectionService.getSelection();
-
- if (selection instanceof IStructuredSelection) {
- IStructuredSelection structuredSelection = (IStructuredSelection) selection;
-
- if (structuredSelection.size() <= 0) {
- return false;
- }
-
- for (Iterator<?> elements = structuredSelection.iterator(); elements
- .hasNext();) {
- IFile file = convertToIFile(elements.next());
- if (file != null) {
- // we only add files that we can actually build
- if (!ManagedBuildManager.manages(file.getProject())) {
- return false;
- }
-
- IManagedBuildInfo buildInfo = ManagedBuildManager
- .getBuildInfo(file.getProject());
-
- // if we have no build info or we can't build the file, then
- // disable the action
- if ((buildInfo == null) || !buildInfo.isValid()
- || !buildInfo.buildsFileType(file
- .getFileExtension())) {
- return false;
- }
- }
-
- else {
- return false;
- }
-
- }
-
- return true;
- }
-
- return false;
- }
-
- /*
- * Updates the enablement state for the action based upon the selection. If
- * the selection corresponds to files buildable by MBS, then the action will
- * be enabled.
- */
- private void update() {
- if (action != null) {
- action.setEnabled(shouldBeEnabled());
- }
- }
-
- /*
- * (non-Javadoc)
- *
- * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction,
- * org.eclipse.jface.viewers.ISelection)
- */
- @Override
- public void selectionChanged(IAction action, ISelection selection) {
- update();
- }
-
-}
diff --git a/build/org.eclipse.cdt.managedbuilder.ui/src/org/eclipse/cdt/managedbuilder/internal/ui/actions/CleanFilesHandler.java b/build/org.eclipse.cdt.managedbuilder.ui/src/org/eclipse/cdt/managedbuilder/internal/ui/actions/CleanFilesHandler.java
new file mode 100644
index 00000000000..5e12e8bf17c
--- /dev/null
+++ b/build/org.eclipse.cdt.managedbuilder.ui/src/org/eclipse/cdt/managedbuilder/internal/ui/actions/CleanFilesHandler.java
@@ -0,0 +1,145 @@
+/*******************************************************************************
+ * Copyright (c) 2006, 2014 Texas Instruments Incorporated and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Texas Instruments - initial API and implementation
+ * Sergey Prigogin (Google)
+ *******************************************************************************/
+package org.eclipse.cdt.managedbuilder.internal.ui.actions;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+
+import org.eclipse.cdt.internal.core.AdapterUtil;
+import org.eclipse.cdt.internal.ui.util.AbstractResourceActionHandler;
+import org.eclipse.cdt.managedbuilder.core.IManagedBuildInfo;
+import org.eclipse.cdt.managedbuilder.core.ManagedBuildManager;
+import org.eclipse.cdt.managedbuilder.internal.core.GeneratedMakefileBuilder;
+import org.eclipse.cdt.managedbuilder.internal.core.ManagedMakeMessages;
+import org.eclipse.core.commands.ExecutionEvent;
+import org.eclipse.core.commands.ExecutionException;
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Platform;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.core.runtime.jobs.Job;
+import org.eclipse.core.runtime.preferences.IPreferencesService;
+import org.eclipse.jface.viewers.IStructuredSelection;
+
+/**
+ * Action to clean build output for selected files
+ */
+public class CleanFilesHandler extends AbstractResourceActionHandler {
+ @Override
+ public Object execute(ExecutionEvent event) throws ExecutionException {
+ List<IFile> selectedFiles = getSelectedBuildableFiles(event);
+ if (selectedFiles.isEmpty())
+ return null;
+
+ CleanFilesJob job = new CleanFilesJob(selectedFiles);
+ job.schedule();
+ return null;
+ }
+
+ /**
+ * Returns a list of buildable resources currently selected.
+ * "Buildable" means buildable by MBS.
+ */
+ private List<IFile> getSelectedBuildableFiles(ExecutionEvent event) throws ExecutionException {
+ Collection<IResource> selectedResources = getSelectedResources(event);
+ List<IFile> files = new ArrayList<>(selectedResources.size());
+ for (IResource resource : selectedResources) {
+ // We only add files that we can actually build.
+ if (resource instanceof IFile && isBuildable(resource)) {
+ files.add((IFile) resource);
+ }
+ }
+ return files;
+ }
+
+ @Override
+ public void setEnabled(Object evaluationContext) {
+ super.setEnabled(evaluationContext);
+ setBaseEnabled(shouldBeEnabled());
+ }
+
+ private boolean shouldBeEnabled() {
+ // Fix for bug 139663.
+ // If build automatically is turned on, then this command should be turned off as
+ // it will trigger the auto build.
+ IPreferencesService preferences = Platform.getPreferencesService();
+
+ if (preferences.getBoolean(ResourcesPlugin.PI_RESOURCES, ResourcesPlugin.PREF_AUTO_BUILDING,
+ false, null)) {
+ // Auto building is on - do not enable the command.
+ return false;
+ }
+
+ IStructuredSelection selection = getSelection();
+ if (selection.isEmpty())
+ return false;
+
+ for (Iterator<?> elements = selection.iterator(); elements.hasNext();) {
+ IFile file = AdapterUtil.adapt(elements.next(), IFile.class);
+ if (file == null)
+ return false;
+
+ // We only add files that we can actually build.
+ if (!isBuildable(file))
+ return false;
+ }
+
+ return true;
+ }
+
+ private static boolean isBuildable(IResource resource) {
+ IProject project = resource.getProject();
+ if (!ManagedBuildManager.manages(project))
+ return false;
+ IManagedBuildInfo buildInfo = ManagedBuildManager.getBuildInfo(project);
+ return buildInfo != null && buildInfo.isValid()
+ && buildInfo.buildsFileType(resource.getFileExtension());
+ }
+
+ private static class CleanFilesJob extends Job {
+ private final List<IFile> files;
+
+ private CleanFilesJob(List<IFile> filesToBuild) {
+ super(ManagedMakeMessages.getResourceString("CleanFilesHandler.cleaningFiles")); //$NON-NLS-1$
+ files = filesToBuild;
+ }
+
+ @Override
+ protected IStatus run(IProgressMonitor monitor) {
+ synchronized (getClass()) {
+ if (monitor.isCanceled()) {
+ return Status.CANCEL_STATUS;
+ }
+ Job[] buildJobs = Job.getJobManager().find(ResourcesPlugin.FAMILY_MANUAL_BUILD);
+ for (Job job : buildJobs) {
+ if ((job != this) && (job instanceof CleanFilesJob)) {
+ job.cancel(); // Cancel all other build jobs of our kind.
+ }
+ }
+ }
+
+ GeneratedMakefileBuilder builder = new GeneratedMakefileBuilder();
+ return builder.cleanFiles(files, monitor);
+ }
+
+ @Override
+ public boolean belongsTo(Object family) {
+ return ResourcesPlugin.FAMILY_MANUAL_BUILD == family;
+ }
+ }
+}

Back to the top