| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2016 Ericsson |
| 3 | * |
| 4 | * All rights reserved. This program and the accompanying materials are |
| 5 | * made available under the terms of the Eclipse Public License v1.0 which |
| 6 | * accompanies this distribution, and is available at |
| 7 | * http://www.eclipse.org/legal/epl-v10.html |
| 8 | *******************************************************************************/ |
| 9 | package org.eclipse.tracecompass.internal.tmf.ui.project.operations; |
| 10 | |
| 11 | import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull; |
| 12 | |
| 13 | import java.util.ArrayList; |
| 14 | import java.util.Arrays; |
| 15 | import java.util.List; |
| 16 | import java.util.Map; |
| 17 | import java.util.Set; |
| 18 | |
| 19 | import org.eclipse.core.runtime.CoreException; |
| 20 | import org.eclipse.core.runtime.IProgressMonitor; |
| 21 | import org.eclipse.core.runtime.IStatus; |
| 22 | import org.eclipse.core.runtime.Status; |
| 23 | import org.eclipse.core.runtime.SubMonitor; |
| 24 | import org.eclipse.jdt.annotation.NonNull; |
| 25 | import org.eclipse.jdt.annotation.Nullable; |
| 26 | import org.eclipse.jface.operation.IRunnableWithProgress; |
| 27 | import org.eclipse.jface.operation.ModalContext; |
| 28 | import org.eclipse.swt.widgets.Display; |
| 29 | import org.eclipse.tracecompass.internal.tmf.ui.Activator; |
| 30 | import org.eclipse.tracecompass.tmf.ui.project.model.TmfExperimentElement; |
| 31 | import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceElement; |
| 32 | |
| 33 | /** |
| 34 | * Operation to add traces to an experiment. |
| 35 | * |
| 36 | * @author Bernd Hufmann |
| 37 | */ |
| 38 | public class SelectTracesOperation implements IRunnableWithProgress { |
| 39 | |
| 40 | private final @Nullable TmfExperimentElement fExperimentElement; |
| 41 | private final @NonNull List<TmfTraceElement> fTraceElements; |
| 42 | private final @NonNull Map<String, TmfTraceElement> fPreviousTraces; |
| 43 | private @NonNull IStatus fStatus = checkNotNull(Status.OK_STATUS); |
| 44 | |
| 45 | /** |
| 46 | * Constructor. It will add traces to given experiment and remove traces |
| 47 | * that don't exist anymore. |
| 48 | * |
| 49 | * @param experimentElement |
| 50 | * the experiment element to add the traces |
| 51 | * @param traces |
| 52 | * the trace elements |
| 53 | * @param previousTraces |
| 54 | * map of traces currently available in the experiment |
| 55 | */ |
| 56 | public SelectTracesOperation(@NonNull TmfExperimentElement experimentElement, @NonNull TmfTraceElement[] traces, @NonNull Map<String, TmfTraceElement> previousTraces) { |
| 57 | fExperimentElement = experimentElement; |
| 58 | fTraceElements = new ArrayList<>(); |
| 59 | fTraceElements.addAll(Arrays.asList(traces)); |
| 60 | fPreviousTraces = previousTraces; |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public void run(IProgressMonitor progressMonitor) { |
| 65 | TmfExperimentElement experimentElement = fExperimentElement; |
| 66 | if (experimentElement == null) { |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | // Check if operation was cancelled. |
| 71 | boolean changed = false; |
| 72 | |
| 73 | |
| 74 | List<TmfTraceElement> elements = fTraceElements; |
| 75 | |
| 76 | Set<String> keys = fPreviousTraces.keySet(); |
| 77 | SubMonitor subMonitor = SubMonitor.convert(progressMonitor, elements.size() + keys.size()); |
| 78 | // Add the selected traces to the experiment |
| 79 | try { |
| 80 | for (TmfTraceElement trace : elements) { |
| 81 | ModalContext.checkCanceled(progressMonitor); |
| 82 | String name = trace.getElementPath(); |
| 83 | if (keys.contains(name)) { |
| 84 | subMonitor.setTaskName(Messages.SelectTracesWizardPage_TraceRemovalTask + " " + trace.getElementPath()); //$NON-NLS-1$ |
| 85 | keys.remove(name); |
| 86 | } else { |
| 87 | subMonitor.setTaskName(Messages.SelectTracesWizardPage_TraceSelectionTask + " " + trace.getElementPath()); //$NON-NLS-1$ |
| 88 | experimentElement.addTrace(trace, false); |
| 89 | changed = true; |
| 90 | } |
| 91 | subMonitor.worked(1); |
| 92 | } |
| 93 | |
| 94 | // Remove traces that were unchecked (thus left in fPreviousTraces) |
| 95 | for (Map.Entry<String, TmfTraceElement> entry : fPreviousTraces.entrySet()) { |
| 96 | ModalContext.checkCanceled(progressMonitor); |
| 97 | TmfTraceElement trace = entry.getValue(); |
| 98 | subMonitor.setTaskName(Messages.SelectTracesWizardPage_TraceRemovalTask + " " + trace.getElementPath()); //$NON-NLS-1$ |
| 99 | |
| 100 | try { |
| 101 | experimentElement.removeTrace(trace); |
| 102 | } catch (CoreException e) { |
| 103 | Activator.getDefault().logError(Messages.SelectTracesWizardPage_SelectionError + " " + experimentElement.getName(), e); //$NON-NLS-1$ |
| 104 | } |
| 105 | changed = true; |
| 106 | subMonitor.worked(1); |
| 107 | } |
| 108 | if (changed) { |
| 109 | Display.getDefault().syncExec(new Runnable() { |
| 110 | @Override |
| 111 | public void run() { |
| 112 | experimentElement.closeEditors(); |
| 113 | } |
| 114 | }); |
| 115 | experimentElement.deleteSupplementaryResources(); |
| 116 | } |
| 117 | setStatus(checkNotNull(Status.OK_STATUS)); |
| 118 | } catch (InterruptedException e) { |
| 119 | setStatus(checkNotNull(Status.CANCEL_STATUS)); |
| 120 | } catch (Exception e) { |
| 121 | Activator.getDefault().logError(Messages.SelectTracesWizardPage_SelectionError, e); |
| 122 | setStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.SelectTracesWizardPage_SelectionError, e)); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Set the status for this operation |
| 128 | * |
| 129 | * @param status |
| 130 | * the status |
| 131 | */ |
| 132 | protected void setStatus(@NonNull IStatus status) { |
| 133 | fStatus = status; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Returns the status of the operation execution. |
| 138 | * |
| 139 | * @return status |
| 140 | */ |
| 141 | public @NonNull IStatus getStatus() { |
| 142 | return fStatus; |
| 143 | } |
| 144 | |
| 145 | } |