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