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
.viewers
.StructuredSelection
;
30 import org
.eclipse
.jface
.window
.Window
;
31 import org
.eclipse
.jface
.wizard
.WizardDialog
;
32 import org
.eclipse
.linuxtools
.internal
.lttng2
.ui
.Activator
;
33 import org
.eclipse
.linuxtools
.internal
.lttng2
.ui
.views
.control
.messages
.Messages
;
34 import org
.eclipse
.linuxtools
.internal
.lttng2
.ui
.views
.control
.model
.impl
.TraceSessionComponent
;
35 import org
.eclipse
.linuxtools
.internal
.lttng2
.ui
.views
.control
.remote
.IRemoteSystemProxy
;
36 import org
.eclipse
.linuxtools
.tmf
.core
.TmfProjectNature
;
37 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceFolder
;
38 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.wizards
.importtrace
.ImportTraceWizard
;
39 import org
.eclipse
.rse
.services
.clientserver
.messages
.SystemMessageException
;
40 import org
.eclipse
.rse
.subsystems
.files
.core
.servicesubsystem
.IFileServiceSubSystem
;
41 import org
.eclipse
.rse
.subsystems
.files
.core
.subsystems
.IRemoteFile
;
42 import org
.eclipse
.swt
.SWT
;
43 import org
.eclipse
.swt
.custom
.CCombo
;
44 import org
.eclipse
.swt
.graphics
.Point
;
45 import org
.eclipse
.swt
.layout
.GridData
;
46 import org
.eclipse
.swt
.layout
.GridLayout
;
47 import org
.eclipse
.swt
.widgets
.Button
;
48 import org
.eclipse
.swt
.widgets
.Composite
;
49 import org
.eclipse
.swt
.widgets
.Control
;
50 import org
.eclipse
.swt
.widgets
.Group
;
51 import org
.eclipse
.swt
.widgets
.Shell
;
52 import org
.eclipse
.swt
.widgets
.Text
;
53 import org
.eclipse
.swt
.widgets
.Tree
;
54 import org
.eclipse
.ui
.PlatformUI
;
55 import org
.eclipse
.ui
.model
.WorkbenchContentProvider
;
56 import org
.eclipse
.ui
.model
.WorkbenchLabelProvider
;
60 * Dialog box for collecting trace import information.
63 * @author Bernd Hufmann
65 public class ImportDialog
extends Dialog
implements IImportDialog
{
67 // ------------------------------------------------------------------------
69 // ------------------------------------------------------------------------
70 /** The icon file for this dialog box. */
71 public static final String IMPORT_ICON_FILE
= "icons/elcl16/import_trace.gif"; //$NON-NLS-1$
73 /** Parent directory for UST traces */
74 public static final String UST_PARENT_DIRECTORY
= "ust"; //$NON-NLS-1$
76 // ------------------------------------------------------------------------
78 // ------------------------------------------------------------------------
80 * The dialog composite.
82 private Composite fDialogComposite
= null;
84 * The checkbox tree viewer for selecting available traces
86 private CheckboxTreeViewer fFolderViewer
;
88 * The combo box for selecting a project.
90 private CCombo fCombo
;
92 * The overwrite button
94 private Button fOverwriteButton
;
96 * List of available LTTng 2.0 projects
98 private List
<IProject
> fProjects
;
100 * The parent where the new node should be added.
102 private TraceSessionComponent fSession
= null;
104 * List of traces to import
106 private final List
<ImportFileInfo
> fTraces
= new ArrayList
<ImportFileInfo
>();
108 * Selection index in project combo box.
110 private int fProjectIndex
;
112 * Flag to indicate that something went wrong when creating the dialog box.
114 private boolean fIsError
= false;
116 // ------------------------------------------------------------------------
118 // ------------------------------------------------------------------------
121 * @param shell - a shell for the display of the dialog
123 public ImportDialog(Shell shell
) {
125 setShellStyle(SWT
.RESIZE
);
128 // ------------------------------------------------------------------------
130 // ------------------------------------------------------------------------
133 public List
<ImportFileInfo
> getTracePathes() {
134 List
<ImportFileInfo
> retList
= new ArrayList
<ImportFileInfo
>();
135 retList
.addAll(fTraces
);
140 public IProject
getProject() {
141 return fProjects
.get(fProjectIndex
);
145 public void setSession(TraceSessionComponent session
) {
149 // ------------------------------------------------------------------------
151 // ------------------------------------------------------------------------
154 protected void configureShell(Shell newShell
) {
155 super.configureShell(newShell
);
156 newShell
.setText(Messages
.TraceControl_ImportDialogTitle
);
157 newShell
.setImage(Activator
.getDefault().loadIcon(IMPORT_ICON_FILE
));
161 protected Control
createDialogArea(Composite parent
) {
164 fDialogComposite
= new Composite(parent
, SWT
.NONE
);
165 GridLayout layout
= new GridLayout(1, true);
166 fDialogComposite
.setLayout(layout
);
167 fDialogComposite
.setLayoutData(new GridData(GridData
.FILL_BOTH
));
170 if (fSession
.isStreamedTrace()) {
171 createLocalComposite();
173 createRemoteComposite();
175 } catch (CoreException e
) {
176 createErrorComposite(parent
, e
.fillInStackTrace());
177 return fDialogComposite
;
178 } catch (SystemMessageException e
) {
179 createErrorComposite(parent
, e
.fillInStackTrace());
180 return fDialogComposite
;
182 return fDialogComposite
;
186 protected void createButtonsForButtonBar(Composite parent
) {
187 createButton(parent
, IDialogConstants
.CANCEL_ID
, "&Cancel", true); //$NON-NLS-1$
188 Button importLocallyButton
= createButton(parent
, IDialogConstants
.OK_ID
, "&Ok", true); //$NON-NLS-1$
189 if (fSession
.isStreamedTrace()) {
190 importLocallyButton
.setText("&Next..."); //$NON-NLS-1$
195 protected void okPressed() {
198 // Validate input data
201 fProjectIndex
= fCombo
.getSelectionIndex();
203 if (fProjectIndex
< 0) {
204 MessageDialog
.openError(getShell(),
205 Messages
.TraceControl_ImportDialogTitle
,
206 Messages
.TraceControl_ImportDialogNoProjectSelectedError
);
210 if (fSession
.isStreamedTrace()) {
211 // For streaming use standard import wizard from TMF because exact location
212 // is not available (lttng backend limitation)
213 IProject project
= fProjects
.get(fCombo
.getSelectionIndex());
214 ImportTraceWizard wizard
= new ImportTraceWizard();
215 wizard
.init(PlatformUI
.getWorkbench(), new StructuredSelection(project
));
216 WizardDialog dialog
= new WizardDialog(getShell(), wizard
);
217 if (dialog
.open() == Window
.OK
) {
220 super.cancelPressed();
224 IProject project
= fProjects
.get(fProjectIndex
);
225 IFolder traceFolder
= project
.getFolder(TmfTraceFolder
.TRACE_FOLDER_NAME
);
227 if (!traceFolder
.exists()) {
228 // Invalid LTTng 2.0 project
229 MessageDialog
.openError(getShell(),
230 Messages
.TraceControl_ImportDialogTitle
,
231 Messages
.TraceControl_ImportDialogInvalidTracingProject
+ " (" + TmfTraceFolder
.TRACE_FOLDER_NAME
+ ")"); //$NON-NLS-1$//$NON-NLS-2$
235 boolean overwriteAll
= fOverwriteButton
.getSelection();
237 Object
[] checked
= fFolderViewer
.getCheckedElements();
238 for (int i
= 0; i
< checked
.length
; i
++) {
239 IRemoteFile file
= (IRemoteFile
) checked
[i
];
241 // Only add actual trace directories
242 if (file
.isDirectory() && !UST_PARENT_DIRECTORY
.equals(file
.getName())) {
244 ImportFileInfo info
= new ImportFileInfo(file
, file
.getName(), overwriteAll
);
245 String traceName
= info
.getLocalTraceName();
246 IFolder folder
= traceFolder
.getFolder(traceName
);
248 // Verify if trace directory already exists (and not overwrite)
249 if (folder
.exists() && !overwriteAll
) {
251 // Ask user for overwrite or new name
252 IImportConfirmationDialog conf
= TraceControlDialogFactory
.getInstance().getImportConfirmationDialog();
253 conf
.setTraceName(traceName
);
255 // Don't add trace to list if dialog was cancelled.
256 if (conf
.open() == Window
.OK
) {
257 info
.setOverwrite(conf
.isOverwrite());
258 if (!conf
.isOverwrite()) {
259 info
.setLocalTraceName(conf
.getNewTraceName());
269 if (fTraces
.isEmpty()) {
270 MessageDialog
.openError(getShell(),
271 Messages
.TraceControl_ImportDialogTitle
,
272 Messages
.TraceControl_ImportDialogNoTraceSelectedError
);
277 // validation successful -> call super.okPressed()
281 // ------------------------------------------------------------------------
282 // Helper methods and classes
283 // ------------------------------------------------------------------------
286 * Helper class for the contents of a folder in a tracing project
288 * @author Bernd Hufmann
290 public static class FolderContentProvider
extends WorkbenchContentProvider
{
292 public Object
[] getChildren(Object o
) {
293 if (o
instanceof IRemoteFile
) {
294 IRemoteFile element
= (IRemoteFile
) o
;
295 // For our purpose, we need folders + files
296 if (!element
.isDirectory()) {
297 return new Object
[0];
300 return super.getChildren(o
);
305 * Creates a dialog composite with an error message which can be used
306 * when an exception occurred during creation time of the dialog box.
307 * @param parent - a parent composite
308 * @param e - a error causing exception
310 private void createErrorComposite(Composite parent
, Throwable e
) {
312 fDialogComposite
.dispose();
314 fDialogComposite
= new Composite(parent
, SWT
.NONE
);
315 GridLayout layout
= new GridLayout(1, true);
316 fDialogComposite
.setLayout(layout
);
317 fDialogComposite
.setLayoutData(new GridData(GridData
.FILL_BOTH
));
319 Text errorText
= new Text(fDialogComposite
, SWT
.MULTI
);
320 StringBuffer error
= new StringBuffer();
321 error
.append(Messages
.TraceControl_ImportDialogCreationError
);
322 error
.append(System
.getProperty("line.separator")); //$NON-NLS-1$
323 error
.append(System
.getProperty("line.separator")); //$NON-NLS-1$
324 error
.append(e
.toString());
325 errorText
.setText(error
.toString());
326 errorText
.setLayoutData(new GridData(GridData
.FILL_BOTH
));
329 private void createRemoteComposite() throws CoreException
, SystemMessageException
{
330 Group contextGroup
= new Group(fDialogComposite
, SWT
.SHADOW_NONE
);
331 contextGroup
.setText(Messages
.TraceControl_ImportDialogTracesGroupName
);
332 GridLayout layout
= new GridLayout(1, true);
333 contextGroup
.setLayout(layout
);
334 contextGroup
.setLayoutData(new GridData(GridData
.FILL_BOTH
));
336 IRemoteSystemProxy proxy
= fSession
.getTargetNode().getRemoteSystemProxy();
338 IFileServiceSubSystem fsss
= proxy
.getFileServiceSubSystem();
340 IRemoteFile remoteFolder
= fsss
.getRemoteFileObject(fSession
.getSessionPath(), new NullProgressMonitor());
342 fFolderViewer
= new CheckboxTreeViewer(contextGroup
, SWT
.BORDER
);
343 GridData data
= new GridData(GridData
.FILL_BOTH
);
344 Tree tree
= fFolderViewer
.getTree();
345 tree
.setLayoutData(data
);
346 tree
.setFont(fDialogComposite
.getFont());
347 tree
.setToolTipText(Messages
.TraceControl_ImportDialogTracesTooltip
);
349 fFolderViewer
.setContentProvider(new FolderContentProvider());
350 fFolderViewer
.setLabelProvider(new WorkbenchLabelProvider());
352 fFolderViewer
.addCheckStateListener(new ICheckStateListener() {
354 public void checkStateChanged(CheckStateChangedEvent event
) {
355 Object elem
= event
.getElement();
356 if (elem
instanceof IRemoteFile
) {
357 IRemoteFile element
= (IRemoteFile
) elem
;
358 if (!element
.isDirectory()) {
359 // A trick to keep selection of a file in sync with the directory
360 boolean p
= fFolderViewer
.getChecked((element
.getParentRemoteFile()));
361 fFolderViewer
.setChecked(element
, p
);
364 fFolderViewer
.setSubtreeChecked(event
.getElement(), event
.getChecked());
365 if (!event
.getChecked()) {
366 fFolderViewer
.setChecked(element
.getParentRemoteFile(), false);
371 fFolderViewer
.setInput(remoteFolder
);
373 Group projectGroup
= new Group(fDialogComposite
, SWT
.SHADOW_NONE
);
374 projectGroup
.setText(Messages
.TraceControl_ImportDialogProjectsGroupName
);
375 layout
= new GridLayout(1, true);
376 projectGroup
.setLayout(layout
);
377 projectGroup
.setLayoutData(new GridData(GridData
.FILL_HORIZONTAL
));
379 fProjects
= new ArrayList
<IProject
>();
380 List
<String
> projectNames
= new ArrayList
<String
>();
381 for (IProject project
: ResourcesPlugin
.getWorkspace().getRoot().getProjects()) {
382 if (project
.isOpen() && project
.hasNature(TmfProjectNature
.ID
)) {
383 fProjects
.add(project
);
384 projectNames
.add(project
.getName());
388 fCombo
= new CCombo(projectGroup
, SWT
.READ_ONLY
);
389 fCombo
.setToolTipText(Messages
.TraceControl_ImportDialogProjectsTooltip
);
390 fCombo
.setLayoutData(new GridData(GridData
.FILL
, GridData
.CENTER
, true, false, 1, 1));
391 fCombo
.setItems(projectNames
.toArray(new String
[projectNames
.size()]));
393 Group overrideGroup
= new Group(fDialogComposite
, SWT
.SHADOW_NONE
);
394 layout
= new GridLayout(1, true);
395 overrideGroup
.setLayout(layout
);
396 overrideGroup
.setLayoutData(new GridData(GridData
.FILL_HORIZONTAL
));
398 fOverwriteButton
= new Button(overrideGroup
, SWT
.CHECK
);
399 fOverwriteButton
.setText(Messages
.TraceControl_ImportDialogOverwriteButtonText
);
400 getShell().setMinimumSize(new Point(500, 400));
403 private void createLocalComposite() throws CoreException
{
405 Group projectGroup
= new Group(fDialogComposite
, SWT
.SHADOW_NONE
);
406 projectGroup
.setText(Messages
.TraceControl_ImportDialogProjectsGroupName
);
407 GridLayout layout
= new GridLayout(1, true);
408 projectGroup
.setLayout(layout
);
409 projectGroup
.setLayoutData(new GridData(GridData
.FILL_HORIZONTAL
));
411 fProjects
= new ArrayList
<IProject
>();
412 List
<String
> projectNames
= new ArrayList
<String
>();
413 for (IProject project
: ResourcesPlugin
.getWorkspace().getRoot().getProjects()) {
414 if (project
.isOpen() && project
.hasNature(TmfProjectNature
.ID
)) {
415 fProjects
.add(project
);
416 projectNames
.add(project
.getName());
420 fCombo
= new CCombo(projectGroup
, SWT
.READ_ONLY
);
421 fCombo
.setToolTipText(Messages
.TraceControl_ImportDialogProjectsTooltip
);
422 fCombo
.setLayoutData(new GridData(GridData
.FILL
, GridData
.CENTER
, true, false, 1, 1));
423 fCombo
.setItems(projectNames
.toArray(new String
[projectNames
.size()]));
425 getShell().setMinimumSize(new Point(500, 50));