Commit | Line | Data |
---|---|---|
b2845b8f BH |
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; | |
45fc1883 | 15 | import java.util.HashMap; |
b2845b8f BH |
16 | import java.util.List; |
17 | import java.util.Map; | |
18 | import java.util.Set; | |
19 | ||
45fc1883 BH |
20 | import org.eclipse.core.resources.IFolder; |
21 | import org.eclipse.core.resources.IResource; | |
b2845b8f BH |
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; | |
45fc1883 | 34 | import org.eclipse.tracecompass.tmf.ui.project.model.TmfExperimentFolder; |
b2845b8f | 35 | import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceElement; |
45fc1883 | 36 | import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceFolder; |
b2845b8f BH |
37 | |
38 | /** | |
39 | * Operation to add traces to an experiment. | |
40 | * | |
41 | * @author Bernd Hufmann | |
42 | */ | |
43 | public class SelectTracesOperation implements IRunnableWithProgress { | |
44 | ||
45 | private final @Nullable TmfExperimentElement fExperimentElement; | |
45fc1883 BH |
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; | |
b2845b8f BH |
50 | private @NonNull IStatus fStatus = checkNotNull(Status.OK_STATUS); |
51 | ||
45fc1883 BH |
52 | /** |
53 | * Constructor | |
54 | * | |
55 | * @param experimentFolderElement | |
56 | * workspace experiment folder containing the experiment | |
57 | * @param experiment | |
58 | * experiment folder where to add traces | |
59 | * @param parentTraceFolder | |
60 | * the parent trace folder containing the trace resources | |
61 | * @param resources | |
62 | * the trace resources to add to the experiment | |
63 | */ | |
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); | |
66 | } | |
67 | ||
b2845b8f BH |
68 | /** |
69 | * Constructor. It will add traces to given experiment and remove traces | |
70 | * that don't exist anymore. | |
71 | * | |
72 | * @param experimentElement | |
73 | * the experiment element to add the traces | |
74 | * @param traces | |
75 | * the trace elements | |
76 | * @param previousTraces | |
77 | * map of traces currently available in the experiment | |
78 | */ | |
79 | public SelectTracesOperation(@NonNull TmfExperimentElement experimentElement, @NonNull TmfTraceElement[] traces, @NonNull Map<String, TmfTraceElement> previousTraces) { | |
45fc1883 BH |
80 | this(experimentElement, null, traces, null, previousTraces); |
81 | } | |
82 | ||
83 | // Full constructor for internal use only | |
84 | private SelectTracesOperation(TmfExperimentElement experimentElement, TmfTraceFolder parentTraceFolder, TmfTraceElement[] traces, List<IResource> resources, Map<String, TmfTraceElement> previousTraces) { | |
b2845b8f | 85 | fExperimentElement = experimentElement; |
45fc1883 BH |
86 | fParentTraceFolder = parentTraceFolder; |
87 | if (traces == null) { | |
88 | fTraceElements = null; | |
89 | } else { | |
90 | fTraceElements = new ArrayList<>(); | |
91 | fTraceElements.addAll(Arrays.asList(traces)); | |
92 | } | |
93 | fResources = resources; | |
b2845b8f BH |
94 | fPreviousTraces = previousTraces; |
95 | } | |
96 | ||
97 | @Override | |
98 | public void run(IProgressMonitor progressMonitor) { | |
99 | TmfExperimentElement experimentElement = fExperimentElement; | |
100 | if (experimentElement == null) { | |
101 | return; | |
102 | } | |
103 | ||
104 | // Check if operation was cancelled. | |
105 | boolean changed = false; | |
106 | ||
45fc1883 BH |
107 | Map<String, TmfTraceElement> previousTraces = new HashMap<>(); |
108 | if (fPreviousTraces != null) { | |
109 | previousTraces = fPreviousTraces; | |
110 | } | |
b2845b8f BH |
111 | |
112 | List<TmfTraceElement> elements = fTraceElements; | |
45fc1883 BH |
113 | if (elements == null) { |
114 | if ((fParentTraceFolder != null) && (fResources != null)) { | |
115 | elements = fParentTraceFolder.getTraceElements(fResources); | |
116 | } else { | |
117 | return; | |
118 | } | |
119 | } | |
b2845b8f | 120 | |
45fc1883 | 121 | Set<String> keys = previousTraces.keySet(); |
b2845b8f BH |
122 | SubMonitor subMonitor = SubMonitor.convert(progressMonitor, elements.size() + keys.size()); |
123 | // Add the selected traces to the experiment | |
124 | try { | |
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$ | |
130 | keys.remove(name); | |
131 | } else { | |
132 | subMonitor.setTaskName(Messages.SelectTracesWizardPage_TraceSelectionTask + " " + trace.getElementPath()); //$NON-NLS-1$ | |
133 | experimentElement.addTrace(trace, false); | |
134 | changed = true; | |
135 | } | |
136 | subMonitor.worked(1); | |
137 | } | |
138 | ||
139 | // Remove traces that were unchecked (thus left in fPreviousTraces) | |
45fc1883 | 140 | for (Map.Entry<String, TmfTraceElement> entry : previousTraces.entrySet()) { |
b2845b8f BH |
141 | ModalContext.checkCanceled(progressMonitor); |
142 | TmfTraceElement trace = entry.getValue(); | |
143 | subMonitor.setTaskName(Messages.SelectTracesWizardPage_TraceRemovalTask + " " + trace.getElementPath()); //$NON-NLS-1$ | |
144 | ||
145 | try { | |
146 | experimentElement.removeTrace(trace); | |
147 | } catch (CoreException e) { | |
148 | Activator.getDefault().logError(Messages.SelectTracesWizardPage_SelectionError + " " + experimentElement.getName(), e); //$NON-NLS-1$ | |
149 | } | |
150 | changed = true; | |
151 | subMonitor.worked(1); | |
152 | } | |
153 | if (changed) { | |
154 | Display.getDefault().syncExec(new Runnable() { | |
155 | @Override | |
156 | public void run() { | |
157 | experimentElement.closeEditors(); | |
158 | } | |
159 | }); | |
160 | experimentElement.deleteSupplementaryResources(); | |
161 | } | |
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)); | |
168 | } | |
169 | } | |
170 | ||
171 | /** | |
172 | * Set the status for this operation | |
173 | * | |
174 | * @param status | |
175 | * the status | |
176 | */ | |
177 | protected void setStatus(@NonNull IStatus status) { | |
178 | fStatus = status; | |
179 | } | |
180 | ||
181 | /** | |
182 | * Returns the status of the operation execution. | |
183 | * | |
184 | * @return status | |
185 | */ | |
186 | public @NonNull IStatus getStatus() { | |
187 | return fStatus; | |
188 | } | |
189 | ||
190 | } |