diff --git a/bundles/org.eclipse.launchbar.core/src/org/eclipse/launchbar/core/AbstractLaunchConfigProvider.java b/bundles/org.eclipse.launchbar.core/src/org/eclipse/launchbar/core/AbstractLaunchConfigProvider.java index 6203abb43bc..19cef3b7bb5 100644 --- a/bundles/org.eclipse.launchbar.core/src/org/eclipse/launchbar/core/AbstractLaunchConfigProvider.java +++ b/bundles/org.eclipse.launchbar.core/src/org/eclipse/launchbar/core/AbstractLaunchConfigProvider.java @@ -47,6 +47,14 @@ public abstract class AbstractLaunchConfigProvider implements ILaunchConfigurati workingCopy.setAttribute(ATTR_PROVIDER_CLASS, getClass().getName()); } + @Override + public boolean launchDescriptorMatches(ILaunchDescriptor descriptor, ILaunchConfiguration configuration, ILaunchTarget target) throws CoreException { + ILaunchConfiguration lc = descriptor.getAdapter(ILaunchConfiguration.class); + if (lc == null) + return false; + return configuration.getName().equals(lc.getName()); + } + protected boolean ownsLaunchConfiguration(ILaunchConfiguration configuration) throws CoreException { if (!configuration.exists()) { // can't own it if it doesn't exist diff --git a/bundles/org.eclipse.launchbar.core/src/org/eclipse/launchbar/core/DefaultLaunchConfigProvider.java b/bundles/org.eclipse.launchbar.core/src/org/eclipse/launchbar/core/DefaultLaunchConfigProvider.java index ab26e5e83c7..faafd04690b 100644 --- a/bundles/org.eclipse.launchbar.core/src/org/eclipse/launchbar/core/DefaultLaunchConfigProvider.java +++ b/bundles/org.eclipse.launchbar.core/src/org/eclipse/launchbar/core/DefaultLaunchConfigProvider.java @@ -19,12 +19,11 @@ import org.eclipse.launchbar.core.target.ILaunchTargetManager; /** * The launch config provider for the default descriptor which is the launch * config itself. - * + * * Override this class and register an extension if you want to support targets * other than the local connection. */ public class DefaultLaunchConfigProvider implements ILaunchConfigurationProvider { - @Override public boolean supports(ILaunchDescriptor descriptor, ILaunchTarget target) throws CoreException { // Only supports Local connection @@ -69,4 +68,11 @@ public class DefaultLaunchConfigProvider implements ILaunchConfigurationProvider // nothing to do } + @Override + public boolean launchDescriptorMatches(ILaunchDescriptor descriptor, ILaunchConfiguration configuration, ILaunchTarget target) throws CoreException { + ILaunchConfiguration lc = descriptor.getAdapter(ILaunchConfiguration.class); + if (lc == null) + return false; + return configuration.getName().equals(lc.getName()); + } } diff --git a/bundles/org.eclipse.launchbar.core/src/org/eclipse/launchbar/core/ILaunchConfigurationProvider.java b/bundles/org.eclipse.launchbar.core/src/org/eclipse/launchbar/core/ILaunchConfigurationProvider.java index ef7c9f473f2..ba918e550f8 100644 --- a/bundles/org.eclipse.launchbar.core/src/org/eclipse/launchbar/core/ILaunchConfigurationProvider.java +++ b/bundles/org.eclipse.launchbar.core/src/org/eclipse/launchbar/core/ILaunchConfigurationProvider.java @@ -18,7 +18,7 @@ import org.eclipse.launchbar.core.target.ILaunchTarget; /** * The provider of launch configurations of a given type for a given descriptor * type and a given target type. - * + * * It is recommended to extend {@link AbstractLaunchConfigProvider} or one of * it's subclasses instead of implementing this directly. */ @@ -26,10 +26,10 @@ public interface ILaunchConfigurationProvider { /** * Does this config provider provide launch configurations for the * combination of descriptor and target. - * + * * Note: this is called when filtering targets for a descriptor. Processing * should be minimal. - * + * * @param descriptor * @param target * @return true if target is supported, false otherwise. @@ -38,7 +38,7 @@ public interface ILaunchConfigurationProvider { /** * Return the launch configuation type for the descriptor and target. - * + * * @param descriptor * @param target * launch configuration type or null if not supported @@ -61,11 +61,29 @@ public interface ILaunchConfigurationProvider { ILaunchConfiguration getLaunchConfiguration(ILaunchDescriptor descriptor, ILaunchTarget target) throws CoreException; + /** + * Return true if given descriptor is associated with given launch configuration. + * I.e. if getLaunchConfiguration(descriptor, target) returns a launch configuration, + * calling launchDescriptorMatches(descriptor, configuration, target) should return true. + * + * This method is used to activate descriptor when launch happens, return false all the time would mean + * launch would track launches of this specific descriptor (if they occur outside of launchbar). + * @param configuration + * launch configuration in question + * @param target - target to match, it can be null if launch bar cannot determine target, + * in this case it should act as wildcard + * + * @return launch configuration + * @throws CoreException + */ + boolean launchDescriptorMatches(ILaunchDescriptor descriptor, ILaunchConfiguration configuration, ILaunchTarget target) + throws CoreException; + /** * A launch configuration has been added. Provider can inspect it and * associate with internal map. Provider should make sure it owns this * launch configuration or it can modify it to take over. - * + * * @return true of provider owns this launch configuration */ boolean launchConfigurationAdded(ILaunchConfiguration configuration) throws CoreException; @@ -76,7 +94,7 @@ public interface ILaunchConfigurationProvider { * configuration has been removed from file system, so accessing its * attributes won't work. If provider cannot determine if it owns it it * should return false. - * + * * @param configuration * @return true if provider owns this launch configuration * @throws CoreException @@ -87,7 +105,7 @@ public interface ILaunchConfigurationProvider { * A launch configuration has been changed. Provider can inspect it to * re-evaluate its internal map. Provider should make sure it owns this * launch configuration or it can modify it to take over. - * + * * @return true of provider owns this launch configuration */ boolean launchConfigurationChanged(ILaunchConfiguration configuration) throws CoreException; @@ -95,7 +113,7 @@ public interface ILaunchConfigurationProvider { /** * A launch descriptor has been removed. Remove any launch configurations * that were created for it. - * + * * @param descriptor * @throws CoreException */ @@ -104,7 +122,7 @@ public interface ILaunchConfigurationProvider { /** * A launch target has been removed. Remove any launch configurations that * were created for it. - * + * * @param target * @throws CoreException */ diff --git a/bundles/org.eclipse.launchbar.core/src/org/eclipse/launchbar/core/internal/LaunchBarManager.java b/bundles/org.eclipse.launchbar.core/src/org/eclipse/launchbar/core/internal/LaunchBarManager.java index 277888a0a40..9908602ddb8 100644 --- a/bundles/org.eclipse.launchbar.core/src/org/eclipse/launchbar/core/internal/LaunchBarManager.java +++ b/bundles/org.eclipse.launchbar.core/src/org/eclipse/launchbar/core/internal/LaunchBarManager.java @@ -32,8 +32,10 @@ import org.eclipse.core.runtime.jobs.Job; import org.eclipse.core.runtime.preferences.IEclipsePreferences; import org.eclipse.core.runtime.preferences.InstanceScope; import org.eclipse.debug.core.DebugPlugin; +import org.eclipse.debug.core.ILaunch; import org.eclipse.debug.core.ILaunchConfiguration; import org.eclipse.debug.core.ILaunchConfigurationType; +import org.eclipse.debug.core.ILaunchListener; import org.eclipse.debug.core.ILaunchManager; import org.eclipse.debug.core.ILaunchMode; import org.eclipse.launchbar.core.ILaunchBarManager; @@ -44,6 +46,7 @@ import org.eclipse.launchbar.core.ILaunchObjectProvider; import org.eclipse.launchbar.core.target.ILaunchTarget; import org.eclipse.launchbar.core.target.ILaunchTargetListener; import org.eclipse.launchbar.core.target.ILaunchTargetManager; +import org.eclipse.launchbar.core.target.launch.ITargetedLaunch; import org.osgi.service.prefs.BackingStoreException; import org.osgi.service.prefs.Preferences; @@ -51,7 +54,6 @@ import org.osgi.service.prefs.Preferences; * The brains of the launch bar. */ public class LaunchBarManager implements ILaunchBarManager, ILaunchTargetListener { - public interface Listener { default void activeLaunchDescriptorChanged() { } @@ -68,39 +70,29 @@ public class LaunchBarManager implements ILaunchBarManager, ILaunchTargetListene default void launchTargetsChanged() { } } - private final List listeners = new LinkedList<>(); - // The launch object providers private final List objectProviders = new ArrayList<>(); - // The descriptor types private final Map descriptorTypes = new HashMap<>(); - // Descriptor types ordered from highest priority to lowest private List orderedDescriptorTypes; - // the extended info for loaded descriptor types private final Map descriptorTypeInfo = new HashMap<>(); private final Map> configProviders = new HashMap<>(); - // Descriptors in MRU order, key is desc type id and desc name. private final Map, ILaunchDescriptor> descriptors = new LinkedHashMap<>(); - // Map of launch objects to launch descriptors private final Map objectDescriptorMap = new HashMap<>(); - private ILaunchTargetManager launchTargetManager; - private ILaunchDescriptor activeLaunchDesc; private ILaunchMode activeLaunchMode; private ILaunchTarget activeLaunchTarget; - // private static final String PREF_ACTIVE_CONFIG_DESC = "activeConfigDesc"; private static final String PREF_ACTIVE_LAUNCH_MODE = "activeLaunchMode"; //$NON-NLS-1$ private static final String PREF_ACTIVE_LAUNCH_TARGET = "activeLaunchTarget"; //$NON-NLS-1$ private static final String PREF_CONFIG_DESC_ORDER = "configDescList"; //$NON-NLS-1$ - + private static final String PREF_TRACK_LAUNCHES = "trackLaunches"; //$NON-NLS-1$ boolean initialized = false; public LaunchBarManager() { @@ -146,17 +138,14 @@ public class LaunchBarManager implements ILaunchBarManager, ILaunchTargetListene // Fetch the desc order before the init messes it up IEclipsePreferences store = getPreferenceStore(); String configDescIds = store.get(PREF_CONFIG_DESC_ORDER, ""); //$NON-NLS-1$ - // Load up the types loadExtensions(); - // Hook up the existing launch configurations and listen ILaunchManager launchManager = getLaunchManager(); for (ILaunchConfiguration configuration : launchManager.getLaunchConfigurations()) { launchConfigurationAdded(configuration); } launchManager.addLaunchConfigurationListener(this); - // Reorder the descriptors based on the preference if (!configDescIds.isEmpty()) { String[] split = configDescIds.split(","); //$NON-NLS-1$ @@ -170,12 +159,51 @@ public class LaunchBarManager implements ILaunchBarManager, ILaunchTargetListene last = desc; } } - // Set the active desc, with MRU, it should be the last one if (last != null) { setActiveLaunchDescriptor(last); } } + launchManager.addLaunchListener(new ILaunchListener() { + @Override + public void launchRemoved(ILaunch launch) { + // ignore + } + + @Override + public void launchAdded(ILaunch launch) { + if (!getPreferenceStore().getBoolean(PREF_TRACK_LAUNCHES, true)) + return; + ILaunchConfiguration lc = launch.getLaunchConfiguration(); + String mode = launch.getLaunchMode(); + ILaunchTarget target = null; + if (launch instanceof ITargetedLaunch) { + target = ((ITargetedLaunch) launch).getLaunchTarget(); + } + try { + setActive(lc, mode, target); + } catch (CoreException e) { + Activator.log(e); + } + } + + @Override + public void launchChanged(ILaunch launch) { + ILaunchConfiguration lc = launch.getLaunchConfiguration(); + ILaunchTarget target = null; + if (launch instanceof ITargetedLaunch) { + target = ((ITargetedLaunch) launch).getLaunchTarget(); + } + if (launchDescriptorMatches(activeLaunchDesc, lc, target)) { + // active launch delegate may have changed target + try { + setActiveLaunchTarget(target); + } catch (CoreException e) { + Activator.log(e); + } + } + } + }); } finally { initialized = true; } @@ -188,7 +216,6 @@ public class LaunchBarManager implements ILaunchBarManager, ILaunchTargetListene private void loadExtensions() throws CoreException { IExtensionPoint point = getExtensionPoint(); IExtension[] extensions = point.getExtensions(); - // Load up the types for (IExtension extension : extensions) { for (IConfigurationElement element : extension.getConfigurationElements()) { @@ -196,9 +223,7 @@ public class LaunchBarManager implements ILaunchBarManager, ILaunchTargetListene String elementName = element.getName(); if (elementName.equals("descriptorType")) { //$NON-NLS-1$ LaunchDescriptorTypeInfo typeInfo = new LaunchDescriptorTypeInfo(element); - descriptorTypes.put(typeInfo.getId(), typeInfo); - if (configProviders.get(typeInfo.getId()) == null) { // Make sure we initialize the list configProviders.put(typeInfo.getId(), new ArrayList()); @@ -217,7 +242,6 @@ public class LaunchBarManager implements ILaunchBarManager, ILaunchTargetListene } } } - // Sort things orderedDescriptorTypes = new ArrayList<>(descriptorTypes.values()); Collections.sort(orderedDescriptorTypes, new Comparator() { @@ -234,7 +258,6 @@ public class LaunchBarManager implements ILaunchBarManager, ILaunchTargetListene } } }); - for (List providers : configProviders.values()) { Collections.sort(providers, new Comparator() { @Override @@ -250,9 +273,7 @@ public class LaunchBarManager implements ILaunchBarManager, ILaunchTargetListene } } }); - } - // Now that all the types are loaded, the object providers which now // populate the descriptors for (IExtension extension : extensions) { @@ -282,7 +303,6 @@ public class LaunchBarManager implements ILaunchBarManager, ILaunchTargetListene if (i < 0) { return null; } - return new Pair(key.substring(0, i), key.substring(i + 1)); } @@ -308,7 +328,6 @@ public class LaunchBarManager implements ILaunchBarManager, ILaunchTargetListene throws CoreException { if (descriptor == null) return null; - for (LaunchConfigProviderInfo providerInfo : configProviders.get(getDescriptorTypeId(descriptor.getType()))) { if (providerInfo.enabled(descriptor) && providerInfo.enabled(target)) { ILaunchConfigurationType type = providerInfo.getProvider().getLaunchConfigurationType(descriptor, @@ -318,7 +337,6 @@ public class LaunchBarManager implements ILaunchBarManager, ILaunchTargetListene } } } - return null; } @@ -329,7 +347,6 @@ public class LaunchBarManager implements ILaunchBarManager, ILaunchTargetListene if (desc != null) { return desc; } - for (LaunchDescriptorTypeInfo descriptorInfo : orderedDescriptorTypes) { try { if (descriptorInfo.enabled(launchObject)) { @@ -343,13 +360,11 @@ public class LaunchBarManager implements ILaunchBarManager, ILaunchTargetListene addDescriptor(launchObject, desc); return desc; } - } } catch (Throwable e) { Activator.log(e); } } - return null; } @@ -362,7 +377,6 @@ public class LaunchBarManager implements ILaunchBarManager, ILaunchTargetListene if (descriptor.equals(activeLaunchDesc)) { setActiveLaunchDescriptor(getLastUsedDescriptor()); } - for (LaunchConfigProviderInfo providerInfo : configProviders .get(getDescriptorTypeId(descriptor.getType()))) { if (providerInfo.enabled(descriptor)) { @@ -375,14 +389,12 @@ public class LaunchBarManager implements ILaunchBarManager, ILaunchTargetListene @Override public void launchObjectChanged(Object launchObject) throws CoreException { // TODO deal with object renames here, somehow - ILaunchDescriptor origDesc = objectDescriptorMap.get(launchObject); if (origDesc == null) { // See if anyone wants it now launchObjectAdded(launchObject); return; } - // check if descriptor still wants it ILaunchDescriptorType origDescType = origDesc.getType(); try { @@ -419,6 +431,38 @@ public class LaunchBarManager implements ILaunchBarManager, ILaunchTargetListene return activeLaunchDesc; } + private void setActive(ILaunchConfiguration config, String mode, ILaunchTarget target) throws CoreException { + ILaunchDescriptor descriptor = getLaunchDescriptor(config, target); + if (descriptor == null) + return; // not found + // we do not call setActiveLaunchTarget because it will cause mode/target switch and cause flickering + boolean changeDesc = activeLaunchDesc != descriptor; + boolean changeTarget = target != null && activeLaunchTarget != target; + if (changeDesc) { + doSetActiveLaunchDescriptor(descriptor); + // store in persistent storage + storeActiveDescriptor(activeLaunchDesc); + } + if (changeTarget) { + activeLaunchTarget = target; + storeLaunchTarget(activeLaunchDesc, target); + } + ILaunchMode[] supportedModes = getLaunchModes(); + for (ILaunchMode launchMode : supportedModes) { + if (launchMode.getIdentifier().equals(mode)) { + setActiveLaunchMode(launchMode); + break; + } + } + // send delayed notification about descriptor change + if (changeDesc) { + fireActiveLaunchDescriptorChanged(); + } + if (changeTarget) { + fireActiveLaunchTargetChanged(); // notify target listeners + } + } + public void setActiveLaunchDescriptor(ILaunchDescriptor descriptor) throws CoreException { Activator.trace("set active descriptor " + descriptor); //$NON-NLS-1$ if (activeLaunchDesc == descriptor) { @@ -436,16 +480,9 @@ public class LaunchBarManager implements ILaunchBarManager, ILaunchTargetListene // do not set to null unless no descriptors descriptor = getLastUsedDescriptor(); } - activeLaunchDesc = descriptor; - if (descriptor != null) { - // keeps most used descriptor last - Pair id = getDescriptorId(descriptor); - descriptors.remove(id); - descriptors.put(id, descriptor); - } + doSetActiveLaunchDescriptor(descriptor); // store in persistent storage storeActiveDescriptor(activeLaunchDesc); - // Send notifications fireActiveLaunchDescriptorChanged(); // Set active target @@ -454,9 +491,18 @@ public class LaunchBarManager implements ILaunchBarManager, ILaunchTargetListene syncActiveMode(); } + private void doSetActiveLaunchDescriptor(ILaunchDescriptor descriptor) { + activeLaunchDesc = descriptor; + if (descriptor != null) { + // keeps most used descriptor last + Pair id = getDescriptorId(descriptor); + descriptors.remove(id); + descriptors.put(id, descriptor); + } + } + private void storeActiveDescriptor(ILaunchDescriptor descriptor) { Activator.trace("new active config is stored " + descriptor); //$NON-NLS-1$ - // Store the desc order, active one is the last one StringBuffer buff = new StringBuffer(); // TODO: this can be very long string @@ -644,14 +690,12 @@ public class LaunchBarManager implements ILaunchBarManager, ILaunchTargetListene public List getLaunchTargets(ILaunchDescriptor descriptor) { if (descriptor == null) return Arrays.asList(launchTargetManager.getLaunchTargets()); - List targets = new ArrayList<>(); for (ILaunchTarget target : launchTargetManager.getLaunchTargets()) { if (supportsTarget(descriptor, target)) { targets.add(target); } } - return targets; } @@ -727,19 +771,17 @@ public class LaunchBarManager implements ILaunchBarManager, ILaunchTargetListene private ILaunchTarget getDefaultLaunchTarget(ILaunchDescriptor descriptor) { List targets = getLaunchTargets(descriptor); // chances are that better target is most recently added, rather then the oldest - return targets.isEmpty() ? ILaunchTarget.NULL_TARGET : targets.get(targets.size()-1); + return targets.isEmpty() ? ILaunchTarget.NULL_TARGET : targets.get(targets.size() - 1); } public ILaunchConfiguration getActiveLaunchConfiguration() throws CoreException { ILaunchConfiguration configuration = getLaunchConfiguration(activeLaunchDesc, activeLaunchTarget); - // This is the only concrete time we have the mapping from launch // configuration to launch target. Record it in the target manager for // the launch delegates to use. if (configuration != null) { launchTargetManager.setDefaultLaunchTarget(configuration, activeLaunchTarget); } - return configuration; } @@ -748,7 +790,6 @@ public class LaunchBarManager implements ILaunchBarManager, ILaunchTargetListene if (descriptor == null) { return null; } - String descTypeId = getDescriptorTypeId(descriptor.getType()); for (LaunchConfigProviderInfo providerInfo : configProviders.get(descTypeId)) { try { @@ -767,7 +808,6 @@ public class LaunchBarManager implements ILaunchBarManager, ILaunchTargetListene Activator.log(e); } } - return null; } @@ -809,7 +849,6 @@ public class LaunchBarManager implements ILaunchBarManager, ILaunchTargetListene } catch (Throwable e) { Activator.log(e); } - for (LaunchDescriptorTypeInfo descTypeInfo : orderedDescriptorTypes) { for (LaunchConfigProviderInfo providerInfo : configProviders.get(descTypeInfo.getId())) { try { @@ -900,4 +939,35 @@ public class LaunchBarManager implements ILaunchBarManager, ILaunchTargetListene } } + private ILaunchDescriptor getLaunchDescriptor(ILaunchConfiguration configuration, ILaunchTarget target) { + // shortcut - check active first + if (launchDescriptorMatches(activeLaunchDesc, configuration, target)) { + return activeLaunchDesc; + } + for (ILaunchDescriptor desc : getLaunchDescriptors()) { // this should be in MRU, most used first + if (launchDescriptorMatches(desc, configuration, target)) { + return desc; + } + } + return null; + } + + private boolean launchDescriptorMatches(ILaunchDescriptor desc, ILaunchConfiguration configuration, + ILaunchTarget target) { + if (desc == null || configuration == null) + return false; + try { + String descriptorTypeId = getDescriptorTypeId(desc.getType()); + for (LaunchConfigProviderInfo providerInfo : configProviders.get(descriptorTypeId)) { + if (providerInfo.enabled(desc) && (target == null || providerInfo.enabled(target))) { + if (providerInfo.getProvider().launchDescriptorMatches(desc, configuration, target)) { + return true; + } + } + } + } catch (CoreException e) { + Activator.log(e); + } + return false; + } }