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