Search in sources :

Example 1 with TmfCommonProjectElement

use of org.eclipse.tracecompass.tmf.ui.project.model.TmfCommonProjectElement in project tracecompass by tracecompass.

the class XMLAnalysesManagerPreferencePage method deleteSupplementaryFiles.

/**
 * Delete the supplementary files associated with XML analysis files.
 *
 * @param xmlFiles
 *            the xml analysis files (with extension)
 * @return the list of elements that should be re-opened
 */
private static Collection<TmfCommonProjectElement> deleteSupplementaryFiles(Collection<String> xmlFiles) {
    // 1. Look for all traces that have these analyses
    // 2. Close them if they are opened, but remember them
    // 3. Delete the related supplementary files
    Collection<TmfCommonProjectElement> toReopen = new ArrayList<>();
    List<IResource> resourceToDelete = new ArrayList<>();
    Set<String> ids = Sets.newHashSet();
    xmlFiles.forEach(xmlFile -> ids.addAll(XmlUtils.getAnalysisIdsFromFile(xmlFile)));
    IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects(0);
    for (IProject project : projects) {
        TmfProjectElement pElement = TmfProjectRegistry.getProject(project);
        if (pElement != null) {
            List<TmfCommonProjectElement> tElements = new ArrayList<>();
            TmfTraceFolder tracesFolder = pElement.getTracesFolder();
            if (tracesFolder != null) {
                tElements.addAll(tracesFolder.getTraces());
            }
            TmfExperimentFolder experimentsFolder = pElement.getExperimentsFolder();
            if (experimentsFolder != null) {
                tElements.addAll(experimentsFolder.getExperiments());
            }
            Set<IEditorReference> editorReferences = getEditorReferences();
            for (TmfCommonProjectElement tElement : tElements) {
                for (IResource resource : tElement.getSupplementaryResources()) {
                    for (String id : ids) {
                        if (resource.getName().startsWith(id)) {
                            resourceToDelete.add(resource);
                        }
                    }
                }
                IFile file = tElement.getBookmarksFile();
                FileEditorInput input = new FileEditorInput(file);
                boolean open = Iterables.any(editorReferences, editorReference -> {
                    try {
                        return editorReference.getEditorInput().equals(input);
                    } catch (PartInitException e) {
                        // $NON-NLS-1$ //$NON-NLS-2$
                        Activator.logError("Failed to test the " + tElement.getName() + " editor", e);
                        return false;
                    }
                });
                if (open) {
                    toReopen.add(tElement);
                }
                tElement.closeEditors();
            }
        }
    }
    for (IResource resource : resourceToDelete) {
        try {
            resource.delete(false, null);
        } catch (CoreException e) {
            Activator.logError(NLS.bind(Messages.ManageXMLAnalysisDialog_DeleteFileError, resource.getName()));
        }
    }
    return toReopen;
}
Also used : IFile(org.eclipse.core.resources.IFile) TmfExperimentFolder(org.eclipse.tracecompass.tmf.ui.project.model.TmfExperimentFolder) ArrayList(java.util.ArrayList) TmfProjectElement(org.eclipse.tracecompass.tmf.ui.project.model.TmfProjectElement) TmfCommonProjectElement(org.eclipse.tracecompass.tmf.ui.project.model.TmfCommonProjectElement) IProject(org.eclipse.core.resources.IProject) IEditorReference(org.eclipse.ui.IEditorReference) CoreException(org.eclipse.core.runtime.CoreException) TmfTraceFolder(org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceFolder) FileEditorInput(org.eclipse.ui.part.FileEditorInput) PartInitException(org.eclipse.ui.PartInitException) IResource(org.eclipse.core.resources.IResource)

Example 2 with TmfCommonProjectElement

use of org.eclipse.tracecompass.tmf.ui.project.model.TmfCommonProjectElement in project tracecompass by tracecompass.

the class XMLAnalysesManagerPreferencePage method importAnalysis.

