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