Add support for streaming feature of LTTng Tools 2.1 (part 1)
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng2.ui / src / org / eclipse / linuxtools / internal / lttng2 / ui / views / control / dialogs / ImportDialog.java
CommitLineData
291cbdbf
BH
1/**********************************************************************
2 * Copyright (c) 2012 Ericsson
cfdb727a 3 *
291cbdbf
BH
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
cfdb727a
AM
8 *
9 * Contributors:
291cbdbf
BH
10 * Bernd Hufmann - Initial API and implementation
11 **********************************************************************/
12package org.eclipse.linuxtools.internal.lttng2.ui.views.control.dialogs;
13
14import java.util.ArrayList;
15import java.util.List;
16
17import org.eclipse.core.resources.IFolder;
18import org.eclipse.core.resources.IProject;
19import org.eclipse.core.resources.ResourcesPlugin;
20import org.eclipse.core.runtime.CoreException;
21import org.eclipse.core.runtime.NullProgressMonitor;
22import org.eclipse.jface.dialogs.Dialog;
23import org.eclipse.jface.dialogs.IDialogConstants;
24import org.eclipse.jface.dialogs.MessageDialog;
25import org.eclipse.jface.viewers.CheckStateChangedEvent;
26import org.eclipse.jface.viewers.CheckboxTreeViewer;
27import org.eclipse.jface.viewers.ICheckStateListener;
f3b33d40 28import org.eclipse.jface.viewers.StructuredSelection;
291cbdbf 29import org.eclipse.jface.window.Window;
f3b33d40 30import org.eclipse.jface.wizard.WizardDialog;
291cbdbf 31import org.eclipse.linuxtools.internal.lttng2.ui.Activator;
9315aeee 32import org.eclipse.linuxtools.internal.lttng2.ui.views.control.messages.Messages;
291cbdbf
BH
33import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.impl.TraceSessionComponent;
34import org.eclipse.linuxtools.internal.lttng2.ui.views.control.remote.IRemoteSystemProxy;
35import org.eclipse.linuxtools.tmf.core.TmfProjectNature;
36import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceFolder;
f3b33d40 37import org.eclipse.linuxtools.tmf.ui.project.wizards.ImportTraceWizard;
291cbdbf
BH
38import org.eclipse.rse.services.clientserver.messages.SystemMessageException;
39import org.eclipse.rse.subsystems.files.core.servicesubsystem.IFileServiceSubSystem;
40import org.eclipse.rse.subsystems.files.core.subsystems.IRemoteFile;
41import org.eclipse.swt.SWT;
42import org.eclipse.swt.custom.CCombo;
43import org.eclipse.swt.graphics.Point;
44import org.eclipse.swt.layout.GridData;
45import org.eclipse.swt.layout.GridLayout;
46import org.eclipse.swt.widgets.Button;
47import org.eclipse.swt.widgets.Composite;
48import org.eclipse.swt.widgets.Control;
49import org.eclipse.swt.widgets.Group;
50import org.eclipse.swt.widgets.Shell;
51import org.eclipse.swt.widgets.Text;
52import org.eclipse.swt.widgets.Tree;
f3b33d40 53import org.eclipse.ui.PlatformUI;
291cbdbf
BH
54import org.eclipse.ui.model.WorkbenchContentProvider;
55import org.eclipse.ui.model.WorkbenchLabelProvider;
56
57/**
291cbdbf
BH
58 * <p>
59 * Dialog box for collecting trace import information.
60 * </p>
cfdb727a 61 *
dbd4432d 62 * @author Bernd Hufmann
291cbdbf
BH
63 */
64public class ImportDialog extends Dialog implements IImportDialog {
65
66 // ------------------------------------------------------------------------
67 // Constants
68 // ------------------------------------------------------------------------
cfdb727a 69 /** The icon file for this dialog box. */
291cbdbf 70 public static final String IMPORT_ICON_FILE = "icons/elcl16/import_trace.gif"; //$NON-NLS-1$
cfdb727a
AM
71
72 /** Parent directory for UST traces */
291cbdbf
BH
73 public static final String UST_PARENT_DIRECTORY = "ust"; //$NON-NLS-1$
74
75 // ------------------------------------------------------------------------
76 // Attributes
77 // ------------------------------------------------------------------------
78 /**
79 * The dialog composite.
80 */
81 private Composite fDialogComposite = null;
82 /**
83 * The checkbox tree viewer for selecting available traces
84 */
85 private CheckboxTreeViewer fFolderViewer;
86 /**
87 * The combo box for selecting a project.
88 */
89 private CCombo fCombo;
90 /**
cfdb727a 91 * The overwrite button
291cbdbf
BH
92 */
93 private Button fOverwriteButton;
f3b33d40
BH
94 /**
95 * The button to open import wizard for import locally.
96 */
97 private Button fImportLocallyButton;
291cbdbf
BH
98 /**
99 * List of available LTTng 2.0 projects
100 */
101 private List<IProject> fProjects;
102 /**
103 * The parent where the new node should be added.
104 */
105 private TraceSessionComponent fSession = null;
106 /**
cfdb727a 107 * List of traces to import
291cbdbf 108 */
cfdb727a 109 private final List<ImportFileInfo> fTraces = new ArrayList<ImportFileInfo>();
291cbdbf 110 /**
cfdb727a 111 * Selection index in project combo box.
291cbdbf
BH
112 */
113 private int fProjectIndex;
114 /**
115 * Flag to indicate that something went wrong when creating the dialog box.
116 */
117 private boolean fIsError = false;
cfdb727a 118
291cbdbf
BH
119 // ------------------------------------------------------------------------
120 // Constructors
121 // ------------------------------------------------------------------------
122 /**
123 * Constructor
124 * @param shell - a shell for the display of the dialog
125 */
126 public ImportDialog(Shell shell) {
127 super(shell);
128 setShellStyle(SWT.RESIZE);
129 }
130
131 // ------------------------------------------------------------------------
132 // Accessors
133 // ------------------------------------------------------------------------
134 /*
135 * (non-Javadoc)
136 * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.dialogs.IImportDialog#getTracePathes()
137 */
138 @Override
139 public List<ImportFileInfo> getTracePathes() {
140 List<ImportFileInfo> retList = new ArrayList<ImportFileInfo>();
141 retList.addAll(fTraces);
142 return retList;
143 }
144
145 /*
146 * (non-Javadoc)
147 * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.dialogs.IImportDialog#getProject()
148 */
149 @Override
150 public IProject getProject() {
151 return fProjects.get(fProjectIndex);
152 }
cfdb727a 153
291cbdbf
BH
154 /*
155 * (non-Javadoc)
156 * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.dialogs.IImportDialog#setSession(org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.impl.TraceSessionComponent)
157 */
158 @Override
159 public void setSession(TraceSessionComponent session) {
160 fSession = session;
161 }
162
163 // ------------------------------------------------------------------------
164 // Operations
165 // ------------------------------------------------------------------------
166 /*
167 * (non-Javadoc)
168 * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
169 */
170 @Override
171 protected void configureShell(Shell newShell) {
172 super.configureShell(newShell);
173 newShell.setText(Messages.TraceControl_ImportDialogTitle);
174 newShell.setImage(Activator.getDefault().loadIcon(IMPORT_ICON_FILE));
175 }
176
177 /*
178 * (non-Javadoc)
179 * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
180 */
181 @Override
182 protected Control createDialogArea(Composite parent) {
cfdb727a 183
291cbdbf
BH
184 // Main dialog panel
185 fDialogComposite = new Composite(parent, SWT.NONE);
186 GridLayout layout = new GridLayout(1, true);
187 fDialogComposite.setLayout(layout);
188 fDialogComposite.setLayoutData(new GridData(GridData.FILL_BOTH));
189
291cbdbf 190 try {
f3b33d40
BH
191 if (fSession.isStreamedTrace()) {
192 createLocalComposite();
193 } else {
194 createRemoteComposite();
291cbdbf 195 }
f3b33d40
BH
196 } catch (CoreException e) {
197 createErrorComposite(parent, e.fillInStackTrace());
198 return fDialogComposite;
291cbdbf
BH
199 } catch (SystemMessageException e) {
200 createErrorComposite(parent, e.fillInStackTrace());
201 return fDialogComposite;
202 }
291cbdbf
BH
203 return fDialogComposite;
204 }
205
206 /*
207 * (non-Javadoc)
208 * @see org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite)
209 */
210 @Override
211 protected void createButtonsForButtonBar(Composite parent) {
79c3db85 212 createButton(parent, IDialogConstants.CANCEL_ID, "&Cancel", true); //$NON-NLS-1$
f3b33d40
BH
213 fImportLocallyButton = createButton(parent, IDialogConstants.OK_ID, "&Ok", true); //$NON-NLS-1$
214 if (fSession.isStreamedTrace()) {
215 fImportLocallyButton.setText("&Next..."); //$NON-NLS-1$
216 }
291cbdbf
BH
217 }
218
219 /*
220 * (non-Javadoc)
221 * @see org.eclipse.jface.dialogs.Dialog#okPressed()
222 */
223 @Override
224 protected void okPressed() {
225 if (!fIsError) {
f3b33d40 226
291cbdbf
BH
227 // Validate input data
228 fTraces.clear();
229
230 fProjectIndex = fCombo.getSelectionIndex();
231
232 if (fProjectIndex < 0) {
233 MessageDialog.openError(getShell(),
234 Messages.TraceControl_ImportDialogTitle,
235 Messages.TraceControl_ImportDialogNoProjectSelectedError);
236 return;
237 }
238
f3b33d40
BH
239 if (fSession.isStreamedTrace()) {
240 // For streaming use standard import wizard from TMF because exact location
241 // is not available (lttng backend limitation)
242 IProject project = fProjects.get(fCombo.getSelectionIndex());
243 ImportTraceWizard wizard = new ImportTraceWizard();
244 wizard.init(PlatformUI.getWorkbench(), new StructuredSelection(project));
245 WizardDialog dialog = new WizardDialog(getShell(), wizard);
246 if (dialog.open() == Window.OK) {
247 super.okPressed();
248 }
249 super.cancelPressed();
250 return;
251 }
252
291cbdbf
BH
253 IProject project = fProjects.get(fProjectIndex);
254 IFolder traceFolder = project.getFolder(TmfTraceFolder.TRACE_FOLDER_NAME);
255
256 if (!traceFolder.exists()) {
257 // Invalid LTTng 2.0 project
258 MessageDialog.openError(getShell(),
259 Messages.TraceControl_ImportDialogTitle,
260 Messages.TraceControl_ImportDialogInvalidTracingProject + " (" + TmfTraceFolder.TRACE_FOLDER_NAME + ")"); //$NON-NLS-1$//$NON-NLS-2$
261 return;
262 }
263
264 boolean overwriteAll = fOverwriteButton.getSelection();
265
266 Object[] checked = fFolderViewer.getCheckedElements();
267 for (int i = 0; i < checked.length; i++) {
268 IRemoteFile file = (IRemoteFile) checked[i];
cfdb727a
AM
269
270 // Only add actual trace directories
291cbdbf 271 if (file.isDirectory() && !UST_PARENT_DIRECTORY.equals(file.getName())) {
cfdb727a 272
291cbdbf
BH
273 ImportFileInfo info = new ImportFileInfo(file, file.getName(), overwriteAll);
274 String traceName = info.getLocalTraceName();
275 IFolder folder = traceFolder.getFolder(traceName);
276
cfdb727a 277 // Verify if trace directory already exists (and not overwrite)
291cbdbf
BH
278 if (folder.exists() && !overwriteAll) {
279
280 // Ask user for overwrite or new name
281 IImportConfirmationDialog conf = TraceControlDialogFactory.getInstance().getImportConfirmationDialog();
282 conf.setTraceName(traceName);
283
284 // Don't add trace to list if dialog was cancelled.
285 if (conf.open() == Window.OK) {
286 info.setOverwrite(conf.isOverwrite());
287 if (!conf.isOverwrite()) {
288 info.setLocalTraceName(conf.getNewTraceName());
289 }
290 fTraces.add(info);
291 }
cfdb727a 292 } else {
291cbdbf
BH
293 fTraces.add(info);
294 }
295 }
296 }
297
298 if (fTraces.isEmpty()) {
299 MessageDialog.openError(getShell(),
300 Messages.TraceControl_ImportDialogTitle,
301 Messages.TraceControl_ImportDialogNoTraceSelectedError);
302 return;
303 }
304 }
305
306 // validation successful -> call super.okPressed()
307 super.okPressed();
308 }
309
310 // ------------------------------------------------------------------------
311 // Helper methods and classes
312 // ------------------------------------------------------------------------
cfdb727a
AM
313 /**
314 * Helper class for the contents of a folder in a tracing project
315 *
316 * @author Bernd Hufmann
317 */
f455db37 318 public static class FolderContentProvider extends WorkbenchContentProvider {
291cbdbf
BH
319 @Override
320 public Object[] getChildren(Object o) {
321 if (o instanceof IRemoteFile) {
322 IRemoteFile element = (IRemoteFile) o;
323 // For our purpose, we need folders + files
324 if (!element.isDirectory()) {
325 return new Object[0];
326 }
327 }
328 return super.getChildren(o);
329 }
330 }
cfdb727a 331
291cbdbf
BH
332 /**
333 * Creates a dialog composite with an error message which can be used
334 * when an exception occurred during creation time of the dialog box.
335 * @param parent - a parent composite
cfdb727a 336 * @param e - a error causing exception
291cbdbf
BH
337 */
338 private void createErrorComposite(Composite parent, Throwable e) {
339 fIsError = true;
340 fDialogComposite.dispose();
cfdb727a 341
291cbdbf
BH
342 fDialogComposite = new Composite(parent, SWT.NONE);
343 GridLayout layout = new GridLayout(1, true);
344 fDialogComposite.setLayout(layout);
345 fDialogComposite.setLayoutData(new GridData(GridData.FILL_BOTH));
cfdb727a 346
291cbdbf
BH
347 Text errorText = new Text(fDialogComposite, SWT.MULTI);
348 StringBuffer error = new StringBuffer();
349 error.append(Messages.TraceControl_ImportDialogCreationError);
350 error.append(System.getProperty("line.separator")); //$NON-NLS-1$
351 error.append(System.getProperty("line.separator")); //$NON-NLS-1$
352 error.append(e.toString());
353 errorText.setText(error.toString());
354 errorText.setLayoutData(new GridData(GridData.FILL_BOTH));
355 }
356
f3b33d40
BH
357 private void createRemoteComposite() throws CoreException, SystemMessageException{
358 Group contextGroup = new Group(fDialogComposite, SWT.SHADOW_NONE);
359 contextGroup.setText(Messages.TraceControl_ImportDialogTracesGroupName);
360 GridLayout layout = new GridLayout(1, true);
361 contextGroup.setLayout(layout);
362 contextGroup.setLayoutData(new GridData(GridData.FILL_BOTH));
363
364 IRemoteSystemProxy proxy = fSession.getTargetNode().getRemoteSystemProxy();
365
366 IFileServiceSubSystem fsss = proxy.getFileServiceSubSystem();
367
368 IRemoteFile remoteFolder = fsss.getRemoteFileObject(fSession.getSessionPath(), new NullProgressMonitor());
369
370 fFolderViewer = new CheckboxTreeViewer(contextGroup, SWT.BORDER);
371 GridData data = new GridData(GridData.FILL_BOTH);
372 Tree tree = fFolderViewer.getTree();
373 tree.setLayoutData(data);
374 tree.setFont(fDialogComposite.getFont());
375 tree.setToolTipText(Messages.TraceControl_ImportDialogTracesTooltip);
376
377 fFolderViewer.setContentProvider(new FolderContentProvider());
378 fFolderViewer.setLabelProvider(new WorkbenchLabelProvider());
379
380 fFolderViewer.addCheckStateListener(new ICheckStateListener() {
381 @Override
382 public void checkStateChanged(CheckStateChangedEvent event) {
383 Object elem = event.getElement();
384 if (elem instanceof IRemoteFile) {
385 IRemoteFile element = (IRemoteFile) elem;
386 if (!element.isDirectory()) {
387 // A trick to keep selection of a file in sync with the directory
388 boolean p = fFolderViewer.getChecked((element.getParentRemoteFile()));
389 fFolderViewer.setChecked(element, p);
390 return;
391 }
392 fFolderViewer.setSubtreeChecked(event.getElement(), event.getChecked());
393 if (!event.getChecked()) {
394 fFolderViewer.setChecked(element.getParentRemoteFile(), false);
395 }
396 }
397 }
398 });
399 fFolderViewer.setInput(remoteFolder);
400
401 Group projectGroup = new Group(fDialogComposite, SWT.SHADOW_NONE);
402 projectGroup.setText(Messages.TraceControl_ImportDialogProjectsGroupName);
403 layout = new GridLayout(1, true);
404 projectGroup.setLayout(layout);
405 projectGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
406
407 fProjects = new ArrayList<IProject>();
408 List<String> projectNames = new ArrayList<String>();
409 for (IProject project : ResourcesPlugin.getWorkspace().getRoot().getProjects()) {
410 if (project.isOpen() && project.hasNature(TmfProjectNature.ID)) {
411 fProjects.add(project);
412 projectNames.add(project.getName());
413 }
414 }
415
416 fCombo = new CCombo(projectGroup, SWT.READ_ONLY);
417 fCombo.setToolTipText(Messages.TraceControl_ImportDialogProjectsTooltip);
418 fCombo.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false, 1, 1));
419 fCombo.setItems(projectNames.toArray(new String[projectNames.size()]));
420
421 Group overrideGroup = new Group(fDialogComposite, SWT.SHADOW_NONE);
422 layout = new GridLayout(1, true);
423 overrideGroup.setLayout(layout);
424 overrideGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
425
426 fOverwriteButton = new Button(overrideGroup, SWT.CHECK);
427 fOverwriteButton.setText(Messages.TraceControl_ImportDialogOverwriteButtonText);
428 getShell().setMinimumSize(new Point(500, 400));
429 }
430
431 private void createLocalComposite() throws CoreException {
432
433 Group projectGroup = new Group(fDialogComposite, SWT.SHADOW_NONE);
434 projectGroup.setText(Messages.TraceControl_ImportDialogProjectsGroupName);
435 GridLayout layout = new GridLayout(1, true);
436 projectGroup.setLayout(layout);
437 projectGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
438
439 fProjects = new ArrayList<IProject>();
440 List<String> projectNames = new ArrayList<String>();
441 for (IProject project : ResourcesPlugin.getWorkspace().getRoot().getProjects()) {
442 if (project.isOpen() && project.hasNature(TmfProjectNature.ID)) {
443 fProjects.add(project);
444 projectNames.add(project.getName());
445 }
446 }
447
448 fCombo = new CCombo(projectGroup, SWT.READ_ONLY);
449 fCombo.setToolTipText(Messages.TraceControl_ImportDialogProjectsTooltip);
450 fCombo.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false, 1, 1));
451 fCombo.setItems(projectNames.toArray(new String[projectNames.size()]));
cfdb727a 452
f3b33d40
BH
453// Group overrideGroup = new Group(fDialogComposite, SWT.SHADOW_NONE);
454// layout = new GridLayout(1, true);
455// overrideGroup.setLayout(layout);
456// overrideGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
457//
458// fOverwriteButton = new Button(overrideGroup, SWT.CHECK);
459// fOverwriteButton.setText(Messages.TraceControl_ImportDialogOverwriteButtonText);
460
461 getShell().setMinimumSize(new Point(500, 50));
462 }
291cbdbf 463 }
This page took 0.054464 seconds and 5 git commands to generate.