/**
 * Import new analysis.
 */
private void importAnalysis() {
    FileDialog dialog = TmfFileDialogFactory.create(Display.getCurrent().getActiveShell(), SWT.OPEN | SWT.MULTI);
    dialog.setText(Messages.ManageXMLAnalysisDialog_SelectFilesImport);
    // $NON-NLS-1$ //$NON-NLS-2$
    dialog.setFilterNames(new String[] { Messages.ManageXMLAnalysisDialog_ImportXmlFile + " (" + XML_FILTER_EXTENSION + ")" });
    dialog.setFilterExtensions(new String[] { XML_FILTER_EXTENSION });
    dialog.open();
    String directoryPath = dialog.getFilterPath();
    if (!directoryPath.isEmpty()) {
        File directory = new File(directoryPath);
        String[] files = dialog.getFileNames();
        Collection<String> filesToProcess = Lists.newArrayList();
        for (String fileName : files) {
            File file = new File(directory, fileName);
            if (loadXmlFile(file, true)) {
                filesToProcess.add(file.getName());
            }
        }
        if (!filesToProcess.isEmpty()) {
            fillAnalysesTable();
            Collection<TmfCommonProjectElement> elements = deleteSupplementaryFiles(filesToProcess);
            XmlAnalysisModuleSource.notifyModuleChange();
            refreshProject(elements);
        }
    }
}
Also used : TmfCommonProjectElement(org.eclipse.tracecompass.tmf.ui.project.model.TmfCommonProjectElement) FileDialog(org.eclipse.swt.widgets.FileDialog) IFile(org.eclipse.core.resources.IFile) File(java.io.File)

Example 3 with TmfCommonProjectElement

use of org.eclipse.tracecompass.tmf.ui.project.model.TmfCommonProjectElement in project tracecompass by tracecompass.

the class ExportTracePackageWizardPage method createElementViewerInput.

