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
.tracecompass
.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
.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
;
46 import com
.google
.common
.collect
.ImmutableList
;
50 * Dialog box for selecting a command script. It parses the script and
51 * provides a list of shell commands to be executed.
54 * @author Bernd Hufmann
56 public class OpenCommandScriptDialog
extends Dialog
implements ISelectCommandScriptDialog
{
58 // ------------------------------------------------------------------------
60 // ------------------------------------------------------------------------
62 * The icon file for this dialog box.
64 public static final String CREATE_SESSION_ICON_FILE
= "icons/elcl16/add_button.gif"; //$NON-NLS-1$
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;
71 // ------------------------------------------------------------------------
73 // ------------------------------------------------------------------------
76 private Control fControl
= null;
77 private Composite fDialogComposite
= null;
78 private Button fBrowseButton
;
79 private Label fFileNameLabel
= null;
80 private Combo fFileNameCombo
= null;
82 // Output list of commands
83 private List
<String
> fCommands
= null;
85 // ------------------------------------------------------------------------
87 // ------------------------------------------------------------------------
90 * @param shell - a shell for the display of the dialog
92 public OpenCommandScriptDialog(Shell shell
) {
94 setShellStyle(SWT
.RESIZE
| getShellStyle());
97 // ------------------------------------------------------------------------
99 // ------------------------------------------------------------------------
102 @NonNull public List
<String
> getCommands() {
103 if (fCommands
!= null) {
106 return new ArrayList
<>();
109 // ------------------------------------------------------------------------
111 // ------------------------------------------------------------------------
114 protected Control
createContents(Composite parent
) {
115 fControl
= super.createContents(parent
);
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
);
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
));
133 protected Control
createDialogArea(Composite parent
) {
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
));
141 Group sessionGroup
= new Group(fDialogComposite
, SWT
.SHADOW_NONE
);
142 sessionGroup
.setLayoutData(new GridData(GridData
.FILL_HORIZONTAL
));
143 sessionGroup
.setLayout(new GridLayout(6, true));
145 fFileNameLabel
= new Label(sessionGroup
, SWT
.RIGHT
);
146 fFileNameLabel
.setText(Messages
.TraceControl_ExecuteScriptSelectLabel
);
147 fFileNameCombo
= new Combo(sessionGroup
, SWT
.BORDER
);
149 fBrowseButton
= new Button(sessionGroup
, SWT
.PUSH
);
150 fBrowseButton
.setText(Messages
.TraceControl_ExecuteScriptBrowseText
);
151 fBrowseButton
.addListener(SWT
.Selection
, new Listener() {
153 public void handleEvent(Event event
) {
154 handleFilePathBrowseButtonPressed(SWT
.OPEN
);
159 GridData data
= new GridData(GridData
.FILL_HORIZONTAL
);
160 data
.horizontalSpan
= 1;
161 data
.grabExcessHorizontalSpace
= false;
162 fFileNameLabel
.setLayoutData(data
);
164 data
= new GridData(GridData
.FILL_HORIZONTAL
);
165 data
.horizontalSpan
= 4;
166 fFileNameCombo
.setLayoutData(data
);
168 data
= new GridData(GridData
.FILL_HORIZONTAL
);
169 data
.horizontalSpan
= 1;
171 // Initialize a empty list
172 fCommands
= new ArrayList
<>();
174 restoreWidgetValues();
176 return fDialogComposite
;
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
);
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
]);
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];
203 fileNames
= addToHistory(fileNames
, fFileNameCombo
.getText().trim());
204 settings
.put(FILE_NAME_ID
, fileNames
);
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>.
213 * @param history the current history
214 * @param newEntry the entry to add to the history
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
);
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
);
226 String
[] r
= new String
[list
.size()];
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
);
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);
248 protected void okPressed() {
249 // Validate input data
250 String sessionPath
= fFileNameCombo
.getText();
252 if (!"".equals(sessionPath
)) { //$NON-NLS-1$
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) {
259 line
= rafile
.getNextLine();
261 } catch (IOException e
) {
262 ErrorDialog
.openError(getShell(), null, null, new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, IStatus
.ERROR
, e
.getLocalizedMessage(), e
));
266 fCommands
= builder
.build();