~ubuntu-branches/ubuntu/trusty/eclipse-linuxtools/trusty

« back to all changes in this revision

Viewing changes to profiling/org.eclipse.linuxtools.profiling.launch/src/org/eclipse/linuxtools/internal/profiling/launch/provider/ProviderOptionsTab.java

  • Committer: Package Import Robot
  • Author(s): tony mancill
  • Date: 2013-05-13 21:43:22 UTC
  • mfrom: (1.2.1) (2.1.2 experimental)
  • Revision ID: package-import@ubuntu.com-20130513214322-6frgd9du1n0w2uo7
Tags: 1.2.1-1
* Team upload.
* New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
7
 *
 
8
 * Contributors:
 
9
 *    Red Hat initial API and implementation
 
10
 *******************************************************************************/
 
11
package org.eclipse.linuxtools.internal.profiling.launch.provider;
 
12
 
 
13
import java.util.HashMap;
 
14
import java.util.Map.Entry;
 
15
import java.util.Set;
 
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;
 
37
 
 
38
public class ProviderOptionsTab extends ProfileLaunchConfigurationTab {
 
39
 
 
40
        String type;
 
41
        String name;
 
42
        Composite top;
 
43
        Combo providerCombo;
 
44
        AbstractLaunchConfigurationTab[] tabs;
 
45
        ILaunchConfiguration initial;
 
46
        HashMap<String, String> comboItems;
 
47
        CTabFolder tabgroup;
 
48
 
 
49
        // if tabs are being initialized do not call performApply()
 
50
        HashMap<String, Boolean> initialized = new HashMap<String, Boolean> ();
 
51
 
 
52
        /**
 
53
         * ProviderOptionsTab constructor.
 
54
         *
 
55
         * @param profilingType String type of profiling this tab will be used for.
 
56
         * @param profilingName String name of this tab to be displayed.
 
57
         */
 
58
        public ProviderOptionsTab(String profilingType, String profilingName) {
 
59
                type = profilingType;
 
60
                name = profilingName;
 
61
        }
 
62
 
 
63
        public void createControl(Composite parent) {
 
64
                top = new Composite(parent, SWT.NONE);
 
65
                setControl(top);
 
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]));
 
72
 
 
73
                tabgroup = new CTabFolder(top, SWT.NONE);
 
74
                tabgroup.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true,
 
75
                                true));
 
76
 
 
77
                providerCombo.addSelectionListener(new SelectionAdapter() {
 
78
                        @Override
 
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();
 
87
                                top.layout();
 
88
                        }
 
89
                });
 
90
        }
 
91
 
 
92
        public void loadTabGroupItems(CTabFolder tabgroup, String curProviderId) {
 
93
                // dispose of old tabs and their state
 
94
                for (CTabItem item : tabgroup.getItems()) {
 
95
                        item.dispose();
 
96
                }
 
97
                initialized.clear();
 
98
 
 
99
                ProfileLaunchConfigurationTabGroup tabGroupConfig;
 
100
 
 
101
                if (curProviderId == null || "".equals(curProviderId)) {
 
102
                        // get the id of a provider
 
103
                        curProviderId = ProviderLaunchConfigurationDelegate
 
104
                                        .getProviderIdToRun(null, getProfilingType());
 
105
                }
 
106
 
 
107
                // starting initialization of this tab's controls
 
108
                initialized.put(curProviderId, false);
 
109
 
 
110
                tabGroupConfig = ProfileLaunchConfigurationTabGroup
 
111
                                .getTabGroupProviderFromId(curProviderId);
 
112
                if (tabGroupConfig == null) {
 
113
                        // no provider found
 
114
                        return;
 
115
                }
 
116
                tabs = tabGroupConfig.getProfileTabs();
 
117
                setProvider(curProviderId);
 
118
 
 
119
                // Show provider name in combo.
 
120
                int itemIndex = getComboItemIndexFromId(curProviderId);
 
121
                providerCombo.select(itemIndex);
 
122
 
 
123
                // Set name of configuration.
 
124
                setConfigurationName(providerCombo.getText());
 
125
 
 
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());
 
132
 
 
133
                        tab.createControl(tabgroup);
 
