From ecb373f340957bc9c382bea6f1868af11d70a96b Mon Sep 17 00:00:00 2001 From: Caroline Rieder Date: Fri, 24 May 2013 15:47:18 +0200 Subject: [PATCH] V.3 - Bug 405239 - Index actions are only available within context menu The provided patch contributes the Index actions to the global Project menu using the command/handler extension points. Project related actions are enabled when: - When an ICProject, an ICContainer or an ITranslationUnit is selected in the Project Explorer and the Project Explorer has the focus - When a file of an ICProject is open in the Editor and the Editor has the focus File related action is enabled when: - When an ITranslationUnit is selected in the Project Explorer and the Project Explorer has the focus - When an ITranslationUnit is open in the Editor and the Editor has the focus Change-Id: I11f05e2b2f0c8f1652b0ee7e655aacacd03c7fde Signed-off-by: Caroline Rieder Reviewed-on: https://git.eclipse.org/r/13123 Reviewed-by: Anton Leherbauer IP-Clean: Anton Leherbauer Tested-by: Anton Leherbauer --- core/org.eclipse.cdt.ui/plugin.xml | 61 ++++----- .../ui/actions/AbstractUpdateIndexAction.java | 109 ++++++---------- .../actions/AbstractUpdateIndexHandler.java | 47 +++++++ .../ui/actions/CreateParserLogAction.java | 94 ++++++-------- .../ui/actions/CreateParserLogHandler.java | 11 ++ .../ui/actions/FreshenAllFilesHandler.java | 17 +-- .../ui/actions/IndexActionsEnabledTester.java | 118 ------------------ .../ui/actions/RebuildIndexAction.java | 104 ++------------- .../ui/actions/RebuildIndexHandler.java | 17 +-- .../UpdateIndexWithModifiedFilesHandler.java | 17 +-- .../UpdateUnresolvedIncludesHandler.java | 19 +-- .../FindUnresolvedIncludesHandler.java | 21 +--- .../FindUnresolvedIncludesProjectAction.java | 88 ++----------- 13 files changed, 201 insertions(+), 522 deletions(-) create mode 100644 core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/actions/AbstractUpdateIndexHandler.java delete mode 100644 core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/actions/IndexActionsEnabledTester.java diff --git a/core/org.eclipse.cdt.ui/plugin.xml b/core/org.eclipse.cdt.ui/plugin.xml index 3f930088a74..59e45e7e271 100644 --- a/core/org.eclipse.cdt.ui/plugin.xml +++ b/core/org.eclipse.cdt.ui/plugin.xml @@ -1407,16 +1407,19 @@ @@ -1438,6 +1441,7 @@ @@ -1456,11 +1460,13 @@ objectClass="org.eclipse.cdt.core.model.ICProject"> @@ -1485,26 +1491,31 @@ @@ -4491,44 +4502,26 @@ - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + - - - - tuSelection= new ArrayList(); - if(fSelection instanceof IStructuredSelection) { - IStructuredSelection cElements= SelectionConverter.convertSelectionToCElements(fSelection); - for (Iterator i= cElements.iterator(); i.hasNext();) { - Object o= i.next(); - if (o instanceof ICProject || o instanceof ICContainer || o instanceof ITranslationUnit) { - tuSelection.add((ICElement) o); - } - } - } else if(fSelection instanceof ITextSelection) { - IProject project = EditorUtility.getProjectForActiveEditor(); - if(project != null) { - ICProject cproject = CCorePlugin.getDefault().getCoreModel().create(project); - if(cproject != null) { - tuSelection.add(cproject); - } - } - } - ICElement[] tuArray= tuSelection.toArray(new ICElement[tuSelection.size()]); + ICProject[] projects = getSelectedCProjects(); + doRun(projects); + } + + protected void doRun(ICProject[] projects) { try { - CCorePlugin.getIndexManager().update(tuArray, getUpdateOptions()); + CCorePlugin.getIndexManager().update(projects, getUpdateOptions()); } catch (CoreException e) { CUIPlugin.log(e); } @@ -84,47 +62,40 @@ public abstract class AbstractUpdateIndexAction implements IObjectActionDelegate * @since 4.0 */ abstract protected int getUpdateOptions(); - - /** - * @see IActionDelegate#selectionChanged(IAction, ISelection) - */ - public void selectionChanged(ISelection selection) { - fSelection= selection; - isEnabled = false; - - if(selection == null || selection instanceof ITextSelection) { - IProject project = EditorUtility.getProjectForActiveEditor(); - if(project != null) { - isEnabled = CoreModel.hasCNature(project); - } - } else if(selection instanceof IStructuredSelection) { - Object selectedElement = ((IStructuredSelection)selection).getFirstElement(); - if(selectedElement instanceof IProject) { - isEnabled = CoreModel.hasCNature((IProject)selectedElement) && ((IProject)selectedElement).isOpen(); - } else if(selectedElement instanceof ITranslationUnit) { - isEnabled = true; - } - } - } - - @Override - public void init(IWorkbenchWindow window) { - } - - @Override - public void dispose() { - } - - /** - * @return {@code true} if the action is enabled or {@code false} otherwise. - */ - public boolean isEnabled() { - selectionChanged(SelectionUtil.getActiveSelection()); - return isEnabled; - } @Override public void selectionChanged(IAction action, ISelection selection) { - selectionChanged(selection); + fSelection = selection; + } + + public boolean isEnabledFor(ISelection selection) { + selectionChanged(null, selection); + ICProject[] project = getSelectedCProjects(); + return project.length > 0; + } + + protected ICProject[] getSelectedCProjects() { + ArrayList tuSelection= new ArrayList(); + if(fSelection instanceof IStructuredSelection) { + IStructuredSelection resources = SelectionConverter.convertSelectionToResources(fSelection); + for (Iterator i= resources.iterator(); i.hasNext();) { + Object o= i.next(); + if(o instanceof IResource) { + ICProject cproject= CCorePlugin.getDefault().getCoreModel().create(((IResource)o).getProject()); + if(cproject != null) { + tuSelection.add(cproject); + } + } + } + } else if(fSelection == null || fSelection instanceof ITextSelection) { + IProject project = EditorUtility.getProjectForActiveEditor(); + if(project != null) { + ICProject cproject= CCorePlugin.getDefault().getCoreModel().create(project); + if(cproject != null) { + tuSelection.add(cproject); + } + } + } + return tuSelection.toArray(new ICProject[tuSelection.size()]); } } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/actions/AbstractUpdateIndexHandler.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/actions/AbstractUpdateIndexHandler.java new file mode 100644 index 00000000000..c0e16c959e4 --- /dev/null +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/actions/AbstractUpdateIndexHandler.java @@ -0,0 +1,47 @@ +/******************************************************************************* + * Copyright (c) 2013 Wind River Systems, Inc. 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: + * Wind River Systems - Initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.internal.ui.actions; + +import org.eclipse.core.commands.AbstractHandler; +import org.eclipse.core.commands.ExecutionEvent; +import org.eclipse.core.commands.ExecutionException; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.ui.IWorkbenchPart; +import org.eclipse.ui.handlers.HandlerUtil; + +import org.eclipse.cdt.internal.ui.util.SelectionUtil; + +/** + * Abstract handler for {@link org.eclipse.cdt.internal.ui.actions.AbstractUpdateIndexAction} + */ +public abstract class AbstractUpdateIndexHandler extends AbstractHandler { + + abstract protected AbstractUpdateIndexAction getAction(); + + @Override + public Object execute(ExecutionEvent event) throws ExecutionException { + ISelection selection = HandlerUtil.getCurrentSelection(event); + IWorkbenchPart part = HandlerUtil.getActivePart(event); + getAction().setActivePart(null, part); + getAction().selectionChanged(null, selection); + getAction().run(null); + return null; + } + + /* (non-Javadoc) + * @see org.eclipse.core.commands.AbstractHandler#setEnabled(java.lang.Object) + */ + @Override + public void setEnabled(Object evaluationContext) { + ISelection selection = SelectionUtil.getActiveSelection(); + setBaseEnabled(getAction().isEnabledFor(selection)); + } +} diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/actions/CreateParserLogAction.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/actions/CreateParserLogAction.java index 43ca7b818f4..aaba5697f97 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/actions/CreateParserLogAction.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/actions/CreateParserLogAction.java @@ -42,7 +42,6 @@ import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.osgi.util.NLS; import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.FileDialog; -import org.eclipse.ui.IActionDelegate; import org.eclipse.ui.IEditorInput; import org.eclipse.ui.IEditorPart; import org.eclipse.ui.IEditorReference; @@ -51,7 +50,6 @@ import org.eclipse.ui.IWorkbenchPage; import org.eclipse.ui.IWorkbenchPart; import org.eclipse.ui.IWorkbenchPartSite; import org.eclipse.ui.IWorkbenchWindow; -import org.eclipse.ui.IWorkbenchWindowActionDelegate; import org.eclipse.ui.PlatformUI; import org.eclipse.ui.ide.IDE; @@ -95,12 +93,10 @@ import org.eclipse.cdt.internal.core.pdom.indexer.ProjectIndexerInputAdapter; import org.eclipse.cdt.internal.ui.editor.ASTProvider; import org.eclipse.cdt.internal.ui.editor.CEditor; -import org.eclipse.cdt.internal.ui.util.SelectionUtil; @SuppressWarnings("nls") -public class CreateParserLogAction implements IObjectActionDelegate, IWorkbenchWindowActionDelegate { +public class CreateParserLogAction implements IObjectActionDelegate { private static final String INDENT = " "; - private boolean isEnabled; private static final class MyVisitor extends ASTVisitor { List fProblems= new ArrayList(); @@ -168,29 +164,20 @@ public class CreateParserLogAction implements IObjectActionDelegate, IWorkbenchW workingCopies.add((IWorkingCopy) inputElement); } } - ArrayList tuSelection= new ArrayList(); - String title = ActionMessages.CreateParserLogAction_title; - if(fSelection instanceof IStructuredSelection) { - IStructuredSelection cElements= SelectionConverter.convertSelectionToCElements(fSelection); - Iterator i= cElements.iterator(); - while (i.hasNext()) { - Object o= i.next(); - if (o instanceof ITranslationUnit) { - tuSelection.add(convertToWorkingCopy((ITranslationUnit) o, workingCopies)); - } - } - } else if(fSelection instanceof ITextSelection) { - IWorkingCopy tu = getTranslationUnitForSelectedEditorInput(); - if(tu != null) { - tuSelection.add(tu); + + ArrayList tuSelection = getSelectedTranslationUnits(); + for(int i = 0; i < tuSelection.size(); i++) { + if(!(tuSelection.get(i) instanceof IWorkingCopy)) { + tuSelection.set(i, convertToWorkingCopy(tuSelection.get(i), workingCopies)); } } - + ITranslationUnit[] tuArray= tuSelection.toArray(new ITranslationUnit[tuSelection.size()]); if (tuArray.length == 0) { return; } FileDialog dlg= new FileDialog(fSite.getShell(), SWT.SAVE); + String title = ActionMessages.CreateParserLogAction_title; dlg.setText(title); dlg.setFilterExtensions(new String[]{"*.log"}); String path= null; @@ -496,27 +483,13 @@ public class CreateParserLogAction implements IObjectActionDelegate, IWorkbenchW out.println(); } } - - /** - * @see IActionDelegate#selectionChanged(IAction, ISelection) - */ - public void selectionChanged(ISelection selection) { - fSelection= selection; - isEnabled = false; - - if(selection == null || selection instanceof ITextSelection) { - IWorkingCopy tu = getTranslationUnitForSelectedEditorInput(); - if(tu != null) { - isEnabled = true; - } - } else if(selection instanceof IStructuredSelection) { - if(((IStructuredSelection)selection).getFirstElement() instanceof ITranslationUnit) { - isEnabled = true; - } - } - } - private IWorkingCopy getTranslationUnitForSelectedEditorInput() { + @Override + public void selectionChanged(IAction action, ISelection selection) { + fSelection = selection; + } + + public IWorkingCopy getTranslationUnitForSelectedEditorInput() { IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); if(window != null) { IWorkbenchPart workbenchPart = window.getPartService().getActivePart(); @@ -531,24 +504,29 @@ public class CreateParserLogAction implements IObjectActionDelegate, IWorkbenchW return null; } - @Override - public void init(IWorkbenchWindow window) { + public boolean isEnabledFor(ISelection selection) { + selectionChanged(null, selection); + ArrayList tus = getSelectedTranslationUnits(); + return tus.size() > 0; } - @Override - public void dispose() { - } - - /** - * @return {@code true} if the action is enabled or {@code false} otherwise. - */ - public boolean isEnabled() { - selectionChanged(SelectionUtil.getActiveSelection()); - return isEnabled; - } - - @Override - public void selectionChanged(IAction action, ISelection selection) { - selectionChanged(selection); + private ArrayList getSelectedTranslationUnits() { + ArrayList tuSelection= new ArrayList(); + if(fSelection instanceof IStructuredSelection) { + IStructuredSelection cElements= SelectionConverter.convertSelectionToCElements(fSelection); + Iterator i= cElements.iterator(); + while (i.hasNext()) { + Object o= i.next(); + if (o instanceof ITranslationUnit) { + tuSelection.add((ITranslationUnit)o); + } + } + } else if(fSelection == null || fSelection instanceof ITextSelection) { + IWorkingCopy tu = getTranslationUnitForSelectedEditorInput(); + if(tu != null) { + tuSelection.add(tu); + } + } + return tuSelection; } } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/actions/CreateParserLogHandler.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/actions/CreateParserLogHandler.java index ebe905777b3..e718bf8445f 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/actions/CreateParserLogHandler.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/actions/CreateParserLogHandler.java @@ -17,6 +17,8 @@ import org.eclipse.jface.viewers.ISelection; import org.eclipse.ui.IWorkbenchPart; import org.eclipse.ui.handlers.HandlerUtil; +import org.eclipse.cdt.internal.ui.util.SelectionUtil; + /** * Handler for {@link org.eclipse.cdt.internal.ui.actions.CreateParserLogAction} * @@ -36,4 +38,13 @@ public class CreateParserLogHandler extends AbstractHandler { createParserLogAction.run(null); return null; } + + /* (non-Javadoc) + * @see org.eclipse.core.commands.AbstractHandler#setEnabled(java.lang.Object) + */ + @Override + public void setEnabled(Object evaluationContext) { + ISelection selection = SelectionUtil.getActiveSelection(); + setBaseEnabled(createParserLogAction.isEnabledFor(selection)); + } } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/actions/FreshenAllFilesHandler.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/actions/FreshenAllFilesHandler.java index 568275f83da..45de61dc102 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/actions/FreshenAllFilesHandler.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/actions/FreshenAllFilesHandler.java @@ -10,12 +10,6 @@ *******************************************************************************/ package org.eclipse.cdt.internal.ui.actions; -import org.eclipse.core.commands.AbstractHandler; -import org.eclipse.core.commands.ExecutionEvent; -import org.eclipse.core.commands.ExecutionException; -import org.eclipse.jface.viewers.ISelection; -import org.eclipse.ui.IWorkbenchPart; -import org.eclipse.ui.handlers.HandlerUtil; /** * Handler for {@link org.eclipse.cdt.internal.ui.actions.FreshenIndexAction} @@ -23,17 +17,12 @@ import org.eclipse.ui.handlers.HandlerUtil; * @noextend This class is not intended to be subclassed by clients. * @noinstantiate This class is not intended to be instantiated by clients. */ -public class FreshenAllFilesHandler extends AbstractHandler { +public class FreshenAllFilesHandler extends AbstractUpdateIndexHandler { private final FreshenIndexAction freshenIndexAction = new FreshenIndexAction(); @Override - public Object execute(ExecutionEvent event) throws ExecutionException { - ISelection selection = HandlerUtil.getCurrentSelection(event); - IWorkbenchPart part = HandlerUtil.getActivePart(event); - freshenIndexAction.setActivePart(null, part); - freshenIndexAction.selectionChanged(null, selection); - freshenIndexAction.run(null); - return null; + public AbstractUpdateIndexAction getAction() { + return freshenIndexAction; } } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/actions/IndexActionsEnabledTester.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/actions/IndexActionsEnabledTester.java deleted file mode 100644 index e6cb1d2736f..00000000000 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/actions/IndexActionsEnabledTester.java +++ /dev/null @@ -1,118 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2013 Wind River Systems, Inc. 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: - * Wind River Systems - Initial API and implementation - *******************************************************************************/ -package org.eclipse.cdt.internal.ui.actions; - -import org.eclipse.core.expressions.PropertyTester; -import org.eclipse.jface.viewers.ISelection; -import org.eclipse.jface.viewers.IStructuredSelection; -import org.eclipse.jface.viewers.StructuredSelection; -import org.eclipse.ui.IEditorPart; -import org.eclipse.ui.ISelectionListener; -import org.eclipse.ui.ISelectionService; -import org.eclipse.ui.IWindowListener; -import org.eclipse.ui.IWorkbenchPart; -import org.eclipse.ui.IWorkbenchWindow; -import org.eclipse.ui.ide.ResourceUtil; - -import org.eclipse.cdt.internal.ui.search.actions.FindUnresolvedIncludesProjectAction; - -public class IndexActionsEnabledTester extends PropertyTester implements ISelectionListener, IWindowListener { - - private static RebuildIndexAction fRebuildIndexAction = new RebuildIndexAction(); - private static CreateParserLogAction fCreateParserLogAction = new CreateParserLogAction(); - private static FreshenIndexAction fFreshenAllFiles = new FreshenIndexAction(); - private static UpdateUnresolvedIncludesAction fUpdateUnresolvedIncludes = new UpdateUnresolvedIncludesAction(); - private static UpdateIndexWithModifiedFilesAction fUpdateWithModifiedFiles = new UpdateIndexWithModifiedFilesAction(); - private static FindUnresolvedIncludesProjectAction fFindUnresolvedIncludes = new FindUnresolvedIncludesProjectAction(); - - /* (non-Javadoc) - * @see org.eclipse.core.expressions.IPropertyTester#test(java.lang.Object, java.lang.String, java.lang.Object[], java.lang.Object) - */ - @Override - public boolean test(Object receiver, String property, Object[] args, Object expectedValue) - { - if(property.equals("enabled")) { //$NON-NLS-1$ - return fRebuildIndexAction.isEnabled() - || fCreateParserLogAction.isEnabled() - || fFreshenAllFiles.isEnabled() - || fUpdateUnresolvedIncludes.isEnabled() - || fUpdateWithModifiedFiles.isEnabled() - || fFindUnresolvedIncludes.isEnabled(); - } else if(property.equals("rebuild")) { //$NON-NLS-1$ - return fRebuildIndexAction.isEnabled(); - } else if(property.equals("log")) { //$NON-NLS-1$ - return fCreateParserLogAction.isEnabled(); - } else if(property.equals("freshen")) { //$NON-NLS-1$ - return fFreshenAllFiles.isEnabled(); - } else if(property.equals("updateUnresolvedIncludes")) { //$NON-NLS-1$ - return fUpdateUnresolvedIncludes.isEnabled(); - } else if(property.equals("udpateWithModifiedFiles")) { //$NON-NLS-1$ - return fUpdateWithModifiedFiles.isEnabled(); - } else if(property.equals("findUnresolvedIncludes")) { //$NON-NLS-1$ - return fFindUnresolvedIncludes.isEnabled(); - } - return false; - } - - private IStructuredSelection getSelectedItem(Object part, Object selection) { - if((selection != null) && (selection instanceof IStructuredSelection)) { - return (IStructuredSelection)selection; - } - if((part != null) && (part instanceof IEditorPart)) { - Object selItem = null; - selItem = ResourceUtil.getResource(((IEditorPart)part).getEditorInput()); - if(selItem != null) { - return new StructuredSelection(selItem); - } - } - return StructuredSelection.EMPTY; - } - - @Override - public void selectionChanged(IWorkbenchPart part, ISelection selection) { - IStructuredSelection sel = getSelectedItem(part, selection); - fRebuildIndexAction.selectionChanged(sel); - fCreateParserLogAction.selectionChanged(sel); - fFreshenAllFiles.selectionChanged(sel); - fUpdateUnresolvedIncludes.selectionChanged(sel); - fUpdateWithModifiedFiles.selectionChanged(sel); - fFindUnresolvedIncludes.selectionChanged(sel); - } - - @Override - public void windowActivated(IWorkbenchWindow window) { - ISelectionService selectionService = window.getSelectionService(); - if (selectionService != null) { - ISelection sel = selectionService.getSelection(); - selectionChanged(null, sel); - } - } - - @Override - public void windowDeactivated(IWorkbenchWindow window) { - } - - @Override - public void windowClosed(IWorkbenchWindow window) { - ISelectionService selectionService = window.getSelectionService(); - if (selectionService != null) { - selectionService.removeSelectionListener(this); - } - } - - @Override - public void windowOpened(IWorkbenchWindow window) { - ISelectionService selectionService = window.getSelectionService(); - if (selectionService != null) { - selectionService.addSelectionListener(this); - } - } -} diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/actions/RebuildIndexAction.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/actions/RebuildIndexAction.java index 502e470530c..a21c823e17a 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/actions/RebuildIndexAction.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/actions/RebuildIndexAction.java @@ -10,108 +10,22 @@ *******************************************************************************/ package org.eclipse.cdt.internal.ui.actions; -import java.util.ArrayList; -import java.util.Iterator; - -import org.eclipse.core.resources.IProject; -import org.eclipse.jface.action.IAction; -import org.eclipse.jface.text.ITextSelection; -import org.eclipse.jface.viewers.ISelection; -import org.eclipse.jface.viewers.IStructuredSelection; -import org.eclipse.ui.IActionDelegate; -import org.eclipse.ui.IObjectActionDelegate; -import org.eclipse.ui.IWorkbenchPart; -import org.eclipse.ui.IWorkbenchWindow; -import org.eclipse.ui.IWorkbenchWindowActionDelegate; - import org.eclipse.cdt.core.CCorePlugin; -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.internal.ui.util.EditorUtility; -import org.eclipse.cdt.internal.ui.util.SelectionUtil; - -public class RebuildIndexAction implements IObjectActionDelegate, IWorkbenchWindowActionDelegate { - private ISelection fSelection; - private boolean isEnabled; - - @Override - public void setActivePart(IAction action, IWorkbenchPart targetPart) { - } - - @Override - public void run(IAction action) { - if(!(fSelection instanceof IStructuredSelection) && !(fSelection instanceof ITextSelection)) { - return; - } - ArrayList tuSelection= new ArrayList(); - if(fSelection instanceof IStructuredSelection) { - IStructuredSelection cElements= SelectionConverter.convertSelectionToCElements(fSelection); - for (Iterator i= cElements.iterator(); i.hasNext();) { - Object o= i.next(); - if (o instanceof ICProject || o instanceof ICContainer || o instanceof ITranslationUnit) { - tuSelection.add((ICElement) o); - } - } - } else if(fSelection instanceof ITextSelection) { - IProject project = EditorUtility.getProjectForActiveEditor(); - if(project != null) { - ICProject cproject= CCorePlugin.getDefault().getCoreModel().create(project); - if(cproject != null) { - tuSelection.add(cproject); - } - } - } - ICElement[] tuArray= tuSelection.toArray(new ICElement[tuSelection.size()]); - for (ICElement elem : tuArray) { - if(elem instanceof ICProject) { - CCorePlugin.getIndexManager().reindex((ICProject) elem); - } - } - } - - /** - * @see IActionDelegate#selectionChanged(IAction, ISelection) - */ - public void selectionChanged(ISelection selection) { - fSelection= selection; - isEnabled = false; - if(selection == null || selection instanceof ITextSelection) { - IProject project = EditorUtility.getProjectForActiveEditor(); - if(project != null) { - isEnabled = CoreModel.hasCNature(project); - } - } else if(selection instanceof IStructuredSelection) { - Object selectedElement = ((IStructuredSelection)selection).getFirstElement(); - if(selectedElement instanceof IProject) { - isEnabled = CoreModel.hasCNature((IProject)selectedElement) && ((IProject)selectedElement).isOpen(); - } else if(selectedElement instanceof ITranslationUnit) { - isEnabled = true; - } - } - } - - @Override - public void init(IWorkbenchWindow window) { - } +public class RebuildIndexAction extends AbstractUpdateIndexAction { @Override - public void dispose() { - } - - /** - * @return {@code true} if the action is enabled or {@code false} otherwise. - */ - public boolean isEnabled() { - selectionChanged(SelectionUtil.getActiveSelection()); - return isEnabled; + protected void doRun(ICProject[] projects) { + for (ICProject proj : projects) { + if(proj != null) { + CCorePlugin.getIndexManager().reindex(proj); + } + } } @Override - public void selectionChanged(IAction action, ISelection selection) { - selectionChanged(selection); + protected int getUpdateOptions() { + return 0; } } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/actions/RebuildIndexHandler.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/actions/RebuildIndexHandler.java index 88e6e980afc..f0ec4f000dc 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/actions/RebuildIndexHandler.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/actions/RebuildIndexHandler.java @@ -10,12 +10,6 @@ *******************************************************************************/ package org.eclipse.cdt.internal.ui.actions; -import org.eclipse.core.commands.AbstractHandler; -import org.eclipse.core.commands.ExecutionEvent; -import org.eclipse.core.commands.ExecutionException; -import org.eclipse.jface.viewers.ISelection; -import org.eclipse.ui.IWorkbenchPart; -import org.eclipse.ui.handlers.HandlerUtil; /** * Handler for {@link org.eclipse.cdt.internal.ui.actions.RebuildIndexAction} @@ -23,17 +17,12 @@ import org.eclipse.ui.handlers.HandlerUtil; * @noextend This class is not intended to be subclassed by clients. * @noinstantiate This class is not intended to be instantiated by clients. */ -public class RebuildIndexHandler extends AbstractHandler { +public class RebuildIndexHandler extends AbstractUpdateIndexHandler { private final RebuildIndexAction rebuildIndexAction = new RebuildIndexAction(); @Override - public Object execute(ExecutionEvent event) throws ExecutionException { - ISelection selection = HandlerUtil.getCurrentSelection(event); - IWorkbenchPart part = HandlerUtil.getActivePart(event); - rebuildIndexAction.setActivePart(null, part); - rebuildIndexAction.selectionChanged(null, selection); - rebuildIndexAction.run(null); - return null; + public AbstractUpdateIndexAction getAction() { + return rebuildIndexAction; } } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/actions/UpdateIndexWithModifiedFilesHandler.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/actions/UpdateIndexWithModifiedFilesHandler.java index 8b3f5a42cfe..76cc151d513 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/actions/UpdateIndexWithModifiedFilesHandler.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/actions/UpdateIndexWithModifiedFilesHandler.java @@ -10,12 +10,6 @@ *******************************************************************************/ package org.eclipse.cdt.internal.ui.actions; -import org.eclipse.core.commands.AbstractHandler; -import org.eclipse.core.commands.ExecutionEvent; -import org.eclipse.core.commands.ExecutionException; -import org.eclipse.jface.viewers.ISelection; -import org.eclipse.ui.IWorkbenchPart; -import org.eclipse.ui.handlers.HandlerUtil; /** * Handler for {@link org.eclipse.cdt.internal.ui.actions.UpdateIndexWithModifiedFilesAction} @@ -23,17 +17,12 @@ import org.eclipse.ui.handlers.HandlerUtil; * @noextend This class is not intended to be subclassed by clients. * @noinstantiate This class is not intended to be instantiated by clients. */ -public class UpdateIndexWithModifiedFilesHandler extends AbstractHandler { +public class UpdateIndexWithModifiedFilesHandler extends AbstractUpdateIndexHandler { private final UpdateIndexWithModifiedFilesAction updateAction = new UpdateIndexWithModifiedFilesAction(); @Override - public Object execute(ExecutionEvent event) throws ExecutionException { - ISelection selection = HandlerUtil.getCurrentSelection(event); - IWorkbenchPart part = HandlerUtil.getActivePart(event); - updateAction.setActivePart(null, part); - updateAction.selectionChanged(null, selection); - updateAction.run(null); - return null; + public AbstractUpdateIndexAction getAction() { + return updateAction; } } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/actions/UpdateUnresolvedIncludesHandler.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/actions/UpdateUnresolvedIncludesHandler.java index 96995e9d7d6..fcbad2ed24d 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/actions/UpdateUnresolvedIncludesHandler.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/actions/UpdateUnresolvedIncludesHandler.java @@ -10,12 +10,6 @@ *******************************************************************************/ package org.eclipse.cdt.internal.ui.actions; -import org.eclipse.core.commands.AbstractHandler; -import org.eclipse.core.commands.ExecutionEvent; -import org.eclipse.core.commands.ExecutionException; -import org.eclipse.jface.viewers.ISelection; -import org.eclipse.ui.IWorkbenchPart; -import org.eclipse.ui.handlers.HandlerUtil; /** * Handler for {@link org.eclipse.cdt.internal.ui.actions.UpdateIndexWithModifiedFilesAction} @@ -23,17 +17,12 @@ import org.eclipse.ui.handlers.HandlerUtil; * @noextend This class is not intended to be subclassed by clients. * @noinstantiate This class is not intended to be instantiated by clients. */ -public class UpdateUnresolvedIncludesHandler extends AbstractHandler { +public class UpdateUnresolvedIncludesHandler extends AbstractUpdateIndexHandler { - private final UpdateUnresolvedIncludesAction udpateAction = new UpdateUnresolvedIncludesAction(); + private final UpdateUnresolvedIncludesAction updateAction = new UpdateUnresolvedIncludesAction(); @Override - public Object execute(ExecutionEvent event) throws ExecutionException { - ISelection selection = HandlerUtil.getCurrentSelection(event); - IWorkbenchPart part = HandlerUtil.getActivePart(event); - udpateAction.setActivePart(null, part); - udpateAction.selectionChanged(null, selection); - udpateAction.run(null); - return null; + public AbstractUpdateIndexAction getAction() { + return updateAction; } } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/actions/FindUnresolvedIncludesHandler.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/actions/FindUnresolvedIncludesHandler.java index 6d9115acb90..b8b47acab40 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/actions/FindUnresolvedIncludesHandler.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/actions/FindUnresolvedIncludesHandler.java @@ -10,12 +10,8 @@ *******************************************************************************/ package org.eclipse.cdt.internal.ui.search.actions; -import org.eclipse.core.commands.AbstractHandler; -import org.eclipse.core.commands.ExecutionEvent; -import org.eclipse.core.commands.ExecutionException; -import org.eclipse.jface.viewers.ISelection; -import org.eclipse.ui.IWorkbenchPart; -import org.eclipse.ui.handlers.HandlerUtil; +import org.eclipse.cdt.internal.ui.actions.AbstractUpdateIndexAction; +import org.eclipse.cdt.internal.ui.actions.AbstractUpdateIndexHandler; /** * Handler for {@link org.eclipse.cdt.internal.ui.search.actions.FindUnresolvedIncludesProjectAction} @@ -23,17 +19,12 @@ import org.eclipse.ui.handlers.HandlerUtil; * @noextend This class is not intended to be subclassed by clients. * @noinstantiate This class is not intended to be instantiated by clients. */ -public class FindUnresolvedIncludesHandler extends AbstractHandler { +public class FindUnresolvedIncludesHandler extends AbstractUpdateIndexHandler { - private final FindUnresolvedIncludesProjectAction rebuildIndexAction= new FindUnresolvedIncludesProjectAction(); + private final FindUnresolvedIncludesProjectAction findUnresolvedIncludesAction = new FindUnresolvedIncludesProjectAction(); @Override - public Object execute(ExecutionEvent event) throws ExecutionException { - ISelection selection = HandlerUtil.getCurrentSelection(event); - IWorkbenchPart part = HandlerUtil.getActivePart(event); - rebuildIndexAction.setActivePart(null, part); - rebuildIndexAction.selectionChanged(null, selection); - rebuildIndexAction.run(null); - return null; + public AbstractUpdateIndexAction getAction() { + return findUnresolvedIncludesAction; } } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/actions/FindUnresolvedIncludesProjectAction.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/actions/FindUnresolvedIncludesProjectAction.java index 929c5b2cedd..45fd549cfd8 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/actions/FindUnresolvedIncludesProjectAction.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/actions/FindUnresolvedIncludesProjectAction.java @@ -10,74 +10,37 @@ *******************************************************************************/ package org.eclipse.cdt.internal.ui.search.actions; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - -import org.eclipse.core.resources.IProject; import org.eclipse.jface.action.IAction; -import org.eclipse.jface.text.ITextSelection; -import org.eclipse.jface.viewers.ISelection; -import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.search.ui.ISearchQuery; import org.eclipse.search.ui.NewSearchUI; -import org.eclipse.ui.IActionDelegate; -import org.eclipse.ui.IObjectActionDelegate; import org.eclipse.ui.IWorkbenchPart; import org.eclipse.ui.IWorkbenchSite; -import org.eclipse.ui.IWorkbenchWindow; -import org.eclipse.ui.IWorkbenchWindowActionDelegate; -import org.eclipse.cdt.core.CCorePlugin; -import org.eclipse.cdt.core.model.CoreModel; import org.eclipse.cdt.core.model.ICProject; -import org.eclipse.cdt.core.model.ITranslationUnit; -import org.eclipse.cdt.internal.ui.actions.SelectionConverter; +import org.eclipse.cdt.internal.ui.actions.AbstractUpdateIndexAction; import org.eclipse.cdt.internal.ui.search.CSearchMessages; import org.eclipse.cdt.internal.ui.search.CSearchUnresolvedIncludesQuery; -import org.eclipse.cdt.internal.ui.util.EditorUtility; -import org.eclipse.cdt.internal.ui.util.SelectionUtil; import org.eclipse.cdt.internal.ui.util.StatusLineHandler; /** * Searches projects for unresolved includes. * Could be extended to work on resource selections. */ -public class FindUnresolvedIncludesProjectAction implements IObjectActionDelegate, IWorkbenchWindowActionDelegate { - private ISelection fSelection; +public class FindUnresolvedIncludesProjectAction extends AbstractUpdateIndexAction { private IWorkbenchSite fSite; - private boolean isEnabled; public FindUnresolvedIncludesProjectAction() { } @Override - public void run(IAction action) { - List projects = new ArrayList(); - if(fSelection instanceof IStructuredSelection) { - IStructuredSelection cElements = SelectionConverter.convertSelectionToCElements(fSelection); - for (Iterator i = cElements.iterator(); i.hasNext();) { - Object o= i.next(); - if (o instanceof ICProject) { - projects.add((ICProject) o); - } - } - } else if(fSelection instanceof ITextSelection) { - IProject project = EditorUtility.getProjectForActiveEditor(); - if(project != null) { - ICProject cproject = CCorePlugin.getDefault().getCoreModel().create(project); - if(cproject != null) { - projects.add(cproject); - } - } - } - if (projects.isEmpty()) { + protected void doRun(ICProject[] projects) { + if (projects.length == 0) { StatusLineHandler.showStatusLineMessage(fSite, CSearchMessages.CSearchOperation_operationUnavailable_message); return; } - ISearchQuery searchJob= new CSearchUnresolvedIncludesQuery(projects.toArray(new ICProject[projects.size()])); + ISearchQuery searchJob= new CSearchUnresolvedIncludesQuery(projects); StatusLineHandler.clearStatusLine(fSite); NewSearchUI.activateSearchResultView(); @@ -89,45 +52,8 @@ public class FindUnresolvedIncludesProjectAction implements IObjectActionDelegat fSite= targetPart.getSite(); } - /** - * @see IActionDelegate#selectionChanged(IAction, ISelection) - */ - public void selectionChanged(ISelection selection) { - fSelection= selection; - isEnabled = false; - if(selection == null || selection instanceof ITextSelection) { - IProject project = EditorUtility.getProjectForActiveEditor(); - if(project != null) { - isEnabled = CoreModel.hasCNature(project); - } - } else if(selection instanceof IStructuredSelection) { - Object selectedElement = ((IStructuredSelection)selection).getFirstElement(); - if(selectedElement instanceof IProject) { - isEnabled = CoreModel.hasCNature((IProject)selectedElement) && ((IProject)selectedElement).isOpen(); - } else if(selectedElement instanceof ITranslationUnit) { - isEnabled = true; - } - } - } - @Override - public void init(IWorkbenchWindow window) { - } - - @Override - public void dispose() { - } - - /** - * @return {@code true} if the action is enabled or {@code false} otherwise. - */ - public boolean isEnabled() { - selectionChanged(SelectionUtil.getActiveSelection()); - return isEnabled; - } - - @Override - public void selectionChanged(IAction action, ISelection selection) { - selectionChanged(selection); + protected int getUpdateOptions() { + return 0; } }