tmf.ui: Introduce TmfFileDialogFactory
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / internal / tmf / ui / project / wizards / importtrace / ImportTraceWizardPage.java
CommitLineData
002f9f07 1/*******************************************************************************
45fc1883 2 * Copyright (c) 2009, 2016 Ericsson and others.
002f9f07
BH
3 *
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
8 *
9 * Contributors:
10 * Francois Chouinard - Initial API and implementation
11 * Francois Chouinard - Got rid of dependency on internal platform class
12 * Francois Chouinard - Complete re-design
13 * Anna Dushistova(Montavista) - [383047] NPE while importing a CFT trace
14 * Matthew Khouzam - Moved out some common functions
15 * Patrick Tasse - Add sorting of file system elements
252c602c 16 * Bernd Hufmann - Re-design of trace selection and trace validation
0e1d517e 17 * Marc-Andre Laperle - Preserve folder structure on import
480e9739 18 * Marc-Andre Laperle - Extract archives during import
7c62be2f 19 * Marc-Andre Laperle - Add support for Gzip (non-Tar)
002f9f07
BH
20 *******************************************************************************/
21
2bdf0193 22package org.eclipse.tracecompass.internal.tmf.ui.project.wizards.importtrace;
002f9f07
BH
23
24import java.io.File;
002f9f07 25import java.lang.reflect.InvocationTargetException;
45fc1883 26import java.util.ArrayList;
29ceb238 27import java.util.Collection;
480e9739 28import java.util.LinkedList;
002f9f07 29import java.util.List;
002f9f07
BH
30
31import org.eclipse.core.resources.IContainer;
32import org.eclipse.core.resources.IFolder;
33import org.eclipse.core.resources.IProject;
45fc1883
BH
34import org.eclipse.core.resources.IResource;
35import org.eclipse.core.resources.IWorkspace;
002f9f07
BH
36import org.eclipse.core.resources.ResourcesPlugin;
37import org.eclipse.core.runtime.CoreException;
38import org.eclipse.core.runtime.IPath;
252c602c 39import org.eclipse.core.runtime.IProgressMonitor;
002f9f07 40import org.eclipse.core.runtime.IStatus;
252c602c 41import org.eclipse.core.runtime.NullProgressMonitor;
002f9f07
BH
42import org.eclipse.core.runtime.Path;
43import org.eclipse.core.runtime.Platform;
252c602c
BH
44import org.eclipse.core.runtime.Status;
45import org.eclipse.core.runtime.SubMonitor;
252c602c 46import org.eclipse.jface.dialogs.IDialogSettings;
c8cf44dc 47import org.eclipse.jface.layout.PixelConverter;
252c602c 48import org.eclipse.jface.operation.IRunnableWithProgress;
282218e9
MAL
49import org.eclipse.jface.viewers.CheckStateChangedEvent;
50import org.eclipse.jface.viewers.ICheckStateListener;
002f9f07
BH
51import org.eclipse.jface.viewers.IStructuredSelection;
52import org.eclipse.jface.viewers.ITreeContentProvider;
45fc1883 53import org.eclipse.osgi.util.NLS;
002f9f07
BH
54import org.eclipse.swt.SWT;
55import org.eclipse.swt.custom.BusyIndicator;
c8cf44dc 56import org.eclipse.swt.events.FocusAdapter;
002f9f07 57import org.eclipse.swt.events.FocusEvent;
2f7b3dd7
BH
58import org.eclipse.swt.events.ModifyEvent;
59import org.eclipse.swt.events.ModifyListener;
002f9f07
BH
60import org.eclipse.swt.events.SelectionAdapter;
61import org.eclipse.swt.events.SelectionEvent;
c8cf44dc
MAL
62import org.eclipse.swt.events.TraverseEvent;
63import org.eclipse.swt.events.TraverseListener;
002f9f07
BH
64import org.eclipse.swt.layout.GridData;
65import org.eclipse.swt.layout.GridLayout;
66import org.eclipse.swt.widgets.Button;
67import org.eclipse.swt.widgets.Combo;
68import org.eclipse.swt.widgets.Composite;
69import org.eclipse.swt.widgets.DirectoryDialog;
70import org.eclipse.swt.widgets.Event;
c8cf44dc 71import org.eclipse.swt.widgets.FileDialog;
002f9f07
BH
72import org.eclipse.swt.widgets.Group;
73import org.eclipse.swt.widgets.Label;
45fc1883 74import org.eclipse.swt.widgets.Text;
2bdf0193 75import org.eclipse.tracecompass.internal.tmf.ui.Activator;
45fc1883
BH
76import org.eclipse.tracecompass.internal.tmf.ui.project.operations.NewExperimentOperation;
77import org.eclipse.tracecompass.internal.tmf.ui.project.operations.SelectTracesOperation;
2bdf0193
AM
78import org.eclipse.tracecompass.tmf.core.TmfCommonConstants;
79import org.eclipse.tracecompass.tmf.core.TmfProjectNature;
2bdf0193 80import org.eclipse.tracecompass.tmf.core.project.model.TmfTraceType;
480e9739 81import org.eclipse.tracecompass.tmf.core.util.Pair;
674c702f 82import org.eclipse.tracecompass.tmf.ui.dialog.TmfFileDialogFactory;
45fc1883
BH
83import org.eclipse.tracecompass.tmf.ui.project.model.TmfExperimentElement;
84import org.eclipse.tracecompass.tmf.ui.project.model.TmfExperimentFolder;
2bdf0193
AM
85import org.eclipse.tracecompass.tmf.ui.project.model.TmfProjectElement;
86import org.eclipse.tracecompass.tmf.ui.project.model.TmfProjectRegistry;
2bdf0193 87import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceFolder;
2bdf0193 88import org.eclipse.tracecompass.tmf.ui.project.model.TmfTracesFolder;
002f9f07
BH
89import org.eclipse.ui.dialogs.FileSystemElement;
90import org.eclipse.ui.dialogs.WizardResourceImportPage;
f17bb886 91import org.eclipse.ui.ide.dialogs.IElementFilter;
73487f4b 92import org.eclipse.ui.ide.dialogs.ResourceTreeAndListGroup;
282218e9 93import org.eclipse.ui.internal.ide.DialogUtil;
002f9f07 94import org.eclipse.ui.model.WorkbenchContentProvider;
282218e9
MAL
95import org.eclipse.ui.model.WorkbenchLabelProvider;
96import org.eclipse.ui.model.WorkbenchViewerComparator;
002f9f07
BH
97
98/**
ba560b10
MK
99 * A variant of the standard resource import wizard for importing traces to
100 * given tracing project. If no project or tracing project was selected the
101 * wizard imports it to the default tracing project which is created if
102 * necessary.
252c602c
BH
103 *
104 * In our case traces could be files or a directory structure. This wizard
ba560b10
MK
105 * supports both cases. It imports traces for a selected trace type or, if no
106 * trace type is selected, it tries to detect the trace type automatically.
107 * However, the automatic detection is a best-effort and cannot guarantee that
108 * the detection is successful. The reason for this is that there might be
109 * multiple trace types that can be assigned to a single trace.
252c602c 110 *
002f9f07 111 *
002f9f07 112 * @author Francois Chouinard
002f9f07 113 */
29ceb238 114@SuppressWarnings("restriction")
002f9f07
BH
115public class ImportTraceWizardPage extends WizardResourceImportPage {
116
002f9f07
BH
117 // ------------------------------------------------------------------------
118 // Constants
119 // ------------------------------------------------------------------------
d0e34947 120 private static final String IMPORT_WIZARD_PAGE_NAME = "ImportTraceWizardPage"; //$NON-NLS-1$
45fc1883 121 private static final String IMPORT_WIZARD_ROOT_DIRECTORY_ID = ".import_root_directory_id"; //$NON-NLS-1$ ;
d0e34947
MAL
122 private static final String IMPORT_WIZARD_ARCHIVE_FILE_NAME_ID = ".import_archive_file_name_id"; //$NON-NLS-1$
123 private static final String IMPORT_WIZARD_IMPORT_UNRECOGNIZED_ID = ".import_unrecognized_traces_id"; //$NON-NLS-1$
124 private static final String IMPORT_WIZARD_PRESERVE_FOLDERS_ID = ".import_preserve_folders_id"; //$NON-NLS-1$
125 private static final String IMPORT_WIZARD_IMPORT_FROM_DIRECTORY_ID = ".import_from_directory"; //$NON-NLS-1$
45fc1883 126 private static final String IMPORT_WIZARD_CREATE_EXPERIMENT_ID = ".create_experiment"; //$NON-NLS-1$
002f9f07 127
c8cf44dc 128 // constant from WizardArchiveFileResourceImportPage1
7c62be2f 129 private static final String[] FILE_IMPORT_MASK = { "*.jar;*.zip;*.tar;*.tar.gz;*.tgz;*.gz", "*.*" }; //$NON-NLS-1$ //$NON-NLS-2$
c8cf44dc 130
d0e34947
MAL
131 /**
132 * A special trace type value to communicate that automatic trace type
133 * detection will occur instead of setting a specific trace type when
134 * importing the traces.
135 */
9b3f4afe 136 public static final String TRACE_TYPE_AUTO_DETECT = Messages.ImportTraceWizard_AutoDetection;
d0e34947
MAL
137
138 /**
139 * Preserve the folder structure of the import traces.
140 */
9b3f4afe 141 public static final int OPTION_PRESERVE_FOLDER_STRUCTURE = 1 << 1;
d0e34947
MAL
142 /**
143 * Create links to the trace files instead of copies.
144 */
9b3f4afe 145 public static final int OPTION_CREATE_LINKS_IN_WORKSPACE = 1 << 2;
d0e34947
MAL
146 /**
147 * Import files that were not recognized as the selected trace type.
148 */
9b3f4afe 149 public static final int OPTION_IMPORT_UNRECOGNIZED_TRACES = 1 << 3;
d0e34947
MAL
150 /**
151 * Overwrite existing resources without prompting.
152 */
9b3f4afe 153 public static final int OPTION_OVERWRITE_EXISTING_RESOURCES = 1 << 4;
45fc1883
BH
154 /**
155 * Create an experiment with imported traces.
156 */
157 public static final int OPTION_CREATE_EXPERIMENT = 1 << 5;
d0e34947 158
002f9f07
BH
159 // ------------------------------------------------------------------------
160 // Attributes
161 // ------------------------------------------------------------------------
162
002f9f07
BH
163 // Target import directory ('Traces' folder)
164 private IFolder fTargetFolder;
16f91bd5
BH
165 // Target Trace folder element
166 private TmfTraceFolder fTraceFolderElement;
45fc1883
BH
167 // The workspace experiment folder
168 private TmfExperimentFolder fExperimentFolderElement;
169 private String fPreviousSource;
252c602c
BH
170 // Flag to handle destination folder change event
171 private Boolean fIsDestinationChanged = false;
28abf18d 172 private final Object fSyncObject = new Object();
252c602c
BH
173 // Combo box containing trace types
174 private Combo fTraceTypes;
175 // Button to ignore unrecognized traces or not
176 private Button fImportUnrecognizedButton;
177 // Button to overwrite existing resources or not
178 private Button fOverwriteExistingResourcesCheckbox;
179 // Button to link or copy traces to workspace
180 private Button fCreateLinksInWorkspaceButton;
0e1d517e
MAL
181 // Button to preserve folder structure
182 private Button fPreserveFolderStructureButton;
45fc1883
BH
183 // Button to create an experiment
184 private Button fCreateExperimentCheckbox;
185 // Text box for experiment name
186 private Text fExperimentNameText;
252c602c 187 private boolean entryChanged = false;
c8cf44dc
MAL
188 // The import from directory radio button
189 private Button fImportFromDirectoryRadio;
190 // The import from archive radio button
191 private Button fImportFromArchiveRadio;
192 // Flag to remember the "create links" checkbox when it gets disabled by
193 // the import from archive radio button
194 private Boolean fPreviousCreateLinksValue = true;
d0e34947 195
c8cf44dc 196 /** The archive name field */
d0e34947 197 protected Combo fArchiveNameField;
c8cf44dc
MAL
198 /** The archive browse button. */
199 protected Button fArchiveBrowseButton;
252c602c
BH
200 /** The directory name field */
201 protected Combo directoryNameField;
202 /** The directory browse button. */
203 protected Button directoryBrowseButton;
002f9f07 204
282218e9
MAL
205 private ResourceTreeAndListGroup fSelectionGroup;
206
c8cf44dc
MAL
207 // Keep trace of the selection root so that we can dispose its related
208 // resources
209 private TraceFileSystemElement fSelectionGroupRoot;
210
002f9f07
BH
211 // ------------------------------------------------------------------------
212 // Constructors
213 // ------------------------------------------------------------------------
214
215 /**
216 * Constructor. Creates the trace wizard page.
217 *
218 * @param name
219 * The name of the page.
220 * @param selection
221 * The current selection
222 */
223 protected ImportTraceWizardPage(String name, IStructuredSelection selection) {
224 super(name, selection);
002f9f07
BH
225 setTitle(Messages.ImportTraceWizard_FileSystemTitle);
226 setDescription(Messages.ImportTraceWizard_ImportTrace);
227
228 // Locate the target trace folder
229 IFolder traceFolder = null;
230 Object element = selection.getFirstElement();
231
232 if (element instanceof TmfTraceFolder) {
16f91bd5 233 fTraceFolderElement = (TmfTraceFolder) element;
16f91bd5 234 traceFolder = fTraceFolderElement.getResource();
002f9f07
BH
235 } else if (element instanceof IProject) {
236 IProject project = (IProject) element;
237 try {
238 if (project.hasNature(TmfProjectNature.ID)) {
6ca50a1d
BH
239 TmfProjectElement projectElement = TmfProjectRegistry.getProject(project, true);
240 fTraceFolderElement = projectElement.getTracesFolder();
339d539c 241 traceFolder = project.getFolder(TmfTracesFolder.TRACES_FOLDER_NAME);
002f9f07
BH
242 }
243 } catch (CoreException e) {
244 }
245 }
246
ba560b10
MK
247 // If no tracing project was selected or trace folder doesn't exist use
248 // default tracing project
252c602c
BH
249 if (traceFolder == null) {
250 IProject project = TmfProjectRegistry.createProject(
251 TmfCommonConstants.DEFAULT_TRACE_PROJECT_NAME, null, new NullProgressMonitor());
6ca50a1d
BH
252 TmfProjectElement projectElement = TmfProjectRegistry.getProject(project, true);
253 fTraceFolderElement = projectElement.getTracesFolder();
339d539c 254 traceFolder = project.getFolder(TmfTracesFolder.TRACES_FOLDER_NAME);
252c602c
BH
255 }
256
002f9f07
BH
257 // Set the target trace folder
258 if (traceFolder != null) {
259 fTargetFolder = traceFolder;
1d6c9022 260 String path = traceFolder.getFullPath().toString();
002f9f07
BH
261 setContainerFieldValue(path);
262 }
45fc1883
BH
263
264 TmfProjectElement project = fTraceFolderElement.getProject();
265 fExperimentFolderElement = project.getExperimentsFolder();
002f9f07
BH
266 }
267
d0e34947
MAL
268 /**
269 * Constructor
270 *
271 * @param selection
272 * The current selection
273 */
274 public ImportTraceWizardPage(IStructuredSelection selection) {
275 this(IMPORT_WIZARD_PAGE_NAME, selection);
276 }
277
278 /**
279 * Create the import source selection widget. (Copied from
280 * WizardResourceImportPage but instead always uses the internal
281 * ResourceTreeAndListGroup to keep compatibility with Kepler)
282 */
283 @Override
284 protected void createFileSelectionGroup(Composite parent) {
285
286 // Just create with a dummy root.
287 fSelectionGroup = new ResourceTreeAndListGroup(parent,
45fc1883 288 new FileSystemElement("Dummy", null, true), //$NON-NLS-1$
d0e34947
MAL
289 getFolderProvider(), new WorkbenchLabelProvider(),
290 getFileProvider(), new WorkbenchLabelProvider(), SWT.NONE,
291 DialogUtil.inRegularFontMode(parent));
292
293 ICheckStateListener listener = new ICheckStateListener() {
294 @Override
295 public void checkStateChanged(CheckStateChangedEvent event) {
296 updateWidgetEnablements();
297 }
298 };
299
300 WorkbenchViewerComparator comparator = new WorkbenchViewerComparator();
301 fSelectionGroup.setTreeComparator(comparator);
302 fSelectionGroup.setListComparator(comparator);
303 fSelectionGroup.addCheckStateListener(listener);
304
305 }
306
002f9f07
BH
307 // ------------------------------------------------------------------------
308 // WizardResourceImportPage
309 // ------------------------------------------------------------------------
310
002f9f07
BH
311 @Override
312 protected void createSourceGroup(Composite parent) {
c8cf44dc 313 createSourceSelectionGroup(parent);
002f9f07
BH
314 createFileSelectionGroup(parent);
315 createTraceTypeGroup(parent);
316 validateSourceGroup();
317 }
318
319 @Override
252c602c
BH
320 protected ITreeContentProvider getFileProvider() {
321 return new WorkbenchContentProvider() {
002f9f07 322 @Override
252c602c
BH
323 public Object[] getChildren(Object object) {
324 if (object instanceof TraceFileSystemElement) {
325 TraceFileSystemElement element = (TraceFileSystemElement) object;
326 return element.getFiles().getChildren(element);
002f9f07 327 }
252c602c 328 return new Object[0];
002f9f07 329 }
252c602c 330 };
002f9f07
BH
331 }
332
333 @Override
334 protected ITreeContentProvider getFolderProvider() {
002f9f07
BH
335 return new WorkbenchContentProvider() {
336 @Override
337 public Object[] getChildren(Object o) {
252c602c
BH
338 if (o instanceof TraceFileSystemElement) {
339 TraceFileSystemElement element = (TraceFileSystemElement) o;
340 return element.getFolders().getChildren();
002f9f07
BH
341 }
342 return new Object[0];
343 }
002f9f07 344
252c602c
BH
345 @Override
346 public boolean hasChildren(Object o) {
347 if (o instanceof TraceFileSystemElement) {
348 TraceFileSystemElement element = (TraceFileSystemElement) o;
349 if (element.isPopulated()) {
350 return getChildren(element).length > 0;
351 }
ba560b10 352 // If we have not populated then wait until asked
252c602c 353 return true;
002f9f07 354 }
252c602c 355 return false;
002f9f07 356 }
252c602c 357 };
002f9f07
BH
358 }
359
360 // ------------------------------------------------------------------------
361 // Directory Selection Group (forked WizardFileSystemResourceImportPage1)
362 // ------------------------------------------------------------------------
363
002f9f07 364 /**
c8cf44dc 365 * creates the source selection group.
002f9f07
BH
366 *
367 * @param parent
368 * the parent composite
369 */
d0e34947 370 protected void createSourceSelectionGroup(Composite parent) {
002f9f07 371
d0e34947 372 Composite sourceGroup = new Composite(parent, SWT.NONE);
002f9f07
BH
373 GridLayout layout = new GridLayout();
374 layout.numColumns = 3;
c8cf44dc
MAL
375 layout.makeColumnsEqualWidth = false;
376 layout.marginWidth = 0;
377 sourceGroup.setLayout(layout);
378 sourceGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
379
380 // import from directory radio button
381 fImportFromDirectoryRadio = new Button(sourceGroup, SWT.RADIO);
45fc1883 382 fImportFromDirectoryRadio.setText(Messages.ImportTraceWizard_DirectoryLocation);
c8cf44dc
MAL
383
384 // import location entry combo
d0e34947
MAL
385 directoryNameField = createPathSelectionCombo(sourceGroup);
386 createDirectoryBrowseButton(sourceGroup);
c8cf44dc
MAL
387
388 // import from archive radio button
389 fImportFromArchiveRadio = new Button(sourceGroup, SWT.RADIO);
45fc1883 390 fImportFromArchiveRadio.setText(Messages.ImportTraceWizard_ArchiveLocation);
c8cf44dc
MAL
391
392 // import location entry combo
d0e34947
MAL
393 fArchiveNameField = createPathSelectionCombo(sourceGroup);
394 createArchiveBrowseButton(sourceGroup);
c8cf44dc
MAL
395
396 fImportFromDirectoryRadio.setSelection(true);
397 fArchiveNameField.setEnabled(false);
398 fArchiveBrowseButton.setEnabled(false);
399
d0e34947 400 fImportFromDirectoryRadio.addSelectionListener(new SelectionAdapter() {
002f9f07
BH
401 @Override
402 public void widgetSelected(SelectionEvent e) {
d0e34947 403 directoryRadioSelected();
002f9f07 404 }
c8cf44dc 405 });
002f9f07 406
d0e34947 407 fImportFromArchiveRadio.addSelectionListener(new SelectionAdapter() {
002f9f07 408 @Override
c8cf44dc 409 public void widgetSelected(SelectionEvent e) {
d0e34947 410 archiveRadioSelected();
002f9f07
BH
411 }
412 });
d0e34947
MAL
413 }
414
415 /**
416 * Select or deselect all files in the file selection group
417 *
418 * @param checked
419 * whether or not the files should be checked
420 */
421 protected void setFileSelectionGroupChecked(boolean checked) {
422 if (fSelectionGroup != null) {
423 fSelectionGroup.setAllSelections(checked);
424 }
425 }
426
427 /**
428 * Create a combo that will be used to select a path to specify the source
429 * of the import. The parent is assumed to have a GridLayout.
430 *
431 * @param parent
432 * the parent composite
433 * @return the created path selection combo
434 */
435 protected Combo createPathSelectionCombo(Composite parent) {
436 Combo pathSelectionCombo = new Combo(parent, SWT.BORDER);
437
438 GridData layoutData = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL);
439 layoutData.widthHint = new PixelConverter(pathSelectionCombo).convertWidthInCharsToPixels(25);
440 pathSelectionCombo.setLayoutData(layoutData);
002f9f07 441
c8cf44dc 442 TraverseListener traverseListener = new TraverseListener() {
002f9f07 443 @Override
c8cf44dc
MAL
444 public void keyTraversed(TraverseEvent e) {
445 if (e.detail == SWT.TRAVERSE_RETURN) {
446 e.doit = false;
447 entryChanged = false;
448 updateFromSourceField();
449 }
002f9f07 450 }
c8cf44dc
MAL
451 };
452
453 FocusAdapter focusAdapter = new FocusAdapter() {
002f9f07
BH
454 @Override
455 public void focusLost(FocusEvent e) {
456 // Clear the flag to prevent constant update
457 if (entryChanged) {
458 entryChanged = false;
459 updateFromSourceField();
460 }
461 }
c8cf44dc
MAL
462 };
463
464 SelectionAdapter selectionAdapter = new SelectionAdapter() {
465 @Override
466 public void widgetSelected(SelectionEvent e) {
467 entryChanged = false;
468 updateFromSourceField();
469 }
470 };
002f9f07 471
c8cf44dc 472 ModifyListener modifyListner = new ModifyListener() {
2f7b3dd7
BH
473 @Override
474 public void modifyText(ModifyEvent e) {
475 entryChanged = true;
476 }
c8cf44dc
MAL
477 };
478
d0e34947
MAL
479 pathSelectionCombo.addModifyListener(modifyListner);
480 pathSelectionCombo.addTraverseListener(traverseListener);
481 pathSelectionCombo.addFocusListener(focusAdapter);
482 pathSelectionCombo.addSelectionListener(selectionAdapter);
c8cf44dc 483
d0e34947
MAL
484 return pathSelectionCombo;
485 }
486
487 /**
488 * Create the directory browse button.
489 *
490 * @param parent
491 * the parent composite
492 */
493 protected void createDirectoryBrowseButton(Composite parent) {
494 directoryBrowseButton = createPathSelectionBrowseButton(parent);
495 directoryBrowseButton.addSelectionListener(new SelectionAdapter() {
c8cf44dc
MAL
496 @Override
497 public void widgetSelected(SelectionEvent e) {
d0e34947 498 handleSourceDirectoryBrowseButtonPressed();
c8cf44dc 499 }
2f7b3dd7 500 });
d0e34947 501 }
2f7b3dd7 502
d0e34947
MAL
503 /**
504 * Create the archive browse button.
505 *
506 * @param parent
507 * the parent composite
508 */
509 protected void createArchiveBrowseButton(Composite parent) {
510 fArchiveBrowseButton = createPathSelectionBrowseButton(parent);
511 fArchiveBrowseButton.addSelectionListener(new SelectionAdapter() {
c8cf44dc
MAL
512 @Override
513 public void widgetSelected(SelectionEvent e) {
d0e34947 514 handleArchiveBrowseButtonPressed(FILE_IMPORT_MASK);
c8cf44dc
MAL
515 }
516 });
517 }
518
d0e34947
MAL
519 /**
520 * Create a browse button that will be used to browse for a path to specify
521 * the source of the import. The parent is assumed to have a GridLayout.
522 *
523 * @param parent
524 * the parent composite
525 * @return the created path selection combo
526 */
527 protected Button createPathSelectionBrowseButton(Composite parent) {
528 Button pathSelectionBrowseButton = new Button(parent, SWT.PUSH);
529 pathSelectionBrowseButton.setText(Messages.ImportTraceWizard_BrowseButton);
530 setButtonLayoutData(pathSelectionBrowseButton);
531
532 return pathSelectionBrowseButton;
533 }
534
c8cf44dc 535 private void archiveRadioSelected() {
d0e34947 536 if (!isImportFromDirectory()) {
c8cf44dc
MAL
537 directoryNameField.setEnabled(false);
538 directoryBrowseButton.setEnabled(false);
539 fArchiveNameField.setEnabled(true);
540 fArchiveBrowseButton.setEnabled(true);
541 updateFromSourceField();
542 fArchiveNameField.setFocus();
d0e34947
MAL
543 if (fCreateLinksInWorkspaceButton != null) {
544 fPreviousCreateLinksValue = fCreateLinksInWorkspaceButton.getSelection();
545 fCreateLinksInWorkspaceButton.setSelection(false);
546 fCreateLinksInWorkspaceButton.setEnabled(false);
547 }
c8cf44dc
MAL
548 }
549 }
550
551 private void directoryRadioSelected() {
d0e34947 552 if (isImportFromDirectory()) {
c8cf44dc
MAL
553 directoryNameField.setEnabled(true);
554 directoryBrowseButton.setEnabled(true);
555 fArchiveNameField.setEnabled(false);
556 fArchiveBrowseButton.setEnabled(false);
557 updateFromSourceField();
558 directoryNameField.setFocus();
d0e34947
MAL
559 if (fCreateLinksInWorkspaceButton != null) {
560 fCreateLinksInWorkspaceButton.setSelection(fPreviousCreateLinksValue);
561 fCreateLinksInWorkspaceButton.setEnabled(true);
562 }
c8cf44dc 563 }
002f9f07
BH
564 }
565
566 // ------------------------------------------------------------------------
567 // Browse for the source directory
568 // ------------------------------------------------------------------------
569
570 @Override
571 public void handleEvent(Event event) {
572 if (event.widget == directoryBrowseButton) {
573 handleSourceDirectoryBrowseButtonPressed();
574 }
252c602c
BH
575
576 // Avoid overwriting destination path without repeatedly trigger
577 // call of handleEvent();
28abf18d 578 synchronized (fSyncObject) {
252c602c
BH
579 if (fIsDestinationChanged == false) {
580 event.display.asyncExec(new Runnable() {
581 @Override
582 public void run() {
28abf18d 583 synchronized (fSyncObject) {
252c602c 584 fIsDestinationChanged = true;
1d6c9022 585 String path = fTargetFolder.getFullPath().toString();
252c602c
BH
586 setContainerFieldValue(path);
587 }
588 }
589 });
590 } else {
591 fIsDestinationChanged = false;
592 }
593 }
002f9f07
BH
594 super.handleEvent(event);
595 }
596
252c602c
BH
597 @Override
598 protected void handleContainerBrowseButtonPressed() {
599 // Do nothing so that destination directory cannot be changed.
600 }
601
002f9f07
BH
602 /**
603 * Handle the button pressed event
604 */
605 protected void handleSourceDirectoryBrowseButtonPressed() {
606 String currentSource = directoryNameField.getText();
607 DirectoryDialog dialog = new DirectoryDialog(directoryNameField.getShell(), SWT.SAVE | SWT.SHEET);
608 dialog.setText(Messages.ImportTraceWizard_SelectTraceDirectoryTitle);
609 dialog.setMessage(Messages.ImportTraceWizard_SelectTraceDirectoryMessage);
610 dialog.setFilterPath(getSourceDirectoryName(currentSource));
611
612 String selectedDirectory = dialog.open();
613 if (selectedDirectory != null) {
614 // Just quit if the directory is not valid
615 if ((getSourceDirectory(selectedDirectory) == null) || selectedDirectory.equals(currentSource)) {
616 return;
617 }
618 // If it is valid then proceed to populate
619 setErrorMessage(null);
d0e34947 620 setSourcePath(selectedDirectory);
002f9f07
BH
621 }
622 }
623
c8cf44dc
MAL
624 /**
625 * Handle the button pressed event
d0e34947
MAL
626 *
627 * @param extensions
628 * file extensions used to filter files shown to the user
c8cf44dc 629 */
d0e34947 630 protected void handleArchiveBrowseButtonPressed(String[] extensions) {
674c702f 631 FileDialog dialog = TmfFileDialogFactory.create(fArchiveNameField.getShell(), SWT.SHEET);
d0e34947 632 dialog.setFilterExtensions(extensions);
c8cf44dc
MAL
633 dialog.setText(Messages.ImportTraceWizard_SelectTraceArchiveTitle);
634 String fileName = fArchiveNameField.getText().trim();
635 if (!fileName.isEmpty()) {
636 File path = new File(fileName).getParentFile();
637 if (path != null && path.exists()) {
638 dialog.setFilterPath(path.toString());
639 }
640 }
641
642 String selectedArchive = dialog.open();
643 if (selectedArchive != null) {
644 setErrorMessage(null);
d0e34947 645 setSourcePath(selectedArchive);
c8cf44dc
MAL
646 updateWidgetEnablements();
647 }
648 }
649
002f9f07 650 private File getSourceDirectory() {
d0e34947
MAL
651 if (directoryNameField == null) {
652 return null;
653 }
002f9f07
BH
654 return getSourceDirectory(directoryNameField.getText());
655 }
656
c8cf44dc 657 private File getSourceArchiveFile() {
d0e34947
MAL
658 if (fArchiveNameField == null) {
659 return null;
660 }
661
c8cf44dc
MAL
662 return getSourceArchiveFile(fArchiveNameField.getText());
663 }
664
665 private String getSourceContainerPath() {
d0e34947 666 if (isImportFromDirectory()) {
c8cf44dc
MAL
667 File sourceDirectory = getSourceDirectory();
668 if (sourceDirectory != null) {
669 return sourceDirectory.getAbsolutePath();
670 }
671 }
672 File sourceArchiveFile = getSourceArchiveFile();
673 if (sourceArchiveFile != null) {
674 return sourceArchiveFile.getParent();
675 }
676 return null;
677 }
678
002f9f07
BH
679 private static File getSourceDirectory(String path) {
680 File sourceDirectory = new File(getSourceDirectoryName(path));
681 if (!sourceDirectory.exists() || !sourceDirectory.isDirectory()) {
682 return null;
683 }
684
685 return sourceDirectory;
686 }
687
c8cf44dc
MAL
688 private static File getSourceArchiveFile(String path) {
689 File sourceArchiveFile = new File(path);
690 if (!sourceArchiveFile.exists() || sourceArchiveFile.isDirectory()) {
691 return null;
692 }
693
694 return sourceArchiveFile;
695 }
696
002f9f07
BH
697 private static String getSourceDirectoryName(String sourceName) {
698 IPath result = new Path(sourceName.trim());
699 if (result.getDevice() != null && result.segmentCount() == 0) {
700 result = result.addTrailingSeparator();
701 } else {
702 result = result.removeTrailingSeparator();
703 }
704 return result.toOSString();
705 }
706
002f9f07 707 private void updateFromSourceField() {
d0e34947 708 setSourcePath(getSourceField().getText());
002f9f07
BH
709 updateWidgetEnablements();
710 }
711
c8cf44dc 712 private Combo getSourceField() {
d0e34947
MAL
713 if (directoryNameField == null) {
714 return fArchiveNameField;
715 }
716
c8cf44dc
MAL
717 return directoryNameField.isEnabled() ? directoryNameField : fArchiveNameField;
718 }
719
d0e34947
MAL
720 /**
721 * Set the source path that was selected by the user by various input
722 * methods (Browse button, typing, etc).
723 *
724 * Clients can also call this to set the path programmatically (hard-coded
725 * initial path) and this can also be overridden to be notified when the
726 * source path changes.
727 *
728 * @param path
729 * the source path
730 */
731 protected void setSourcePath(String path) {
c8cf44dc 732 Combo sourceField = getSourceField();
d0e34947
MAL
733 if (sourceField == null) {
734 return;
735 }
736
002f9f07 737 if (path.length() > 0) {
c8cf44dc 738 String[] currentItems = sourceField.getItems();
002f9f07
BH
739 int selectionIndex = -1;
740 for (int i = 0; i < currentItems.length; i++) {
741 if (currentItems[i].equals(path)) {
742 selectionIndex = i;
743 }
744 }
745 if (selectionIndex < 0) {
746 int oldLength = currentItems.length;
747 String[] newItems = new String[oldLength + 1];
748 System.arraycopy(currentItems, 0, newItems, 0, oldLength);
749 newItems[oldLength] = path;
c8cf44dc 750 sourceField.setItems(newItems);
002f9f07
BH
751 selectionIndex = oldLength;
752 }
c8cf44dc 753 sourceField.select(selectionIndex);
002f9f07
BH
754 }
755 resetSelection();
756 }
757
758 // ------------------------------------------------------------------------
759 // File Selection Group (forked WizardFileSystemResourceImportPage1)
760 // ------------------------------------------------------------------------
002f9f07 761 private void resetSelection() {
45fc1883 762
c8cf44dc
MAL
763 if (fSelectionGroupRoot != null) {
764 disposeSelectionGroupRoot();
765 }
766 fSelectionGroupRoot = getFileSystemTree();
767 fSelectionGroup.setRoot(fSelectionGroupRoot);
45fc1883
BH
768
769 if (fCreateExperimentCheckbox != null) {
770 File file = getSourceFile();
771 if (file != null) {
772 String previousName = fExperimentNameText.getText().trim();
773 if (((fPreviousSource != null) && (previousName.equals(fPreviousSource))) || previousName.isEmpty()) {
774 fExperimentNameText.setText(file.getName());
775 }
776 fPreviousSource = file.getName();
777 }
778 }
c8cf44dc
MAL
779 }
780
781 private void disposeSelectionGroupRoot() {
782 if (fSelectionGroupRoot != null && fSelectionGroupRoot.getProvider() != null) {
783 FileSystemObjectImportStructureProvider provider = fSelectionGroupRoot.getProvider();
784 provider.dispose();
785 fSelectionGroupRoot = null;
786 }
002f9f07
BH
787 }
788
252c602c 789 private TraceFileSystemElement getFileSystemTree() {
f17bb886 790 Pair<IFileSystemObject, FileSystemObjectImportStructureProvider> rootObjectAndProvider = ArchiveUtil.getRootObjectAndProvider(getSourceFile(), getContainer().getShell());
480e9739
MAL
791 if (rootObjectAndProvider == null) {
792 return null;
793 }
794 return selectFiles(rootObjectAndProvider.getFirst(), rootObjectAndProvider.getSecond());
795 }
796
c8cf44dc
MAL
797 private TraceFileSystemElement selectFiles(final IFileSystemObject rootFileSystemObject,
798 final FileSystemObjectImportStructureProvider structureProvider) {
252c602c 799 final TraceFileSystemElement[] results = new TraceFileSystemElement[1];
002f9f07
BH
800 BusyIndicator.showWhile(getShell().getDisplay(), new Runnable() {
801 @Override
802 public void run() {
803 // Create the root element from the supplied file system object
f17bb886 804 results[0] = TraceFileSystemElement.createRootTraceFileElement(rootFileSystemObject, structureProvider);
002f9f07
BH
805 }
806 });
807 return results[0];
808 }
809
002f9f07
BH
810 // ------------------------------------------------------------------------
811 // Trace Type Group
812 // ------------------------------------------------------------------------
002f9f07
BH
813 private final void createTraceTypeGroup(Composite parent) {
814 Composite composite = new Composite(parent, SWT.NONE);
815 GridLayout layout = new GridLayout();
816 layout.numColumns = 3;
817 layout.makeColumnsEqualWidth = false;
818 composite.setLayout(layout);
819 composite.setFont(parent.getFont());
820 GridData buttonData = new GridData(SWT.FILL, SWT.FILL, true, false);
821 composite.setLayoutData(buttonData);
822
823 // Trace type label ("Trace Type:")
824 Label typeLabel = new Label(composite, SWT.NONE);
825 typeLabel.setText(Messages.ImportTraceWizard_TraceType);
826 typeLabel.setFont(parent.getFont());
827
828 // Trace type combo
252c602c 829 fTraceTypes = new Combo(composite, SWT.BORDER | SWT.READ_ONLY);
002f9f07
BH
830 GridData data = new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1);
831 fTraceTypes.setLayoutData(data);
832 fTraceTypes.setFont(parent.getFont());
833
a4a116c3 834 String[] availableTraceTypes = TmfTraceType.getAvailableTraceTypes();
252c602c 835 String[] traceTypeList = new String[availableTraceTypes.length + 1];
d0e34947 836 traceTypeList[0] = TRACE_TYPE_AUTO_DETECT;
252c602c
BH
837 for (int i = 0; i < availableTraceTypes.length; i++) {
838 traceTypeList[i + 1] = availableTraceTypes[i];
839 }
840 fTraceTypes.setItems(traceTypeList);
e2a50e26 841 fTraceTypes.addSelectionListener(new SelectionAdapter() {
002f9f07
BH
842 @Override
843 public void widgetSelected(SelectionEvent e) {
844 updateWidgetEnablements();
d0e34947 845 boolean enabled = fTraceTypes.getText().equals(TRACE_TYPE_AUTO_DETECT);
e2a50e26 846 fImportUnrecognizedButton.setEnabled(enabled);
002f9f07
BH
847 }
848 });
252c602c
BH
849 fTraceTypes.select(0);
850
16f91bd5 851 // Unrecognized checkbox
252c602c
BH
852 fImportUnrecognizedButton = new Button(composite, SWT.CHECK);
853 fImportUnrecognizedButton.setSelection(true);
854 fImportUnrecognizedButton.setText(Messages.ImportTraceWizard_ImportUnrecognized);
002f9f07
BH
855 }
856
857 // ------------------------------------------------------------------------
858 // Options
859 // ------------------------------------------------------------------------
860
002f9f07
BH
861 @Override
862 protected void createOptionsGroupButtons(Group optionsGroup) {
863
864 // Overwrite checkbox
252c602c
BH
865 fOverwriteExistingResourcesCheckbox = new Button(optionsGroup, SWT.CHECK);
866 fOverwriteExistingResourcesCheckbox.setFont(optionsGroup.getFont());
867 fOverwriteExistingResourcesCheckbox.setText(Messages.ImportTraceWizard_OverwriteExistingTrace);
868 fOverwriteExistingResourcesCheckbox.setSelection(false);
002f9f07
BH
869
870 // Create links checkbox
252c602c
BH
871 fCreateLinksInWorkspaceButton = new Button(optionsGroup, SWT.CHECK);
872 fCreateLinksInWorkspaceButton.setFont(optionsGroup.getFont());
873 fCreateLinksInWorkspaceButton.setText(Messages.ImportTraceWizard_CreateLinksInWorkspace);
874 fCreateLinksInWorkspaceButton.setSelection(true);
002f9f07 875
252c602c 876 fCreateLinksInWorkspaceButton.addSelectionListener(new SelectionAdapter() {
002f9f07
BH
877 @Override
878 public void widgetSelected(SelectionEvent e) {
879 updateWidgetEnablements();
880 }
881 });
882
0e1d517e
MAL
883 fPreserveFolderStructureButton = new Button(optionsGroup, SWT.CHECK);
884 fPreserveFolderStructureButton.setFont(optionsGroup.getFont());
885 fPreserveFolderStructureButton.setText(Messages.ImportTraceWizard_PreserveFolderStructure);
886 fPreserveFolderStructureButton.setSelection(true);
887
45fc1883
BH
888 Composite comp = new Composite(optionsGroup, SWT.NONE);
889 GridLayout layout = new GridLayout(2, false);
890 layout.marginLeft = 0;
891 layout.marginRight = 0;
892 layout.marginBottom = 0;
893 layout.marginTop = 0;
894 layout.marginWidth = 0;
895 comp.setLayout(layout);
896 GridData data = new GridData(GridData.FILL, GridData.CENTER, true, false);
897 comp.setLayoutData(data);
898
899 fCreateExperimentCheckbox = new Button(comp, SWT.CHECK);
900 fCreateExperimentCheckbox.setFont(comp.getFont());
901 fCreateExperimentCheckbox.setText(Messages.ImportTraceWizard_CreateExperiment);
902 fCreateExperimentCheckbox.setSelection(false);
903 data = new GridData(GridData.BEGINNING, GridData.CENTER, false, false);
904 fCreateExperimentCheckbox.setLayoutData(data);
905
906 fExperimentNameText = new Text(comp, SWT.BORDER);
907 data = new GridData(GridData.FILL, GridData.CENTER, true, false);
908 fExperimentNameText.setLayoutData(data);
909
910 fExperimentNameText.addModifyListener(new ModifyListener() {
911 @Override
912 public void modifyText(ModifyEvent e) {
913 updateWidgetEnablements();
914 }
915 });
916
917 fCreateExperimentCheckbox.addSelectionListener(new SelectionAdapter() {
918 @Override
919 public void widgetSelected(SelectionEvent e) {
920 fExperimentNameText.setEnabled(fCreateExperimentCheckbox.getSelection());
921 updateWidgetEnablements();
922 }
923 });
924
002f9f07
BH
925 updateWidgetEnablements();
926 }
927
928 // ------------------------------------------------------------------------
929 // Determine if the finish button can be enabled
930 // ------------------------------------------------------------------------
002f9f07
BH
931 @Override
932 public boolean validateSourceGroup() {
933
480e9739 934 File source = getSourceFile();
c8cf44dc 935 if (source == null) {
002f9f07 936 setMessage(Messages.ImportTraceWizard_SelectTraceSourceEmpty);
c8cf44dc 937 setErrorMessage(null);
002f9f07
BH
938 return false;
939 }
940
c8cf44dc 941 if (sourceConflictsWithDestination(new Path(source.getPath()))) {
002f9f07
BH
942 setMessage(null);
943 setErrorMessage(getSourceConflictMessage());
944 return false;
945 }
946
6120dc63 947 if (!isImportFromDirectory() && !ArchiveUtil.ensureTarSourceIsValid(source.getAbsolutePath()) && !ArchiveUtil.ensureZipSourceIsValid(source.getAbsolutePath())
45fc1883 948 && !ArchiveUtil.ensureGzipSourceIsValid(source.getAbsolutePath())) {
1fb9f55c
MK
949 setMessage(null);
950 setErrorMessage(Messages.ImportTraceWizard_BadArchiveFormat);
951 return false;
c8cf44dc
MAL
952 }
953
282218e9 954 if (fSelectionGroup.getCheckedElementCount() == 0) {
002f9f07
BH
955 setMessage(null);
956 setErrorMessage(Messages.ImportTraceWizard_SelectTraceNoneSelected);
957 return false;
958 }
959
960 IContainer container = getSpecifiedContainer();
961 if (container != null && container.isVirtual()) {
962 if (Platform.getPreferencesService().getBoolean(Activator.PLUGIN_ID, ResourcesPlugin.PREF_DISABLE_LINKING, false, null)) {
963 setMessage(null);
964 setErrorMessage(Messages.ImportTraceWizard_CannotImportFilesUnderAVirtualFolder);
965 return false;
966 }
252c602c 967 if (fCreateLinksInWorkspaceButton == null || !fCreateLinksInWorkspaceButton.getSelection()) {
002f9f07
BH
968 setMessage(null);
969 setErrorMessage(Messages.ImportTraceWizard_HaveToCreateLinksUnderAVirtualFolder);
970 return false;
971 }
972 }
45fc1883
BH
973 setErrorMessage(null);
974 return true;
975 }
002f9f07 976
45fc1883
BH
977 @Override
978 protected boolean validateOptionsGroup() {
979 if (fCreateExperimentCheckbox != null && fCreateExperimentCheckbox.getSelection()) {
980 String name = fExperimentNameText.getText().trim();
981 // verify if experiment name is empty
982 if (name.isEmpty()) {
983 setMessage(null);
984 setErrorMessage(Messages.ImportTraceWizard_ErrorEmptyExperimentName);
985 return false;
986 }
987 // verify that name is a valid resource name
988 IWorkspace workspace = ResourcesPlugin.getWorkspace();
989 if ((workspace != null) && (!workspace.validateName(name, IResource.FILE).isOK())) {
990 setMessage(null);
991 setErrorMessage(NLS.bind(Messages.ImportTraceWizard_ErrorExperimentNameInvalid, name));
992 return false;
993 }
994 // verify if experiment already exists
995 if (fExperimentFolderElement != null) {
996 TmfExperimentElement element = fExperimentFolderElement.getExperiment(name);
997 if (element != null) {
998 setMessage(null);
999 setErrorMessage(NLS.bind(Messages.ImportTraceWizard_ErrorExperimentAlreadyExists, name));
1000 return false;
1001 }
1002 IFolder expResource = fExperimentFolderElement.getResource();
1003 IResource res = expResource.findMember(name);
1004 if (res != null) {
1005 setMessage(null);
1006 setErrorMessage(NLS.bind(Messages.ImportTraceWizard_ErrorResourceAlreadyExists, name));
1007 return false;
1008 }
1009 }
1010 }
002f9f07
BH
1011 setErrorMessage(null);
1012 return true;
1013 }
1014
480e9739
MAL
1015 private File getSourceFile() {
1016 return isImportFromDirectory() ? getSourceDirectory() : getSourceArchiveFile();
1017 }
1018
d0e34947
MAL
1019 private boolean isImportFromDirectory() {
1020 return fImportFromDirectoryRadio != null && fImportFromDirectoryRadio.getSelection();
1021 }
1022
6781d5b9
BH
1023 @Override
1024 protected void restoreWidgetValues() {
1025 super.restoreWidgetValues();
1026
1027 IDialogSettings settings = getDialogSettings();
1028 boolean value;
d0e34947
MAL
1029 if (fImportUnrecognizedButton != null) {
1030 if (settings.get(getPageStoreKey(IMPORT_WIZARD_IMPORT_UNRECOGNIZED_ID)) == null) {
1031 value = true;
1032 } else {
1033 value = settings.getBoolean(getPageStoreKey(IMPORT_WIZARD_IMPORT_UNRECOGNIZED_ID));
1034 }
1035 fImportUnrecognizedButton.setSelection(value);
6781d5b9 1036 }
6781d5b9 1037
d0e34947
MAL
1038 if (fPreserveFolderStructureButton != null) {
1039 if (settings.get(getPageStoreKey(IMPORT_WIZARD_PRESERVE_FOLDERS_ID)) == null) {
1040 value = true;
1041 } else {
1042 value = settings.getBoolean(getPageStoreKey(IMPORT_WIZARD_PRESERVE_FOLDERS_ID));
1043 }
1044 fPreserveFolderStructureButton.setSelection(value);
6781d5b9 1045 }
6781d5b9 1046
45fc1883
BH
1047 if (fCreateExperimentCheckbox != null) {
1048 if (settings.get(getPageStoreKey(IMPORT_WIZARD_CREATE_EXPERIMENT_ID)) == null) {
1049 value = false;
1050 } else {
1051 value = settings.getBoolean(getPageStoreKey(IMPORT_WIZARD_CREATE_EXPERIMENT_ID));
1052 }
1053 fCreateExperimentCheckbox.setSelection(value);
1054 fExperimentNameText.setEnabled(fCreateExperimentCheckbox.getSelection());
1055 }
1056
d0e34947 1057 if (settings.get(getPageStoreKey(IMPORT_WIZARD_IMPORT_FROM_DIRECTORY_ID)) == null) {
c8cf44dc
MAL
1058 value = true;
1059 } else {
d0e34947 1060 value = settings.getBoolean(getPageStoreKey(IMPORT_WIZARD_IMPORT_FROM_DIRECTORY_ID));
c8cf44dc 1061 }
c8cf44dc 1062
d0e34947
MAL
1063 if (directoryNameField != null) {
1064 restoreComboValues(directoryNameField, settings, getPageStoreKey(IMPORT_WIZARD_ROOT_DIRECTORY_ID));
1065 }
1066 if (fArchiveNameField != null) {
1067 restoreComboValues(fArchiveNameField, settings, getPageStoreKey(IMPORT_WIZARD_ARCHIVE_FILE_NAME_ID));
1068 }
1069
1070 if (fImportFromDirectoryRadio != null) {
1071 fImportFromDirectoryRadio.setSelection(value);
1072 if (value) {
1073 directoryRadioSelected();
1074 }
1075 }
1076 if (fImportFromArchiveRadio != null) {
1077 fImportFromArchiveRadio.setSelection(!value);
1078 if (!value) {
1079 archiveRadioSelected();
1080 }
6781d5b9
BH
1081 }
1082 }
1083
1084 @Override
1085 protected void saveWidgetValues() {
1086 // Persist dialog settings
1087 IDialogSettings settings = getDialogSettings();
d0e34947
MAL
1088 if (fImportUnrecognizedButton != null) {
1089 settings.put(getPageStoreKey(IMPORT_WIZARD_IMPORT_UNRECOGNIZED_ID), fImportUnrecognizedButton.getSelection());
1090 }
1091 if (fPreserveFolderStructureButton != null) {
1092 settings.put(getPageStoreKey(IMPORT_WIZARD_PRESERVE_FOLDERS_ID), fPreserveFolderStructureButton.getSelection());
1093 }
45fc1883
BH
1094
1095 if (fCreateExperimentCheckbox != null) {
1096 settings.put(getPageStoreKey(IMPORT_WIZARD_CREATE_EXPERIMENT_ID), fCreateExperimentCheckbox.getSelection());
1097 }
1098
d0e34947
MAL
1099 settings.put(getPageStoreKey(IMPORT_WIZARD_IMPORT_FROM_DIRECTORY_ID), isImportFromDirectory());
1100
1101 if (directoryNameField != null) {
1102 saveComboValues(directoryNameField, settings, getPageStoreKey(IMPORT_WIZARD_ROOT_DIRECTORY_ID));
1103 }
1104 if (fArchiveNameField != null) {
1105 saveComboValues(fArchiveNameField, settings, getPageStoreKey(IMPORT_WIZARD_ARCHIVE_FILE_NAME_ID));
1106 }
1107 }
c8cf44dc 1108
d0e34947
MAL
1109 private String getPageStoreKey(String key) {
1110 return getName() + key;
c8cf44dc
MAL
1111 }
1112
1113 private static void restoreComboValues(Combo combo, IDialogSettings settings, String key) {
1114 String[] directoryNames = settings.getArray(key);
1115 if ((directoryNames != null) && (directoryNames.length != 0)) {
1116 for (int i = 0; i < directoryNames.length; i++) {
1117 combo.add(directoryNames[i]);
1118 }
1119 }
1120 }
6781d5b9 1121
c8cf44dc
MAL
1122 private void saveComboValues(Combo combo, IDialogSettings settings, String key) {
1123 // update names history
1124 String[] directoryNames = settings.getArray(key);
6781d5b9
BH
1125 if (directoryNames == null) {
1126 directoryNames = new String[0];
1127 }
1128
c8cf44dc 1129 String items[] = combo.getItems();
6781d5b9
BH
1130 for (int i = 0; i < items.length; i++) {
1131 directoryNames = addToHistory(directoryNames, items[i]);
1132 }
c8cf44dc 1133 settings.put(key, directoryNames);
6781d5b9
BH
1134 }
1135
002f9f07
BH
1136 // ------------------------------------------------------------------------
1137 // Import the trace(s)
1138 // ------------------------------------------------------------------------
1139
1140 /**
1141 * Finish the import.
1142 *
252c602c 1143 * @return <code>true</code> if successful else <code>false</code>
002f9f07
BH
1144 */
1145 public boolean finish() {
2b0005f0 1146 String traceTypeLabel = getImportTraceTypeId();
f17bb886 1147 final String traceId = !TRACE_TYPE_AUTO_DETECT.equals(traceTypeLabel) ? TmfTraceType.getTraceTypeId(traceTypeLabel) : null;
002f9f07 1148
6781d5b9
BH
1149 // Save dialog settings
1150 saveWidgetValues();
002f9f07 1151
f17bb886
MAL
1152 final IPath baseSourceContainerPath = new Path(getSourceContainerPath());
1153 final boolean importFromArchive = getSourceArchiveFile() != null;
1154 final int importOptionFlags = getImportOptionFlags();
1155 final IPath destinationContainerPath = getContainerFullPath();
002f9f07 1156
f17bb886
MAL
1157 final IStatus[] operationStatus = new IStatus[1];
1158 operationStatus[0] = Status.OK_STATUS;
45fc1883 1159 final List<IResource> traceResources = new ArrayList<>();
252c602c
BH
1160 try {
1161 getContainer().run(true, true, new IRunnableWithProgress() {
1162 @Override
1163 public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
f17bb886
MAL
1164
1165 final List<TraceFileSystemElement> selectedFileSystemElements = new LinkedList<>();
1166 IElementFilter passThroughFilter = new IElementFilter() {
1167
1168 @Override
1169 public void filterElements(Collection elements, IProgressMonitor m) {
1170 selectedFileSystemElements.addAll(elements);
1171 }
1172
1173 @Override
1174 public void filterElements(Object[] elements, IProgressMonitor m) {
1175 for (int i = 0; i < elements.length; i++) {
1176 selectedFileSystemElements.add((TraceFileSystemElement) elements[i]);
1177 }
1178 }
1179 };
1180
1181 // List fileSystemElements will be filled using the
1182 // passThroughFilter
1183 SubMonitor subMonitor = SubMonitor.convert(monitor, 1);
1184 fSelectionGroup.getAllCheckedListItems(passThroughFilter, subMonitor);
1185
1186 final TraceValidateAndImportOperation operation = new TraceValidateAndImportOperation(getContainer().getShell(), selectedFileSystemElements, traceId, baseSourceContainerPath, destinationContainerPath, importFromArchive,
1187 importOptionFlags, fTraceFolderElement);
252c602c
BH
1188 operation.run(monitor);
1189 monitor.done();
f17bb886 1190 operationStatus[0] = operation.getStatus();
45fc1883 1191 traceResources.addAll(operation.getImportedResources());
002f9f07 1192 }
252c602c 1193 });
002f9f07 1194
45fc1883
BH
1195 // Only create experiment when option is selected and
1196 // if there has been at least one trace imported
1197 if (((importOptionFlags & OPTION_CREATE_EXPERIMENT) != 0) && (traceResources.size() > 0)) {
1198 final IFolder[] experimentFolders = new IFolder[1];
1199 final TmfExperimentFolder root = fExperimentFolderElement;
1200 final String experimentName = fExperimentNameText.getText().trim();
1201 // just safety guards
1202 if ((root == null) || (experimentName == null)) {
1203 return true;
1204 }
1205 if ((operationStatus[0] != null) && (operationStatus[0].isOK())) {
1206 getContainer().run(true, true, new IRunnableWithProgress() {
1207 @Override
1208 public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
1209 operationStatus[0] = null;
1210 final NewExperimentOperation operation = new NewExperimentOperation(root, experimentName);
1211 operation.run(monitor);
1212 monitor.done();
1213 operationStatus[0] = operation.getStatus();
1214 experimentFolders[0] = operation.getExperimentFolder();
1215 }
1216 });
1217
1218 final IFolder expFolder = experimentFolders[0];
1219 final TmfTraceFolder parentTraceFolder = fTraceFolderElement;
1220 // just safety guards
1221 if ((expFolder == null) || (parentTraceFolder == null)) {
1222 return true;
1223 }
1224 if ((operationStatus[0] != null) && (operationStatus[0].isOK())) {
1225 getContainer().run(true, true, new IRunnableWithProgress() {
1226 @Override
1227 public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
1228 operationStatus[0] = null;
1229 final SelectTracesOperation operation = new SelectTracesOperation(root, expFolder, parentTraceFolder, traceResources);
1230 operation.run(monitor);
1231 monitor.done();
1232 operationStatus[0] = operation.getStatus();
1233 }
1234 });
1235 }
1236 }
1237 }
252c602c 1238 } catch (InvocationTargetException e) {
f17bb886 1239 operationStatus[0] = new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.ImportTraceWizard_ImportProblem, e.getTargetException());
252c602c 1240 } catch (InterruptedException e) {
f17bb886 1241 operationStatus[0] = Status.CANCEL_STATUS;
ba560b10 1242 }
f17bb886
MAL
1243 if (!operationStatus[0].isOK()) {
1244 if (operationStatus[0].getSeverity() == IStatus.CANCEL) {
ba560b10
MK
1245 setMessage(Messages.ImportTraceWizard_ImportOperationCancelled);
1246 setErrorMessage(null);
1247 } else {
f17bb886
MAL
1248 Throwable exception = operationStatus[0].getException();
1249 if (exception != null) {
1250 Activator.getDefault().logError(exception.getMessage(), exception);
1251 displayErrorDialog(operationStatus[0].getMessage() + ": " + exception); //$NON-NLS-1$
002f9f07 1252 }
ba560b10
MK
1253 setMessage(null);
1254 setErrorMessage(Messages.ImportTraceWizard_ImportProblem);
252c602c 1255 }
ba560b10 1256 return false;
252c602c 1257 }
252c602c
BH
1258 setErrorMessage(null);
1259 return true;
1260 }
002f9f07 1261
d0e34947
MAL
1262 /**
1263 * Get the trace type id to import as. This can also return
1264 * {@link #TRACE_TYPE_AUTO_DETECT} to communicate that automatic trace type
1265 * detection will occur instead of setting a specific trace type when
1266 * importing the traces.
1267 *
1268 * @return the trace type id or {@link #TRACE_TYPE_AUTO_DETECT}
1269 */
1270 protected String getImportTraceTypeId() {
1271 return fTraceTypes.getText();
1272 }
1273
1274 /**
1275 * Get import options in the form of flags (bits).
1276 *
1277 * @return the import flags.
1278 * @see #OPTION_CREATE_LINKS_IN_WORKSPACE
1279 * @see #OPTION_IMPORT_UNRECOGNIZED_TRACES
1280 * @see #OPTION_OVERWRITE_EXISTING_RESOURCES
1281 * @see #OPTION_PRESERVE_FOLDER_STRUCTURE
45fc1883 1282 * @see #OPTION_CREATE_EXPERIMENT
d0e34947
MAL
1283 */
1284 protected int getImportOptionFlags() {
1285 int flags = 0;
1286 if (fCreateLinksInWorkspaceButton != null && fCreateLinksInWorkspaceButton.getSelection()) {
1287 flags |= OPTION_CREATE_LINKS_IN_WORKSPACE;
1288 }
1289 if (fImportUnrecognizedButton != null && fImportUnrecognizedButton.getSelection()) {
1290 flags |= OPTION_IMPORT_UNRECOGNIZED_TRACES;
1291 }
1292 if (fOverwriteExistingResourcesCheckbox != null && fOverwriteExistingResourcesCheckbox.getSelection()) {
1293 flags |= OPTION_OVERWRITE_EXISTING_RESOURCES;
1294 }
1295 if (fPreserveFolderStructureButton != null && fPreserveFolderStructureButton.getSelection()) {
1296 flags |= OPTION_PRESERVE_FOLDER_STRUCTURE;
1297 }
45fc1883
BH
1298 if (fCreateExperimentCheckbox != null && fCreateExperimentCheckbox.getSelection()) {
1299 flags |= OPTION_CREATE_EXPERIMENT;
1300 }
d0e34947
MAL
1301 return flags;
1302 }
1303
c8cf44dc
MAL
1304 @Override
1305 public void dispose() {
1306 super.dispose();
1307 disposeSelectionGroupRoot();
1308 }
002f9f07 1309}
This page took 0.149403 seconds and 5 git commands to generate.