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 * <b><u>ImportTraceWizardPage</u></b>
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
implements Listener
{
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
)
285 for (Object file
: files
)
288 return result
.toArray();
290 return new Object
[0];
295 private 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 File
getSourceDirectory(String path
) {
459 File sourceDirectory
= new File(getSourceDirectoryName(path
));
460 if (!sourceDirectory
.exists() || !sourceDirectory
.isDirectory()) {
464 return sourceDirectory
;
467 private 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 FileSystemElement
createRootElement(Object fileSystemObject
, IImportStructureProvider provider
) {
543 boolean isContainer
= provider
.isFolder(fileSystemObject
);
544 String elementLabel
= provider
.getLabel(fileSystemObject
);
546 FileSystemElement dummyParent
= new FileSystemElement("", null, true); //$NON-NLS-1$
547 FileSystemElement element
= new FileSystemElement(elementLabel
, dummyParent
, isContainer
);
548 element
.setFileSystemObject(fileSystemObject
);
550 // Get the first level
551 populateChildren(element
);
556 // ------------------------------------------------------------------------
558 // ------------------------------------------------------------------------
560 private Combo fTraceTypes
;
562 private final void createTraceTypeGroup(Composite parent
) {
563 Composite composite
= new Composite(parent
, SWT
.NONE
);
564 GridLayout layout
= new GridLayout();
565 layout
.numColumns
= 3;
566 layout
.makeColumnsEqualWidth
= false;
567 composite
.setLayout(layout
);
568 composite
.setFont(parent
.getFont());
569 GridData buttonData
= new GridData(SWT
.FILL
, SWT
.FILL
, true, false);
570 composite
.setLayoutData(buttonData
);
572 // Trace type label ("Trace Type:")
573 Label typeLabel
= new Label(composite
, SWT
.NONE
);
574 typeLabel
.setText(Messages
.ImportTraceWizard_TraceType
);
575 typeLabel
.setFont(parent
.getFont());
578 fTraceTypes
= new Combo(composite
, SWT
.BORDER
);
579 GridData data
= new GridData(SWT
.FILL
, SWT
.FILL
, true, false, 2, 1);
580 fTraceTypes
.setLayoutData(data
);
581 fTraceTypes
.setFont(parent
.getFont());
583 String
[] availableTraceTypes
= getAvailableTraceTypes();
584 fTraceTypes
.setItems(availableTraceTypes
);
586 fTraceTypes
.addSelectionListener(new SelectionListener() {
588 public void widgetSelected(SelectionEvent e
) {
589 validateSourceGroup();
593 public void widgetDefaultSelected(SelectionEvent e
) {
598 // The mapping of available trace type IDs to their corresponding configuration element
599 private Map
<String
, IConfigurationElement
> fTraceTypeAttributes
= new HashMap
<String
, IConfigurationElement
>();
600 private Map
<String
, IConfigurationElement
> fTraceCategories
= new HashMap
<String
, IConfigurationElement
>();
601 private final Map
<String
, IConfigurationElement
> fTraceAttributes
= new HashMap
<String
, IConfigurationElement
>();
603 private String
[] getAvailableTraceTypes() {
605 // Populate the Categories and Trace Types
606 IConfigurationElement
[] config
= Platform
.getExtensionRegistry().getConfigurationElementsFor(TmfTraceType
.TMF_TRACE_TYPE_ID
);
607 for (IConfigurationElement ce
: config
) {
608 String elementName
= ce
.getName();
609 if (elementName
.equals(TmfTraceType
.TYPE_ELEM
)) {
610 String traceTypeId
= ce
.getAttribute(TmfTraceType
.ID_ATTR
);
611 fTraceTypeAttributes
.put(traceTypeId
, ce
);
612 } else if (elementName
.equals(TmfTraceType
.CATEGORY_ELEM
)) {
613 String categoryId
= ce
.getAttribute(TmfTraceType
.ID_ATTR
);
614 fTraceCategories
.put(categoryId
, ce
);
618 // Generate the list of Category:TraceType to populate the ComboBox
619 List
<String
> traceTypes
= new ArrayList
<String
>();
620 for (String typeId
: fTraceTypeAttributes
.keySet()) {
621 IConfigurationElement ce
= fTraceTypeAttributes
.get(typeId
);
622 String traceTypeName
= getCategory(ce
) + " : " + ce
.getAttribute(TmfTraceType
.NAME_ATTR
); //$NON-NLS-1$
623 fTraceAttributes
.put(traceTypeName
, ce
);
624 traceTypes
.add(traceTypeName
);
626 Collections
.sort(traceTypes
);
628 // add the custom trace types
629 for (CustomTxtTraceDefinition def
: CustomTxtTraceDefinition
.loadAll()) {
630 String traceTypeName
= CUSTOM_TXT_CATEGORY
+ " : " + def
.definitionName
; //$NON-NLS-1$
631 traceTypes
.add(traceTypeName
);
633 for (CustomXmlTraceDefinition def
: CustomXmlTraceDefinition
.loadAll()) {
634 String traceTypeName
= CUSTOM_XML_CATEGORY
+ " : " + def
.definitionName
; //$NON-NLS-1$
635 traceTypes
.add(traceTypeName
);
639 return traceTypes
.toArray(new String
[traceTypes
.size()]);
642 private String
getCategory(IConfigurationElement ce
) {
643 String categoryId
= ce
.getAttribute(TmfTraceType
.CATEGORY_ATTR
);
644 if (categoryId
!= null) {
645 IConfigurationElement category
= fTraceCategories
.get(categoryId
);
646 if (category
!= null && !category
.getName().equals("")) { //$NON-NLS-1$
647 return category
.getAttribute(TmfTraceType
.NAME_ATTR
);
650 return "[no category]"; //$NON-NLS-1$
653 // ------------------------------------------------------------------------
655 // ------------------------------------------------------------------------
657 private Button overwriteExistingResourcesCheckbox
;
658 private Button createLinksInWorkspaceButton
;
662 * @see org.eclipse.ui.dialogs.WizardDataTransferPage#createOptionsGroupButtons(org.eclipse.swt.widgets.Group)
665 protected void createOptionsGroupButtons(Group optionsGroup
) {
667 // Overwrite checkbox
668 overwriteExistingResourcesCheckbox
= new Button(optionsGroup
, SWT
.CHECK
);
669 overwriteExistingResourcesCheckbox
.setFont(optionsGroup
.getFont());
670 overwriteExistingResourcesCheckbox
.setText(Messages
.ImportTraceWizard_OverwriteExistingTrace
);
671 overwriteExistingResourcesCheckbox
.setSelection(false);
673 // Create links checkbox
674 createLinksInWorkspaceButton
= new Button(optionsGroup
, SWT
.CHECK
);
675 createLinksInWorkspaceButton
.setFont(optionsGroup
.getFont());
676 createLinksInWorkspaceButton
.setText(Messages
.ImportTraceWizard_CreateLinksInWorkspace
);
677 createLinksInWorkspaceButton
.setSelection(true);
679 createLinksInWorkspaceButton
.addSelectionListener(new SelectionAdapter() {
681 public void widgetSelected(SelectionEvent e
) {
682 updateWidgetEnablements();
686 updateWidgetEnablements();
689 // ------------------------------------------------------------------------
690 // Determine if the finish button can be enabled
691 // ------------------------------------------------------------------------
695 * @see org.eclipse.ui.dialogs.WizardDataTransferPage#validateSourceGroup()
698 public boolean validateSourceGroup() {
700 File sourceDirectory
= getSourceDirectory();
701 if (sourceDirectory
== null) {
702 setMessage(Messages
.ImportTraceWizard_SelectTraceSourceEmpty
);
706 if (sourceConflictsWithDestination(new Path(sourceDirectory
.getPath()))) {
708 setErrorMessage(getSourceConflictMessage());
712 List
<FileSystemElement
> resourcesToImport
= getSelectedResources();
713 if (resourcesToImport
.size() == 0) {
715 setErrorMessage(Messages
.ImportTraceWizard_SelectTraceNoneSelected
);
719 IContainer container
= getSpecifiedContainer();
720 if (container
!= null && container
.isVirtual()) {
721 if (Platform
.getPreferencesService().getBoolean(Activator
.PLUGIN_ID
, ResourcesPlugin
.PREF_DISABLE_LINKING
, false, null)) {
723 setErrorMessage(Messages
.ImportTraceWizard_CannotImportFilesUnderAVirtualFolder
);
726 if (createLinksInWorkspaceButton
== null || !createLinksInWorkspaceButton
.getSelection()) {
728 setErrorMessage(Messages
.ImportTraceWizard_HaveToCreateLinksUnderAVirtualFolder
);
733 // Perform trace validation
734 String traceTypeName
= fTraceTypes
.getText();
735 if (traceTypeName
!= null && !"".equals(traceTypeName
) && //$NON-NLS-1$
736 !traceTypeName
.startsWith(CUSTOM_TXT_CATEGORY
) && !traceTypeName
.startsWith(CUSTOM_XML_CATEGORY
)) {
738 List
<File
> traces
= isolateTraces();
739 for (File trace
: traces
) {
740 ITmfTrace
<?
> tmfTrace
= null;
742 IConfigurationElement ce
= fTraceAttributes
.get(traceTypeName
);
743 tmfTrace
= (ITmfTrace
<?
>) ce
.createExecutableExtension(TmfTraceType
.TRACE_TYPE_ATTR
);
744 if (tmfTrace
!= null && !tmfTrace
.validate(fProject
, trace
.getAbsolutePath())) {
746 setErrorMessage(Messages
.ImportTraceWizard_TraceValidationFailed
);
750 } catch (CoreException e
) {
752 if (tmfTrace
!= null)
758 setErrorMessage(null);
762 private List
<File
> isolateTraces() {
764 List
<File
> traces
= new ArrayList
<File
>();
767 List
<FileSystemElement
> selectedResources
= getSelectedResources();
768 Iterator
<FileSystemElement
> resources
= selectedResources
.iterator();
770 // Get the sorted list of unique entries
771 Map
<String
, File
> fileSystemObjects
= new HashMap
<String
, File
>();
772 while (resources
.hasNext()) {
773 File resource
= (File
) resources
.next().getFileSystemObject();
774 String key
= resource
.getAbsolutePath();
775 fileSystemObjects
.put(key
, resource
);
777 List
<String
> files
= new ArrayList
<String
>(fileSystemObjects
.keySet());
778 Collections
.sort(files
);
780 // After sorting, traces correspond to the unique prefixes
781 String prefix
= null;
782 for (int i
= 0; i
< files
.size(); i
++) {
783 File file
= fileSystemObjects
.get(files
.get(i
));
784 String name
= file
.getAbsolutePath();
785 if (prefix
== null || !name
.startsWith(prefix
)) {
786 prefix
= name
; // new prefix
794 // ------------------------------------------------------------------------
795 // Import the trace(s)
796 // ------------------------------------------------------------------------
800 * @return <code>true</code> if successful else false
802 public boolean finish() {
803 // Ensure source is valid
804 File sourceDir
= new File(getSourceDirectoryName());
805 if (!sourceDir
.isDirectory()) {
806 setErrorMessage(Messages
.ImportTraceWizard_InvalidTraceDirectory
);
810 String sourceDirPath
;
812 sourceDirPath
= sourceDir
.getCanonicalPath();
813 } catch (IOException e
) {
814 MessageDialog
.openInformation(getContainer().getShell(), Messages
.ImportTraceWizard_Information
,
815 Messages
.ImportTraceWizard_InvalidTraceDirectory
);
819 // Save directory for next import operation
820 fRootDirectory
= getSourceDirectoryName();
822 List
<FileSystemElement
> selectedResources
= getSelectedResources();
823 Iterator
<FileSystemElement
> resources
= selectedResources
.iterator();
825 // Use a map to end up with unique resources (getSelectedResources() can return duplicates)
826 Map
<String
, File
> fileSystemObjects
= new HashMap
<String
, File
>();
827 while (resources
.hasNext()) {
828 File file
= (File
) resources
.next().getFileSystemObject();
829 String key
= file
.getAbsolutePath();
830 fileSystemObjects
.put(key
, file
);
833 if (fileSystemObjects
.size() > 0) {
834 boolean ok
= importResources(sourceDirPath
, fileSystemObjects
);
835 String traceBundle
= null;
836 String traceTypeId
= null;
837 String traceIcon
= null;
838 String traceType
= fTraceTypes
.getText();
839 boolean traceTypeOK
= false;
840 if (traceType
.startsWith(CUSTOM_TXT_CATEGORY
)) {
841 for (CustomTxtTraceDefinition def
: CustomTxtTraceDefinition
.loadAll()) {
842 if (traceType
.equals(CUSTOM_TXT_CATEGORY
+ " : " + def
.definitionName
)) { //$NON-NLS-1$
844 traceBundle
= Activator
.getDefault().getBundle().getSymbolicName();
845 traceTypeId
= CustomTxtTrace
.class.getCanonicalName() + ":" + def
.definitionName
; //$NON-NLS-1$
846 traceIcon
= DEFAULT_TRACE_ICON_PATH
;
850 } else if (traceType
.startsWith(CUSTOM_XML_CATEGORY
)) {
851 for (CustomXmlTraceDefinition def
: CustomXmlTraceDefinition
.loadAll()) {
852 if (traceType
.equals(CUSTOM_XML_CATEGORY
+ " : " + def
.definitionName
)) { //$NON-NLS-1$
854 traceBundle
= Activator
.getDefault().getBundle().getSymbolicName();
855 traceTypeId
= CustomXmlTrace
.class.getCanonicalName() + ":" + def
.definitionName
; //$NON-NLS-1$
856 traceIcon
= DEFAULT_TRACE_ICON_PATH
;
861 IConfigurationElement ce
= fTraceAttributes
.get(traceType
);
864 traceBundle
= ce
.getContributor().getName();
865 traceTypeId
= ce
.getAttribute(TmfTraceType
.ID_ATTR
);
866 traceIcon
= ce
.getAttribute(TmfTraceType
.ICON_ATTR
);
869 if (ok
&& traceTypeOK
&& !traceType
.equals("")) { //$NON-NLS-1$
870 // Tag the selected traces with their type
871 List
<String
> files
= new ArrayList
<String
>(fileSystemObjects
.keySet());
872 Collections
.sort(files
);
873 // After sorting, traces correspond to the unique prefixes
874 String prefix
= null;
875 for (int i
= 0; i
< files
.size(); i
++) {
876 File file
= fileSystemObjects
.get(files
.get(i
));
877 String name
= file
.getAbsolutePath();
878 if (fTargetFolder
!= null && (prefix
== null || !name
.startsWith(prefix
))) {
879 prefix
= name
; // new prefix
880 IResource resource
= fTargetFolder
.findMember(file
.getName());
881 if (resource
!= null) {
883 // Set the trace properties for this resource
884 resource
.setPersistentProperty(TmfCommonConstants
.TRACEBUNDLE
, traceBundle
);
885 resource
.setPersistentProperty(TmfCommonConstants
.TRACETYPE
, traceTypeId
);
886 resource
.setPersistentProperty(TmfCommonConstants
.TRACEICON
, traceIcon
);
887 for (TmfTraceElement traceElement
: TmfProjectRegistry
.getProject(resource
.getProject()).getTracesFolder().getTraces()) {
888 if (traceElement
.getName().equals(resource
.getName())) {
889 traceElement
.refreshTraceType();
893 } catch (CoreException e
) {
894 Activator
.getDefault().logError("Error importing trace resource " + resource
.getName(), e
); //$NON-NLS-1$
903 MessageDialog
.openInformation(getContainer().getShell(), Messages
.ImportTraceWizard_Information
,
904 Messages
.ImportTraceWizard_SelectTraceNoneSelected
);
908 private boolean importResources(String rootDirectory
, Map
<String
, File
> fileSystemObjects
) {
910 // Determine the sorted canonical list of items to import
911 List
<File
> fileList
= new ArrayList
<File
>();
912 for (Entry
<String
, File
> entry
: fileSystemObjects
.entrySet()) {
913 fileList
.add(entry
.getValue());
915 Collections
.sort(fileList
);
918 // Perform a distinct import operation for everything that has the same prefix
919 // (distinct prefixes correspond to traces - we don't want to re-create parent structures)
921 boolean isLinked
= createLinksInWorkspaceButton
.getSelection();
922 for (int i
= 0; i
< fileList
.size(); i
++) {
923 File resource
= fileList
.get(i
);
924 File parentFolder
= new File(resource
.getParent());
926 List
<File
> subList
= new ArrayList
<File
>();
927 subList
.add(resource
);
928 if (resource
.isDirectory()) {
929 String prefix
= resource
.getAbsolutePath();
930 boolean hasSamePrefix
= true;
931 for (int j
= i
; j
< fileList
.size() && hasSamePrefix
; j
++) {
932 File res
= fileList
.get(j
);
933 hasSamePrefix
= res
.getAbsolutePath().startsWith(prefix
);
935 // Import children individually if not linked
944 // Perform the import operation for this subset
945 FileSystemStructureProvider fileSystemStructureProvider
= FileSystemStructureProvider
.INSTANCE
;
946 ImportOperation operation
= new ImportOperation(getContainerFullPath(), parentFolder
, fileSystemStructureProvider
, this,
948 operation
.setContext(getShell());
949 ok
= executeImportOperation(operation
);
955 private boolean executeImportOperation(ImportOperation op
) {
956 initializeOperation(op
);
959 getContainer().run(true, true, op
);
960 } catch (InterruptedException e
) {
962 } catch (InvocationTargetException e
) {
963 displayErrorDialog(e
.getTargetException());
967 IStatus status
= op
.getStatus();
968 if (!status
.isOK()) {
969 ErrorDialog
.openError(getContainer().getShell(), Messages
.ImportTraceWizard_ImportProblem
, null, status
);
976 private void initializeOperation(ImportOperation op
) {
977 op
.setCreateContainerStructure(false);
978 op
.setOverwriteResources(overwriteExistingResourcesCheckbox
.getSelection());
979 op
.setCreateLinks(createLinksInWorkspaceButton
.getSelection());
980 op
.setVirtualFolders(false);