From 46b8856d2281edfbfe534954dbdd658d699d8a0a Mon Sep 17 00:00:00 2001 From: Tobias Schwarz Date: Fri, 23 Aug 2013 07:30:13 +0200 Subject: Target Explorer: add launch config type unbingding extension point --- .../bindings/LaunchConfigTypeBindingsManager.java | 66 ++- .../bindings/internal/LaunchConfigTypeBinding.java | 558 ++++++++++----------- .../internal/LaunchConfigTypeUnBinding.java | 158 ++++++ 3 files changed, 476 insertions(+), 306 deletions(-) create mode 100644 target_explorer/plugins/org.eclipse.tcf.te.launch.core/src/org/eclipse/tcf/te/launch/core/bindings/internal/LaunchConfigTypeUnBinding.java (limited to 'target_explorer/plugins/org.eclipse.tcf.te.launch.core/src') diff --git a/target_explorer/plugins/org.eclipse.tcf.te.launch.core/src/org/eclipse/tcf/te/launch/core/bindings/LaunchConfigTypeBindingsManager.java b/target_explorer/plugins/org.eclipse.tcf.te.launch.core/src/org/eclipse/tcf/te/launch/core/bindings/LaunchConfigTypeBindingsManager.java index 0102b4af8..c2263a345 100644 --- a/target_explorer/plugins/org.eclipse.tcf.te.launch.core/src/org/eclipse/tcf/te/launch/core/bindings/LaunchConfigTypeBindingsManager.java +++ b/target_explorer/plugins/org.eclipse.tcf.te.launch.core/src/org/eclipse/tcf/te/launch/core/bindings/LaunchConfigTypeBindingsManager.java @@ -28,6 +28,7 @@ import org.eclipse.core.runtime.Platform; import org.eclipse.debug.core.DebugPlugin; import org.eclipse.debug.core.ILaunchConfigurationType; import org.eclipse.tcf.te.launch.core.bindings.internal.LaunchConfigTypeBinding; +import org.eclipse.tcf.te.launch.core.bindings.internal.LaunchConfigTypeUnBinding; import org.eclipse.tcf.te.launch.core.bindings.internal.OverwritableLaunchBinding; import org.eclipse.tcf.te.launch.core.lm.interfaces.ILaunchManagerDelegate; import org.eclipse.tcf.te.launch.core.lm.internal.ExtensionPointManager; @@ -42,6 +43,8 @@ import org.eclipse.tcf.te.runtime.extensions.ExtensionPointComparator; public class LaunchConfigTypeBindingsManager { // Map of all launch configuration type bindings by id private final Map bindings = new Hashtable(); + // Map of all launch configuration type unbindings by id + private final Map unBindings = new Hashtable(); /* * Thread save singleton instance creation. @@ -94,10 +97,12 @@ public class LaunchConfigTypeBindingsManager { Assert.isNotNull(selection); LaunchConfigTypeBinding binding = bindings.get(typeId); + LaunchConfigTypeUnBinding unBinding = unBindings.get(typeId); ILaunchConfigurationType launchConfigType = DebugPlugin.getDefault().getLaunchManager().getLaunchConfigurationType(typeId); return (launchConfigType != null && (selection.getLaunchMode() == null || launchConfigType.supportsMode(selection.getLaunchMode())) && - binding != null && binding.validate(selection) == EvaluationResult.TRUE); + binding != null && binding.validate(selection) == EvaluationResult.TRUE && + (unBinding == null || unBinding.validate(selection) == EvaluationResult.FALSE)); } /** @@ -112,10 +117,12 @@ public class LaunchConfigTypeBindingsManager { Assert.isNotNull(context); LaunchConfigTypeBinding binding = bindings.get(typeId); + LaunchConfigTypeUnBinding unBinding = unBindings.get(typeId); ILaunchConfigurationType launchConfigType = DebugPlugin.getDefault().getLaunchManager().getLaunchConfigurationType(typeId); return (launchConfigType != null && (mode == null || launchConfigType.supportsMode(mode)) && - binding != null && binding.validate(mode, context) != EvaluationResult.FALSE); + binding != null && binding.validate(mode, context) != EvaluationResult.FALSE && + (unBinding == null || unBinding.validate(mode, context) == EvaluationResult.FALSE)); } /** @@ -173,7 +180,8 @@ public class LaunchConfigTypeBindingsManager { for (IExtension binding : bindings) { IConfigurationElement[] elements = binding.getConfigurationElements(); for (IConfigurationElement element : elements) { - loadBinding(element); + if (!loadBinding(element)) + loadUnBinding(element); } } } @@ -184,11 +192,11 @@ public class LaunchConfigTypeBindingsManager { * * @param element The configuration element. Must not be null. */ - private void loadBinding(IConfigurationElement element) { + private boolean loadBinding(IConfigurationElement element) { Assert.isNotNull(element); if (!element.getName().equals("launchConfigTypeBinding")) { //$NON-NLS-1$ - return; + return false; } String launchConfigTypeId = element.getAttribute("launchConfigTypeId"); //$NON-NLS-1$ @@ -206,15 +214,6 @@ public class LaunchConfigTypeBindingsManager { binding.addLaunchManagerDelegate(new OverwritableLaunchBinding(id, overwrites, modes)); } - IConfigurationElement[] stepperBindings = element.getChildren("stepper"); //$NON-NLS-1$ - for (IConfigurationElement stepperBinding : stepperBindings) { - String id = stepperBinding.getAttribute("id"); //$NON-NLS-1$ - String overwrites = stepperBinding.getAttribute("overwrites"); //$NON-NLS-1$ - String modes = stepperBinding.getAttribute("modes"); //$NON-NLS-1$ - - binding.addStepper(new OverwritableLaunchBinding(id, overwrites, modes)); - } - IConfigurationElement[] stepGroupBindings = element.getChildren("stepGroup"); //$NON-NLS-1$ for (IConfigurationElement stepGroupBinding : stepGroupBindings) { String id = stepGroupBinding.getAttribute("id"); //$NON-NLS-1$ @@ -239,6 +238,45 @@ public class LaunchConfigTypeBindingsManager { binding.addEnablement(expression); } } + + return true; } + + /** + * Load a single launch configuration type unbinding. + * + * @param element The configuration element. Must not be null. + */ + private boolean loadUnBinding(IConfigurationElement element) { + Assert.isNotNull(element); + + if (!element.getName().equals("launchConfigTypeUnBinding")) { //$NON-NLS-1$ + return false; + } + + String launchConfigTypeId = element.getAttribute("launchConfigTypeId"); //$NON-NLS-1$ + if (!unBindings.containsKey(launchConfigTypeId)) { + unBindings.put(launchConfigTypeId, new LaunchConfigTypeUnBinding(launchConfigTypeId)); + } + LaunchConfigTypeUnBinding unBinding = unBindings.get(launchConfigTypeId); + + IConfigurationElement[] enablements = element.getChildren("enablement"); //$NON-NLS-1$ + for (IConfigurationElement enablement : enablements) { + Expression expression = null; + try { + expression = ExpressionConverter.getDefault().perform(enablement); + } catch (CoreException e) { + if (Platform.inDebugMode()) { + e.printStackTrace(); + } + } + + if (expression != null) { + unBinding.addEnablement(expression); + } + } + + return true; + } } diff --git a/target_explorer/plugins/org.eclipse.tcf.te.launch.core/src/org/eclipse/tcf/te/launch/core/bindings/internal/LaunchConfigTypeBinding.java b/target_explorer/plugins/org.eclipse.tcf.te.launch.core/src/org/eclipse/tcf/te/launch/core/bindings/internal/LaunchConfigTypeBinding.java index fb0d4ae68..dff95ce66 100644 --- a/target_explorer/plugins/org.eclipse.tcf.te.launch.core/src/org/eclipse/tcf/te/launch/core/bindings/internal/LaunchConfigTypeBinding.java +++ b/target_explorer/plugins/org.eclipse.tcf.te.launch.core/src/org/eclipse/tcf/te/launch/core/bindings/internal/LaunchConfigTypeBinding.java @@ -1,292 +1,266 @@ -/******************************************************************************* - * Copyright (c) 2012 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.tcf.te.launch.core.bindings.internal; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; - -import org.eclipse.core.expressions.EvaluationContext; -import org.eclipse.core.expressions.EvaluationResult; -import org.eclipse.core.expressions.Expression; -import org.eclipse.core.runtime.Assert; -import org.eclipse.core.runtime.CoreException; -import org.eclipse.core.runtime.IStatus; -import org.eclipse.core.runtime.Platform; -import org.eclipse.core.runtime.Status; -import org.eclipse.tcf.te.launch.core.activator.CoreBundleActivator; -import org.eclipse.tcf.te.launch.core.bindings.interfaces.ILaunchBinding; -import org.eclipse.tcf.te.launch.core.bindings.interfaces.IOverwritableLaunchBinding; -import org.eclipse.tcf.te.launch.core.selection.interfaces.ILaunchSelection; -import org.eclipse.tcf.te.launch.core.selection.interfaces.ISelectionContext; - -/** - * Launch configuration type binding implementation. - */ -public class LaunchConfigTypeBinding { - // The launch configuration type id - private final String typeId; - - // Lists of sub bindings - private final List lmDelegateBindings = new ArrayList(); - private final List stepperBindings = new ArrayList(); - private final List stepGroupBindings = new ArrayList(); - - // The list of enablement expressions - private final List expressions = new ArrayList(); - - /** - * Constructor. - * - * @param typeId The launch configuration type id the binding applies to. Must not be - * null. - */ - public LaunchConfigTypeBinding(String typeId) { - Assert.isNotNull(typeId); - this.typeId = typeId; - } - - /** - * Returns the launch configuration type id the binding applies to. - * - * @return The launch configuration type id. - */ - public String getTypeId() { - return typeId; - } - - /** - * Returns the launch manager delegate id for the given launch mode. - * - * @param mode The launch mode. Must not be null. - * @return The launch manager delegate id or null. - */ - public String getLaunchManagerDelegate(String mode) { - Assert.isNotNull(mode); - - ILaunchBinding binding = getBinding(lmDelegateBindings, mode); - return binding != null ? binding.getId() : null; - } - - /** - * Adds the given launch manager delegate binding. - * - * @param binding The binding. Must not be null. - */ - public void addLaunchManagerDelegate(IOverwritableLaunchBinding binding) { - Assert.isNotNull(binding); - if (!lmDelegateBindings.contains(binding)) { - lmDelegateBindings.add(binding); - } - } - - /** - * Returns the stepper id for the given launch mode. - * - * @param mode The launch mode. Must not be null. - * @return The stepper id or null. - */ - public String getStepper(String mode) { - Assert.isNotNull(mode); - - ILaunchBinding binding = getBinding(stepperBindings, mode); - return binding != null ? binding.getId() : null; - } - - /** - * Adds the given stepper binding. - * - * @param binding The binding. Must not be null. - */ - public void addStepper(IOverwritableLaunchBinding binding) { - Assert.isNotNull(binding); - if (!stepperBindings.contains(binding)) { - stepperBindings.add(binding); - } - } - - /** - * Returns the step group id for the given launch mode. - * - * @param mode The launch mode. Must not be null. - * - * @return The step group id or null. - */ - public String getStepGroupId(String mode) { - ILaunchBinding binding = getBinding(stepGroupBindings, mode); - return binding != null ? binding.getId() : null; - } - - /** - * Adds the given step group binding. - * - * @param binding The binding. Must not be null. - */ - public void addStepGroup(ILaunchBinding binding) { - Assert.isNotNull(binding); - if (!stepGroupBindings.contains(binding)) { - stepGroupBindings.add(binding); - } - } - - /** - * Adds the given enablement expression. - * - * @param enablement The enablement expression. Must not be null. - */ - public void addEnablement(Expression expression) { - Assert.isNotNull(expression); - if (!expressions.contains(expression)) { - expressions.add(expression); - } - } - - /** - * Evaluates the enablement expressions with the given launch selection. - * - * @param selection The launch selection. Must not be null. - * @return The result of the enablement expression evaluation. - */ - public EvaluationResult validate(ILaunchSelection selection) { - Assert.isNotNull(selection); - - EvaluationResult result = EvaluationResult.NOT_LOADED; - - EvaluationResult valresult; - for (ISelectionContext context : selection.getSelectedContexts()) { - if (context.isPreferredContext()) { - valresult = validate(selection.getLaunchMode(), context); - if (valresult == EvaluationResult.FALSE) { - return EvaluationResult.FALSE; - } - else if (valresult != EvaluationResult.NOT_LOADED) { - result = valresult; - } - } - } - return result; - } - - /** - * Evaluates the enablement expressions with the given launch mode and selection context. - * - * @param mode The launch mode. Must not be null. - * @param context The launch selection context or null. - * - * @return The result of the enablement expression evaluation. - */ - public EvaluationResult validate(String mode, ISelectionContext context) { - EvaluationResult result = context.isPreferredContext() ? EvaluationResult.FALSE : EvaluationResult.NOT_LOADED; - - if (expressions.isEmpty()) { - return EvaluationResult.TRUE; - } - EvaluationResult valresult; - for (Expression expression : expressions) { - // Set the default variable and "selection" is the selection context - EvaluationContext evalContext = new EvaluationContext(null, context); - evalContext.addVariable("context", context.getContext()); //$NON-NLS-1$ - evalContext.addVariable("selection", context.getSelections() != null ? Arrays.asList(context.getSelections()) : Collections.EMPTY_LIST); //$NON-NLS-1$ - evalContext.addVariable("type", context.getType() != null ? context.getType() : ""); //$NON-NLS-1$ //$NON-NLS-2$ - evalContext.addVariable("mode", mode != null ? mode : ""); //$NON-NLS-1$ //$NON-NLS-2$ - // Allow plugin activation - evalContext.setAllowPluginActivation(true); - // Evaluate the expression - try { - valresult = expression.evaluate(evalContext); - } catch (CoreException e) { - valresult = EvaluationResult.FALSE; - - if (Platform.inDebugMode()) { - IStatus status = new Status(IStatus.ERROR, CoreBundleActivator.getUniqueIdentifier(), e.getLocalizedMessage(), e); - Platform.getLog(CoreBundleActivator.getContext().getBundle()).log(status); - } - } - - if (valresult == EvaluationResult.TRUE) { - return EvaluationResult.TRUE; - } - if (valresult != EvaluationResult.NOT_LOADED) { - result = valresult; - } - } - - return result; - } - - /** - * Returns the list of bindings valid for the given launch mode. - * - * @param bindings The list of available bindings. Must not be null. - * @param mode The launch mode. Must not be null. - * - * @return The list of valid bindings for the given launch mode or an empty list. - */ - private List getBindings(List bindings, String mode) { - Assert.isNotNull(bindings); - Assert.isNotNull(mode); - - List candidates = new ArrayList(); - for (ILaunchBinding binding : bindings) { - if (binding.isValidLaunchMode(mode)) { - candidates.add(binding); - } - } - - return candidates; - } - - /** - * Returns the resolved binding in case of overwritable bindings. - * - * @param bindings The list of available bindings. Must not be null. - * @param mode The launch mode. Must not be null. - * - * @return The resolved binding or null. - */ - private ILaunchBinding getBinding(List bindings, String mode) { - Assert.isNotNull(bindings); - Assert.isNotNull(mode); - - ILaunchBinding binding = null; - - List candidates = getBindings(bindings, mode); - for (int i = 0; i < candidates.size(); i++) { - if (binding == null) { - binding = candidates.get(i); - } - for (int j = 0; j < candidates.size(); j++) { - ILaunchBinding cj = candidates.get(j); - if (cj instanceof IOverwritableLaunchBinding - && ((IOverwritableLaunchBinding) cj).overwrites(binding.getId())) { - binding = cj; - } - } - } - - return binding; - } - - /* - * (non-Javadoc) - * @see java.lang.Object#toString() - */ - @Override - public String toString() { - StringBuffer toString = new StringBuffer(); - - toString.append("LaunchConfigTypeBinding("); //$NON-NLS-1$ - toString.append(typeId); - toString.append(")"); //$NON-NLS-1$ - - return toString.toString(); - } -} +/******************************************************************************* + * Copyright (c) 2012 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.tcf.te.launch.core.bindings.internal; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import org.eclipse.core.expressions.EvaluationContext; +import org.eclipse.core.expressions.EvaluationResult; +import org.eclipse.core.expressions.Expression; +import org.eclipse.core.runtime.Assert; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Platform; +import org.eclipse.core.runtime.Status; +import org.eclipse.tcf.te.launch.core.activator.CoreBundleActivator; +import org.eclipse.tcf.te.launch.core.bindings.interfaces.ILaunchBinding; +import org.eclipse.tcf.te.launch.core.bindings.interfaces.IOverwritableLaunchBinding; +import org.eclipse.tcf.te.launch.core.selection.interfaces.ILaunchSelection; +import org.eclipse.tcf.te.launch.core.selection.interfaces.ISelectionContext; + +/** + * Launch configuration type binding implementation. + */ +public class LaunchConfigTypeBinding { + // The launch configuration type id + private final String typeId; + + // Lists of sub bindings + private final List lmDelegateBindings = new ArrayList(); + private final List stepGroupBindings = new ArrayList(); + + // The list of enablement expressions + private final List expressions = new ArrayList(); + + /** + * Constructor. + * + * @param typeId The launch configuration type id the binding applies to. Must not be + * null. + */ + public LaunchConfigTypeBinding(String typeId) { + Assert.isNotNull(typeId); + this.typeId = typeId; + } + + /** + * Returns the launch configuration type id the binding applies to. + * + * @return The launch configuration type id. + */ + public String getTypeId() { + return typeId; + } + + /** + * Returns the launch manager delegate id for the given launch mode. + * + * @param mode The launch mode. Must not be null. + * @return The launch manager delegate id or null. + */ + public String getLaunchManagerDelegate(String mode) { + Assert.isNotNull(mode); + + ILaunchBinding binding = getBinding(lmDelegateBindings, mode); + return binding != null ? binding.getId() : null; + } + + /** + * Adds the given launch manager delegate binding. + * + * @param binding The binding. Must not be null. + */ + public void addLaunchManagerDelegate(IOverwritableLaunchBinding binding) { + Assert.isNotNull(binding); + if (!lmDelegateBindings.contains(binding)) { + lmDelegateBindings.add(binding); + } + } + + /** + * Returns the step group id for the given launch mode. + * + * @param mode The launch mode. Must not be null. + * + * @return The step group id or null. + */ + public String getStepGroupId(String mode) { + ILaunchBinding binding = getBinding(stepGroupBindings, mode); + return binding != null ? binding.getId() : null; + } + + /** + * Adds the given step group binding. + * + * @param binding The binding. Must not be null. + */ + public void addStepGroup(ILaunchBinding binding) { + Assert.isNotNull(binding); + if (!stepGroupBindings.contains(binding)) { + stepGroupBindings.add(binding); + } + } + + /** + * Adds the given enablement expression. + * + * @param enablement The enablement expression. Must not be null. + */ + public void addEnablement(Expression expression) { + Assert.isNotNull(expression); + if (!expressions.contains(expression)) { + expressions.add(expression); + } + } + + /** + * Evaluates the enablement expressions with the given launch selection. + * + * @param selection The launch selection. Must not be null. + * @return The result of the enablement expression evaluation. + */ + public EvaluationResult validate(ILaunchSelection selection) { + Assert.isNotNull(selection); + + EvaluationResult result = EvaluationResult.NOT_LOADED; + + EvaluationResult valresult; + for (ISelectionContext context : selection.getSelectedContexts()) { + if (context.isPreferredContext()) { + valresult = validate(selection.getLaunchMode(), context); + if (valresult == EvaluationResult.FALSE) { + return EvaluationResult.FALSE; + } + else if (valresult != EvaluationResult.NOT_LOADED) { + result = valresult; + } + } + } + return result; + } + + /** + * Evaluates the enablement expressions with the given launch mode and selection context. + * + * @param mode The launch mode. Must not be null. + * @param context The launch selection context or null. + * + * @return The result of the enablement expression evaluation. + */ + public EvaluationResult validate(String mode, ISelectionContext context) { + EvaluationResult result = context.isPreferredContext() ? EvaluationResult.FALSE : EvaluationResult.NOT_LOADED; + + if (expressions.isEmpty()) { + return EvaluationResult.TRUE; + } + EvaluationResult valresult; + for (Expression expression : expressions) { + // Set the default variable and "selection" is the selection context + EvaluationContext evalContext = new EvaluationContext(null, context); + evalContext.addVariable("context", context.getContext()); //$NON-NLS-1$ + evalContext.addVariable("selection", context.getSelections() != null ? Arrays.asList(context.getSelections()) : Collections.EMPTY_LIST); //$NON-NLS-1$ + evalContext.addVariable("type", context.getType() != null ? context.getType() : ""); //$NON-NLS-1$ //$NON-NLS-2$ + evalContext.addVariable("mode", mode != null ? mode : ""); //$NON-NLS-1$ //$NON-NLS-2$ + // Allow plugin activation + evalContext.setAllowPluginActivation(true); + // Evaluate the expression + try { + valresult = expression.evaluate(evalContext); + } catch (CoreException e) { + valresult = EvaluationResult.FALSE; + + if (Platform.inDebugMode()) { + IStatus status = new Status(IStatus.ERROR, CoreBundleActivator.getUniqueIdentifier(), e.getLocalizedMessage(), e); + Platform.getLog(CoreBundleActivator.getContext().getBundle()).log(status); + } + } + + if (valresult == EvaluationResult.TRUE) { + return EvaluationResult.TRUE; + } + if (valresult != EvaluationResult.NOT_LOADED) { + result = valresult; + } + } + + return result; + } + + /** + * Returns the list of bindings valid for the given launch mode. + * + * @param bindings The list of available bindings. Must not be null. + * @param mode The launch mode. Must not be null. + * + * @return The list of valid bindings for the given launch mode or an empty list. + */ + private List getBindings(List bindings, String mode) { + Assert.isNotNull(bindings); + Assert.isNotNull(mode); + + List candidates = new ArrayList(); + for (ILaunchBinding binding : bindings) { + if (binding.isValidLaunchMode(mode)) { + candidates.add(binding); + } + } + + return candidates; + } + + /** + * Returns the resolved binding in case of overwritable bindings. + * + * @param bindings The list of available bindings. Must not be null. + * @param mode The launch mode. Must not be null. + * + * @return The resolved binding or null. + */ + private ILaunchBinding getBinding(List bindings, String mode) { + Assert.isNotNull(bindings); + Assert.isNotNull(mode); + + ILaunchBinding binding = null; + + List candidates = getBindings(bindings, mode); + for (int i = 0; i < candidates.size(); i++) { + if (binding == null) { + binding = candidates.get(i); + } + for (int j = 0; j < candidates.size(); j++) { + ILaunchBinding cj = candidates.get(j); + if (cj instanceof IOverwritableLaunchBinding + && ((IOverwritableLaunchBinding) cj).overwrites(binding.getId())) { + binding = cj; + } + } + } + + return binding; + } + + /* + * (non-Javadoc) + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + StringBuffer toString = new StringBuffer(); + + toString.append("LaunchConfigTypeBinding("); //$NON-NLS-1$ + toString.append(typeId); + toString.append(")"); //$NON-NLS-1$ + + return toString.toString(); + } +} diff --git a/target_explorer/plugins/org.eclipse.tcf.te.launch.core/src/org/eclipse/tcf/te/launch/core/bindings/internal/LaunchConfigTypeUnBinding.java b/target_explorer/plugins/org.eclipse.tcf.te.launch.core/src/org/eclipse/tcf/te/launch/core/bindings/internal/LaunchConfigTypeUnBinding.java new file mode 100644 index 000000000..4597e192a --- /dev/null +++ b/target_explorer/plugins/org.eclipse.tcf.te.launch.core/src/org/eclipse/tcf/te/launch/core/bindings/internal/LaunchConfigTypeUnBinding.java @@ -0,0 +1,158 @@ +/******************************************************************************* + * Copyright (c) 2012 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.tcf.te.launch.core.bindings.internal; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import org.eclipse.core.expressions.EvaluationContext; +import org.eclipse.core.expressions.EvaluationResult; +import org.eclipse.core.expressions.Expression; +import org.eclipse.core.runtime.Assert; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Platform; +import org.eclipse.core.runtime.Status; +import org.eclipse.tcf.te.launch.core.activator.CoreBundleActivator; +import org.eclipse.tcf.te.launch.core.selection.interfaces.ILaunchSelection; +import org.eclipse.tcf.te.launch.core.selection.interfaces.ISelectionContext; + +/** + * Launch configuration type unbinding implementation. + */ +public class LaunchConfigTypeUnBinding { + // The launch configuration type id + private final String typeId; + + // The list of enablement expressions + private final List expressions = new ArrayList(); + + /** + * Constructor. + * + * @param typeId The launch configuration type id the unbinding applies to. Must not be + * null. + */ + public LaunchConfigTypeUnBinding(String typeId) { + Assert.isNotNull(typeId); + this.typeId = typeId; + } + + /** + * Returns the launch configuration type id the unbinding applies to. + * + * @return The launch configuration type id. + */ + public String getTypeId() { + return typeId; + } + + /** + * Adds the given enablement expression. + * + * @param enablement The enablement expression. Must not be null. + */ + public void addEnablement(Expression expression) { + Assert.isNotNull(expression); + if (!expressions.contains(expression)) { + expressions.add(expression); + } + } + + /** + * Evaluates the enablement expressions with the given launch selection. + * + * @param selection The launch selection. Must not be null. + * @return The result of the enablement expression evaluation. + */ + public EvaluationResult validate(ILaunchSelection selection) { + Assert.isNotNull(selection); + + EvaluationResult result = EvaluationResult.NOT_LOADED; + + EvaluationResult valresult; + for (ISelectionContext context : selection.getSelectedContexts()) { + if (context.isPreferredContext()) { + valresult = validate(selection.getLaunchMode(), context); + if (valresult == EvaluationResult.FALSE) { + return EvaluationResult.FALSE; + } + else if (valresult != EvaluationResult.NOT_LOADED) { + result = valresult; + } + } + } + return result; + } + + /** + * Evaluates the enablement expressions with the given launch mode and selection context. + * + * @param mode The launch mode. Must not be null. + * @param context The launch selection context or null. + * + * @return The result of the enablement expression evaluation. + */ + public EvaluationResult validate(String mode, ISelectionContext context) { + EvaluationResult result = context.isPreferredContext() ? EvaluationResult.FALSE : EvaluationResult.NOT_LOADED; + + if (expressions.isEmpty()) { + return EvaluationResult.TRUE; + } + EvaluationResult valresult; + for (Expression expression : expressions) { + // Set the default variable and "selection" is the selection context + EvaluationContext evalContext = new EvaluationContext(null, context); + evalContext.addVariable("context", context.getContext()); //$NON-NLS-1$ + evalContext.addVariable("selection", context.getSelections() != null ? Arrays.asList(context.getSelections()) : Collections.EMPTY_LIST); //$NON-NLS-1$ + evalContext.addVariable("type", context.getType() != null ? context.getType() : ""); //$NON-NLS-1$ //$NON-NLS-2$ + evalContext.addVariable("mode", mode != null ? mode : ""); //$NON-NLS-1$ //$NON-NLS-2$ + // Allow plugin activation + evalContext.setAllowPluginActivation(true); + // Evaluate the expression + try { + valresult = expression.evaluate(evalContext); + } catch (CoreException e) { + valresult = EvaluationResult.FALSE; + + if (Platform.inDebugMode()) { + IStatus status = new Status(IStatus.ERROR, CoreBundleActivator.getUniqueIdentifier(), e.getLocalizedMessage(), e); + Platform.getLog(CoreBundleActivator.getContext().getBundle()).log(status); + } + } + + if (valresult == EvaluationResult.TRUE) { + return EvaluationResult.TRUE; + } + if (valresult != EvaluationResult.NOT_LOADED) { + result = valresult; + } + } + + return result; + } + + /* + * (non-Javadoc) + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + StringBuffer toString = new StringBuffer(); + + toString.append("LaunchConfigTypeUnBinding("); //$NON-NLS-1$ + toString.append(typeId); + toString.append(")"); //$NON-NLS-1$ + + return toString.toString(); + } +} -- cgit v1.2.3