Bug 417036 - [TMF] Legacy import: Trace type is not set after importing custom trace
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / project / wizards / importtrace / ImportTraceWizardPage.java
1 /*******************************************************************************
2 * Copyright (c) 2009, 2013 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 *******************************************************************************/
17
18 package org.eclipse.linuxtools.tmf.ui.project.wizards.importtrace;
19
20 import java.io.File;
21 import java.io.IOException;
22 import java.lang.reflect.InvocationTargetException;
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.Comparator;
26 import java.util.HashMap;
27 import java.util.Iterator;
28 import java.util.LinkedList;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.Map.Entry;
32
33 import org.eclipse.core.resources.IContainer;
34 import org.eclipse.core.resources.IFolder;
35 import org.eclipse.core.resources.IProject;
36 import org.eclipse.core.resources.IResource;
37 import org.eclipse.core.resources.ResourcesPlugin;
38 import org.eclipse.core.runtime.CoreException;
39 import org.eclipse.core.runtime.IConfigurationElement;
40 import org.eclipse.core.runtime.IPath;
41 import org.eclipse.core.runtime.IStatus;
42 import org.eclipse.core.runtime.Path;
43 import org.eclipse.core.runtime.Platform;
44 import org.eclipse.jface.dialogs.ErrorDialog;
45 import org.eclipse.jface.dialogs.MessageDialog;
46 import org.eclipse.jface.viewers.CheckStateChangedEvent;
47 import org.eclipse.jface.viewers.CheckboxTreeViewer;
48 import org.eclipse.jface.viewers.ICheckStateListener;
49 import org.eclipse.jface.viewers.IStructuredSelection;
50 import org.eclipse.jface.viewers.ITreeContentProvider;
51 import org.eclipse.linuxtools.internal.tmf.ui.Activator;
52 import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomTxtTrace;
53 import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomTxtTraceDefinition;
54 import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomXmlTrace;
55 import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomXmlTraceDefinition;
56 import org.eclipse.linuxtools.tmf.core.TmfCommonConstants;
57 import org.eclipse.linuxtools.tmf.core.TmfProjectNature;
58 import org.eclipse.linuxtools.tmf.ui.project.model.TmfProjectElement;
59 import org.eclipse.linuxtools.tmf.ui.project.model.TmfProjectRegistry;
60 import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement;
61 import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceFolder;
62 import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceType;
63 import org.eclipse.linuxtools.tmf.ui.project.wizards.Messages;
64 import org.eclipse.swt.SWT;
65 import org.eclipse.swt.custom.BusyIndicator;
66 import org.eclipse.swt.events.FocusEvent;
67 import org.eclipse.swt.events.FocusListener;
68 import org.eclipse.swt.events.KeyEvent;
69 import org.eclipse.swt.events.KeyListener;
70 import org.eclipse.swt.events.SelectionAdapter;
71 import org.eclipse.swt.events.SelectionEvent;
72 import org.eclipse.swt.events.SelectionListener;
73 import org.eclipse.swt.layout.GridData;
74 import org.eclipse.swt.layout.GridLayout;
75 import org.eclipse.swt.widgets.Button;
76 import org.eclipse.swt.widgets.Combo;
77 import org.eclipse.swt.widgets.Composite;
78 import org.eclipse.swt.widgets.DirectoryDialog;
79 import org.eclipse.swt.widgets.Event;
80 import org.eclipse.swt.widgets.Group;
81 import org.eclipse.swt.widgets.Label;
82 import org.eclipse.ui.IWorkbench;
83 import org.eclipse.ui.dialogs.FileSystemElement;
84 import org.eclipse.ui.dialogs.WizardResourceImportPage;
85 import org.eclipse.ui.model.WorkbenchContentProvider;
86 import org.eclipse.ui.model.WorkbenchLabelProvider;
87 import org.eclipse.ui.wizards.datatransfer.FileSystemStructureProvider;
88 import org.eclipse.ui.wizards.datatransfer.IImportStructureProvider;
89 import org.eclipse.ui.wizards.datatransfer.ImportOperation;
90
91 /**
92 * A variant of the standard resource import wizard with the following changes:
93 * <ul>
94 * <li>A folder/file combined checkbox tree viewer to select traces
95 * <li>Cherry-picking of traces in the file structure without re-creating the
96 * file hierarchy
97 * <li>A trace types dropbox for optional characterization
98 * </ul>
99 * For our purpose, a trace can either be a single file or a whole directory
100 * sub-tree, whichever is reached first from the root directory.
101 * <p>
102 *
103 * @version 1.0
104 * @author Francois Chouinard
105 * @since 2.0
106 */
107 public class ImportTraceWizardPage extends WizardResourceImportPage {
108
109 // ------------------------------------------------------------------------
110 // Classes
111 // ------------------------------------------------------------------------
112
113 private static class FileSystemElementComparator implements Comparator<Object> {
114
115 FileSystemStructureProvider provider = FileSystemStructureProvider.INSTANCE;
116
117 @Override
118 public int compare(Object o1, Object o2) {
119 String label1 = provider.getLabel(o1);
120 String label2 = provider.getLabel(o2);
121 return label1.compareTo(label2);
122 }
123
124 }
125
126 // ------------------------------------------------------------------------
127 // Constants
128 // ------------------------------------------------------------------------
129
130 private static final String IMPORT_WIZARD_PAGE = "ImportTraceWizardPage"; //$NON-NLS-1$
131 private static final String DEFAULT_TRACE_ICON_PATH = "icons/elcl16/trace.gif"; //$NON-NLS-1$
132 private static final FileSystemElementComparator FILE_SYSTEM_ELEMENT_COMPARATOR = new FileSystemElementComparator();
133
134 // ------------------------------------------------------------------------
135 // Attributes
136 // ------------------------------------------------------------------------
137
138 // Folder navigation start point (saved between invocations)
139 private static String fRootDirectory = null;
140
141 // Navigation folder content viewer and selector
142 private CheckboxTreeViewer fFolderViewer;
143
144 // Target import directory ('Traces' folder)
145 private IFolder fTargetFolder;
146 private static final String SEPARATOR = ":"; //$NON-NLS-1$
147
148 // ------------------------------------------------------------------------
149 // Constructors
150 // ------------------------------------------------------------------------
151
152 /**
153 * Constructor. Creates the trace wizard page.
154 *
155 * @param name
156 * The name of the page.
157 * @param selection
158 * The current selection
159 */
160 protected ImportTraceWizardPage(String name, IStructuredSelection selection) {
161 super(name, selection);
162 }
163
164 /**
165 * Constructor
166 *
167 * @param workbench
168 * The workbench reference.
169 * @param selection
170 * The current selection
171 */
172 public ImportTraceWizardPage(IWorkbench workbench, IStructuredSelection selection) {
173 this(IMPORT_WIZARD_PAGE, selection);
174 setTitle(Messages.ImportTraceWizard_FileSystemTitle);
175 setDescription(Messages.ImportTraceWizard_ImportTrace);
176
177 // Locate the target trace folder
178 IFolder traceFolder = null;
179 Object element = selection.getFirstElement();
180
181 if (element instanceof TmfTraceFolder) {
182 TmfTraceFolder tmfTraceFolder = (TmfTraceFolder) element;
183 tmfTraceFolder.getProject().getResource();
184 traceFolder = tmfTraceFolder.getResource();
185 } else if (element instanceof IProject) {
186 IProject project = (IProject) element;
187 try {
188 if (project.hasNature(TmfProjectNature.ID)) {
189 traceFolder = (IFolder) project.findMember(TmfTraceFolder.TRACE_FOLDER_NAME);
190 }
191 } catch (CoreException e) {
192 }
193 }
194
195 // Set the target trace folder
196 if (traceFolder != null) {
197 fTargetFolder = traceFolder;
198 String path = traceFolder.getFullPath().toOSString();
199 setContainerFieldValue(path);
200 }
201 }
202
203 // ------------------------------------------------------------------------
204 // WizardResourceImportPage
205 // ------------------------------------------------------------------------
206
207 @Override
208 public void createControl(Composite parent) {
209 super.createControl(parent);
210 // Restore last directory if applicable
211 if (fRootDirectory != null) {
212 directoryNameField.setText(fRootDirectory);
213 updateFromSourceField();
214 }
215 }
216
217 @Override
218 protected void createSourceGroup(Composite parent) {
219 createDirectorySelectionGroup(parent);
220 createFileSelectionGroup(parent);
221 createTraceTypeGroup(parent);
222 validateSourceGroup();
223 }
224
225 @Override
226 protected void createFileSelectionGroup(Composite parent) {
227
228 // This Composite is only used for widget alignment purposes
229 Composite composite = new Composite(parent, SWT.NONE);
230 composite.setFont(parent.getFont());
231 GridLayout layout = new GridLayout();
232 composite.setLayout(layout);
233 composite.setLayoutData(new GridData(GridData.FILL_BOTH));
234
235 final int PREFERRED_LIST_HEIGHT = 150;
236
237 fFolderViewer = new CheckboxTreeViewer(composite, SWT.BORDER);
238 GridData data = new GridData(GridData.FILL_BOTH);
239 data.heightHint = PREFERRED_LIST_HEIGHT;
240 fFolderViewer.getTree().setLayoutData(data);
241 fFolderViewer.getTree().setFont(parent.getFont());
242
243 fFolderViewer.setContentProvider(getFileProvider());
244 fFolderViewer.setLabelProvider(new WorkbenchLabelProvider());
245 fFolderViewer.addCheckStateListener(new ICheckStateListener() {
246 @Override
247 public void checkStateChanged(CheckStateChangedEvent event) {
248 Object elem = event.getElement();
249 if (elem instanceof FileSystemElement) {
250 FileSystemElement element = (FileSystemElement) elem;
251 if (fFolderViewer.getGrayed(element)) {
252 fFolderViewer.setSubtreeChecked(element, false);
253 fFolderViewer.setGrayed(element, false);
254 } else if (event.getChecked()) {
255 fFolderViewer.setSubtreeChecked(event.getElement(), true);
256 } else {
257 fFolderViewer.setParentsGrayed(element, true);
258 if (!element.isDirectory()) {
259 fFolderViewer.setGrayed(element, false);
260 }
261 }
262 updateWidgetEnablements();
263 }
264 }
265 });
266 }
267
268 @Override
269 protected ITreeContentProvider getFolderProvider() {
270 return null;
271 }
272
273 @Override
274 protected ITreeContentProvider getFileProvider() {
275 return new WorkbenchContentProvider() {
276 @Override
277 public Object[] getChildren(Object o) {
278 if (o instanceof FileSystemElement) {
279 FileSystemElement element = (FileSystemElement) o;
280 populateChildren(element);
281 // For our purpose, we need folders + files
282 Object[] folders = element.getFolders().getChildren();
283 Object[] files = element.getFiles().getChildren();
284
285 List<Object> result = new LinkedList<Object>();
286 for (Object folder : folders) {
287 result.add(folder);
288 }
289 for (Object file : files) {
290 result.add(file);
291 }
292
293 return result.toArray();
294 }
295 return new Object[0];
296 }
297 };
298 }
299
300 private static void populateChildren(FileSystemElement parent) {
301 // Do not re-populate if the job was done already...
302 FileSystemStructureProvider provider = FileSystemStructureProvider.INSTANCE;
303 if (parent.getFolders().size() == 0 && parent.getFiles().size() == 0) {
304 Object fileSystemObject = parent.getFileSystemObject();
305 List<?> children = provider.getChildren(fileSystemObject);
306 if (children != null) {
307 Collections.sort(children, FILE_SYSTEM_ELEMENT_COMPARATOR);
308 Iterator<?> iterator = children.iterator();
309 while (iterator.hasNext()) {
310 Object child = iterator.next();
311 String label = provider.getLabel(child);
312 FileSystemElement element = new FileSystemElement(label, parent, provider.isFolder(child));
313 element.setFileSystemObject(child);
314 }
315 }
316 }
317 }
318
319 @Override
320 protected List<FileSystemElement> getSelectedResources() {
321 List<FileSystemElement> resources = new ArrayList<FileSystemElement>();
322 Object[] checkedItems = fFolderViewer.getCheckedElements();
323 for (Object item : checkedItems) {
324 if (item instanceof FileSystemElement && !fFolderViewer.getGrayed(item)) {
325 resources.add((FileSystemElement) item);
326 }
327 }
328 return resources;
329 }
330
331 // ------------------------------------------------------------------------
332 // Directory Selection Group (forked WizardFileSystemResourceImportPage1)
333 // ------------------------------------------------------------------------
334
335 /**
336 * The directory name field
337 */
338 protected Combo directoryNameField;
339 /**
340 * The directory browse button.
341 */
342 protected Button directoryBrowseButton;
343
344 private boolean entryChanged = false;
345
346 /**
347 * creates the directory selection group.
348 *
349 * @param parent
350 * the parent composite
351 */
352 protected void createDirectorySelectionGroup(Composite parent) {
353
354 Composite directoryContainerGroup = new Composite(parent, SWT.NONE);
355 GridLayout layout = new GridLayout();
356 layout.numColumns = 3;
357 directoryContainerGroup.setLayout(layout);
358 directoryContainerGroup.setFont(parent.getFont());
359 directoryContainerGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
360
361 // Label ("Trace directory:")
362 Label groupLabel = new Label(directoryContainerGroup, SWT.NONE);
363 groupLabel.setText(Messages.ImportTraceWizard_DirectoryLocation);
364 groupLabel.setFont(parent.getFont());
365
366 // Directory name entry field
367 directoryNameField = new Combo(directoryContainerGroup, SWT.BORDER);
368 GridData data = new GridData(SWT.FILL, SWT.FILL, true, false);
369 data.widthHint = SIZING_TEXT_FIELD_WIDTH;
370 directoryNameField.setLayoutData(data);
371 directoryNameField.setFont(parent.getFont());
372
373 directoryNameField.addSelectionListener(new SelectionAdapter() {
374 @Override
375 public void widgetSelected(SelectionEvent e) {
376 updateFromSourceField();
377 }
378 });
379
380 directoryNameField.addKeyListener(new KeyListener() {
381 @Override
382 public void keyPressed(KeyEvent e) {
383 // If there has been a key pressed then mark as dirty
384 entryChanged = true;
385 if (e.character == SWT.CR) { // Windows...
386 entryChanged = false;
387 updateFromSourceField();
388 }
389 }
390
391 @Override
392 public void keyReleased(KeyEvent e) {
393 }
394 });
395
396 directoryNameField.addFocusListener(new FocusListener() {
397 @Override
398 public void focusGained(FocusEvent e) {
399 // Do nothing when getting focus
400 }
401
402 @Override
403 public void focusLost(FocusEvent e) {
404 // Clear the flag to prevent constant update
405 if (entryChanged) {
406 entryChanged = false;
407 updateFromSourceField();
408 }
409 }
410 });
411
412 // Browse button
413 directoryBrowseButton = new Button(directoryContainerGroup, SWT.PUSH);
414 directoryBrowseButton.setText(Messages.ImportTraceWizard_BrowseButton);
415 directoryBrowseButton.addListener(SWT.Selection, this);
416 directoryBrowseButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
417 directoryBrowseButton.setFont(parent.getFont());
418 setButtonLayoutData(directoryBrowseButton);
419 }
420
421 // ------------------------------------------------------------------------
422 // Browse for the source directory
423 // ------------------------------------------------------------------------
424
425 @Override
426 public void handleEvent(Event event) {
427 if (event.widget == directoryBrowseButton) {
428 handleSourceDirectoryBrowseButtonPressed();
429 }
430 super.handleEvent(event);
431 }
432
433 /**
434 * Handle the button pressed event
435 */
436 protected void handleSourceDirectoryBrowseButtonPressed() {
437 String currentSource = directoryNameField.getText();
438 DirectoryDialog dialog = new DirectoryDialog(directoryNameField.getShell(), SWT.SAVE | SWT.SHEET);
439 dialog.setText(Messages.ImportTraceWizard_SelectTraceDirectoryTitle);
440 dialog.setMessage(Messages.ImportTraceWizard_SelectTraceDirectoryMessage);
441 dialog.setFilterPath(getSourceDirectoryName(currentSource));
442
443 String selectedDirectory = dialog.open();
444 if (selectedDirectory != null) {
445 // Just quit if the directory is not valid
446 if ((getSourceDirectory(selectedDirectory) == null) || selectedDirectory.equals(currentSource)) {
447 return;
448 }
449 // If it is valid then proceed to populate
450 setErrorMessage(null);
451 setSourceName(selectedDirectory);
452 }
453 }
454
455 private File getSourceDirectory() {
456 return getSourceDirectory(directoryNameField.getText());
457 }
458
459 private static File getSourceDirectory(String path) {
460 File sourceDirectory = new File(getSourceDirectoryName(path));
461 if (!sourceDirectory.exists() || !sourceDirectory.isDirectory()) {
462 return null;
463 }
464
465 return sourceDirectory;
466 }
467
468 private static String getSourceDirectoryName(String sourceName) {
469 IPath result = new Path(sourceName.trim());
470 if (result.getDevice() != null && result.segmentCount() == 0) {
471 result = result.addTrailingSeparator();
472 } else {
473 result = result.removeTrailingSeparator();
474 }
475 return result.toOSString();
476 }
477
478 private String getSourceDirectoryName() {
479 return getSourceDirectoryName(directoryNameField.getText());
480 }
481
482 private void updateFromSourceField() {
483 setSourceName(directoryNameField.getText());
484 updateWidgetEnablements();
485 }
486
487 private void setSourceName(String path) {
488 if (path.length() > 0) {
489 String[] currentItems = directoryNameField.getItems();
490 int selectionIndex = -1;
491 for (int i = 0; i < currentItems.length; i++) {
492 if (currentItems[i].equals(path)) {
493 selectionIndex = i;
494 }
495 }
496 if (selectionIndex < 0) {
497 int oldLength = currentItems.length;
498 String[] newItems = new String[oldLength + 1];
499 System.arraycopy(currentItems, 0, newItems, 0, oldLength);
500 newItems[oldLength] = path;
501 directoryNameField.setItems(newItems);
502 selectionIndex = oldLength;
503 }
504 directoryNameField.select(selectionIndex);
505 }
506 resetSelection();
507 }
508
509 // ------------------------------------------------------------------------
510 // File Selection Group (forked WizardFileSystemResourceImportPage1)
511 // ------------------------------------------------------------------------
512
513 private void resetSelection() {
514 FileSystemElement root = getFileSystemTree();
515 populateListViewer(root);
516 }
517
518 private void populateListViewer(final Object treeElement) {
519 fFolderViewer.setInput(treeElement);
520 }
521
522 private FileSystemElement getFileSystemTree() {
523 File sourceDirectory = getSourceDirectory();
524 if (sourceDirectory == null) {
525 return null;
526 }
527 return selectFiles(sourceDirectory, FileSystemStructureProvider.INSTANCE);
528 }
529
530 private FileSystemElement selectFiles(final Object rootFileSystemObject, final IImportStructureProvider structureProvider) {
531 final FileSystemElement[] results = new FileSystemElement[1];
532 BusyIndicator.showWhile(getShell().getDisplay(), new Runnable() {
533 @Override
534 public void run() {
535 // Create the root element from the supplied file system object
536 results[0] = createRootElement(rootFileSystemObject, structureProvider);
537 }
538 });
539 return results[0];
540 }
541
542 private static FileSystemElement createRootElement(Object fileSystemObject,
543 IImportStructureProvider provider) {
544
545 boolean isContainer = provider.isFolder(fileSystemObject);
546 String elementLabel = provider.getLabel(fileSystemObject);
547
548 FileSystemElement dummyParent = new FileSystemElement("", null, true); //$NON-NLS-1$
549 FileSystemElement element = new FileSystemElement(elementLabel, dummyParent, isContainer);
550 element.setFileSystemObject(fileSystemObject);
551
552 // Get the first level
553 populateChildren(element);
554
555 return dummyParent;
556 }
557
558 // ------------------------------------------------------------------------
559 // Trace Type Group
560 // ------------------------------------------------------------------------
561
562 private Combo fTraceTypes;
563
564 private final void createTraceTypeGroup(Composite parent) {
565 Composite composite = new Composite(parent, SWT.NONE);
566 GridLayout layout = new GridLayout();
567 layout.numColumns = 3;
568 layout.makeColumnsEqualWidth = false;
569 composite.setLayout(layout);
570 composite.setFont(parent.getFont());
571 GridData buttonData = new GridData(SWT.FILL, SWT.FILL, true, false);
572 composite.setLayoutData(buttonData);
573
574 // Trace type label ("Trace Type:")
575 Label typeLabel = new Label(composite, SWT.NONE);
576 typeLabel.setText(Messages.ImportTraceWizard_TraceType);
577 typeLabel.setFont(parent.getFont());
578
579 // Trace type combo
580 fTraceTypes = new Combo(composite, SWT.BORDER);
581 GridData data = new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1);
582 fTraceTypes.setLayoutData(data);
583 fTraceTypes.setFont(parent.getFont());
584
585 String[] availableTraceTypes = TmfTraceType.getInstance().getAvailableTraceTypes();
586 fTraceTypes.setItems(availableTraceTypes);
587
588 fTraceTypes.addSelectionListener(new SelectionListener() {
589 @Override
590 public void widgetSelected(SelectionEvent e) {
591 updateWidgetEnablements();
592 }
593
594 @Override
595 public void widgetDefaultSelected(SelectionEvent e) {
596 }
597 });
598 }
599
600 // ------------------------------------------------------------------------
601 // Options
602 // ------------------------------------------------------------------------
603
604 private Button overwriteExistingResourcesCheckbox;
605 private Button createLinksInWorkspaceButton;
606
607 @Override
608 protected void createOptionsGroupButtons(Group optionsGroup) {
609
610 // Overwrite checkbox
611 overwriteExistingResourcesCheckbox = new Button(optionsGroup, SWT.CHECK);
612 overwriteExistingResourcesCheckbox.setFont(optionsGroup.getFont());
613 overwriteExistingResourcesCheckbox.setText(Messages.ImportTraceWizard_OverwriteExistingTrace);
614 overwriteExistingResourcesCheckbox.setSelection(false);
615
616 // Create links checkbox
617 createLinksInWorkspaceButton = new Button(optionsGroup, SWT.CHECK);
618 createLinksInWorkspaceButton.setFont(optionsGroup.getFont());
619 createLinksInWorkspaceButton.setText(Messages.ImportTraceWizard_CreateLinksInWorkspace);
620 createLinksInWorkspaceButton.setSelection(true);
621
622 createLinksInWorkspaceButton.addSelectionListener(new SelectionAdapter() {
623 @Override
624 public void widgetSelected(SelectionEvent e) {
625 updateWidgetEnablements();
626 }
627 });
628
629 updateWidgetEnablements();
630 }
631
632 // ------------------------------------------------------------------------
633 // Determine if the finish button can be enabled
634 // ------------------------------------------------------------------------
635
636 @Override
637 public boolean validateSourceGroup() {
638
639 File sourceDirectory = getSourceDirectory();
640 if (sourceDirectory == null) {
641 setMessage(Messages.ImportTraceWizard_SelectTraceSourceEmpty);
642 return false;
643 }
644
645 if (sourceConflictsWithDestination(new Path(sourceDirectory.getPath()))) {
646 setMessage(null);
647 setErrorMessage(getSourceConflictMessage());
648 return false;
649 }
650
651 List<FileSystemElement> resourcesToImport = getSelectedResources();
652 if (resourcesToImport.size() == 0) {
653 setMessage(null);
654 setErrorMessage(Messages.ImportTraceWizard_SelectTraceNoneSelected);
655 return false;
656 }
657
658 IContainer container = getSpecifiedContainer();
659 if (container != null && container.isVirtual()) {
660 if (Platform.getPreferencesService().getBoolean(Activator.PLUGIN_ID, ResourcesPlugin.PREF_DISABLE_LINKING, false, null)) {
661 setMessage(null);
662 setErrorMessage(Messages.ImportTraceWizard_CannotImportFilesUnderAVirtualFolder);
663 return false;
664 }
665 if (createLinksInWorkspaceButton == null || !createLinksInWorkspaceButton.getSelection()) {
666 setMessage(null);
667 setErrorMessage(Messages.ImportTraceWizard_HaveToCreateLinksUnderAVirtualFolder);
668 return false;
669 }
670 }
671
672 // Perform trace validation
673 String traceTypeName = fTraceTypes.getText();
674 String tokens[] = traceTypeName.split(SEPARATOR, 2);
675 if (tokens.length >= 2) {
676 String id = TmfTraceType.getInstance().getTraceTypeId(tokens[0], tokens[1]);
677 if (!TmfTraceType.getInstance().validateTrace(id, getSelectedResources())) {
678 setMessage(null);
679 setErrorMessage(Messages.ImportTraceWizard_TraceValidationFailed);
680 return false;
681 }
682 }
683 setErrorMessage(null);
684 return true;
685 }
686
687 // ------------------------------------------------------------------------
688 // Import the trace(s)
689 // ------------------------------------------------------------------------
690
691 /**
692 * Finish the import.
693 *
694 * @return <code>true</code> if successful else false
695 */
696 public boolean finish() {
697 // Ensure source is valid
698 File sourceDir = new File(getSourceDirectoryName());
699 if (!sourceDir.isDirectory()) {
700 setErrorMessage(Messages.ImportTraceWizard_InvalidTraceDirectory);
701 return false;
702 }
703
704 try {
705 sourceDir.getCanonicalPath();
706 } catch (IOException e) {
707 MessageDialog.openInformation(getContainer().getShell(), Messages.ImportTraceWizard_Information,
708 Messages.ImportTraceWizard_InvalidTraceDirectory);
709 return false;
710 }
711
712 // Save directory for next import operation
713 fRootDirectory = getSourceDirectoryName();
714
715 List<FileSystemElement> selectedResources = getSelectedResources();
716 Iterator<FileSystemElement> resources = selectedResources.iterator();
717
718 // Use a map to end up with unique resources (getSelectedResources() can
719 // return duplicates)
720 Map<String, File> fileSystemObjects = new HashMap<String, File>();
721 while (resources.hasNext()) {
722 File file = (File) resources.next().getFileSystemObject();
723 String key = file.getAbsolutePath();
724 fileSystemObjects.put(key, file);
725 }
726
727 if (fileSystemObjects.size() > 0) {
728 boolean ok = importResources(fileSystemObjects);
729 String traceBundle = null;
730 String traceTypeId = null;
731 String traceIcon = null;
732 String traceType = fTraceTypes.getText();
733 boolean traceTypeOK = false;
734 if (traceType.startsWith(TmfTraceType.CUSTOM_TXT_CATEGORY)) {
735 for (CustomTxtTraceDefinition def : CustomTxtTraceDefinition.loadAll()) {
736 if (traceType.equals(TmfTraceType.CUSTOM_TXT_CATEGORY + SEPARATOR + def.definitionName)) {
737 traceTypeOK = true;
738 traceBundle = Activator.getDefault().getBundle().getSymbolicName();
739 traceTypeId = CustomTxtTrace.class.getCanonicalName() + SEPARATOR + def.definitionName;
740 traceIcon = DEFAULT_TRACE_ICON_PATH;
741 break;
742 }
743 }
744 } else if (traceType.startsWith(TmfTraceType.CUSTOM_XML_CATEGORY)) {
745 for (CustomXmlTraceDefinition def : CustomXmlTraceDefinition.loadAll()) {
746 if (traceType.equals(TmfTraceType.CUSTOM_XML_CATEGORY + SEPARATOR + def.definitionName)) {
747 traceTypeOK = true;
748 traceBundle = Activator.getDefault().getBundle().getSymbolicName();
749 traceTypeId = CustomXmlTrace.class.getCanonicalName() + SEPARATOR + def.definitionName;
750 traceIcon = DEFAULT_TRACE_ICON_PATH;
751 break;
752 }
753 }
754 } else {
755 if (!traceType.equals("")) { //$NON-NLS-1$
756 // Trace type was selected
757 String temp[] = traceType.split(SEPARATOR, 2);
758 if (temp.length < 2) {
759 Activator.getDefault().logError("Error with trace type " + traceType); //$NON-NLS-1$
760 return false;
761 }
762 final String traceId = TmfTraceType.getInstance().getTraceTypeId(temp[0], temp[1]);
763 if (traceId != null) {
764 if (!TmfTraceType.getInstance().validateTrace(traceId, getSelectedResources())) {
765 setMessage(null);
766 setErrorMessage(Messages.ImportTraceWizard_TraceValidationFailed);
767 return false;
768 }
769 } else {
770 setMessage(null);
771 setErrorMessage(Messages.ImportTraceWizard_TraceValidationFailed);
772 return false;
773 }
774 IConfigurationElement ce = TmfTraceType.getInstance().getTraceAttributes(traceId);
775 if (ce != null) {
776 traceTypeOK = true;
777 traceBundle = ce.getContributor().getName();
778 traceTypeId = ce.getAttribute(TmfTraceType.ID_ATTR);
779 traceIcon = ce.getAttribute(TmfTraceType.ICON_ATTR);
780 }
781 }
782 }
783 if (ok && traceTypeOK && !traceType.equals("")) { //$NON-NLS-1$
784 // Tag the selected traces with their type
785 List<String> files = new ArrayList<String>(fileSystemObjects.keySet());
786 Collections.sort(files, new Comparator<String>() {
787 @Override
788 public int compare(String o1, String o2) {
789 String v1 = o1 + File.separatorChar;
790 String v2 = o2 + File.separatorChar;
791 return v1.compareTo(v2);
792 }
793 });
794 // After sorting, traces correspond to the unique prefixes
795 String prefix = null;
796 for (int i = 0; i < files.size(); i++) {
797 File file = fileSystemObjects.get(files.get(i));
798 String name = file.getAbsolutePath() + File.separatorChar;
799 if (fTargetFolder != null && (prefix == null || !name.startsWith(prefix))) {
800 prefix = name; // new prefix
801 IResource resource = fTargetFolder.findMember(file.getName());
802 if (resource != null) {
803 try {
804 // Set the trace properties for this resource
805 resource.setPersistentProperty(TmfCommonConstants.TRACEBUNDLE, traceBundle);
806 resource.setPersistentProperty(TmfCommonConstants.TRACETYPE, traceTypeId);
807 resource.setPersistentProperty(TmfCommonConstants.TRACEICON, traceIcon);
808 TmfProjectElement tmfProject = TmfProjectRegistry.getProject(resource.getProject());
809 if (tmfProject != null) {
810 for (TmfTraceElement traceElement : tmfProject.getTracesFolder().getTraces()) {
811 if (traceElement.getName().equals(resource.getName())) {
812 traceElement.refreshTraceType();
813 break;
814 }
815 }
816 }
817 } catch (CoreException e) {
818 Activator.getDefault().logError("Error importing trace resource " + resource.getName(), e); //$NON-NLS-1$
819 }
820 }
821 }
822 }
823 }
824 return ok;
825 }
826
827 MessageDialog.openInformation(getContainer().getShell(), Messages.ImportTraceWizard_Information,
828 Messages.ImportTraceWizard_SelectTraceNoneSelected);
829 return false;
830 }
831
832 private boolean importResources(Map<String, File> fileSystemObjects) {
833
834 // Determine the sorted canonical list of items to import
835 List<File> fileList = new ArrayList<File>();
836 for (Entry<String, File> entry : fileSystemObjects.entrySet()) {
837 fileList.add(entry.getValue());
838 }
839 Collections.sort(fileList, new Comparator<File>() {
840 @Override
841 public int compare(File o1, File o2) {
842 String v1 = o1.getAbsolutePath() + File.separatorChar;
843 String v2 = o2.getAbsolutePath() + File.separatorChar;
844 return v1.compareTo(v2);
845 }
846 });
847
848 // Perform a distinct import operation for everything that has the same
849 // prefix
850 // (distinct prefixes correspond to traces - we don't want to re-create
851 // parent structures)
852 boolean ok = true;
853 boolean isLinked = createLinksInWorkspaceButton.getSelection();
854 for (int i = 0; i < fileList.size(); i++) {
855 File resource = fileList.get(i);
856 File parentFolder = new File(resource.getParent());
857
858 List<File> subList = new ArrayList<File>();
859 subList.add(resource);
860 if (resource.isDirectory()) {
861 String prefix = resource.getAbsolutePath() + File.separatorChar;
862 boolean hasSamePrefix = true;
863 for (int j = i + 1; j < fileList.size() && hasSamePrefix; j++) {
864 File res = fileList.get(j);
865 hasSamePrefix = res.getAbsolutePath().startsWith(prefix);
866 if (hasSamePrefix) {
867 // Import children individually if not linked
868 if (!isLinked) {
869 subList.add(res);
870 }
871 i = j;
872 }
873 }
874 }
875
876 // Perform the import operation for this subset
877 FileSystemStructureProvider fileSystemStructureProvider = FileSystemStructureProvider.INSTANCE;
878 ImportOperation operation = new ImportOperation(getContainerFullPath(), parentFolder, fileSystemStructureProvider, this,
879 subList);
880 operation.setContext(getShell());
881 ok = executeImportOperation(operation);
882 }
883
884 return ok;
885 }
886
887 private boolean executeImportOperation(ImportOperation op) {
888 initializeOperation(op);
889
890 try {
891 getContainer().run(true, true, op);
892 } catch (InterruptedException e) {
893 return false;
894 } catch (InvocationTargetException e) {
895 displayErrorDialog(e.getTargetException());
896 return false;
897 }
898
899 IStatus status = op.getStatus();
900 if (!status.isOK()) {
901 ErrorDialog.openError(getContainer().getShell(), Messages.ImportTraceWizard_ImportProblem, null, status);
902 return false;
903 }
904
905 return true;
906 }
907
908 private void initializeOperation(ImportOperation op) {
909 op.setCreateContainerStructure(false);
910 op.setOverwriteResources(overwriteExistingResourcesCheckbox.getSelection());
911 op.setCreateLinks(createLinksInWorkspaceButton.getSelection());
912 op.setVirtualFolders(false);
913 }
914
915 }
This page took 0.057836 seconds and 5 git commands to generate.