1 /*******************************************************************************
2 * Copyright (c) 2009, 2010, 2011, 2012 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 file hierarchy
94 * <li>A trace types dropbox for optional characterization
96 * For our purpose, a trace can either be a single file or a whole directory sub-tree, whichever is reached first from
101 * @author Francois Chouinard
103 public class ImportTraceWizardPage
extends WizardResourceImportPage
{
105 // ------------------------------------------------------------------------
107 // ------------------------------------------------------------------------
109 static private final String IMPORT_WIZARD_PAGE
= "ImportTraceWizardPage"; //$NON-NLS-1$
110 private static final String CUSTOM_TXT_CATEGORY
= "Custom Text"; //$NON-NLS-1$
111 private static final String CUSTOM_XML_CATEGORY
= "Custom XML"; //$NON-NLS-1$
112 private static final String DEFAULT_TRACE_ICON_PATH
= "icons/elcl16/trace.gif"; //$NON-NLS-1$
114 // ------------------------------------------------------------------------
116 // ------------------------------------------------------------------------
118 // Folder navigation start point (saved between invocations)
119 private static String fRootDirectory
= null;
121 // Navigation folder content viewer and selector
122 private CheckboxTreeViewer fFolderViewer
;
124 // Parent tracing project
125 private IProject fProject
;
127 // Target import directory ('Traces' folder)
128 private IFolder fTargetFolder
;
130 // ------------------------------------------------------------------------
132 // ------------------------------------------------------------------------
136 * Creates the trace wizard page.
138 * @param name The name of the page.
139 * @param selection The current selection
141 protected ImportTraceWizardPage(String name
, IStructuredSelection selection
) {
142 super(name
, selection
);
147 * @param workbench The workbench reference.
148 * @param selection The current selection
150 public ImportTraceWizardPage(IWorkbench workbench
, IStructuredSelection selection
) {
151 this(IMPORT_WIZARD_PAGE
, selection
);
152 setTitle(Messages
.ImportTraceWizard_FileSystemTitle
);
153 setDescription(Messages
.ImportTraceWizard_ImportTrace
);
155 // Locate the target trace folder
156 IFolder traceFolder
= null;
157 Object element
= selection
.getFirstElement();
159 if (element
instanceof TmfTraceFolder
) {
160 TmfTraceFolder tmfTraceFolder
= (TmfTraceFolder
) element
;
161 fProject
= tmfTraceFolder
.getProject().getResource();
162 traceFolder
= tmfTraceFolder
.getResource();
163 } else if (element
instanceof IProject
) {
164 IProject project
= (IProject
) element
;
166 if (project
.hasNature(TmfProjectNature
.ID
)) {
167 traceFolder
= (IFolder
) project
.findMember(TmfTraceFolder
.TRACE_FOLDER_NAME
);
169 } catch (CoreException e
) {
173 // Set the target trace folder
174 if (traceFolder
!= null) {
175 fTargetFolder
= traceFolder
;
176 String path
= traceFolder
.getFullPath().toOSString();
177 setContainerFieldValue(path
);
181 // ------------------------------------------------------------------------
182 // WizardResourceImportPage
183 // ------------------------------------------------------------------------
186 * @see org.eclipse.ui.dialogs.WizardResourceImportPage#createControl(org.eclipse.swt.widgets.Composite)
189 public void createControl(Composite parent
) {
190 super.createControl(parent
);
191 // Restore last directory if applicable
192 if (fRootDirectory
!= null) {
193 directoryNameField
.setText(fRootDirectory
);
194 updateFromSourceField();
200 * @see org.eclipse.ui.dialogs.WizardResourceImportPage#createSourceGroup(org.eclipse.swt.widgets.Composite)
203 protected void createSourceGroup(Composite parent
) {
204 createDirectorySelectionGroup(parent
);
205 createFileSelectionGroup(parent
);
206 createTraceTypeGroup(parent
);
207 validateSourceGroup();
212 * @see org.eclipse.ui.dialogs.WizardResourceImportPage#createFileSelectionGroup(org.eclipse.swt.widgets.Composite)
215 protected void createFileSelectionGroup(Composite parent
) {
217 // This Composite is only used for widget alignment purposes
218 Composite composite
= new Composite(parent
, SWT
.NONE
);
219 composite
.setFont(parent
.getFont());
220 GridLayout layout
= new GridLayout();
221 composite
.setLayout(layout
);
222 composite
.setLayoutData(new GridData(GridData
.FILL_BOTH
));
224 final int PREFERRED_LIST_HEIGHT
= 150;
226 fFolderViewer
= new CheckboxTreeViewer(composite
, SWT
.BORDER
);
227 GridData data
= new GridData(GridData
.FILL_BOTH
);
228 data
.heightHint
= PREFERRED_LIST_HEIGHT
;
229 fFolderViewer
.getTree().setLayoutData(data
);
230 fFolderViewer
.getTree().setFont(parent
.getFont());
232 fFolderViewer
.setContentProvider(getFileProvider());
233 fFolderViewer
.setLabelProvider(new WorkbenchLabelProvider());
234 fFolderViewer
.addCheckStateListener(new ICheckStateListener() {
236 public void checkStateChanged(CheckStateChangedEvent event
) {
237 Object elem
= event
.getElement();
238 if (elem
instanceof FileSystemElement
) {
239 FileSystemElement element
= (FileSystemElement
) elem
;
240 if (fFolderViewer
.getGrayed(element
)) {
241 fFolderViewer
.setSubtreeChecked(element
, false);
242 fFolderViewer
.setGrayed(element
, false);
243 } else if (event
.getChecked()) {
244 fFolderViewer
.setSubtreeChecked(event
.getElement(), true);
246 fFolderViewer
.setParentsGrayed(element
, true);
247 if (!element
.isDirectory()) {
248 fFolderViewer
.setGrayed(element
, false);
251 updateWidgetEnablements();
259 * @see org.eclipse.ui.dialogs.WizardResourceImportPage#getFolderProvider()
262 protected ITreeContentProvider
getFolderProvider() {
268 * @see org.eclipse.ui.dialogs.WizardResourceImportPage#getFileProvider()
271 protected ITreeContentProvider
getFileProvider() {
272 return new WorkbenchContentProvider() {
274 public Object
[] getChildren(Object o
) {
275 if (o
instanceof FileSystemElement
) {
276 FileSystemElement element
= (FileSystemElement
) o
;
277 populateChildren(element
);
278 // For our purpose, we need folders + files
279 Object
[] folders
= element
.getFolders().getChildren();
280 Object
[] files
= element
.getFiles().getChildren();
282 List
<Object
> result
= new LinkedList
<Object
>();
283 for (Object folder
: folders
) {
286 for (Object file
: files
) {
290 return result
.toArray();
292 return new Object
[0];
297 private static void populateChildren(FileSystemElement parent
) {
298 // Do not re-populate if the job was done already...
299 FileSystemStructureProvider provider
= FileSystemStructureProvider
.INSTANCE
;
300 if (parent
.getFolders().size() == 0 && parent
.getFiles().size() == 0) {
301 Object fileSystemObject
= parent
.getFileSystemObject();
302 List
<?
> children
= provider
.getChildren(fileSystemObject
);
303 if (children
!= null) {
304 Iterator
<?
> iterator
= children
.iterator();
305 while (iterator
.hasNext()) {
306 Object child
= iterator
.next();
307 String label
= provider
.getLabel(child
);
308 FileSystemElement element
= new FileSystemElement(label
, parent
, provider
.isFolder(child
));
309 element
.setFileSystemObject(child
);
317 * @see org.eclipse.ui.dialogs.WizardResourceImportPage#getSelectedResources()
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
);
331 // ------------------------------------------------------------------------
332 // Directory Selection Group (forked WizardFileSystemResourceImportPage1)
333 // ------------------------------------------------------------------------
336 * The directory name field
338 protected Combo directoryNameField
;
340 * The directory browse button.
342 protected Button directoryBrowseButton
;
343 private boolean entryChanged
= false;
346 * creates the directory selection group.
347 * @param parent the parent composite
349 protected void createDirectorySelectionGroup(Composite parent
) {
351 Composite directoryContainerGroup
= new Composite(parent
, SWT
.NONE
);
352 GridLayout layout
= new GridLayout();
353 layout
.numColumns
= 3;
354 directoryContainerGroup
.setLayout(layout
);
355 directoryContainerGroup
.setFont(parent
.getFont());
356 directoryContainerGroup
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, false));
358 // Label ("Trace directory:")
359 Label groupLabel
= new Label(directoryContainerGroup
, SWT
.NONE
);
360 groupLabel
.setText(Messages
.ImportTraceWizard_DirectoryLocation
);
361 groupLabel
.setFont(parent
.getFont());
363 // Directory name entry field
364 directoryNameField
= new Combo(directoryContainerGroup
, SWT
.BORDER
);
365 GridData data
= new GridData(SWT
.FILL
, SWT
.FILL
, true, false);
366 data
.widthHint
= SIZING_TEXT_FIELD_WIDTH
;
367 directoryNameField
.setLayoutData(data
);
368 directoryNameField
.setFont(parent
.getFont());
370 directoryNameField
.addSelectionListener(new SelectionAdapter() {
372 public void widgetSelected(SelectionEvent e
) {
373 updateFromSourceField();
377 directoryNameField
.addKeyListener(new KeyListener() {
379 public void keyPressed(KeyEvent e
) {
380 // If there has been a key pressed then mark as dirty
382 if (e
.character
== SWT
.CR
) { // Windows...
383 entryChanged
= false;
384 updateFromSourceField();
389 public void keyReleased(KeyEvent e
) {
393 directoryNameField
.addFocusListener(new FocusListener() {
395 public void focusGained(FocusEvent e
) {
396 // Do nothing when getting focus
400 public void focusLost(FocusEvent e
) {
401 // Clear the flag to prevent constant update
403 entryChanged
= false;
404 updateFromSourceField();
410 directoryBrowseButton
= new Button(directoryContainerGroup
, SWT
.PUSH
);
411 directoryBrowseButton
.setText(Messages
.ImportTraceWizard_BrowseButton
);
412 directoryBrowseButton
.addListener(SWT
.Selection
, this);
413 directoryBrowseButton
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, false, false));
414 directoryBrowseButton
.setFont(parent
.getFont());
415 setButtonLayoutData(directoryBrowseButton
);
418 // ------------------------------------------------------------------------
419 // Browse for the source directory
420 // ------------------------------------------------------------------------
424 * @see org.eclipse.ui.dialogs.WizardResourceImportPage#handleEvent(org.eclipse.swt.widgets.Event)
427 public void handleEvent(Event event
) {
428 if (event
.widget
== directoryBrowseButton
) {
429 handleSourceDirectoryBrowseButtonPressed();
431 super.handleEvent(event
);
435 * Handle the button pressed event
437 protected void handleSourceDirectoryBrowseButtonPressed() {
438 String currentSource
= directoryNameField
.getText();
439 DirectoryDialog dialog
= new DirectoryDialog(directoryNameField
.getShell(), SWT
.SAVE
| SWT
.SHEET
);
440 dialog
.setText(Messages
.ImportTraceWizard_SelectTraceDirectoryTitle
);
441 dialog
.setMessage(Messages
.ImportTraceWizard_SelectTraceDirectoryMessage
);
442 dialog
.setFilterPath(getSourceDirectoryName(currentSource
));
444 String selectedDirectory
= dialog
.open();
445 if (selectedDirectory
!= null) {
446 // Just quit if the directory is not valid
447 if ((getSourceDirectory(selectedDirectory
) == null) || selectedDirectory
.equals(currentSource
)) {
450 // If it is valid then proceed to populate
451 setErrorMessage(null);
452 setSourceName(selectedDirectory
);
456 private File
getSourceDirectory() {
457 return getSourceDirectory(directoryNameField
.getText());
460 private static File
getSourceDirectory(String path
) {
461 File sourceDirectory
= new File(getSourceDirectoryName(path
));
462 if (!sourceDirectory
.exists() || !sourceDirectory
.isDirectory()) {
466 return sourceDirectory
;
469 private static String
getSourceDirectoryName(String sourceName
) {
470 IPath result
= new Path(sourceName
.trim());
471 if (result
.getDevice() != null && result
.segmentCount() == 0) {
472 result
= result
.addTrailingSeparator();
474 result
= result
.removeTrailingSeparator();
476 return result
.toOSString();
479 private String
getSourceDirectoryName() {
480 return getSourceDirectoryName(directoryNameField
.getText());
483 private void updateFromSourceField() {
484 setSourceName(directoryNameField
.getText());
485 updateWidgetEnablements();
488 private void setSourceName(String path
) {
489 if (path
.length() > 0) {
490 String
[] currentItems
= directoryNameField
.getItems();
491 int selectionIndex
= -1;
492 for (int i
= 0; i
< currentItems
.length
; i
++) {
493 if (currentItems
[i
].equals(path
)) {
497 if (selectionIndex
< 0) {
498 int oldLength
= currentItems
.length
;
499 String
[] newItems
= new String
[oldLength
+ 1];
500 System
.arraycopy(currentItems
, 0, newItems
, 0, oldLength
);
501 newItems
[oldLength
] = path
;
502 directoryNameField
.setItems(newItems
);
503 selectionIndex
= oldLength
;
505 directoryNameField
.select(selectionIndex
);
510 // ------------------------------------------------------------------------
511 // File Selection Group (forked WizardFileSystemResourceImportPage1)
512 // ------------------------------------------------------------------------
514 private void resetSelection() {
515 FileSystemElement root
= getFileSystemTree();
516 populateListViewer(root
);
519 private void populateListViewer(final Object treeElement
) {
520 fFolderViewer
.setInput(treeElement
);
523 private FileSystemElement
getFileSystemTree() {
524 File sourceDirectory
= getSourceDirectory();
525 if (sourceDirectory
== null) {
528 return selectFiles(sourceDirectory
, FileSystemStructureProvider
.INSTANCE
);
531 private FileSystemElement
selectFiles(final Object rootFileSystemObject
, final IImportStructureProvider structureProvider
) {
532 final FileSystemElement
[] results
= new FileSystemElement
[1];
533 BusyIndicator
.showWhile(getShell().getDisplay(), new Runnable() {
536 // Create the root element from the supplied file system object
537 results
[0] = createRootElement(rootFileSystemObject
, structureProvider
);
543 private static FileSystemElement
createRootElement(Object fileSystemObject
,
544 IImportStructureProvider provider
) {
546 boolean isContainer
= provider
.isFolder(fileSystemObject
);
547 String elementLabel
= provider
.getLabel(fileSystemObject
);
549 FileSystemElement dummyParent
= new FileSystemElement("", null, true); //$NON-NLS-1$
550 FileSystemElement element
= new FileSystemElement(elementLabel
, dummyParent
, isContainer
);
551 element
.setFileSystemObject(fileSystemObject
);
553 // Get the first level
554 populateChildren(element
);
559 // ------------------------------------------------------------------------
561 // ------------------------------------------------------------------------
563 private Combo fTraceTypes
;
565 private final void createTraceTypeGroup(Composite parent
) {
566 Composite composite
= new Composite(parent
, SWT
.NONE
);
567 GridLayout layout
= new GridLayout();
568 layout
.numColumns
= 3;
569 layout
.makeColumnsEqualWidth
= false;
570 composite
.setLayout(layout
);
571 composite
.setFont(parent
.getFont());
572 GridData buttonData
= new GridData(SWT
.FILL
, SWT
.FILL
, true, false);
573 composite
.setLayoutData(buttonData
);
575 // Trace type label ("Trace Type:")
576 Label typeLabel
= new Label(composite
, SWT
.NONE
);
577 typeLabel
.setText(Messages
.ImportTraceWizard_TraceType
);
578 typeLabel
.setFont(parent
.getFont());
581 fTraceTypes
= new Combo(composite
, SWT
.BORDER
);
582 GridData data
= new GridData(SWT
.FILL
, SWT
.FILL
, true, false, 2, 1);
583 fTraceTypes
.setLayoutData(data
);
584 fTraceTypes
.setFont(parent
.getFont());
586 String
[] availableTraceTypes
= getAvailableTraceTypes();
587 fTraceTypes
.setItems(availableTraceTypes
);
589 fTraceTypes
.addSelectionListener(new SelectionListener() {
591 public void widgetSelected(SelectionEvent e
) {
592 updateWidgetEnablements();
596 public void widgetDefaultSelected(SelectionEvent e
) {
601 // The mapping of available trace type IDs to their corresponding configuration element
602 private final Map
<String
, IConfigurationElement
> fTraceTypeAttributes
= new HashMap
<String
, IConfigurationElement
>();
603 private final Map
<String
, IConfigurationElement
> fTraceCategories
= new HashMap
<String
, IConfigurationElement
>();
604 private final Map
<String
, IConfigurationElement
> fTraceAttributes
= new HashMap
<String
, IConfigurationElement
>();
606 private String
[] getAvailableTraceTypes() {
608 // Populate the Categories and Trace Types
609 IConfigurationElement
[] config
= Platform
.getExtensionRegistry().getConfigurationElementsFor(TmfTraceType
.TMF_TRACE_TYPE_ID
);
610 for (IConfigurationElement ce
: config
) {
611 String elementName
= ce
.getName();
612 if (elementName
.equals(TmfTraceType
.TYPE_ELEM
)) {
613 String traceTypeId
= ce
.getAttribute(TmfTraceType
.ID_ATTR
);
614 fTraceTypeAttributes
.put(traceTypeId
, ce
);
615 } else if (elementName
.equals(TmfTraceType
.CATEGORY_ELEM
)) {
616 String categoryId
= ce
.getAttribute(TmfTraceType
.ID_ATTR
);
617 fTraceCategories
.put(categoryId
, ce
);
621 // Generate the list of Category:TraceType to populate the ComboBox
622 List
<String
> traceTypes
= new ArrayList
<String
>();
623 for (String typeId
: fTraceTypeAttributes
.keySet()) {
624 IConfigurationElement ce
= fTraceTypeAttributes
.get(typeId
);
625 String traceTypeName
= getCategory(ce
) + " : " + ce
.getAttribute(TmfTraceType
.NAME_ATTR
); //$NON-NLS-1$
626 fTraceAttributes
.put(traceTypeName
, ce
);
627 traceTypes
.add(traceTypeName
);
629 Collections
.sort(traceTypes
);
631 // add the custom trace types
632 for (CustomTxtTraceDefinition def
: CustomTxtTraceDefinition
.loadAll()) {
633 String traceTypeName
= CUSTOM_TXT_CATEGORY
+ " : " + def
.definitionName
; //$NON-NLS-1$
634 traceTypes
.add(traceTypeName
);
636 for (CustomXmlTraceDefinition def
: CustomXmlTraceDefinition
.loadAll()) {
637 String traceTypeName
= CUSTOM_XML_CATEGORY
+ " : " + def
.definitionName
; //$NON-NLS-1$
638 traceTypes
.add(traceTypeName
);
642 return traceTypes
.toArray(new String
[traceTypes
.size()]);
645 private String
getCategory(IConfigurationElement ce
) {
646 String categoryId
= ce
.getAttribute(TmfTraceType
.CATEGORY_ATTR
);
647 if (categoryId
!= null) {
648 IConfigurationElement category
= fTraceCategories
.get(categoryId
);
649 if (category
!= null && !category
.getName().equals("")) { //$NON-NLS-1$
650 return category
.getAttribute(TmfTraceType
.NAME_ATTR
);
653 return "[no category]"; //$NON-NLS-1$
656 // ------------------------------------------------------------------------
658 // ------------------------------------------------------------------------
660 private Button overwriteExistingResourcesCheckbox
;
661 private Button createLinksInWorkspaceButton
;
665 * @see org.eclipse.ui.dialogs.WizardDataTransferPage#createOptionsGroupButtons(org.eclipse.swt.widgets.Group)
668 protected void createOptionsGroupButtons(Group optionsGroup
) {
670 // Overwrite checkbox
671 overwriteExistingResourcesCheckbox
= new Button(optionsGroup
, SWT
.CHECK
);
672 overwriteExistingResourcesCheckbox
.setFont(optionsGroup
.getFont());
673 overwriteExistingResourcesCheckbox
.setText(Messages
.ImportTraceWizard_OverwriteExistingTrace
);
674 overwriteExistingResourcesCheckbox
.setSelection(false);
676 // Create links checkbox
677 createLinksInWorkspaceButton
= new Button(optionsGroup
, SWT
.CHECK
);
678 createLinksInWorkspaceButton
.setFont(optionsGroup
.getFont());
679 createLinksInWorkspaceButton
.setText(Messages
.ImportTraceWizard_CreateLinksInWorkspace
);
680 createLinksInWorkspaceButton
.setSelection(true);
682 createLinksInWorkspaceButton
.addSelectionListener(new SelectionAdapter() {
684 public void widgetSelected(SelectionEvent e
) {
685 updateWidgetEnablements();
689 updateWidgetEnablements();
692 // ------------------------------------------------------------------------
693 // Determine if the finish button can be enabled
694 // ------------------------------------------------------------------------
698 * @see org.eclipse.ui.dialogs.WizardDataTransferPage#validateSourceGroup()
701 public boolean validateSourceGroup() {
703 File sourceDirectory
= getSourceDirectory();
704 if (sourceDirectory
== null) {
705 setMessage(Messages
.ImportTraceWizard_SelectTraceSourceEmpty
);
709 if (sourceConflictsWithDestination(new Path(sourceDirectory
.getPath()))) {
711 setErrorMessage(getSourceConflictMessage());
715 List
<FileSystemElement
> resourcesToImport
= getSelectedResources();
716 if (resourcesToImport
.size() == 0) {
718 setErrorMessage(Messages
.ImportTraceWizard_SelectTraceNoneSelected
);
722 IContainer container
= getSpecifiedContainer();
723 if (container
!= null && container
.isVirtual()) {
724 if (Platform
.getPreferencesService().getBoolean(Activator
.PLUGIN_ID
, ResourcesPlugin
.PREF_DISABLE_LINKING
, false, null)) {
726 setErrorMessage(Messages
.ImportTraceWizard_CannotImportFilesUnderAVirtualFolder
);
729 if (createLinksInWorkspaceButton
== null || !createLinksInWorkspaceButton
.getSelection()) {
731 setErrorMessage(Messages
.ImportTraceWizard_HaveToCreateLinksUnderAVirtualFolder
);
736 // Perform trace validation
737 String traceTypeName
= fTraceTypes
.getText();
738 if (traceTypeName
!= null && !"".equals(traceTypeName
) && //$NON-NLS-1$
739 !traceTypeName
.startsWith(CUSTOM_TXT_CATEGORY
) && !traceTypeName
.startsWith(CUSTOM_XML_CATEGORY
)) {
741 List
<File
> traces
= isolateTraces();
742 for (File trace
: traces
) {
743 ITmfTrace tmfTrace
= null;
745 IConfigurationElement ce
= fTraceAttributes
.get(traceTypeName
);
746 tmfTrace
= (ITmfTrace
) ce
.createExecutableExtension(TmfTraceType
.TRACE_TYPE_ATTR
);
747 if (tmfTrace
!= null && !tmfTrace
.validate(fProject
, trace
.getAbsolutePath())) {
749 setErrorMessage(Messages
.ImportTraceWizard_TraceValidationFailed
);
753 } catch (CoreException e
) {
755 if (tmfTrace
!= null) {
762 setErrorMessage(null);
766 private List
<File
> isolateTraces() {
768 List
<File
> traces
= new ArrayList
<File
>();
771 List
<FileSystemElement
> selectedResources
= getSelectedResources();
772 Iterator
<FileSystemElement
> resources
= selectedResources
.iterator();
774 // Get the sorted list of unique entries
775 Map
<String
, File
> fileSystemObjects
= new HashMap
<String
, File
>();
776 while (resources
.hasNext()) {
777 File resource
= (File
) resources
.next().getFileSystemObject();
778 String key
= resource
.getAbsolutePath();
779 fileSystemObjects
.put(key
, resource
);
781 List
<String
> files
= new ArrayList
<String
>(fileSystemObjects
.keySet());
782 Collections
.sort(files
);
784 // After sorting, traces correspond to the unique prefixes
785 String prefix
= null;
786 for (int i
= 0; i
< files
.size(); i
++) {
787 File file
= fileSystemObjects
.get(files
.get(i
));
788 String name
= file
.getAbsolutePath();
789 if (prefix
== null || !name
.startsWith(prefix
)) {
790 prefix
= name
; // new prefix
798 // ------------------------------------------------------------------------
799 // Import the trace(s)
800 // ------------------------------------------------------------------------
804 * @return <code>true</code> if successful else false
806 public boolean finish() {
807 // Ensure source is valid
808 File sourceDir
= new File(getSourceDirectoryName());
809 if (!sourceDir
.isDirectory()) {
810 setErrorMessage(Messages
.ImportTraceWizard_InvalidTraceDirectory
);
814 String sourceDirPath
;
816 sourceDirPath
= sourceDir
.getCanonicalPath();
817 } catch (IOException e
) {
818 MessageDialog
.openInformation(getContainer().getShell(), Messages
.ImportTraceWizard_Information
,
819 Messages
.ImportTraceWizard_InvalidTraceDirectory
);
823 // Save directory for next import operation
824 fRootDirectory
= getSourceDirectoryName();
826 List
<FileSystemElement
> selectedResources
= getSelectedResources();
827 Iterator
<FileSystemElement
> resources
= selectedResources
.iterator();
829 // Use a map to end up with unique resources (getSelectedResources() can return duplicates)
830 Map
<String
, File
> fileSystemObjects
= new HashMap
<String
, File
>();
831 while (resources
.hasNext()) {
832 File file
= (File
) resources
.next().getFileSystemObject();
833 String key
= file
.getAbsolutePath();
834 fileSystemObjects
.put(key
, file
);
837 if (fileSystemObjects
.size() > 0) {
838 boolean ok
= importResources(sourceDirPath
, fileSystemObjects
);
839 String traceBundle
= null;
840 String traceTypeId
= null;
841 String traceIcon
= null;
842 String traceType
= fTraceTypes
.getText();
843 boolean traceTypeOK
= false;
844 if (traceType
.startsWith(CUSTOM_TXT_CATEGORY
)) {
845 for (CustomTxtTraceDefinition def
: CustomTxtTraceDefinition
.loadAll()) {
846 if (traceType
.equals(CUSTOM_TXT_CATEGORY
+ " : " + def
.definitionName
)) { //$NON-NLS-1$
848 traceBundle
= Activator
.getDefault().getBundle().getSymbolicName();
849 traceTypeId
= CustomTxtTrace
.class.getCanonicalName() + ":" + def
.definitionName
; //$NON-NLS-1$
850 traceIcon
= DEFAULT_TRACE_ICON_PATH
;
854 } else if (traceType
.startsWith(CUSTOM_XML_CATEGORY
)) {
855 for (CustomXmlTraceDefinition def
: CustomXmlTraceDefinition
.loadAll()) {
856 if (traceType
.equals(CUSTOM_XML_CATEGORY
+ " : " + def
.definitionName
)) { //$NON-NLS-1$
858 traceBundle
= Activator
.getDefault().getBundle().getSymbolicName();
859 traceTypeId
= CustomXmlTrace
.class.getCanonicalName() + ":" + def
.definitionName
; //$NON-NLS-1$
860 traceIcon
= DEFAULT_TRACE_ICON_PATH
;
865 IConfigurationElement ce
= fTraceAttributes
.get(traceType
);
868 traceBundle
= ce
.getContributor().getName();
869 traceTypeId
= ce
.getAttribute(TmfTraceType
.ID_ATTR
);
870 traceIcon
= ce
.getAttribute(TmfTraceType
.ICON_ATTR
);
873 if (ok
&& traceTypeOK
&& !traceType
.equals("")) { //$NON-NLS-1$
874 // Tag the selected traces with their type
875 List
<String
> files
= new ArrayList
<String
>(fileSystemObjects
.keySet());
876 Collections
.sort(files
, new Comparator
<String
>() {
878 public int compare(String o1
, String o2
) {
879 String v1
= o1
+ File
.separatorChar
;
880 String v2
= o2
+ File
.separatorChar
;
881 return v1
.compareTo(v2
);
884 // After sorting, traces correspond to the unique prefixes
885 String prefix
= null;
886 for (int i
= 0; i
< files
.size(); i
++) {
887 File file
= fileSystemObjects
.get(files
.get(i
));
888 String name
= file
.getAbsolutePath() + File
.separatorChar
;
889 if (fTargetFolder
!= null && (prefix
== null || !name
.startsWith(prefix
))) {
890 prefix
= name
; // new prefix
891 IResource resource
= fTargetFolder
.findMember(file
.getName());
892 if (resource
!= null) {
894 // Set the trace properties for this resource
895 resource
.setPersistentProperty(TmfCommonConstants
.TRACEBUNDLE
, traceBundle
);
896 resource
.setPersistentProperty(TmfCommonConstants
.TRACETYPE
, traceTypeId
);
897 resource
.setPersistentProperty(TmfCommonConstants
.TRACEICON
, traceIcon
);
898 TmfProjectElement tmfProject
= TmfProjectRegistry
.getProject(resource
.getProject());
899 if (tmfProject
!= null) {
900 for (TmfTraceElement traceElement
: tmfProject
.getTracesFolder().getTraces()) {
901 if (traceElement
.getName().equals(resource
.getName())) {
902 traceElement
.refreshTraceType();
907 } catch (CoreException e
) {
908 Activator
.getDefault().logError("Error importing trace resource " + resource
.getName(), e
); //$NON-NLS-1$
917 MessageDialog
.openInformation(getContainer().getShell(), Messages
.ImportTraceWizard_Information
,
918 Messages
.ImportTraceWizard_SelectTraceNoneSelected
);
922 private boolean importResources(String rootDirectory
, Map
<String
, File
> fileSystemObjects
) {
924 // Determine the sorted canonical list of items to import
925 List
<File
> fileList
= new ArrayList
<File
>();
926 for (Entry
<String
, File
> entry
: fileSystemObjects
.entrySet()) {
927 fileList
.add(entry
.getValue());
929 Collections
.sort(fileList
, new Comparator
<File
>() {
931 public int compare(File o1
, File o2
) {
932 String v1
= o1
.getAbsolutePath() + File
.separatorChar
;
933 String v2
= o2
.getAbsolutePath() + File
.separatorChar
;
934 return v1
.compareTo(v2
);
938 // Perform a distinct import operation for everything that has the same prefix
939 // (distinct prefixes correspond to traces - we don't want to re-create parent structures)
941 boolean isLinked
= createLinksInWorkspaceButton
.getSelection();
942 for (int i
= 0; i
< fileList
.size(); i
++) {
943 File resource
= fileList
.get(i
);
944 File parentFolder
= new File(resource
.getParent());
946 List
<File
> subList
= new ArrayList
<File
>();
947 subList
.add(resource
);
948 if (resource
.isDirectory()) {
949 String prefix
= resource
.getAbsolutePath() + File
.separatorChar
;
950 boolean hasSamePrefix
= true;
951 for (int j
= i
+ 1; j
< fileList
.size() && hasSamePrefix
; j
++) {
952 File res
= fileList
.get(j
);
953 hasSamePrefix
= res
.getAbsolutePath().startsWith(prefix
);
955 // Import children individually if not linked
964 // Perform the import operation for this subset
965 FileSystemStructureProvider fileSystemStructureProvider
= FileSystemStructureProvider
.INSTANCE
;
966 ImportOperation operation
= new ImportOperation(getContainerFullPath(), parentFolder
, fileSystemStructureProvider
, this,
968 operation
.setContext(getShell());
969 ok
= executeImportOperation(operation
);
975 private boolean executeImportOperation(ImportOperation op
) {
976 initializeOperation(op
);
979 getContainer().run(true, true, op
);
980 } catch (InterruptedException e
) {
982 } catch (InvocationTargetException e
) {
983 displayErrorDialog(e
.getTargetException());
987 IStatus status
= op
.getStatus();
988 if (!status
.isOK()) {
989 ErrorDialog
.openError(getContainer().getShell(), Messages
.ImportTraceWizard_ImportProblem
, null, status
);
996 private void initializeOperation(ImportOperation op
) {
997 op
.setCreateContainerStructure(false);
998 op
.setOverwriteResources(overwriteExistingResourcesCheckbox
.getSelection());
999 op
.setCreateLinks(createLinksInWorkspaceButton
.getSelection());
1000 op
.setVirtualFolders(false);