1 /*******************************************************************************
2 * Copyright (c) 2009, 2010, 2011 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
;
28 import org
.eclipse
.core
.resources
.IContainer
;
29 import org
.eclipse
.core
.resources
.IFolder
;
30 import org
.eclipse
.core
.resources
.IProject
;
31 import org
.eclipse
.core
.resources
.IResource
;
32 import org
.eclipse
.core
.resources
.ResourcesPlugin
;
33 import org
.eclipse
.core
.runtime
.CoreException
;
34 import org
.eclipse
.core
.runtime
.IConfigurationElement
;
35 import org
.eclipse
.core
.runtime
.IPath
;
36 import org
.eclipse
.core
.runtime
.IStatus
;
37 import org
.eclipse
.core
.runtime
.Path
;
38 import org
.eclipse
.core
.runtime
.Platform
;
39 import org
.eclipse
.jface
.dialogs
.ErrorDialog
;
40 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
41 import org
.eclipse
.jface
.viewers
.CheckStateChangedEvent
;
42 import org
.eclipse
.jface
.viewers
.CheckboxTreeViewer
;
43 import org
.eclipse
.jface
.viewers
.ICheckStateListener
;
44 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
45 import org
.eclipse
.jface
.viewers
.ITreeContentProvider
;
46 import org
.eclipse
.linuxtools
.tmf
.core
.TmfProjectNature
;
47 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
48 import org
.eclipse
.linuxtools
.tmf
.core
.util
.TmfTraceType
;
49 import org
.eclipse
.linuxtools
.tmf
.ui
.TmfUiPlugin
;
50 import org
.eclipse
.linuxtools
.tmf
.ui
.parsers
.custom
.CustomTxtTrace
;
51 import org
.eclipse
.linuxtools
.tmf
.ui
.parsers
.custom
.CustomTxtTraceDefinition
;
52 import org
.eclipse
.linuxtools
.tmf
.ui
.parsers
.custom
.CustomXmlTrace
;
53 import org
.eclipse
.linuxtools
.tmf
.ui
.parsers
.custom
.CustomXmlTraceDefinition
;
54 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfProjectRegistry
;
55 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceElement
;
56 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceFolder
;
57 import org
.eclipse
.swt
.SWT
;
58 import org
.eclipse
.swt
.custom
.BusyIndicator
;
59 import org
.eclipse
.swt
.events
.FocusEvent
;
60 import org
.eclipse
.swt
.events
.FocusListener
;
61 import org
.eclipse
.swt
.events
.KeyEvent
;
62 import org
.eclipse
.swt
.events
.KeyListener
;
63 import org
.eclipse
.swt
.events
.SelectionAdapter
;
64 import org
.eclipse
.swt
.events
.SelectionEvent
;
65 import org
.eclipse
.swt
.events
.SelectionListener
;
66 import org
.eclipse
.swt
.layout
.GridData
;
67 import org
.eclipse
.swt
.layout
.GridLayout
;
68 import org
.eclipse
.swt
.widgets
.Button
;
69 import org
.eclipse
.swt
.widgets
.Combo
;
70 import org
.eclipse
.swt
.widgets
.Composite
;
71 import org
.eclipse
.swt
.widgets
.DirectoryDialog
;
72 import org
.eclipse
.swt
.widgets
.Event
;
73 import org
.eclipse
.swt
.widgets
.Group
;
74 import org
.eclipse
.swt
.widgets
.Label
;
75 import org
.eclipse
.swt
.widgets
.Listener
;
76 import org
.eclipse
.ui
.IWorkbench
;
77 import org
.eclipse
.ui
.dialogs
.FileSystemElement
;
78 import org
.eclipse
.ui
.dialogs
.WizardResourceImportPage
;
79 import org
.eclipse
.ui
.model
.WorkbenchContentProvider
;
80 import org
.eclipse
.ui
.model
.WorkbenchLabelProvider
;
81 import org
.eclipse
.ui
.wizards
.datatransfer
.FileSystemStructureProvider
;
82 import org
.eclipse
.ui
.wizards
.datatransfer
.IImportStructureProvider
;
83 import org
.eclipse
.ui
.wizards
.datatransfer
.ImportOperation
;
86 * <b><u>ImportTraceWizardPage</u></b>
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
97 * TODO: Consider adding Filter/Select/Deselect buttons
99 public class ImportTraceWizardPage
extends WizardResourceImportPage
implements Listener
{
101 // ------------------------------------------------------------------------
103 // ------------------------------------------------------------------------
105 static private final String IMPORT_WIZARD_PAGE
= "ImportTraceWizardPage"; //$NON-NLS-1$
106 private static final String CUSTOM_TXT_CATEGORY
= "Custom Text"; //$NON-NLS-1$
107 private static final String CUSTOM_XML_CATEGORY
= "Custom XML"; //$NON-NLS-1$
108 private static final String DEFAULT_TRACE_ICON_PATH
= "icons/elcl16/trace.gif"; //$NON-NLS-1$
110 // ------------------------------------------------------------------------
112 // ------------------------------------------------------------------------
114 // Folder navigation start point (saved between invocations)
115 private static String fRootDirectory
= null;
117 // Navigation folder content viewer and selector
118 private CheckboxTreeViewer fFolderViewer
;
120 // Parent tracing project
121 private IProject fProject
;
123 // Target import directory ('Traces' folder)
124 private IFolder fTargetFolder
;
126 // ------------------------------------------------------------------------
128 // ------------------------------------------------------------------------
130 protected ImportTraceWizardPage(String name
, IStructuredSelection selection
) {
131 super(name
, selection
);
134 public ImportTraceWizardPage(IWorkbench workbench
, IStructuredSelection selection
) {
135 this(IMPORT_WIZARD_PAGE
, selection
);
136 setTitle(Messages
.ImportTraceWizard_FileSystemTitle
);
137 setDescription(Messages
.ImportTraceWizard_ImportTrace
);
139 // Locate the target trace folder
140 IFolder traceFolder
= null;
141 Object element
= selection
.getFirstElement();
143 if (element
instanceof TmfTraceFolder
) {
144 TmfTraceFolder tmfTraceFolder
= (TmfTraceFolder
) element
;
145 fProject
= tmfTraceFolder
.getProject().getResource();
146 traceFolder
= tmfTraceFolder
.getResource();
147 } else if (element
instanceof IProject
) {
148 IProject project
= (IProject
) element
;
150 if (project
.hasNature(TmfProjectNature
.ID
)) {
151 traceFolder
= (IFolder
) project
.findMember(TmfTraceFolder
.TRACE_FOLDER_NAME
);
153 } catch (CoreException e
) {
157 // Set the target trace folder
158 if (traceFolder
!= null) {
159 fTargetFolder
= traceFolder
;
160 String path
= traceFolder
.getFullPath().toOSString();
161 setContainerFieldValue(path
);
165 // ------------------------------------------------------------------------
166 // WizardResourceImportPage
167 // ------------------------------------------------------------------------
170 public void createControl(Composite parent
) {
171 super.createControl(parent
);
172 // Restore last directory if applicable
173 if (fRootDirectory
!= null) {
174 directoryNameField
.setText(fRootDirectory
);
175 updateFromSourceField();
180 protected void createSourceGroup(Composite parent
) {
181 createDirectorySelectionGroup(parent
);
182 createFileSelectionGroup(parent
);
183 createTraceTypeGroup(parent
);
184 validateSourceGroup();
188 protected void createFileSelectionGroup(Composite parent
) {
190 // This Composite is only used for widget alignment purposes
191 Composite composite
= new Composite(parent
, SWT
.NONE
);
192 composite
.setFont(parent
.getFont());
193 GridLayout layout
= new GridLayout();
194 composite
.setLayout(layout
);
195 composite
.setLayoutData(new GridData(GridData
.FILL_BOTH
));
197 final int PREFERRED_LIST_HEIGHT
= 150;
199 fFolderViewer
= new CheckboxTreeViewer(composite
, SWT
.BORDER
);
200 GridData data
= new GridData(GridData
.FILL_BOTH
);
201 data
.heightHint
= PREFERRED_LIST_HEIGHT
;
202 fFolderViewer
.getTree().setLayoutData(data
);
203 fFolderViewer
.getTree().setFont(parent
.getFont());
205 fFolderViewer
.setContentProvider(getFileProvider());
206 fFolderViewer
.setLabelProvider(new WorkbenchLabelProvider());
207 fFolderViewer
.addCheckStateListener(new ICheckStateListener() {
209 public void checkStateChanged(CheckStateChangedEvent event
) {
210 Object elem
= event
.getElement();
211 if (elem
instanceof FileSystemElement
) {
212 FileSystemElement element
= (FileSystemElement
) elem
;
213 if (fFolderViewer
.getGrayed(element
)) {
214 fFolderViewer
.setSubtreeChecked(element
, false);
215 fFolderViewer
.setGrayed(element
, false);
216 } else if (event
.getChecked()) {
217 fFolderViewer
.setSubtreeChecked(event
.getElement(), true);
219 fFolderViewer
.setParentsGrayed(element
, true);
220 if (!element
.isDirectory()) {
221 fFolderViewer
.setGrayed(element
, false);
224 updateWidgetEnablements();
231 protected ITreeContentProvider
getFolderProvider() {
236 protected ITreeContentProvider
getFileProvider() {
237 return new WorkbenchContentProvider() {
239 public Object
[] getChildren(Object o
) {
240 if (o
instanceof FileSystemElement
) {
241 FileSystemElement element
= (FileSystemElement
) o
;
242 populateChildren(element
);
243 // For our purpose, we need folders + files
244 Object
[] folders
= element
.getFolders().getChildren();
245 Object
[] files
= element
.getFiles().getChildren();
247 List
<Object
> result
= new LinkedList
<Object
>();
248 for (Object folder
: folders
)
250 for (Object file
: files
)
253 return result
.toArray();
255 return new Object
[0];
260 private void populateChildren(FileSystemElement parent
) {
261 // Do not re-populate if the job was done already...
262 FileSystemStructureProvider provider
= FileSystemStructureProvider
.INSTANCE
;
263 if (parent
.getFolders().size() == 0 && parent
.getFiles().size() == 0) {
264 Object fileSystemObject
= parent
.getFileSystemObject();
265 List
<?
> children
= provider
.getChildren(fileSystemObject
);
266 if (children
!= null) {
267 Iterator
<?
> iterator
= children
.iterator();
268 while (iterator
.hasNext()) {
269 Object child
= iterator
.next();
270 String label
= provider
.getLabel(child
);
271 FileSystemElement element
= new FileSystemElement(label
, parent
, provider
.isFolder(child
));
272 element
.setFileSystemObject(child
);
279 protected List
<FileSystemElement
> getSelectedResources() {
280 List
<FileSystemElement
> resources
= new ArrayList
<FileSystemElement
>();
281 Object
[] checkedItems
= fFolderViewer
.getCheckedElements();
282 for (Object item
: checkedItems
) {
283 if (item
instanceof FileSystemElement
&& !fFolderViewer
.getGrayed(item
)) {
284 resources
.add((FileSystemElement
) item
);
290 // ------------------------------------------------------------------------
291 // Directory Selection Group (forked WizardFileSystemResourceImportPage1)
292 // ------------------------------------------------------------------------
294 protected Combo directoryNameField
;
295 protected Button directoryBrowseButton
;
296 private boolean entryChanged
= false;
298 protected void createDirectorySelectionGroup(Composite parent
) {
300 Composite directoryContainerGroup
= new Composite(parent
, SWT
.NONE
);
301 GridLayout layout
= new GridLayout();
302 layout
.numColumns
= 3;
303 directoryContainerGroup
.setLayout(layout
);
304 directoryContainerGroup
.setFont(parent
.getFont());
305 directoryContainerGroup
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, false));
307 // Label ("Trace directory:")
308 Label groupLabel
= new Label(directoryContainerGroup
, SWT
.NONE
);
309 groupLabel
.setText(Messages
.ImportTraceWizard_DirectoryLocation
);
310 groupLabel
.setFont(parent
.getFont());
312 // Directory name entry field
313 directoryNameField
= new Combo(directoryContainerGroup
, SWT
.BORDER
);
314 GridData data
= new GridData(SWT
.FILL
, SWT
.FILL
, true, false);
315 data
.widthHint
= SIZING_TEXT_FIELD_WIDTH
;
316 directoryNameField
.setLayoutData(data
);
317 directoryNameField
.setFont(parent
.getFont());
319 directoryNameField
.addSelectionListener(new SelectionAdapter() {
321 public void widgetSelected(SelectionEvent e
) {
322 updateFromSourceField();
326 directoryNameField
.addKeyListener(new KeyListener() {
328 public void keyPressed(KeyEvent e
) {
329 // If there has been a key pressed then mark as dirty
331 if (e
.character
== SWT
.CR
) { // Windows...
332 entryChanged
= false;
333 updateFromSourceField();
338 public void keyReleased(KeyEvent e
) {
342 directoryNameField
.addFocusListener(new FocusListener() {
344 public void focusGained(FocusEvent e
) {
345 // Do nothing when getting focus
349 public void focusLost(FocusEvent e
) {
350 // Clear the flag to prevent constant update
352 entryChanged
= false;
353 updateFromSourceField();
359 directoryBrowseButton
= new Button(directoryContainerGroup
, SWT
.PUSH
);
360 directoryBrowseButton
.setText(Messages
.ImportTraceWizard_BrowseButton
);
361 directoryBrowseButton
.addListener(SWT
.Selection
, this);
362 directoryBrowseButton
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, false, false));
363 directoryBrowseButton
.setFont(parent
.getFont());
364 setButtonLayoutData(directoryBrowseButton
);
367 // ------------------------------------------------------------------------
368 // Browse for the source directory
369 // ------------------------------------------------------------------------
372 public void handleEvent(Event event
) {
373 if (event
.widget
== directoryBrowseButton
) {
374 handleSourceDirectoryBrowseButtonPressed();
376 super.handleEvent(event
);
379 protected void handleSourceDirectoryBrowseButtonPressed() {
380 String currentSource
= directoryNameField
.getText();
381 DirectoryDialog dialog
= new DirectoryDialog(directoryNameField
.getShell(), SWT
.SAVE
| SWT
.SHEET
);
382 dialog
.setText(Messages
.ImportTraceWizard_SelectTraceDirectoryTitle
);
383 dialog
.setMessage(Messages
.ImportTraceWizard_SelectTraceDirectoryMessage
);
384 dialog
.setFilterPath(getSourceDirectoryName(currentSource
));
386 String selectedDirectory
= dialog
.open();
387 if (selectedDirectory
!= null) {
388 // Just quit if the directory is not valid
389 if ((getSourceDirectory(selectedDirectory
) == null) || selectedDirectory
.equals(currentSource
)) {
392 // If it is valid then proceed to populate
393 setErrorMessage(null);
394 setSourceName(selectedDirectory
);
398 private File
getSourceDirectory() {
399 return getSourceDirectory(directoryNameField
.getText());
402 private File
getSourceDirectory(String path
) {
403 File sourceDirectory
= new File(getSourceDirectoryName(path
));
404 if (!sourceDirectory
.exists() || !sourceDirectory
.isDirectory()) {
408 return sourceDirectory
;
411 private String
getSourceDirectoryName(String sourceName
) {
412 IPath result
= new Path(sourceName
.trim());
413 if (result
.getDevice() != null && result
.segmentCount() == 0) {
414 result
= result
.addTrailingSeparator();
416 result
= result
.removeTrailingSeparator();
418 return result
.toOSString();
421 private String
getSourceDirectoryName() {
422 return getSourceDirectoryName(directoryNameField
.getText());
425 private void updateFromSourceField() {
426 setSourceName(directoryNameField
.getText());
427 updateWidgetEnablements();
430 private void setSourceName(String path
) {
431 if (path
.length() > 0) {
432 String
[] currentItems
= directoryNameField
.getItems();
433 int selectionIndex
= -1;
434 for (int i
= 0; i
< currentItems
.length
; i
++) {
435 if (currentItems
[i
].equals(path
)) {
439 if (selectionIndex
< 0) {
440 int oldLength
= currentItems
.length
;
441 String
[] newItems
= new String
[oldLength
+ 1];
442 System
.arraycopy(currentItems
, 0, newItems
, 0, oldLength
);
443 newItems
[oldLength
] = path
;
444 directoryNameField
.setItems(newItems
);
445 selectionIndex
= oldLength
;
447 directoryNameField
.select(selectionIndex
);
452 // ------------------------------------------------------------------------
453 // File Selection Group (forked WizardFileSystemResourceImportPage1)
454 // ------------------------------------------------------------------------
456 private void resetSelection() {
457 FileSystemElement root
= getFileSystemTree();
458 populateListViewer(root
);
461 private void populateListViewer(final Object treeElement
) {
462 fFolderViewer
.setInput(treeElement
);
465 private FileSystemElement
getFileSystemTree() {
466 File sourceDirectory
= getSourceDirectory();
467 if (sourceDirectory
== null) {
470 return selectFiles(sourceDirectory
, FileSystemStructureProvider
.INSTANCE
);
473 private FileSystemElement
selectFiles(final Object rootFileSystemObject
, final IImportStructureProvider structureProvider
) {
474 final FileSystemElement
[] results
= new FileSystemElement
[1];
475 BusyIndicator
.showWhile(getShell().getDisplay(), new Runnable() {
478 // Create the root element from the supplied file system object
479 results
[0] = createRootElement(rootFileSystemObject
, structureProvider
);
485 private FileSystemElement
createRootElement(Object fileSystemObject
, IImportStructureProvider provider
) {
487 boolean isContainer
= provider
.isFolder(fileSystemObject
);
488 String elementLabel
= provider
.getLabel(fileSystemObject
);
490 FileSystemElement dummyParent
= new FileSystemElement("", null, true); //$NON-NLS-1$
491 FileSystemElement element
= new FileSystemElement(elementLabel
, dummyParent
, isContainer
);
492 element
.setFileSystemObject(fileSystemObject
);
494 // Get the first level
495 populateChildren(element
);
500 // ------------------------------------------------------------------------
502 // ------------------------------------------------------------------------
504 private Combo fTraceTypes
;
506 private final void createTraceTypeGroup(Composite parent
) {
507 Composite composite
= new Composite(parent
, SWT
.NONE
);
508 GridLayout layout
= new GridLayout();
509 layout
.numColumns
= 3;
510 layout
.makeColumnsEqualWidth
= false;
511 composite
.setLayout(layout
);
512 composite
.setFont(parent
.getFont());
513 GridData buttonData
= new GridData(SWT
.FILL
, SWT
.FILL
, true, false);
514 composite
.setLayoutData(buttonData
);
516 // Trace type label ("Trace Type:")
517 Label typeLabel
= new Label(composite
, SWT
.NONE
);
518 typeLabel
.setText(Messages
.ImportTraceWizard_TraceType
);
519 typeLabel
.setFont(parent
.getFont());
522 fTraceTypes
= new Combo(composite
, SWT
.BORDER
);
523 GridData data
= new GridData(SWT
.FILL
, SWT
.FILL
, true, false, 2, 1);
524 fTraceTypes
.setLayoutData(data
);
525 fTraceTypes
.setFont(parent
.getFont());
527 String
[] availableTraceTypes
= getAvailableTraceTypes();
528 fTraceTypes
.setItems(availableTraceTypes
);
530 fTraceTypes
.addSelectionListener(new SelectionListener() {
532 public void widgetSelected(SelectionEvent e
) {
533 validateSourceGroup();
537 public void widgetDefaultSelected(SelectionEvent e
) {
542 // The mapping of available trace type IDs to their corresponding configuration element
543 private Map
<String
, IConfigurationElement
> fTraceTypeAttributes
= new HashMap
<String
, IConfigurationElement
>();
544 private Map
<String
, IConfigurationElement
> fTraceCategories
= new HashMap
<String
, IConfigurationElement
>();
545 private final Map
<String
, IConfigurationElement
> fTraceAttributes
= new HashMap
<String
, IConfigurationElement
>();
547 private String
[] getAvailableTraceTypes() {
549 // Populate the Categories and Trace Types
550 IConfigurationElement
[] config
= Platform
.getExtensionRegistry().getConfigurationElementsFor(TmfTraceType
.TMF_TRACE_TYPE_ID
);
551 for (IConfigurationElement ce
: config
) {
552 String elementName
= ce
.getName();
553 if (elementName
.equals(TmfTraceType
.TYPE_ELEM
)) {
554 String traceTypeId
= ce
.getAttribute(TmfTraceType
.ID_ATTR
);
555 fTraceTypeAttributes
.put(traceTypeId
, ce
);
556 } else if (elementName
.equals(TmfTraceType
.CATEGORY_ELEM
)) {
557 String categoryId
= ce
.getAttribute(TmfTraceType
.ID_ATTR
);
558 fTraceCategories
.put(categoryId
, ce
);
562 // Generate the list of Category:TraceType to populate the ComboBox
563 List
<String
> traceTypes
= new ArrayList
<String
>();
564 for (String typeId
: fTraceTypeAttributes
.keySet()) {
565 IConfigurationElement ce
= fTraceTypeAttributes
.get(typeId
);
566 String traceTypeName
= getCategory(ce
) + " : " + ce
.getAttribute(TmfTraceType
.NAME_ATTR
); //$NON-NLS-1$
567 fTraceAttributes
.put(traceTypeName
, ce
);
568 traceTypes
.add(traceTypeName
);
570 Collections
.sort(traceTypes
);
572 // add the custom trace types
573 for (CustomTxtTraceDefinition def
: CustomTxtTraceDefinition
.loadAll()) {
574 String traceTypeName
= CUSTOM_TXT_CATEGORY
+ " : " + def
.definitionName
; //$NON-NLS-1$
575 traceTypes
.add(traceTypeName
);
577 for (CustomXmlTraceDefinition def
: CustomXmlTraceDefinition
.loadAll()) {
578 String traceTypeName
= CUSTOM_XML_CATEGORY
+ " : " + def
.definitionName
; //$NON-NLS-1$
579 traceTypes
.add(traceTypeName
);
583 return traceTypes
.toArray(new String
[0]);
586 private String
getCategory(IConfigurationElement ce
) {
587 String categoryId
= ce
.getAttribute(TmfTraceType
.CATEGORY_ATTR
);
588 if (categoryId
!= null) {
589 IConfigurationElement category
= fTraceCategories
.get(categoryId
);
590 if (category
!= null && !category
.getName().equals("")) { //$NON-NLS-1$
591 return category
.getAttribute(TmfTraceType
.NAME_ATTR
);
594 return "[no category]"; //$NON-NLS-1$
597 // ------------------------------------------------------------------------
599 // ------------------------------------------------------------------------
601 private Button overwriteExistingResourcesCheckbox
;
602 private Button createLinksInWorkspaceButton
;
605 protected void createOptionsGroupButtons(Group optionsGroup
) {
607 // Overwrite checkbox
608 overwriteExistingResourcesCheckbox
= new Button(optionsGroup
, SWT
.CHECK
);
609 overwriteExistingResourcesCheckbox
.setFont(optionsGroup
.getFont());
610 overwriteExistingResourcesCheckbox
.setText(Messages
.ImportTraceWizard_OverwriteExistingTrace
);
611 overwriteExistingResourcesCheckbox
.setSelection(false);
613 // Create links checkbox
614 createLinksInWorkspaceButton
= new Button(optionsGroup
, SWT
.CHECK
);
615 createLinksInWorkspaceButton
.setFont(optionsGroup
.getFont());
616 createLinksInWorkspaceButton
.setText(Messages
.ImportTraceWizard_CreateLinksInWorkspace
);
617 createLinksInWorkspaceButton
.setSelection(true);
619 createLinksInWorkspaceButton
.addSelectionListener(new SelectionAdapter() {
621 public void widgetSelected(SelectionEvent e
) {
622 updateWidgetEnablements();
626 updateWidgetEnablements();
629 // ------------------------------------------------------------------------
630 // Determine if the finish button can be enabled
631 // ------------------------------------------------------------------------
634 public boolean validateSourceGroup() {
636 File sourceDirectory
= getSourceDirectory();
637 if (sourceDirectory
== null) {
638 setMessage(Messages
.ImportTraceWizard_SelectTraceSourceEmpty
);
642 if (sourceConflictsWithDestination(new Path(sourceDirectory
.getPath()))) {
644 setErrorMessage(getSourceConflictMessage());
648 List
<FileSystemElement
> resourcesToImport
= getSelectedResources();
649 if (resourcesToImport
.size() == 0) {
651 setErrorMessage(Messages
.ImportTraceWizard_SelectTraceNoneSelected
);
655 IContainer container
= getSpecifiedContainer();
656 if (container
!= null && container
.isVirtual()) {
657 if (Platform
.getPreferencesService().getBoolean(TmfUiPlugin
.PLUGIN_ID
, ResourcesPlugin
.PREF_DISABLE_LINKING
, false, null)) {
659 setErrorMessage(Messages
.ImportTraceWizard_CannotImportFilesUnderAVirtualFolder
);
662 if (createLinksInWorkspaceButton
== null || createLinksInWorkspaceButton
.getSelection() == false) {
664 setErrorMessage(Messages
.ImportTraceWizard_HaveToCreateLinksUnderAVirtualFolder
);
669 // Perform trace validation
670 String traceTypeName
= fTraceTypes
.getText();
671 if (traceTypeName
!= null && !"".equals(traceTypeName
) && //$NON-NLS-1$
672 !traceTypeName
.startsWith(CUSTOM_TXT_CATEGORY
) && !traceTypeName
.startsWith(CUSTOM_XML_CATEGORY
)) {
674 List
<File
> traces
= isolateTraces();
675 for (File trace
: traces
) {
676 ITmfTrace
<?
> tmfTrace
= null;
678 IConfigurationElement ce
= fTraceAttributes
.get(traceTypeName
);
679 tmfTrace
= (ITmfTrace
<?
>) ce
.createExecutableExtension(TmfTraceType
.TRACE_TYPE_ATTR
);
680 if (tmfTrace
!= null && !tmfTrace
.validate(fProject
, trace
.getAbsolutePath())) {
682 setErrorMessage(Messages
.ImportTraceWizard_TraceValidationFailed
);
686 } catch (CoreException e
) {
688 if (tmfTrace
!= null)
694 setErrorMessage(null);
698 private List
<File
> isolateTraces() {
700 List
<File
> traces
= new ArrayList
<File
>();
703 List
<FileSystemElement
> selectedResources
= getSelectedResources();
704 Iterator
<FileSystemElement
> resources
= selectedResources
.iterator();
706 // Get the sorted list of unique entries
707 Map
<String
, File
> fileSystemObjects
= new HashMap
<String
, File
>();
708 while (resources
.hasNext()) {
709 File resource
= (File
) resources
.next().getFileSystemObject();
710 String key
= resource
.getAbsolutePath();
711 fileSystemObjects
.put(key
, resource
);
713 List
<String
> files
= new ArrayList
<String
>(fileSystemObjects
.keySet());
714 Collections
.sort(files
);
716 // After sorting, traces correspond to the unique prefixes
717 String prefix
= null;
718 for (int i
= 0; i
< files
.size(); i
++) {
719 File file
= fileSystemObjects
.get(files
.get(i
));
720 String name
= file
.getAbsolutePath();
721 if (prefix
== null || !name
.startsWith(prefix
)) {
722 prefix
= name
; // new prefix
730 // ------------------------------------------------------------------------
731 // Import the trace(s)
732 // ------------------------------------------------------------------------
734 public boolean finish() {
735 // Ensure source is valid
736 File sourceDir
= new File(getSourceDirectoryName());
737 if (!sourceDir
.isDirectory()) {
738 setErrorMessage(Messages
.ImportTraceWizard_InvalidTraceDirectory
);
742 String sourceDirPath
;
744 sourceDirPath
= sourceDir
.getCanonicalPath();
745 } catch (IOException e
) {
746 MessageDialog
.openInformation(getContainer().getShell(), Messages
.ImportTraceWizard_Information
,
747 Messages
.ImportTraceWizard_InvalidTraceDirectory
);
751 // Save directory for next import operation
752 fRootDirectory
= getSourceDirectoryName();
754 List
<FileSystemElement
> selectedResources
= getSelectedResources();
755 Iterator
<FileSystemElement
> resources
= selectedResources
.iterator();
757 // Use a map to end up with unique resources (getSelectedResources() can return duplicates)
758 Map
<String
, File
> fileSystemObjects
= new HashMap
<String
, File
>();
759 while (resources
.hasNext()) {
760 File file
= (File
) resources
.next().getFileSystemObject();
761 String key
= file
.getAbsolutePath();
762 fileSystemObjects
.put(key
, file
);
765 if (fileSystemObjects
.size() > 0) {
766 boolean ok
= importResources(sourceDirPath
, fileSystemObjects
);
767 String traceBundle
= null;
768 String traceTypeId
= null;
769 String traceIcon
= null;
770 String traceType
= fTraceTypes
.getText();
771 boolean traceTypeOK
= false;
772 if (traceType
.startsWith(CUSTOM_TXT_CATEGORY
)) {
773 for (CustomTxtTraceDefinition def
: CustomTxtTraceDefinition
.loadAll()) {
774 if (traceType
.equals(CUSTOM_TXT_CATEGORY
+ " : " + def
.definitionName
)) { //$NON-NLS-1$
776 traceBundle
= TmfUiPlugin
.getDefault().getBundle().getSymbolicName();
777 traceTypeId
= CustomTxtTrace
.class.getCanonicalName() + ":" + def
.definitionName
; //$NON-NLS-1$
778 traceIcon
= DEFAULT_TRACE_ICON_PATH
;
782 } else if (traceType
.startsWith(CUSTOM_XML_CATEGORY
)) {
783 for (CustomXmlTraceDefinition def
: CustomXmlTraceDefinition
.loadAll()) {
784 if (traceType
.equals(CUSTOM_XML_CATEGORY
+ " : " + def
.definitionName
)) { //$NON-NLS-1$
786 traceBundle
= TmfUiPlugin
.getDefault().getBundle().getSymbolicName();
787 traceTypeId
= CustomXmlTrace
.class.getCanonicalName() + ":" + def
.definitionName
; //$NON-NLS-1$
788 traceIcon
= DEFAULT_TRACE_ICON_PATH
;
793 IConfigurationElement ce
= fTraceAttributes
.get(traceType
);
796 traceBundle
= ce
.getContributor().getName();
797 traceTypeId
= ce
.getAttribute(TmfTraceType
.ID_ATTR
);
798 traceIcon
= ce
.getAttribute(TmfTraceType
.ICON_ATTR
);
801 if (ok
&& traceTypeOK
&& !traceType
.equals("")) { //$NON-NLS-1$
802 // Tag the selected traces with their type
803 List
<String
> files
= new ArrayList
<String
>(fileSystemObjects
.keySet());
804 Collections
.sort(files
);
805 // After sorting, traces correspond to the unique prefixes
806 String prefix
= null;
807 for (int i
= 0; i
< files
.size(); i
++) {
808 File file
= fileSystemObjects
.get(files
.get(i
));
809 String name
= file
.getAbsolutePath();
810 if (prefix
== null || !name
.startsWith(prefix
)) {
811 prefix
= name
; // new prefix
812 IResource resource
= fTargetFolder
.findMember(file
.getName());
813 if (resource
!= null) {
815 // Set the trace properties for this resource
816 resource
.setPersistentProperty(TmfTraceElement
.TRACEBUNDLE
, traceBundle
);
817 resource
.setPersistentProperty(TmfTraceElement
.TRACETYPE
, traceTypeId
);
818 resource
.setPersistentProperty(TmfTraceElement
.TRACEICON
, traceIcon
);
819 for (TmfTraceElement traceElement
: TmfProjectRegistry
.getProject(resource
.getProject()).getTracesFolder().getTraces()) {
820 if (traceElement
.getName().equals(resource
.getName())) {
821 traceElement
.refreshTraceType();
825 } catch (CoreException e
) {
835 MessageDialog
.openInformation(getContainer().getShell(), Messages
.ImportTraceWizard_Information
,
836 Messages
.ImportTraceWizard_SelectTraceNoneSelected
);
840 private boolean importResources(String rootDirectory
, Map
<String
, File
> fileSystemObjects
) {
842 // Determine the sorted canonical list of items to import
843 List
<File
> fileList
= new ArrayList
<File
>();
844 for (String key
: fileSystemObjects
.keySet()) {
845 fileList
.add(fileSystemObjects
.get(key
));
847 Collections
.sort(fileList
);
849 // Perform a distinct import operation for everything that has the same prefix
850 // (distinct prefixes correspond to traces - we don't want to re-create parent structures)
852 boolean isLinked
= createLinksInWorkspaceButton
.getSelection();
853 for (int i
= 0; i
< fileList
.size(); i
++) {
854 File resource
= fileList
.get(i
);
855 File parentFolder
= new File(resource
.getParent());
857 List
<File
> subList
= new ArrayList
<File
>();
858 subList
.add(resource
);
859 if (resource
.isDirectory()) {
860 String prefix
= resource
.getAbsolutePath();
861 boolean hasSamePrefix
= true;
862 for (int j
= i
; j
< fileList
.size() && hasSamePrefix
; j
++) {
863 File res
= fileList
.get(j
);
864 hasSamePrefix
= res
.getAbsolutePath().startsWith(prefix
);
866 // Import children individually if not linked
875 // Perform the import operation for this subset
876 FileSystemStructureProvider fileSystemStructureProvider
= FileSystemStructureProvider
.INSTANCE
;
877 ImportOperation operation
= new ImportOperation(getContainerFullPath(), parentFolder
, fileSystemStructureProvider
, this,
879 operation
.setContext(getShell());
880 ok
= executeImportOperation(operation
);
886 private boolean executeImportOperation(ImportOperation op
) {
887 initializeOperation(op
);
890 getContainer().run(true, true, op
);
891 } catch (InterruptedException e
) {
893 } catch (InvocationTargetException e
) {
894 displayErrorDialog(e
.getTargetException());
898 IStatus status
= op
.getStatus();
899 if (!status
.isOK()) {
900 ErrorDialog
.openError(getContainer().getShell(), Messages
.ImportTraceWizard_ImportProblem
, null, status
);
907 private void initializeOperation(ImportOperation op
) {
908 op
.setCreateContainerStructure(false);
909 op
.setOverwriteResources(overwriteExistingResourcesCheckbox
.getSelection());
910 op
.setCreateLinks(createLinksInWorkspaceButton
.getSelection());
911 op
.setVirtualFolders(false);