Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 12be1d8025f4be3f00b260a66da54d68e232074b (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
/*******************************************************************************
 * Copyright (c) 2006 IBM Corporation.
 * 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:
 *     IBM Corporation - Jeff Briggs, Henry Hughes, Ryan Morse
 *******************************************************************************/

package org.eclipse.linuxtools.internal.systemtap.ui.ide.views;

import java.io.File;
import java.net.URI;

import org.eclipse.core.filesystem.IFileInfo;
import org.eclipse.core.filesystem.IFileStore;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.linuxtools.internal.systemtap.ui.ide.IDEPlugin;
import org.eclipse.linuxtools.internal.systemtap.ui.ide.Localization;
import org.eclipse.linuxtools.internal.systemtap.ui.ide.actions.KernelSourceAction;
import org.eclipse.linuxtools.internal.systemtap.ui.ide.preferences.IDEPreferenceConstants;
import org.eclipse.linuxtools.internal.systemtap.ui.ide.preferences.PathPreferencePage;
import org.eclipse.linuxtools.profiling.launch.IRemoteFileProxy;
import org.eclipse.linuxtools.profiling.launch.RemoteProxyManager;
import org.eclipse.linuxtools.systemtap.graphing.ui.widgets.ExceptionErrorDialog;
import org.eclipse.linuxtools.systemtap.structures.KernelSourceTree;
import org.eclipse.linuxtools.systemtap.structures.TreeNode;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.progress.UIJob;

/**
 * The Kernel Source Browser module for the SystemTap GUI. This browser provides a list of kernel source
 * files and allows the user to open those files in an editor in order to place probes in arbitrary locations.
 * @author Henry Hughes
 * @author Ryan Morse
 */

public class KernelBrowserView extends BrowserView {
    public static final String ID = "org.eclipse.linuxtools.internal.systemtap.ui.ide.views.KernelBrowserView"; //$NON-NLS-1$

    private class KernelRefreshJob extends Job {
        private boolean remote;
        private URI kernelLocationURI;
        private IRemoteFileProxy proxy;
        private String kernelSource;

        public KernelRefreshJob(boolean remote, URI kernelLocationURI, IRemoteFileProxy proxy, String kernelSource) {
            super(Localization.getString("KernelBrowserView.RefreshingKernelSource")); //$NON-NLS-1$
            this.remote = remote;
            this.kernelLocationURI = kernelLocationURI;
            this.proxy = proxy;
            this.kernelSource = kernelSource;
        }

        @Override
        public IStatus run(IProgressMonitor monitor) {
            IPreferenceStore p = IDEPlugin.getDefault().getPreferenceStore();
            KernelSourceTree kst = new KernelSourceTree();
            String excluded[] = p.getString(IDEPreferenceConstants.P_EXCLUDED_KERNEL_SOURCE).split(File.pathSeparator);
            if (remote) {
                try {
                    kst.buildKernelTree(kernelLocationURI, excluded, proxy, monitor);
                } catch (CoreException e) {
                    ExceptionErrorDialog.openError(Localization.getString("KernelBrowserView.CouldNotInitializeTree"), e); //$NON-NLS-1$
                }
            } else {
                kst.buildKernelTree(kernelSource, excluded);
            }
            if (monitor.isCanceled()) {
                return Status.CANCEL_STATUS;
            }
            UpdateKernelBrowserJob job = new UpdateKernelBrowserJob(kst);
            job.schedule();
            monitor.done();
            return Status.OK_STATUS;
        }
    }

    private class UpdateKernelBrowserJob extends UIJob {
        KernelSourceTree kst;
        public UpdateKernelBrowserJob(KernelSourceTree kst) {
            super(Localization.getString("KernelBrowserView.UpdateKernelBrowser")); //$NON-NLS-1$
            this.kst = kst;
        }

        @Override
        public IStatus runInUIThread(IProgressMonitor monitor) {
            monitor.beginTask(Localization.getString("KernelBrowserView.UpdateKernelBrowser"), 100); //$NON-NLS-1$
            if (kst == null) {
                return Status.OK_STATUS;
            }
            tree = kst.getTree();
            viewer.setInput(tree);
            kst.dispose();
            monitor.done();
            return Status.OK_STATUS;
        }
    }

    /**
     * Creates the UI on the given <code>Composite</code>
     */
    @Override
    public void createPartControl(Composite parent) {
        super.createPartControl(parent);
        refresh();
        makeActions();
    }

    /**
     * Wires up all of the actions for this browser, such as double and right click handlers.
     */
    private void makeActions() {
        doubleClickAction = new KernelSourceAction(getSite().getWorkbenchWindow(), this);
        viewer.addDoubleClickListener(doubleClickAction);
        IDEPlugin.getDefault().getPreferenceStore().addPropertyChangeListener(propertyChangeListener);
    }

    @Override
    protected Image getEntryImage(TreeNode treeObj) {
        if (treeObj.toString().lastIndexOf('.') != -1) {
            String item = treeObj.getData().toString();
            if (item.endsWith(".c")) { //$NON-NLS-1$
                return IDEPlugin.getImageDescriptor("icons/files/file_c.gif").createImage(); //$NON-NLS-1$
            }
            if (item.endsWith(".h")) { //$NON-NLS-1$
                return IDEPlugin.getImageDescriptor("icons/files/file_h.gif").createImage(); //$NON-NLS-1$
            }
            return IDEPlugin.getImageDescriptor("icons/vars/var_unk.gif").createImage(); //$NON-NLS-1$
        }
        return PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJ_FOLDER);
    }

    /**
     * Updates the kernel source displayed to the user with the new kernel source tree. Usually
     * a response to the user changing the preferences related to the kernel source location, requiring
     * that the application update the kernel source information.
     */
    @Override
    public void refresh() {
        IPreferenceStore p = IDEPlugin.getDefault().getPreferenceStore();
        String kernelSource = p.getString(IDEPreferenceConstants.P_KERNEL_SOURCE);
        if (kernelSource == null || kernelSource.length() < 1) {
            showBrowserErrorMessage(Localization.getString("KernelBrowserView.NoKernelSourceFound")); //$NON-NLS-1$
            return;
        }

        String localOrRemote = p.getString(IDEPreferenceConstants.P_REMOTE_LOCAL_KERNEL_SOURCE);
        URI kernelLocationURI = null;
        IRemoteFileProxy proxy = null;
        boolean remote = localOrRemote.equals(PathPreferencePage.REMOTE);
        if (remote) {
            boolean error = false;
            try {
                kernelLocationURI = IDEPlugin.getDefault().createRemoteUri(kernelSource);
                if (kernelLocationURI == null) {
                    error = true;
                } else {
                    proxy = RemoteProxyManager.getInstance().getFileProxy(kernelLocationURI);
                    if (!validateProxy(proxy, kernelSource)) {
                        error = true;
                    }
                }
            } catch (CoreException e2) {
                error = true;
            }
            if (error) {
                showBrowserErrorMessage(Localization.getString("KernelBrowserView.KernelSourceDirNotFound")); //$NON-NLS-1$
                return;
            }
        }

        KernelRefreshJob refreshJob = new KernelRefreshJob(remote, kernelLocationURI, proxy, kernelSource);
        refreshJob.setUser(true);
        refreshJob.setPriority(Job.SHORT);
        refreshJob.schedule();
    }

    private boolean validateProxy(IRemoteFileProxy proxy, String kernelSource) {
        if (proxy == null) {
            return false;
        }
        IFileStore fs = proxy.getResource(kernelSource);
        if (fs == null) {
            return false;
        }
        IFileInfo info = fs.fetchInfo();
        if (info == null) {
            return false;
        }
        if (!info.exists()) {
            return false;
        }
        return true;
    }

    private void showBrowserErrorMessage(String message) {
        tree = new TreeNode("", "", false); //$NON-NLS-1$ //$NON-NLS-2$
        tree.add(new TreeNode("", message, false)); //$NON-NLS-1$
        viewer.setInput(tree);
    }

    /**
     * A <code>IPropertyChangeListener</code> that detects changes to the Kernel Source location
     * and runs the <code>updateKernelSourceTree</code> method.
     */
    private final IPropertyChangeListener propertyChangeListener = new IPropertyChangeListener() {
        @Override
        public void propertyChange(PropertyChangeEvent event) {
            if (event.getProperty().equals(IDEPreferenceConstants.P_KERNEL_SOURCE) ||
                event.getProperty().equals(IDEPreferenceConstants.P_REMOTE_LOCAL_KERNEL_SOURCE) ||
                event.getProperty().equals(IDEPreferenceConstants.P_EXCLUDED_KERNEL_SOURCE)) {
                viewUpdater.handleUpdateEvent();
            }
        }
    };

    @Override
    public void dispose() {
        IDEPlugin.getDefault().getPreferenceStore().removePropertyChangeListener(propertyChangeListener);
        super.dispose();
    }
}

Back to the top