bd648f559d0c192fae91d12583d18b691722332f
[deliverable/tracecompass.git] / lttng / org.eclipse.tracecompass.lttng2.control.ui / src / org / eclipse / tracecompass / 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.tracecompass.internal.lttng2.control.ui.views.dialogs;
13
14 import java.io.IOException;
15 import java.util.ArrayList;
16 import java.util.Arrays;
17 import java.util.List;
18
19 import org.eclipse.core.runtime.IStatus;
20 import org.eclipse.core.runtime.Status;
21 import org.eclipse.jdt.annotation.NonNull;
22 import org.eclipse.jface.dialogs.Dialog;
23 import org.eclipse.jface.dialogs.ErrorDialog;
24 import org.eclipse.jface.dialogs.IDialogConstants;
25 import org.eclipse.jface.dialogs.IDialogSettings;
26 import org.eclipse.swt.SWT;
27 import org.eclipse.swt.graphics.Point;
28 import org.eclipse.swt.graphics.Rectangle;
29 import org.eclipse.swt.layout.GridData;
30 import org.eclipse.swt.layout.GridLayout;
31 import org.eclipse.swt.widgets.Button;
32 import org.eclipse.swt.widgets.Combo;
33 import org.eclipse.swt.widgets.Composite;
34 import org.eclipse.swt.widgets.Control;
35 import org.eclipse.swt.widgets.Event;
36 import org.eclipse.swt.widgets.FileDialog;
37 import org.eclipse.swt.widgets.Group;
38 import org.eclipse.swt.widgets.Label;
39 import org.eclipse.swt.widgets.Listener;
40 import org.eclipse.swt.widgets.Shell;
41 import org.eclipse.tracecompass.internal.lttng2.control.ui.Activator;
42 import org.eclipse.tracecompass.internal.lttng2.control.ui.views.messages.Messages;
43 import org.eclipse.tracecompass.tmf.core.io.BufferedRandomAccessFile;
44
45 import com.google.common.collect.ImmutableList;
46
47 /**
48 * <p>
49 * Dialog box for selecting a command script. It parses the script and
50 * provides a list of shell commands to be executed.
51 * </p>
52 *
53 * @author Bernd Hufmann
54 */
55 public class OpenCommandScriptDialog extends Dialog implements ISelectCommandScriptDialog {
56
57 // ------------------------------------------------------------------------
58 // Constants
59 // ------------------------------------------------------------------------
60 /**
61 * The icon file for this dialog box.
62 */
63 public static final String CREATE_SESSION_ICON_FILE = "icons/elcl16/add_button.gif"; //$NON-NLS-1$
64
65 // Dialog settings constants
66 private static final String DIALOG_SETTINGS_SECTION = "OpenCommandScriptDialog"; //$NON-NLS-1$
67 private static final String FILE_NAME_ID = "STORE_FILE_NAME_ID"; //$NON-NLS-1$
68 private static final int COMBO_HISTORY_LENGTH = 5;
69
70 // ------------------------------------------------------------------------
71 // Attributes
72 // ------------------------------------------------------------------------
73
74 // Dialog attributes
75 private Control fControl = null;
76 private Composite fDialogComposite = null;
77 private Button fBrowseButton;
78 private Label fFileNameLabel = null;
79 private Combo fFileNameCombo = null;
80
81 // Output list of commands
82 private List<String> fCommands = null;
83
84 // ------------------------------------------------------------------------
85 // Constructors
86 // ------------------------------------------------------------------------
87 /**
88 * Constructor
89 * @param shell - a shell for the display of the dialog
90 */
91 public OpenCommandScriptDialog(Shell shell) {
92 super(shell);
93 setShellStyle(SWT.RESIZE | getShellStyle());
94 }
95
96 // ------------------------------------------------------------------------
97 // Accessors
98 // ------------------------------------------------------------------------
99
100 @Override
101 @NonNull public List<String> getCommands() {
102 if (fCommands != null) {
103 return fCommands;
104 }
105 return new ArrayList<>();
106 }
107
108 // ------------------------------------------------------------------------
109 // Operations
110 // ------------------------------------------------------------------------
111
112 @Override
113 protected Control createContents(Composite parent) {
114 fControl = super.createContents(parent);
115
116 /* set the shell minimum size */
117 Point clientArea = fControl.computeSize(SWT.DEFAULT, SWT.DEFAULT);
118 Rectangle trim = getShell().computeTrim(0, 0, clientArea.x, clientArea.y);
119 getShell().setMinimumSize(trim.width, trim.height);
120
121 return fControl;
122 }
123
124 @Override
125 protected void configureShell(Shell newShell) {
126 super.configureShell(newShell);
127 newShell.setText(Messages.TraceControl_ExecuteScriptDialogTitle);
128 newShell.setImage(Activator.getDefault().loadIcon(CREATE_SESSION_ICON_FILE));
129 }
130
131 @Override
132 protected Control createDialogArea(Composite parent) {
133
134 // Main dialog panel
135 fDialogComposite = new Composite(parent, SWT.NONE);
136 GridLayout layout = new GridLayout(1, true);
137 fDialogComposite.setLayout(layout);
138 fDialogComposite.setLayoutData(new GridData(GridData.FILL_BOTH));
139
140 Group sessionGroup = new Group(fDialogComposite, SWT.SHADOW_NONE);
141 sessionGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
142 sessionGroup.setLayout(new GridLayout(6, true));
143
144 fFileNameLabel = new Label(sessionGroup, SWT.RIGHT);
145 fFileNameLabel.setText(Messages.TraceControl_ExecuteScriptSelectLabel);
146 fFileNameCombo = new Combo(sessionGroup, SWT.BORDER);
147
148 fBrowseButton = new Button(sessionGroup, SWT.PUSH);
149 fBrowseButton.setText(Messages.TraceControl_ExecuteScriptBrowseText);
150 fBrowseButton.addListener(SWT.Selection, new Listener() {
151 @Override
152 public void handleEvent(Event event) {
153 handleFilePathBrowseButtonPressed(SWT.OPEN);
154 }
155 });
156
157 // layout widgets
158 GridData data = new GridData(GridData.FILL_HORIZONTAL);
159 data.horizontalSpan = 1;
160 data.grabExcessHorizontalSpace = false;
161 fFileNameLabel.setLayoutData(data);
162
163 data = new GridData(GridData.FILL_HORIZONTAL);
164 data.horizontalSpan = 4;
165 fFileNameCombo.setLayoutData(data);
166
167 data = new GridData(GridData.FILL_HORIZONTAL);
168 data.horizontalSpan = 1;
169
170 // Initialize a empty list
171 fCommands = new ArrayList<>();
172
173 restoreWidgetValues();
174
175 return fDialogComposite;
176 }
177
178 private void restoreWidgetValues() {
179 IDialogSettings workbenchSettings = Activator.getDefault().getDialogSettings();
180 IDialogSettings settings = workbenchSettings.getSection(DIALOG_SETTINGS_SECTION);
181 if (settings == null) {
182 settings = workbenchSettings.addNewSection(DIALOG_SETTINGS_SECTION);
183 }
184 String[] fileNames = settings.getArray(FILE_NAME_ID);
185 if ((fileNames != null) && (fileNames.length != 0)) {
186 for (int i = 0; i < fileNames.length; i++) {
187 fFileNameCombo.add(fileNames[i]);
188 }
189 }
190 }
191
192 private void saveWidgetValues() {
193 IDialogSettings workbenchSettings = Activator.getDefault().getDialogSettings();
194 IDialogSettings settings = workbenchSettings.getSection(DIALOG_SETTINGS_SECTION);
195 if (settings != null) {
196 // update file names history
197 String[] fileNames = settings.getArray(FILE_NAME_ID);
198 if (fileNames == null) {
199 fileNames = new String[0];
200 }
201
202 fileNames = addToHistory(fileNames, fFileNameCombo.getText().trim());
203 settings.put(FILE_NAME_ID, fileNames);
204 }
205 }
206
207 /**
208 * Adds an entry to a history, while taking care of duplicate history items
209 * and excessively long histories. The assumption is made that all histories
210 * should be of length <code>COMBO_HISTORY_LENGTH</code>.
211 *
212 * @param history the current history
213 * @param newEntry the entry to add to the history
214 */
215 private static String[] addToHistory(String[] history, String newEntry) {
216 List<String> list = new ArrayList<>(Arrays.asList(history));
217 list.remove(newEntry);
218 list.add(0, newEntry);
219
220 // since only one new item was added, we can be over the limit
221 // by at most one item
222 if (list.size() > COMBO_HISTORY_LENGTH) {
223 list.remove(COMBO_HISTORY_LENGTH);
224 }
225 String[] r = new String[list.size()];
226 list.toArray(r);
227 return r;
228 }
229
230 private void handleFilePathBrowseButtonPressed(int fileDialogStyle) {
231 FileDialog dialog = new FileDialog(getShell(), fileDialogStyle | SWT.SHEET);
232 dialog.setFilterExtensions(new String[] { "*.*", "*.*" }); //$NON-NLS-1$ //$NON-NLS-2$
233 dialog.setText(Messages.TraceControl_ExecuteScriptDialogTitle);
234 String selectedFileName = dialog.open();
235 if (selectedFileName != null) {
236 fFileNameCombo.setText(selectedFileName);
237 }
238 }
239
240 @Override
241 protected void createButtonsForButtonBar(Composite parent) {
242 createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, true);
243 createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
244 }
245
246 @Override
247 protected void okPressed() {
248 // Validate input data
249 String sessionPath = fFileNameCombo.getText();
250
251 if (!"".equals(sessionPath)) { //$NON-NLS-1$
252
253 ImmutableList.Builder<String> builder = new ImmutableList.Builder<>();
254 try (BufferedRandomAccessFile rafile = new BufferedRandomAccessFile(sessionPath, "r")) { //$NON-NLS-1$
255 String line = rafile.getNextLine();
256 while (line != null) {
257 builder.add(line);
258 line = rafile.getNextLine();
259 }
260 } catch (IOException e) {
261 ErrorDialog.openError(getShell(), null, null, new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.ERROR, e.getLocalizedMessage(), e));
262 return;
263 }
264 saveWidgetValues();
265 fCommands = builder.build();
266 super.okPressed();
267 }
268 }
269 }
This page took 0.036212 seconds and 4 git commands to generate.