From ef6d28343dddb893db0eeebd02e881af58b661e5 Mon Sep 17 00:00:00 2001 From: David Dubrow Date: Mon, 28 Jun 2010 12:57:55 +0000 Subject: [PATCH] Bug 317707 convert resource configs context menu to handlers to allow hiding via capabilities --- core/org.eclipse.cdt.ui/META-INF/MANIFEST.MF | 2 +- core/org.eclipse.cdt.ui/plugin.properties | 3 + core/org.eclipse.cdt.ui/plugin.xml | 121 +++++--- .../ui/actions/DeleteResConfigsAction.java | 2 + .../ui/actions/DeleteResConfigsHandler.java | 237 ++++++++++++++++ .../ui/actions/ExcludeFromBuildAction.java | 2 + .../ui/actions/ExcludeFromBuildHandler.java | 262 ++++++++++++++++++ 7 files changed, 592 insertions(+), 37 deletions(-) create mode 100644 core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/actions/DeleteResConfigsHandler.java create mode 100644 core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/actions/ExcludeFromBuildHandler.java diff --git a/core/org.eclipse.cdt.ui/META-INF/MANIFEST.MF b/core/org.eclipse.cdt.ui/META-INF/MANIFEST.MF index 470a909ed4e..3d8e95d2d62 100644 --- a/core/org.eclipse.cdt.ui/META-INF/MANIFEST.MF +++ b/core/org.eclipse.cdt.ui/META-INF/MANIFEST.MF @@ -2,7 +2,7 @@ Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: %pluginName Bundle-SymbolicName: org.eclipse.cdt.ui; singleton:=true -Bundle-Version: 5.2.100.qualifier +Bundle-Version: 5.3.0.qualifier Bundle-Activator: org.eclipse.cdt.ui.CUIPlugin Bundle-Vendor: %providerName Bundle-Localization: plugin diff --git a/core/org.eclipse.cdt.ui/plugin.properties b/core/org.eclipse.cdt.ui/plugin.properties index 503d3b19c14..e358445b590 100644 --- a/core/org.eclipse.cdt.ui/plugin.properties +++ b/core/org.eclipse.cdt.ui/plugin.properties @@ -574,3 +574,6 @@ ShiftLeftAction.label= S&hift Left excluded-file.name = C/C++ Files Excluded from Build templatesViewName= Templates + +deleteConfigsCommand.name = Reset to Default +excludeCommand.name = Exclude from Build \ No newline at end of file diff --git a/core/org.eclipse.cdt.ui/plugin.xml b/core/org.eclipse.cdt.ui/plugin.xml index c3f72edd652..f6f08b62bcd 100644 --- a/core/org.eclipse.cdt.ui/plugin.xml +++ b/core/org.eclipse.cdt.ui/plugin.xml @@ -1055,42 +1055,6 @@ - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/actions/DeleteResConfigsAction.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/actions/DeleteResConfigsAction.java index ff380754258..57accc9b018 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/actions/DeleteResConfigsAction.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/actions/DeleteResConfigsAction.java @@ -48,7 +48,9 @@ import org.eclipse.cdt.internal.ui.actions.ActionMessages; /** * Action which deletes resource description. (If resource description is missing * one from parent is normally used) + * @deprecated as of CDT 8.0 now using {@link DeleteResConfigsHandler} */ +@Deprecated public class DeleteResConfigsAction implements IWorkbenchWindowPulldownDelegate2, IObjectActionDelegate { diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/actions/DeleteResConfigsHandler.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/actions/DeleteResConfigsHandler.java new file mode 100644 index 00000000000..f357874ac59 --- /dev/null +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/actions/DeleteResConfigsHandler.java @@ -0,0 +1,237 @@ +/******************************************************************************* + * Copyright (c) 2007, 2010 Intel Corporation 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: + * Intel Corporation - initial API and implementation + * Nokia - converted from action to handler + *******************************************************************************/ +package org.eclipse.cdt.ui.actions; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; + +import org.eclipse.core.commands.AbstractHandler; +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.runtime.CoreException; +import org.eclipse.core.runtime.IPath; +import org.eclipse.jface.text.ITextSelection; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.jface.viewers.IStructuredContentProvider; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.jface.viewers.LabelProvider; +import org.eclipse.jface.viewers.Viewer; +import org.eclipse.jface.window.Window; +import org.eclipse.ui.IEditorInput; +import org.eclipse.ui.IEditorPart; +import org.eclipse.ui.ISources; +import org.eclipse.ui.IWorkbenchPage; +import org.eclipse.ui.IWorkbenchWindow; +import org.eclipse.ui.PlatformUI; +import org.eclipse.ui.dialogs.ListSelectionDialog; +import org.eclipse.ui.handlers.HandlerUtil; + +import org.eclipse.cdt.core.model.CoreModel; +import org.eclipse.cdt.core.model.ICContainer; +import org.eclipse.cdt.core.model.ICElement; +import org.eclipse.cdt.core.model.ITranslationUnit; +import org.eclipse.cdt.core.settings.model.ICConfigurationDescription; +import org.eclipse.cdt.core.settings.model.ICProjectDescription; +import org.eclipse.cdt.core.settings.model.ICResourceDescription; +import org.eclipse.cdt.ui.CUIPlugin; +import org.eclipse.cdt.ui.newui.AbstractPage; + +import org.eclipse.cdt.internal.ui.actions.ActionMessages; + + +/** + * Handler for command that deletes resource description. (If resource description is missing + * one from parent is normally used) + * @since 5.3 + */ +public class DeleteResConfigsHandler extends AbstractHandler { + + protected ArrayList objects; + private ArrayList outData; + + @Override + public void setEnabled(Object context) { + ISelection selection = getSelection(context); + setEnabledFromSelection(selection); + } + + protected ISelection getSelection(Object context) { + Object s = HandlerUtil.getVariable(context, ISources.ACTIVE_MENU_SELECTION_NAME); + if (s instanceof ISelection) { + return (ISelection) s; + } + return null; + } + + public void setEnabledFromSelection(ISelection selection) { + objects = null; + + if (!selection.isEmpty()) { + // case for context menu + Object[] obs = null; + if (selection instanceof IStructuredSelection) { + obs = ((IStructuredSelection)selection).toArray(); + } + else if (selection instanceof ITextSelection) { + IFile file = getFileFromActiveEditor(); + if (file != null) + obs = Collections.singletonList(file).toArray(); + } + if (obs != null && obs.length > 0) { + for (int i=0; i(); + objects.add(res); + break; // stop configurations scanning + } + } + } + } + } + } + setBaseEnabled(objects != null); + } + + private IFile getFileFromActiveEditor() { + IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); + if (window != null) { + IWorkbenchPage page = window.getActivePage(); + if (page != null) { + IEditorPart editor = page.getActiveEditor(); + if (editor != null) { + IEditorInput input = editor.getEditorInput(); + if (input != null) + return (IFile) input.getAdapter(IFile.class); + } + } + } + return null; + } + + public Object execute(ExecutionEvent event) throws ExecutionException { + openDialog(); + return null; + } + + private void openDialog() { + if (objects == null || objects.size() == 0) return; + // create list of configurations to delete + + ListSelectionDialog dialog = new ListSelectionDialog( + CUIPlugin.getActiveWorkbenchShell(), + objects, + createSelectionDialogContentProvider(), + new LabelProvider() {}, ActionMessages.DeleteResConfigsAction_0); + dialog.setTitle(ActionMessages.DeleteResConfigsAction_1); + if (dialog.open() == Window.OK) { + Object[] selected = dialog.getResult(); + if (selected != null && selected.length > 0) { + for (Object element : selected) { + ((ResCfgData)element).delete(); + AbstractPage.updateViews(((ResCfgData)element).res); + } + } + } + } + + // Stores data for resource description with its "parents". + class ResCfgData { + IResource res; + ICProjectDescription prjd; + ICConfigurationDescription cfgd; + ICResourceDescription rdesc; + + public ResCfgData(IResource res2, ICProjectDescription prjd2, + ICConfigurationDescription cfgd2, ICResourceDescription rdesc2) { + res = res2; prjd = prjd2; cfgd = cfgd2; rdesc = rdesc2; + } + + // performs deletion + public void delete() { + try { + cfgd.removeResourceDescription(rdesc); + CoreModel.getDefault().setProjectDescription(res.getProject(), prjd); + } catch (CoreException e) {} + } + @Override + public String toString() { + return "[" + cfgd.getName() + "] for " + res.getName(); //$NON-NLS-1$ //$NON-NLS-2$ + } + } + + + private IStructuredContentProvider createSelectionDialogContentProvider() { + outData = null; + + return new IStructuredContentProvider() { + + public Object[] getElements(Object inputElement) { + if (outData != null) return outData.toArray(); + + outData = new ArrayList(); + List ls = (List)inputElement; + Iterator it = ls.iterator(); + IProject proj = null; + ICProjectDescription prjd = null; + ICConfigurationDescription[] cfgds = null; + + // creating list of all res descs for all objects + while (it.hasNext()) { + IResource res = (IResource)it.next(); + IPath path = res.getProjectRelativePath(); + if (res.getProject() != proj) { + proj = res.getProject(); + prjd = CoreModel.getDefault().getProjectDescription(proj); + cfgds = prjd.getConfigurations(); + } + if (cfgds != null) { + for (ICConfigurationDescription cfgd : cfgds) { + ICResourceDescription rd = cfgd.getResourceDescription(path, true); + if (rd != null) + outData.add(new ResCfgData(res, prjd, cfgd, rd)); + } + } + } + return outData.toArray(); + } + public void dispose() {} + public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {} + }; + } + +} diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/actions/ExcludeFromBuildAction.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/actions/ExcludeFromBuildAction.java index f4ae8796205..a6cebe94537 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/actions/ExcludeFromBuildAction.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/actions/ExcludeFromBuildAction.java @@ -50,7 +50,9 @@ import org.eclipse.cdt.internal.ui.actions.ActionMessages; /** * Action which excludes resources from build. + * @deprecated as of CDT 8.0 now using {@link ExcludeFromBuildHandler} */ +@Deprecated public class ExcludeFromBuildAction implements IWorkbenchWindowPulldownDelegate2, IObjectActionDelegate { diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/actions/ExcludeFromBuildHandler.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/actions/ExcludeFromBuildHandler.java new file mode 100644 index 00000000000..443a02c048f --- /dev/null +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/actions/ExcludeFromBuildHandler.java @@ -0,0 +1,262 @@ +/******************************************************************************* + * Copyright (c) 2007, 2010 Intel Corporation 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: + * Intel Corporation - initial API and implementation + * Nokia - converted from action to handler + *******************************************************************************/ +package org.eclipse.cdt.ui.actions; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Iterator; + +import org.eclipse.core.commands.AbstractHandler; +import org.eclipse.core.commands.ExecutionEvent; +import org.eclipse.core.commands.ExecutionException; +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IFolder; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.IResource; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IPath; +import org.eclipse.jface.text.ITextSelection; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.jface.viewers.IStructuredContentProvider; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.jface.viewers.LabelProvider; +import org.eclipse.jface.viewers.Viewer; +import org.eclipse.jface.window.Window; +import org.eclipse.ui.IEditorInput; +import org.eclipse.ui.IEditorPart; +import org.eclipse.ui.ISources; +import org.eclipse.ui.IWorkbenchPage; +import org.eclipse.ui.IWorkbenchWindow; +import org.eclipse.ui.PlatformUI; +import org.eclipse.ui.dialogs.ListSelectionDialog; +import org.eclipse.ui.handlers.HandlerUtil; + +import org.eclipse.cdt.core.model.CoreModel; +import org.eclipse.cdt.core.model.ICContainer; +import org.eclipse.cdt.core.model.ICElement; +import org.eclipse.cdt.core.model.ICProject; +import org.eclipse.cdt.core.model.ITranslationUnit; +import org.eclipse.cdt.core.settings.model.ICConfigurationDescription; +import org.eclipse.cdt.core.settings.model.ICProjectDescription; +import org.eclipse.cdt.core.settings.model.ICSourceEntry; +import org.eclipse.cdt.core.settings.model.util.CDataUtil; +import org.eclipse.cdt.ui.CUIPlugin; +import org.eclipse.cdt.ui.newui.AbstractPage; +import org.eclipse.cdt.ui.newui.UIMessages; + +import org.eclipse.cdt.internal.ui.actions.ActionMessages; + +/** + * Handler for command that excludes resources from build. + * @since 5.3 + */ +public class ExcludeFromBuildHandler extends AbstractHandler { + + protected ArrayList objects; + protected ArrayList cfgNames; + + @Override + public void setEnabled(Object context) { + ISelection selection = getSelection(context); + setEnabledFromSelection(selection); + } + + protected ISelection getSelection(Object context) { + Object s = HandlerUtil.getVariable(context, ISources.ACTIVE_MENU_SELECTION_NAME); + if (s instanceof ISelection) { + return (ISelection) s; + } + return null; + } + + public void setEnabledFromSelection(ISelection selection) { + objects = null; + cfgNames = null; + boolean cfgsOK = true; + + if (!selection.isEmpty()) { + // case for context menu + Object[] obs = null; + if (selection instanceof IStructuredSelection) { + obs = ((IStructuredSelection)selection).toArray(); + } + else if (selection instanceof ITextSelection) { + IFile file = getFileFromActiveEditor(); + if (file != null) + obs = Collections.singletonList(file).toArray(); + } + if (obs != null && obs.length > 0) { + for (int i=0; i(); + objects.add(res); + if (cfgNames == null) { + cfgNames = new ArrayList(cfgds.length); + for (int j=0; j it = objects.iterator(); + while (it.hasNext()) { + IResource res = it.next(); + ICConfigurationDescription[] cfgds = getCfgsRead(res); + IPath p = res.getFullPath(); + for (int i=0; i lst = new ArrayList(); + for (int i=0; i 0) + dialog.setInitialElementSelections(lst); + + if (dialog.open() == Window.OK) { + Object[] selected = dialog.getResult(); // may be empty + Iterator it2 = objects.iterator(); + while (it2.hasNext()) { + IResource res = it2.next(); + IProject p = res.getProject(); + if (!p.isOpen()) continue; + // get writable description + ICProjectDescription prjd = CoreModel.getDefault().getProjectDescription(p, true); + if (prjd == null) continue; + ICConfigurationDescription[] cfgds = prjd.getConfigurations(); + for (int i=0; i