lttng: Fix more Sonar warnings in LTTng Tracer Control
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng2.ui / src / org / eclipse / linuxtools / internal / lttng2 / ui / views / control / dialogs / ImportDialog.java
CommitLineData
291cbdbf 1/**********************************************************************
ba3a9bd2 2 * Copyright (c) 2012, 2013 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 10 * Bernd Hufmann - Initial API and implementation
ba3a9bd2 11 * Bernd Hufmann - Added handling of streamed traces
291cbdbf
BH
12 **********************************************************************/
13package org.eclipse.linuxtools.internal.lttng2.ui.views.control.dialogs;
14
15import java.util.ArrayList;
16import java.util.List;
17
18import org.eclipse.core.resources.IFolder;
19import org.eclipse.core.resources.IProject;
20import org.eclipse.core.resources.ResourcesPlugin;
21import org.eclipse.core.runtime.CoreException;
22import org.eclipse.core.runtime.NullProgressMonitor;
23import org.eclipse.jface.dialogs.Dialog;
24import org.eclipse.jface.dialogs.IDialogConstants;
25import org.eclipse.jface.dialogs.MessageDialog;
26import org.eclipse.jface.viewers.CheckStateChangedEvent;
27import org.eclipse.jface.viewers.CheckboxTreeViewer;
28import org.eclipse.jface.viewers.ICheckStateListener;
f3b33d40 29import org.eclipse.jface.viewers.StructuredSelection;
291cbdbf 30import org.eclipse.jface.window.Window;
f3b33d40 31import org.eclipse.jface.wizard.WizardDialog;
291cbdbf 32import org.eclipse.linuxtools.internal.lttng2.ui.Activator;
9315aeee 33import org.eclipse.linuxtools.internal.lttng2.ui.views.control.messages.Messages;
291cbdbf
BH
34import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.impl.TraceSessionComponent;
35import org.eclipse.linuxtools.internal.lttng2.ui.views.control.remote.IRemoteSystemProxy;
36import org.eclipse.linuxtools.tmf.core.TmfProjectNature;
37import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceFolder;
d04ec5a7 38import org.eclipse.linuxtools.tmf.ui.project.wizards.importtrace.ImportTraceWizard;
291cbdbf
BH
39import org.eclipse.rse.services.clientserver.messages.SystemMessageException;
40import org.eclipse.rse.subsystems.files.core.servicesubsystem.IFileServiceSubSystem;
41import org.eclipse.rse.subsystems.files.core.subsystems.IRemoteFile;
42import org.eclipse.swt.SWT;
43import org.eclipse.swt.custom.CCombo;
44import org.eclipse.swt.graphics.Point;
45import org.eclipse.swt.layout.GridData;
46import org.eclipse.swt.layout.GridLayout;
47import org.eclipse.swt.widgets.Button;
48import org.eclipse.swt.widgets.Composite;
49import org.eclipse.swt.widgets.Control;
50import org.eclipse.swt.widgets.Group;
51import org.eclipse.swt.widgets.Shell;
52import org.eclipse.swt.widgets.Text;
53import org.eclipse.swt.widgets.Tree;
f3b33d40 54import org.eclipse.ui.PlatformUI;
291cbdbf
BH
55import org.eclipse.ui.model.WorkbenchContentProvider;
56import org.eclipse.ui.model.WorkbenchLabelProvider;
57
58/**
291cbdbf
BH
59 * <p>
60 * Dialog box for collecting trace import information.
61 * </p>
cfdb727a 62 *
dbd4432d 63 * @author Bernd Hufmann
291cbdbf
BH
64 */
65public class ImportDialog extends Dialog implements IImportDialog {
66
67 // ------------------------------------------------------------------------
68 // Constants
69 // ------------------------------------------------------------------------
cfdb727a 70 /** The icon file for this dialog box. */
291cbdbf 71 public static final String IMPORT_ICON_FILE = "icons/elcl16/import_trace.gif"; //$NON-NLS-1$
cfdb727a
AM
72
73 /** Parent directory for UST traces */
291cbdbf
BH
74 public static final String UST_PARENT_DIRECTORY = "ust"; //$NON-NLS-1$
75
76 // ------------------------------------------------------------------------
77 // Attributes
78 // ------------------------------------------------------------------------
79 /**
80 * The dialog composite.
81 */
82 private Composite fDialogComposite = null;
83 /**
84 * The checkbox tree viewer for selecting available traces
85 */
86 private CheckboxTreeViewer fFolderViewer;
87 /**
88 * The combo box for selecting a project.
89 */
90 private CCombo fCombo;
91 /**
cfdb727a 92 * The overwrite button
291cbdbf
BH
93 */
94 private Button fOverwriteButton;
95 /**
96 * List of available LTTng 2.0 projects
97 */
98 private List<IProject> fProjects;
99 /**
100 * The parent where the new node should be added.
101 */
102 private TraceSessionComponent fSession = null;
103 /**
cfdb727a 104 * List of traces to import
291cbdbf 105 */
cfdb727a 106 private final List<ImportFileInfo> fTraces = new ArrayList<ImportFileInfo>();
291cbdbf 107 /**
cfdb727a 108 * Selection index in project combo box.
291cbdbf
BH
109 */
110 private int fProjectIndex;
111 /**
112 * Flag to indicate that something went wrong when creating the dialog box.
113 */
114 private boolean fIsError = false;
cfdb727a 115
291cbdbf
BH
116 // ------------------------------------------------------------------------
117 // Constructors
118 // ------------------------------------------------------------------------
119 /**
120 * Constructor
121 * @param shell - a shell for the display of the dialog
122 */
123 public ImportDialog(Shell shell) {
124 super(shell);
125 setShellStyle(SWT.RESIZE);
126 }
127
128 // ------------------------------------------------------------------------
129 // Accessors
130 // ------------------------------------------------------------------------
11252342 131
291cbdbf
BH
132 @Override
133 public List<ImportFileInfo> getTracePathes() {
134 List<ImportFileInfo> retList = new ArrayList<ImportFileInfo>();
135 retList.addAll(fTraces);
136 return retList;
137 }
138
291cbdbf
BH
139 @Override
140 public IProject getProject() {
141 return fProjects.get(fProjectIndex);
142 }
cfdb727a 143
291cbdbf
BH
144 @Override
145 public void setSession(TraceSessionComponent session) {
146 fSession = session;
147 }
148
149 // ------------------------------------------------------------------------
150 // Operations
151 // ------------------------------------------------------------------------
11252342 152
291cbdbf
BH
153 @Override
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));
158 }
159
291cbdbf
BH
160 @Override
161 protected Control createDialogArea(Composite parent) {
cfdb727a 162
291cbdbf
BH
163 // Main dialog panel
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));
168
291cbdbf 169 try {
f3b33d40
BH
170 if (fSession.isStreamedTrace()) {
171 createLocalComposite();
172 } else {
173 createRemoteComposite();
291cbdbf 174 }
f3b33d40
BH
175 } catch (CoreException e) {
176 createErrorComposite(parent, e.fillInStackTrace());
177 return fDialogComposite;
291cbdbf
BH
178 } catch (SystemMessageException e) {
179 createErrorComposite(parent, e.fillInStackTrace());
180 return fDialogComposite;
181 }
291cbdbf
BH
182 return fDialogComposite;
183 }
184
291cbdbf
BH
185 @Override
186 protected void createButtonsForButtonBar(Composite parent) {
79c3db85 187 createButton(parent, IDialogConstants.CANCEL_ID, "&Cancel", true); //$NON-NLS-1$
046b6849 188 Button importLocallyButton = createButton(parent, IDialogConstants.OK_ID, "&Ok", true); //$NON-NLS-1$
f3b33d40 189 if (fSession.isStreamedTrace()) {
046b6849 190 importLocallyButton.setText("&Next..."); //$NON-NLS-1$
f3b33d40 191 }
291cbdbf
BH
192 }
193
291cbdbf
BH
194 @Override
195 protected void okPressed() {
196 if (!fIsError) {
f3b33d40 197
291cbdbf
BH
198 // Validate input data
199 fTraces.clear();
200
201 fProjectIndex = fCombo.getSelectionIndex();
202
203 if (fProjectIndex < 0) {
204 MessageDialog.openError(getShell(),
205 Messages.TraceControl_ImportDialogTitle,
206 Messages.TraceControl_ImportDialogNoProjectSelectedError);
207 return;
208 }
209
f3b33d40
BH
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) {
218 super.okPressed();
219 }
220 super.cancelPressed();
221 return;
222 }
223
291cbdbf
BH
224 IProject project = fProjects.get(fProjectIndex);
225 IFolder traceFolder = project.getFolder(TmfTraceFolder.TRACE_FOLDER_NAME);
226
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$
232 return;
233 }
234
235 boolean overwriteAll = fOverwriteButton.getSelection();
236
237 Object[] checked = fFolderViewer.getCheckedElements();
238 for (int i = 0; i < checked.length; i++) {
239 IRemoteFile file = (IRemoteFile) checked[i];
cfdb727a
AM
240
241 // Only add actual trace directories
291cbdbf 242 if (file.isDirectory() && !UST_PARENT_DIRECTORY.equals(file.getName())) {
cfdb727a 243
291cbdbf
BH
244 ImportFileInfo info = new ImportFileInfo(file, file.getName(), overwriteAll);
245 String traceName = info.getLocalTraceName();
246 IFolder folder = traceFolder.getFolder(traceName);
247
cfdb727a 248 // Verify if trace directory already exists (and not overwrite)
291cbdbf
BH
249 if (folder.exists() && !overwriteAll) {
250
251 // Ask user for overwrite or new name
252 IImportConfirmationDialog conf = TraceControlDialogFactory.getInstance().getImportConfirmationDialog();
253 conf.setTraceName(traceName);
254
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());
260 }
261 fTraces.add(info);
262 }
cfdb727a 263 } else {
291cbdbf
BH
264 fTraces.add(info);
265 }
266 }
267 }
268
269 if (fTraces.isEmpty()) {
270 MessageDialog.openError(getShell(),
271 Messages.TraceControl_ImportDialogTitle,
272 Messages.TraceControl_ImportDialogNoTraceSelectedError);
273 return;
274 }
275 }
276
277 // validation successful -> call super.okPressed()
278 super.okPressed();
279 }
280
281 // ------------------------------------------------------------------------
282 // Helper methods and classes
283 // ------------------------------------------------------------------------
11252342 284
cfdb727a
AM
285 /**
286 * Helper class for the contents of a folder in a tracing project
287 *
288 * @author Bernd Hufmann
289 */
f455db37 290 public static class FolderContentProvider extends WorkbenchContentProvider {
291cbdbf
BH
291 @Override
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];
298 }
299 }
300 return super.getChildren(o);
301 }
302 }
cfdb727a 303
291cbdbf
BH
304 /**
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
cfdb727a 308 * @param e - a error causing exception
291cbdbf
BH
309 */
310 private void createErrorComposite(Composite parent, Throwable e) {
311 fIsError = true;
312 fDialogComposite.dispose();
cfdb727a 313
291cbdbf
BH
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));
cfdb727a 318
291cbdbf
BH
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));
327 }
328
f3b33d40
BH
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));
335
336 IRemoteSystemProxy proxy = fSession.getTargetNode().getRemoteSystemProxy();
337
338 IFileServiceSubSystem fsss = proxy.getFileServiceSubSystem();
339
340 IRemoteFile remoteFolder = fsss.getRemoteFileObject(fSession.getSessionPath(), new NullProgressMonitor());
341
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);
348
349 fFolderViewer.setContentProvider(new FolderContentProvider());
350 fFolderViewer.setLabelProvider(new WorkbenchLabelProvider());
351
352 fFolderViewer.addCheckStateListener(new ICheckStateListener() {
353 @Override
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);
362 return;
363 }
364 fFolderViewer.setSubtreeChecked(event.getElement(), event.getChecked());
365 if (!event.getChecked()) {
366 fFolderViewer.setChecked(element.getParentRemoteFile(), false);
367 }
368 }
369 }
370 });
371 fFolderViewer.setInput(remoteFolder);
372
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));
378
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());
385 }
386 }
387
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()]));
392
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));
397
398 fOverwriteButton = new Button(overrideGroup, SWT.CHECK);
399 fOverwriteButton.setText(Messages.TraceControl_ImportDialogOverwriteButtonText);
400 getShell().setMinimumSize(new Point(500, 400));
401 }
402
403 private void createLocalComposite() throws CoreException {
404
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));
410
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());
417 }
418 }
419
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()]));
cfdb727a 424
f3b33d40
BH
425 getShell().setMinimumSize(new Point(500, 50));
426 }
291cbdbf 427 }
This page took 0.065062 seconds and 5 git commands to generate.