@Override
protected Object createElementViewerInput() {
    List<TracePackageTraceElement> traceElements = new ArrayList<>();
    for (TmfCommonProjectElement projectElement : fSelectedTraces) {
        TracePackageTraceElement traceElement;
        if (projectElement instanceof TmfExperimentElement) {
            TmfExperimentElement experimentElement = (TmfExperimentElement) projectElement;
            traceElement = new TracePackageExperimentElement(null, experimentElement);
            for (TmfTraceElement expTrace : experimentElement.getTraces()) {
                ((TracePackageExperimentElement) traceElement).addExpTrace(expTrace.getElementPath());
            }
        } else {
            traceElement = new TracePackageTraceElement(null, projectElement);
        }
        // Trace files
        TracePackageFilesElement filesElement = new TracePackageFilesElement(traceElement, projectElement.getResource());
        filesElement.setChecked(true);
        // Supplementary files
        try {
            String supplementaryFolder = projectElement.getResource().getPersistentProperty(TmfCommonConstants.TRACE_SUPPLEMENTARY_FOLDER);
            TracePackageSupplFilesElement suppFilesElement = new TracePackageSupplFilesElement(null);
            IResource[] supplementaryResources = projectElement.getSupplementaryResources();
            for (IResource res : supplementaryResources) {
                String name = supplementaryFolder == null ? res.getName() : res.getLocation().makeRelativeTo(new Path(supplementaryFolder)).toString();
                new TracePackageSupplFileElement(res, name, suppFilesElement);
            }
            if (supplementaryFolder != null) {
                // If the supplementary folder path is in a shadow project,
                // the resources must be taken from the parent project.
                IFolder projectSupplementaryFolder = projectElement.getProject().getSupplementaryFolder();
                IPath path = new Path(supplementaryFolder).append(TmfCommonConstants.TRACE_PROPERTIES_FOLDER);
                IFolder propertiesFolder = projectSupplementaryFolder.getFolder(path.makeRelativeTo(projectSupplementaryFolder.getLocation()));
                if (propertiesFolder.exists()) {
                    for (IResource res : propertiesFolder.members()) {
                        String name = res.getLocation().makeRelativeTo(new Path(supplementaryFolder)).toString();
                        new TracePackageSupplFileElement(res, name, suppFilesElement);
                    }
                }
            }
            if (suppFilesElement.getChildren().length > 0) {
                traceElement.addChild(suppFilesElement);
            }
        } catch (CoreException e) {
            // Should not happen
            // $NON-NLS-1$
            Activator.getDefault().logError("Error finding supplementary files", e);
        }
        // Bookmarks
        IFile bookmarksFile = projectElement.getBookmarksFile();
        if (bookmarksFile != null && bookmarksFile.exists()) {
            IMarker[] findMarkers;
            try {
                findMarkers = bookmarksFile.findMarkers(IMarker.BOOKMARK, false, IResource.DEPTH_ZERO);
                if (findMarkers.length > 0) {
                    new TracePackageBookmarkElement(traceElement, null);
                }
            } catch (CoreException e) {
                // Should not happen since we just checked bookmarksFile.exists() but log it just in case
                // $NON-NLS-1$
                Activator.getDefault().logError("Error finding bookmarks", e);
            }
        }
        traceElements.add(traceElement);
    }
    return traceElements.toArray(new TracePackageTraceElement[] {});
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) TracePackageExperimentElement(org.eclipse.tracecompass.internal.tmf.ui.project.wizards.tracepkg.TracePackageExperimentElement) TmfTraceElement(org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceElement) TracePackageFilesElement(org.eclipse.tracecompass.internal.tmf.ui.project.wizards.tracepkg.TracePackageFilesElement) TracePackageBookmarkElement(org.eclipse.tracecompass.internal.tmf.ui.project.wizards.tracepkg.TracePackageBookmarkElement) ArrayList(java.util.ArrayList) TmfCommonProjectElement(org.eclipse.tracecompass.tmf.ui.project.model.TmfCommonProjectElement) TracePackageSupplFilesElement(org.eclipse.tracecompass.internal.tmf.ui.project.wizards.tracepkg.TracePackageSupplFilesElement) CoreException(org.eclipse.core.runtime.CoreException) TracePackageTraceElement(org.eclipse.tracecompass.internal.tmf.ui.project.wizards.tracepkg.TracePackageTraceElement) TmfExperimentElement(org.eclipse.tracecompass.tmf.ui.project.model.TmfExperimentElement) IMarker(org.eclipse.core.resources.IMarker) IResource(org.eclipse.core.resources.IResource) TracePackageSupplFileElement(org.eclipse.tracecompass.internal.tmf.ui.project.wizards.tracepkg.TracePackageSupplFileElement) IFolder(org.eclipse.core.resources.IFolder)

Example 4 with TmfCommonProjectElement

use of org.eclipse.tracecompass.tmf.ui.project.model.TmfCommonProjectElement in project tracecompass by tracecompass.

the class TracePackageExportOperation method exportSupplementaryFiles.

