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