1
/*******************************************************************************
2
* Copyright (c) 2012 Red Hat, Inc.
3
* All rights reserved. This program and the accompanying materials
4
* are made available under the terms of the Eclipse Public License v1.0
5
* which accompanies this distribution, and is available at
6
* http://www.eclipse.org/legal/epl-v10.html
9
* Red Hat initial API and implementation
10
*******************************************************************************/
11
package org.eclipse.linuxtools.internal.profiling.launch.provider;
13
import java.util.HashMap;
14
import java.util.Map.Entry;
16
import org.eclipse.cdt.debug.core.ICDTLaunchConfigurationConstants;
17
import org.eclipse.core.runtime.CoreException;
18
import org.eclipse.debug.core.ILaunchConfiguration;
19
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
20
import org.eclipse.debug.ui.AbstractLaunchConfigurationTab;
21
import org.eclipse.debug.ui.ILaunchConfigurationTab;
22
import org.eclipse.linuxtools.internal.profiling.launch.provider.launch.Messages;
23
import org.eclipse.linuxtools.internal.profiling.launch.provider.launch.ProviderFramework;
24
import org.eclipse.linuxtools.internal.profiling.launch.provider.launch.ProviderLaunchConfigurationDelegate;
25
import org.eclipse.linuxtools.internal.profiling.launch.provider.launch.ProviderLaunchShortcut;
26
import org.eclipse.linuxtools.profiling.launch.ProfileLaunchConfigurationTab;
27
import org.eclipse.linuxtools.profiling.launch.ProfileLaunchConfigurationTabGroup;
28
import org.eclipse.swt.SWT;
29
import org.eclipse.swt.custom.CTabFolder;
30
import org.eclipse.swt.custom.CTabItem;
31
import org.eclipse.swt.events.SelectionAdapter;
32
import org.eclipse.swt.events.SelectionEvent;
33
import org.eclipse.swt.layout.GridData;
34
import org.eclipse.swt.layout.GridLayout;
35
import org.eclipse.swt.widgets.Combo;
36
import org.eclipse.swt.widgets.Composite;
38
public class ProviderOptionsTab extends ProfileLaunchConfigurationTab {
44
AbstractLaunchConfigurationTab[] tabs;
45
ILaunchConfiguration initial;
46
HashMap<String, String> comboItems;
49
// if tabs are being initialized do not call performApply()
50
HashMap<String, Boolean> initialized = new HashMap<String, Boolean> ();
53
* ProviderOptionsTab constructor.
55
* @param profilingType String type of profiling this tab will be used for.
56
* @param profilingName String name of this tab to be displayed.
58
public ProviderOptionsTab(String profilingType, String profilingName) {
63
public void createControl(Composite parent) {
64
top = new Composite(parent, SWT.NONE);
66
top.setLayout(new GridLayout(1, true));
67
providerCombo = new Combo(top, SWT.READ_ONLY);
68
comboItems = ProviderFramework
69
.getProviderNamesForType(getProfilingType());
70
Set<String> providerNames = comboItems.keySet();
71
providerCombo.setItems(providerNames.toArray(new String[0]));
73
tabgroup = new CTabFolder(top, SWT.NONE);
74
tabgroup.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true,
77
providerCombo.addSelectionListener(new SelectionAdapter() {
79
public void widgetSelected(SelectionEvent e) {
80
String curProviderId = comboItems.get(providerCombo.getText());
81
loadTabGroupItems(tabgroup, curProviderId);
82
initializeFrom(initial);
83
// Since we are calling initializeFrom manually, we have to
84
// update the launch configuration dialog manually to ensure
85
// initial validation on the configuration.
86
updateLaunchConfigurationDialog();
92
public void loadTabGroupItems(CTabFolder tabgroup, String curProviderId) {
93
// dispose of old tabs and their state
94
for (CTabItem item : tabgroup.getItems()) {
99
ProfileLaunchConfigurationTabGroup tabGroupConfig;
101
if (curProviderId == null || "".equals(curProviderId)) {
102
// get the id of a provider
103
curProviderId = ProviderLaunchConfigurationDelegate
104
.getProviderIdToRun(null, getProfilingType());
107
// starting initialization of this tab's controls
108
initialized.put(curProviderId, false);
110
tabGroupConfig = ProfileLaunchConfigurationTabGroup
111
.getTabGroupProviderFromId(curProviderId);
112
if (tabGroupConfig == null) {
116
tabs = tabGroupConfig.getProfileTabs();
117
setProvider(curProviderId);
119
// Show provider name in combo.
120
int itemIndex = getComboItemIndexFromId(curProviderId);
121
providerCombo.select(itemIndex);
123
// Set name of configuration.
124
setConfigurationName(providerCombo.getText());
126
// create the tab item, and load the specified tab inside
127
for (ILaunchConfigurationTab tab : tabs) {
128
tab.setLaunchConfigurationDialog(getLaunchConfigurationDialog());
129
CTabItem item = new CTabItem(tabgroup, SWT.NONE);
130
item.setText(tab.getName());
131
item.setImage(tab.getImage());
133
tab.createControl(tabgroup);
134
item.setControl(tab.getControl());
135
tabgroup.setSelection(0);
139
public void setDefaults(ILaunchConfigurationWorkingCopy configuration) {
140
if (providerCombo != null && !providerCombo.getText().equals("")) {
141
for (AbstractLaunchConfigurationTab tab : tabs) {
142
tab.setDefaults(configuration);
147
public void initializeFrom(ILaunchConfiguration configuration) {
149
* First time the configuration is selected.
151
* This is a cheap way to get access to the launch configuration. Our
152
* tabs are loaded dynamically, so the tab group doesn't "know" about
153
* them. We get access to this launch configuration to ensure that we
154
* can properly load the widgets the first time.
157
// update current configuration (initial) with configuration being
159
initial = configuration;
162
// check if there exists a launch provider id in the configuration
163
if (initial != null) {
165
String providerId = initial.getAttribute(
166
ProviderProfileConstants.PROVIDER_CONFIG_ATT, "");
167
if (providerId != null && !providerId.equals("")) {
168
// load provider corresponding to specified id
169
loadTabGroupItems(tabgroup, providerId);
171
// find a provider to load if none found
172
loadTabGroupItems(tabgroup, null);
174
} catch (CoreException e) {
175
// continue, initialize tabs
179
for (AbstractLaunchConfigurationTab tab : tabs) {
180
tab.initializeFrom(configuration);
184
// finished initialization
185
initialized.put(getProviderId(), true);
188
public void performApply(ILaunchConfigurationWorkingCopy configuration) {
189
// make sure tabs are not null, and the tab's controls have been
192
Boolean isInitialized = initialized.get(getProviderId());
193
isInitialized = (isInitialized != null) ? isInitialized : false;
195
if (tabs != null && isInitialized) {
196
for (AbstractLaunchConfigurationTab tab : tabs) {
197
tab.performApply(configuration);
203
* Set the provider attribute in the specified configuration.
205
* @param configuration a configuration
207
private void setProvider(String providerId) {
209
ILaunchConfigurationWorkingCopy wc = initial.getWorkingCopy();
210
wc.setAttribute(ProviderProfileConstants.PROVIDER_CONFIG_ATT,
212
initial = wc.doSave();
213
} catch (CoreException e1) {
214
e1.printStackTrace();
219
* Get the provider ID for the provider of the currently loaded
222
* @return the provider ID or an empty string if the configuration
223
* has no provider ID defined.
225
private String getProviderId() {
227
return initial.getAttribute(
228
ProviderProfileConstants.PROVIDER_CONFIG_ATT, "");
229
} catch (CoreException e) {
235
* Get Combo item name from specified id
237
* @param id provider id
238
* @return name of item, <code>null</code> if no entry found with given id.
240
private String getComboItemNameFromId(String id) {
241
for (Entry<String, String> entry : comboItems.entrySet()) {
242
if (id.equals(entry.getValue())) {
243
return entry.getKey();
250
* Get index of specific name in the combo items list
252
* @param name name of item
253
* @return index of given name, -1 if it not found
255
private int getItemIndex(String name) {
256
int itemCount = providerCombo.getItemCount();
257
for (int i = 0; i < itemCount; i++) {
258
if (providerCombo.getItem(i).equals(name)) {
266
* Get index of specific id in the combo items list
269
* @return index of given id in combo items list, -1 if it not found.
271
private int getComboItemIndexFromId(String id) {
272
String providerName = getComboItemNameFromId(id);
273
return getItemIndex(providerName);
277
public boolean isValid(ILaunchConfiguration config) {
280
provider = config.getAttribute(
281
ProviderProfileConstants.PROVIDER_CONFIG_ATT, "");
282
} catch (CoreException e) {
283
setErrorMessage(e.getMessage());
286
if (provider.equals("")) {
287
setErrorMessage(Messages.ProviderOptionsTab_0);
291
Boolean isInitialized = initialized.get(getProviderId());
294
// Tabs should not be null after initialization.
299
// Validate tab configurations of underlying tool.
300
for (AbstractLaunchConfigurationTab tab : tabs) {
301
if (!tab.isValid(config)) {
310
* Get profiling type of the configuration.
312
* @return String profiling type this plug-in supports.
314
protected String getProfilingType() {
319
* Get name of profiling type that used for this tab.
321
* @return String profiling name.
323
public String getName() {
328
* Set name of the launch configuration.
330
* @param newToolName String tool name to be appended to configuration name,
332
private void setConfigurationName(String newToolName) {
334
String currentToolName = initial.getAttribute(
335
ProviderProfileConstants.PROVIDER_CONFIG_TOOLNAME_ATT, "");
337
// Append the new tool name as long as the current and new tool
338
// names are different.
339
if (newToolName != null && !newToolName.equals("")
340
&& !currentToolName.equals(newToolName)) {
342
String projectName = initial.getAttribute(
343
ICDTLaunchConfigurationConstants.ATTR_PROJECT_NAME, "");
345
// String of the form <project name> [<tool name>].
346
String newConfigurationName = ProviderLaunchShortcut
347
.generateProviderConfigurationName(projectName,
350
// Unique name of the form <project name> [<tool name>]{(<number>)}.
351
String newUniqueToolName = getLaunchManager()
352
.generateLaunchConfigurationName(newConfigurationName);
354
// Save changes in current configuration.
355
ILaunchConfigurationWorkingCopy wc = initial.getWorkingCopy();
356
wc.rename(newUniqueToolName);
358
ProviderProfileConstants.PROVIDER_CONFIG_TOOLNAME_ATT,
360
initial = wc.doSave();
362
// Set name field in launch configuration dialog to avoid the
363
// new configuration name from being overwritten.
364
getLaunchConfigurationDialog().setName(newUniqueToolName);
366
} catch (CoreException e) {
367
// If unable to set the name, leave the original name as is.