/*******************************************************************************
- * Copyright (c) 2009, 2010, 2011 Ericsson
- *
+ * Copyright (c) 2009, 2013 Ericsson and others.
+ *
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v1.0 which
* accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
- *
+ *
* Contributors:
* Francois Chouinard - Initial API and implementation
* Francois Chouinard - Got rid of dependency on internal platform class
* Francois Chouinard - Complete re-design
+ * Anna Dushistova(Montavista) - [383047] NPE while importing a CFT trace
*******************************************************************************/
package org.eclipse.linuxtools.tmf.ui.project.wizards;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collections;
+import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
+import java.util.Map.Entry;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFolder;
import org.eclipse.jface.viewers.ICheckStateListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITreeContentProvider;
-import org.eclipse.linuxtools.tmf.TmfCorePlugin;
-import org.eclipse.linuxtools.tmf.TmfProjectNature;
-import org.eclipse.linuxtools.tmf.trace.ITmfTrace;
-import org.eclipse.linuxtools.tmf.ui.TmfUiPlugin;
+import org.eclipse.linuxtools.internal.tmf.ui.Activator;
+import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomTxtTrace;
+import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomTxtTraceDefinition;
+import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomXmlTrace;
+import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomXmlTraceDefinition;
+import org.eclipse.linuxtools.tmf.core.TmfCommonConstants;
+import org.eclipse.linuxtools.tmf.core.TmfProjectNature;
+import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
+import org.eclipse.linuxtools.tmf.ui.project.model.TmfProjectElement;
+import org.eclipse.linuxtools.tmf.ui.project.model.TmfProjectRegistry;
import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement;
import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceFolder;
+import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceType;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.BusyIndicator;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
-import org.eclipse.swt.widgets.Listener;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.dialogs.FileSystemElement;
import org.eclipse.ui.dialogs.WizardResourceImportPage;
import org.eclipse.ui.wizards.datatransfer.ImportOperation;
/**
- * <b><u>ImportTraceWizardPage</u></b>
- * <p>
* A variant of the standard resource import wizard with the following changes:
* <ul>
* <li>A folder/file combined checkbox tree viewer to select traces
* For our purpose, a trace can either be a single file or a whole directory sub-tree, whichever is reached first from
* the root directory.
* <p>
- * TODO: Consider adding Filter/Select/Deselect buttons
+ *
+ * @version 1.0
+ * @author Francois Chouinard
*/
-public class ImportTraceWizardPage extends WizardResourceImportPage implements Listener {
+public class ImportTraceWizardPage extends WizardResourceImportPage {
// ------------------------------------------------------------------------
// Constants
// ------------------------------------------------------------------------
static private final String IMPORT_WIZARD_PAGE = "ImportTraceWizardPage"; //$NON-NLS-1$
+ private static final String CUSTOM_TXT_CATEGORY = "Custom Text"; //$NON-NLS-1$
+ private static final String CUSTOM_XML_CATEGORY = "Custom XML"; //$NON-NLS-1$
+ private static final String DEFAULT_TRACE_ICON_PATH = "icons/elcl16/trace.gif"; //$NON-NLS-1$
// ------------------------------------------------------------------------
// Attributes
// Target import directory ('Traces' folder)
private IFolder fTargetFolder;
- // Selected trace attributes
- private final Map<String, IConfigurationElement> fTraceAttributes = new HashMap<String, IConfigurationElement>();
-
// ------------------------------------------------------------------------
// Constructors
// ------------------------------------------------------------------------
+ /**
+ * Constructor.
+ * Creates the trace wizard page.
+ *
+ * @param name The name of the page.
+ * @param selection The current selection
+ */
protected ImportTraceWizardPage(String name, IStructuredSelection selection) {
super(name, selection);
}
+ /**
+ * Constructor
+ * @param workbench The workbench reference.
+ * @param selection The current selection
+ */
public ImportTraceWizardPage(IWorkbench workbench, IStructuredSelection selection) {
this(IMPORT_WIZARD_PAGE, selection);
setTitle(Messages.ImportTraceWizard_FileSystemTitle);
// ------------------------------------------------------------------------
// WizardResourceImportPage
// ------------------------------------------------------------------------
-
+ /*
+ * (non-Javadoc)
+ * @see org.eclipse.ui.dialogs.WizardResourceImportPage#createControl(org.eclipse.swt.widgets.Composite)
+ */
@Override
public void createControl(Composite parent) {
super.createControl(parent);
}
}
+ /*
+ * (non-Javadoc)
+ * @see org.eclipse.ui.dialogs.WizardResourceImportPage#createSourceGroup(org.eclipse.swt.widgets.Composite)
+ */
@Override
protected void createSourceGroup(Composite parent) {
createDirectorySelectionGroup(parent);
validateSourceGroup();
}
+ /*
+ * (non-Javadoc)
+ * @see org.eclipse.ui.dialogs.WizardResourceImportPage#createFileSelectionGroup(org.eclipse.swt.widgets.Composite)
+ */
@Override
protected void createFileSelectionGroup(Composite parent) {
});
}
+ /*
+ * (non-Javadoc)
+ * @see org.eclipse.ui.dialogs.WizardResourceImportPage#getFolderProvider()
+ */
@Override
protected ITreeContentProvider getFolderProvider() {
return null;
}
+ /*
+ * (non-Javadoc)
+ * @see org.eclipse.ui.dialogs.WizardResourceImportPage#getFileProvider()
+ */
@Override
protected ITreeContentProvider getFileProvider() {
return new WorkbenchContentProvider() {
Object[] files = element.getFiles().getChildren();
List<Object> result = new LinkedList<Object>();
- for (Object folder : folders)
+ for (Object folder : folders) {
result.add(folder);
- for (Object file : files)
+ }
+ for (Object file : files) {
result.add(file);
+ }
return result.toArray();
}
};
}
- private void populateChildren(FileSystemElement parent) {
+ private static void populateChildren(FileSystemElement parent) {
// Do not re-populate if the job was done already...
FileSystemStructureProvider provider = FileSystemStructureProvider.INSTANCE;
if (parent.getFolders().size() == 0 && parent.getFiles().size() == 0) {
}
}
+ /*
+ * (non-Javadoc)
+ * @see org.eclipse.ui.dialogs.WizardResourceImportPage#getSelectedResources()
+ */
@Override
protected List<FileSystemElement> getSelectedResources() {
List<FileSystemElement> resources = new ArrayList<FileSystemElement>();
// Directory Selection Group (forked WizardFileSystemResourceImportPage1)
// ------------------------------------------------------------------------
+ /**
+ * The directory name field
+ */
protected Combo directoryNameField;
+ /**
+ * The directory browse button.
+ */
protected Button directoryBrowseButton;
private boolean entryChanged = false;
+ /**
+ * creates the directory selection group.
+ * @param parent the parent composite
+ */
protected void createDirectorySelectionGroup(Composite parent) {
Composite directoryContainerGroup = new Composite(parent, SWT.NONE);
// Browse for the source directory
// ------------------------------------------------------------------------
+ /*
+ * (non-Javadoc)
+ * @see org.eclipse.ui.dialogs.WizardResourceImportPage#handleEvent(org.eclipse.swt.widgets.Event)
+ */
@Override
public void handleEvent(Event event) {
if (event.widget == directoryBrowseButton) {
super.handleEvent(event);
}
+ /**
+ * Handle the button pressed event
+ */
protected void handleSourceDirectoryBrowseButtonPressed() {
String currentSource = directoryNameField.getText();
DirectoryDialog dialog = new DirectoryDialog(directoryNameField.getShell(), SWT.SAVE | SWT.SHEET);
return getSourceDirectory(directoryNameField.getText());
}
- private File getSourceDirectory(String path) {
+ private static File getSourceDirectory(String path) {
File sourceDirectory = new File(getSourceDirectoryName(path));
if (!sourceDirectory.exists() || !sourceDirectory.isDirectory()) {
return null;
return sourceDirectory;
}
- private String getSourceDirectoryName(String sourceName) {
+ private static String getSourceDirectoryName(String sourceName) {
IPath result = new Path(sourceName.trim());
if (result.getDevice() != null && result.segmentCount() == 0) {
result = result.addTrailingSeparator();
return selectFiles(sourceDirectory, FileSystemStructureProvider.INSTANCE);
}
- private FileSystemElement selectFiles(final Object rootFileSystemObject,
- final IImportStructureProvider structureProvider) {
+ private FileSystemElement selectFiles(final Object rootFileSystemObject, final IImportStructureProvider structureProvider) {
final FileSystemElement[] results = new FileSystemElement[1];
BusyIndicator.showWhile(getShell().getDisplay(), new Runnable() {
@Override
return results[0];
}
- private FileSystemElement createRootElement(Object fileSystemObject, IImportStructureProvider provider) {
+ private static FileSystemElement createRootElement(Object fileSystemObject,
+ IImportStructureProvider provider) {
boolean isContainer = provider.isFolder(fileSystemObject);
String elementLabel = provider.getLabel(fileSystemObject);
fTraceTypes.addSelectionListener(new SelectionListener() {
@Override
public void widgetSelected(SelectionEvent e) {
- validateSourceGroup();
+ updateWidgetEnablements();
}
@Override
});
}
+ // The mapping of available trace type IDs to their corresponding configuration element
+ private final Map<String, IConfigurationElement> fTraceTypeAttributes = new HashMap<String, IConfigurationElement>();
+ private final Map<String, IConfigurationElement> fTraceCategories = new HashMap<String, IConfigurationElement>();
+ private final Map<String, IConfigurationElement> fTraceAttributes = new HashMap<String, IConfigurationElement>();
+
private String[] getAvailableTraceTypes() {
- List<String> traceTypes = new ArrayList<String>();
- IConfigurationElement[] config = Platform.getExtensionRegistry().getConfigurationElementsFor(
- TmfCorePlugin.TMF_TRACE_TYPE_ID);
+
+ // Populate the Categories and Trace Types
+ IConfigurationElement[] config = Platform.getExtensionRegistry().getConfigurationElementsFor(TmfTraceType.TMF_TRACE_TYPE_ID);
for (IConfigurationElement ce : config) {
- String traceTypeName = ce.getAttribute("name"); //$NON-NLS-1$
+ String elementName = ce.getName();
+ if (elementName.equals(TmfTraceType.TYPE_ELEM)) {
+ String traceTypeId = ce.getAttribute(TmfTraceType.ID_ATTR);
+ fTraceTypeAttributes.put(traceTypeId, ce);
+ } else if (elementName.equals(TmfTraceType.CATEGORY_ELEM)) {
+ String categoryId = ce.getAttribute(TmfTraceType.ID_ATTR);
+ fTraceCategories.put(categoryId, ce);
+ }
+ }
+
+ // Generate the list of Category:TraceType to populate the ComboBox
+ List<String> traceTypes = new ArrayList<String>();
+ for (String typeId : fTraceTypeAttributes.keySet()) {
+ IConfigurationElement ce = fTraceTypeAttributes.get(typeId);
+ String traceTypeName = getCategory(ce) + " : " + ce.getAttribute(TmfTraceType.NAME_ATTR); //$NON-NLS-1$
fTraceAttributes.put(traceTypeName, ce);
traceTypes.add(traceTypeName);
}
Collections.sort(traceTypes);
- // TODO: Add Custom Parsers
+ // add the custom trace types
+ for (CustomTxtTraceDefinition def : CustomTxtTraceDefinition.loadAll()) {
+ String traceTypeName = CUSTOM_TXT_CATEGORY + " : " + def.definitionName; //$NON-NLS-1$
+ traceTypes.add(traceTypeName);
+ }
+ for (CustomXmlTraceDefinition def : CustomXmlTraceDefinition.loadAll()) {
+ String traceTypeName = CUSTOM_XML_CATEGORY + " : " + def.definitionName; //$NON-NLS-1$
+ traceTypes.add(traceTypeName);
+ }
// Format result
- String[] result = new String[traceTypes.size()];
- result = traceTypes.toArray(result);
- return result;
+ return traceTypes.toArray(new String[traceTypes.size()]);
+ }
+
+ private String getCategory(IConfigurationElement ce) {
+ String categoryId = ce.getAttribute(TmfTraceType.CATEGORY_ATTR);
+ if (categoryId != null) {
+ IConfigurationElement category = fTraceCategories.get(categoryId);
+ if (category != null && !category.getName().equals("")) { //$NON-NLS-1$
+ return category.getAttribute(TmfTraceType.NAME_ATTR);
+ }
+ }
+ return "[no category]"; //$NON-NLS-1$
}
// ------------------------------------------------------------------------
private Button overwriteExistingResourcesCheckbox;
private Button createLinksInWorkspaceButton;
+ /*
+ * (non-Javadoc)
+ * @see org.eclipse.ui.dialogs.WizardDataTransferPage#createOptionsGroupButtons(org.eclipse.swt.widgets.Group)
+ */
@Override
protected void createOptionsGroupButtons(Group optionsGroup) {
// Determine if the finish button can be enabled
// ------------------------------------------------------------------------
+ /*
+ * (non-Javadoc)
+ * @see org.eclipse.ui.dialogs.WizardDataTransferPage#validateSourceGroup()
+ */
@Override
public boolean validateSourceGroup() {
IContainer container = getSpecifiedContainer();
if (container != null && container.isVirtual()) {
- if (Platform.getPreferencesService().getBoolean(TmfUiPlugin.PLUGIN_ID,
- ResourcesPlugin.PREF_DISABLE_LINKING, false, null)) {
+ if (Platform.getPreferencesService().getBoolean(Activator.PLUGIN_ID, ResourcesPlugin.PREF_DISABLE_LINKING, false, null)) {
setMessage(null);
setErrorMessage(Messages.ImportTraceWizard_CannotImportFilesUnderAVirtualFolder);
return false;
}
- if (createLinksInWorkspaceButton == null || createLinksInWorkspaceButton.getSelection() == false) {
+ if (createLinksInWorkspaceButton == null || !createLinksInWorkspaceButton.getSelection()) {
setMessage(null);
setErrorMessage(Messages.ImportTraceWizard_HaveToCreateLinksUnderAVirtualFolder);
return false;
// Perform trace validation
String traceTypeName = fTraceTypes.getText();
- if (traceTypeName != null && !"".equals(traceTypeName)) { //$NON-NLS-1$
+ if (traceTypeName != null && !"".equals(traceTypeName) && //$NON-NLS-1$
+ !traceTypeName.startsWith(CUSTOM_TXT_CATEGORY) && !traceTypeName.startsWith(CUSTOM_XML_CATEGORY)) {
+
List<File> traces = isolateTraces();
for (File trace : traces) {
- IConfigurationElement ce = fTraceAttributes.get(traceTypeName);
- ITmfTrace<?> tmfTrace = null;
+ ITmfTrace tmfTrace = null;
try {
- tmfTrace = (ITmfTrace<?>) ce.createExecutableExtension(TmfTraceElement.TRACE_TYPE);
- if (!tmfTrace.validate(fProject, trace.getAbsolutePath())) {
+ IConfigurationElement ce = fTraceAttributes.get(traceTypeName);
+ tmfTrace = (ITmfTrace) ce.createExecutableExtension(TmfTraceType.TRACE_TYPE_ATTR);
+ if (tmfTrace != null && !tmfTrace.validate(fProject, trace.getAbsolutePath())) {
setMessage(null);
setErrorMessage(Messages.ImportTraceWizard_TraceValidationFailed);
+ tmfTrace.dispose();
return false;
}
- if (tmfTrace != null)
- tmfTrace.dispose();
} catch (CoreException e) {
} finally {
- if (tmfTrace != null)
+ if (tmfTrace != null) {
tmfTrace.dispose();
+ }
}
}
}
// Import the trace(s)
// ------------------------------------------------------------------------
+ /**
+ * Finish the import.
+ * @return <code>true</code> if successful else false
+ */
public boolean finish() {
// Ensure source is valid
File sourceDir = new File(getSourceDirectoryName());
return false;
}
- String sourceDirPath;
try {
- sourceDirPath = sourceDir.getCanonicalPath();
+ sourceDir.getCanonicalPath();
} catch (IOException e) {
MessageDialog.openInformation(getContainer().getShell(), Messages.ImportTraceWizard_Information,
Messages.ImportTraceWizard_InvalidTraceDirectory);
}
if (fileSystemObjects.size() > 0) {
- boolean ok = importResources(sourceDirPath, fileSystemObjects);
+ boolean ok = importResources(fileSystemObjects);
+ String traceBundle = null;
+ String traceTypeId = null;
+ String traceIcon = null;
String traceType = fTraceTypes.getText();
- IConfigurationElement ce = fTraceAttributes.get(traceType);
- if (ok && !traceType.equals("") && ce != null) { //$NON-NLS-1$
+ boolean traceTypeOK = false;
+ if (traceType.startsWith(CUSTOM_TXT_CATEGORY)) {
+ for (CustomTxtTraceDefinition def : CustomTxtTraceDefinition.loadAll()) {
+ if (traceType.equals(CUSTOM_TXT_CATEGORY + " : " + def.definitionName)) { //$NON-NLS-1$
+ traceTypeOK = true;
+ traceBundle = Activator.getDefault().getBundle().getSymbolicName();
+ traceTypeId = CustomTxtTrace.class.getCanonicalName() + ":" + def.definitionName; //$NON-NLS-1$
+ traceIcon = DEFAULT_TRACE_ICON_PATH;
+ break;
+ }
+ }
+ } else if (traceType.startsWith(CUSTOM_XML_CATEGORY)) {
+ for (CustomXmlTraceDefinition def : CustomXmlTraceDefinition.loadAll()) {
+ if (traceType.equals(CUSTOM_XML_CATEGORY + " : " + def.definitionName)) { //$NON-NLS-1$
+ traceTypeOK = true;
+ traceBundle = Activator.getDefault().getBundle().getSymbolicName();
+ traceTypeId = CustomXmlTrace.class.getCanonicalName() + ":" + def.definitionName; //$NON-NLS-1$
+ traceIcon = DEFAULT_TRACE_ICON_PATH;
+ break;
+ }
+ }
+ } else {
+ IConfigurationElement ce = fTraceAttributes.get(traceType);
+ if (ce != null) {
+ traceTypeOK = true;
+ traceBundle = ce.getContributor().getName();
+ traceTypeId = ce.getAttribute(TmfTraceType.ID_ATTR);
+ traceIcon = ce.getAttribute(TmfTraceType.ICON_ATTR);
+ }
+ }
+ if (ok && traceTypeOK && !traceType.equals("")) { //$NON-NLS-1$
// Tag the selected traces with their type
List<String> files = new ArrayList<String>(fileSystemObjects.keySet());
- Collections.sort(files);
+ Collections.sort(files, new Comparator<String>() {
+ @Override
+ public int compare(String o1, String o2) {
+ String v1 = o1 + File.separatorChar;
+ String v2 = o2 + File.separatorChar;
+ return v1.compareTo(v2);
+ }
+ });
// After sorting, traces correspond to the unique prefixes
String prefix = null;
for (int i = 0; i < files.size(); i++) {
File file = fileSystemObjects.get(files.get(i));
- String name = file.getAbsolutePath();
- if (prefix == null || !name.startsWith(prefix)) {
+ String name = file.getAbsolutePath() + File.separatorChar;
+ if (fTargetFolder != null && (prefix == null || !name.startsWith(prefix))) {
prefix = name; // new prefix
IResource resource = fTargetFolder.findMember(file.getName());
if (resource != null) {
try {
// Set the trace properties for this resource
- resource.setPersistentProperty(TmfTraceElement.TRACEBUNDLE, ce.getContributor()
- .getName());
- resource.setPersistentProperty(TmfTraceElement.TRACETYPE,
- ce.getAttribute(TmfTraceElement.ID));
- resource.setPersistentProperty(TmfTraceElement.TRACEICON,
- ce.getAttribute(TmfTraceElement.ICON));
+ resource.setPersistentProperty(TmfCommonConstants.TRACEBUNDLE, traceBundle);
+ resource.setPersistentProperty(TmfCommonConstants.TRACETYPE, traceTypeId);
+ resource.setPersistentProperty(TmfCommonConstants.TRACEICON, traceIcon);
+ TmfProjectElement tmfProject = TmfProjectRegistry.getProject(resource.getProject());
+ if (tmfProject != null) {
+ for (TmfTraceElement traceElement : tmfProject.getTracesFolder().getTraces()) {
+ if (traceElement.getName().equals(resource.getName())) {
+ traceElement.refreshTraceType();
+ break;
+ }
+ }
+ }
} catch (CoreException e) {
- e.printStackTrace();
+ Activator.getDefault().logError("Error importing trace resource " + resource.getName(), e); //$NON-NLS-1$
}
}
}
return false;
}
- private boolean importResources(String rootDirectory, Map<String, File> fileSystemObjects) {
+ private boolean importResources(Map<String, File> fileSystemObjects) {
// Determine the sorted canonical list of items to import
List<File> fileList = new ArrayList<File>();
- for (String key : fileSystemObjects.keySet()) {
- fileList.add(fileSystemObjects.get(key));
+ for (Entry<String, File> entry : fileSystemObjects.entrySet()) {
+ fileList.add(entry.getValue());
}
- Collections.sort(fileList);
+ Collections.sort(fileList, new Comparator<File>() {
+ @Override
+ public int compare(File o1, File o2) {
+ String v1 = o1.getAbsolutePath() + File.separatorChar;
+ String v2 = o2.getAbsolutePath() + File.separatorChar;
+ return v1.compareTo(v2);
+ }
+ });
// Perform a distinct import operation for everything that has the same prefix
// (distinct prefixes correspond to traces - we don't want to re-create parent structures)
List<File> subList = new ArrayList<File>();
subList.add(resource);
if (resource.isDirectory()) {
- String prefix = resource.getAbsolutePath();
+ String prefix = resource.getAbsolutePath() + File.separatorChar;
boolean hasSamePrefix = true;
- for (int j = i; j < fileList.size() && hasSamePrefix; j++) {
+ for (int j = i + 1; j < fileList.size() && hasSamePrefix; j++) {
File res = fileList.get(j);
hasSamePrefix = res.getAbsolutePath().startsWith(prefix);
if (hasSamePrefix) {
// Perform the import operation for this subset
FileSystemStructureProvider fileSystemStructureProvider = FileSystemStructureProvider.INSTANCE;
- ImportOperation operation = new ImportOperation(getContainerFullPath(), parentFolder,
- fileSystemStructureProvider, this, subList);
+ ImportOperation operation = new ImportOperation(getContainerFullPath(), parentFolder, fileSystemStructureProvider, this,
+ subList);
operation.setContext(getShell());
ok = executeImportOperation(operation);
}