Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c7dcd97f766f1490a40f852f001a98b5ddf2a1ca (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
/*******************************************************************************
 * Copyright (c) 2009 Red Hat, Inc.
 * 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:
 *     Red Hat - initial API and implementation
 *******************************************************************************/
package org.eclipse.linuxtools.internal.callgraph.core;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.linuxtools.profiling.ui.ProfileUIUtils;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.dialogs.ElementListSelectionDialog;

/**
 * Helper class that finds and opens files. Finds based on function names,
 * opens based on path and in the current default editor.
 *
 */
public class FileFinderOpener {

    private static Map<String, int []> map = new HashMap<>();

    /**
     * Seeks all functions in the given proejct that contains the given function name.
     * Farms off the work of generating the list to the findFunctionsInProject function.
     * Opens a selection dialog if more than one file is found.
     *
     * @param project
     * @param functionName
     * @return
     */
    public static void findAndOpen(ICProject project, String functionName) {
        //Safety valve: Do not enforce use of project names
        if (project == null) {
            return;
        }

        map = ProfileUIUtils.findFunctionsInProject(project, functionName, -1, null);
        ArrayList<String> files = new ArrayList<>(map.keySet());

        if (files == null || files.size() < 1) {
            return;
        }

        if (files.size() == 1) {
            open(files.get(0), map.get(files.get(0))[0], map.get(files.get(0))[1]);
        } else {
            ElementListSelectionDialog d = new ElementListSelectionDialog(new Shell(), new LabelProvider());
            d.setTitle(Messages.getString("FileFinderOpener.MultipleFilesDialog"));  //$NON-NLS-1$
            d.setMessage(Messages.getString("FileFinderOpener.MultFilesDialogM1") + functionName + Messages.getString("FileFinderOpener.MultFilesDialogM2") +   //$NON-NLS-1$ //$NON-NLS-2$
                    Messages.getString("FileFinderOpener.MultFilesDialogM3"));  //$NON-NLS-1$
            d.setElements(files.toArray());
            d.open();

            if (d.getResult() == null) {
                return;
            }

            for (Object o : d.getResult()) {
                if (o instanceof String) {
                    String s = (String) o;
                    open(s, map.get(s)[0], map.get(s)[1]);
                }
            }
        }
    }

    private static void open(String path, int offset, int length) {
        if (path == null) {
            return;
        }
        try {
            ProfileUIUtils.openEditorAndSelect(path, offset, length);
        } catch (PartInitException e) {
            e.printStackTrace();
        }
    }
}

Back to the top