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
.HashMap
;
23 import java
.util
.Iterator
;
24 import java
.util
.LinkedList
;
25 import java
.util
.List
;
27 import java
.util
.Map
.Entry
;
29 import org
.eclipse
.core
.resources
.IContainer
;
30 import org
.eclipse
.core
.resources
.IFolder
;
31 import org
.eclipse
.core
.resources
.IProject
;
32 import org
.eclipse
.core
.resources
.IResource
;
33 import org
.eclipse
.core
.resources
.ResourcesPlugin
;
34 import org
.eclipse
.core
.runtime
.CoreException
;
35 import org
.eclipse
.core
.runtime
.IConfigurationElement
;
36 import org
.eclipse
.core
.runtime
.IPath
;
37 import org
.eclipse
.core
.runtime
.IStatus
;
38 import org
.eclipse
.core
.runtime
.Path
;
39 import org
.eclipse
.core
.runtime
.Platform
;
40 import org
.eclipse
.jface
.dialogs
.ErrorDialog
;
41 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
42 import org
.eclipse
.jface
.viewers
.CheckStateChangedEvent
;
43 import org
.eclipse
.jface
.viewers
.CheckboxTreeViewer
;
44 import org
.eclipse
.jface
.viewers
.ICheckStateListener
;
45 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
46 import org
.eclipse
.jface
.viewers
.ITreeContentProvider
;
47 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.Activator
;
48 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.parsers
.custom
.CustomTxtTrace
;
49 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.parsers
.custom
.CustomTxtTraceDefinition
;
50 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.parsers
.custom
.CustomXmlTrace
;
51 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.parsers
.custom
.CustomXmlTraceDefinition
;
52 import org
.eclipse
.linuxtools
.tmf
.core
.TmfCommonConstants
;
53 import org
.eclipse
.linuxtools
.tmf
.core
.TmfProjectNature
;
54 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
55 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfProjectRegistry
;
56 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceElement
;
57 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceFolder
;
58 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceType
;
59 import org
.eclipse
.swt
.SWT
;
60 import org
.eclipse
.swt
.custom
.BusyIndicator
;
61 import org
.eclipse
.swt
.events
.FocusEvent
;
62 import org
.eclipse
.swt
.events
.FocusListener
;
63 import org
.eclipse
.swt
.events
.KeyEvent
;
64 import org
.eclipse
.swt
.events
.KeyListener
;
65 import org
.eclipse
.swt
.events
.SelectionAdapter
;
66 import org
.eclipse
.swt
.events
.SelectionEvent
;
67 import org
.eclipse
.swt
.events
.SelectionListener
;
68 import org
.eclipse
.swt
.layout
.GridData
;
69 import org
.eclipse
.swt
.layout
.GridLayout
;
70 import org
.eclipse
.swt
.widgets
.Button
;
71 import org
.eclipse
.swt
.widgets
.Combo
;
72 import org
.eclipse
.swt
.widgets
.Composite
;
73 import org
.eclipse
.swt
.widgets
.DirectoryDialog
;
74 import org
.eclipse
.swt
.widgets
.Event
;
75 import org
.eclipse
.swt
.widgets
.Group
;
76 import org
.eclipse
.swt
.widgets
.Label
;
77 import org
.eclipse
.swt
.widgets
.Listener
;
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
implements Listener
{
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
)
283 for (Object file
: files
)
286 return result
.toArray();
288 return new Object
[0];
293 private void populateChildren(FileSystemElement parent
) {
294 // Do not re-populate if the job was done already...
295 FileSystemStructureProvider provider
= FileSystemStructureProvider
.INSTANCE
;
296 if (parent
.getFolders().size() == 0 && parent
.getFiles().size() == 0) {
297 Object fileSystemObject
= parent
.getFileSystemObject();
298 List
<?
> children
= provider
.getChildren(fileSystemObject
);
299 if (children
!= null) {
300 Iterator
<?
> iterator
= children
.iterator();
301 while (iterator
.hasNext()) {
302 Object child
= iterator
.next();
303 String label
= provider
.getLabel(child
);
304 FileSystemElement element
= new FileSystemElement(label
, parent
, provider
.isFolder(child
));
305 element
.setFileSystemObject(child
);
313 * @see org.eclipse.ui.dialogs.WizardResourceImportPage#getSelectedResources()
316 protected List
<FileSystemElement
> getSelectedResources() {
317 List
<FileSystemElement
> resources
= new ArrayList
<FileSystemElement
>();
318 Object
[] checkedItems
= fFolderViewer
.getCheckedElements();
319 for (Object item
: checkedItems
) {
320 if (item
instanceof FileSystemElement
&& !fFolderViewer
.getGrayed(item
)) {
321 resources
.add((FileSystemElement
) item
);
327 // ------------------------------------------------------------------------
328 // Directory Selection Group (forked WizardFileSystemResourceImportPage1)
329 // ------------------------------------------------------------------------
332 * The directory name field
334 protected Combo directoryNameField
;
336 * The directory browse button.
338 protected Button directoryBrowseButton
;
339 private boolean entryChanged
= false;
342 * creates the directory selection group.
343 * @param parent the parent composite
345 protected void createDirectorySelectionGroup(Composite parent
) {
347 Composite directoryContainerGroup
= new Composite(parent
, SWT
.NONE
);
348 GridLayout layout
= new GridLayout();
349 layout
.numColumns
= 3;
350 directoryContainerGroup
.setLayout(layout
);
351 directoryContainerGroup
.setFont(parent
.getFont());
352 directoryContainerGroup
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, false));
354 // Label ("Trace directory:")
355 Label groupLabel
= new Label(directoryContainerGroup
, SWT
.NONE
);
356 groupLabel
.setText(Messages
.ImportTraceWizard_DirectoryLocation
);
357 groupLabel
.setFont(parent
.getFont());
359 // Directory name entry field
360 directoryNameField
= new Combo(directoryContainerGroup
, SWT
.BORDER
);
361 GridData data
= new GridData(SWT
.FILL
, SWT
.FILL
, true, false);
362 data
.widthHint
= SIZING_TEXT_FIELD_WIDTH
;
363 directoryNameField
.setLayoutData(data
);
364 directoryNameField
.setFont(parent
.getFont());
366 directoryNameField
.addSelectionListener(new SelectionAdapter() {
368 public void widgetSelected(SelectionEvent e
) {
369 updateFromSourceField();
373 directoryNameField
.addKeyListener(new KeyListener() {
375 public void keyPressed(KeyEvent e
) {
376 // If there has been a key pressed then mark as dirty
378 if (e
.character
== SWT
.CR
) { // Windows...
379 entryChanged
= false;
380 updateFromSourceField();
385 public void keyReleased(KeyEvent e
) {
389 directoryNameField
.addFocusListener(new FocusListener() {
391 public void focusGained(FocusEvent e
) {
392 // Do nothing when getting focus
396 public void focusLost(FocusEvent e
) {
397 // Clear the flag to prevent constant update
399 entryChanged
= false;
400 updateFromSourceField();
406 directoryBrowseButton
= new Button(directoryContainerGroup
, SWT
.PUSH
);
407 directoryBrowseButton
.setText(Messages
.ImportTraceWizard_BrowseButton
);
408 directoryBrowseButton
.addListener(SWT
.Selection
, this);
409 directoryBrowseButton
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, false, false));
410 directoryBrowseButton
.setFont(parent
.getFont());
411 setButtonLayoutData(directoryBrowseButton
);
414 // ------------------------------------------------------------------------
415 // Browse for the source directory
416 // ------------------------------------------------------------------------
420 * @see org.eclipse.ui.dialogs.WizardResourceImportPage#handleEvent(org.eclipse.swt.widgets.Event)
423 public void handleEvent(Event event
) {
424 if (event
.widget
== directoryBrowseButton
) {
425 handleSourceDirectoryBrowseButtonPressed();
427 super.handleEvent(event
);
431 * Handle the button pressed event
433 protected void handleSourceDirectoryBrowseButtonPressed() {
434 String currentSource
= directoryNameField
.getText();
435 DirectoryDialog dialog
= new DirectoryDialog(directoryNameField
.getShell(), SWT
.SAVE
| SWT
.SHEET
);
436 dialog
.setText(Messages
.ImportTraceWizard_SelectTraceDirectoryTitle
);
437 dialog
.setMessage(Messages
.ImportTraceWizard_SelectTraceDirectoryMessage
);
438 dialog
.setFilterPath(getSourceDirectoryName(currentSource
));
440 String selectedDirectory
= dialog
.open();
441 if (selectedDirectory
!= null) {
442 // Just quit if the directory is not valid
443 if ((getSourceDirectory(selectedDirectory
) == null) || selectedDirectory
.equals(currentSource
)) {
446 // If it is valid then proceed to populate
447 setErrorMessage(null);
448 setSourceName(selectedDirectory
);
452 private File
getSourceDirectory() {
453 return getSourceDirectory(directoryNameField
.getText());
456 private File
getSourceDirectory(String path
) {
457 File sourceDirectory
= new File(getSourceDirectoryName(path
));
458 if (!sourceDirectory
.exists() || !sourceDirectory
.isDirectory()) {
462 return sourceDirectory
;
465 private String
getSourceDirectoryName(String sourceName
) {
466 IPath result
= new Path(sourceName
.trim());
467 if (result
.getDevice() != null && result
.segmentCount() == 0) {
468 result
= result
.addTrailingSeparator();
470 result
= result
.removeTrailingSeparator();
472 return result
.toOSString();
475 private String
getSourceDirectoryName() {
476 return getSourceDirectoryName(directoryNameField
.getText());
479 private void updateFromSourceField() {
480 setSourceName(directoryNameField
.getText());
481 updateWidgetEnablements();
484 private void setSourceName(String path
) {
485 if (path
.length() > 0) {
486 String
[] currentItems
= directoryNameField
.getItems();
487 int selectionIndex
= -1;
488 for (int i
= 0; i
< currentItems
.length
; i
++) {
489 if (currentItems
[i
].equals(path
)) {
493 if (selectionIndex
< 0) {
494 int oldLength
= currentItems
.length
;
495 String
[] newItems
= new String
[oldLength
+ 1];
496 System
.arraycopy(currentItems
, 0, newItems
, 0, oldLength
);
497 newItems
[oldLength
] = path
;
498 directoryNameField
.setItems(newItems
);
499 selectionIndex
= oldLength
;
501 directoryNameField
.select(selectionIndex
);
506 // ------------------------------------------------------------------------
507 // File Selection Group (forked WizardFileSystemResourceImportPage1)
508 // ------------------------------------------------------------------------
510 private void resetSelection() {
511 FileSystemElement root
= getFileSystemTree();
512 populateListViewer(root
);
515 private void populateListViewer(final Object treeElement
) {
516 fFolderViewer
.setInput(treeElement
);
519 private FileSystemElement
getFileSystemTree() {
520 File sourceDirectory
= getSourceDirectory();
521 if (sourceDirectory
== null) {
524 return selectFiles(sourceDirectory
, FileSystemStructureProvider
.INSTANCE
);
527 private FileSystemElement
selectFiles(final Object rootFileSystemObject
, final IImportStructureProvider structureProvider
) {
528 final FileSystemElement
[] results
= new FileSystemElement
[1];
529 BusyIndicator
.showWhile(getShell().getDisplay(), new Runnable() {
532 // Create the root element from the supplied file system object
533 results
[0] = createRootElement(rootFileSystemObject
, structureProvider
);
539 private FileSystemElement
createRootElement(Object fileSystemObject
, IImportStructureProvider provider
) {
541 boolean isContainer
= provider
.isFolder(fileSystemObject
);
542 String elementLabel
= provider
.getLabel(fileSystemObject
);
544 FileSystemElement dummyParent
= new FileSystemElement("", null, true); //$NON-NLS-1$
545 FileSystemElement element
= new FileSystemElement(elementLabel
, dummyParent
, isContainer
);
546 element
.setFileSystemObject(fileSystemObject
);
548 // Get the first level
549 populateChildren(element
);
554 // ------------------------------------------------------------------------
556 // ------------------------------------------------------------------------
558 private Combo fTraceTypes
;
560 private final void createTraceTypeGroup(Composite parent
) {
561 Composite composite
= new Composite(parent
, SWT
.NONE
);
562 GridLayout layout
= new GridLayout();
563 layout
.numColumns
= 3;
564 layout
.makeColumnsEqualWidth
= false;
565 composite
.setLayout(layout
);
566 composite
.setFont(parent
.getFont());
567 GridData buttonData
= new GridData(SWT
.FILL
, SWT
.FILL
, true, false);
568 composite
.setLayoutData(buttonData
);
570 // Trace type label ("Trace Type:")
571 Label typeLabel
= new Label(composite
, SWT
.NONE
);
572 typeLabel
.setText(Messages
.ImportTraceWizard_TraceType
);
573 typeLabel
.setFont(parent
.getFont());
576 fTraceTypes
= new Combo(composite
, SWT
.BORDER
);
577 GridData data
= new GridData(SWT
.FILL
, SWT
.FILL
, true, false, 2, 1);
578 fTraceTypes
.setLayoutData(data
);
579 fTraceTypes
.setFont(parent
.getFont());
581 String
[] availableTraceTypes
= getAvailableTraceTypes();
582 fTraceTypes
.setItems(availableTraceTypes
);
584 fTraceTypes
.addSelectionListener(new SelectionListener() {
586 public void widgetSelected(SelectionEvent e
) {
587 validateSourceGroup();
591 public void widgetDefaultSelected(SelectionEvent e
) {
596 // The mapping of available trace type IDs to their corresponding configuration element
597 private Map
<String
, IConfigurationElement
> fTraceTypeAttributes
= new HashMap
<String
, IConfigurationElement
>();
598 private Map
<String
, IConfigurationElement
> fTraceCategories
= new HashMap
<String
, IConfigurationElement
>();
599 private final Map
<String
, IConfigurationElement
> fTraceAttributes
= new HashMap
<String
, IConfigurationElement
>();
601 private String
[] getAvailableTraceTypes() {
603 // Populate the Categories and Trace Types
604 IConfigurationElement
[] config
= Platform
.getExtensionRegistry().getConfigurationElementsFor(TmfTraceType
.TMF_TRACE_TYPE_ID
);
605 for (IConfigurationElement ce
: config
) {
606 String elementName
= ce
.getName();
607 if (elementName
.equals(TmfTraceType
.TYPE_ELEM
)) {
608 String traceTypeId
= ce
.getAttribute(TmfTraceType
.ID_ATTR
);
609 fTraceTypeAttributes
.put(traceTypeId
, ce
);
610 } else if (elementName
.equals(TmfTraceType
.CATEGORY_ELEM
)) {
611 String categoryId
= ce
.getAttribute(TmfTraceType
.ID_ATTR
);
612 fTraceCategories
.put(categoryId
, ce
);
616 // Generate the list of Category:TraceType to populate the ComboBox
617 List
<String
> traceTypes
= new ArrayList
<String
>();
618 for (String typeId
: fTraceTypeAttributes
.keySet()) {
619 IConfigurationElement ce
= fTraceTypeAttributes
.get(typeId
);
620 String traceTypeName
= getCategory(ce
) + " : " + ce
.getAttribute(TmfTraceType
.NAME_ATTR
); //$NON-NLS-1$
621 fTraceAttributes
.put(traceTypeName
, ce
);
622 traceTypes
.add(traceTypeName
);
624 Collections
.sort(traceTypes
);
626 // add the custom trace types
627 for (CustomTxtTraceDefinition def
: CustomTxtTraceDefinition
.loadAll()) {
628 String traceTypeName
= CUSTOM_TXT_CATEGORY
+ " : " + def
.definitionName
; //$NON-NLS-1$
629 traceTypes
.add(traceTypeName
);
631 for (CustomXmlTraceDefinition def
: CustomXmlTraceDefinition
.loadAll()) {
632 String traceTypeName
= CUSTOM_XML_CATEGORY
+ " : " + def
.definitionName
; //$NON-NLS-1$
633 traceTypes
.add(traceTypeName
);
637 return traceTypes
.toArray(new String
[traceTypes
.size()]);
640 private String
getCategory(IConfigurationElement ce
) {
641 String categoryId
= ce
.getAttribute(TmfTraceType
.CATEGORY_ATTR
);
642 if (categoryId
!= null) {
643 IConfigurationElement category
= fTraceCategories
.get(categoryId
);
644 if (category
!= null && !category
.getName().equals("")) { //$NON-NLS-1$
645 return category
.getAttribute(TmfTraceType
.NAME_ATTR
);
648 return "[no category]"; //$NON-NLS-1$
651 // ------------------------------------------------------------------------
653 // ------------------------------------------------------------------------
655 private Button overwriteExistingResourcesCheckbox
;
656 private Button createLinksInWorkspaceButton
;
660 * @see org.eclipse.ui.dialogs.WizardDataTransferPage#createOptionsGroupButtons(org.eclipse.swt.widgets.Group)
663 protected void createOptionsGroupButtons(Group optionsGroup
) {
665 // Overwrite checkbox
666 overwriteExistingResourcesCheckbox
= new Button(optionsGroup
, SWT
.CHECK
);
667 overwriteExistingResourcesCheckbox
.setFont(optionsGroup
.getFont());
668 overwriteExistingResourcesCheckbox
.setText(Messages
.ImportTraceWizard_OverwriteExistingTrace
);
669 overwriteExistingResourcesCheckbox
.setSelection(false);
671 // Create links checkbox
672 createLinksInWorkspaceButton
= new Button(optionsGroup
, SWT
.CHECK
);
673 createLinksInWorkspaceButton
.setFont(optionsGroup
.getFont());
674 createLinksInWorkspaceButton
.setText(Messages
.ImportTraceWizard_CreateLinksInWorkspace
);
675 createLinksInWorkspaceButton
.setSelection(true);
677 createLinksInWorkspaceButton
.addSelectionListener(new SelectionAdapter() {
679 public void widgetSelected(SelectionEvent e
) {
680 updateWidgetEnablements();
684 updateWidgetEnablements();
687 // ------------------------------------------------------------------------
688 // Determine if the finish button can be enabled
689 // ------------------------------------------------------------------------
693 * @see org.eclipse.ui.dialogs.WizardDataTransferPage#validateSourceGroup()
696 public boolean validateSourceGroup() {
698 File sourceDirectory
= getSourceDirectory();
699 if (sourceDirectory
== null) {
700 setMessage(Messages
.ImportTraceWizard_SelectTraceSourceEmpty
);
704 if (sourceConflictsWithDestination(new Path(sourceDirectory
.getPath()))) {
706 setErrorMessage(getSourceConflictMessage());
710 List
<FileSystemElement
> resourcesToImport
= getSelectedResources();
711 if (resourcesToImport
.size() == 0) {
713 setErrorMessage(Messages
.ImportTraceWizard_SelectTraceNoneSelected
);
717 IContainer container
= getSpecifiedContainer();
718 if (container
!= null && container
.isVirtual()) {
719 if (Platform
.getPreferencesService().getBoolean(Activator
.PLUGIN_ID
, ResourcesPlugin
.PREF_DISABLE_LINKING
, false, null)) {
721 setErrorMessage(Messages
.ImportTraceWizard_CannotImportFilesUnderAVirtualFolder
);
724 if (createLinksInWorkspaceButton
== null || !createLinksInWorkspaceButton
.getSelection()) {
726 setErrorMessage(Messages
.ImportTraceWizard_HaveToCreateLinksUnderAVirtualFolder
);
731 // Perform trace validation
732 String traceTypeName
= fTraceTypes
.getText();
733 if (traceTypeName
!= null && !"".equals(traceTypeName
) && //$NON-NLS-1$
734 !traceTypeName
.startsWith(CUSTOM_TXT_CATEGORY
) && !traceTypeName
.startsWith(CUSTOM_XML_CATEGORY
)) {
736 List
<File
> traces
= isolateTraces();
737 for (File trace
: traces
) {
738 ITmfTrace
<?
> tmfTrace
= null;
740 IConfigurationElement ce
= fTraceAttributes
.get(traceTypeName
);
741 tmfTrace
= (ITmfTrace
<?
>) ce
.createExecutableExtension(TmfTraceType
.TRACE_TYPE_ATTR
);
742 if (tmfTrace
!= null && !tmfTrace
.validate(fProject
, trace
.getAbsolutePath())) {
744 setErrorMessage(Messages
.ImportTraceWizard_TraceValidationFailed
);
748 } catch (CoreException e
) {
750 if (tmfTrace
!= null)
756 setErrorMessage(null);
760 private List
<File
> isolateTraces() {
762 List
<File
> traces
= new ArrayList
<File
>();
765 List
<FileSystemElement
> selectedResources
= getSelectedResources();
766 Iterator
<FileSystemElement
> resources
= selectedResources
.iterator();
768 // Get the sorted list of unique entries
769 Map
<String
, File
> fileSystemObjects
= new HashMap
<String
, File
>();
770 while (resources
.hasNext()) {
771 File resource
= (File
) resources
.next().getFileSystemObject();
772 String key
= resource
.getAbsolutePath();
773 fileSystemObjects
.put(key
, resource
);
775 List
<String
> files
= new ArrayList
<String
>(fileSystemObjects
.keySet());
776 Collections
.sort(files
);
778 // After sorting, traces correspond to the unique prefixes
779 String prefix
= null;
780 for (int i
= 0; i
< files
.size(); i
++) {
781 File file
= fileSystemObjects
.get(files
.get(i
));
782 String name
= file
.getAbsolutePath();
783 if (prefix
== null || !name
.startsWith(prefix
)) {
784 prefix
= name
; // new prefix
792 // ------------------------------------------------------------------------
793 // Import the trace(s)
794 // ------------------------------------------------------------------------
798 * @return <code>true</code> if successful else false
800 public boolean finish() {
801 // Ensure source is valid
802 File sourceDir
= new File(getSourceDirectoryName());
803 if (!sourceDir
.isDirectory()) {
804 setErrorMessage(Messages
.ImportTraceWizard_InvalidTraceDirectory
);
808 String sourceDirPath
;
810 sourceDirPath
= sourceDir
.getCanonicalPath();
811 } catch (IOException e
) {
812 MessageDialog
.openInformation(getContainer().getShell(), Messages
.ImportTraceWizard_Information
,
813 Messages
.ImportTraceWizard_InvalidTraceDirectory
);
817 // Save directory for next import operation
818 fRootDirectory
= getSourceDirectoryName();
820 List
<FileSystemElement
> selectedResources
= getSelectedResources();
821 Iterator
<FileSystemElement
> resources
= selectedResources
.iterator();
823 // Use a map to end up with unique resources (getSelectedResources() can return duplicates)
824 Map
<String
, File
> fileSystemObjects
= new HashMap
<String
, File
>();
825 while (resources
.hasNext()) {
826 File file
= (File
) resources
.next().getFileSystemObject();
827 String key
= file
.getAbsolutePath();
828 fileSystemObjects
.put(key
, file
);
831 if (fileSystemObjects
.size() > 0) {
832 boolean ok
= importResources(sourceDirPath
, fileSystemObjects
);
833 String traceBundle
= null;
834 String traceTypeId
= null;
835 String traceIcon
= null;
836 String traceType
= fTraceTypes
.getText();
837 boolean traceTypeOK
= false;
838 if (traceType
.startsWith(CUSTOM_TXT_CATEGORY
)) {
839 for (CustomTxtTraceDefinition def
: CustomTxtTraceDefinition
.loadAll()) {
840 if (traceType
.equals(CUSTOM_TXT_CATEGORY
+ " : " + def
.definitionName
)) { //$NON-NLS-1$
842 traceBundle
= Activator
.getDefault().getBundle().getSymbolicName();
843 traceTypeId
= CustomTxtTrace
.class.getCanonicalName() + ":" + def
.definitionName
; //$NON-NLS-1$
844 traceIcon
= DEFAULT_TRACE_ICON_PATH
;
848 } else if (traceType
.startsWith(CUSTOM_XML_CATEGORY
)) {
849 for (CustomXmlTraceDefinition def
: CustomXmlTraceDefinition
.loadAll()) {
850 if (traceType
.equals(CUSTOM_XML_CATEGORY
+ " : " + def
.definitionName
)) { //$NON-NLS-1$
852 traceBundle
= Activator
.getDefault().getBundle().getSymbolicName();
853 traceTypeId
= CustomXmlTrace
.class.getCanonicalName() + ":" + def
.definitionName
; //$NON-NLS-1$
854 traceIcon
= DEFAULT_TRACE_ICON_PATH
;
859 IConfigurationElement ce
= fTraceAttributes
.get(traceType
);
862 traceBundle
= ce
.getContributor().getName();
863 traceTypeId
= ce
.getAttribute(TmfTraceType
.ID_ATTR
);
864 traceIcon
= ce
.getAttribute(TmfTraceType
.ICON_ATTR
);
867 if (ok
&& traceTypeOK
&& !traceType
.equals("")) { //$NON-NLS-1$
868 // Tag the selected traces with their type
869 List
<String
> files
= new ArrayList
<String
>(fileSystemObjects
.keySet());
870 Collections
.sort(files
);
871 // After sorting, traces correspond to the unique prefixes
872 String prefix
= null;
873 for (int i
= 0; i
< files
.size(); i
++) {
874 File file
= fileSystemObjects
.get(files
.get(i
));
875 String name
= file
.getAbsolutePath();
876 if (fTargetFolder
!= null && (prefix
== null || !name
.startsWith(prefix
))) {
877 prefix
= name
; // new prefix
878 IResource resource
= fTargetFolder
.findMember(file
.getName());
879 if (resource
!= null) {
881 // Set the trace properties for this resource
882 resource
.setPersistentProperty(TmfCommonConstants
.TRACEBUNDLE
, traceBundle
);
883 resource
.setPersistentProperty(TmfCommonConstants
.TRACETYPE
, traceTypeId
);
884 resource
.setPersistentProperty(TmfCommonConstants
.TRACEICON
, traceIcon
);
885 for (TmfTraceElement traceElement
: TmfProjectRegistry
.getProject(resource
.getProject()).getTracesFolder().getTraces()) {
886 if (traceElement
.getName().equals(resource
.getName())) {
887 traceElement
.refreshTraceType();
891 } catch (CoreException e
) {
892 Activator
.getDefault().logError("Error importing trace resource " + resource
.getName(), e
); //$NON-NLS-1$
901 MessageDialog
.openInformation(getContainer().getShell(), Messages
.ImportTraceWizard_Information
,
902 Messages
.ImportTraceWizard_SelectTraceNoneSelected
);
906 private boolean importResources(String rootDirectory
, Map
<String
, File
> fileSystemObjects
) {
908 // Determine the sorted canonical list of items to import
909 List
<File
> fileList
= new ArrayList
<File
>();
910 for (Entry
<String
, File
> entry
: fileSystemObjects
.entrySet()) {
911 fileList
.add(entry
.getValue());
913 Collections
.sort(fileList
);
916 // Perform a distinct import operation for everything that has the same prefix
917 // (distinct prefixes correspond to traces - we don't want to re-create parent structures)
919 boolean isLinked
= createLinksInWorkspaceButton
.getSelection();
920 for (int i
= 0; i
< fileList
.size(); i
++) {
921 File resource
= fileList
.get(i
);
922 File parentFolder
= new File(resource
.getParent());
924 List
<File
> subList
= new ArrayList
<File
>();
925 subList
.add(resource
);
926 if (resource
.isDirectory()) {
927 String prefix
= resource
.getAbsolutePath();
928 boolean hasSamePrefix
= true;
929 for (int j
= i
; j
< fileList
.size() && hasSamePrefix
; j
++) {
930 File res
= fileList
.get(j
);
931 hasSamePrefix
= res
.getAbsolutePath().startsWith(prefix
);
933 // Import children individually if not linked
942 // Perform the import operation for this subset
943 FileSystemStructureProvider fileSystemStructureProvider
= FileSystemStructureProvider
.INSTANCE
;
944 ImportOperation operation
= new ImportOperation(getContainerFullPath(), parentFolder
, fileSystemStructureProvider
, this,
946 operation
.setContext(getShell());
947 ok
= executeImportOperation(operation
);
953 private boolean executeImportOperation(ImportOperation op
) {
954 initializeOperation(op
);
957 getContainer().run(true, true, op
);
958 } catch (InterruptedException e
) {
960 } catch (InvocationTargetException e
) {
961 displayErrorDialog(e
.getTargetException());
965 IStatus status
= op
.getStatus();
966 if (!status
.isOK()) {
967 ErrorDialog
.openError(getContainer().getShell(), Messages
.ImportTraceWizard_ImportProblem
, null, status
);
974 private void initializeOperation(ImportOperation op
) {
975 op
.setCreateContainerStructure(false);
976 op
.setOverwriteResources(overwriteExistingResourcesCheckbox
.getSelection());
977 op
.setCreateLinks(createLinksInWorkspaceButton
.getSelection());
978 op
.setVirtualFolders(false);