1 /**********************************************************************
2 * Copyright (c) 2012, 2013 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 * Bernd Hufmann - Added handling of streamed traces
12 **********************************************************************/
13 package org
.eclipse
.linuxtools
.internal
.lttng2
.ui
.views
.control
.dialogs
;
15 import java
.util
.ArrayList
;
16 import java
.util
.List
;
18 import org
.eclipse
.core
.resources
.IFolder
;
19 import org
.eclipse
.core
.resources
.IProject
;
20 import org
.eclipse
.core
.resources
.ResourcesPlugin
;
21 import org
.eclipse
.core
.runtime
.CoreException
;
22 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
23 import org
.eclipse
.jface
.dialogs
.Dialog
;
24 import org
.eclipse
.jface
.dialogs
.IDialogConstants
;
25 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
26 import org
.eclipse
.jface
.viewers
.CheckStateChangedEvent
;
27 import org
.eclipse
.jface
.viewers
.CheckboxTreeViewer
;
28 import org
.eclipse
.jface
.viewers
.ICheckStateListener
;
29 import org
.eclipse
.jface
.window
.Window
;
30 import org
.eclipse
.linuxtools
.internal
.lttng2
.ui
.Activator
;
31 import org
.eclipse
.linuxtools
.internal
.lttng2
.ui
.views
.control
.messages
.Messages
;
32 import org
.eclipse
.linuxtools
.internal
.lttng2
.ui
.views
.control
.model
.impl
.TraceSessionComponent
;
33 import org
.eclipse
.linuxtools
.internal
.lttng2
.ui
.views
.control
.remote
.IRemoteSystemProxy
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.TmfProjectNature
;
35 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceFolder
;
36 import org
.eclipse
.rse
.core
.subsystems
.RemoteChildrenContentsType
;
37 import org
.eclipse
.rse
.services
.clientserver
.messages
.SystemMessageException
;
38 import org
.eclipse
.rse
.subsystems
.files
.core
.servicesubsystem
.IFileServiceSubSystem
;
39 import org
.eclipse
.rse
.subsystems
.files
.core
.subsystems
.IRemoteFile
;
40 import org
.eclipse
.swt
.SWT
;
41 import org
.eclipse
.swt
.custom
.CCombo
;
42 import org
.eclipse
.swt
.graphics
.Point
;
43 import org
.eclipse
.swt
.layout
.GridData
;
44 import org
.eclipse
.swt
.layout
.GridLayout
;
45 import org
.eclipse
.swt
.widgets
.Button
;
46 import org
.eclipse
.swt
.widgets
.Composite
;
47 import org
.eclipse
.swt
.widgets
.Control
;
48 import org
.eclipse
.swt
.widgets
.Group
;
49 import org
.eclipse
.swt
.widgets
.Shell
;
50 import org
.eclipse
.swt
.widgets
.Text
;
51 import org
.eclipse
.swt
.widgets
.Tree
;
52 import org
.eclipse
.ui
.model
.WorkbenchContentProvider
;
53 import org
.eclipse
.ui
.model
.WorkbenchLabelProvider
;
57 * Dialog box for collecting trace import information.
60 * @author Bernd Hufmann
62 public class ImportDialog
extends Dialog
implements IImportDialog
{
64 // ------------------------------------------------------------------------
66 // ------------------------------------------------------------------------
67 /** The icon file for this dialog box. */
68 public static final String IMPORT_ICON_FILE
= "icons/elcl16/import_trace.gif"; //$NON-NLS-1$
70 /** Parent directory for UST traces */
71 public static final String UST_PARENT_DIRECTORY
= "ust"; //$NON-NLS-1$
73 /** Name of metadata file of trace */
74 public static final String METADATA_FILE_NAME
= "metadata"; //$NON-NLS-1$
76 /** Default name of kernel traces */
77 public static final String DEFAULT_KERNEL_TRACE_NAME
= "kernel"; //$NON-NLS-1$
79 // ------------------------------------------------------------------------
81 // ------------------------------------------------------------------------
83 * The dialog composite.
85 private Composite fDialogComposite
= null;
87 * The checkbox tree viewer for selecting available traces
89 private CheckboxTreeViewer fFolderViewer
;
91 * The combo box for selecting a project.
93 private CCombo fCombo
;
95 * The overwrite button
97 private Button fOverwriteButton
;
99 * List of available LTTng 2.0 projects
101 private List
<IProject
> fProjects
;
103 * The parent where the new node should be added.
105 private TraceSessionComponent fSession
= null;
108 * The name of the default project name
110 private String fDefaultProjectName
= null;
112 * List of traces to import
114 private final List
<ImportFileInfo
> fTraces
= new ArrayList
<ImportFileInfo
>();
116 * Selection index in project combo box.
118 private int fProjectIndex
;
120 * Flag to indicate that something went wrong when creating the dialog box.
122 private boolean fIsError
= false;
124 // ------------------------------------------------------------------------
126 // ------------------------------------------------------------------------
129 * @param shell - a shell for the display of the dialog
131 public ImportDialog(Shell shell
) {
133 setShellStyle(SWT
.RESIZE
);
136 // ------------------------------------------------------------------------
138 // ------------------------------------------------------------------------
141 public List
<ImportFileInfo
> getTracePathes() {
142 List
<ImportFileInfo
> retList
= new ArrayList
<ImportFileInfo
>();
143 retList
.addAll(fTraces
);
148 public IProject
getProject() {
149 return fProjects
.get(fProjectIndex
);
153 public void setSession(TraceSessionComponent session
) {
158 public void setDefaultProject(String defaultProject
) {
159 fDefaultProjectName
= defaultProject
;
162 // ------------------------------------------------------------------------
164 // ------------------------------------------------------------------------
167 protected void configureShell(Shell newShell
) {
168 super.configureShell(newShell
);
169 newShell
.setText(Messages
.TraceControl_ImportDialogTitle
);
170 newShell
.setImage(Activator
.getDefault().loadIcon(IMPORT_ICON_FILE
));
174 protected Control
createDialogArea(Composite parent
) {
177 fDialogComposite
= new Composite(parent
, SWT
.NONE
);
178 GridLayout layout
= new GridLayout(1, true);
179 fDialogComposite
.setLayout(layout
);
180 fDialogComposite
.setLayoutData(new GridData(GridData
.FILL_BOTH
));
183 createRemoteComposite();
184 } catch (CoreException e
) {
185 createErrorComposite(parent
, e
.fillInStackTrace());
186 return fDialogComposite
;
187 } catch (SystemMessageException e
) {
188 createErrorComposite(parent
, e
.fillInStackTrace());
189 return fDialogComposite
;
191 return fDialogComposite
;
195 protected void createButtonsForButtonBar(Composite parent
) {
196 createButton(parent
, IDialogConstants
.CANCEL_ID
, "&Cancel", true); //$NON-NLS-1$
197 createButton(parent
, IDialogConstants
.OK_ID
, "&Ok", true); //$NON-NLS-1$
201 protected void okPressed() {
204 // Validate input data
207 fProjectIndex
= fCombo
.getSelectionIndex();
209 if (fProjectIndex
< 0) {
210 MessageDialog
.openError(getShell(),
211 Messages
.TraceControl_ImportDialogTitle
,
212 Messages
.TraceControl_ImportDialogNoProjectSelectedError
);
216 IProject project
= fProjects
.get(fProjectIndex
);
217 IFolder traceFolder
= project
.getFolder(TmfTraceFolder
.TRACE_FOLDER_NAME
);
219 if (!traceFolder
.exists()) {
220 // Invalid LTTng 2.0 project
221 MessageDialog
.openError(getShell(),
222 Messages
.TraceControl_ImportDialogTitle
,
223 Messages
.TraceControl_ImportDialogInvalidTracingProject
+ " (" + TmfTraceFolder
.TRACE_FOLDER_NAME
+ ")"); //$NON-NLS-1$//$NON-NLS-2$
227 boolean overwriteAll
= fOverwriteButton
.getSelection();
229 Object
[] checked
= fFolderViewer
.getCheckedElements();
230 for (int i
= 0; i
< checked
.length
; i
++) {
231 IRemoteFile file
= (IRemoteFile
) checked
[i
];
232 if (!file
.isDirectory() && file
.getName().equals(METADATA_FILE_NAME
)) {
233 IRemoteFile trace
= file
.getParentRemoteFile();
234 IRemoteFile parent
= trace
.getParentRemoteFile();
235 boolean isKernel
= false;
236 if (trace
.getName().equals(DEFAULT_KERNEL_TRACE_NAME
)) {
239 StringBuffer traceName
= new StringBuffer();
240 traceName
.append(trace
.getName());
241 traceName
.insert(0, '-');
243 String path
= fSession
.isSnapshotSession() ? fSession
.getSnapshotInfo().getSnapshotPath() : fSession
.getSessionPath();
245 while (!parent
.getAbsolutePath().equals(path
)) {
246 traceName
.insert(0, parent
.getName());
247 traceName
.insert(0, '-');
248 parent
= parent
.getParentRemoteFile();
250 traceName
.insert(0, parent
.getName());
252 ImportFileInfo info
= new ImportFileInfo(trace
, traceName
.toString(), overwriteAll
, isKernel
);
253 IFolder folder
= traceFolder
.getFolder(traceName
.toString());
255 // Verify if trace directory already exists (and not overwrite)
256 if (folder
.exists() && !overwriteAll
) {
258 // Ask user for overwrite or new name
259 IImportConfirmationDialog conf
= TraceControlDialogFactory
.getInstance().getImportConfirmationDialog();
260 conf
.setTraceName(traceName
.toString());
262 // Don't add trace to list if dialog was cancelled.
263 if (conf
.open() == Window
.OK
) {
264 info
.setOverwrite(conf
.isOverwrite());
265 if (!conf
.isOverwrite()) {
266 info
.setLocalTraceName(conf
.getNewTraceName());
276 if (fTraces
.isEmpty()) {
277 MessageDialog
.openError(getShell(),
278 Messages
.TraceControl_ImportDialogTitle
,
279 Messages
.TraceControl_ImportDialogNoTraceSelectedError
);
284 // validation successful -> call super.okPressed()
288 // ------------------------------------------------------------------------
289 // Helper methods and classes
290 // ------------------------------------------------------------------------
293 * Helper class for the contents of a folder in a tracing project
295 * @author Bernd Hufmann
297 public static class FolderContentProvider
extends WorkbenchContentProvider
{
299 public Object
[] getChildren(Object o
) {
300 if (o
instanceof IRemoteFile
) {
301 IRemoteFile element
= (IRemoteFile
) o
;
302 // For our purpose, we need folders + files
303 if (!element
.isDirectory()) {
304 return new Object
[0];
307 return super.getChildren(o
);
312 * Creates a dialog composite with an error message which can be used
313 * when an exception occurred during creation time of the dialog box.
314 * @param parent - a parent composite
315 * @param e - a error causing exception
317 private void createErrorComposite(Composite parent
, Throwable e
) {
319 fDialogComposite
.dispose();
321 fDialogComposite
= new Composite(parent
, SWT
.NONE
);
322 GridLayout layout
= new GridLayout(1, true);
323 fDialogComposite
.setLayout(layout
);
324 fDialogComposite
.setLayoutData(new GridData(GridData
.FILL_BOTH
));
326 Text errorText
= new Text(fDialogComposite
, SWT
.MULTI
);
327 StringBuffer error
= new StringBuffer();
328 error
.append(Messages
.TraceControl_ImportDialogCreationError
);
329 error
.append(System
.getProperty("line.separator")); //$NON-NLS-1$
330 error
.append(System
.getProperty("line.separator")); //$NON-NLS-1$
331 error
.append(e
.toString());
332 errorText
.setText(error
.toString());
333 errorText
.setLayoutData(new GridData(GridData
.FILL_BOTH
));
336 private void createRemoteComposite() throws CoreException
, SystemMessageException
{
337 Group contextGroup
= new Group(fDialogComposite
, SWT
.SHADOW_NONE
);
338 contextGroup
.setText(Messages
.TraceControl_ImportDialogTracesGroupName
);
339 GridLayout layout
= new GridLayout(1, true);
340 contextGroup
.setLayout(layout
);
341 contextGroup
.setLayoutData(new GridData(GridData
.FILL_BOTH
));
343 IRemoteSystemProxy proxy
= fSession
.getTargetNode().getRemoteSystemProxy();
345 IFileServiceSubSystem fsss
= proxy
.getFileServiceSubSystem();
347 final String path
= fSession
.isSnapshotSession() ? fSession
.getSnapshotInfo().getSnapshotPath() : fSession
.getSessionPath();
348 final IRemoteFile remoteFolder
= fsss
.getRemoteFileObject(path
, new NullProgressMonitor());
349 // make sure that remote directory is read and not cached
350 remoteFolder
.markStale(true, true);
352 fFolderViewer
= new CheckboxTreeViewer(contextGroup
, SWT
.BORDER
);
353 GridData data
= new GridData(GridData
.FILL_BOTH
);
354 Tree tree
= fFolderViewer
.getTree();
355 tree
.setLayoutData(data
);
356 tree
.setFont(fDialogComposite
.getFont());
357 tree
.setToolTipText(Messages
.TraceControl_ImportDialogTracesTooltip
);
359 fFolderViewer
.setContentProvider(new FolderContentProvider());
360 fFolderViewer
.setLabelProvider(new WorkbenchLabelProvider());
362 fFolderViewer
.addCheckStateListener(new ICheckStateListener() {
364 public void checkStateChanged(CheckStateChangedEvent event
) {
365 Object elem
= event
.getElement();
366 if (elem
instanceof IRemoteFile
) {
367 IRemoteFile element
= (IRemoteFile
) elem
;
368 if (!element
.isDirectory()) {
369 // A trick to keep selection of a file in sync with the directory
370 boolean p
= fFolderViewer
.getChecked((element
.getParentRemoteFile()));
371 fFolderViewer
.setChecked(element
, p
);
374 fFolderViewer
.setSubtreeChecked(event
.getElement(), event
.getChecked());
375 if (!event
.getChecked()) {
376 fFolderViewer
.setChecked(element
.getParentRemoteFile(), false);
381 fFolderViewer
.setInput(remoteFolder
);
383 Object
[] children
= remoteFolder
.getContents(RemoteChildrenContentsType
.getInstance());
384 // children can be null if there the path doesn't exist. This happens when a trace
385 // session hadn't been started and no output was created.
386 if (children
!= null) {
387 // Select all traces by default
388 for (int i
= 0; i
< children
.length
; i
++) {
389 fFolderViewer
.setSubtreeChecked(children
[i
], true);
393 Group projectGroup
= new Group(fDialogComposite
, SWT
.SHADOW_NONE
);
394 projectGroup
.setText(Messages
.TraceControl_ImportDialogProjectsGroupName
);
395 layout
= new GridLayout(1, true);
396 projectGroup
.setLayout(layout
);
397 projectGroup
.setLayoutData(new GridData(GridData
.FILL_HORIZONTAL
));
399 fProjects
= new ArrayList
<IProject
>();
400 List
<String
> projectNames
= new ArrayList
<String
>();
402 for (IProject project
: ResourcesPlugin
.getWorkspace().getRoot().getProjects()) {
403 if (project
.isOpen() && project
.hasNature(TmfProjectNature
.ID
)) {
404 fProjects
.add(project
);
405 projectNames
.add(project
.getName());
409 fCombo
= new CCombo(projectGroup
, SWT
.READ_ONLY
);
410 fCombo
.setToolTipText(Messages
.TraceControl_ImportDialogProjectsTooltip
);
411 fCombo
.setLayoutData(new GridData(GridData
.FILL
, GridData
.CENTER
, true, false, 1, 1));
412 fCombo
.setItems(projectNames
.toArray(new String
[projectNames
.size()]));
414 if (fDefaultProjectName
!= null) {
415 int select
= projectNames
.indexOf(fDefaultProjectName
);
416 fCombo
.select(select
);
419 Group overrideGroup
= new Group(fDialogComposite
, SWT
.SHADOW_NONE
);
420 layout
= new GridLayout(1, true);
421 overrideGroup
.setLayout(layout
);
422 overrideGroup
.setLayoutData(new GridData(GridData
.FILL_HORIZONTAL
));
424 fOverwriteButton
= new Button(overrideGroup
, SWT
.CHECK
);
425 fOverwriteButton
.setText(Messages
.TraceControl_ImportDialogOverwriteButtonText
);
426 getShell().setMinimumSize(new Point(500, 400));