/*******************************************************************************
- * Copyright (c) 2009, 2010, 2011, 2012 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 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.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;
* A variant of the standard resource import wizard with the following changes:
* <ul>
* <li>A folder/file combined checkbox tree viewer to select traces
- * <li>Cherry-picking of traces in the file structure without re-creating the file hierarchy
+ * <li>Cherry-picking of traces in the file structure without re-creating the
+ * file hierarchy
* <li>A trace types dropbox for optional characterization
* </ul>
- * 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.
+ * 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>
- *
+ *
* @version 1.0
* @author Francois Chouinard
*/
-public class ImportTraceWizardPage extends WizardResourceImportPage implements Listener {
+public class ImportTraceWizardPage extends WizardResourceImportPage {
// ------------------------------------------------------------------------
// Constants
// ------------------------------------------------------------------------
/**
- * Constructor.
- * Creates the trace wizard page.
- *
- * @param name The name of the page.
- * @param selection The current selection
+ * 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
+ *
+ * @param workbench
+ * The workbench reference.
+ * @param selection
+ * The current selection
*/
public ImportTraceWizardPage(IWorkbench workbench, IStructuredSelection selection) {
this(IMPORT_WIZARD_PAGE, selection);
// ------------------------------------------------------------------------
/*
* (non-Javadoc)
- * @see org.eclipse.ui.dialogs.WizardResourceImportPage#createControl(org.eclipse.swt.widgets.Composite)
+ *
+ * @see
+ * org.eclipse.ui.dialogs.WizardResourceImportPage#createControl(org.eclipse
+ * .swt.widgets.Composite)
*/
@Override
public void createControl(Composite parent) {
/*
* (non-Javadoc)
- * @see org.eclipse.ui.dialogs.WizardResourceImportPage#createSourceGroup(org.eclipse.swt.widgets.Composite)
+ *
+ * @see
+ * org.eclipse.ui.dialogs.WizardResourceImportPage#createSourceGroup(org
+ * .eclipse.swt.widgets.Composite)
*/
@Override
protected void createSourceGroup(Composite parent) {
/*
* (non-Javadoc)
- * @see org.eclipse.ui.dialogs.WizardResourceImportPage#createFileSelectionGroup(org.eclipse.swt.widgets.Composite)
+ *
+ * @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
/*
* (non-Javadoc)
+ *
* @see org.eclipse.ui.dialogs.WizardResourceImportPage#getFileProvider()
*/
@Override
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()
+ *
+ * @see
+ * org.eclipse.ui.dialogs.WizardResourceImportPage#getSelectedResources()
*/
@Override
protected List<FileSystemElement> getSelectedResources() {
/**
* creates the directory selection group.
- * @param parent the parent composite
+ *
+ * @param parent
+ * the parent composite
*/
protected void createDirectorySelectionGroup(Composite parent) {
/*
* (non-Javadoc)
- * @see org.eclipse.ui.dialogs.WizardResourceImportPage#handleEvent(org.eclipse.swt.widgets.Event)
+ *
+ * @see
+ * org.eclipse.ui.dialogs.WizardResourceImportPage#handleEvent(org.eclipse
+ * .swt.widgets.Event)
*/
@Override
public void handleEvent(Event event) {
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 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 Map<String, IConfigurationElement> fTraceTypeAttributes = new HashMap<String, IConfigurationElement>();
- private Map<String, IConfigurationElement> fTraceCategories = new HashMap<String, IConfigurationElement>();
+ // 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() {
/*
* (non-Javadoc)
- * @see org.eclipse.ui.dialogs.WizardDataTransferPage#createOptionsGroupButtons(org.eclipse.swt.widgets.Group)
+ *
+ * @see
+ * org.eclipse.ui.dialogs.WizardDataTransferPage#createOptionsGroupButtons
+ * (org.eclipse.swt.widgets.Group)
*/
@Override
protected void createOptionsGroupButtons(Group optionsGroup) {
/*
* (non-Javadoc)
+ *
* @see org.eclipse.ui.dialogs.WizardDataTransferPage#validateSourceGroup()
*/
@Override
// Perform trace validation
String traceTypeName = fTraceTypes.getText();
if (traceTypeName != null && !"".equals(traceTypeName) && //$NON-NLS-1$
- !traceTypeName.startsWith(CUSTOM_TXT_CATEGORY) && !traceTypeName.startsWith(CUSTOM_XML_CATEGORY)) {
+ !traceTypeName.startsWith(CUSTOM_TXT_CATEGORY) && !traceTypeName.startsWith(CUSTOM_XML_CATEGORY)) {
List<File> traces = isolateTraces();
for (File trace : traces) {
- ITmfTrace<?> tmfTrace = null;
+ ITmfTrace tmfTrace = null;
+
try {
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;
+ tmfTrace = (ITmfTrace) ce.createExecutableExtension(TmfTraceType.TRACE_TYPE_ATTR);
+ if (tmfTrace != null) {
+ IStatus status = tmfTrace.validate(fProject, trace.getAbsolutePath());
+ if (!status.isOK()) {
+ setMessage(null);
+ setErrorMessage(Messages.ImportTraceWizard_TraceValidationFailed);
+ tmfTrace.dispose();
+ return false;
+ }
}
} catch (CoreException e) {
} finally {
- if (tmfTrace != null)
+ if (tmfTrace != null) {
tmfTrace.dispose();
+ }
}
}
}
/**
* Finish the import.
+ *
* @return <code>true</code> if successful else false
*/
public boolean finish() {
return false;
}
- String sourceDirPath;
try {
- sourceDirPath = sourceDir.getCanonicalPath();
+ sourceDir.getCanonicalPath();
} catch (IOException e) {
MessageDialog.openInformation(getContainer().getShell(), Messages.ImportTraceWizard_Information,
Messages.ImportTraceWizard_InvalidTraceDirectory);
List<FileSystemElement> selectedResources = getSelectedResources();
Iterator<FileSystemElement> resources = selectedResources.iterator();
- // Use a map to end up with unique resources (getSelectedResources() can return duplicates)
+ // Use a map to end up with unique resources (getSelectedResources() can
+ // return duplicates)
Map<String, File> fileSystemObjects = new HashMap<String, File>();
while (resources.hasNext()) {
File file = (File) resources.next().getFileSystemObject();
}
if (fileSystemObjects.size() > 0) {
- boolean ok = importResources(sourceDirPath, fileSystemObjects);
+ boolean ok = importResources(fileSystemObjects);
String traceBundle = null;
String traceTypeId = null;
String traceIcon = null;
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();
+ String name = file.getAbsolutePath() + File.separatorChar;
if (fTargetFolder != null && (prefix == null || !name.startsWith(prefix))) {
prefix = name; // new prefix
IResource resource = fTargetFolder.findMember(file.getName());
resource.setPersistentProperty(TmfCommonConstants.TRACEBUNDLE, traceBundle);
resource.setPersistentProperty(TmfCommonConstants.TRACETYPE, traceTypeId);
resource.setPersistentProperty(TmfCommonConstants.TRACEICON, traceIcon);
- for (TmfTraceElement traceElement : TmfProjectRegistry.getProject(resource.getProject()).getTracesFolder().getTraces()) {
- if (traceElement.getName().equals(resource.getName())) {
- traceElement.refreshTraceType();
- break;
+ 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) {
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 (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)
+ // 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)
boolean ok = true;
boolean isLinked = createLinksInWorkspaceButton.getSelection();
for (int i = 0; i < fileList.size(); i++) {
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) {