From 099a269e472238e5e4e30c2fbbeb6ad175949fef Mon Sep 17 00:00:00 2001 From: Patrick Tasse Date: Thu, 1 May 2014 20:34:10 -0400 Subject: [PATCH] tmf: Improve Delete Supplementary Files command - Make all checkbox elements checked by default in the dialog - Allow multiple selection as long as at least one of the selected elements has supplementary files - Update trace handler to support both traces and experiments, and delete the experiment handler - Ensure supplementary files for a particular trace are only shown once in the list (e.g. same trace selected both under Traces folder and under experiment, or two experiments selected that contain the same trace) - Ensure only traces and experiments that have supplementary resources to delete have their editors closed, and that their project is refreshed only once. Change-Id: I91f823c67c15da699c815fdf9daf9cfec5082ec9 Signed-off-by: Patrick Tasse Signed-off-by: Alexandre Montplaisir Reviewed-on: https://git.eclipse.org/r/25858 Tested-by: Hudson CI --- .../META-INF/MANIFEST.MF | 3 +- org.eclipse.linuxtools.tmf.ui/plugin.xml | 63 ++------ .../SelectSupplementaryResourcesDialog.java | 29 ++-- ...teExperimentSupplementaryFilesHandler.java | 141 ------------------ .../DeleteTraceSupplementaryFilesHandler.java | 121 ++++++++++----- 5 files changed, 117 insertions(+), 240 deletions(-) delete mode 100644 org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/internal/tmf/ui/project/handlers/DeleteExperimentSupplementaryFilesHandler.java diff --git a/org.eclipse.linuxtools.tmf.ui/META-INF/MANIFEST.MF b/org.eclipse.linuxtools.tmf.ui/META-INF/MANIFEST.MF index cf7bbea22e..547ae55b6c 100644 --- a/org.eclipse.linuxtools.tmf.ui/META-INF/MANIFEST.MF +++ b/org.eclipse.linuxtools.tmf.ui/META-INF/MANIFEST.MF @@ -73,5 +73,6 @@ Export-Package: org.eclipse.linuxtools.internal.tmf.ui;x-friends:="org.eclipse.l org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model, org.eclipse.linuxtools.tmf.ui.widgets.timegraph.widgets, org.eclipse.linuxtools.tmf.ui.widgets.virtualtable -Import-Package: org.eclipse.emf.common.util, +Import-Package: com.google.common.collect, + org.eclipse.emf.common.util, org.eclipse.emf.ecore diff --git a/org.eclipse.linuxtools.tmf.ui/plugin.xml b/org.eclipse.linuxtools.tmf.ui/plugin.xml index 9688e349b8..5500c4c725 100644 --- a/org.eclipse.linuxtools.tmf.ui/plugin.xml +++ b/org.eclipse.linuxtools.tmf.ui/plugin.xml @@ -665,26 +665,25 @@ - - - - - - - - - - - - + + + + + + + + + + @@ -1419,19 +1418,6 @@ - - - - - - - - - - - - - - - - - - - - - - diff --git a/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/internal/tmf/ui/project/dialogs/SelectSupplementaryResourcesDialog.java b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/internal/tmf/ui/project/dialogs/SelectSupplementaryResourcesDialog.java index 5a1673589b..501cc55a7d 100644 --- a/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/internal/tmf/ui/project/dialogs/SelectSupplementaryResourcesDialog.java +++ b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/internal/tmf/ui/project/dialogs/SelectSupplementaryResourcesDialog.java @@ -120,9 +120,6 @@ public class SelectSupplementaryResourcesDialog extends Dialog { @Override public boolean hasChildren(Object element) { - if (element instanceof IResource[]) { - return true; - } return false; } @@ -133,14 +130,14 @@ public class SelectSupplementaryResourcesDialog extends Dialog { @Override public Object[] getElements(Object inputElement) { - return getChildren(inputElement); + if (inputElement instanceof IResource[]) { + return (Object[]) inputElement; + } + return null; } @Override public Object[] getChildren(Object parentElement) { - if (parentElement instanceof IResource[]) { - return (Object[]) parentElement; - } return null; } }); @@ -159,6 +156,8 @@ public class SelectSupplementaryResourcesDialog extends Dialog { fTreeViewer.setInput(fAvailableResources); + setAllChecked(true); + fTreeViewer.addSelectionChangedListener(new ISelectionChangedListener() { @Override @@ -181,10 +180,7 @@ public class SelectSupplementaryResourcesDialog extends Dialog { selectAll.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { - Object[] items = fAvailableResources; - for (Object treeItem : items) { - fTreeViewer.setChecked(treeItem, true); - } + setAllChecked(true); updateOKButtonEnablement(); } @@ -195,10 +191,7 @@ public class SelectSupplementaryResourcesDialog extends Dialog { deselectAll.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { - Object[] items = fAvailableResources; - for (Object treeItem : items) { - fTreeViewer.setChecked(treeItem, false); - } + setAllChecked(false); updateOKButtonEnablement(); } @@ -209,6 +202,12 @@ public class SelectSupplementaryResourcesDialog extends Dialog { return composite; } + private void setAllChecked(boolean state) { + for (Object element : fAvailableResources) { + fTreeViewer.setChecked(element, state); + } + } + private void updateOKButtonEnablement() { Object[] checked = fTreeViewer.getCheckedElements(); getButton(IDialogConstants.OK_ID).setEnabled(checked.length > 0); diff --git a/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/internal/tmf/ui/project/handlers/DeleteExperimentSupplementaryFilesHandler.java b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/internal/tmf/ui/project/handlers/DeleteExperimentSupplementaryFilesHandler.java deleted file mode 100644 index ce69d2c3c7..0000000000 --- a/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/internal/tmf/ui/project/handlers/DeleteExperimentSupplementaryFilesHandler.java +++ /dev/null @@ -1,141 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2012, 2014 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: - * Bernd Hufmann - Initial API and implementation - * Patrick Tasse - Close editors to release resources - * Marc-Andre Laperle - Fix NPE (Bug 416574) - *******************************************************************************/ - -package org.eclipse.linuxtools.internal.tmf.ui.project.handlers; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; - -import org.eclipse.core.commands.AbstractHandler; -import org.eclipse.core.commands.ExecutionEvent; -import org.eclipse.core.commands.ExecutionException; -import org.eclipse.core.resources.IResource; -import org.eclipse.core.runtime.CoreException; -import org.eclipse.core.runtime.NullProgressMonitor; -import org.eclipse.jface.viewers.ISelection; -import org.eclipse.jface.viewers.ISelectionProvider; -import org.eclipse.jface.viewers.TreeSelection; -import org.eclipse.jface.window.Window; -import org.eclipse.linuxtools.internal.tmf.ui.Activator; -import org.eclipse.linuxtools.internal.tmf.ui.project.dialogs.SelectSupplementaryResourcesDialog; -import org.eclipse.linuxtools.tmf.ui.project.model.TmfExperimentElement; -import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement; -import org.eclipse.ui.IWorkbenchPage; -import org.eclipse.ui.IWorkbenchPart; -import org.eclipse.ui.IWorkbenchWindow; -import org.eclipse.ui.PlatformUI; - -/** - * Handler for Delete Supplementary Files command on experiment - */ -public class DeleteExperimentSupplementaryFilesHandler extends AbstractHandler { - - // ------------------------------------------------------------------------ - // Execution - // ------------------------------------------------------------------------ - - @Override - public Object execute(ExecutionEvent event) throws ExecutionException { - - // Check if we are closing down - IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); - if (window == null) { - return null; - } - - // Get the selection - IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); - IWorkbenchPart part = page.getActivePart(); - if (part == null) { - return Boolean.FALSE; - } - ISelectionProvider selectionProvider = part.getSite().getSelectionProvider(); - if (selectionProvider == null) { - return Boolean.FALSE; - } - - ISelection selection = selectionProvider.getSelection(); - - // Make sure there is only selection and that it is an experiment - if (selection instanceof TreeSelection) { - TreeSelection sel = (TreeSelection) selection; - // There should be only one item selected as per the plugin.xml - Object element = sel.getFirstElement(); - List resourcesList = new ArrayList<>(); - - if (element instanceof TmfExperimentElement) { - - TmfExperimentElement experiment = (TmfExperimentElement) element; - - IResource[] resources = experiment.getSupplementaryResources(); - // List to know which resources belong to the experiment - List experimentResources = Arrays.asList(resources); - - resourcesList.addAll(experimentResources); - - // Map to know which trace to close for each resource - HashMap traceMap = new HashMap<>(); - - for (TmfTraceElement aTrace : experiment.getTraces()) { - - // If trace is under an experiment, use the original trace from the traces folder - aTrace = aTrace.getElementUnderTraceFolder(); - - // Add the trace supplementary resources for the dialog - resources = aTrace.getSupplementaryResources(); - resourcesList.addAll(Arrays.asList(resources)); - - for (IResource resource : resources) { - traceMap.put(resource, aTrace); - } - } - - SelectSupplementaryResourcesDialog dialog = new SelectSupplementaryResourcesDialog(window.getShell(), resourcesList.toArray(new IResource[resourcesList.size()])); - - if (dialog.open() != Window.OK) { - return null; - } - - IResource[] resourcesToDelete = dialog.getResources(); - - // Delete the selected resources - for (IResource resource : resourcesToDelete) { - if (experimentResources.contains(resource)) { - experiment.closeEditors(); - } else { - traceMap.get(resource).closeEditors(); - } - - try { - resource.delete(true, new NullProgressMonitor()); - } catch (CoreException e) { - Activator.getDefault().logError("Error deleting supplementary resource " + resource, e); //$NON-NLS-1$ - } - } - - IResource resource = experiment.getProject().getResource(); - if (resource != null) { - try { - resource.refreshLocal(IResource.DEPTH_INFINITE, null); - } catch (CoreException e) { - Activator.getDefault().logError("Error refreshing resource " + resource, e); //$NON-NLS-1$ - } - } - } - } - return null; - } -} diff --git a/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/internal/tmf/ui/project/handlers/DeleteTraceSupplementaryFilesHandler.java b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/internal/tmf/ui/project/handlers/DeleteTraceSupplementaryFilesHandler.java index c54adaa8e4..5f526ea299 100644 --- a/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/internal/tmf/ui/project/handlers/DeleteTraceSupplementaryFilesHandler.java +++ b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/internal/tmf/ui/project/handlers/DeleteTraceSupplementaryFilesHandler.java @@ -13,28 +13,58 @@ package org.eclipse.linuxtools.internal.tmf.ui.project.handlers; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Comparator; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Set; + import org.eclipse.core.commands.AbstractHandler; import org.eclipse.core.commands.ExecutionEvent; import org.eclipse.core.commands.ExecutionException; +import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IResource; import org.eclipse.core.runtime.CoreException; import org.eclipse.jface.viewers.ISelection; -import org.eclipse.jface.viewers.ISelectionProvider; -import org.eclipse.jface.viewers.TreeSelection; +import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.window.Window; import org.eclipse.linuxtools.internal.tmf.ui.Activator; import org.eclipse.linuxtools.internal.tmf.ui.project.dialogs.SelectSupplementaryResourcesDialog; +import org.eclipse.linuxtools.tmf.ui.project.model.TmfCommonProjectElement; +import org.eclipse.linuxtools.tmf.ui.project.model.TmfExperimentElement; import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement; -import org.eclipse.ui.IWorkbenchPage; -import org.eclipse.ui.IWorkbenchPart; import org.eclipse.ui.IWorkbenchWindow; import org.eclipse.ui.PlatformUI; +import org.eclipse.ui.handlers.HandlerUtil; + +import com.google.common.collect.Multimap; +import com.google.common.collect.TreeMultimap; /** * Handler for Delete Supplementary Files command on trace */ public class DeleteTraceSupplementaryFilesHandler extends AbstractHandler { + // ------------------------------------------------------------------------ + // Inner classes + // ------------------------------------------------------------------------ + + private class ElementComparator implements Comparator { + @Override + public int compare(TmfCommonProjectElement e1, TmfCommonProjectElement e2) { + return e1.getPath().toString().compareTo(e2.getPath().toString()); + } + } + + private class ResourceComparator implements Comparator { + @Override + public int compare(IResource r1, IResource r2) { + return r1.getFullPath().toString().compareTo(r2.getFullPath().toString()); + } + } + // ------------------------------------------------------------------------ // Execution // ------------------------------------------------------------------------ @@ -49,49 +79,68 @@ public class DeleteTraceSupplementaryFilesHandler extends AbstractHandler { } // Get the selection - IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); - IWorkbenchPart part = page.getActivePart(); - if (part == null) { - return Boolean.FALSE; - } - ISelectionProvider selectionProvider = part.getSite().getSelectionProvider(); - if (selectionProvider == null) { - return Boolean.FALSE; + ISelection selection = HandlerUtil.getCurrentSelection(event); + if (!(selection instanceof IStructuredSelection)) { + return null; } + final Multimap resourceMap = + TreeMultimap.create(new ElementComparator(), new ResourceComparator()); + final Iterator iterator = ((IStructuredSelection) selection).iterator(); - ISelection selection = selectionProvider.getSelection(); - - // Make sure there is only selection and that it is an experiment - if (selection instanceof TreeSelection) { - TreeSelection sel = (TreeSelection) selection; - // There should be only one item selected as per the plugin.xml - Object element = sel.getFirstElement(); + while (iterator.hasNext()) { + Object element = iterator.next(); if (element instanceof TmfTraceElement) { TmfTraceElement trace = (TmfTraceElement) element; // If trace is under an experiment, use the original trace from the traces folder trace = trace.getElementUnderTraceFolder(); + for (IResource resource : trace.getSupplementaryResources()) { + resourceMap.put(trace, resource); + } - // Delete the selected resources - IResource[] resources = trace.getSupplementaryResources(); - - SelectSupplementaryResourcesDialog dialog = new SelectSupplementaryResourcesDialog(window.getShell(), resources); - if (dialog.open() != Window.OK) { - return null; + } else if (element instanceof TmfExperimentElement) { + TmfExperimentElement experiment = (TmfExperimentElement) element; + for (IResource resource : experiment.getSupplementaryResources()) { + resourceMap.put(experiment, resource); + } + for (TmfTraceElement trace : experiment.getTraces()) { + // If trace is under an experiment, use the original trace from the traces folder + trace = trace.getElementUnderTraceFolder(); + for (IResource resource : trace.getSupplementaryResources()) { + resourceMap.put(trace, resource); + } } + } + } + + SelectSupplementaryResourcesDialog dialog = new SelectSupplementaryResourcesDialog(window.getShell(), + resourceMap.values().toArray(new IResource[0])); + if (dialog.open() != Window.OK) { + return null; + } - trace.closeEditors(); - trace.deleteSupplementaryResources(dialog.getResources()); + Set projectsToRefresh = new HashSet<>(); - // Refresh project - IResource resource = trace.getProject().getResource(); + // Delete the resources that were selected + List allResourcesToDelete = Arrays.asList(dialog.getResources()); + for (TmfCommonProjectElement element : resourceMap.keySet()) { + List traceResourcesToDelete = new ArrayList<>(resourceMap.get(element)); + traceResourcesToDelete.retainAll(allResourcesToDelete); + if (!traceResourcesToDelete.isEmpty()) { + // Delete the selected resources + element.closeEditors(); + element.deleteSupplementaryResources(traceResourcesToDelete.toArray(new IResource[0])); + projectsToRefresh.add(element.getProject().getResource()); + } + } - if (resource != null) { - try { - resource.refreshLocal(IResource.DEPTH_INFINITE, null); - } catch (CoreException e) { - Activator.getDefault().logError("Error refreshing resource " + resource, e); //$NON-NLS-1$ - } - } + // Refresh projects + Iterator projectIterator = projectsToRefresh.iterator(); + while (projectIterator.hasNext()) { + IProject project = projectIterator.next(); + try { + project.refreshLocal(IResource.DEPTH_INFINITE, null); + } catch (CoreException e) { + Activator.getDefault().logError("Error refreshing project " + project, e); //$NON-NLS-1$ } } -- 2.34.1