diff options
Diffstat (limited to 'core/org.eclipse.cdt.ui/src/org/eclipse')
4 files changed, 284 insertions, 54 deletions
diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/expressions/CPropertyTester.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/expressions/CPropertyTester.java new file mode 100644 index 00000000000..ecf152b3170 --- /dev/null +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/expressions/CPropertyTester.java @@ -0,0 +1,64 @@ +/******************************************************************************* + * Copyright (c) 2007, 2014 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 + * Sergey Prigogin (Google) + *******************************************************************************/ +package org.eclipse.cdt.internal.ui.expressions; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.eclipse.core.expressions.PropertyTester; +import org.eclipse.core.resources.IFile; +import org.eclipse.core.runtime.Platform; +import org.eclipse.core.runtime.preferences.IPreferencesService; + +import org.eclipse.cdt.core.model.CoreModel; +import org.eclipse.cdt.core.model.ITranslationUnit; + +/** + * Property tester to test expressions in plugin.xml. Tests following expressions: + * 1. Checks whether given object is a source file. Usage: + * <test property="org.eclipse.cdt.ui.isSource"/> + * 2. Checks value of a preference. Usage: + * <test property="org.eclipse.cdt.ui.checkPreference" value="org.eclipse.cdt.ui:properties.export.page.enable=true"/> + */ +public class CPropertyTester extends PropertyTester { + private static final String KEY_SRC = "isSource"; //$NON-NLS-1$ + private static final String KEY_PREF = "checkPreference"; //$NON-NLS-1$ + private static final Pattern PREFERENCE_PATTERN = Pattern.compile("(.*)[/:](.*)=(.*)"); //$NON-NLS-1$ + + @Override + public boolean test(Object receiver, String property, Object[] args, Object expectedValue) { + if (KEY_SRC.equals(property)) { + if (receiver instanceof ITranslationUnit) { + return ((ITranslationUnit) receiver).isSourceUnit(); + } else if (receiver instanceof IFile) { + IFile file = (IFile) receiver; + return CoreModel.isValidSourceUnitName(file.getProject(), file.getName()); + } + } else if (KEY_PREF.equals(property) && expectedValue instanceof String) { + Matcher matcher = PREFERENCE_PATTERN.matcher((String) expectedValue); + if (matcher.matches()) { + String pluginId = matcher.group(1); + String preference = matcher.group(2); + String wantedValue = matcher.group(3); + + IPreferencesService preferences = Platform.getPreferencesService(); + String actualValue = preferences.getString(pluginId, preference, null, null); + if (wantedValue != null) { + return wantedValue.equals(actualValue) || (actualValue == null && wantedValue.equals("false")); //$NON-NLS-1$ + } else { + return actualValue != null; + } + } + } + return false; + } +} diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/expressions/ResourcePropertyTester.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/expressions/ResourcePropertyTester.java new file mode 100644 index 00000000000..7764746df76 --- /dev/null +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/expressions/ResourcePropertyTester.java @@ -0,0 +1,44 @@ +/******************************************************************************* + * Copyright (c) 2014 Google, 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: + * Sergey Prigogin (Google) - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.internal.ui.expressions; + +import org.eclipse.core.expressions.PropertyTester; +import org.eclipse.core.resources.ICommand; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.IProjectDescription; +import org.eclipse.core.resources.IResource; +import org.eclipse.core.runtime.CoreException; + +public class ResourcePropertyTester extends PropertyTester { + private static final String PROP_PROJECT_BUILDER = "projectBuilder"; //$NON-NLS-1$ + + @Override + public boolean test(Object receiver, String property, Object[] args, Object expectedValue) { + if (!(receiver instanceof IResource) || args.length != 0) + return false; + if (property.equals(PROP_PROJECT_BUILDER)) { + IProject project = ((IResource) receiver).getProject(); + if (project == null) + return false; + try { + IProjectDescription description = project.getDescription(); + ICommand[] buildSpec = description.getBuildSpec(); + for (ICommand builder : buildSpec) { + if (builder.getBuilderName().equals(expectedValue)) + return true; + } + } catch (CoreException e) { + // Ignore to return false; + } + } + return false; + } +} diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/util/AbstractResourceActionHandler.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/util/AbstractResourceActionHandler.java new file mode 100644 index 00000000000..172e9b570cb --- /dev/null +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/util/AbstractResourceActionHandler.java @@ -0,0 +1,174 @@ +/******************************************************************************* + * Copyright (c) 2014 Google, 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: + * Sergey Prigogin (Google) - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.internal.ui.util; + +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Set; + +import org.eclipse.core.commands.AbstractHandler; +import org.eclipse.core.commands.ExecutionEvent; +import org.eclipse.core.commands.ExecutionException; +import org.eclipse.core.expressions.IEvaluationContext; +import org.eclipse.core.resources.IResource; +import org.eclipse.core.resources.mapping.ResourceMapping; +import org.eclipse.core.resources.mapping.ResourceTraversal; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.jface.text.ITextSelection; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.jface.viewers.StructuredSelection; +import org.eclipse.ui.IEditorInput; +import org.eclipse.ui.ISources; +import org.eclipse.ui.IWorkbenchWindow; +import org.eclipse.ui.PlatformUI; +import org.eclipse.ui.handlers.HandlerUtil; +import org.eclipse.ui.handlers.IHandlerService; +import org.eclipse.ui.ide.ResourceUtil; + +import org.eclipse.cdt.ui.CUIPlugin; + +import org.eclipse.cdt.internal.core.AdapterUtil; + +/** + * Base class for command handlers operating on resources. + */ +public abstract class AbstractResourceActionHandler extends AbstractHandler { + private IEvaluationContext evaluationContext; + private IStructuredSelection selection; + + public void setSelection(ISelection selection) { + this.selection = convertSelection(null, selection); + } + + @Override + public void setEnabled(Object evaluationContext) { + this.evaluationContext = (IEvaluationContext) evaluationContext; + } + + protected IStructuredSelection getSelection() { + if (selection == null) { + selection = convertSelection(evaluationContext, null); + } + return selection; + } + + protected IStructuredSelection getSelection(ExecutionEvent event) throws ExecutionException { + Object selection = HandlerUtil.getActiveMenuSelection(event); + if (selection == null) { + selection = HandlerUtil.getCurrentSelectionChecked(event); + } + if (selection instanceof ITextSelection) { + IEditorInput editorInput = HandlerUtil.getActiveEditorInputChecked(event); + IResource resource = ResourceUtil.getResource(editorInput); + if (resource != null) { + return new StructuredSelection(resource); + } + + resource = ResourceUtil.getFile(editorInput); + if (resource != null) { + return new StructuredSelection(resource); + } + } + if (selection instanceof IStructuredSelection) { + return (IStructuredSelection) selection; + } + return StructuredSelection.EMPTY; + } + + private static IStructuredSelection convertSelection(IEvaluationContext context, + Object selection) { + if (selection == null) { + if (context == null) { + return StructuredSelection.EMPTY; + } + selection = context.getVariable(ISources.ACTIVE_MENU_SELECTION_NAME); + if (!(selection instanceof ISelection)) { + selection = context.getVariable(ISources.ACTIVE_CURRENT_SELECTION_NAME); + } + } + if (selection instanceof ITextSelection) { + if (context == null) { + context = getEvaluationContext(); + } + IResource resource = + ResourceUtil.getResource(context.getVariable(ISources.ACTIVE_EDITOR_INPUT_NAME)); + if (resource != null) { + return new StructuredSelection(resource); + } + } + if (selection instanceof IStructuredSelection) { + return (IStructuredSelection) selection; + } + return StructuredSelection.EMPTY; + } + + private static IEvaluationContext getEvaluationContext() { + IWorkbenchWindow activeWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); + if (activeWindow == null) { + return null; + } + IHandlerService service = (IHandlerService) activeWindow.getService(IHandlerService.class); + return service.getCurrentState(); + } + + /** + * Returns the selected resources. + */ + protected Collection<IResource> getSelectedResources() { + return getSelectedResources(getSelection()); + } + + /** + * Returns the selected resources. + */ + protected Collection<IResource> getSelectedResources(ExecutionEvent event) throws ExecutionException { + IStructuredSelection selection = getSelection(event); + return getSelectedResources(selection); + } + + private static Collection<IResource> getSelectedResources(IStructuredSelection selection) { + Set<IResource> result = new LinkedHashSet<IResource>(); + for (Object obj : selection.toList()) { + IResource resource = AdapterUtil.adapt(obj, IResource.class); + if (resource != null) { + result.add(resource); + } else { + result.addAll(extractResourcesFromMapping(obj)); + } + } + return result; + } + + /** + * Extracts resources associated with a {@link ResourceMapping}. + * + * @param obj an object adaptable to {@link ResourceMapping} + * @return a list of resources associated with the mapping + */ + private static List<IResource> extractResourcesFromMapping(Object obj) { + ResourceMapping mapping = AdapterUtil.adapt(obj, ResourceMapping.class); + if (mapping != null) { + try { + ResourceTraversal[] traversals = mapping.getTraversals(null, null); + for (ResourceTraversal traversal : traversals) { + return Arrays.asList(traversal.getResources()); + } + } catch (CoreException e) { + CUIPlugin.log(e); + } + } + return Collections.emptyList(); + } +} diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/newui/PropertyTester.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/newui/PropertyTester.java index cd2775223d5..90ba32b556b 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/newui/PropertyTester.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/newui/PropertyTester.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007, 2009 Intel Corporation and others. + * Copyright (c) 2007, 2014 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 @@ -10,65 +10,13 @@ *******************************************************************************/ package org.eclipse.cdt.ui.newui; -import java.util.Arrays; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -import org.eclipse.core.resources.IFile; -import org.eclipse.core.runtime.preferences.IEclipsePreferences; -import org.eclipse.core.runtime.preferences.InstanceScope; -import org.osgi.service.prefs.BackingStoreException; - -import org.eclipse.cdt.core.model.CoreModel; -import org.eclipse.cdt.core.model.ITranslationUnit; -import org.eclipse.cdt.ui.CUIPlugin; - /** - * Property tester to test expressions in plugin.xml. Tests following expressions: - * 1. Checks whether given object is a source file. Usage: - * <test property="org.eclipse.cdt.ui.isSource"/> - * 2. Checks value of a preference. Usage: - * <test property="org.eclipse.cdt.ui.checkPreference" value="org.eclipse.cdt.ui:properties.export.page.enable=true"/> - * + * @deprecated Doesn't do anything useful. * @noextend This class is not intended to be subclassed by clients. */ public class PropertyTester extends org.eclipse.core.expressions.PropertyTester { - private static final String KEY_SRC = "isSource"; //$NON-NLS-1$ - private static final String KEY_PREF = "checkPreference"; //$NON-NLS-1$ - @Override public boolean test(Object receiver, String property, Object[] args, Object expectedValue) { - if (KEY_SRC.equals(property)) { - if (receiver instanceof ITranslationUnit) { - return ((ITranslationUnit) receiver).isSourceUnit(); - } else if (receiver instanceof IFile) { - IFile file = (IFile) receiver; - return CoreModel.isValidSourceUnitName(file.getProject(), file.getName()); - } - } else if (KEY_PREF.equals(property) && expectedValue instanceof String) { - boolean result = false; - final Pattern pat = Pattern.compile("(.*):(.*)=(.*)"); //$NON-NLS-1$ - Matcher matcher = pat.matcher((String) expectedValue); - if (matcher.matches()) { - String pluginId = matcher.group(1); - String preference = matcher.group(2); - String wantedValue = matcher.group(3); - - IEclipsePreferences node = InstanceScope.INSTANCE.getNode(pluginId); - if (wantedValue != null) { - String actualValue = node.get(preference, ""); //$NON-NLS-1$ - result = wantedValue.equals(actualValue) || (wantedValue.equals("false") && actualValue.isEmpty()); //$NON-NLS-1$ - } else { - try { - result = Arrays.asList(node.keys()).contains(preference); - } catch (BackingStoreException e) { - CUIPlugin.log(e); - } - } - } - return result; - } return false; } - } |