diff options
author | Camilo Bernal | 2012-07-30 20:40:24 +0000 |
---|---|---|
committer | Roland Grunberg | 2012-08-03 17:48:48 +0000 |
commit | d871a1ec508061697044f254182981a27e1ac388 (patch) | |
tree | 8c1100df1ea232e7b5e81e5ba22e45949c6ca176 | |
parent | bda13eb5ed6121e27f744db37c0af9ac428799e2 (diff) | |
download | org.eclipse.linuxtools-d871a1ec508061697044f254182981a27e1ac388.tar.gz org.eclipse.linuxtools-d871a1ec508061697044f254182981a27e1ac388.tar.xz org.eclipse.linuxtools-d871a1ec508061697044f254182981a27e1ac388.zip |
Add preselection of launch provider backend to launch provider's config tab.
When the launch provider's config tab is opened, a previously selected
provider is preselected; if no provider is found in the launch configuration,
the provider with the highest priority is preselected. This change required
refactoring that allowed for a fix of the launch configuration persistence
problem with the launch provider config tab to be made.
Change-Id: Ibca0dbc89a25563f130d4a33d17677f29edf8fc0
Reviewed-on: https://git.eclipse.org/r/7038
Reviewed-by: Roland Grunberg <rgrunber@redhat.com>
IP-Clean: Roland Grunberg <rgrunber@redhat.com>
Tested-by: Roland Grunberg <rgrunber@redhat.com>
3 files changed, 213 insertions, 40 deletions
diff --git a/profiling/org.eclipse.linuxtools.profiling.launch/META-INF/MANIFEST.MF b/profiling/org.eclipse.linuxtools.profiling.launch/META-INF/MANIFEST.MF index 89547e61a5..09ff3f7bf7 100644 --- a/profiling/org.eclipse.linuxtools.profiling.launch/META-INF/MANIFEST.MF +++ b/profiling/org.eclipse.linuxtools.profiling.launch/META-INF/MANIFEST.MF @@ -2,7 +2,7 @@ Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: %Bundle-Name.0 Bundle-SymbolicName: org.eclipse.linuxtools.profiling.launch;singleton:=true -Bundle-Version: 1.1.0.qualifier +Bundle-Version: 1.2.0.qualifier Bundle-Activator: org.eclipse.linuxtools.internal.profiling.launch.ProfileLaunchPlugin Require-Bundle: org.eclipse.ui, org.eclipse.core.runtime, diff --git a/profiling/org.eclipse.linuxtools.profiling.launch/src/org/eclipse/linuxtools/profiling/launch/ProfileLaunchConfigurationTabGroup.java b/profiling/org.eclipse.linuxtools.profiling.launch/src/org/eclipse/linuxtools/profiling/launch/ProfileLaunchConfigurationTabGroup.java index 6ae830cc8c..3b018d1e52 100644 --- a/profiling/org.eclipse.linuxtools.profiling.launch/src/org/eclipse/linuxtools/profiling/launch/ProfileLaunchConfigurationTabGroup.java +++ b/profiling/org.eclipse.linuxtools.profiling.launch/src/org/eclipse/linuxtools/profiling/launch/ProfileLaunchConfigurationTabGroup.java @@ -12,6 +12,8 @@ package org.eclipse.linuxtools.profiling.launch; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; +import java.util.Comparator; import java.util.HashMap; import org.eclipse.cdt.launch.ui.CArgumentsTab; @@ -120,6 +122,84 @@ public abstract class ProfileLaunchConfigurationTabGroup extends AbstractLaunchC } return null; } + /** + * Get id of highest priority profiling tabgroup launch configuration that + * provides the type of profiling. This looks through extensions of the + * <code>org.eclipse.linuxtools.profiling.launch.launchProvider</code> + * extension point that have a specific type attribute. + * + * @param type A profiling type (eg. memory, snapshot, timing, etc.) + * @return an id of the profiling launch shortcut that implements + * <code>ProfileLaunchShortcut</code> and provides the necessary + * profiling type, or <code>null</code> if none could be found. + * @since 1.2 + */ + public static String getHighestProviderId(String type) { + IExtensionPoint extPoint = Platform.getExtensionRegistry() + .getExtensionPoint(ProfileLaunchPlugin.PLUGIN_ID, + "launchProvider"); //$NON-NLS-1$ + IConfigurationElement[] configs = extPoint.getConfigurationElements(); + ArrayList<IConfigurationElement> configList = new ArrayList<IConfigurationElement>(); + + for (IConfigurationElement config : configs) { + if (config.getName().equals("provider")) { //$NON-NLS-1$ + String currentName = config.getAttribute("name"); //$NON-NLS-1$ + String currentType = config.getAttribute("type"); //$NON-NLS-1$ + String tabgroup = config.getAttribute("tabgroup"); //$NON-NLS-1$ + if (currentType != null && tabgroup != null + && currentName != null && currentType.equals(type)) { + + String priority = config.getAttribute("priority"); + if (priority != null) { + try { + Integer.parseInt(priority); + configList.add(config); + } catch (NumberFormatException e) { + // continue + } + } + } + } + } + + Collections.sort(configList, new Comparator<IConfigurationElement>() { + public int compare(IConfigurationElement c1, + IConfigurationElement c2) { + int p1, p2; + // If priority is not an int or is < 0, corresponding config has + // lowest priority. + try { + p1 = Integer.parseInt(c1.getAttribute("priority")); + if (p1 <= 0) { + return 1; + } + } catch (NumberFormatException e) { + return 1; + } + try { + p2 = Integer.parseInt(c2.getAttribute("priority")); + if (p2 <= 0) { + return -1; + } + } catch (NumberFormatException e) { + return -1; + } + return p1 < p2 ? -1 : 1; + } + }); + + for (IConfigurationElement config : configList) { + try { + Object obj = config.createExecutableExtension("tabgroup"); //$NON-NLS-1$ + if (obj instanceof ProfileLaunchConfigurationTabGroup) { + return config.getAttribute("id"); + } + } catch (CoreException e) { + // continue, other configuration may succeed + } + } + return null; + } /** * Get all IDs of the specific type. This looks through extensions of @@ -129,8 +209,23 @@ public abstract class ProfileLaunchConfigurationTabGroup extends AbstractLaunchC * @param type A profiling type (eg. memory, snapshot, timing, etc.) * @return A <code>String []</code> of all IDs of the specific type. * @since 1.1 + * @deprecated */ + @Deprecated public static String[] getTabGroupIdsForType(String type) { + return getProviderIdsForType(type); + } + + /** + * Get all IDs of the specific type. This looks through extensions of + * the extension point <code>org.eclipse.linuxtools.profiling.launch.launchProvider</code> + * that have a specific type. + * + * @param type A profiling type (eg. memory, snapshot, timing, etc.) + * @return A <code>String []</code> of all IDs of the specific type. + * @since 1.2 + */ + public static String[] getProviderIdsForType(String type) { ArrayList<String> ret = new ArrayList<String> (); IExtensionPoint extPoint = Platform.getExtensionRegistry() .getExtensionPoint(ProfileLaunchPlugin.PLUGIN_ID, @@ -158,9 +253,9 @@ public abstract class ProfileLaunchConfigurationTabGroup extends AbstractLaunchC * @param type A profiling type (eg. memory, snapshot, timing, etc.) * @return A <code>HashMap<String, String></code> of all pairs of names and IDs * of the specific type. - * @since 1.1 + * @since 1.2 */ - public static HashMap<String, String> getTabGroupNamesForType(String type) { + public static HashMap<String, String> getProviderNamesForType(String type) { HashMap<String, String> ret = new HashMap<String, String>(); IExtensionPoint extPoint = Platform.getExtensionRegistry() .getExtensionPoint(ProfileLaunchPlugin.PLUGIN_ID, diff --git a/profiling/org.eclipse.linuxtools.profiling.snapshot/src/org/eclipse/linuxtools/profiling/snapshot/SnapshotOptionsTab.java b/profiling/org.eclipse.linuxtools.profiling.snapshot/src/org/eclipse/linuxtools/profiling/snapshot/SnapshotOptionsTab.java index 980dadc330..df3d9b264c 100644 --- a/profiling/org.eclipse.linuxtools.profiling.snapshot/src/org/eclipse/linuxtools/profiling/snapshot/SnapshotOptionsTab.java +++ b/profiling/org.eclipse.linuxtools.profiling.snapshot/src/org/eclipse/linuxtools/profiling/snapshot/SnapshotOptionsTab.java @@ -11,6 +11,7 @@ package org.eclipse.linuxtools.profiling.snapshot; import java.util.HashMap; +import java.util.Map.Entry; import java.util.Set; import org.eclipse.core.runtime.CoreException; @@ -35,8 +36,9 @@ public class SnapshotOptionsTab extends ProfileLaunchConfigurationTab { Combo providerCombo; AbstractLaunchConfigurationTab[] tabs; ILaunchConfiguration initial; - String providerId = ""; HashMap<String, String> comboItems; + CTabFolder tabgroup; + Boolean initialized; public void createControl(Composite parent) { top = new Composite(parent, SWT.NONE); @@ -44,43 +46,59 @@ public class SnapshotOptionsTab extends ProfileLaunchConfigurationTab { top.setLayout(new GridLayout(1, true)); providerCombo = new Combo(top, SWT.READ_ONLY); comboItems = ProfileLaunchConfigurationTabGroup - .getTabGroupNamesForType("snapshot"); + .getProviderNamesForType("snapshot"); Set<String> providerNames = comboItems.keySet(); providerCombo.setItems(providerNames.toArray(new String[0])); - final CTabFolder tabgroup = new CTabFolder(top, SWT.NONE); + tabgroup = new CTabFolder(top, SWT.NONE); providerCombo.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { - // dispose of old tabs - for (CTabItem item : tabgroup.getItems()) { - item.dispose(); - } - - providerId = comboItems.get(providerCombo.getText()); - // get the tabs associated with the selected ID - tabs = ProfileLaunchConfigurationTabGroup - .getTabGroupProviderFromId(providerId) - .getProfileTabs(); - - // create the tab item, and load the specified tab inside - for (ILaunchConfigurationTab tab : tabs) { - tab.setLaunchConfigurationDialog(getLaunchConfigurationDialog()); - CTabItem item = new CTabItem(tabgroup, SWT.NONE); - item.setText(tab.getName()); - item.setImage(tab.getImage()); - - tab.createControl(tabgroup); - item.setControl(tab.getControl()); - } - - // initialize all tab widgets based on the configuration + String curProviderId = comboItems.get(providerCombo.getText()); + loadTabGroupItems(tabgroup, curProviderId); initializeFrom(initial); top.layout(); } }); + } + + public void loadTabGroupItems(CTabFolder tabgroup, String curProviderId){ + // dispose of old tabs + for (CTabItem item : tabgroup.getItems()) { + item.dispose(); + } + + ProfileLaunchConfigurationTabGroup tabGroupConfig; + if (curProviderId == null || "".equals(curProviderId)) { + // get id of highest priority provider + curProviderId = ProfileLaunchConfigurationTabGroup + .getHighestProviderId("snapshot"); + } + tabGroupConfig = ProfileLaunchConfigurationTabGroup + .getTabGroupProviderFromId(curProviderId); + if (tabGroupConfig == null) { + // no provider found + return; + } + tabs = tabGroupConfig.getProfileTabs(); + setProvider(curProviderId); + + // Show provider name in combo. + int itemIndex = getComboItemIndexFromId(curProviderId); + providerCombo.select(itemIndex); + + // create the tab item, and load the specified tab inside + for (ILaunchConfigurationTab tab : tabs) { + tab.setLaunchConfigurationDialog(getLaunchConfigurationDialog()); + CTabItem item = new CTabItem(tabgroup, SWT.NONE); + item.setText(tab.getName()); + item.setImage(tab.getImage()); + + tab.createControl(tabgroup); + item.setControl(tab.getControl()); + } } public void setDefaults(ILaunchConfigurationWorkingCopy configuration) { @@ -101,22 +119,40 @@ public class SnapshotOptionsTab extends ProfileLaunchConfigurationTab { * that we can properly load the widgets the first time. */ - // store current provider id in the configuration - if (configuration != null) { - setProvider(configuration); - } - if (initial == null){ - initial = configuration; + // starting initialization of this tab's controls + initialized = false; + + // update current configuration (initial) with configuration being + // passed in + initial = configuration; + + // check if there exists a launch provider id in the configuration + if (initial != null) { + try { + String providerId = initial.getAttribute("provider", ""); + if (providerId != null && !providerId.equals("")) { + // load provider corresponding to specified id + loadTabGroupItems(tabgroup, providerId); + } else { + // load highest priority provider if none found + loadTabGroupItems(tabgroup, null); + } + } catch (CoreException e) { + // continue, initialize tabs + } } - if (providerCombo != null && !providerCombo.getText().equals("")) { + if (tabs != null) { for (AbstractLaunchConfigurationTab tab : tabs) { tab.initializeFrom(configuration); } } + // finished initialization + initialized = true; } public void performApply(ILaunchConfigurationWorkingCopy configuration) { - if (providerCombo != null && !providerCombo.getText().equals("")) { + // make sure tabs are not null, and the tab's controls have been initialized. + if (tabs != null && initialized) { for (AbstractLaunchConfigurationTab tab : tabs) { tab.performApply(configuration); } @@ -126,18 +162,60 @@ public class SnapshotOptionsTab extends ProfileLaunchConfigurationTab { public String getName() { return "Snapshot"; } + /** * Set the provider attribute in the specified configuration. * @param configuration a configuration */ - public void setProvider(ILaunchConfiguration configuration) { + public void setProvider(String providerId) { try { - ILaunchConfigurationWorkingCopy wc = configuration.getWorkingCopy(); + ILaunchConfigurationWorkingCopy wc = initial.getWorkingCopy(); wc.setAttribute("provider", providerId); - configuration = wc.doSave(); + initial = wc.doSave(); } catch (CoreException e1) { e1.printStackTrace(); } } + /** + * Get Combo item name from specified id + * + * @param id provider id + * @return name of item, <code>null</code> if no entry found with given id. + */ + private String getComboItemNameFromId(String id) { + for (Entry<String, String> entry : comboItems.entrySet()) { + if (id.equals(entry.getValue())) { + return entry.getKey(); + } + } + return null; + } + + /** + * Get index of specific name in the combo items list + * + * @param name name of item + * @return index of given name, -1 if it not found + */ + private int getItemIndex(String name) { + int itemCount = providerCombo.getItemCount(); + for (int i = 0; i < itemCount; i++) { + if (providerCombo.getItem(i).equals(name)) { + return i; + } + } + return -1; + } + + /** + * Get index of specific id in the combo items list + * + * @param id + * @return index of given id in combo items list, -1 if it not found. + */ + private int getComboItemIndexFromId(String id) { + String providerName = getComboItemNameFromId(id); + return getItemIndex(providerName); + } } |