1 /*******************************************************************************
2 * Copyright (c) 2009, 2010, 2011, 2012 Ericsson
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 *******************************************************************************/
15 package org
.eclipse
.linuxtools
.tmf
.ui
.project
.wizards
;
18 import java
.io
.IOException
;
19 import java
.lang
.reflect
.InvocationTargetException
;
20 import java
.util
.ArrayList
;
21 import java
.util
.Collections
;
22 import java
.util
.Comparator
;
23 import java
.util
.HashMap
;
24 import java
.util
.Iterator
;
25 import java
.util
.LinkedList
;
26 import java
.util
.List
;
28 import java
.util
.Map
.Entry
;
30 import org
.eclipse
.core
.resources
.IContainer
;
31 import org
.eclipse
.core
.resources
.IFolder
;
32 import org
.eclipse
.core
.resources
.IProject
;
33 import org
.eclipse
.core
.resources
.IResource
;
34 import org
.eclipse
.core
.resources
.ResourcesPlugin
;
35 import org
.eclipse
.core
.runtime
.CoreException
;
36 import org
.eclipse
.core
.runtime
.IConfigurationElement
;
37 import org
.eclipse
.core
.runtime
.IPath
;
38 import org
.eclipse
.core
.runtime
.IStatus
;
39 import org
.eclipse
.core
.runtime
.Path
;
40 import org
.eclipse
.core
.runtime
.Platform
;
41 import org
.eclipse
.jface
.dialogs
.ErrorDialog
;
42 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
43 import org
.eclipse
.jface
.viewers
.CheckStateChangedEvent
;
44 import org
.eclipse
.jface
.viewers
.CheckboxTreeViewer
;
45 import org
.eclipse
.jface
.viewers
.ICheckStateListener
;
46 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
47 import org
.eclipse
.jface
.viewers
.ITreeContentProvider
;
48 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.Activator
;
49 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.parsers
.custom
.CustomTxtTrace
;
50 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.parsers
.custom
.CustomTxtTraceDefinition
;
51 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.parsers
.custom
.CustomXmlTrace
;
52 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.parsers
.custom
.CustomXmlTraceDefinition
;
53 import org
.eclipse
.linuxtools
.tmf
.core
.TmfCommonConstants
;
54 import org
.eclipse
.linuxtools
.tmf
.core
.TmfProjectNature
;
55 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
56 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfProjectRegistry
;
57 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceElement
;
58 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceFolder
;
59 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceType
;
60 import org
.eclipse
.swt
.SWT
;
61 import org
.eclipse
.swt
.custom
.BusyIndicator
;
62 import org
.eclipse
.swt
.events
.FocusEvent
;
63 import org
.eclipse
.swt
.events
.FocusListener
;
64 import org
.eclipse
.swt
.events
.KeyEvent
;
65 import org
.eclipse
.swt
.events
.KeyListener
;
66 import org
.eclipse
.swt
.events
.SelectionAdapter
;
67 import org
.eclipse
.swt
.events
.SelectionEvent
;
68 import org
.eclipse
.swt
.events
.SelectionListener
;
69 import org
.eclipse
.swt
.layout
.GridData
;
70 import org
.eclipse
.swt
.layout
.GridLayout
;
71 import org
.eclipse
.swt
.widgets
.Button
;
72 import org
.eclipse
.swt
.widgets
.Combo
;
73 import org
.eclipse
.swt
.widgets
.Composite
;
74 import org
.eclipse
.swt
.widgets
.DirectoryDialog
;
75 import org
.eclipse
.swt
.widgets
.Event
;
76 import org
.eclipse
.swt
.widgets
.Group
;
77 import org
.eclipse
.swt
.widgets
.Label
;
78 import org
.eclipse
.ui
.IWorkbench
;
79 import org
.eclipse
.ui
.dialogs
.FileSystemElement
;
80 import org
.eclipse
.ui
.dialogs
.WizardResourceImportPage
;
81 import org
.eclipse
.ui
.model
.WorkbenchContentProvider
;
82 import org
.eclipse
.ui
.model
.WorkbenchLabelProvider
;
83 import org
.eclipse
.ui
.wizards
.datatransfer
.FileSystemStructureProvider
;
84 import org
.eclipse
.ui
.wizards
.datatransfer
.IImportStructureProvider
;
85 import org
.eclipse
.ui
.wizards
.datatransfer
.ImportOperation
;
88 * A variant of the standard resource import wizard with the following changes:
90 * <li>A folder/file combined checkbox tree viewer to select traces
91 * <li>Cherry-picking of traces in the file structure without re-creating the file hierarchy
92 * <li>A trace types dropbox for optional characterization
94 * For our purpose, a trace can either be a single file or a whole directory sub-tree, whichever is reached first from
99 * @author Francois Chouinard
101 public class ImportTraceWizardPage
extends WizardResourceImportPage
{
103 // ------------------------------------------------------------------------
105 // ------------------------------------------------------------------------
107 static private final String IMPORT_WIZARD_PAGE
= "ImportTraceWizardPage"; //$NON-NLS-1$
108 private static final String CUSTOM_TXT_CATEGORY
= "Custom Text"; //$NON-NLS-1$
109 private static final String CUSTOM_XML_CATEGORY
= "Custom XML"; //$NON-NLS-1$
110 private static final String DEFAULT_TRACE_ICON_PATH
= "icons/elcl16/trace.gif"; //$NON-NLS-1$
112 // ------------------------------------------------------------------------
114 // ------------------------------------------------------------------------
116 // Folder navigation start point (saved between invocations)
117 private static String fRootDirectory
= null;
119 // Navigation folder content viewer and selector
120 private CheckboxTreeViewer fFolderViewer
;
122 // Parent tracing project
123 private IProject fProject
;
125 // Target import directory ('Traces' folder)
126 private IFolder fTargetFolder
;
128 // ------------------------------------------------------------------------
130 // ------------------------------------------------------------------------
134 * Creates the trace wizard page.
136 * @param name The name of the page.
137 * @param selection The current selection
139 protected ImportTraceWizardPage(String name
, IStructuredSelection selection
) {
140 super(name
, selection
);
145 * @param workbench The workbench reference.
146 * @param selection The current selection
148 public ImportTraceWizardPage(IWorkbench workbench
, IStructuredSelection selection
) {
149 this(IMPORT_WIZARD_PAGE
, selection
);
150 setTitle(Messages
.ImportTraceWizard_FileSystemTitle
);
151 setDescription(Messages
.ImportTraceWizard_ImportTrace
);
153 // Locate the target trace folder
154 IFolder traceFolder
= null;
155 Object element
= selection
.getFirstElement();
157 if (element
instanceof TmfTraceFolder
) {
158 TmfTraceFolder tmfTraceFolder
= (TmfTraceFolder
) element
;
159 fProject
= tmfTraceFolder
.getProject().getResource();
160 traceFolder
= tmfTraceFolder
.getResource();
161 } else if (element
instanceof IProject
) {
162 IProject project
= (IProject
) element
;
164 if (project
.hasNature(TmfProjectNature
.ID
)) {
165 traceFolder
= (IFolder
) project
.findMember(TmfTraceFolder
.TRACE_FOLDER_NAME
);
167 } catch (CoreException e
) {
171 // Set the target trace folder
172 if (traceFolder
!= null) {
173 fTargetFolder
= traceFolder
;
174 String path
= traceFolder
.getFullPath().toOSString();
175 setContainerFieldValue(path
);
179 // ------------------------------------------------------------------------
180 // WizardResourceImportPage
181 // ------------------------------------------------------------------------
184 * @see org.eclipse.ui.dialogs.WizardResourceImportPage#createControl(org.eclipse.swt.widgets.Composite)
187 public void createControl(Composite parent
) {
188 super.createControl(parent
);
189 // Restore last directory if applicable
190 if (fRootDirectory
!= null) {
191 directoryNameField
.setText(fRootDirectory
);
192 updateFromSourceField();
198 * @see org.eclipse.ui.dialogs.WizardResourceImportPage#createSourceGroup(org.eclipse.swt.widgets.Composite)
201 protected void createSourceGroup(Composite parent
) {
202 createDirectorySelectionGroup(parent
);
203 createFileSelectionGroup(parent
);
204 createTraceTypeGroup(parent
);
205 validateSourceGroup();
210 * @see org.eclipse.ui.dialogs.WizardResourceImportPage#createFileSelectionGroup(org.eclipse.swt.widgets.Composite)
213 protected void createFileSelectionGroup(Composite parent
) {
215 // This Composite is only used for widget alignment purposes
216 Composite composite
= new Composite(parent
, SWT
.NONE
);
217 composite
.setFont(parent
.getFont());
218 GridLayout layout
= new GridLayout();
219 composite
.setLayout(layout
);
220 composite
.setLayoutData(new GridData(GridData
.FILL_BOTH
));
222 final int PREFERRED_LIST_HEIGHT
= 150;
224 fFolderViewer
= new CheckboxTreeViewer(composite
, SWT
.BORDER
);
225 GridData data
= new GridData(GridData
.FILL_BOTH
);
226 data
.heightHint
= PREFERRED_LIST_HEIGHT
;
227 fFolderViewer
.getTree().setLayoutData(data
);
228 fFolderViewer
.getTree().setFont(parent
.getFont());
230 fFolderViewer
.setContentProvider(getFileProvider());
231 fFolderViewer
.setLabelProvider(new WorkbenchLabelProvider());
232 fFolderViewer
.addCheckStateListener(new ICheckStateListener() {
234 public void checkStateChanged(CheckStateChangedEvent event
) {
235 Object elem
= event
.getElement();
236 if (elem
instanceof FileSystemElement
) {
237 FileSystemElement element
= (FileSystemElement
) elem
;
238 if (fFolderViewer
.getGrayed(element
)) {
239 fFolderViewer
.setSubtreeChecked(element
, false);
240 fFolderViewer
.setGrayed(element
, false);
241 } else if (event
.getChecked()) {
242 fFolderViewer
.setSubtreeChecked(event
.getElement(), true);
244 fFolderViewer
.setParentsGrayed(element
, true);
245 if (!element
.isDirectory()) {
246 fFolderViewer
.setGrayed(element
, false);
249 updateWidgetEnablements();
257 * @see org.eclipse.ui.dialogs.WizardResourceImportPage#getFolderProvider()
260 protected ITreeContentProvider
getFolderProvider() {
266 * @see org.eclipse.ui.dialogs.WizardResourceImportPage#getFileProvider()
269 protected ITreeContentProvider
getFileProvider() {
270 return new WorkbenchContentProvider() {
272 public Object
[] getChildren(Object o
) {
273 if (o
instanceof FileSystemElement
) {
274 FileSystemElement element
= (FileSystemElement
) o
;
275 populateChildren(element
);
276 // For our purpose, we need folders + files
277 Object
[] folders
= element
.getFolders().getChildren();
278 Object
[] files
= element
.getFiles().getChildren();
280 List
<Object
> result
= new LinkedList
<Object
>();
281 for (Object folder
: folders
) {
284 for (Object file
: files
) {
288 return result
.toArray();
290 return new Object
[0];
295 private static void populateChildren(FileSystemElement parent
) {
296 // Do not re-populate if the job was done already...
297 FileSystemStructureProvider provider
= FileSystemStructureProvider
.INSTANCE
;
298 if (parent
.getFolders().size() == 0 && parent
.getFiles().size() == 0) {
299 Object fileSystemObject
= parent
.getFileSystemObject();
300 List
<?
> children
= provider
.getChildren(fileSystemObject
);
301 if (children
!= null) {
302 Iterator
<?
> iterator
= children
.iterator();
303 while (iterator
.hasNext()) {
304 Object child
= iterator
.next();
305 String label
= provider
.getLabel(child
);
306 FileSystemElement element
= new FileSystemElement(label
, parent
, provider
.isFolder(child
));
307 element
.setFileSystemObject(child
);
315 * @see org.eclipse.ui.dialogs.WizardResourceImportPage#getSelectedResources()
318 protected List
<FileSystemElement
> getSelectedResources() {
319 List
<FileSystemElement
> resources
= new ArrayList
<FileSystemElement
>();
320 Object
[] checkedItems
= fFolderViewer
.getCheckedElements();
321 for (Object item
: checkedItems
) {
322 if (item
instanceof FileSystemElement
&& !fFolderViewer
.getGrayed(item
)) {
323 resources
.add((FileSystemElement
) item
);
329 // ------------------------------------------------------------------------
330 // Directory Selection Group (forked WizardFileSystemResourceImportPage1)
331 // ------------------------------------------------------------------------
334 * The directory name field
336 protected Combo directoryNameField
;
338 * The directory browse button.
340 protected Button directoryBrowseButton
;
341 private boolean entryChanged
= false;
344 * creates the directory selection group.
345 * @param parent the parent composite
347 protected void createDirectorySelectionGroup(Composite parent
) {
349 Composite directoryContainerGroup
= new Composite(parent
, SWT
.NONE
);
350 GridLayout layout
= new GridLayout();
351 layout
.numColumns
= 3;
352 directoryContainerGroup
.setLayout(layout
);
353 directoryContainerGroup
.setFont(parent
.getFont());
354 directoryContainerGroup
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, false));
356 // Label ("Trace directory:")
357 Label groupLabel
= new Label(directoryContainerGroup
, SWT
.NONE
);
358 groupLabel
.setText(Messages
.ImportTraceWizard_DirectoryLocation
);
359 groupLabel
.setFont(parent
.getFont());
361 // Directory name entry field
362 directoryNameField
= new Combo(directoryContainerGroup
, SWT
.BORDER
);
363 GridData data
= new GridData(SWT
.FILL
, SWT
.FILL
, true, false);
364 data
.widthHint
= SIZING_TEXT_FIELD_WIDTH
;
365 directoryNameField
.setLayoutData(data
);
366 directoryNameField
.setFont(parent
.getFont());
368 directoryNameField
.addSelectionListener(new SelectionAdapter() {
370 public void widgetSelected(SelectionEvent e
) {
371 updateFromSourceField();
375 directoryNameField
.addKeyListener(new KeyListener() {
377 public void keyPressed(KeyEvent e
) {
378 // If there has been a key pressed then mark as dirty
380 if (e
.character
== SWT
.CR
) { // Windows...
381 entryChanged
= false;
382 updateFromSourceField();
387 public void keyReleased(KeyEvent e
) {
391 directoryNameField
.addFocusListener(new FocusListener() {
393 public void focusGained(FocusEvent e
) {
394 // Do nothing when getting focus
398 public void focusLost(FocusEvent e
) {
399 // Clear the flag to prevent constant update
401 entryChanged
= false;
402 updateFromSourceField();
408 directoryBrowseButton
= new Button(directoryContainerGroup
, SWT
.PUSH
);
409 directoryBrowseButton
.setText(Messages
.ImportTraceWizard_BrowseButton
);
410 directoryBrowseButton
.addListener(SWT
.Selection
, this);
411 directoryBrowseButton
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, false, false));
412 directoryBrowseButton
.setFont(parent
.getFont());
413 setButtonLayoutData(directoryBrowseButton
);
416 // ------------------------------------------------------------------------
417 // Browse for the source directory
418 // ------------------------------------------------------------------------
422 * @see org.eclipse.ui.dialogs.WizardResourceImportPage#handleEvent(org.eclipse.swt.widgets.Event)
425 public void handleEvent(Event event
) {
426 if (event
.widget
== directoryBrowseButton
) {
427 handleSourceDirectoryBrowseButtonPressed();
429 super.handleEvent(event
);
433 * Handle the button pressed event
435 protected void handleSourceDirectoryBrowseButtonPressed() {
436 String currentSource
= directoryNameField
.getText();
437 DirectoryDialog dialog
= new DirectoryDialog(directoryNameField
.getShell(), SWT
.SAVE
| SWT
.SHEET
);
438 dialog
.setText(Messages
.ImportTraceWizard_SelectTraceDirectoryTitle
);
439 dialog
.setMessage(Messages
.ImportTraceWizard_SelectTraceDirectoryMessage
);
440 dialog
.setFilterPath(getSourceDirectoryName(currentSource
));
442 String selectedDirectory
= dialog
.open();
443 if (selectedDirectory
!= null) {
444 // Just quit if the directory is not valid
445 if ((getSourceDirectory(selectedDirectory
) == null) || selectedDirectory
.equals(currentSource
)) {
448 // If it is valid then proceed to populate
449 setErrorMessage(null);
450 setSourceName(selectedDirectory
);
454 private File
getSourceDirectory() {
455 return getSourceDirectory(directoryNameField
.getText());
458 private static File
getSourceDirectory(String path
) {
459 File sourceDirectory
= new File(getSourceDirectoryName(path
));
460 if (!sourceDirectory
.exists() || !sourceDirectory
.isDirectory()) {
464 return sourceDirectory
;
467 private static String
getSourceDirectoryName(String sourceName
) {
468 IPath result
= new Path(sourceName
.trim());
469 if (result
.getDevice() != null && result
.segmentCount() == 0) {
470 result
= result
.addTrailingSeparator();
472 result
= result
.removeTrailingSeparator();
474 return result
.toOSString();
477 private String
getSourceDirectoryName() {
478 return getSourceDirectoryName(directoryNameField
.getText());
481 private void updateFromSourceField() {
482 setSourceName(directoryNameField
.getText());
483 updateWidgetEnablements();
486 private void setSourceName(String path
) {
487 if (path
.length() > 0) {
488 String
[] currentItems
= directoryNameField
.getItems();
489 int selectionIndex
= -1;
490 for (int i
= 0; i
< currentItems
.length
; i
++) {
491 if (currentItems
[i
].equals(path
)) {
495 if (selectionIndex
< 0) {
496 int oldLength
= currentItems
.length
;
497 String
[] newItems
= new String
[oldLength
+ 1];
498 System
.arraycopy(currentItems
, 0, newItems
, 0, oldLength
);
499 newItems
[oldLength
] = path
;
500 directoryNameField
.setItems(newItems
);
501 selectionIndex
= oldLength
;
503 directoryNameField
.select(selectionIndex
);
508 // ------------------------------------------------------------------------
509 // File Selection Group (forked WizardFileSystemResourceImportPage1)
510 // ------------------------------------------------------------------------
512 private void resetSelection() {
513 FileSystemElement root
= getFileSystemTree();
514 populateListViewer(root
);
517 private void populateListViewer(final Object treeElement
) {
518 fFolderViewer
.setInput(treeElement
);
521 private FileSystemElement
getFileSystemTree() {
522 File sourceDirectory
= getSourceDirectory();
523 if (sourceDirectory
== null) {
526 return selectFiles(sourceDirectory
, FileSystemStructureProvider
.INSTANCE
);
529 private FileSystemElement
selectFiles(final Object rootFileSystemObject
, final IImportStructureProvider structureProvider
) {
530 final FileSystemElement
[] results
= new FileSystemElement
[1];
531 BusyIndicator
.showWhile(getShell().getDisplay(), new Runnable() {
534 // Create the root element from the supplied file system object
535 results
[0] = createRootElement(rootFileSystemObject
, structureProvider
);
541 private static FileSystemElement
createRootElement(Object fileSystemObject
,
542 IImportStructureProvider provider
) {
544 boolean isContainer
= provider
.isFolder(fileSystemObject
);
545 String elementLabel
= provider
.getLabel(fileSystemObject
);
547 FileSystemElement dummyParent
= new FileSystemElement("", null, true); //$NON-NLS-1$
548 FileSystemElement element
= new FileSystemElement(elementLabel
, dummyParent
, isContainer
);
549 element
.setFileSystemObject(fileSystemObject
);
551 // Get the first level
552 populateChildren(element
);
557 // ------------------------------------------------------------------------
559 // ------------------------------------------------------------------------
561 private Combo fTraceTypes
;
563 private final void createTraceTypeGroup(Composite parent
) {
564 Composite composite
= new Composite(parent
, SWT
.NONE
);
565 GridLayout layout
= new GridLayout();
566 layout
.numColumns
= 3;
567 layout
.makeColumnsEqualWidth
= false;
568 composite
.setLayout(layout
);
569 composite
.setFont(parent
.getFont());
570 GridData buttonData
= new GridData(SWT
.FILL
, SWT
.FILL
, true, false);
571 composite
.setLayoutData(buttonData
);
573 // Trace type label ("Trace Type:")
574 Label typeLabel
= new Label(composite
, SWT
.NONE
);
575 typeLabel
.setText(Messages
.ImportTraceWizard_TraceType
);
576 typeLabel
.setFont(parent
.getFont());
579 fTraceTypes
= new Combo(composite
, SWT
.BORDER
);
580 GridData data
= new GridData(SWT
.FILL
, SWT
.FILL
, true, false, 2, 1);
581 fTraceTypes
.setLayoutData(data
);
582 fTraceTypes
.setFont(parent
.getFont());
584 String
[] availableTraceTypes
= getAvailableTraceTypes();
585 fTraceTypes
.setItems(availableTraceTypes
);
587 fTraceTypes
.addSelectionListener(new SelectionListener() {
589 public void widgetSelected(SelectionEvent e
) {
590 updateWidgetEnablements();
594 public void widgetDefaultSelected(SelectionEvent e
) {
599 // The mapping of available trace type IDs to their corresponding configuration element
600 private final Map
<String
, IConfigurationElement
> fTraceTypeAttributes
= new HashMap
<String
, IConfigurationElement
>();
601 private final Map
<String
, IConfigurationElement
> fTraceCategories
= new HashMap
<String
, IConfigurationElement
>();
602 private final Map
<String
, IConfigurationElement
> fTraceAttributes
= new HashMap
<String
, IConfigurationElement
>();
604 private String
[] getAvailableTraceTypes() {
606 // Populate the Categories and Trace Types
607 IConfigurationElement
[] config
= Platform
.getExtensionRegistry().getConfigurationElementsFor(TmfTraceType
.TMF_TRACE_TYPE_ID
);
608 for (IConfigurationElement ce
: config
) {
609 String elementName
= ce
.getName();
610 if (elementName
.equals(TmfTraceType
.TYPE_ELEM
)) {
611 String traceTypeId
= ce
.getAttribute(TmfTraceType
.ID_ATTR
);
612 fTraceTypeAttributes
.put(traceTypeId
, ce
);
613 } else if (elementName
.equals(TmfTraceType
.CATEGORY_ELEM
)) {
614 String categoryId
= ce
.getAttribute(TmfTraceType
.ID_ATTR
);
615 fTraceCategories
.put(categoryId
, ce
);
619 // Generate the list of Category:TraceType to populate the ComboBox
620 List
<String
> traceTypes
= new ArrayList
<String
>();
621 for (String typeId
: fTraceTypeAttributes
.keySet()) {
622 IConfigurationElement ce
= fTraceTypeAttributes
.get(typeId
);
623 String traceTypeName
= getCategory(ce
) + " : " + ce
.getAttribute(TmfTraceType
.NAME_ATTR
); //$NON-NLS-1$
624 fTraceAttributes
.put(traceTypeName
, ce
);
625 traceTypes
.add(traceTypeName
);
627 Collections
.sort(traceTypes
);
629 // add the custom trace types
630 for (CustomTxtTraceDefinition def
: CustomTxtTraceDefinition
.loadAll()) {
631 String traceTypeName
= CUSTOM_TXT_CATEGORY
+ " : " + def
.definitionName
; //$NON-NLS-1$
632 traceTypes
.add(traceTypeName
);
634 for (CustomXmlTraceDefinition def
: CustomXmlTraceDefinition
.loadAll()) {
635 String traceTypeName
= CUSTOM_XML_CATEGORY
+ " : " + def
.definitionName
; //$NON-NLS-1$
636 traceTypes
.add(traceTypeName
);
640 return traceTypes
.toArray(new String
[traceTypes
.size()]);
643 private String
getCategory(IConfigurationElement ce
) {
644 String categoryId
= ce
.getAttribute(TmfTraceType
.CATEGORY_ATTR
);
645 if (categoryId
!= null) {
646 IConfigurationElement category
= fTraceCategories
.get(categoryId
);
647 if (category
!= null && !category
.getName().equals("")) { //$NON-NLS-1$
648 return category
.getAttribute(TmfTraceType
.NAME_ATTR
);
651 return "[no category]"; //$NON-NLS-1$
654 // ------------------------------------------------------------------------
656 // ------------------------------------------------------------------------
658 private Button overwriteExistingResourcesCheckbox
;
659 private Button createLinksInWorkspaceButton
;
663 * @see org.eclipse.ui.dialogs.WizardDataTransferPage#createOptionsGroupButtons(org.eclipse.swt.widgets.Group)
666 protected void createOptionsGroupButtons(Group optionsGroup
) {
668 // Overwrite checkbox
669 overwriteExistingResourcesCheckbox
= new Button(optionsGroup
, SWT
.CHECK
);
670 overwriteExistingResourcesCheckbox
.setFont(optionsGroup
.getFont());
671 overwriteExistingResourcesCheckbox
.setText(Messages
.ImportTraceWizard_OverwriteExistingTrace
);
672 overwriteExistingResourcesCheckbox
.setSelection(false);
674 // Create links checkbox
675 createLinksInWorkspaceButton
= new Button(optionsGroup
, SWT
.CHECK
);
676 createLinksInWorkspaceButton
.setFont(optionsGroup
.getFont());
677 createLinksInWorkspaceButton
.setText(Messages
.ImportTraceWizard_CreateLinksInWorkspace
);
678 createLinksInWorkspaceButton
.setSelection(true);
680 createLinksInWorkspaceButton
.addSelectionListener(new SelectionAdapter() {
682 public void widgetSelected(SelectionEvent e
) {
683 updateWidgetEnablements();
687 updateWidgetEnablements();
690 // ------------------------------------------------------------------------
691 // Determine if the finish button can be enabled
692 // ------------------------------------------------------------------------
696 * @see org.eclipse.ui.dialogs.WizardDataTransferPage#validateSourceGroup()
699 public boolean validateSourceGroup() {
701 File sourceDirectory
= getSourceDirectory();
702 if (sourceDirectory
== null) {
703 setMessage(Messages
.ImportTraceWizard_SelectTraceSourceEmpty
);
707 if (sourceConflictsWithDestination(new Path(sourceDirectory
.getPath()))) {
709 setErrorMessage(getSourceConflictMessage());
713 List
<FileSystemElement
> resourcesToImport
= getSelectedResources();
714 if (resourcesToImport
.size() == 0) {
716 setErrorMessage(Messages
.ImportTraceWizard_SelectTraceNoneSelected
);
720 IContainer container
= getSpecifiedContainer();
721 if (container
!= null && container
.isVirtual()) {
722 if (Platform
.getPreferencesService().getBoolean(Activator
.PLUGIN_ID
, ResourcesPlugin
.PREF_DISABLE_LINKING
, false, null)) {
724 setErrorMessage(Messages
.ImportTraceWizard_CannotImportFilesUnderAVirtualFolder
);
727 if (createLinksInWorkspaceButton
== null || !createLinksInWorkspaceButton
.getSelection()) {
729 setErrorMessage(Messages
.ImportTraceWizard_HaveToCreateLinksUnderAVirtualFolder
);
734 // Perform trace validation
735 String traceTypeName
= fTraceTypes
.getText();
736 if (traceTypeName
!= null && !"".equals(traceTypeName
) && //$NON-NLS-1$
737 !traceTypeName
.startsWith(CUSTOM_TXT_CATEGORY
) && !traceTypeName
.startsWith(CUSTOM_XML_CATEGORY
)) {
739 List
<File
> traces
= isolateTraces();
740 for (File trace
: traces
) {
741 ITmfTrace
<?
> tmfTrace
= null;
743 IConfigurationElement ce
= fTraceAttributes
.get(traceTypeName
);
744 tmfTrace
= (ITmfTrace
<?
>) ce
.createExecutableExtension(TmfTraceType
.TRACE_TYPE_ATTR
);
745 if (tmfTrace
!= null && !tmfTrace
.validate(fProject
, trace
.getAbsolutePath())) {
747 setErrorMessage(Messages
.ImportTraceWizard_TraceValidationFailed
);
751 } catch (CoreException e
) {
753 if (tmfTrace
!= null) {
760 setErrorMessage(null);
764 private List
<File
> isolateTraces() {
766 List
<File
> traces
= new ArrayList
<File
>();
769 List
<FileSystemElement
> selectedResources
= getSelectedResources();
770 Iterator
<FileSystemElement
> resources
= selectedResources
.iterator();
772 // Get the sorted list of unique entries
773 Map
<String
, File
> fileSystemObjects
= new HashMap
<String
, File
>();
774 while (resources
.hasNext()) {
775 File resource
= (File
) resources
.next().getFileSystemObject();
776 String key
= resource
.getAbsolutePath();
777 fileSystemObjects
.put(key
, resource
);
779 List
<String
> files
= new ArrayList
<String
>(fileSystemObjects
.keySet());
780 Collections
.sort(files
);
782 // After sorting, traces correspond to the unique prefixes
783 String prefix
= null;
784 for (int i
= 0; i
< files
.size(); i
++) {
785 File file
= fileSystemObjects
.get(files
.get(i
));
786 String name
= file
.getAbsolutePath();
787 if (prefix
== null || !name
.startsWith(prefix
)) {
788 prefix
= name
; // new prefix
796 // ------------------------------------------------------------------------
797 // Import the trace(s)
798 // ------------------------------------------------------------------------
802 * @return <code>true</code> if successful else false
804 public boolean finish() {
805 // Ensure source is valid
806 File sourceDir
= new File(getSourceDirectoryName());
807 if (!sourceDir
.isDirectory()) {
808 setErrorMessage(Messages
.ImportTraceWizard_InvalidTraceDirectory
);
812 String sourceDirPath
;
814 sourceDirPath
= sourceDir
.getCanonicalPath();
815 } catch (IOException e
) {
816 MessageDialog
.openInformation(getContainer().getShell(), Messages
.ImportTraceWizard_Information
,
817 Messages
.ImportTraceWizard_InvalidTraceDirectory
);
821 // Save directory for next import operation
822 fRootDirectory
= getSourceDirectoryName();
824 List
<FileSystemElement
> selectedResources
= getSelectedResources();
825 Iterator
<FileSystemElement
> resources
= selectedResources
.iterator();
827 // Use a map to end up with unique resources (getSelectedResources() can return duplicates)
828 Map
<String
, File
> fileSystemObjects
= new HashMap
<String
, File
>();
829 while (resources
.hasNext()) {
830 File file
= (File
) resources
.next().getFileSystemObject();
831 String key
= file
.getAbsolutePath();
832 fileSystemObjects
.put(key
, file
);
835 if (fileSystemObjects
.size() > 0) {
836 boolean ok
= importResources(sourceDirPath
, fileSystemObjects
);
837 String traceBundle
= null;
838 String traceTypeId
= null;
839 String traceIcon
= null;
840 String traceType
= fTraceTypes
.getText();
841 boolean traceTypeOK
= false;
842 if (traceType
.startsWith(CUSTOM_TXT_CATEGORY
)) {
843 for (CustomTxtTraceDefinition def
: CustomTxtTraceDefinition
.loadAll()) {
844 if (traceType
.equals(CUSTOM_TXT_CATEGORY
+ " : " + def
.definitionName
)) { //$NON-NLS-1$
846 traceBundle
= Activator
.getDefault().getBundle().getSymbolicName();
847 traceTypeId
= CustomTxtTrace
.class.getCanonicalName() + ":" + def
.definitionName
; //$NON-NLS-1$
848 traceIcon
= DEFAULT_TRACE_ICON_PATH
;
852 } else if (traceType
.startsWith(CUSTOM_XML_CATEGORY
)) {
853 for (CustomXmlTraceDefinition def
: CustomXmlTraceDefinition
.loadAll()) {
854 if (traceType
.equals(CUSTOM_XML_CATEGORY
+ " : " + def
.definitionName
)) { //$NON-NLS-1$
856 traceBundle
= Activator
.getDefault().getBundle().getSymbolicName();
857 traceTypeId
= CustomXmlTrace
.class.getCanonicalName() + ":" + def
.definitionName
; //$NON-NLS-1$
858 traceIcon
= DEFAULT_TRACE_ICON_PATH
;
863 IConfigurationElement ce
= fTraceAttributes
.get(traceType
);
866 traceBundle
= ce
.getContributor().getName();
867 traceTypeId
= ce
.getAttribute(TmfTraceType
.ID_ATTR
);
868 traceIcon
= ce
.getAttribute(TmfTraceType
.ICON_ATTR
);
871 if (ok
&& traceTypeOK
&& !traceType
.equals("")) { //$NON-NLS-1$
872 // Tag the selected traces with their type
873 List
<String
> files
= new ArrayList
<String
>(fileSystemObjects
.keySet());
874 Collections
.sort(files
, new Comparator
<String
>() {
876 public int compare(String o1
, String o2
) {
877 String v1
= o1
+ File
.separatorChar
;
878 String v2
= o2
+ File
.separatorChar
;
879 return v1
.compareTo(v2
);
882 // After sorting, traces correspond to the unique prefixes
883 String prefix
= null;
884 for (int i
= 0; i
< files
.size(); i
++) {
885 File file
= fileSystemObjects
.get(files
.get(i
));
886 String name
= file
.getAbsolutePath() + File
.separatorChar
;
887 if (fTargetFolder
!= null && (prefix
== null || !name
.startsWith(prefix
))) {
888 prefix
= name
; // new prefix
889 IResource resource
= fTargetFolder
.findMember(file
.getName());
890 if (resource
!= null) {
892 // Set the trace properties for this resource
893 resource
.setPersistentProperty(TmfCommonConstants
.TRACEBUNDLE
, traceBundle
);
894 resource
.setPersistentProperty(TmfCommonConstants
.TRACETYPE
, traceTypeId
);
895 resource
.setPersistentProperty(TmfCommonConstants
.TRACEICON
, traceIcon
);
896 for (TmfTraceElement traceElement
: TmfProjectRegistry
.getProject(resource
.getProject()).getTracesFolder().getTraces()) {
897 if (traceElement
.getName().equals(resource
.getName())) {
898 traceElement
.refreshTraceType();
902 } catch (CoreException e
) {
903 Activator
.getDefault().logError("Error importing trace resource " + resource
.getName(), e
); //$NON-NLS-1$
912 MessageDialog
.openInformation(getContainer().getShell(), Messages
.ImportTraceWizard_Information
,
913 Messages
.ImportTraceWizard_SelectTraceNoneSelected
);
917 private boolean importResources(String rootDirectory
, Map
<String
, File
> fileSystemObjects
) {
919 // Determine the sorted canonical list of items to import
920 List
<File
> fileList
= new ArrayList
<File
>();
921 for (Entry
<String
, File
> entry
: fileSystemObjects
.entrySet()) {
922 fileList
.add(entry
.getValue());
924 Collections
.sort(fileList
, new Comparator
<File
>() {
926 public int compare(File o1
, File o2
) {
927 String v1
= o1
.getAbsolutePath() + File
.separatorChar
;
928 String v2
= o2
.getAbsolutePath() + File
.separatorChar
;
929 return v1
.compareTo(v2
);
933 // Perform a distinct import operation for everything that has the same prefix
934 // (distinct prefixes correspond to traces - we don't want to re-create parent structures)
936 boolean isLinked
= createLinksInWorkspaceButton
.getSelection();
937 for (int i
= 0; i
< fileList
.size(); i
++) {
938 File resource
= fileList
.get(i
);
939 File parentFolder
= new File(resource
.getParent());
941 List
<File
> subList
= new ArrayList
<File
>();
942 subList
.add(resource
);
943 if (resource
.isDirectory()) {
944 String prefix
= resource
.getAbsolutePath() + File
.separatorChar
;
945 boolean hasSamePrefix
= true;
946 for (int j
= i
+ 1; j
< fileList
.size() && hasSamePrefix
; j
++) {
947 File res
= fileList
.get(j
);
948 hasSamePrefix
= res
.getAbsolutePath().startsWith(prefix
);
950 // Import children individually if not linked
959 // Perform the import operation for this subset
960 FileSystemStructureProvider fileSystemStructureProvider
= FileSystemStructureProvider
.INSTANCE
;
961 ImportOperation operation
= new ImportOperation(getContainerFullPath(), parentFolder
, fileSystemStructureProvider
, this,
963 operation
.setContext(getShell());
964 ok
= executeImportOperation(operation
);
970 private boolean executeImportOperation(ImportOperation op
) {
971 initializeOperation(op
);
974 getContainer().run(true, true, op
);
975 } catch (InterruptedException e
) {
977 } catch (InvocationTargetException e
) {
978 displayErrorDialog(e
.getTargetException());
982 IStatus status
= op
.getStatus();
983 if (!status
.isOK()) {
984 ErrorDialog
.openError(getContainer().getShell(), Messages
.ImportTraceWizard_ImportProblem
, null, status
);
991 private void initializeOperation(ImportOperation op
) {
992 op
.setCreateContainerStructure(false);
993 op
.setOverwriteResources(overwriteExistingResourcesCheckbox
.getSelection());
994 op
.setCreateLinks(createLinksInWorkspaceButton
.getSelection());
995 op
.setVirtualFolders(false);