134
                        item.setControl(tab.getControl());
 
135
                        tabgroup.setSelection(0);
 
136
                }
 
137
        }
 
138
 
 
139
        public void setDefaults(ILaunchConfigurationWorkingCopy configuration) {
 
140
                if (providerCombo != null && !providerCombo.getText().equals("")) {
 
141
                        for (AbstractLaunchConfigurationTab tab : tabs) {
 
142
                                tab.setDefaults(configuration);
 
143
                        }
 
144
                }
 
145
        }
 
146
 
 
147
        public void initializeFrom(ILaunchConfiguration configuration) {
 
148
                /**
 
149
                 * First time the configuration is selected.
 
150
                 *
 
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.
 
155
                 */
 
156
 
 
157
                // update current configuration (initial) with configuration being
 
158
                // passed in
 
159
                initial = configuration;
 
160
 
 
161
 
 
162
                // check if there exists a launch provider id in the configuration
 
163
                if (initial != null) {
 
164
                        try {
 
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);
 
170
                                } else {
 
171
                                        // find a provider to load if none found
 
172
                                        loadTabGroupItems(tabgroup, null);
 
173
                                }
 
174
                        } catch (CoreException e) {
 
175
                                // continue, initialize tabs
 
176
                        }
 
177
                }
 
178
                if (tabs != null) {
 
179
                        for (AbstractLaunchConfigurationTab tab : tabs) {
 
180
                                tab.initializeFrom(configuration);
 
181
                        }
 
182
                }
 
183
 
 
184
                // finished initialization
 
185
                initialized.put(getProviderId(), true);
 
186
        }
 
187
 
 
188
        public void performApply(ILaunchConfigurationWorkingCopy configuration) {
 
189
                // make sure tabs are not null, and the tab's controls have been
 
190
                // initialized.
 
191
 
 
192
                Boolean isInitialized = initialized.get(getProviderId());
 
193
                isInitialized = (isInitialized != null) ? isInitialized : false;
 
194
 
 
195
                if (tabs != null && isInitialized) {
 
196
                        for (AbstractLaunchConfigurationTab tab : tabs) {
 
197
                                tab.performApply(configuration);
 
198
                        }
 
199
                }
 
200
        }
 
201
 
 
202
        /**
 
203
         * Set the provider attribute in the specified configuration.
 
204
         *
 
205
         * @param configuration a configuration
 
206
         */
 
207
        private void setProvider(String providerId) {
 
208
                try {
 
209
                        ILaunchConfigurationWorkingCopy wc = initial.getWorkingCopy();
 
210
                        wc.setAttribute(ProviderProfileConstants.PROVIDER_CONFIG_ATT,
 
211
                                        providerId);
 
212
                        initial = wc.doSave();
 
213
                } catch (CoreException e1) {
 
214
                        e1.printStackTrace();
 
215
                }
 
216
        }
 
217
 
 
218
        /**
 
219
         * Get the provider ID for the provider of the currently loaded
 
220
         * configuration.
 
221
         *
 
222
         * @return the provider ID or an empty string if the configuration
 
223
         * has no provider ID defined.
 
224
         */
 
225
        private String getProviderId() {
 
226
                try {
 
227
                        return initial.getAttribute(
 
228
                                        ProviderProfileConstants.PROVIDER_CONFIG_ATT, "");
 
229
                } catch (CoreException e) {
 
230
                        return "";
 
231
                }
 
232
        }
 
233
 
 
234
        /**
 
235
         * Get Combo item name from specified id
 
236
         *
 
237
         * @param id provider id
 
238
         * @return name of item, <code>null</code> if no entry found with given id.
 
239
         */
 
240
        private String getComboItemNameFromId(String id) {
 
241
                for (Entry<String, String> entry : comboItems.entrySet()) {
 
242
                        if (id.equals(entry.getValue())) {
 
243
                                return entry.getKey();
 
244
                        }
 
245
                }
 
246
                return null;
 
247
        }
 
248
 
 
249
        /**
 
250
         * Get index of specific name in the combo items list
 
251
         *
 
252
         * @param name name of item
 
253
         * @return index of given name, -1 if it not found
 
254
         */
 
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)) {
 
259
                                return i;
 
260
                        }
 
