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
.List
;
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
;
40 import com
.google
.common
.collect
.ImmutableList
;
44 * Dialog box for selecting a command script. It parses the script and
45 * provides a list of shell commands to be executed.
48 * @author Bernd Hufmann
50 public class OpenCommandScriptDialog
extends Dialog
implements ISelectCommandScriptDialog
{
52 // ------------------------------------------------------------------------
54 // ------------------------------------------------------------------------
56 * The icon file for this dialog box.
58 public static final String CREATE_SESSION_ICON_FILE
= "icons/elcl16/add_button.gif"; //$NON-NLS-1$
60 // ------------------------------------------------------------------------
62 // ------------------------------------------------------------------------
65 private Control fControl
= null;
66 private Composite fDialogComposite
= null;
67 private Button fBrowseButton
;
68 private Label fFileNameLabel
= null;
69 private Text fFileNameText
= null;
71 // Output list of commands
72 private List
<String
> fCommands
= null;
74 // ------------------------------------------------------------------------
76 // ------------------------------------------------------------------------
79 * @param shell - a shell for the display of the dialog
81 public OpenCommandScriptDialog(Shell shell
) {
83 setShellStyle(SWT
.RESIZE
| getShellStyle());
86 // ------------------------------------------------------------------------
88 // ------------------------------------------------------------------------
91 @NonNull public List
<String
> getCommands() {
92 if (fCommands
!= null) {
95 return new ArrayList
<>();
98 // ------------------------------------------------------------------------
100 // ------------------------------------------------------------------------
103 protected Control
createContents(Composite parent
) {
104 fControl
= super.createContents(parent
);
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
);
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
));
122 protected Control
createDialogArea(Composite parent
) {
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
));
130 Group sessionGroup
= new Group(fDialogComposite
, SWT
.SHADOW_NONE
);
131 sessionGroup
.setLayoutData(new GridData(GridData
.FILL_HORIZONTAL
));
132 sessionGroup
.setLayout(new GridLayout(6, true));
134 fFileNameLabel
= new Label(sessionGroup
, SWT
.RIGHT
);
135 fFileNameLabel
.setText(Messages
.TraceControl_ExecuteScriptSelectLabel
);
136 fFileNameText
= new Text(sessionGroup
, SWT
.NONE
);
138 fBrowseButton
= new Button(sessionGroup
, SWT
.PUSH
);
139 fBrowseButton
.setText(Messages
.TraceControl_ExecuteScriptBrowseText
);
140 fBrowseButton
.addListener(SWT
.Selection
, new Listener() {
142 public void handleEvent(Event event
) {
143 handleFilePathBrowseButtonPressed(SWT
.OPEN
);
148 GridData data
= new GridData(GridData
.FILL_HORIZONTAL
);
149 data
.horizontalSpan
= 1;
150 data
.grabExcessHorizontalSpace
= false;
151 fFileNameLabel
.setLayoutData(data
);
153 data
= new GridData(GridData
.FILL_HORIZONTAL
);
154 data
.horizontalSpan
= 4;
155 fFileNameText
.setLayoutData(data
);
157 data
= new GridData(GridData
.FILL_HORIZONTAL
);
158 data
.horizontalSpan
= 1;
160 // Initialize a empty list
161 fCommands
= new ArrayList
<>();
163 return fDialogComposite
;
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
);
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);
181 protected void okPressed() {
182 // Validate input data
183 String sessionPath
= fFileNameText
.getText();
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) {
191 line
= rafile
.getNextLine();
193 } catch (IOException e
) {
196 fCommands
= builder
.build();