private void exportSupplementaryFiles(IProgressMonitor monitor, Node commonNode, TmfCommonProjectElement commonElement, TracePackageSupplFilesElement element) throws InterruptedException, CoreException {
    Document doc = commonNode.getOwnerDocument();
    if (element.getChildren().length > 0) {
        IPath projectPath = commonElement.getProject().getPath();
        for (TracePackageElement child : element.getChildren()) {
            TracePackageSupplFileElement supplFile = (TracePackageSupplFileElement) child;
            ModalContext.checkCanceled(monitor);
            IResource res = supplFile.getResource();
            // project/.tracing/A/B/statistics.ht -> .tracing/A/B/statistics.ht
            IPath relativeToExportFolder = res.getFullPath().makeRelativeTo(projectPath);
            // project/.traceExport/.tracing/A/B
            IFolder folder = fExportFolder.getFolder(relativeToExportFolder.removeLastSegments(1));
            TraceUtils.createFolder(folder, SubMonitor.convert(monitor));
            res.refreshLocal(0, SubMonitor.convert(monitor));
            createExportResource(folder, res);
            Element suppFileElement = doc.createElement(ITracePackageConstants.SUPPLEMENTARY_FILE_ELEMENT);
            suppFileElement.setAttribute(ITracePackageConstants.SUPPLEMENTARY_FILE_NAME_ATTRIB, relativeToExportFolder.toString());
            commonNode.appendChild(suppFileElement);
        }
        IFolder suppFilesFolder = fExportFolder.getFolder(TmfCommonConstants.TRACE_SUPPLEMENTARY_FOLDER_NAME);
        fResources.add(suppFilesFolder);
    }
}
Also used : IPath(org.eclipse.core.runtime.IPath) TmfCommonProjectElement(org.eclipse.tracecompass.tmf.ui.project.model.TmfCommonProjectElement) TracePackageFilesElement(org.eclipse.tracecompass.internal.tmf.ui.project.wizards.tracepkg.TracePackageFilesElement) TracePackageElement(org.eclipse.tracecompass.internal.tmf.ui.project.wizards.tracepkg.TracePackageElement) TmfProjectModelElement(org.eclipse.tracecompass.tmf.ui.project.model.TmfProjectModelElement) TracePackageTraceElement(org.eclipse.tracecompass.internal.tmf.ui.project.wizards.tracepkg.TracePackageTraceElement) TracePackageSupplFilesElement(org.eclipse.tracecompass.internal.tmf.ui.project.wizards.tracepkg.TracePackageSupplFilesElement) TmfTraceElement(org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceElement) TmfExperimentElement(org.eclipse.tracecompass.tmf.ui.project.model.TmfExperimentElement) TracePackageSupplFileElement(org.eclipse.tracecompass.internal.tmf.ui.project.wizards.tracepkg.TracePackageSupplFileElement) Element(org.w3c.dom.Element) TracePackageExperimentElement(org.eclipse.tracecompass.internal.tmf.ui.project.wizards.tracepkg.TracePackageExperimentElement) TracePackageBookmarkElement(org.eclipse.tracecompass.internal.tmf.ui.project.wizards.tracepkg.TracePackageBookmarkElement) TracePackageElement(org.eclipse.tracecompass.internal.tmf.ui.project.wizards.tracepkg.TracePackageElement) Document(org.w3c.dom.Document) TracePackageSupplFileElement(org.eclipse.tracecompass.internal.tmf.ui.project.wizards.tracepkg.TracePackageSupplFileElement) IResource(org.eclipse.core.resources.IResource) IFolder(org.eclipse.core.resources.IFolder)

Example 5 with TmfCommonProjectElement

use of org.eclipse.tracecompass.tmf.ui.project.model.TmfCommonProjectElement in project tracecompass by tracecompass.

the class TracePackageImportOperation method importBookmarks.

