Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1572010d6ee2e33c7c6af7af664022b66ccbee5e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*******************************************************************************
 * Copyright (c) 2008, 2017 Wind River Systems, Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tcf.internal.cdt.ui;

import java.io.File;
import java.util.LinkedHashMap;
import java.util.Map;

import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.model.CModelException;
import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.model.IBinary;
import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.settings.model.ICConfigurationDescription;
import org.eclipse.cdt.core.settings.model.ICProjectDescription;
import org.eclipse.cdt.launch.LaunchUtils;
import org.eclipse.cdt.ui.CElementLabelProvider;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.debug.ui.DebugUITools;
import org.eclipse.debug.ui.IDebugUIConstants;
import org.eclipse.debug.ui.ILaunchConfigurationDialog;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.custom.BusyIndicator;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.tcf.debug.ui.ITCFLaunchContext;
import org.eclipse.tcf.internal.debug.launch.TCFLaunchDelegate;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.dialogs.TwoPaneElementSelector;

public class TCFLaunchContext implements ITCFLaunchContext {

    public boolean isSupportedSelection(Object selection) {
        if (selection == null) {
            return getContext(null) != null;
        }
        if (selection instanceof IProject) {
            return CoreModel.getDefault().getCModel().getCProject(((IProject)selection).getName()) != null;
        }
        return selection instanceof ICElement;
    }

    public IPath getPath(Object selection) {
        if (selection instanceof IResource) {
            return ((IResource)selection).getLocation();
        }
        if (selection instanceof ICElement) {
            IResource res = ((ICElement)selection).getResource();
            if (res != null) return res.getLocation();
        }
        return null;
    }

    public IProject getProject(Object selection) {
        if (selection instanceof IProject) return (IProject)selection;
        if (selection instanceof ICElement) return ((ICElement)selection).getCProject().getProject();
        return null;
    }

    public void setDefaults(ILaunchConfigurationDialog dlg, ILaunchConfigurationWorkingCopy config) {
        ICElement element = getContext(config);
        if (element != null) {
            initializeCProject(element, config);
            initializeProgramName(element, dlg, config);
        }
    }

    /**
     * Returns the current C element context from which to initialize default
     * settings, or <code>null</code> if none. Note, if possible we will
     * return the IBinary based on configuration entry as this may be more useful then
     * just the project.
     *
     * @return C element context.
     */
    private ICElement getContext(ILaunchConfiguration config) {
        String projectName = null;
        String programName = null;
        if (config != null) {
            try {
                projectName = config.getAttribute(TCFLaunchDelegate.ATTR_PROJECT_NAME, (String)null);
                programName = config.getAttribute(TCFLaunchDelegate.ATTR_LOCAL_PROGRAM_FILE, (String)null);
            }
            catch (CoreException e) {
                Activator.log(e);
            }
        }
        Object obj = null;
        IWorkbenchPage page = Activator.getActivePage();
        if (projectName != null && !projectName.equals("")) { //$NON-NLS-1$
            IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
            ICProject cProject = CCorePlugin.getDefault().getCoreModel().create(project);
            if (cProject != null && cProject.exists()) obj = cProject;
        }
        else {
            if (page != null) {
                ISelection selection = page.getSelection();
                if (selection instanceof IStructuredSelection) {
                    IStructuredSelection ss = (IStructuredSelection)selection;
                    if (!ss.isEmpty()) obj = ss.getFirstElement();
                }
            }
        }
        if (obj instanceof IResource) {
            ICElement ce = CoreModel.getDefault().create((IResource)obj);
            if (ce == null) {
                IProject pro = ((IResource)obj).getProject();
                ce = CoreModel.getDefault().create(pro);
            }
            obj = ce;
        }
        if (obj instanceof ICElement) {
            if (programName == null || programName.equals("")) { //$NON-NLS-1$
                return (ICElement)obj;
            }
            ICElement ce = (ICElement)obj;
            IProject project;
            project = (IProject)ce.getCProject().getResource();
            IPath programFile = project.getFile(programName).getLocation();
            ce = CCorePlugin.getDefault().getCoreModel().create(programFile);
            if (ce != null && ce.exists()) return ce;
            return (ICElement)obj;
        }
        if (page != null) {
            IEditorPart part = page.getActiveEditor();

            if (part != null) {
                IEditorInput input = part.getEditorInput();
                return (ICElement)input.getAdapter(ICElement.class);
            }
        }
        return null;
    }

    /**
     * Set the C project attribute based on the ICElement.
     */
    private void initializeCProject(ICElement cElement, ILaunchConfigurationWorkingCopy config) {
        ICProject cProject = cElement.getCProject();
        String name = null;
        if (cProject != null && cProject.exists()) {
            name = cProject.getElementName();
            config.setMappedResources(new IResource[] {cProject.getProject()});
        }
        config.setAttribute(TCFLaunchDelegate.ATTR_PROJECT_NAME, name);
    }

