lttng.control: add possibility to run a list of lttng commands
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng2.control.ui / src / org / eclipse / linuxtools / internal / lttng2 / control / ui / views / dialogs / OpenCommandScriptDialog.java
1 /**********************************************************************
2 * Copyright (c) 2014 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Bernd Hufmann - Initial API and implementation
11 **********************************************************************/
12 package org.eclipse.linuxtools.internal.lttng2.control.ui.views.dialogs;
13
14 import java.io.IOException;
15 import java.util.ArrayList;
16 import java.util.List;
17
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.eclipse.jface.dialogs.Dialog;
20 import org.eclipse.jface.dialogs.IDialogConstants;
21 import org.eclipse.linuxtools.internal.lttng2.control.ui.Activator;
22 import org.eclipse.linuxtools.internal.lttng2.control.ui.views.messages.Messages;
23 import org.eclipse.linuxtools.tmf.core.io.BufferedRandomAccessFile;
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.graphics.Point;
26 import org.eclipse.swt.graphics.Rectangle;
27 import org.eclipse.swt.layout.GridData;
28 import org.eclipse.swt.layout.GridLayout;
29 import org.eclipse.swt.widgets.Button;
30 import org.eclipse.swt.widgets.Composite;
31 import org.eclipse.swt.widgets.Control;
32 import org.eclipse.swt.widgets.Event;
33 import org.eclipse.swt.widgets.FileDialog;
34 import org.eclipse.swt.widgets.Group;
35 import org.eclipse.swt.widgets.Label;
36 import org.eclipse.swt.widgets.Listener;
37 import org.eclipse.swt.widgets.Shell;
38 import org.eclipse.swt.widgets.Text;
39
40 import com.google.common.collect.ImmutableList;
41
42 /**
43 * <p>
44 * Dialog box for selecting a command script. It parses the script and
45 * provides a list of shell commands to be executed.
46 * </p>
47 *
48 * @author Bernd Hufmann
49 */
50 public class OpenCommandScriptDialog extends Dialog implements ISelectCommandScriptDialog {
51
52 // ------------------------------------------------------------------------
53 // Constants
54 // ------------------------------------------------------------------------
55 /**
56 * The icon file for this dialog box.
57 */
58 public static final String CREATE_SESSION_ICON_FILE = "icons/elcl16/add_button.gif"; //$NON-NLS-1$
59
60 // ------------------------------------------------------------------------
61 // Attributes
62 // ------------------------------------------------------------------------
63
64 // Dialog attributes
65 private Control fControl = null;
66 private Composite fDialogComposite = null;
67 private Button fBrowseButton;
68 private Label fFileNameLabel = null;
69 private Text fFileNameText = null;
70
71 // Output list of commands
72 private List<String> fCommands = null;
73
74 // ------------------------------------------------------------------------
75 // Constructors
76 // ------------------------------------------------------------------------
77 /**
78 * Constructor
79 * @param shell - a shell for the display of the dialog
80 */
81 public OpenCommandScriptDialog(Shell shell) {
82 super(shell);
83 setShellStyle(SWT.RESIZE | getShellStyle());
84 }
85
86 // ------------------------------------------------------------------------
87 // Accessors
88 // ------------------------------------------------------------------------
89
90 @Override
91 @NonNull public List<String> getCommands() {
92 if (fCommands != null) {
93 return fCommands;
94 }
95 return new ArrayList<>();
96 }
97
98 // ------------------------------------------------------------------------
99 // Operations
100 // ------------------------------------------------------------------------
101
102 @Override
103 protected Control createContents(Composite parent) {
104 fControl = super.createContents(parent);
105
106 /* set the shell minimum size */
107 Point clientArea = fControl.computeSize(SWT.DEFAULT, SWT.DEFAULT);
108 Rectangle trim = getShell().computeTrim(0, 0, clientArea.x, clientArea.y);
109 getShell().setMinimumSize(trim.width, trim.height);
110
111 return fControl;
112 }
113
114 @Override
115 protected void configureShell(Shell newShell) {
116 super.configureShell(newShell);
117 newShell.setText(Messages.TraceControl_ExecuteScriptDialogTitle);
118 newShell.setImage(Activator.getDefault().loadIcon(CREATE_SESSION_ICON_FILE));
119 }
120
121 @Override
122 protected Control createDialogArea(Composite parent) {
123
124 // Main dialog panel
125 fDialogComposite = new Composite(parent, SWT.NONE);
126 GridLayout layout = new GridLayout(1, true);
127 fDialogComposite.setLayout(layout);
128 fDialogComposite.setLayoutData(new GridData(GridData.FILL_BOTH));
129
130 Group sessionGroup = new Group(fDialogComposite, SWT.SHADOW_NONE);
131 sessionGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
132 sessionGroup.setLayout(new GridLayout(6, true));
133
134 fFileNameLabel = new Label(sessionGroup, SWT.RIGHT);
135 fFileNameLabel.setText(Messages.TraceControl_ExecuteScriptSelectLabel);
136 fFileNameText = new Text(sessionGroup, SWT.NONE);
137
138 fBrowseButton = new Button(sessionGroup, SWT.PUSH);
139 fBrowseButton.setText(Messages.TraceControl_ExecuteScriptBrowseText);
140 fBrowseButton.addListener(SWT.Selection, new Listener() {
141 @Override
142 public void handleEvent(Event event) {
143 handleFilePathBrowseButtonPressed(SWT.OPEN);
144 }
145 });
146
147 // layout widgets
148 GridData data = new GridData(GridData.FILL_HORIZONTAL);
149 data.horizontalSpan = 1;
150 data.grabExcessHorizontalSpace = false;
151 fFileNameLabel.setLayoutData(data);
152
153 data = new GridData(GridData.FILL_HORIZONTAL);
154 data.horizontalSpan = 4;
155 fFileNameText.setLayoutData(data);
156
157 data = new GridData(GridData.FILL_HORIZONTAL);
158 data.horizontalSpan = 1;
159
160 // Initialize a empty list
161 fCommands = new ArrayList<>();
162
163 return fDialogComposite;
164 }
165
166 private void handleFilePathBrowseButtonPressed(int fileDialogStyle) {
167 FileDialog dialog = new FileDialog(getShell(), fileDialogStyle | SWT.SHEET);
168 dialog.setFilterExtensions(new String[] { "*.*", "*.*" }); //$NON-NLS-1$ //$NON-NLS-2$
169 dialog.setText(Messages.TraceControl_ExecuteScriptDialogTitle);
170 String selectedFileName = dialog.open();
171 fFileNameText.setText(selectedFileName);
172 }
173
174 @Override
175 protected void createButtonsForButtonBar(Composite parent) {
176 createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, true);
177 createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
178 }
179
180 @Override
181 protected void okPressed() {
182 // Validate input data
183 String sessionPath = fFileNameText.getText();
184
185 if (!"".equals(sessionPath)) { //$NON-NLS-1$
186 ImmutableList.Builder<String> builder = new ImmutableList.Builder<>();
187 try (BufferedRandomAccessFile rafile = new BufferedRandomAccessFile(sessionPath, "r")) { //$NON-NLS-1$
188 String line = rafile.getNextLine();
189 while (line != null) {
190 builder.add(line);
191 line = rafile.getNextLine();
192 }
193 } catch (IOException e) {
194 return;
195 }
196 fCommands = builder.build();
197 super.okPressed();
198 }
199 }
200 }
This page took 0.035487 seconds and 5 git commands to generate.