| 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.dialog.TmfFileDialogFactory; |
| 83 | import org.eclipse.tracecompass.tmf.ui.project.model.TmfExperimentElement; |
| 84 | import org.eclipse.tracecompass.tmf.ui.project.model.TmfExperimentFolder; |
| 85 | import org.eclipse.tracecompass.tmf.ui.project.model.TmfProjectElement; |
| 86 | import org.eclipse.tracecompass.tmf.ui.project.model.TmfProjectRegistry; |
| 87 | import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceFolder; |
| 88 | import org.eclipse.tracecompass.tmf.ui.project.model.TmfTracesFolder; |
| 89 | import org.eclipse.ui.dialogs.FileSystemElement; |
| 90 | import org.eclipse.ui.dialogs.WizardResourceImportPage; |
| 91 | import org.eclipse.ui.ide.dialogs.IElementFilter; |
| 92 | import org.eclipse.ui.ide.dialogs.ResourceTreeAndListGroup; |
| 93 | import org.eclipse.ui.internal.ide.DialogUtil; |
| 94 | import org.eclipse.ui.model.WorkbenchContentProvider; |
| 95 | import org.eclipse.ui.model.WorkbenchLabelProvider; |
| 96 | import org.eclipse.ui.model.WorkbenchViewerComparator; |
| 97 | |
| 98 | /** |
| 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. |
| 103 | * |
| 104 | * In our case traces could be files or a directory structure. This wizard |
| 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. |
| 110 | * |
| 111 | * |
| 112 | * @author Francois Chouinard |
| 113 | */ |
| 114 | @SuppressWarnings("restriction") |
| 115 | public class ImportTraceWizardPage extends WizardResourceImportPage { |
| 116 | |
| 117 | // ------------------------------------------------------------------------ |
| 118 | // Constants |
| 119 | // ------------------------------------------------------------------------ |
| 120 | private static final String IMPORT_WIZARD_PAGE_NAME = "ImportTraceWizardPage"; //$NON-NLS-1$ |
| 121 | private static final String IMPORT_WIZARD_ROOT_DIRECTORY_ID = ".import_root_directory_id"; //$NON-NLS-1$ ; |
| 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$ |
| 126 | private static final String IMPORT_WIZARD_CREATE_EXPERIMENT_ID = ".create_experiment"; //$NON-NLS-1$ |
| 127 | |
| 128 | // constant from WizardArchiveFileResourceImportPage1 |
| 129 | private static final String[] FILE_IMPORT_MASK = { "*.jar;*.zip;*.tar;*.tar.gz;*.tgz;*.gz", "*.*" }; //$NON-NLS-1$ //$NON-NLS-2$ |
| 130 | |
| 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 | */ |
| 136 | public static final String TRACE_TYPE_AUTO_DETECT = Messages.ImportTraceWizard_AutoDetection; |
| 137 | |
| 138 | /** |
| 139 | * Preserve the folder structure of the import traces. |
| 140 | */ |
| 141 | public static final int OPTION_PRESERVE_FOLDER_STRUCTURE = 1 << 1; |
| 142 | /** |
| 143 | * Create links to the trace files instead of copies. |
| 144 | */ |
| 145 | public static final int OPTION_CREATE_LINKS_IN_WORKSPACE = 1 << 2; |
| 146 | /** |
| 147 | * Import files that were not recognized as the selected trace type. |
| 148 | */ |
| 149 | public static final int OPTION_IMPORT_UNRECOGNIZED_TRACES = 1 << 3; |
| 150 | /** |
| 151 | * Overwrite existing resources without prompting. |
| 152 | */ |
| 153 | public static final int OPTION_OVERWRITE_EXISTING_RESOURCES = 1 << 4; |
| 154 | /** |
| 155 | * Create an experiment with imported traces. |
| 156 | */ |
| 157 | public static final int OPTION_CREATE_EXPERIMENT = 1 << 5; |
| 158 | |
| 159 | // ------------------------------------------------------------------------ |
| 160 | // Attributes |
| 161 | // ------------------------------------------------------------------------ |
| 162 | |
| 163 | // Target import directory ('Traces' folder) |
| 164 | private IFolder fTargetFolder; |
| 165 | // Target Trace folder element |
| 166 | private TmfTraceFolder fTraceFolderElement; |
| 167 | // The workspace experiment folder |
| 168 | private TmfExperimentFolder fExperimentFolderElement; |
| 169 | private String fPreviousSource; |
| 170 | // Flag to handle destination folder change event |
| 171 | private Boolean fIsDestinationChanged = false; |
| 172 | private final Object fSyncObject = new Object(); |
| 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; |
| 181 | // Button to preserve folder structure |
| 182 | private Button fPreserveFolderStructureButton; |
| 183 | // Button to create an experiment |
| 184 | private Button fCreateExperimentCheckbox; |
| 185 | // Text box for experiment name |
| 186 | private Text fExperimentNameText; |
| 187 | private boolean entryChanged = false; |
| 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; |
| 195 | |
| 196 | /** The archive name field */ |
| 197 | protected Combo fArchiveNameField; |
| 198 | /** The archive browse button. */ |
| 199 | protected Button fArchiveBrowseButton; |
| 200 | /** The directory name field */ |
| 201 | protected Combo directoryNameField; |
| 202 | /** The directory browse button. */ |
| 203 | protected Button directoryBrowseButton; |
| 204 | |
| 205 | private ResourceTreeAndListGroup fSelectionGroup; |
| 206 | |
| 207 | // Keep trace of the selection root so that we can dispose its related |
| 208 | // resources |
| 209 | private TraceFileSystemElement fSelectionGroupRoot; |
| 210 | |
| 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); |
| 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) { |
| 233 | fTraceFolderElement = (TmfTraceFolder) element; |
| 234 | traceFolder = fTraceFolderElement.getResource(); |
| 235 | } else if (element instanceof IProject) { |
| 236 | IProject project = (IProject) element; |
| 237 | try { |
| 238 | if (project.hasNature(TmfProjectNature.ID)) { |
| 239 | TmfProjectElement projectElement = TmfProjectRegistry.getProject(project, true); |
| 240 | fTraceFolderElement = projectElement.getTracesFolder(); |
| 241 | traceFolder = project.getFolder(TmfTracesFolder.TRACES_FOLDER_NAME); |
| 242 | } |
| 243 | } catch (CoreException e) { |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | // If no tracing project was selected or trace folder doesn't exist use |
| 248 | // default tracing project |
| 249 | if (traceFolder == null) { |
| 250 | IProject project = TmfProjectRegistry.createProject( |
| 251 | TmfCommonConstants.DEFAULT_TRACE_PROJECT_NAME, null, new NullProgressMonitor()); |
| 252 | TmfProjectElement projectElement = TmfProjectRegistry.getProject(project, true); |
| 253 | fTraceFolderElement = projectElement.getTracesFolder(); |
| 254 | traceFolder = project.getFolder(TmfTracesFolder.TRACES_FOLDER_NAME); |
| 255 | } |
| 256 | |
| 257 | // Set the target trace folder |
| 258 | if (traceFolder != null) { |
| 259 | fTargetFolder = traceFolder; |
| 260 | String path = traceFolder.getFullPath().toString(); |
| 261 | setContainerFieldValue(path); |
| 262 | } |
| 263 | |
| 264 | TmfProjectElement project = fTraceFolderElement.getProject(); |
| 265 | fExperimentFolderElement = project.getExperimentsFolder(); |
| 266 | } |
| 267 | |
| 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, |
| 288 | new FileSystemElement("Dummy", null, true), //$NON-NLS-1$ |
| 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 | |
| 307 | // ------------------------------------------------------------------------ |
| 308 | // WizardResourceImportPage |
| 309 | // ------------------------------------------------------------------------ |
| 310 | |
| 311 | @Override |
| 312 | protected void createSourceGroup(Composite parent) { |
| 313 | createSourceSelectionGroup(parent); |
| 314 | createFileSelectionGroup(parent); |
| 315 | createTraceTypeGroup(parent); |
| 316 | validateSourceGroup(); |
| 317 | } |
| 318 | |
| 319 | @Override |
| 320 | protected ITreeContentProvider getFileProvider() { |
| 321 | return new WorkbenchContentProvider() { |
| 322 | @Override |
| 323 | public Object[] getChildren(Object object) { |
| 324 | if (object instanceof TraceFileSystemElement) { |
| 325 | TraceFileSystemElement element = (TraceFileSystemElement) object; |
| 326 | return element.getFiles().getChildren(element); |
| 327 | } |
| 328 | return new Object[0]; |
| 329 | } |
| 330 | }; |
| 331 | } |
| 332 | |
| 333 | @Override |
| 334 | protected ITreeContentProvider getFolderProvider() { |
| 335 | return new WorkbenchContentProvider() { |
| 336 | @Override |
| 337 | public Object[] getChildren(Object o) { |
| 338 | if (o instanceof TraceFileSystemElement) { |
| 339 | TraceFileSystemElement element = (TraceFileSystemElement) o; |
| 340 | return element.getFolders().getChildren(); |
| 341 | } |
| 342 | return new Object[0]; |
| 343 | } |
| 344 | |
| 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 | } |
| 352 | // If we have not populated then wait until asked |
| 353 | return true; |
| 354 | } |
| 355 | return false; |
| 356 | } |
| 357 | }; |
| 358 | } |
| 359 | |
| 360 | // ------------------------------------------------------------------------ |
| 361 | // Directory Selection Group (forked WizardFileSystemResourceImportPage1) |
| 362 | // ------------------------------------------------------------------------ |
| 363 | |
| 364 | /** |
| 365 | * creates the source selection group. |
| 366 | * |
| 367 | * @param parent |
| 368 | * the parent composite |
| 369 | */ |
| 370 | protected void createSourceSelectionGroup(Composite parent) { |
| 371 | |
| 372 | Composite sourceGroup = new Composite(parent, SWT.NONE); |
| 373 | GridLayout layout = new GridLayout(); |
| 374 | layout.numColumns = 3; |
| 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); |
| 382 | fImportFromDirectoryRadio.setText(Messages.ImportTraceWizard_DirectoryLocation); |
| 383 | |
| 384 | // import location entry combo |
| 385 | directoryNameField = createPathSelectionCombo(sourceGroup); |
| 386 | createDirectoryBrowseButton(sourceGroup); |
| 387 | |
| 388 | // import from archive radio button |
| 389 | fImportFromArchiveRadio = new Button(sourceGroup, SWT.RADIO); |
| 390 | fImportFromArchiveRadio.setText(Messages.ImportTraceWizard_ArchiveLocation); |
| 391 | |
| 392 | // import location entry combo |
| 393 | fArchiveNameField = createPathSelectionCombo(sourceGroup); |
| 394 | createArchiveBrowseButton(sourceGroup); |
| 395 | |
| 396 | fImportFromDirectoryRadio.setSelection(true); |
| 397 | fArchiveNameField.setEnabled(false); |
| 398 | fArchiveBrowseButton.setEnabled(false); |
| 399 | |
| 400 | fImportFromDirectoryRadio.addSelectionListener(new SelectionAdapter() { |
| 401 | @Override |
| 402 | public void widgetSelected(SelectionEvent e) { |
| 403 | directoryRadioSelected(); |
| 404 | } |
| 405 | }); |
| 406 | |
| 407 | fImportFromArchiveRadio.addSelectionListener(new SelectionAdapter() { |
| 408 | @Override |
| 409 | public void widgetSelected(SelectionEvent e) { |
| 410 | archiveRadioSelected(); |
| 411 | } |
| 412 | }); |
| 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); |
| 441 | |
| 442 | TraverseListener traverseListener = new TraverseListener() { |
| 443 | @Override |
| 444 | public void keyTraversed(TraverseEvent e) { |
| 445 | if (e.detail == SWT.TRAVERSE_RETURN) { |
| 446 | e.doit = false; |
| 447 | entryChanged = false; |
| 448 | updateFromSourceField(); |
| 449 | } |
| 450 | } |
| 451 | }; |
| 452 | |
| 453 | FocusAdapter focusAdapter = new FocusAdapter() { |
| 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 | } |
| 462 | }; |
| 463 | |
| 464 | SelectionAdapter selectionAdapter = new SelectionAdapter() { |
| 465 | @Override |
| 466 | public void widgetSelected(SelectionEvent e) { |
| 467 | entryChanged = false; |
| 468 | updateFromSourceField(); |
| 469 | } |
| 470 | }; |
| 471 | |
| 472 | ModifyListener modifyListner = new ModifyListener() { |
| 473 | @Override |
| 474 | public void modifyText(ModifyEvent e) { |
| 475 | entryChanged = true; |
| 476 | } |
| 477 | }; |
| 478 | |
| 479 | pathSelectionCombo.addModifyListener(modifyListner); |
| 480 | pathSelectionCombo.addTraverseListener(traverseListener); |
| 481 | pathSelectionCombo.addFocusListener(focusAdapter); |
| 482 | pathSelectionCombo.addSelectionListener(selectionAdapter); |
| 483 | |
| 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() { |
| 496 | @Override |
| 497 | public void widgetSelected(SelectionEvent e) { |
| 498 | handleSourceDirectoryBrowseButtonPressed(); |
| 499 | } |
| 500 | }); |
| 501 | } |
| 502 | |
| 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() { |
| 512 | @Override |
| 513 | public void widgetSelected(SelectionEvent e) { |
| 514 | handleArchiveBrowseButtonPressed(FILE_IMPORT_MASK); |
| 515 | } |
| 516 | }); |
| 517 | } |
| 518 | |
| 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 | |
| 535 | private void archiveRadioSelected() { |
| 536 | if (!isImportFromDirectory()) { |
| 537 | directoryNameField.setEnabled(false); |
| 538 | directoryBrowseButton.setEnabled(false); |
| 539 | fArchiveNameField.setEnabled(true); |
| 540 | fArchiveBrowseButton.setEnabled(true); |
| 541 | updateFromSourceField(); |
| 542 | fArchiveNameField.setFocus(); |
| 543 | if (fCreateLinksInWorkspaceButton != null) { |
| 544 | fPreviousCreateLinksValue = fCreateLinksInWorkspaceButton.getSelection(); |
| 545 | fCreateLinksInWorkspaceButton.setSelection(false); |
| 546 | fCreateLinksInWorkspaceButton.setEnabled(false); |
| 547 | } |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | private void directoryRadioSelected() { |
| 552 | if (isImportFromDirectory()) { |
| 553 | directoryNameField.setEnabled(true); |
| 554 | directoryBrowseButton.setEnabled(true); |
| 555 | fArchiveNameField.setEnabled(false); |
| 556 | fArchiveBrowseButton.setEnabled(false); |
| 557 | updateFromSourceField(); |
| 558 | directoryNameField.setFocus(); |
| 559 | if (fCreateLinksInWorkspaceButton != null) { |
| 560 | fCreateLinksInWorkspaceButton.setSelection(fPreviousCreateLinksValue); |
| 561 | fCreateLinksInWorkspaceButton.setEnabled(true); |
| 562 | } |
| 563 | } |
| 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 | } |
| 575 | |
| 576 | // Avoid overwriting destination path without repeatedly trigger |
| 577 | // call of handleEvent(); |
| 578 | synchronized (fSyncObject) { |
| 579 | if (fIsDestinationChanged == false) { |
| 580 | event.display.asyncExec(new Runnable() { |
| 581 | @Override |
| 582 | public void run() { |
| 583 | synchronized (fSyncObject) { |
| 584 | fIsDestinationChanged = true; |
| 585 | String path = fTargetFolder.getFullPath().toString(); |
| 586 | setContainerFieldValue(path); |
| 587 | } |
| 588 | } |
| 589 | }); |
| 590 | } else { |
| 591 | fIsDestinationChanged = false; |
| 592 | } |
| 593 | } |
| 594 | super.handleEvent(event); |
| 595 | } |
| 596 | |
| 597 | @Override |
| 598 | protected void handleContainerBrowseButtonPressed() { |
| 599 | // Do nothing so that destination directory cannot be changed. |
| 600 | } |
| 601 | |
| 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); |
| 620 | setSourcePath(selectedDirectory); |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | /** |
| 625 | * Handle the button pressed event |
| 626 | * |
| 627 | * @param extensions |
| 628 | * file extensions used to filter files shown to the user |
| 629 | */ |
| 630 | protected void handleArchiveBrowseButtonPressed(String[] extensions) { |
| 631 | FileDialog dialog = TmfFileDialogFactory.create(fArchiveNameField.getShell(), SWT.SHEET); |
| 632 | dialog.setFilterExtensions(extensions); |
| 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); |
| 645 | setSourcePath(selectedArchive); |
| 646 | updateWidgetEnablements(); |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | private File getSourceDirectory() { |
| 651 | if (directoryNameField == null) { |
| 652 | return null; |
| 653 | } |
| 654 | return getSourceDirectory(directoryNameField.getText()); |
| 655 | } |
| 656 | |
| 657 | private File getSourceArchiveFile() { |
| 658 | if (fArchiveNameField == null) { |
| 659 | return null; |
| 660 | } |
| 661 | |
| 662 | return getSourceArchiveFile(fArchiveNameField.getText()); |
| 663 | } |
| 664 | |
| 665 | private String getSourceContainerPath() { |
| 666 | if (isImportFromDirectory()) { |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 707 | private void updateFromSourceField() { |
| 708 | setSourcePath(getSourceField().getText()); |
| 709 | updateWidgetEnablements(); |
| 710 | } |
| 711 | |
| 712 | private Combo getSourceField() { |
| 713 | if (directoryNameField == null) { |
| 714 | return fArchiveNameField; |
| 715 | } |
| 716 | |
| 717 | return directoryNameField.isEnabled() ? directoryNameField : fArchiveNameField; |
| 718 | } |
| 719 | |
| 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) { |
| 732 | Combo sourceField = getSourceField(); |
| 733 | if (sourceField == null) { |
| 734 | return; |
| 735 | } |
| 736 | |
| 737 | if (path.length() > 0) { |
| 738 | String[] currentItems = sourceField.getItems(); |
| 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; |
| 750 | sourceField.setItems(newItems); |
| 751 | selectionIndex = oldLength; |
| 752 | } |
| 753 | sourceField.select(selectionIndex); |
| 754 | } |
| 755 | resetSelection(); |
| 756 | } |
| 757 | |
| 758 | // ------------------------------------------------------------------------ |
| 759 | // File Selection Group (forked WizardFileSystemResourceImportPage1) |
| 760 | // ------------------------------------------------------------------------ |
| 761 | private void resetSelection() { |
| 762 | |
| 763 | if (fSelectionGroupRoot != null) { |
| 764 | disposeSelectionGroupRoot(); |
| 765 | } |
| 766 | fSelectionGroupRoot = getFileSystemTree(); |
| 767 | fSelectionGroup.setRoot(fSelectionGroupRoot); |
| 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 | } |
| 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 | } |
| 787 | } |
| 788 | |
| 789 | private TraceFileSystemElement getFileSystemTree() { |
| 790 | Pair<IFileSystemObject, FileSystemObjectImportStructureProvider> rootObjectAndProvider = ArchiveUtil.getRootObjectAndProvider(getSourceFile(), getContainer().getShell()); |
| 791 | if (rootObjectAndProvider == null) { |
| 792 | return null; |
| 793 | } |
| 794 | return selectFiles(rootObjectAndProvider.getFirst(), rootObjectAndProvider.getSecond()); |
| 795 | } |
| 796 | |
| 797 | private TraceFileSystemElement selectFiles(final IFileSystemObject rootFileSystemObject, |
| 798 | final FileSystemObjectImportStructureProvider structureProvider) { |
| 799 | final TraceFileSystemElement[] results = new TraceFileSystemElement[1]; |
| 800 | BusyIndicator.showWhile(getShell().getDisplay(), new Runnable() { |
| 801 | @Override |
| 802 | public void run() { |
| 803 | // Create the root element from the supplied file system object |
| 804 | results[0] = TraceFileSystemElement.createRootTraceFileElement(rootFileSystemObject, structureProvider); |
| 805 | } |
| 806 | }); |
| 807 | return results[0]; |
| 808 | } |
| 809 | |
| 810 | // ------------------------------------------------------------------------ |
| 811 | // Trace Type Group |
| 812 | // ------------------------------------------------------------------------ |
| 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 |
| 829 | fTraceTypes = new Combo(composite, SWT.BORDER | SWT.READ_ONLY); |
| 830 | GridData data = new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1); |
| 831 | fTraceTypes.setLayoutData(data); |
| 832 | fTraceTypes.setFont(parent.getFont()); |
| 833 | |
| 834 | String[] availableTraceTypes = TmfTraceType.getAvailableTraceTypes(); |
| 835 | String[] traceTypeList = new String[availableTraceTypes.length + 1]; |
| 836 | traceTypeList[0] = TRACE_TYPE_AUTO_DETECT; |
| 837 | for (int i = 0; i < availableTraceTypes.length; i++) { |
| 838 | traceTypeList[i + 1] = availableTraceTypes[i]; |
| 839 | } |
| 840 | fTraceTypes.setItems(traceTypeList); |
| 841 | fTraceTypes.addSelectionListener(new SelectionAdapter() { |
| 842 | @Override |
| 843 | public void widgetSelected(SelectionEvent e) { |
| 844 | updateWidgetEnablements(); |
| 845 | boolean enabled = fTraceTypes.getText().equals(TRACE_TYPE_AUTO_DETECT); |
| 846 | fImportUnrecognizedButton.setEnabled(enabled); |
| 847 | } |
| 848 | }); |
| 849 | fTraceTypes.select(0); |
| 850 | |
| 851 | // Unrecognized checkbox |
| 852 | fImportUnrecognizedButton = new Button(composite, SWT.CHECK); |
| 853 | fImportUnrecognizedButton.setSelection(true); |
| 854 | fImportUnrecognizedButton.setText(Messages.ImportTraceWizard_ImportUnrecognized); |
| 855 | } |
| 856 | |
| 857 | // ------------------------------------------------------------------------ |
| 858 | // Options |
| 859 | // ------------------------------------------------------------------------ |
| 860 | |
| 861 | @Override |
| 862 | protected void createOptionsGroupButtons(Group optionsGroup) { |
| 863 | |
| 864 | // Overwrite checkbox |
| 865 | fOverwriteExistingResourcesCheckbox = new Button(optionsGroup, SWT.CHECK); |
| 866 | fOverwriteExistingResourcesCheckbox.setFont(optionsGroup.getFont()); |
| 867 | fOverwriteExistingResourcesCheckbox.setText(Messages.ImportTraceWizard_OverwriteExistingTrace); |
| 868 | fOverwriteExistingResourcesCheckbox.setSelection(false); |
| 869 | |
| 870 | // Create links checkbox |
| 871 | fCreateLinksInWorkspaceButton = new Button(optionsGroup, SWT.CHECK); |
| 872 | fCreateLinksInWorkspaceButton.setFont(optionsGroup.getFont()); |
| 873 | fCreateLinksInWorkspaceButton.setText(Messages.ImportTraceWizard_CreateLinksInWorkspace); |
| 874 | fCreateLinksInWorkspaceButton.setSelection(true); |
| 875 | |
| 876 | fCreateLinksInWorkspaceButton.addSelectionListener(new SelectionAdapter() { |
| 877 | @Override |
| 878 | public void widgetSelected(SelectionEvent e) { |
| 879 | updateWidgetEnablements(); |
| 880 | } |
| 881 | }); |
| 882 | |
| 883 | fPreserveFolderStructureButton = new Button(optionsGroup, SWT.CHECK); |
| 884 | fPreserveFolderStructureButton.setFont(optionsGroup.getFont()); |
| 885 | fPreserveFolderStructureButton.setText(Messages.ImportTraceWizard_PreserveFolderStructure); |
| 886 | fPreserveFolderStructureButton.setSelection(true); |
| 887 | |
| 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 | |
| 925 | updateWidgetEnablements(); |
| 926 | } |
| 927 | |
| 928 | // ------------------------------------------------------------------------ |
| 929 | // Determine if the finish button can be enabled |
| 930 | // ------------------------------------------------------------------------ |
| 931 | @Override |
| 932 | public boolean validateSourceGroup() { |
| 933 | |
| 934 | File source = getSourceFile(); |
| 935 | if (source == null) { |
| 936 | setMessage(Messages.ImportTraceWizard_SelectTraceSourceEmpty); |
| 937 | setErrorMessage(null); |
| 938 | return false; |
| 939 | } |
| 940 | |
| 941 | if (sourceConflictsWithDestination(new Path(source.getPath()))) { |
| 942 | setMessage(null); |
| 943 | setErrorMessage(getSourceConflictMessage()); |
| 944 | return false; |
| 945 | } |
| 946 | |
| 947 | if (!isImportFromDirectory() && !ArchiveUtil.ensureTarSourceIsValid(source.getAbsolutePath()) && !ArchiveUtil.ensureZipSourceIsValid(source.getAbsolutePath()) |
| 948 | && !ArchiveUtil.ensureGzipSourceIsValid(source.getAbsolutePath())) { |
| 949 | setMessage(null); |
| 950 | setErrorMessage(Messages.ImportTraceWizard_BadArchiveFormat); |
| 951 | return false; |
| 952 | } |
| 953 | |
| 954 | if (fSelectionGroup.getCheckedElementCount() == 0) { |
| 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 | } |
| 967 | if (fCreateLinksInWorkspaceButton == null || !fCreateLinksInWorkspaceButton.getSelection()) { |
| 968 | setMessage(null); |
| 969 | setErrorMessage(Messages.ImportTraceWizard_HaveToCreateLinksUnderAVirtualFolder); |
| 970 | return false; |
| 971 | } |
| 972 | } |
| 973 | setErrorMessage(null); |
| 974 | return true; |
| 975 | } |
| 976 | |
| 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 | } |
| 1011 | setErrorMessage(null); |
| 1012 | return true; |
| 1013 | } |
| 1014 | |
| 1015 | private File getSourceFile() { |
| 1016 | return isImportFromDirectory() ? getSourceDirectory() : getSourceArchiveFile(); |
| 1017 | } |
| 1018 | |
| 1019 | private boolean isImportFromDirectory() { |
| 1020 | return fImportFromDirectoryRadio != null && fImportFromDirectoryRadio.getSelection(); |
| 1021 | } |
| 1022 | |
| 1023 | @Override |
| 1024 | protected void restoreWidgetValues() { |
| 1025 | super.restoreWidgetValues(); |
| 1026 | |
| 1027 | IDialogSettings settings = getDialogSettings(); |
| 1028 | boolean value; |
| 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); |
| 1036 | } |
| 1037 | |
| 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); |
| 1045 | } |
| 1046 | |
| 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 | |
| 1057 | if (settings.get(getPageStoreKey(IMPORT_WIZARD_IMPORT_FROM_DIRECTORY_ID)) == null) { |
| 1058 | value = true; |
| 1059 | } else { |
| 1060 | value = settings.getBoolean(getPageStoreKey(IMPORT_WIZARD_IMPORT_FROM_DIRECTORY_ID)); |
| 1061 | } |
| 1062 | |
| 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 | } |
| 1081 | } |
| 1082 | } |
| 1083 | |
| 1084 | @Override |
| 1085 | protected void saveWidgetValues() { |
| 1086 | // Persist dialog settings |
| 1087 | IDialogSettings settings = getDialogSettings(); |
| 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 | } |
| 1094 | |
| 1095 | if (fCreateExperimentCheckbox != null) { |
| 1096 | settings.put(getPageStoreKey(IMPORT_WIZARD_CREATE_EXPERIMENT_ID), fCreateExperimentCheckbox.getSelection()); |
| 1097 | } |
| 1098 | |
| 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 | } |
| 1108 | |
| 1109 | private String getPageStoreKey(String key) { |
| 1110 | return getName() + key; |
| 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 | } |
| 1121 | |
| 1122 | private void saveComboValues(Combo combo, IDialogSettings settings, String key) { |
| 1123 | // update names history |
| 1124 | String[] directoryNames = settings.getArray(key); |
| 1125 | if (directoryNames == null) { |
| 1126 | directoryNames = new String[0]; |
| 1127 | } |
| 1128 | |
| 1129 | String items[] = combo.getItems(); |
| 1130 | for (int i = 0; i < items.length; i++) { |
| 1131 | directoryNames = addToHistory(directoryNames, items[i]); |
| 1132 | } |
| 1133 | settings.put(key, directoryNames); |
| 1134 | } |
| 1135 | |
| 1136 | // ------------------------------------------------------------------------ |
| 1137 | // Import the trace(s) |
| 1138 | // ------------------------------------------------------------------------ |
| 1139 | |
| 1140 | /** |
| 1141 | * Finish the import. |
| 1142 | * |
| 1143 | * @return <code>true</code> if successful else <code>false</code> |
| 1144 | */ |
| 1145 | public boolean finish() { |
| 1146 | String traceTypeLabel = getImportTraceTypeId(); |
| 1147 | final String traceId = !TRACE_TYPE_AUTO_DETECT.equals(traceTypeLabel) ? TmfTraceType.getTraceTypeId(traceTypeLabel) : null; |
| 1148 | |
| 1149 | // Save dialog settings |
| 1150 | saveWidgetValues(); |
| 1151 | |
| 1152 | final IPath baseSourceContainerPath = new Path(getSourceContainerPath()); |
| 1153 | final boolean importFromArchive = getSourceArchiveFile() != null; |
| 1154 | final int importOptionFlags = getImportOptionFlags(); |
| 1155 | final IPath destinationContainerPath = getContainerFullPath(); |
| 1156 | |
| 1157 | final IStatus[] operationStatus = new IStatus[1]; |
| 1158 | operationStatus[0] = Status.OK_STATUS; |
| 1159 | final List<IResource> traceResources = new ArrayList<>(); |
| 1160 | try { |
| 1161 | getContainer().run(true, true, new IRunnableWithProgress() { |
| 1162 | @Override |
| 1163 | public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException { |
| 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); |
| 1188 | operation.run(monitor); |
| 1189 | monitor.done(); |
| 1190 | operationStatus[0] = operation.getStatus(); |
| 1191 | traceResources.addAll(operation.getImportedResources()); |
| 1192 | } |
| 1193 | }); |
| 1194 | |
| 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 | } |
| 1238 | } catch (InvocationTargetException e) { |
| 1239 | operationStatus[0] = new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.ImportTraceWizard_ImportProblem, e.getTargetException()); |
| 1240 | } catch (InterruptedException e) { |
| 1241 | operationStatus[0] = Status.CANCEL_STATUS; |
| 1242 | } |
| 1243 | if (!operationStatus[0].isOK()) { |
| 1244 | if (operationStatus[0].getSeverity() == IStatus.CANCEL) { |
| 1245 | setMessage(Messages.ImportTraceWizard_ImportOperationCancelled); |
| 1246 | setErrorMessage(null); |
| 1247 | } else { |
| 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$ |
| 1252 | } |
| 1253 | setMessage(null); |
| 1254 | setErrorMessage(Messages.ImportTraceWizard_ImportProblem); |
| 1255 | } |
| 1256 | return false; |
| 1257 | } |
| 1258 | setErrorMessage(null); |
| 1259 | return true; |
| 1260 | } |
| 1261 | |
| 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 |
| 1282 | * @see #OPTION_CREATE_EXPERIMENT |
| 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 | } |
| 1298 | if (fCreateExperimentCheckbox != null && fCreateExperimentCheckbox.getSelection()) { |
| 1299 | flags |= OPTION_CREATE_EXPERIMENT; |
| 1300 | } |
| 1301 | return flags; |
| 1302 | } |
| 1303 | |
| 1304 | @Override |
| 1305 | public void dispose() { |
| 1306 | super.dispose(); |
| 1307 | disposeSelectionGroupRoot(); |
| 1308 | } |
| 1309 | } |