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
.HashMap
;
16 import java
.util
.List
;
20 import org
.eclipse
.core
.resources
.IFolder
;
21 import org
.eclipse
.core
.resources
.IResource
;
22 import org
.eclipse
.core
.runtime
.CoreException
;
23 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
24 import org
.eclipse
.core
.runtime
.IStatus
;
25 import org
.eclipse
.core
.runtime
.Status
;
26 import org
.eclipse
.core
.runtime
.SubMonitor
;
27 import org
.eclipse
.jdt
.annotation
.NonNull
;
28 import org
.eclipse
.jdt
.annotation
.Nullable
;
29 import org
.eclipse
.jface
.operation
.IRunnableWithProgress
;
30 import org
.eclipse
.jface
.operation
.ModalContext
;
31 import org
.eclipse
.swt
.widgets
.Display
;
32 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.Activator
;
33 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfExperimentElement
;
34 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfExperimentFolder
;
35 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfTraceElement
;
36 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfTraceFolder
;
39 * Operation to add traces to an experiment.
41 * @author Bernd Hufmann
43 public class SelectTracesOperation
implements IRunnableWithProgress
{
45 private final @Nullable TmfExperimentElement fExperimentElement
;
46 private final @Nullable TmfTraceFolder fParentTraceFolder
;
47 private final @Nullable List
<TmfTraceElement
> fTraceElements
;
48 private final @Nullable List
<IResource
> fResources
;
49 private final @Nullable Map
<String
, TmfTraceElement
> fPreviousTraces
;
50 private @NonNull IStatus fStatus
= checkNotNull(Status
.OK_STATUS
);
55 * @param experimentFolderElement
56 * workspace experiment folder containing the experiment
58 * experiment folder where to add traces
59 * @param parentTraceFolder
60 * the parent trace folder containing the trace resources
62 * the trace resources to add to the experiment
64 public SelectTracesOperation(@NonNull TmfExperimentFolder experimentFolderElement
, @NonNull IFolder experiment
, @NonNull TmfTraceFolder parentTraceFolder
, @NonNull List
<IResource
> resources
) {
65 this(experimentFolderElement
.getExperiment(experiment
), parentTraceFolder
, null, resources
, null);
69 * Constructor. It will add traces to given experiment and remove traces
70 * that don't exist anymore.
72 * @param experimentElement
73 * the experiment element to add the traces
76 * @param previousTraces
77 * map of traces currently available in the experiment
79 public SelectTracesOperation(@NonNull TmfExperimentElement experimentElement
, @NonNull TmfTraceElement
[] traces
, @NonNull Map
<String
, TmfTraceElement
> previousTraces
) {
80 this(experimentElement
, null, traces
, null, previousTraces
);
83 // Full constructor for internal use only
84 private SelectTracesOperation(TmfExperimentElement experimentElement
, TmfTraceFolder parentTraceFolder
, TmfTraceElement
[] traces
, List
<IResource
> resources
, Map
<String
, TmfTraceElement
> previousTraces
) {
85 fExperimentElement
= experimentElement
;
86 fParentTraceFolder
= parentTraceFolder
;
88 fTraceElements
= null;
90 fTraceElements
= new ArrayList
<>();
91 fTraceElements
.addAll(Arrays
.asList(traces
));
93 fResources
= resources
;
94 fPreviousTraces
= previousTraces
;
98 public void run(IProgressMonitor progressMonitor
) {
99 TmfExperimentElement experimentElement
= fExperimentElement
;
100 if (experimentElement
== null) {
104 // Check if operation was cancelled.
105 boolean changed
= false;
107 Map
<String
, TmfTraceElement
> previousTraces
= new HashMap
<>();
108 if (fPreviousTraces
!= null) {
109 previousTraces
= fPreviousTraces
;
112 List
<TmfTraceElement
> elements
= fTraceElements
;
113 if (elements
== null) {
114 if ((fParentTraceFolder
!= null) && (fResources
!= null)) {
115 elements
= fParentTraceFolder
.getTraceElements(fResources
);
121 Set
<String
> keys
= previousTraces
.keySet();
122 SubMonitor subMonitor
= SubMonitor
.convert(progressMonitor
, elements
.size() + keys
.size());
123 // Add the selected traces to the experiment
125 for (TmfTraceElement trace
: elements
) {
126 ModalContext
.checkCanceled(progressMonitor
);
127 String name
= trace
.getElementPath();
128 if (keys
.contains(name
)) {
129 subMonitor
.setTaskName(Messages
.SelectTracesWizardPage_TraceRemovalTask
+ " " + trace
.getElementPath()); //$NON-NLS-1$
132 subMonitor
.setTaskName(Messages
.SelectTracesWizardPage_TraceSelectionTask
+ " " + trace
.getElementPath()); //$NON-NLS-1$
133 experimentElement
.addTrace(trace
, false);
136 subMonitor
.worked(1);
139 // Remove traces that were unchecked (thus left in fPreviousTraces)
140 for (Map
.Entry
<String
, TmfTraceElement
> entry
: previousTraces
.entrySet()) {
141 ModalContext
.checkCanceled(progressMonitor
);
142 TmfTraceElement trace
= entry
.getValue();
143 subMonitor
.setTaskName(Messages
.SelectTracesWizardPage_TraceRemovalTask
+ " " + trace
.getElementPath()); //$NON-NLS-1$
146 experimentElement
.removeTrace(trace
);
147 } catch (CoreException e
) {
148 Activator
.getDefault().logError(Messages
.SelectTracesWizardPage_SelectionError
+ " " + experimentElement
.getName(), e
); //$NON-NLS-1$
151 subMonitor
.worked(1);
154 Display
.getDefault().syncExec(new Runnable() {
157 experimentElement
.closeEditors();
160 experimentElement
.deleteSupplementaryResources();
162 setStatus(checkNotNull(Status
.OK_STATUS
));
163 } catch (InterruptedException e
) {
164 setStatus(checkNotNull(Status
.CANCEL_STATUS
));
165 } catch (Exception e
) {
166 Activator
.getDefault().logError(Messages
.SelectTracesWizardPage_SelectionError
, e
);
167 setStatus(new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.SelectTracesWizardPage_SelectionError
, e
));
172 * Set the status for this operation
177 protected void setStatus(@NonNull IStatus status
) {
182 * Returns the status of the operation execution.
186 public @NonNull IStatus
getStatus() {