private void importBookmarks(IResource traceRes, TracePackageTraceElement traceElement, IProgressMonitor monitor) {
    for (TracePackageElement o : traceElement.getChildren()) {
        if (o instanceof TracePackageBookmarkElement && o.isChecked()) {
            // Get element
            IFile bookmarksFile = null;
            TmfCommonProjectElement projectElement = getMatchingProjectElement(traceElement);
            if (projectElement != null) {
                try {
                    bookmarksFile = projectElement.createBookmarksFile(monitor);
                    // Make sure that if a bookmark is double-clicked first
                    // before opening the trace, it opens the right editor
                    // Get the editor id from the extension point
                    String traceEditorId = projectElement.getEditorId();
                    final String editorId = (traceEditorId != null) ? traceEditorId : TmfEventsEditor.ID;
                    IDE.setDefaultEditor(bookmarksFile, editorId);
                } catch (CoreException e) {
                    Activator.getDefault().logError(MessageFormat.format(Messages.TracePackageImportOperation_ErrorCreatingBookmarkFile, traceRes.getName()), e);
                }
            }
            if (bookmarksFile == null) {
                break;
            }
            TracePackageBookmarkElement bookmarkElement = (TracePackageBookmarkElement) o;
            List<Map<String, String>> bookmarks = bookmarkElement.getBookmarks();
            for (Map<String, String> attrs : bookmarks) {
                IMarker createMarker = null;
                try {
                    createMarker = bookmarksFile.createMarker(IMarker.BOOKMARK);
                } catch (CoreException e) {
                    Activator.getDefault().logError(MessageFormat.format(Messages.TracePackageImportOperation_ErrorCreatingBookmark, traceRes.getName()), e);
                }
                if (createMarker != null && createMarker.exists()) {
                    try {
                        for (Entry<String, String> entry : attrs.entrySet()) {
                            String key = entry.getKey();
                            String value = entry.getValue();
                            if (key.equals(IMarker.LOCATION)) {
                                try {
                                    /* try location as an integer for backward compatibility */
                                    createMarker.setAttribute(IMarker.LOCATION, Integer.parseInt(value));
                                } catch (NumberFormatException e) {
                                    createMarker.setAttribute(IMarker.LOCATION, value);
                                }
                            } else {
                                createMarker.setAttribute(key, value);
                            }
                        }
                    } catch (CoreException e) {
                        Activator.getDefault().logError(MessageFormat.format(Messages.TracePackageImportOperation_ErrorCreatingBookmark, traceRes.getName()), e);
                    }
                }
            }
        }
    }
    monitor.worked(1);
}
Also used : IFile(org.eclipse.core.resources.IFile) CoreException(org.eclipse.core.runtime.CoreException) TracePackageBookmarkElement(org.eclipse.tracecompass.internal.tmf.ui.project.wizards.tracepkg.TracePackageBookmarkElement) TracePackageElement(org.eclipse.tracecompass.internal.tmf.ui.project.wizards.tracepkg.TracePackageElement) TmfCommonProjectElement(org.eclipse.tracecompass.tmf.ui.project.model.TmfCommonProjectElement) IMarker(org.eclipse.core.resources.IMarker) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

TmfCommonProjectElement (org.eclipse.tracecompass.tmf.ui.project.model.TmfCommonProjectElement)24 TmfTraceElement (org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceElement)10 CoreException (org.eclipse.core.runtime.CoreException)8 IResource (org.eclipse.core.resources.IResource)7 IPath (org.eclipse.core.runtime.IPath)7 IStatus (org.eclipse.core.runtime.IStatus)7 TmfExperimentElement (org.eclipse.tracecompass.tmf.ui.project.model.TmfExperimentElement)7 ArrayList (java.util.ArrayList)6 Path (org.eclipse.core.runtime.Path)6 TracePackageElement (org.eclipse.tracecompass.internal.tmf.ui.project.wizards.tracepkg.TracePackageElement)6 TmfTraceFolder (org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceFolder)6 IFile (org.eclipse.core.resources.IFile)5 IFolder (org.eclipse.core.resources.IFolder)5 TracePackageBookmarkElement (org.eclipse.tracecompass.internal.tmf.ui.project.wizards.tracepkg.TracePackageBookmarkElement)5 TracePackageExperimentElement (org.eclipse.tracecompass.internal.tmf.ui.project.wizards.tracepkg.TracePackageExperimentElement)5 TracePackageSupplFileElement (org.eclipse.tracecompass.internal.tmf.ui.project.wizards.tracepkg.TracePackageSupplFileElement)5 TracePackageSupplFilesElement (org.eclipse.tracecompass.internal.tmf.ui.project.wizards.tracepkg.TracePackageSupplFilesElement)5 TracePackageTraceElement (org.eclipse.tracecompass.internal.tmf.ui.project.wizards.tracepkg.TracePackageTraceElement)5 IProject (org.eclipse.core.resources.IProject)4 Status (org.eclipse.core.runtime.Status)4