1 /*******************************************************************************
2 * Copyright (c) 2016 Ericsson
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
;
11 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
13 import java
.util
.ArrayList
;
14 import java
.util
.Arrays
;
15 import java
.util
.List
;
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
;
34 * Operation to add traces to an experiment.
36 * @author Bernd Hufmann
38 public class SelectTracesOperation
implements IRunnableWithProgress
{
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
);
46 * Constructor. It will add traces to given experiment and remove traces
47 * that don't exist anymore.
49 * @param experimentElement
50 * the experiment element to add the traces
53 * @param previousTraces
54 * map of traces currently available in the experiment
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
;
64 public void run(IProgressMonitor progressMonitor
) {
65 TmfExperimentElement experimentElement
= fExperimentElement
;
66 if (experimentElement
== null) {
70 // Check if operation was cancelled.
71 boolean changed
= false;
74 List
<TmfTraceElement
> elements
= fTraceElements
;
76 Set
<String
> keys
= fPreviousTraces
.keySet();
77 SubMonitor subMonitor
= SubMonitor
.convert(progressMonitor
, elements
.size() + keys
.size());
78 // Add the selected traces to the experiment
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$
87 subMonitor
.setTaskName(Messages
.SelectTracesWizardPage_TraceSelectionTask
+ " " + trace
.getElementPath()); //$NON-NLS-1$
88 experimentElement
.addTrace(trace
, false);
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$
101 experimentElement
.removeTrace(trace
);
102 } catch (CoreException e
) {
103 Activator
.getDefault().logError(Messages
.SelectTracesWizardPage_SelectionError
+ " " + experimentElement
.getName(), e
); //$NON-NLS-1$
106 subMonitor
.worked(1);
109 Display
.getDefault().syncExec(new Runnable() {
112 experimentElement
.closeEditors();
115 experimentElement
.deleteSupplementaryResources();
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
));
127 * Set the status for this operation
132 protected void setStatus(@NonNull IStatus status
) {
137 * Returns the status of the operation execution.
141 public @NonNull IStatus
getStatus() {