Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: fdf72beff35c7185c384802d390d15d64340d997 (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
351
352
353
/*******************************************************************************
 * Copyright (c) 2012, 2018 Red Hat.
 *
 * This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License 2.0
 * which is available at https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     Red Hat - Sami Wagiaalla
 *******************************************************************************/

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

import java.text.MessageFormat;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.debug.ui.AbstractLaunchConfigurationTab;
import org.eclipse.jface.text.TextSelection;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.TreeSelection;
import org.eclipse.linuxtools.internal.systemtap.ui.ide.IDEPlugin;
import org.eclipse.linuxtools.internal.systemtap.ui.ide.editors.stp.PathEditorInput;
import org.eclipse.linuxtools.systemtap.graphing.ui.widgets.ExceptionErrorDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.ide.ResourceUtil;
import org.eclipse.ui.plugin.AbstractUIPlugin;

public class SystemTapScriptLaunchConfigurationTab extends
        AbstractLaunchConfigurationTab {

    static final String SCRIPT_PATH_ATTR = "ScriptPath"; //$NON-NLS-1$
    static final String CURRENT_USER_ATTR = "executeAsCurrentUser"; //$NON-NLS-1$
    static final String USER_NAME_ATTR = "userName"; //$NON-NLS-1$
    static final String USER_PASS_ATTR = "userPassword"; //$NON-NLS-1$
    static final String LOCAL_HOST_ATTR = "executeOnLocalHost"; //$NON-NLS-1$
    static final String HOST_NAME_ATTR = "hostName"; //$NON-NLS-1$
    static final String PORT_ATTR = "port"; //$NON-NLS-1$
    static final String USE_DEFAULT_PORT_ATTR = "useDefaultPort"; //$NON-NLS-1$

    private Text scriptPathText;
    private Button currentUserCheckButton;
    private Text userNameText;
    private Label userNameLabel;
    private Text userPasswordText;
    private Label userPasswordLabel;
    private Button localHostCheckButton;
    private Group hostSettingsGroup;
    private Text hostNameText;
    private Label hostNameLabel;
    private Text portText;
    private Label portLabel;
    private Button portCheckButton;
    private FileDialog fileDialog;

    SelectionListener checkListener = new SelectionListener() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            update();
        }

        @Override
        public void widgetDefaultSelected(SelectionEvent e) {
            update();
        }

        private void update() {
            updateLaunchConfigurationDialog();
        }

    };

    /**
     * @return The path of the chosen script the Run Configuration will be applied to,
     * or <code>null</code> if no file exists at the given path.
     */
    IPath getScriptPath() {
        IPath scriptPath = new Path(scriptPathText.getText());
        return scriptPath.toFile().exists() ? scriptPath : null;
    }

    @Override
    public void createControl(Composite parent) {

        fileDialog = new FileDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), SWT.OPEN);
        fileDialog.setText(Messages.SystemTapScriptLaunchConfigurationTab_selectScript);
        fileDialog.setFilterPath(Platform.getLocation().toOSString());

        GridLayout layout = new GridLayout();
        Composite top = new Composite(parent, SWT.NONE);
        setControl(top);
        top.setLayout(layout);
        top.setLayoutData( new GridData(SWT.FILL, SWT.FILL, true, true));

        // Script path
        Group scriptSettingsGroup = new Group(top, SWT.SHADOW_ETCHED_IN);
        scriptSettingsGroup.setText(Messages.SystemTapScriptLaunchConfigurationTab_script);
        scriptSettingsGroup.setLayoutData( new GridData(SWT.FILL, SWT.FILL, true, false));
        layout = new GridLayout();
        layout.numColumns = 2;
        scriptSettingsGroup.setLayout(layout);
        scriptPathText = new Text(scriptSettingsGroup,  SWT.SINGLE | SWT.BORDER);
        scriptPathText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        scriptPathText.addModifyListener(e -> updateLaunchConfigurationDialog());
        Button selectScriptButon = new Button(scriptSettingsGroup, 0);
        GridData gridData = new GridData();
        gridData.widthHint = 110;
        selectScriptButon.setLayoutData(gridData);
        selectScriptButon.setText(Messages.SystemTapScriptLaunchConfigurationTab_browse);
		selectScriptButon.addSelectionListener(SelectionListener.widgetSelectedAdapter(e -> {
			String path = fileDialog.open();
			if (path != null) {
				scriptPathText.setText(path);
			}
		}));

        // User Settings
        Group userSettingsGroup = new Group(top, SWT.SHADOW_ETCHED_IN);
        layout = new GridLayout();
        userSettingsGroup.setLayout(layout);
        layout.numColumns = 2;
        userSettingsGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));

        currentUserCheckButton = new Button(userSettingsGroup, SWT.CHECK);
        currentUserCheckButton.setText(Messages.SystemTapScriptLaunchConfigurationTab_currentUser);
        currentUserCheckButton.setSelection(true);
        gridData = new GridData();
        gridData.horizontalSpan = 2;
        currentUserCheckButton.setLayoutData(gridData);
        currentUserCheckButton.addSelectionListener(checkListener);

        userNameLabel = new Label(userSettingsGroup, SWT.NONE);
        userNameLabel.setText(Messages.SystemTapScriptLaunchConfigurationTab_username);
        userNameText = new Text(userSettingsGroup, SWT.SINGLE | SWT.BORDER);
        userNameText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));

        userPasswordLabel = new Label(userSettingsGroup, SWT.NONE);
        userPasswordLabel.setText(Messages.SystemTapScriptLaunchConfigurationTab_password);
        userPasswordText = new Text(userSettingsGroup, SWT.SINGLE | SWT.BORDER | SWT.PASSWORD);
        userPasswordText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));

        userSettingsGroup.setLayoutData( new GridData(SWT.FILL, SWT.FILL, true, false));
        userSettingsGroup.setText(Messages.SystemTapScriptLaunchConfigurationTab_user);

        userNameText.addModifyListener(e -> updateLaunchConfigurationDialog());

        userPasswordText.addModifyListener(e -> updateLaunchConfigurationDialog());

        // Host settings
        hostSettingsGroup = new Group(top, SWT.SHADOW_ETCHED_IN);
        hostSettingsGroup.setLayoutData( new GridData(SWT.FILL, SWT.FILL, true, false));
        hostSettingsGroup.setText(Messages.SystemTapScriptLaunchConfigurationTab_host);
        layout = new GridLayout();
        hostSettingsGroup.setLayout(layout);
        layout.numColumns = 2;

        localHostCheckButton = new Button(hostSettingsGroup, SWT.CHECK);
        localHostCheckButton.setText(Messages.SystemTapScriptLaunchConfigurationTab_runLocally);
        gridData = new GridData();
        gridData.horizontalSpan = 2;

        hostNameLabel = new Label(hostSettingsGroup, SWT.NONE);
        hostNameLabel.setText(Messages.SystemTapScriptLaunchConfigurationTab_hostname);
        hostNameText = new Text(hostSettingsGroup, SWT.SINGLE | SWT.BORDER);
        hostNameText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
        localHostCheckButton.setLayoutData(gridData);
        localHostCheckButton.addSelectionListener(checkListener);
        hostNameText.addModifyListener(e -> updateLaunchConfigurationDialog());

        portCheckButton = new Button(hostSettingsGroup, SWT.CHECK);
        portCheckButton.setText(Messages.SystemTapScriptLaunchConfigurationTab_useDefaultPort);
        gridData = new GridData();
        gridData.horizontalSpan = 2;
        portCheckButton.setLayoutData(gridData);
        portCheckButton.addSelectionListener(checkListener);

        portLabel = new Label(hostSettingsGroup, SWT.NONE);
        portLabel.setText(Messages.SystemTapScriptLaunchConfigurationTab_port);
        portText = new Text(hostSettingsGroup, SWT.SINGLE | SWT.BORDER);
        portText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
        portText.setTextLimit(5);
        portText.addVerifyListener(e -> {
		    if (e.keyCode == SWT.BS) {
		        return;
		    }
		    for (int i = 0, n = e.text.length(); i < n; i++) {
		        if (!Character.isDigit(e.text.charAt(i))) {
		            e.doit = false;
		            return;
		        }
		    }
		});
        portText.addModifyListener(e -> updateLaunchConfigurationDialog());
    }

    private void setUserGroupEnablement(boolean enable) {
        userNameText.setEnabled(enable);
        userNameLabel.setEnabled(enable);
        userPasswordText.setEnabled(enable);
        userPasswordLabel.setEnabled(enable);

        hostSettingsGroup.setEnabled(enable);
        localHostCheckButton.setEnabled(enable);
    }

    private void setHostGroupEnablement(boolean enable) {
        hostNameText.setEnabled(enable);
        hostNameLabel.setEnabled(enable);
        portCheckButton.setEnabled(enable);

        boolean portEnabled = enable && !portCheckButton.getSelection();
        portText.setEnabled(portEnabled);
        portLabel.setEnabled(portEnabled);
    }

    @Override
    public void setDefaults(ILaunchConfigurationWorkingCopy configuration) {
        configuration.setAttribute(SCRIPT_PATH_ATTR, getSelectedScriptPath());
        configuration.setAttribute(CURRENT_USER_ATTR, true);
        configuration.setAttribute(USER_NAME_ATTR, ""); //$NON-NLS-1$
        configuration.setAttribute(USER_PASS_ATTR, ""); //$NON-NLS-1$
        configuration.setAttribute(LOCAL_HOST_ATTR, true);
        configuration.setAttribute(HOST_NAME_ATTR, ""); //$NON-NLS-1$
        configuration.setAttribute(PORT_ATTR,
                SystemTapScriptLaunchConfigurationDelegate.DEFAULT_PORT);
        configuration.setAttribute(USE_DEFAULT_PORT_ATTR, true);
    }

    @Override
    public void initializeFrom(ILaunchConfiguration configuration) {
        try {
            this.scriptPathText.setText(configuration.getAttribute(SCRIPT_PATH_ATTR, "")); //$NON-NLS-1$
            this.currentUserCheckButton.setSelection(configuration.getAttribute(CURRENT_USER_ATTR, true));
            this.userNameText.setText(configuration.getAttribute(USER_NAME_ATTR, "")); //$NON-NLS-1$
            this.userPasswordText.setText(configuration.getAttribute(USER_PASS_ATTR, "")); //$NON-NLS-1$
            this.localHostCheckButton.setSelection(configuration.getAttribute(LOCAL_HOST_ATTR, true));
            this.hostNameText.setText(configuration.getAttribute(HOST_NAME_ATTR, "")); //$NON-NLS-1$
            this.portText.setText(Integer.toString(configuration.getAttribute(PORT_ATTR,
                    SystemTapScriptLaunchConfigurationDelegate.DEFAULT_PORT)));
            this.portCheckButton.setSelection(configuration.getAttribute(USE_DEFAULT_PORT_ATTR, true));
        } catch (CoreException e) {
            ExceptionErrorDialog.openError(Messages.SystemTapScriptLaunchConfigurationTab_errorInitializingTab, e);
        }
    }

    @Override
    public void performApply(ILaunchConfigurationWorkingCopy configuration) {
        configuration.setAttribute(SCRIPT_PATH_ATTR, this.scriptPathText.getText());
        configuration.setAttribute(CURRENT_USER_ATTR, this.currentUserCheckButton.getSelection());
        configuration.setAttribute(USER_NAME_ATTR, this.userNameText.getText());
        configuration.setAttribute(USER_PASS_ATTR, this.userPasswordText.getText());
        configuration.setAttribute(LOCAL_HOST_ATTR, this.localHostCheckButton.getSelection());
        configuration.setAttribute(HOST_NAME_ATTR, this.hostNameText.getText());

        configuration.setAttribute(USE_DEFAULT_PORT_ATTR, portCheckButton.getSelection());
        String portString = this.portText.getText();
        configuration.setAttribute(PORT_ATTR, !portString.isEmpty() ? Integer.valueOf(portString) : 0);

        boolean enable = !currentUserCheckButton.getSelection();
        setUserGroupEnablement(enable);

        enable &= !localHostCheckButton.getSelection();
        setHostGroupEnablement(enable);
    }

    @Override
    public boolean isValid(ILaunchConfiguration launchConfig) {
        setErrorMessage(null);

        IPath scriptPath = getScriptPath();
        if (scriptPath == null) {
            setErrorMessage(MessageFormat.format(Messages.SystemTapScriptLaunchConfigurationTab_fileNotFound, scriptPathText.getText()));
            return false;
        }
        String extension = scriptPath.getFileExtension();
        if (extension == null || !extension.equals("stp")) { //$NON-NLS-1$
            setErrorMessage(Messages.SystemTapScriptLaunchConfigurationTab_fileNotStp);
            return false;
        }

        return true;
    }

    @Override
    public String getName() {
        return Messages.SystemTapScriptLaunchConfigurationTab_general;
    }

    private String getSelectedScriptPath() {
        IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();

        String pathString = ""; //$NON-NLS-1$

        if (window != null)
        {
            ISelection selection = window.getSelectionService().getSelection();

            // Figure out the selected systemtap script
            if (selection instanceof TreeSelection) {
                Object selectedElement = ((TreeSelection)selection).getFirstElement();
                if (selectedElement instanceof IFile)
                {
                    IPath path = ((IFile)selectedElement).getLocation();
                    pathString = path.toOSString();
                }
            }

            // If it is a text selection use the path from the active editor.
            if (selection instanceof TextSelection) {
                IEditorPart ed = window.getActivePage().getActiveEditor();
                if(ed.getEditorInput() instanceof PathEditorInput) {
                    pathString = ((PathEditorInput)ed.getEditorInput()).getPath().toString();
                } else {
                    pathString = ResourceUtil.getFile(ed.getEditorInput()).getLocation().toString();
                }
            }
        }

        if (pathString.endsWith(".stp")) { //$NON-NLS-1$
            return pathString;
        }

        return ""; //$NON-NLS-1$
    }

    @Override
    public Image getImage() {
        return AbstractUIPlugin.imageDescriptorFromPlugin(IDEPlugin.PLUGIN_ID,
                "icons/main_tab.gif").createImage(); //$NON-NLS-1$
    }
}

Back to the top