261
                }
 
262
                return -1;
 
263
        }
 
264
 
 
265
        /**
 
266
         * Get index of specific id in the combo items list
 
267
         *
 
268
         * @param id
 
269
         * @return index of given id in combo items list, -1 if it not found.
 
270
         */
 
271
        private int getComboItemIndexFromId(String id) {
 
272
                String providerName = getComboItemNameFromId(id);
 
273
                return getItemIndex(providerName);
 
274
        }
 
275
 
 
276
        @Override
 
277
        public boolean isValid(ILaunchConfiguration config) {
 
278
                String provider;
 
279
                try {
 
280
                        provider = config.getAttribute(
 
281
                                        ProviderProfileConstants.PROVIDER_CONFIG_ATT, "");
 
282
                } catch (CoreException e) {
 
283
                        setErrorMessage(e.getMessage());
 
284
                        return false;
 
285
                }
 
286
                if (provider.equals("")) {
 
287
                        setErrorMessage(Messages.ProviderOptionsTab_0);
 
288
                        return false;
 
289
                }
 
290
 
 
291
                Boolean isInitialized = initialized.get(getProviderId());
 
292
 
 
293
                if (isInitialized) {
 
294
                        // Tabs should not be null after initialization.
 
295
                        if (tabs == null) {
 
296
                                return false;
 
297
                        }
 
298
 
 
299
                        // Validate tab configurations of underlying tool.
 
300
                        for (AbstractLaunchConfigurationTab tab : tabs) {
 
301
                                if (!tab.isValid(config)) {
 
302
                                        return false;
 
303
                                }
 
304
                        }
 
305
                }
 
306
                return true;
 
307
        }
 
308
 
 
309
        /**
 
310
         * Get profiling type of the configuration.
 
311
         * 
 
312
         * @return String profiling type this plug-in supports.
 
313
         */
 
314
        protected String getProfilingType() {
 
315
                return type;
 
316
        }
 
317
 
 
318
        /**
 
319
         * Get name of profiling type that used for this tab.
 
320
         * 
 
321
         * @return String profiling name.
 
322
         */
 
323
        public String getName() {
 
324
                return name;
 
325
        }
 
326
 
 
327
        /**
 
328
         * Set name of the launch configuration.
 
329
         *
 
330
         * @param newToolName String tool name to be appended to configuration name,
 
331
         */
 
332
        private void setConfigurationName(String newToolName) {
 
333
                try {
 
334
                        String currentToolName = initial.getAttribute(
 
335
                                        ProviderProfileConstants.PROVIDER_CONFIG_TOOLNAME_ATT, "");
 
336
 
 
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)) {
 
341
 
 
342
                                String projectName = initial.getAttribute(
 
343
                                                ICDTLaunchConfigurationConstants.ATTR_PROJECT_NAME, "");
 
344
 
 
345
                                // String of the form <project name> [<tool name>].
 
346
                                String newConfigurationName = ProviderLaunchShortcut
 
347
                                                .generateProviderConfigurationName(projectName,
 
348
                                                                newToolName);
 
349
 
 
350
                                // Unique name of the form <project name> [<tool name>]{(<number>)}.
 
351
                                String newUniqueToolName = getLaunchManager()
 
352
                                                .generateLaunchConfigurationName(newConfigurationName);
 
353
 
 
354
                                // Save changes in current configuration.
 
355
                                ILaunchConfigurationWorkingCopy wc = initial.getWorkingCopy();
 
356
                                wc.rename(newUniqueToolName);
 
357
                                wc.setAttribute(
 
358
                                                ProviderProfileConstants.PROVIDER_CONFIG_TOOLNAME_ATT,
 
359
                                                newToolName);
 
360
                                initial = wc.doSave();
 
361
 
 
362
                                // Set name field in launch configuration dialog to avoid the
 
363
                                // new configuration name from being overwritten.
 
364
                                getLaunchConfigurationDialog().setName(newUniqueToolName);
 
365
                        }
 
366
                } catch (CoreException e) {
 
367
                        // If unable to set the name, leave the original name as is.
 
368
                }
 
369
        }
 
370
}