    /**
     * Set the program name attributes on the working copy based on the ICElement
     */
    private void initializeProgramName(ICElement cElement, ILaunchConfigurationDialog dlg, ILaunchConfigurationWorkingCopy config) {

        boolean renamed = false;

        if (!(cElement instanceof IBinary)) {
            cElement = cElement.getCProject();
        }

        if (cElement instanceof ICProject) {

            IProject project = cElement.getCProject().getProject();
            String name = project.getName();
            ICProjectDescription projDes = CCorePlugin.getDefault().getProjectDescription(project);
            if (projDes != null) {
                String buildConfigName = projDes.getActiveConfiguration().getName();
                name = name + " " + buildConfigName; //$NON-NLS-1$
            }
            name = dlg.generateName(name);
            config.rename(name);
            renamed = true;
        }

        IBinary binary = null;
        if (cElement instanceof ICProject) {
            IBinary[] bins = getBinaryFiles((ICProject)cElement);
            if (bins != null && bins.length == 1) {
                binary = bins[0];
            }
        }
        else if (cElement instanceof IBinary) {
            binary = (IBinary)cElement;
        }

        if (binary != null) {
            String path;
            path = binary.getResource().getProjectRelativePath().toOSString();
            config.setAttribute(TCFLaunchDelegate.ATTR_LOCAL_PROGRAM_FILE, path);
            if (!renamed)
            {
                String name = binary.getElementName();
                int index = name.lastIndexOf('.');
                if (index > 0) {
                    name = name.substring(0, index);
                }
                name = dlg.generateName(name);
                config.rename(name);
                renamed = true;
            }
        }

        if (!renamed) {
            String name = dlg.generateName(cElement.getCProject().getElementName());
            config.rename(name);
        }
    }

    public String getBuildConfigID(IProject project) {
        ICProjectDescription projDes = CCorePlugin.getDefault().getProjectDescription(project);
        if (projDes == null) return null;
        return projDes.getActiveConfiguration().getId();
    }

    public Map<String,String> getBuildConfigIDs(IProject project) {
        ICProjectDescription projDes = CCorePlugin.getDefault().getProjectDescription(project);
        if (projDes == null) return null;
        ICConfigurationDescription[] configurations = projDes.getConfigurations();
        if (configurations == null) return null;
        Map<String,String> map = new LinkedHashMap<String,String>();
        for (ICConfigurationDescription c : configurations) map.put(c.getId(), c.getName());
        return map;
    }

    /**
     * Iterate through and suck up all of the executable files that we can find.
     */
    private IBinary[] getBinaryFiles(final ICProject cproject) {
        if (cproject == null || !cproject.exists()) return null;
        final Display display = Display.getCurrent();
        final Object[] ret = new Object[1];
        BusyIndicator.showWhile(display, new Runnable() {

            public void run() {
                try {
                    ret[0] = cproject.getBinaryContainer().getBinaries();
                }
                catch (CModelException e) {
                    Activator.errorDialog("Launch UI internal error", e);
                }
            }
        });
        return (IBinary[])ret[0];
    }

    /**
     * Return true if given file path names a binary file.
     * @param project
     * @param path
     * @return true if binary file.
     * @throws CoreException
     */
    public boolean isBinary(IProject project, IPath path) throws CoreException {
        /* Note: CDT throws an exception if 'path' is not a regular file */
        if (path == null) return false;
        File file = path.toFile();
        if (file == null || !file.isFile()) return false;
        return LaunchUtils.getBinary(project, path) != null;
    }

    public String chooseBinary(Shell shell, IProject project) {
        ILabelProvider programLabelProvider = new CElementLabelProvider() {

            @Override
            public String getText(Object element) {
                if (element instanceof IBinary) {
                    IBinary bin = (IBinary)element;
                    StringBuffer name = new StringBuffer();
                    name.append(bin.getPath().lastSegment());
                    return name.toString();
                }
                return super.getText(element);
            }

            @Override
            public Image getImage(Object element) {
                if (! (element instanceof ICElement)) {
                    return super.getImage(element);
                }
                ICElement celement = (ICElement)element;

                if (celement.getElementType() == ICElement.C_BINARY) {
                    IBinary belement = (IBinary)celement;
                    if (belement.isExecutable()) {
                        return DebugUITools.getImage(IDebugUIConstants.IMG_ACT_RUN);
                    }
                }

                return super.getImage(element);
            }
        };

        ILabelProvider qualifierLabelProvider = new CElementLabelProvider() {

            @Override
            public String getText(Object element) {
                if (element instanceof IBinary) {
                    IBinary bin = (IBinary)element;
                    StringBuffer name = new StringBuffer();
                    name.append(bin.getCPU() + (bin.isLittleEndian() ? "le" : "be")); //$NON-NLS-1$ //$NON-NLS-2$
                    name.append(" - "); //$NON-NLS-1$
                    name.append(bin.getPath().toString());
                    return name.toString();
                }
                return super.getText(element);
            }
        };

        TwoPaneElementSelector dialog = new TwoPaneElementSelector(shell, programLabelProvider, qualifierLabelProvider);
        dialog.setElements(getBinaryFiles(getCProject(project.getName())));
        dialog.setMessage("Choose program to run");
        dialog.setTitle("Program Selection");
        dialog.setUpperListLabel("Binaries:");
        dialog.setLowerListLabel("Qualifier:");
        dialog.setMultipleSelection(false);
        // dialog.set
        if (dialog.open() != Window.OK) return null;
        IBinary binary = (IBinary)dialog.getFirstResult();
        return binary.getResource().getProjectRelativePath().toString();
    }

    /**
     * Return the ICProject corresponding to the project name in the project name text field, or
     * null if the text does not match a project name.
     */
    private ICProject getCProject(String name) {
        String projectName = name.trim();
        if (projectName.length() < 1) return null;
        return CoreModel.getDefault().getCModel().getCProject(projectName);
    }
}

Back to the top