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