1 /**********************************************************************
2 * Copyright (c) 2014 Ericsson
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
10 * Bernd Hufmann - Initial API and implementation
11 **********************************************************************/
12 package org
.eclipse
.linuxtools
.internal
.lttng2
.control
.ui
.views
.dialogs
;
14 import java
.io
.IOException
;
15 import java
.util
.ArrayList
;
16 import java
.util
.Arrays
;
17 import java
.util
.List
;
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
.linuxtools
.internal
.lttng2
.control
.ui
.Activator
;
27 import org
.eclipse
.linuxtools
.internal
.lttng2
.control
.ui
.views
.messages
.Messages
;
28 import org
.eclipse
.swt
.SWT
;
29 import org
.eclipse
.swt
.graphics
.Point
;
30 import org
.eclipse
.swt
.graphics
.Rectangle
;
31 import org
.eclipse
.swt
.layout
.GridData
;
32 import org
.eclipse
.swt
.layout
.GridLayout
;
33 import org
.eclipse
.swt
.widgets
.Button
;
34 import org
.eclipse
.swt
.widgets
.Combo
;
35 import org
.eclipse
.swt
.widgets
.Composite
;
36 import org
.eclipse
.swt
.widgets
.Control
;
37 import org
.eclipse
.swt
.widgets
.Event
;
38 import org
.eclipse
.swt
.widgets
.FileDialog
;
39 import org
.eclipse
.swt
.widgets
.Group
;
40 import org
.eclipse
.swt
.widgets
.Label
;
41 import org
.eclipse
.swt
.widgets
.Listener
;
42 import org
.eclipse
.swt
.widgets
.Shell
;
43 import org
.eclipse
.tracecompass
.tmf
.core
.io
.BufferedRandomAccessFile
;
45 import com
.google
.common
.collect
.ImmutableList
;
49 * Dialog box for selecting a command script. It parses the script and
50 * provides a list of shell commands to be executed.
53 * @author Bernd Hufmann
55 public class OpenCommandScriptDialog
extends Dialog
implements ISelectCommandScriptDialog
{
57 // ------------------------------------------------------------------------
59 // ------------------------------------------------------------------------
61 * The icon file for this dialog box.
63 public static final String CREATE_SESSION_ICON_FILE
= "icons/elcl16/add_button.gif"; //$NON-NLS-1$
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;
70 // ------------------------------------------------------------------------
72 // ------------------------------------------------------------------------
75 private Control fControl
= null;
76 private Composite fDialogComposite
= null;
77 private Button fBrowseButton
;
78 private Label fFileNameLabel
= null;
79 private Combo fFileNameCombo
= null;
81 // Output list of commands
82 private List
<String
> fCommands
= null;
84 // ------------------------------------------------------------------------
86 // ------------------------------------------------------------------------
89 * @param shell - a shell for the display of the dialog
91 public OpenCommandScriptDialog(Shell shell
) {
93 setShellStyle(SWT
.RESIZE
| getShellStyle());
96 // ------------------------------------------------------------------------
98 // ------------------------------------------------------------------------
101 @NonNull public List
<String
> getCommands() {
102 if (fCommands
!= null) {
105 return new ArrayList
<>();
108 // ------------------------------------------------------------------------
110 // ------------------------------------------------------------------------
113 protected Control
createContents(Composite parent
) {
114 fControl
= super.createContents(parent
);
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
);
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
));
132 protected Control
createDialogArea(Composite parent
) {
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
));
140 Group sessionGroup
= new Group(fDialogComposite
, SWT
.SHADOW_NONE
);
141 sessionGroup
.setLayoutData(new GridData(GridData
.FILL_HORIZONTAL
));
142 sessionGroup
.setLayout(new GridLayout(6, true));
144 fFileNameLabel
= new Label(sessionGroup
, SWT
.RIGHT
);
145 fFileNameLabel
.setText(Messages
.TraceControl_ExecuteScriptSelectLabel
);
146 fFileNameCombo
= new Combo(sessionGroup
, SWT
.BORDER
);
148 fBrowseButton
= new Button(sessionGroup
, SWT
.PUSH
);
149 fBrowseButton
.setText(Messages
.TraceControl_ExecuteScriptBrowseText
);
150 fBrowseButton
.addListener(SWT
.Selection
, new Listener() {
152 public void handleEvent(Event event
) {
153 handleFilePathBrowseButtonPressed(SWT
.OPEN
);
158 GridData data
= new GridData(GridData
.FILL_HORIZONTAL
);
159 data
.horizontalSpan
= 1;
160 data
.grabExcessHorizontalSpace
= false;
161 fFileNameLabel
.setLayoutData(data
);
163 data
= new GridData(GridData
.FILL_HORIZONTAL
);
164 data
.horizontalSpan
= 4;
165 fFileNameCombo
.setLayoutData(data
);
167 data
= new GridData(GridData
.FILL_HORIZONTAL
);
168 data
.horizontalSpan
= 1;
170 // Initialize a empty list
171 fCommands
= new ArrayList
<>();
173 restoreWidgetValues();
175 return fDialogComposite
;
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
);
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
]);
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];
202 fileNames
= addToHistory(fileNames
, fFileNameCombo
.getText().trim());
203 settings
.put(FILE_NAME_ID
, fileNames
);
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>.
212 * @param history the current history
213 * @param newEntry the entry to add to the history
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
);
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
);
225 String
[] r
= new String
[list
.size()];
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
);
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);
247 protected void okPressed() {
248 // Validate input data
249 String sessionPath
= fFileNameCombo
.getText();
251 if (!"".equals(sessionPath
)) { //$NON-NLS-1$
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) {
258 line
= rafile
.getNextLine();
260 } catch (IOException e
) {
261 ErrorDialog
.openError(getShell(), null, null, new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, IStatus
.ERROR
, e
.getLocalizedMessage(), e
));
265 fCommands
= builder
.build();