From 23b1b5d8572888b1ec24b7cbfea3976fec3155d4 Mon Sep 17 00:00:00 2001 From: Camilo Bernal Date: Wed, 31 Oct 2012 13:39:58 -0400 Subject: Bug 392790: Prompt user to create a new launch configuration. When a proper launch configuration cannot be found for a specific type and tool , when launching from a shortcut, prompt the user on whether they want to create such a configuration. Change-Id: If17907521c31e8efa0838ec45d54b3ca564cbb94 Reviewed-on: https://git.eclipse.org/r/8469 Tested-by: Hudson CI Reviewed-by: Roland Grunberg IP-Clean: Roland Grunberg Tested-by: Roland Grunberg --- .../profiling/launch/provider/launch/Messages.java | 3 +- .../provider/launch/ProviderLaunchShortcut.java | 111 ++++++++++++--------- .../launch/provider/launch/messages.properties | 3 + 3 files changed, 68 insertions(+), 49 deletions(-) diff --git a/profiling/org.eclipse.linuxtools.profiling.launch/src/org/eclipse/linuxtools/internal/profiling/launch/provider/launch/Messages.java b/profiling/org.eclipse.linuxtools.profiling.launch/src/org/eclipse/linuxtools/internal/profiling/launch/provider/launch/Messages.java index c3c1a33dcc..95037eebfa 100644 --- a/profiling/org.eclipse.linuxtools.profiling.launch/src/org/eclipse/linuxtools/internal/profiling/launch/provider/launch/Messages.java +++ b/profiling/org.eclipse.linuxtools.profiling.launch/src/org/eclipse/linuxtools/internal/profiling/launch/provider/launch/Messages.java @@ -21,7 +21,8 @@ public class Messages extends NLS { public static String UseProjectSetting_0; public static String ProjectSettings_0; public static String PreferenceLink_0; - + public static String ProviderLaunchConfigurationPrompt_0; + static { // initialize resource bundle NLS.initializeMessages(BUNDLE_NAME, Messages.class); diff --git a/profiling/org.eclipse.linuxtools.profiling.launch/src/org/eclipse/linuxtools/internal/profiling/launch/provider/launch/ProviderLaunchShortcut.java b/profiling/org.eclipse.linuxtools.profiling.launch/src/org/eclipse/linuxtools/internal/profiling/launch/provider/launch/ProviderLaunchShortcut.java index ccb06fed32..7917c20b84 100644 --- a/profiling/org.eclipse.linuxtools.profiling.launch/src/org/eclipse/linuxtools/internal/profiling/launch/provider/launch/ProviderLaunchShortcut.java +++ b/profiling/org.eclipse.linuxtools.profiling.launch/src/org/eclipse/linuxtools/internal/profiling/launch/provider/launch/ProviderLaunchShortcut.java @@ -10,8 +10,7 @@ *******************************************************************************/ package org.eclipse.linuxtools.internal.profiling.launch.provider.launch; -import java.util.Arrays; -import java.util.List; +import java.text.MessageFormat; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -24,13 +23,14 @@ import org.eclipse.core.runtime.IExecutableExtension; import org.eclipse.debug.core.ILaunchConfiguration; import org.eclipse.debug.core.ILaunchConfigurationType; import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy; -import org.eclipse.debug.ui.IDebugUIConstants; import org.eclipse.debug.ui.ILaunchConfigurationDialog; import org.eclipse.debug.ui.ILaunchConfigurationTab; import org.eclipse.jface.operation.IRunnableWithProgress; import org.eclipse.linuxtools.internal.profiling.launch.provider.ProviderProfileConstants; import org.eclipse.linuxtools.profiling.launch.ProfileLaunchConfigurationTabGroup; import org.eclipse.linuxtools.profiling.launch.ProfileLaunchShortcut; +import org.eclipse.swt.SWT; +import org.eclipse.swt.widgets.MessageBox; public class ProviderLaunchShortcut extends ProfileLaunchShortcut implements IExecutableExtension { @@ -66,85 +66,100 @@ public class ProviderLaunchShortcut extends ProfileLaunchShortcut implements IEx @Override protected ILaunchConfiguration findLaunchConfiguration(IBinary bin, String mode) { - String type = getProfilingType(); + + // create a default launch configuration based on the shortcut + ILaunchConfiguration config = createConfiguration(bin, false); + + String providerId = null; + try { + providerId = ProviderLaunchConfigurationDelegate.getProviderIdToRun(config.getWorkingCopy(), type); + } catch (CoreException e1) { + e1.printStackTrace(); + } // check that there exists a provider for the given profiling type - if (ProviderLaunchConfigurationDelegate.getProviderIdToRun(null, type) == null) { + if (providerId == null) { handleFail(Messages.ProviderLaunchShortcut_0 + " " + type); return null; } - // create a launch configuration based on the shortcut - ILaunchConfiguration config = createConfiguration(bin, false); - boolean exists = false; + boolean exists = false; + ILaunchConfiguration[] configs = null; try { - for (ILaunchConfiguration cfg : getLaunchManager().getLaunchConfigurations(getLaunchConfigType())){ - if (areEqual(config, cfg)){ + configs = getLaunchManager().getLaunchConfigurations(getLaunchConfigType()); + + // attributes for which a configuration can be considered valid for + // the current tool and profiling type + String[] relevantAttributes = new String[] { + ProviderProfileConstants.PROVIDER_CONFIG_ATT, + ICDTLaunchConfigurationConstants.ATTR_PROJECT_NAME, + ICDTLaunchConfigurationConstants.ATTR_PROGRAM_NAME }; + + // check that there a exists a valid configuration for the current + // tool and profiling type + for (ILaunchConfiguration cfg : configs) { + if (equalAttributes(config, cfg, relevantAttributes)) { exists = true; + break; } } } catch (CoreException e) { exists = true; } - // only save the configuration if it does not exist - if (! exists) { + // automatically create a configuration if there are none available + if (configs == null || configs.length == 0) { createConfiguration(bin); - } + } else if (!exists) { + String provider = ProviderFramework + .getProviderToolNameFromId(providerId); + + String profileType = Character.toUpperCase(type.charAt(0)) + + type.substring(1); + + // prompt message + String promptMsg = MessageFormat.format( + Messages.ProviderLaunchConfigurationPrompt_0, new String[] { + profileType, provider }); + MessageBox prompt = new MessageBox(getActiveWorkbenchShell(), + SWT.ICON_QUESTION | SWT.YES | SWT.NO); + prompt.setMessage(promptMsg); + + // prompt user for configuration creation + if (prompt.open() == SWT.YES) { + return createConfiguration(bin); + } + } return super.findLaunchConfiguration(bin, mode); } /** + * Check whether configurations cfg1 and cfg2 + * share specified attributes attributes + * * @param cfg1 a launch configuration * @param cfg2 a launch configuration - * @return true if the launch configurations contain the exact - * same attributes, and false otherwise. + * @param attributes attributes to compare + * @return true if specified attributes are equal on both configuration, false otherwise. */ - private boolean areEqual(ILaunchConfiguration cfg1, - ILaunchConfiguration cfg2) { - - // We don't care about these attributes. - final List IGNORED_ATTRS = Arrays.asList(new String [] { - ICDTLaunchConfigurationConstants.ATTR_BUILD_BEFORE_LAUNCH, - ICDTLaunchConfigurationConstants.ATTR_COREFILE_PATH, - IDebugUIConstants.ATTR_CAPTURE_IN_CONSOLE }); - + private boolean equalAttributes(ILaunchConfiguration cfg1, + ILaunchConfiguration cfg2, String[] attributes) { try { - Map attrs1 = cfg1.getAttributes(); - Map attrs2 = cfg2.getAttributes(); - - for (Object key1 : attrs1.keySet()) { - if (! attrs2.containsKey(key1) - && ! IGNORED_ATTRS.contains(key1.toString())) { + for (String attribute : attributes) { + String cfg1Attribute = cfg1.getAttribute(attribute, ""); + String cfg2Attribute = cfg2.getAttribute(attribute, ""); + if (!cfg1Attribute.equals(cfg2Attribute)) { return false; } } - - for (Object key2 : attrs2.keySet()) { - if (! attrs1.containsKey(key2) - && ! IGNORED_ATTRS.contains(key2.toString())) { - return false; - } - } - - for (Object key1 : attrs1.keySet()) { - for (Object key2 : attrs2.keySet()) { - if (key1.toString().equals(key2.toString()) - && ! attrs1.get(key1).toString().equals(attrs2.get(key2).toString())) { - return false; - } - } - } } catch (CoreException e) { return false; } - return true; } - @Override protected void setDefaultProfileAttributes(ILaunchConfigurationWorkingCopy wc) { diff --git a/profiling/org.eclipse.linuxtools.profiling.launch/src/org/eclipse/linuxtools/internal/profiling/launch/provider/launch/messages.properties b/profiling/org.eclipse.linuxtools.profiling.launch/src/org/eclipse/linuxtools/internal/profiling/launch/provider/launch/messages.properties index 9386581337..8308d810c9 100644 --- a/profiling/org.eclipse.linuxtools.profiling.launch/src/org/eclipse/linuxtools/internal/profiling/launch/provider/launch/messages.properties +++ b/profiling/org.eclipse.linuxtools.profiling.launch/src/org/eclipse/linuxtools/internal/profiling/launch/provider/launch/messages.properties @@ -5,3 +5,6 @@ ProviderOptionsTab_0=No plug-in provider found in launch configuration UseProjectSetting_0=Enable project-specific settings ProjectSettings_0=Project Settings PreferenceLink_0=Configure Workspace Settings... +ProviderLaunchConfigurationPrompt_0=This project has one or more Profile {0} configurations set up for the executable selected, \ +but none of these profiling configurations use the current default tool for Profile {0} which is set to use {1}. \n\n\ +Do you want to create and use a new profiling configuration that uses {1}? \ No newline at end of file -- cgit v1.2.3