From a2d29ca1009381662de64f171585c30ba48c4a43 Mon Sep 17 00:00:00 2001 From: Matthew Khouzam Date: Tue, 28 May 2013 15:29:48 -0400 Subject: [PATCH] tmf: fix bug when importing and no project is selected fix importing custom text and xml traces fix importing single files in copy mode when one file is imported several times, sometimes renamed after importing non-renamed versions of the same trace Change-Id: I70a13fdfd800bc46475040e6d73a86e19a6bbdc2 Signed-off-by: Matthew Khouzam Reviewed-on: https://git.eclipse.org/r/13289 Reviewed-by: Bernd Hufmann IP-Clean: Bernd Hufmann Tested-by: Bernd Hufmann Reviewed-by: Alexandre Montplaisir IP-Clean: Alexandre Montplaisir Tested-by: Hudson CI --- .../tmf/ui/project/model/TmfTraceType.java | 12 +- .../importtrace/BatchImportTraceWizard.java | 141 +++++++++++++++--- .../wizards/importtrace/FileAndName.java | 9 ++ .../ImportTraceWizardPageOptions.java | 115 ++++++++++++++ .../ImportTraceWizardScanPage.java | 4 +- .../project/wizards/importtrace/Messages.java | 2 + .../wizards/importtrace/messages.properties | 13 ++ 7 files changed, 266 insertions(+), 30 deletions(-) create mode 100644 org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/project/wizards/importtrace/ImportTraceWizardPageOptions.java diff --git a/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/project/model/TmfTraceType.java b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/project/model/TmfTraceType.java index d2d165372b..6b99f3ab6f 100644 --- a/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/project/model/TmfTraceType.java +++ b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/project/model/TmfTraceType.java @@ -44,6 +44,8 @@ import org.eclipse.ui.dialogs.FileSystemElement; */ public final class TmfTraceType { + private static final char SEPARATOR = ':'; + /** * Extension point ID */ @@ -222,9 +224,9 @@ public final class TmfTraceType { // ------------------------------------------------------------------ /** - * Returns a list of "category : tracetype , ..." + * Returns a list of "category:tracetype , ..." * - * @return returns a list of "category : tracetype , ..." + * @return returns a list of "category:tracetype , ..." * @since 2.0 */ public String[] getAvailableTraceTypes() { @@ -235,7 +237,7 @@ public final class TmfTraceType { List customTypes = getCustomTraceTypes(); for (String key : this.fTraceTypes.keySet()) { TraceTypeHelper tt = this.fTraceTypes.get(key); - traceTypes.add(tt.getCategoryName() + " : " + tt.getName()); //$NON-NLS-1$ + traceTypes.add(tt.getCategoryName() + SEPARATOR + tt.getName()); } traceTypes.addAll(customTypes); @@ -286,13 +288,13 @@ public final class TmfTraceType { // add the custom trace types for (CustomTxtTraceDefinition def : CustomTxtTraceDefinition.loadAll()) { - String traceTypeName = CUSTOM_TXT_CATEGORY + " : " + def.definitionName; //$NON-NLS-1$ + String traceTypeName = CUSTOM_TXT_CATEGORY + SEPARATOR + def.definitionName; TraceTypeHelper tt = new TraceTypeHelper(traceTypeName, CUSTOM_TXT_CATEGORY, def.definitionName, null); fTraceTypes.put(traceTypeName, tt); traceTypes.add(traceTypeName); } for (CustomXmlTraceDefinition def : CustomXmlTraceDefinition.loadAll()) { - String traceTypeName = CUSTOM_XML_CATEGORY + " : " + def.definitionName; //$NON-NLS-1$ + String traceTypeName = CUSTOM_XML_CATEGORY + SEPARATOR + def.definitionName; TraceTypeHelper tt = new TraceTypeHelper(traceTypeName, CUSTOM_TXT_CATEGORY, def.definitionName, null); fTraceTypes.put(traceTypeName, tt); traceTypes.add(traceTypeName); diff --git a/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/project/wizards/importtrace/BatchImportTraceWizard.java b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/project/wizards/importtrace/BatchImportTraceWizard.java index 94838cb861..7336ae2377 100644 --- a/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/project/wizards/importtrace/BatchImportTraceWizard.java +++ b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/project/wizards/importtrace/BatchImportTraceWizard.java @@ -13,8 +13,11 @@ package org.eclipse.linuxtools.tmf.ui.project.wizards.importtrace; import java.io.File; +import java.io.FileInputStream; import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; @@ -46,7 +49,9 @@ import org.eclipse.jface.wizard.IWizardPage; import org.eclipse.jface.wizard.WizardDialog; 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.CustomXmlTrace; import org.eclipse.linuxtools.tmf.core.TmfCommonConstants; +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; @@ -85,6 +90,7 @@ public class BatchImportTraceWizard extends ImportTraceWizard { private IWizardPage fSelectDirectoriesPage; private ImportTraceWizardScanPage fScanPage; private IWizardPage fSelectTypePage; + private IWizardPage fOptions; private final List fTraceTypesToScan = new ArrayList(); private final Set fParentFilesToScan = new HashSet(); @@ -131,6 +137,7 @@ public class BatchImportTraceWizard extends ImportTraceWizard { fSelectDirectoriesPage = new ImportTraceWizardSelectDirectoriesPage(workbench, selection); fScanPage = new ImportTraceWizardScanPage(workbench, selection); fSelectTypePage = new ImportTraceWizardSelectTraceTypePage(workbench, selection); + fOptions = new ImportTraceWizardPageOptions(workbench, selection); // keep in case it's called later Iterator iter = selection.iterator(); while (iter.hasNext()) { @@ -148,10 +155,10 @@ public class BatchImportTraceWizard extends ImportTraceWizard { addPage(fSelectTypePage); addPage(fSelectDirectoriesPage); addPage(fScanPage); + addPage(fOptions); final WizardDialog container = (WizardDialog) getContainer(); if (container != null) { container.setPageSize(WIN_WIDTH, WIN_HEIGHT); - container.updateSize(); } } @@ -250,17 +257,51 @@ public class BatchImportTraceWizard extends ImportTraceWizard { } else { List subList = new ArrayList(); - subList.add(traceToImport.getFile()); - IPath path = fTargetFolder.getFullPath().append(traceToImport.getName()); - final File parentFile = traceToImport.getFile().getParentFile(); - ImportOperation operation = new ImportOperation(path, - parentFile, fileSystemStructureProvider, overwriteQuery, - subList); - operation.setContext(getShell()); - if (executeImportOperation(operation)) { - setTraceType(traceToImport); - success = true; + IPath path = fTargetFolder.getFullPath(); + File parentFile = traceToImport.getFile(); + final boolean isFile = parentFile.isFile(); + if (isFile) { + IFile resource = ResourcesPlugin.getWorkspace().getRoot().getFile(path.append(traceToImport.getName())); + if (fOverwrite || !resource.exists()) { + subList.add(parentFile); + parentFile = parentFile.getParentFile(); + final FileInputStream source = new FileInputStream(traceToImport.getFile()); + if( resource.exists()) { + resource.delete(IResource.FORCE, new NullProgressMonitor()); + } + resource.create(source, true, new NullProgressMonitor()); + source.close(); + setTraceType(traceToImport); + success = true; + } + } else { + path = fTargetFolder.getFullPath().append(traceToImport.getName()); + File[] fileList = traceToImport.getFile().listFiles(); + for (File child : fileList) { + subList.add(child); + } + + Collections.sort(subList, new Comparator() { + @Override + public int compare(File o1, File o2) { + return o1.getAbsolutePath().compareTo(o2.getAbsolutePath()); + } + }); + + ImportOperation operation = new ImportOperation( + path, + parentFile, + fileSystemStructureProvider, + overwriteQuery, + subList); + operation.setContext(getShell()); + operation.setCreateContainerStructure(false); + if (executeImportOperation(operation)) { + setTraceType(traceToImport); + success = true; + } } + } } catch (Exception e) { } @@ -296,6 +337,7 @@ public class BatchImportTraceWizard extends ImportTraceWizard { } private IStatus setTraceType(FileAndName traceToImport) { + IStatus validate = Status.OK_STATUS; IPath path = fTargetFolder.getFullPath().append(traceToImport.getName()); IResource resource = ResourcesPlugin.getWorkspace().getRoot().findMember(path); if (resource != null) { @@ -312,16 +354,26 @@ public class BatchImportTraceWizard extends ImportTraceWizard { traceIcon = ce.getAttribute(TmfTraceType.ICON_ATTR); } final String traceType = traceTypeId; - if (!traceTypeOK && - (traceType.startsWith(TmfTraceType.CUSTOM_TXT_CATEGORY) || - traceType.startsWith(TmfTraceType.CUSTOM_XML_CATEGORY))) { + final boolean startsWithTxt = traceType.startsWith(TmfTraceType.CUSTOM_TXT_CATEGORY); + final boolean startsWithXML = traceType.startsWith(TmfTraceType.CUSTOM_XML_CATEGORY); + if (!traceTypeOK && (startsWithTxt || startsWithXML)) { + final char SEPARATOR = ':'; // do custom trace stuff here traceTypeOK = true; - traceBundle = - Activator.getDefault().getBundle().getSymbolicName(); - - traceTypeId = CustomTxtTrace.class.getCanonicalName() + ":" + traceType; //$NON-NLS-1$ - traceIcon = DEFAULT_TRACE_ICON_PATH; + String traceTypeToken[] = traceType.split(":", 2); //$NON-NLS-1$ + if (traceTypeToken.length == 2) { + traceBundle = + Activator.getDefault().getBundle().getSymbolicName(); + if (startsWithTxt) { + traceTypeId = CustomTxtTrace.class.getCanonicalName() + SEPARATOR + traceTypeToken[1]; + } + else { + traceTypeId = CustomXmlTrace.class.getCanonicalName() + SEPARATOR + traceTypeToken[1]; + } + traceIcon = DEFAULT_TRACE_ICON_PATH; + } else { + traceTypeOK = false; + } } if (traceTypeOK) { resource.setPersistentProperty(TmfCommonConstants.TRACEBUNDLE, @@ -334,24 +386,56 @@ public class BatchImportTraceWizard extends ImportTraceWizard { TmfProjectElement tmfProject = TmfProjectRegistry.getProject(resource.getProject()); if (tmfProject != null) { - for (TmfTraceElement traceElement : tmfProject.getTracesFolder().getTraces()) { + final TmfTraceFolder tracesFolder = tmfProject.getTracesFolder(); + tracesFolder.refresh(); + + List traces = tracesFolder.getTraces(); + boolean found = false; + for (TmfTraceElement traceElement : traces) { if (traceElement.getName().equals(resource.getName())) { traceElement.refreshTraceType(); + found = true; break; } } + if (!found) { + TmfTraceElement te = new TmfTraceElement(traceToImport.getName(), resource, tracesFolder); + te.refreshTraceType(); + traces = tracesFolder.getTraces(); + for (TmfTraceElement traceElement : traces) { + if (traceElement.getName().equals(resource.getName())) { + traceElement.refreshTraceType(); + ITmfTrace tmfTrace = null; + try { + tmfTrace = traceElement.instantiateTrace(); + if (tmfTrace != null) { + validate = tmfTrace.validate(tmfProject.getResource(), traceElement.getLocation().getPath()); + } else { + return new Status(IStatus.ERROR, traceElement.getName(), "File does not exist : " + traceElement.getLocation().getPath()); //$NON-NLS-1$ + } + } finally { + if (tmfTrace != null) { + tmfTrace.dispose(); + } + } + break; + } + } + + } + } } catch (CoreException e) { Activator.getDefault().logError(Messages.BatchImportTraceWizardErrorImportingTraceResource + " " + resource.getName(), e); //$NON-NLS-1$ } } - return Status.OK_STATUS; + return validate; } @Override public boolean canFinish() { - return super.canFinish() && hasTracesToImport() && !hasConflicts(); + return super.canFinish() && hasTracesToImport() && !hasConflicts() && (fTargetFolder != null); } /** @@ -476,7 +560,6 @@ public class BatchImportTraceWizard extends ImportTraceWizard { private static void initializeOperation(ImportOperation op) { op.setCreateContainerStructure(false); op.setOverwriteResources(false); - op.setCreateLinks(true); op.setVirtualFolders(false); } @@ -681,6 +764,18 @@ public class BatchImportTraceWizard extends ImportTraceWizard { */ public void setTraceFolder(IFolder targetFolder) { fTargetFolder = targetFolder; + if (this.getContainer() != null && this.getContainer().getCurrentPage() != null) { + this.getContainer().updateButtons(); + } + } + + /** + * Gets the target folder + * + * @return the target folder + */ + public IFolder getTargetFolder() { + return fTargetFolder; } } diff --git a/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/project/wizards/importtrace/FileAndName.java b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/project/wizards/importtrace/FileAndName.java index ccbbf94366..937dcb2a71 100644 --- a/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/project/wizards/importtrace/FileAndName.java +++ b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/project/wizards/importtrace/FileAndName.java @@ -118,6 +118,15 @@ class FileAndName implements Comparable { return fConflict; } + /** + * Is the fileAndName renamed + * + * @return true if the name does not match the filename + */ + public boolean isRenamed() { + return !fName.equals(fFile.getName()); + } + // ------------------------------------------------------------------------ // Comparator & Equals // ------------------------------------------------------------------------ diff --git a/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/project/wizards/importtrace/ImportTraceWizardPageOptions.java b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/project/wizards/importtrace/ImportTraceWizardPageOptions.java new file mode 100644 index 0000000000..ee124ef636 --- /dev/null +++ b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/project/wizards/importtrace/ImportTraceWizardPageOptions.java @@ -0,0 +1,115 @@ +/******************************************************************************* + * Copyright (c) 2013 Ericsson + * + * 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: + * Matthew Khouzam - Initial API and implementation + *******************************************************************************/ + +package org.eclipse.linuxtools.tmf.ui.project.wizards.importtrace; + +import java.util.LinkedHashMap; +import java.util.Map; + +import org.eclipse.core.resources.IFolder; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.ResourcesPlugin; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.linuxtools.tmf.core.TmfProjectNature; +import org.eclipse.swt.SWT; +import org.eclipse.swt.events.SelectionEvent; +import org.eclipse.swt.events.SelectionListener; +import org.eclipse.swt.layout.GridData; +import org.eclipse.swt.layout.GridLayout; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.List; +import org.eclipse.ui.IWorkbench; + +/** + * This page selects the project to import to. + * + * @author Matthew Khouzam + * @since 2.0 + */ +public class ImportTraceWizardPageOptions extends AbstractImportTraceWizardPage { + + private List fProjects; + private final Map fProjectsMap = new LinkedHashMap(); + + /** + * Import page that tells where the trace will go + * + * @param workbench + * The workbench reference. + * @param selection + * The current selection + */ + public ImportTraceWizardPageOptions(IWorkbench workbench, IStructuredSelection selection) { + super(workbench, selection); + } + + @Override + public void createControl(Composite parent) { + super.createControl(parent); + IFolder originalFolder = getBatchWizard().getTargetFolder(); + IProject proj = null; + if (originalFolder != null) { + proj = originalFolder.getProject(); + } + + Composite optionPane = (Composite) this.getControl(); + optionPane.setLayout(new GridLayout()); + optionPane.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, true, true)); + + IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects(); + + fProjects = new List(optionPane, SWT.NONE); + fProjects.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); + + for (IProject project : projects) { + try { + if (project.getNature(TmfProjectNature.ID) != null) { + final String name = project.getName(); + fProjectsMap.put(name, project); + fProjects.add(name); + } + } catch (CoreException e) { + // TODO: add a logger to activator and then log it + } + } + + fProjects.getSelection(); + fProjects.addSelectionListener(new SelectionListener() { + + private static final String TRACE = "Traces"; //$NON-NLS-1$ + + @Override + public void widgetSelected(SelectionEvent e) { + final String listItem = fProjects.getSelection()[0]; + IFolder folder = fProjectsMap.get(listItem).getFolder(TRACE); + getBatchWizard().setTraceFolder(folder); + ImportTraceWizardPageOptions.this.setErrorMessage(null); + } + + @Override + public void widgetDefaultSelected(SelectionEvent e) { + final String listItem = fProjects.getSelection()[0]; + IFolder folder = fProjectsMap.get(listItem).getFolder(TRACE); + getBatchWizard().setTraceFolder(folder); + ImportTraceWizardPageOptions.this.setErrorMessage(null); + } + }); + if (proj != null) { + fProjects.setSelection(fProjects.indexOf(proj.getName())); + this.setErrorMessage(null); + } else { + this.setErrorMessage(Messages.SharedSelectProject); + } + } + +} diff --git a/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/project/wizards/importtrace/ImportTraceWizardScanPage.java b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/project/wizards/importtrace/ImportTraceWizardScanPage.java index 4983d91563..0243bf50c9 100644 --- a/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/project/wizards/importtrace/ImportTraceWizardScanPage.java +++ b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/project/wizards/importtrace/ImportTraceWizardScanPage.java @@ -132,7 +132,7 @@ public class ImportTraceWizardScanPage extends AbstractImportTraceWizardPage { // -------------------- TreeViewerColumn column = new TreeViewerColumn(traceTypeViewer, SWT.NONE); column.getColumn().setWidth(COL_WIDTH); - column.getColumn().setText(Messages.ImportTraceWizardImportCaption); + column.getColumn().setText(Messages.ImportTraceWizardTraceDisplayName); column.setLabelProvider(new FirstColumnLabelProvider()); column.setEditingSupport(new ColumnEditorSupport(traceTypeViewer, textCellEditor)); @@ -142,7 +142,7 @@ public class ImportTraceWizardScanPage extends AbstractImportTraceWizardPage { column = new TreeViewerColumn(traceTypeViewer, SWT.NONE); column.getColumn().setWidth(COL_WIDTH); - column.getColumn().setText(Messages.ImportTraceWizardTraceDisplayName); + column.getColumn().setText(Messages.ImportTraceWizardImportCaption); column.setLabelProvider(new ColumnLabelProvider() { @Override public String getText(Object element) { diff --git a/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/project/wizards/importtrace/Messages.java b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/project/wizards/importtrace/Messages.java index 31be6b4e3b..189ed27835 100644 --- a/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/project/wizards/importtrace/Messages.java +++ b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/project/wizards/importtrace/Messages.java @@ -46,6 +46,8 @@ public class Messages extends NLS { public static String BatchImportTraceWizardErrorImportingTraceResource; + public static String SharedSelectProject; + static { // initialize resource bundle NLS.initializeMessages(BUNDLE_NAME, Messages.class); diff --git a/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/project/wizards/importtrace/messages.properties b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/project/wizards/importtrace/messages.properties index e2ce68749f..164b82fcff 100644 --- a/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/project/wizards/importtrace/messages.properties +++ b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/project/wizards/importtrace/messages.properties @@ -1,3 +1,15 @@ +############################################################################### +# Copyright (c) 2013 Ericsson +# +# 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: +# Ericsson - Initial API and implementation +############################################################################### + ImportTraceWizardImportCaption=Trace to import ImportTraceWizardTraceDisplayName=Trace display name ImportTraceWizardLinkTraces=Link traces (Recommended) @@ -19,3 +31,4 @@ BatchImportTraceWizardRemove=Removing BatchImportTraceWizardAdd=Adding BatchImportTraceWizardErrorImportingTraceResource=Error importing trace resource ImportTraceWizardImportProblem=Error +SharedSelectProject=Please select a project to import to -- 2.34.1