Commit | Line | Data |
---|---|---|
12c155f5 | 1 | /******************************************************************************* |
8a917424 | 2 | * Copyright (c) 2011, 2014 Ericsson, École Polytechnique de Montréal |
6256d8ad | 3 | * |
12c155f5 FC |
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 | |
6256d8ad | 8 | * |
12c155f5 FC |
9 | * Contributors: |
10 | * Francois Chouinard - Initial API and implementation | |
ab37ff41 | 11 | * Patrick Tasse - Fix propagation to experiment traces |
8a917424 | 12 | * Geneviève Bastien - Add support of experiment types |
12c155f5 FC |
13 | *******************************************************************************/ |
14 | ||
d34665f9 | 15 | package org.eclipse.linuxtools.internal.tmf.ui.project.handlers; |
12c155f5 | 16 | |
a94410d9 | 17 | import java.util.ArrayList; |
f537c959 | 18 | import java.util.HashSet; |
12c155f5 | 19 | import java.util.Iterator; |
a94410d9 | 20 | import java.util.List; |
f537c959 | 21 | import java.util.Set; |
12c155f5 FC |
22 | |
23 | import org.eclipse.core.commands.AbstractHandler; | |
24 | import org.eclipse.core.commands.ExecutionEvent; | |
25 | import org.eclipse.core.commands.ExecutionException; | |
26 | import org.eclipse.core.resources.IProject; | |
27 | import org.eclipse.core.resources.IResource; | |
28 | import org.eclipse.core.runtime.CoreException; | |
a94410d9 MK |
29 | import org.eclipse.core.runtime.IStatus; |
30 | import org.eclipse.core.runtime.MultiStatus; | |
31 | import org.eclipse.core.runtime.Status; | |
32 | import org.eclipse.jface.dialogs.ErrorDialog; | |
12c155f5 | 33 | import org.eclipse.jface.viewers.ISelection; |
1595249b | 34 | import org.eclipse.jface.viewers.ISelectionProvider; |
12c155f5 | 35 | import org.eclipse.jface.viewers.TreeSelection; |
8fd82db5 | 36 | import org.eclipse.linuxtools.internal.tmf.ui.Activator; |
e12ecd30 | 37 | import org.eclipse.linuxtools.tmf.core.TmfCommonConstants; |
a6e37e4c PT |
38 | import org.eclipse.linuxtools.tmf.core.project.model.TmfTraceType; |
39 | import org.eclipse.linuxtools.tmf.core.project.model.TraceTypeHelper; | |
6c13869b | 40 | import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace; |
8a917424 | 41 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfCommonProjectElement; |
a6e37e4c | 42 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfExperimentElement; |
12c155f5 | 43 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfExperimentFolder; |
f537c959 | 44 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfProjectElement; |
12c155f5 | 45 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement; |
a6e37e4c | 46 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceTypeUIUtils; |
a94410d9 | 47 | import org.eclipse.swt.widgets.Shell; |
12c155f5 FC |
48 | import org.eclipse.ui.IWorkbenchPage; |
49 | import org.eclipse.ui.IWorkbenchPart; | |
50 | import org.eclipse.ui.IWorkbenchWindow; | |
51 | import org.eclipse.ui.PlatformUI; | |
52 | ||
53 | /** | |
54 | * <b><u>SetTraceTypeHandler</u></b> | |
55 | * <p> | |
56 | */ | |
57 | public class SelectTraceTypeHandler extends AbstractHandler { | |
58 | ||
59 | // ------------------------------------------------------------------------ | |
60 | // Constants | |
61 | // ------------------------------------------------------------------------ | |
62 | ||
37d150dd | 63 | private static final String TYPE_PARAMETER = "org.eclipse.linuxtools.tmf.ui.commandparameter.select_trace_type.type"; //$NON-NLS-1$ |
12c155f5 FC |
64 | |
65 | // ------------------------------------------------------------------------ | |
66 | // Attributes | |
67 | // ------------------------------------------------------------------------ | |
68 | ||
69 | private TreeSelection fSelection = null; | |
70 | ||
71 | // ------------------------------------------------------------------------ | |
72 | // Validation | |
73 | // ------------------------------------------------------------------------ | |
74 | ||
75 | @Override | |
76 | public boolean isEnabled() { | |
77 | ||
78 | // Check if we are closing down | |
79 | IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); | |
6256d8ad | 80 | if (window == null) { |
12c155f5 | 81 | return false; |
6256d8ad | 82 | } |
12c155f5 FC |
83 | |
84 | // Get the selection | |
85 | IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); | |
86 | IWorkbenchPart part = page.getActivePart(); | |
d5210347 PT |
87 | if (part == null) { |
88 | return false; | |
89 | } | |
1595249b | 90 | ISelectionProvider selectionProvider = part.getSite().getSelectionProvider(); |
6256d8ad | 91 | if (selectionProvider == null) { |
1595249b | 92 | return false; |
6256d8ad | 93 | } |
1595249b | 94 | ISelection selection = selectionProvider.getSelection(); |
12c155f5 FC |
95 | |
96 | // Make sure selection contains only traces | |
97 | fSelection = null; | |
98 | if (selection instanceof TreeSelection) { | |
99 | fSelection = (TreeSelection) selection; | |
12c155f5 FC |
100 | Iterator<Object> iterator = fSelection.iterator(); |
101 | while (iterator.hasNext()) { | |
102 | Object element = iterator.next(); | |
8a917424 | 103 | if (!(element instanceof TmfCommonProjectElement)) { |
12c155f5 FC |
104 | return false; |
105 | } | |
106 | } | |
107 | } | |
108 | ||
109 | // If we get here, either nothing is selected or everything is a trace | |
110 | return !selection.isEmpty(); | |
111 | } | |
112 | ||
113 | // ------------------------------------------------------------------------ | |
114 | // Execution | |
115 | // ------------------------------------------------------------------------ | |
116 | ||
117 | @Override | |
118 | public Object execute(ExecutionEvent event) throws ExecutionException { | |
119 | ||
120 | // Check if we are closing down | |
121 | IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); | |
6256d8ad | 122 | if (window == null) { |
12c155f5 | 123 | return null; |
6256d8ad | 124 | } |
507b1336 | 125 | List<IStatus> statuses = new ArrayList<>(); |
f537c959 | 126 | Set<TmfProjectElement> projects = new HashSet<>(); |
12c155f5 FC |
127 | boolean ok = true; |
128 | for (Object element : fSelection.toList()) { | |
8a917424 GB |
129 | TmfCommonProjectElement trace = (TmfCommonProjectElement) element; |
130 | if (trace instanceof TmfTraceElement) { | |
131 | trace = ((TmfTraceElement) trace).getElementUnderTraceFolder(); | |
132 | } | |
12c155f5 FC |
133 | IResource resource = trace.getResource(); |
134 | if (resource != null) { | |
135 | try { | |
a6e37e4c | 136 | // Set the trace type for this resource |
12c155f5 | 137 | String traceType = event.getParameter(TYPE_PARAMETER); |
4726e963 | 138 | String previousTraceType = trace.getTraceType(); |
2d64a788 | 139 | IStatus status = propagateProperties(trace, traceType); |
a94410d9 | 140 | ok &= status.isOK(); |
4726e963 BH |
141 | |
142 | if (status.isOK()) { | |
143 | if ((previousTraceType != null) && (!traceType.equals(previousTraceType))) { | |
a72a6830 PT |
144 | // Close the trace if open |
145 | trace.closeEditors(); | |
4726e963 BH |
146 | // Delete all supplementary resources |
147 | trace.deleteSupplementaryResources(); | |
148 | } | |
149 | } else { | |
a94410d9 MK |
150 | statuses.add(status); |
151 | } | |
f537c959 | 152 | projects.add(trace.getProject()); |
12c155f5 | 153 | } catch (CoreException e) { |
a94410d9 | 154 | Activator.getDefault().logError(Messages.SelectTraceTypeHandler_ErrorSelectingTrace + trace.getName(), e); |
12c155f5 FC |
155 | } |
156 | } | |
f537c959 PT |
157 | trace.getProject(); |
158 | } | |
159 | for (TmfProjectElement project : projects) { | |
160 | project.refresh(); | |
12c155f5 | 161 | } |
12c155f5 FC |
162 | |
163 | if (!ok) { | |
a94410d9 MK |
164 | final Shell shell = window.getShell(); |
165 | MultiStatus info = new MultiStatus(Activator.PLUGIN_ID, 1, Messages.SelectTraceTypeHandler_TraceFailedValidation, null); | |
166 | if (statuses.size() > 1) | |
167 | { | |
168 | info = new MultiStatus(Activator.PLUGIN_ID, 1, Messages.SelectTraceTypeHandler_TracesFailedValidation, null); | |
169 | } | |
170 | for (IStatus status : statuses) { | |
171 | info.add(status); | |
172 | } | |
173 | ErrorDialog.openError(shell, Messages.SelectTraceTypeHandler_Title, Messages.SelectTraceTypeHandler_InvalidTraceType, info); | |
12c155f5 | 174 | } |
12c155f5 FC |
175 | return null; |
176 | } | |
177 | ||
8a917424 | 178 | private static IStatus propagateProperties(TmfCommonProjectElement element, String traceType) |
abbdd66a | 179 | throws CoreException { |
12c155f5 | 180 | |
8a917424 | 181 | IResource resource = element.getResource(); |
a6e37e4c PT |
182 | String svTraceType = resource.getPersistentProperty(TmfCommonConstants.TRACETYPE); |
183 | TraceTypeHelper svTraceTypeHelper = TmfTraceType.getInstance().getTraceType(svTraceType); | |
12c155f5 | 184 | |
a6e37e4c PT |
185 | TraceTypeHelper traceTypeHelper = TmfTraceType.getInstance().getTraceType(traceType); |
186 | TmfTraceTypeUIUtils.setTraceType(resource, traceTypeHelper); | |
8a917424 | 187 | final IStatus validateTraceType = validateTraceType(element); |
a94410d9 | 188 | if (!validateTraceType.isOK()) { |
a6e37e4c | 189 | TmfTraceTypeUIUtils.setTraceType(resource, svTraceTypeHelper); |
a94410d9 | 190 | return validateTraceType; |
12c155f5 FC |
191 | } |
192 | ||
8a917424 | 193 | TmfExperimentFolder experimentFolder = element.getProject().getExperimentsFolder(); |
a6e37e4c PT |
194 | for (final TmfExperimentElement experiment : experimentFolder.getExperiments()) { |
195 | for (final TmfTraceElement child : experiment.getTraces()) { | |
8a917424 | 196 | if (child.getName().equals(element.getName())) { |
a6e37e4c PT |
197 | TmfTraceTypeUIUtils.setTraceType(child.getResource(), traceTypeHelper); |
198 | break; | |
12c155f5 FC |
199 | } |
200 | } | |
201 | } | |
c50b1d3b | 202 | |
a94410d9 | 203 | return Status.OK_STATUS; |
12c155f5 FC |
204 | } |
205 | ||
8a917424 | 206 | private static IStatus validateTraceType(TmfCommonProjectElement trace) { |
12c155f5 | 207 | IProject project = trace.getProject().getResource(); |
6256d8ad | 208 | ITmfTrace tmfTrace = null; |
a94410d9 | 209 | IStatus validate = null; |
12c155f5 FC |
210 | try { |
211 | tmfTrace = trace.instantiateTrace(); | |
a94410d9 MK |
212 | if (tmfTrace != null) { |
213 | validate = tmfTrace.validate(project, trace.getLocation().getPath()); | |
a6e37e4c | 214 | } else { |
a94410d9 MK |
215 | validate = new Status(IStatus.ERROR, trace.getName(), "File does not exist : " + trace.getLocation().getPath()); //$NON-NLS-1$ |
216 | } | |
12c155f5 | 217 | } finally { |
6256d8ad | 218 | if (tmfTrace != null) { |
12c155f5 | 219 | tmfTrace.dispose(); |
6256d8ad | 220 | } |
12c155f5 | 221 | } |
a94410d9 MK |
222 | if (validate == null) { |
223 | validate = new Status(IStatus.ERROR, "unknown", "unknown"); //$NON-NLS-1$ //$NON-NLS-2$ | |
224 | } | |
225 | return validate; | |
12c155f5 FC |
226 | } |
227 | ||
228 | } |