1 /*******************************************************************************
2 * Copyright (c) 2009, 2013 Ericsson and others.
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
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 *******************************************************************************/
16 package org
.eclipse
.linuxtools
.tmf
.ui
.project
.wizards
;
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
;
29 import java
.util
.Map
.Entry
;
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
;
90 * A variant of the standard resource import wizard with the following changes:
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
95 * <li>A trace types dropbox for optional characterization
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.
102 * @author Francois Chouinard
104 public class ImportTraceWizardPage
extends WizardResourceImportPage
{
106 // ------------------------------------------------------------------------
108 // ------------------------------------------------------------------------
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$
115 // ------------------------------------------------------------------------
117 // ------------------------------------------------------------------------
119 // Folder navigation start point (saved between invocations)
120 private static String fRootDirectory
= null;
122 // Navigation folder content viewer and selector
123 private CheckboxTreeViewer fFolderViewer
;
125 // Parent tracing project
126 private IProject fProject
;
128 // Target import directory ('Traces' folder)
129 private IFolder fTargetFolder
;
131 // ------------------------------------------------------------------------
133 // ------------------------------------------------------------------------
136 * Constructor. Creates the trace wizard page.
139 * The name of the page.
141 * The current selection
143 protected ImportTraceWizardPage(String name
, IStructuredSelection selection
) {
144 super(name
, selection
);
151 * The workbench reference.
153 * The current selection
155 public ImportTraceWizardPage(IWorkbench workbench
, IStructuredSelection selection
) {
156 this(IMPORT_WIZARD_PAGE
, selection
);
157 setTitle(Messages
.ImportTraceWizard_FileSystemTitle
);
158 setDescription(Messages
.ImportTraceWizard_ImportTrace
);
160 // Locate the target trace folder
161 IFolder traceFolder
= null;
162 Object element
= selection
.getFirstElement();
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
;
171 if (project
.hasNature(TmfProjectNature
.ID
)) {
172 traceFolder
= (IFolder
) project
.findMember(TmfTraceFolder
.TRACE_FOLDER_NAME
);
174 } catch (CoreException e
) {
178 // Set the target trace folder
179 if (traceFolder
!= null) {
180 fTargetFolder
= traceFolder
;
181 String path
= traceFolder
.getFullPath().toOSString();
182 setContainerFieldValue(path
);
186 // ------------------------------------------------------------------------
187 // WizardResourceImportPage
188 // ------------------------------------------------------------------------
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();
201 protected void createSourceGroup(Composite parent
) {
202 createDirectorySelectionGroup(parent
);
203 createFileSelectionGroup(parent
);
204 createTraceTypeGroup(parent
);
205 validateSourceGroup();
209 protected void createFileSelectionGroup(Composite parent
) {
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
));
218 final int PREFERRED_LIST_HEIGHT
= 150;
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());
226 fFolderViewer
.setContentProvider(getFileProvider());
227 fFolderViewer
.setLabelProvider(new WorkbenchLabelProvider());
228 fFolderViewer
.addCheckStateListener(new ICheckStateListener() {
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);
240 fFolderViewer
.setParentsGrayed(element
, true);
241 if (!element
.isDirectory()) {
242 fFolderViewer
.setGrayed(element
, false);
245 updateWidgetEnablements();
252 protected ITreeContentProvider
getFolderProvider() {
257 protected ITreeContentProvider
getFileProvider() {
258 return new WorkbenchContentProvider() {
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();
268 List
<Object
> result
= new LinkedList
<Object
>();
269 for (Object folder
: folders
) {
272 for (Object file
: files
) {
276 return result
.toArray();
278 return new Object
[0];
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
);
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
);
313 // ------------------------------------------------------------------------
314 // Directory Selection Group (forked WizardFileSystemResourceImportPage1)
315 // ------------------------------------------------------------------------
318 * The directory name field
320 protected Combo directoryNameField
;
322 * The directory browse button.
324 protected Button directoryBrowseButton
;
325 private boolean entryChanged
= false;
328 * creates the directory selection group.
331 * the parent composite
333 protected void createDirectorySelectionGroup(Composite parent
) {
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));
342 // Label ("Trace directory:")
343 Label groupLabel
= new Label(directoryContainerGroup
, SWT
.NONE
);
344 groupLabel
.setText(Messages
.ImportTraceWizard_DirectoryLocation
);
345 groupLabel
.setFont(parent
.getFont());
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());
354 directoryNameField
.addSelectionListener(new SelectionAdapter() {
356 public void widgetSelected(SelectionEvent e
) {
357 updateFromSourceField();
361 directoryNameField
.addKeyListener(new KeyListener() {
363 public void keyPressed(KeyEvent e
) {
364 // If there has been a key pressed then mark as dirty
366 if (e
.character
== SWT
.CR
) { // Windows...
367 entryChanged
= false;
368 updateFromSourceField();
373 public void keyReleased(KeyEvent e
) {
377 directoryNameField
.addFocusListener(new FocusListener() {
379 public void focusGained(FocusEvent e
) {
380 // Do nothing when getting focus
384 public void focusLost(FocusEvent e
) {
385 // Clear the flag to prevent constant update
387 entryChanged
= false;
388 updateFromSourceField();
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
);
402 // ------------------------------------------------------------------------
403 // Browse for the source directory
404 // ------------------------------------------------------------------------
407 public void handleEvent(Event event
) {
408 if (event
.widget
== directoryBrowseButton
) {
409 handleSourceDirectoryBrowseButtonPressed();
411 super.handleEvent(event
);
415 * Handle the button pressed event
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
));
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
)) {
430 // If it is valid then proceed to populate
431 setErrorMessage(null);
432 setSourceName(selectedDirectory
);
436 private File
getSourceDirectory() {
437 return getSourceDirectory(directoryNameField
.getText());
440 private static File
getSourceDirectory(String path
) {
441 File sourceDirectory
= new File(getSourceDirectoryName(path
));
442 if (!sourceDirectory
.exists() || !sourceDirectory
.isDirectory()) {
446 return sourceDirectory
;
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();
454 result
= result
.removeTrailingSeparator();
456 return result
.toOSString();
459 private String
getSourceDirectoryName() {
460 return getSourceDirectoryName(directoryNameField
.getText());
463 private void updateFromSourceField() {
464 setSourceName(directoryNameField
.getText());
465 updateWidgetEnablements();
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
)) {
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
;
485 directoryNameField
.select(selectionIndex
);
490 // ------------------------------------------------------------------------
491 // File Selection Group (forked WizardFileSystemResourceImportPage1)
492 // ------------------------------------------------------------------------
494 private void resetSelection() {
495 FileSystemElement root
= getFileSystemTree();
496 populateListViewer(root
);
499 private void populateListViewer(final Object treeElement
) {
500 fFolderViewer
.setInput(treeElement
);
503 private FileSystemElement
getFileSystemTree() {
504 File sourceDirectory
= getSourceDirectory();
505 if (sourceDirectory
== null) {
508 return selectFiles(sourceDirectory
, FileSystemStructureProvider
.INSTANCE
);
511 private FileSystemElement
selectFiles(final Object rootFileSystemObject
, final IImportStructureProvider structureProvider
) {
512 final FileSystemElement
[] results
= new FileSystemElement
[1];
513 BusyIndicator
.showWhile(getShell().getDisplay(), new Runnable() {
516 // Create the root element from the supplied file system object
517 results
[0] = createRootElement(rootFileSystemObject
, structureProvider
);
523 private static FileSystemElement
createRootElement(Object fileSystemObject
,
524 IImportStructureProvider provider
) {
526 boolean isContainer
= provider
.isFolder(fileSystemObject
);
527 String elementLabel
= provider
.getLabel(fileSystemObject
);
529 FileSystemElement dummyParent
= new FileSystemElement("", null, true); //$NON-NLS-1$
530 FileSystemElement element
= new FileSystemElement(elementLabel
, dummyParent
, isContainer
);
531 element
.setFileSystemObject(fileSystemObject
);
533 // Get the first level
534 populateChildren(element
);
539 // ------------------------------------------------------------------------
541 // ------------------------------------------------------------------------
543 private Combo fTraceTypes
;
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
);
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());
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());
566 String
[] availableTraceTypes
= getAvailableTraceTypes();
567 fTraceTypes
.setItems(availableTraceTypes
);
569 fTraceTypes
.addSelectionListener(new SelectionListener() {
571 public void widgetSelected(SelectionEvent e
) {
572 updateWidgetEnablements();
576 public void widgetDefaultSelected(SelectionEvent e
) {
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
>();
587 private String
[] getAvailableTraceTypes() {
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
);
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
);
610 Collections
.sort(traceTypes
);
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
);
617 for (CustomXmlTraceDefinition def
: CustomXmlTraceDefinition
.loadAll()) {
618 String traceTypeName
= CUSTOM_XML_CATEGORY
+ " : " + def
.definitionName
; //$NON-NLS-1$
619 traceTypes
.add(traceTypeName
);
623 return traceTypes
.toArray(new String
[traceTypes
.size()]);
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
);
634 return "[no category]"; //$NON-NLS-1$
637 // ------------------------------------------------------------------------
639 // ------------------------------------------------------------------------
641 private Button overwriteExistingResourcesCheckbox
;
642 private Button createLinksInWorkspaceButton
;
645 protected void createOptionsGroupButtons(Group optionsGroup
) {
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);
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);
659 createLinksInWorkspaceButton
.addSelectionListener(new SelectionAdapter() {
661 public void widgetSelected(SelectionEvent e
) {
662 updateWidgetEnablements();
666 updateWidgetEnablements();
669 // ------------------------------------------------------------------------
670 // Determine if the finish button can be enabled
671 // ------------------------------------------------------------------------
674 public boolean validateSourceGroup() {
676 File sourceDirectory
= getSourceDirectory();
677 if (sourceDirectory
== null) {
678 setMessage(Messages
.ImportTraceWizard_SelectTraceSourceEmpty
);
682 if (sourceConflictsWithDestination(new Path(sourceDirectory
.getPath()))) {
684 setErrorMessage(getSourceConflictMessage());
688 List
<FileSystemElement
> resourcesToImport
= getSelectedResources();
689 if (resourcesToImport
.size() == 0) {
691 setErrorMessage(Messages
.ImportTraceWizard_SelectTraceNoneSelected
);
695 IContainer container
= getSpecifiedContainer();
696 if (container
!= null && container
.isVirtual()) {
697 if (Platform
.getPreferencesService().getBoolean(Activator
.PLUGIN_ID
, ResourcesPlugin
.PREF_DISABLE_LINKING
, false, null)) {
699 setErrorMessage(Messages
.ImportTraceWizard_CannotImportFilesUnderAVirtualFolder
);
702 if (createLinksInWorkspaceButton
== null || !createLinksInWorkspaceButton
.getSelection()) {
704 setErrorMessage(Messages
.ImportTraceWizard_HaveToCreateLinksUnderAVirtualFolder
);
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
)) {
714 List
<File
> traces
= isolateTraces();
715 for (File trace
: traces
) {
716 ITmfTrace tmfTrace
= null;
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()) {
725 setErrorMessage(Messages
.ImportTraceWizard_TraceValidationFailed
);
730 } catch (CoreException e
) {
732 if (tmfTrace
!= null) {
739 setErrorMessage(null);
743 private List
<File
> isolateTraces() {
745 List
<File
> traces
= new ArrayList
<File
>();
748 List
<FileSystemElement
> selectedResources
= getSelectedResources();
749 Iterator
<FileSystemElement
> resources
= selectedResources
.iterator();
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
);
758 List
<String
> files
= new ArrayList
<String
>(fileSystemObjects
.keySet());
759 Collections
.sort(files
);
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
775 // ------------------------------------------------------------------------
776 // Import the trace(s)
777 // ------------------------------------------------------------------------
782 * @return <code>true</code> if successful else false
784 public boolean finish() {
785 // Ensure source is valid
786 File sourceDir
= new File(getSourceDirectoryName());
787 if (!sourceDir
.isDirectory()) {
788 setErrorMessage(Messages
.ImportTraceWizard_InvalidTraceDirectory
);
793 sourceDir
.getCanonicalPath();
794 } catch (IOException e
) {
795 MessageDialog
.openInformation(getContainer().getShell(), Messages
.ImportTraceWizard_Information
,
796 Messages
.ImportTraceWizard_InvalidTraceDirectory
);
800 // Save directory for next import operation
801 fRootDirectory
= getSourceDirectoryName();
803 List
<FileSystemElement
> selectedResources
= getSelectedResources();
804 Iterator
<FileSystemElement
> resources
= selectedResources
.iterator();
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
);
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$
826 traceBundle
= Activator
.getDefault().getBundle().getSymbolicName();
827 traceTypeId
= CustomTxtTrace
.class.getCanonicalName() + ":" + def
.definitionName
; //$NON-NLS-1$
828 traceIcon
= DEFAULT_TRACE_ICON_PATH
;
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$
836 traceBundle
= Activator
.getDefault().getBundle().getSymbolicName();
837 traceTypeId
= CustomXmlTrace
.class.getCanonicalName() + ":" + def
.definitionName
; //$NON-NLS-1$
838 traceIcon
= DEFAULT_TRACE_ICON_PATH
;
843 IConfigurationElement ce
= fTraceAttributes
.get(traceType
);
846 traceBundle
= ce
.getContributor().getName();
847 traceTypeId
= ce
.getAttribute(TmfTraceType
.ID_ATTR
);
848 traceIcon
= ce
.getAttribute(TmfTraceType
.ICON_ATTR
);
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
>() {
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
);
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) {
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();
885 } catch (CoreException e
) {
886 Activator
.getDefault().logError("Error importing trace resource " + resource
.getName(), e
); //$NON-NLS-1$
895 MessageDialog
.openInformation(getContainer().getShell(), Messages
.ImportTraceWizard_Information
,
896 Messages
.ImportTraceWizard_SelectTraceNoneSelected
);
900 private boolean importResources(Map
<String
, File
> fileSystemObjects
) {
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());
907 Collections
.sort(fileList
, new Comparator
<File
>() {
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
);
916 // Perform a distinct import operation for everything that has the same
918 // (distinct prefixes correspond to traces - we don't want to re-create
919 // parent structures)
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());
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
);
935 // Import children individually if not linked
944 // Perform the import operation for this subset
945 FileSystemStructureProvider fileSystemStructureProvider
= FileSystemStructureProvider
.INSTANCE
;
946 ImportOperation operation
= new ImportOperation(getContainerFullPath(), parentFolder
, fileSystemStructureProvider
, this,
948 operation
.setContext(getShell());
949 ok
= executeImportOperation(operation
);
955 private boolean executeImportOperation(ImportOperation op
) {
956 initializeOperation(op
);
959 getContainer().run(true, true, op
);
960 } catch (InterruptedException e
) {
962 } catch (InvocationTargetException e
) {
963 displayErrorDialog(e
.getTargetException());
967 IStatus status
= op
.getStatus();
968 if (!status
.isOK()) {
969 ErrorDialog
.openError(getContainer().getShell(), Messages
.ImportTraceWizard_ImportProblem
, null, status
);
976 private void initializeOperation(ImportOperation op
) {
977 op
.setCreateContainerStructure(false);
978 op
.setOverwriteResources(overwriteExistingResourcesCheckbox
.getSelection());
979 op
.setCreateLinks(createLinksInWorkspaceButton
.getSelection());
980 op
.setVirtualFolders(false);